Skip to content

Commit 1e02b3b

Browse files
Quick support for calling extern c functions with Builtin vector types. (#82225)
This gives us a means to use llvm's intrinsics that implement more niche SIMD instructions from the standard library, where we cannot use the C intrinsics headers from clang (because they're in the cpp module).
1 parent 76df55d commit 1e02b3b

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lib/AST/ClangTypeConverter.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,15 @@ ClangTypeConverter::visitBuiltinFloatType(BuiltinFloatType *type) {
813813
llvm_unreachable("cannot translate floating-point format to C");
814814
}
815815

816+
clang::QualType
817+
ClangTypeConverter::visitBuiltinVectorType(BuiltinVectorType *type) {
818+
auto &clangCtx = ClangASTContext;
819+
auto eltTy = visit(type->getElementType());
820+
return clangCtx.getVectorType(
821+
eltTy, type->getNumElements(), clang::VectorKind::Generic
822+
);
823+
}
824+
816825
clang::QualType ClangTypeConverter::visitArchetypeType(ArchetypeType *type) {
817826
// We see these in the case where we invoke an @objc function
818827
// through a protocol.

lib/AST/ClangTypeConverter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class ClangTypeConverter :
157157
clang::QualType visitBuiltinRawPointerType(BuiltinRawPointerType *type);
158158
clang::QualType visitBuiltinIntegerType(BuiltinIntegerType *type);
159159
clang::QualType visitBuiltinFloatType(BuiltinFloatType *type);
160+
clang::QualType visitBuiltinVectorType(BuiltinVectorType *type);
160161
clang::QualType visitArchetypeType(ArchetypeType *type);
161162
clang::QualType visitDependentMemberType(DependentMemberType *type);
162163
template <bool templateArgument = false>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----------------------------------------------------------*- swift -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
// REQUIRES: swift_feature_Extern
13+
// REQUIRES: swift_feature_BuiltinModule
14+
// RUN: %target-swift-frontend -primary-file %s -enable-experimental-feature BuiltinModule -enable-experimental-feature Extern -emit-ir | %FileCheck %s --check-prefix=CHECK
15+
16+
import Builtin
17+
18+
@_extern(c, "llvm.uadd.sat.v16i8") @usableFromInline
19+
func _uaddSat(_ a: Builtin.Vec16xInt8, _ b: Builtin.Vec16xInt8) -> Builtin.Vec16xInt8
20+
21+
@_transparent
22+
public func saturatingAdd(_ a: SIMD16<UInt8>, _ b: SIMD16<UInt8>) -> SIMD16<UInt8> {
23+
// Hack around init from Builtin type being stdlib-internal using unsafeBitCast.
24+
unsafeBitCast(_uaddSat(a._storage._value, b._storage._value), to: SIMD16<UInt8>.self)
25+
}
26+
// CHECK: saturatingAdd{{.*}} {
27+
// CHECK: entry:
28+
// CHECK: call <16 x i8> @llvm.uadd.sat.v16i8
29+

0 commit comments

Comments
 (0)