Skip to content

Commit e8e61a9

Browse files
committed
SIL: create cond_fail 1 instead of builtin "int_trap" for unconditional failures
`int_trap` doesn't provide a failure message, which makes crash reports hard to understand. This is mostly the case for optimized casts which fail. rdar://97681511
1 parent 48b0dc4 commit e8e61a9

15 files changed

+68
-43
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,11 +2115,11 @@ class SILBuilder {
21152115
Message, getModule()));
21162116
}
21172117

2118-
BuiltinInst *createBuiltinTrap(SILLocation Loc) {
2119-
ASTContext &AST = getASTContext();
2120-
auto Id_trap = AST.getIdentifier("int_trap");
2121-
return createBuiltin(Loc, Id_trap, getModule().Types.getEmptyTupleType(),
2122-
{}, {});
2118+
CondFailInst *createUnconditionalFail(SILLocation loc, StringRef message) {
2119+
Type int1Ty = BuiltinIntegerType::get(1, getASTContext());
2120+
auto int1SILTy = SILType::getPrimitiveObjectType(int1Ty->getCanonicalType());
2121+
auto *one = createIntegerLiteral(loc, int1SILTy, 1);
2122+
return createCondFail(loc, one, message);
21232123
}
21242124

21252125
//===--------------------------------------------------------------------===//

lib/SILGen/SILGenPattern.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,7 +2607,7 @@ static void emitDiagnoseOfUnexpectedEnumCaseValue(SILGenFunction &SGF,
26072607
ASTContext &ctx = SGF.getASTContext();
26082608
auto diagnoseFailure = ctx.getDiagnoseUnexpectedEnumCaseValue();
26092609
if (!diagnoseFailure) {
2610-
SGF.B.createBuiltinTrap(loc);
2610+
SGF.B.createUnconditionalFail(loc, "unexpected enum case");
26112611
return;
26122612
}
26132613

@@ -2642,7 +2642,7 @@ static void emitDiagnoseOfUnexpectedEnumCase(SILGenFunction &SGF,
26422642
ASTContext &ctx = SGF.getASTContext();
26432643
auto diagnoseFailure = ctx.getDiagnoseUnexpectedEnumCase();
26442644
if (!diagnoseFailure) {
2645-
SGF.B.createBuiltinTrap(loc);
2645+
SGF.B.createUnconditionalFail(loc, "unexpected enum case");
26462646
return;
26472647
}
26482648

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,8 @@ SILInstruction *SILCombiner::visitUnconditionalCheckedCastAddrInst(
772772
SILInstruction *
773773
SILCombiner::
774774
visitUnconditionalCheckedCastInst(UnconditionalCheckedCastInst *UCCI) {
775-
if (CastOpt.optimizeUnconditionalCheckedCastInst(UCCI)) {
775+
CastOpt.optimizeUnconditionalCheckedCastInst(UCCI);
776+
if (UCCI->isDeleted()) {
776777
MadeChange = true;
777778
return nullptr;
778779
}

lib/SILOptimizer/UtilityPasses/BugReducerTester.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ class BugReducerTester : public SILFunctionTransform {
101101

102102
// Insert a builtin int trap. Then return F.
103103
SILBuilder B(BB);
104-
B.createBuiltinTrap(RegularLocation::getAutoGeneratedLocation());
104+
B.createUnconditionalFail(RegularLocation::getAutoGeneratedLocation(),
105+
"bug reducer crash");
105106
B.createUnreachable(ArtificialUnreachableLocation());
106107
return F;
107108
}

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ ValueBase *CastOptimizer::optimizeUnconditionalCheckedCastInst(
13541354
// Remove the cast and insert a trap, followed by an
13551355
// unreachable instruction.
13561356
SILBuilderWithScope Builder(Inst, builderContext);
1357-
auto *Trap = Builder.createBuiltinTrap(Loc);
1357+
auto *Trap = Builder.createUnconditionalFail(Loc, "failed cast");
13581358
Inst->replaceAllUsesWithUndef();
13591359
eraseInstAction(Inst);
13601360
Builder.setInsertionPoint(std::next(SILBasicBlock::iterator(Trap)));
@@ -1366,7 +1366,7 @@ ValueBase *CastOptimizer::optimizeUnconditionalCheckedCastInst(
13661366
deleteInstructionsAfterUnreachable(UnreachableInst, Trap);
13671367

13681368
willFailAction();
1369-
return Trap;
1369+
return nullptr;
13701370
}
13711371

13721372
if (Feasibility == DynamicCastFeasibility::WillSucceed) {
@@ -1546,7 +1546,7 @@ SILInstruction *CastOptimizer::optimizeUnconditionalCheckedCastAddrInst(
15461546
// Remove the cast and insert a trap, followed by an
15471547
// unreachable instruction.
15481548
SILBuilderWithScope Builder(Inst, builderContext);
1549-
auto *TrapI = Builder.createBuiltinTrap(Loc);
1549+
auto *TrapI = Builder.createUnconditionalFail(Loc, "failed cast");
15501550
eraseInstAction(Inst);
15511551
Builder.setInsertionPoint(std::next(TrapI->getIterator()));
15521552
auto *UnreachableInst =

test/SILOptimizer/cast_folding.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,8 @@ public class DD : PP {
741741
// contains a trap followed by unreachable
742742
// and no code afterwards.
743743
// CHECK-LABEL: sil {{.*}}@$s12cast_folding7getAsDDyAA0E0CAA2CCCF
744-
// CHECK: builtin "int_trap"
744+
// CHECK: [[ONE:%[0-9]+]] = integer_literal $Builtin.Int1, -1
745+
// CHECK: cond_fail [[ONE]] : $Builtin.Int1, "failed cast"
745746
// CHECK-NEXT: unreachable
746747
// CHECK-NEXT: }
747748
public func getAsDD(_ c: CC) -> DD {
@@ -752,7 +753,8 @@ public func getAsDD(_ c: CC) -> DD {
752753
// contains a trap followed by unreachable
753754
// and no code afterwards.
754755
// CHECK-LABEL: sil {{.*}}@$s12cast_folding7callFooySiAA2CCCF
755-
// CHECK: builtin "int_trap"
756+
// CHECK: [[ONE:%[0-9]+]] = integer_literal $Builtin.Int1, -1
757+
// CHECK: cond_fail [[ONE]] : $Builtin.Int1, "failed cast"
756758
// CHECK-NEXT: unreachable
757759
// CHECK-NEXT: }
758760
public func callFoo(_ c: CC) -> Int {
@@ -768,7 +770,8 @@ func callFooGeneric<T : PP>(_ c: T) -> Int {
768770
// Check that the inlined version of callFooGeneric contains only a trap
769771
// followed by unreachable and no code afterwards
770772
// CHECK-LABEL: sil [noinline] {{.*}}@$s12cast_folding16callForGenericCCyyAA0F0CF
771-
// CHECK: builtin "int_trap"
773+
// CHECK: [[ONE:%[0-9]+]] = integer_literal $Builtin.Int1, -1
774+
// CHECK: cond_fail [[ONE]] : $Builtin.Int1, "failed cast"
772775
// CHECK-NEXT: unreachable
773776
// CHECK-NEXT: }
774777
@inline(never)

test/SILOptimizer/cast_folding_objc.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public func castObjCToSwift<T>(_ t: T) -> Int {
7979

8080
// Check that compiler understands that this cast always fails
8181
// CHECK-LABEL: sil [noinline] {{.*}}@$s17cast_folding_objc37testFailingBridgedCastFromObjCtoSwiftySiSo8NSStringCF
82-
// CHECK: builtin "int_trap"
82+
// CHECK: [[ONE:%[0-9]+]] = integer_literal $Builtin.Int1, -1
83+
// CHECK: cond_fail [[ONE]] : $Builtin.Int1, "failed cast"
8384
// CHECK-NEXT: unreachable
8485
// CHECK-NEXT: }
8586
@inline(never)
@@ -89,7 +90,8 @@ public func testFailingBridgedCastFromObjCtoSwift(_ ns: NSString) -> Int {
8990

9091
// Check that compiler understands that this cast always fails
9192
// CHECK-LABEL: sil [noinline] {{.*}}@$s17cast_folding_objc37testFailingBridgedCastFromSwiftToObjCySiSSF
92-
// CHECK: builtin "int_trap"
93+
// CHECK: [[ONE:%[0-9]+]] = integer_literal $Builtin.Int1, -1
94+
// CHECK: cond_fail [[ONE]] : $Builtin.Int1, "failed cast"
9395
// CHECK-NEXT: unreachable
9496
// CHECK-NEXT: }
9597
@inline(never)
@@ -236,13 +238,15 @@ print("test0=\(test0())")
236238
// CHECK: unconditional_checked_cast_addr
237239

238240
// CHECK-LABEL: sil [noinline] {{.*}}@{{.*}}testCastNSObjectToNonClassType
239-
// CHECK: builtin "int_trap"
241+
// CHECK: [[ONE:%[0-9]+]] = integer_literal $Builtin.Int1, -1
242+
// CHECK: cond_fail [[ONE]] : $Builtin.Int1, "failed cast"
240243

241244
// CHECK-LABEL: sil [noinline] {{.*}}@{{.*}}testCastAnyObjectToEveryType{{.*}}
242245
// CHECK: unconditional_checked_cast_addr
243246

244247
// CHECK-LABEL: sil [noinline] {{.*}}@{{.*}}testCastAnyObjectToNonClassType
245-
// CHECK-NOT: builtin "int_trap"
248+
// CHECK: [[MT:%[0-9]+]] = metatype $@thin Int.Type
249+
// CHECK: return [[MT]]
246250

247251
// CHECK-LABEL: sil [noinline] {{.*}}@{{.*}}testCastAnyToAny2Class{{.*}}
248252
// CHECK: unconditional_checked_cast_addr

test/SILOptimizer/cast_folding_objc_generics.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import objc_generics
77

88
// CHECK-LABEL: sil [noinline] {{.*}}@$s26cast_folding_objc_generics26testObjCGenericParamChangeySo12GenericClassCySo8NSStringCGADySo15NSMutableStringCGF : $@convention(thin) (@guaranteed GenericClass<NSMutableString>) -> @owned GenericClass<NSString> {
99
// CHECK: upcast
10-
// CHECK-NOT: int_trap
10+
// CHECK-NOT: cond_fail
1111
// CHECK: } // end sil function '$s26cast_folding_objc_generics26testObjCGenericParamChangeySo12GenericClassCySo8NSStringCGADySo15NSMutableStringCGF'
1212
@inline(never)
1313
public func testObjCGenericParamChange(_ a: GenericClass<NSMutableString>) -> GenericClass<NSString> {
@@ -16,7 +16,7 @@ public func testObjCGenericParamChange(_ a: GenericClass<NSMutableString>) -> Ge
1616

1717
// CHECK-LABEL: sil [noinline] @$s26cast_folding_objc_generics34testObjCGenericParamChangeSubclassySo07GenericJ0CySo8NSStringCGSo0K5ClassCySo15NSMutableStringCGF : $@convention(thin) (@guaranteed GenericClass<NSMutableString>) -> @owned GenericSubclass<NSString> {
1818
// CHECK: unconditional_checked_cast
19-
// CHECK-NOT: int_trap
19+
// CHECK-NOT: cond_fail
2020
// CHECK: } // end sil function '$s26cast_folding_objc_generics34testObjCGenericParamChangeSubclassySo07GenericJ0CySo8NSStringCGSo0K5ClassCySo15NSMutableStringCGF'
2121
@inline(never)
2222
public func testObjCGenericParamChangeSubclass(_ a: GenericClass<NSMutableString>) -> GenericSubclass<NSString> {
@@ -25,7 +25,7 @@ public func testObjCGenericParamChangeSubclass(_ a: GenericClass<NSMutableString
2525

2626
// CHECK-LABEL: sil [noinline] {{.*}}@$s26cast_folding_objc_generics36testObjCGenericParamChangeSuperclassySo12GenericClassCySo8NSStringCGSo0K8SubclassCySo15NSMutableStringCGF : $@convention(thin) (@guaranteed GenericSubclass<NSMutableString>) -> @owned GenericClass<NSString> {
2727
// CHECK: upcast
28-
// CHECK-NOT: int_trap
28+
// CHECK-NOT: cond_fail
2929
// CHECK: } // end sil function '$s26cast_folding_objc_generics36testObjCGenericParamChangeSuperclassySo12GenericClassCySo8NSStringCGSo0K8SubclassCySo15NSMutableStringCGF'
3030
@inline(never)
3131
public func testObjCGenericParamChangeSuperclass(_ a: GenericSubclass<NSMutableString>) -> GenericClass<NSString> {

test/SILOptimizer/constant_propagation.sil

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ class AnObject {}
690690
// CHECK-LABEL: sil @replace_unconditional_check_cast_failure
691691
// CHECK: bb2:
692692
// CHECK: alloc_stack $AnObject
693-
// CHECK: builtin "int_trap"()
693+
// CHECK: [[ONE:%[0-9]+]] = integer_literal $Builtin.Int1, -1
694+
// CHECK: cond_fail [[ONE]] : $Builtin.Int1, "failed cast"
694695
// CHECK: unreachable
695696

696697
sil @replace_unconditional_check_cast_failure : $@convention(thin) (Builtin.Int1, @guaranteed AnObject) -> (@out Value) {

test/SILOptimizer/constant_propagation_ownership.sil

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,8 @@ class AnSubObject : AnObject {}
857857
// CHECK-LABEL: sil [ossa] @replace_unconditional_check_cast_failure
858858
// CHECK: bb2:
859859
// CHECK: alloc_stack $AnObject
860-
// CHECK: builtin "int_trap"()
860+
// CHECK: [[ONE:%[0-9]+]] = integer_literal $Builtin.Int1, -1
861+
// CHECK: cond_fail [[ONE]] : $Builtin.Int1, "failed cast"
861862
// CHECK: unreachable
862863

863864
sil [ossa] @replace_unconditional_check_cast_failure : $@convention(thin) (Builtin.Int1, @guaranteed AnObject) -> (@out Value) {

0 commit comments

Comments
 (0)