18
18
// /
19
19
// / 1. We remove calls to Builtin.poundAssert() and Builtin.staticReport(),
20
20
// / which are not needed post SIL.
21
+ // / 2. We transform polymorphic builtins in transparent functions into traps.
21
22
// /
22
23
// ===----------------------------------------------------------------------===//
23
24
@@ -42,28 +43,47 @@ static bool cleanFunction(SILFunction &fn) {
42
43
SILInstruction *inst = &*i;
43
44
++i;
44
45
45
- // Remove calls to Builtin.poundAssert() and Builtin.staticReport().
46
46
auto *bi = dyn_cast<BuiltinInst>(inst);
47
47
if (!bi) {
48
48
continue ;
49
49
}
50
50
51
- switch (bi->getBuiltinInfo ().ID ) {
52
- case BuiltinValueKind::CondFailMessage: {
53
- SILBuilderWithScope Builder (bi);
54
- Builder.createCondFail (bi->getLoc (), bi->getOperand (0 ),
55
- " unknown program error" );
56
- LLVM_FALLTHROUGH;
57
- }
58
- case BuiltinValueKind::PoundAssert:
59
- case BuiltinValueKind::StaticReport:
60
- // The call to the builtin should get removed before we reach
61
- // IRGen.
62
- recursivelyDeleteTriviallyDeadInstructions (bi, /* Force */ true );
63
- madeChange = true ;
64
- break ;
65
- default :
66
- break ;
51
+ auto kind = bi->getBuiltinKind ();
52
+ if (!kind) {
53
+ continue ;
54
+ }
55
+
56
+ // Transform polymorphic builtins into int_trap.
57
+ if (isPolymorphicBuiltin (*kind)) {
58
+ assert (bi->getFunction ()->isTransparent () == IsTransparent &&
59
+ " Should only see these in transparent functions. If these were "
60
+ " mandatory inlined into a caller, we should either have emitted "
61
+ " a call to the static overload or emitted a diagnostic" );
62
+ // Replace all uses with undef since we are going to trap. Any such uses
63
+ // are now unreachable along a path.
64
+ bi->replaceAllUsesWithUndef ();
65
+ SILBuilderWithScope (bi).createBuiltinTrap (bi->getLoc ());
66
+ bi->eraseFromParent ();
67
+ madeChange = true ;
68
+ continue ;
69
+ }
70
+
71
+ switch (*kind) {
72
+ case BuiltinValueKind::CondFailMessage: {
73
+ SILBuilderWithScope Builder (bi);
74
+ Builder.createCondFail (bi->getLoc (), bi->getOperand (0 ),
75
+ " unknown program error" );
76
+ LLVM_FALLTHROUGH;
77
+ }
78
+ case BuiltinValueKind::PoundAssert:
79
+ case BuiltinValueKind::StaticReport:
80
+ // The call to the builtin should get removed before we reach
81
+ // IRGen.
82
+ recursivelyDeleteTriviallyDeadInstructions (bi, /* Force */ true );
83
+ madeChange = true ;
84
+ break ;
85
+ default :
86
+ break ;
67
87
}
68
88
}
69
89
}
0 commit comments