Skip to content

Quick support for calling extern c functions with Builtin vector types. #82225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/AST/ClangTypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,15 @@ ClangTypeConverter::visitBuiltinFloatType(BuiltinFloatType *type) {
llvm_unreachable("cannot translate floating-point format to C");
}

clang::QualType
ClangTypeConverter::visitBuiltinVectorType(BuiltinVectorType *type) {
auto &clangCtx = ClangASTContext;
auto eltTy = visit(type->getElementType());
return clangCtx.getVectorType(
eltTy, type->getNumElements(), clang::VectorKind::Generic
);
}

clang::QualType ClangTypeConverter::visitArchetypeType(ArchetypeType *type) {
// We see these in the case where we invoke an @objc function
// through a protocol.
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ClangTypeConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class ClangTypeConverter :
clang::QualType visitBuiltinRawPointerType(BuiltinRawPointerType *type);
clang::QualType visitBuiltinIntegerType(BuiltinIntegerType *type);
clang::QualType visitBuiltinFloatType(BuiltinFloatType *type);
clang::QualType visitBuiltinVectorType(BuiltinVectorType *type);
clang::QualType visitArchetypeType(ArchetypeType *type);
clang::QualType visitDependentMemberType(DependentMemberType *type);
template <bool templateArgument = false>
Expand Down
29 changes: 29 additions & 0 deletions test/Prototypes/builtin-vector-via-extern-c.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// REQUIRES: swift_feature_Extern
// REQUIRES: swift_feature_BuiltinModule
// RUN: %target-swift-frontend -primary-file %s -enable-experimental-feature BuiltinModule -enable-experimental-feature Extern -emit-ir | %FileCheck %s --check-prefix=CHECK

import Builtin

@_extern(c, "llvm.uadd.sat.v16i8") @usableFromInline
func _uaddSat(_ a: Builtin.Vec16xInt8, _ b: Builtin.Vec16xInt8) -> Builtin.Vec16xInt8

@_transparent
public func saturatingAdd(_ a: SIMD16<UInt8>, _ b: SIMD16<UInt8>) -> SIMD16<UInt8> {
// Hack around init from Builtin type being stdlib-internal using unsafeBitCast.
unsafeBitCast(_uaddSat(a._storage._value, b._storage._value), to: SIMD16<UInt8>.self)
}
// CHECK: saturatingAdd{{.*}} {
// CHECK: entry:
// CHECK: call <16 x i8> @llvm.uadd.sat.v16i8