Skip to content

Commit c3d304c

Browse files
authored
Merge pull request #27302 from gottesmm/pr-7a861faa03ec80b01ad9a212afe79e529fb6857e
2 parents f086b9b + b9046c6 commit c3d304c

File tree

5 files changed

+597
-7
lines changed

5 files changed

+597
-7
lines changed

lib/IRGen/GenBuiltin.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,14 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
314314
return out.add(v); \
315315
}
316316
#define BUILTIN_BINARY_OPERATION_POLYMORPHIC(id, name, attrs) \
317-
assert(Builtin.ID != BuiltinValueKind::id && \
318-
"This builtin should never be seen by IRGen. It is invalid in " \
319-
"Lowered sil");
317+
if (Builtin.ID == BuiltinValueKind::id) { \
318+
/* This builtin must be guarded so that dynamically it is never called. */ \
319+
IGF.emitTrap("invalid use of polymorphic builtin", /*Unreachable*/ false); \
320+
auto returnValue = llvm::UndefValue::get(IGF.IGM.Int8PtrTy); \
321+
/* Consume the arguments of the builtin. */ \
322+
(void)args.claimAll(); \
323+
return out.add(returnValue); \
324+
}
320325

321326
#define BUILTIN_RUNTIME_CALL(id, name, attrs) \
322327
if (Builtin.ID == BuiltinValueKind::id) { \

lib/SIL/SILVerifier.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,10 +1654,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
16541654
verifyLLVMIntrinsic(BI, BI->getIntrinsicInfo().ID);
16551655
return;
16561656
}
1657-
1658-
require(BI->getModule().getStage() != SILStage::Lowered ||
1659-
!isPolymorphicBuiltin(*BI->getBuiltinKind()),
1660-
"Polymorphic builtins are illegal in lowered SIL?!");
16611657
}
16621658

16631659
void checkFunctionRefBaseInst(FunctionRefBaseInst *FRI) {

test/IRGen/polymorphic_builtins.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This line tests that IRGen is properly turning the unspecialized builtins
2+
// into traps.
3+
//
4+
// RUN: %target-swift-frontend -emit-ir -parse-as-library -parse-stdlib %s | %FileCheck %s
5+
6+
// Make sure we are not eliminating these builtins before IRGen runs. As part of
7+
// the builtin's contract, we expect IRGen to convert them to traps, not
8+
// anything before.
9+
//
10+
// RUN: %target-swift-frontend -emit-sil -parse-as-library -parse-stdlib %s | %FileCheck --check-prefix=SIL %s
11+
12+
import Swift
13+
14+
// SIL-LABEL: sil [transparent] [serialized] @$s20polymorphic_builtins11_isConcrete4typeSbxm_tlF : $@convention(thin) <T> (@thick T.Type) -> Bool {
15+
// SIL: builtin "isConcrete"<T>({{%[0-9]*}} : $@thick T.Type) : $Builtin.Int1
16+
// SIL: // end sil function '$s20polymorphic_builtins11_isConcrete4typeSbxm_tlF'
17+
@_transparent
18+
public func _isConcrete<T>(type: T.Type) -> Bool {
19+
return Bool(_builtinBooleanLiteral: Builtin.isConcrete(type))
20+
}
21+
22+
// SIL-LABEL: sil [transparent] [serialized] @$s20polymorphic_builtins41calleeAddVectorsGenericTransparentGuardedyxx_xtlF : $@convention(thin) <T> (@in_guaranteed T, @in_guaranteed T) -> @out T {
23+
// SIL: builtin "isConcrete"<T>({{%[0-9]*}} : $@thick T.Type) : $Builtin.Int1
24+
// SIL: builtin "generic_add"<T>(
25+
// SIL: } // end sil function '$s20polymorphic_builtins41calleeAddVectorsGenericTransparentGuardedyxx_xtlF'
26+
27+
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc void @"$s20polymorphic_builtins41calleeAddVectorsGenericTransparentGuardedyxx_xtlF"(
28+
// CHECK: br i1 false, label %[[CONCRETE_LABEL:[0-9][0-9]*]], label %[[NON_CONCRETE_LABEL:[0-9][0-9]*]]
29+
//
30+
// CHECK: <label>:[[CONCRETE_LABEL]]:
31+
// CHECK: call void @llvm.trap()
32+
// CHECK: br label %[[EPILOGUE_BLOCK:[0-9][0-9]*]]
33+
//
34+
// CHECK: <label>:[[NON_CONCRETE_LABEL]]
35+
// CHECK: br label %[[EPILOGUE_BLOCK]]
36+
///
37+
// CHECK: <label>:[[EPILOGUE_BLOCK]]:
38+
// CHECK: ret void
39+
// CHECK-NEXT: }
40+
@_transparent
41+
public func calleeAddVectorsGenericTransparentGuarded<T>(_ lhs: T, _ rhs: T) -> T {
42+
if _isConcrete(T.self) {
43+
return Builtin.generic_add(lhs, rhs)
44+
}
45+
return lhs
46+
}
47+
48+
public func callerAddVectorsGenericTransparent(_ lhs: Builtin.Vec4xInt32, _ rhs: Builtin.Vec4xInt32) -> Builtin.Vec4xInt32 {
49+
return calleeAddVectorsGenericTransparentGuarded(lhs, rhs)
50+
}

0 commit comments

Comments
 (0)