Skip to content

[polymorphic-builtin] Rather than asserting in IRGen if we see a buil… #27302

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
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
11 changes: 8 additions & 3 deletions lib/IRGen/GenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,14 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
return out.add(v); \
}
#define BUILTIN_BINARY_OPERATION_POLYMORPHIC(id, name, attrs) \
assert(Builtin.ID != BuiltinValueKind::id && \
"This builtin should never be seen by IRGen. It is invalid in " \
"Lowered sil");
if (Builtin.ID == BuiltinValueKind::id) { \
/* This builtin must be guarded so that dynamically it is never called. */ \
IGF.emitTrap("invalid use of polymorphic builtin", /*Unreachable*/ false); \
auto returnValue = llvm::UndefValue::get(IGF.IGM.Int8PtrTy); \
/* Consume the arguments of the builtin. */ \
(void)args.claimAll(); \
return out.add(returnValue); \
}

#define BUILTIN_RUNTIME_CALL(id, name, attrs) \
if (Builtin.ID == BuiltinValueKind::id) { \
Expand Down
4 changes: 0 additions & 4 deletions lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,10 +1654,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
verifyLLVMIntrinsic(BI, BI->getIntrinsicInfo().ID);
return;
}

require(BI->getModule().getStage() != SILStage::Lowered ||
!isPolymorphicBuiltin(*BI->getBuiltinKind()),
"Polymorphic builtins are illegal in lowered SIL?!");
}

void checkFunctionRefBaseInst(FunctionRefBaseInst *FRI) {
Expand Down
50 changes: 50 additions & 0 deletions test/IRGen/polymorphic_builtins.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// This line tests that IRGen is properly turning the unspecialized builtins
// into traps.
//
// RUN: %target-swift-frontend -emit-ir -parse-as-library -parse-stdlib %s | %FileCheck %s

// Make sure we are not eliminating these builtins before IRGen runs. As part of
// the builtin's contract, we expect IRGen to convert them to traps, not
// anything before.
//
// RUN: %target-swift-frontend -emit-sil -parse-as-library -parse-stdlib %s | %FileCheck --check-prefix=SIL %s

import Swift

// SIL-LABEL: sil [transparent] [serialized] @$s20polymorphic_builtins11_isConcrete4typeSbxm_tlF : $@convention(thin) <T> (@thick T.Type) -> Bool {
// SIL: builtin "isConcrete"<T>({{%[0-9]*}} : $@thick T.Type) : $Builtin.Int1
// SIL: // end sil function '$s20polymorphic_builtins11_isConcrete4typeSbxm_tlF'
@_transparent
public func _isConcrete<T>(type: T.Type) -> Bool {
return Bool(_builtinBooleanLiteral: Builtin.isConcrete(type))
}

// SIL-LABEL: sil [transparent] [serialized] @$s20polymorphic_builtins41calleeAddVectorsGenericTransparentGuardedyxx_xtlF : $@convention(thin) <T> (@in_guaranteed T, @in_guaranteed T) -> @out T {
// SIL: builtin "isConcrete"<T>({{%[0-9]*}} : $@thick T.Type) : $Builtin.Int1
// SIL: builtin "generic_add"<T>(
// SIL: } // end sil function '$s20polymorphic_builtins41calleeAddVectorsGenericTransparentGuardedyxx_xtlF'

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc void @"$s20polymorphic_builtins41calleeAddVectorsGenericTransparentGuardedyxx_xtlF"(
// CHECK: br i1 false, label %[[CONCRETE_LABEL:[0-9][0-9]*]], label %[[NON_CONCRETE_LABEL:[0-9][0-9]*]]
//
// CHECK: <label>:[[CONCRETE_LABEL]]:
// CHECK: call void @llvm.trap()
// CHECK: br label %[[EPILOGUE_BLOCK:[0-9][0-9]*]]
//
// CHECK: <label>:[[NON_CONCRETE_LABEL]]
// CHECK: br label %[[EPILOGUE_BLOCK]]
///
// CHECK: <label>:[[EPILOGUE_BLOCK]]:
// CHECK: ret void
// CHECK-NEXT: }
@_transparent
public func calleeAddVectorsGenericTransparentGuarded<T>(_ lhs: T, _ rhs: T) -> T {
if _isConcrete(T.self) {
return Builtin.generic_add(lhs, rhs)
}
return lhs
}

public func callerAddVectorsGenericTransparent(_ lhs: Builtin.Vec4xInt32, _ rhs: Builtin.Vec4xInt32) -> Builtin.Vec4xInt32 {
return calleeAddVectorsGenericTransparentGuarded(lhs, rhs)
}
Loading