Skip to content

Commit ec645e3

Browse files
authored
Merge pull request #60540 from adrian-prantl/97617367
SILInliner: Fix scope & source location of terminator instructions.
2 parents feecc51 + 742328d commit ec645e3

File tree

2 files changed

+93
-12
lines changed

2 files changed

+93
-12
lines changed

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -547,50 +547,56 @@ void SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
547547
}
548548

549549
void SILInlineCloner::visitTerminator(SILBasicBlock *BB) {
550+
TermInst *Terminator = BB->getTerminator();
550551
// Coroutine terminators need special handling.
551552
if (BeginApply) {
552553
if (BeginApply->processTerminator(
553-
BB->getTerminator(), ReturnToBB,
554+
Terminator, ReturnToBB,
554555
[=](SILBasicBlock *Block) -> SILBasicBlock * {
555556
return this->remapBasicBlock(Block);
556557
},
557-
[=](SILValue Val) -> SILValue { return this->getMappedValue(Val); }))
558+
[=](SILValue Val) -> SILValue {
559+
return this->getMappedValue(Val);
560+
}))
558561
return;
559562
}
560563

561-
// Modify return terminators to branch to the return-to BB, rather than
562-
// trying to clone the ReturnInst.
563-
if (auto *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
564+
// Modify return terminators to branch to the return-to BB, rather
565+
// than trying to clone the ReturnInst. Because of that, the scope
566+
// needs to be remapped manually.
567+
getBuilder().setCurrentDebugScope(getOpScope(Terminator->getDebugScope()));
568+
if (auto *RI = dyn_cast<ReturnInst>(Terminator)) {
564569
auto returnedValue = getMappedValue(RI->getOperand());
565-
getBuilder().createBranch(Loc.getValue(), ReturnToBB, returnedValue);
570+
getBuilder().createBranch(getOpLocation(RI->getLoc()), ReturnToBB,
571+
returnedValue);
566572
return;
567573
}
568574

569575
// Modify throw terminators to branch to the error-return BB, rather than
570576
// trying to clone the ThrowInst.
571-
if (auto *TI = dyn_cast<ThrowInst>(BB->getTerminator())) {
577+
if (auto *TI = dyn_cast<ThrowInst>(Terminator)) {
578+
SILLocation Loc = getOpLocation(TI->getLoc());
572579
switch (Apply.getKind()) {
573580
case FullApplySiteKind::ApplyInst:
574581
assert(cast<ApplyInst>(Apply)->isNonThrowing()
575582
&& "apply of a function with error result must be non-throwing");
576-
getBuilder().createUnreachable(Loc.getValue());
583+
getBuilder().createUnreachable(Loc);
577584
return;
578585
case FullApplySiteKind::BeginApplyInst:
579586
assert(cast<BeginApplyInst>(Apply)->isNonThrowing()
580587
&& "apply of a function with error result must be non-throwing");
581-
getBuilder().createUnreachable(Loc.getValue());
588+
getBuilder().createUnreachable(Loc);
582589
return;
583590
case FullApplySiteKind::TryApplyInst:
584591
auto tryAI = cast<TryApplyInst>(Apply);
585592
auto returnedValue = getMappedValue(TI->getOperand());
586-
getBuilder().createBranch(Loc.getValue(), tryAI->getErrorBB(),
587-
returnedValue);
593+
getBuilder().createBranch(Loc, tryAI->getErrorBB(), returnedValue);
588594
return;
589595
}
590596
}
591597
// Otherwise use normal visitor, which clones the existing instruction
592598
// but remaps basic blocks and values.
593-
visit(BB->getTerminator());
599+
visit(Terminator);
594600
}
595601

596602
void SILInlineCloner::preFixUp(SILFunction *calleeFunction) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -inline -sil-print-debuginfo | %FileCheck %s
2+
3+
// Generated from:
4+
// func g(_ x: Int) -> Int {
5+
// if (x > 0) {
6+
// return 1
7+
// }
8+
// return 0
9+
// }
10+
//
11+
// public func f(_ x: Int) -> Int { return g(x) }
12+
13+
// CHECK: sil @$s1a1fyS2iF : $@convention(thin) (Int) -> Int {
14+
// CHECK: bb0(%0 : $Int):
15+
// CHECK-NEXT: scope [[SCOPE_10:[0-9]+]]
16+
// CHECK: cond_br {{.*}}, bb1, bb2, loc "t.swift":3:9, scope [[SCOPE_10]]
17+
// CHECK: bb1:
18+
// CHECK-NEXT: scope [[SCOPE_11:[0-9]+]]
19+
// CHECK: br bb3({{.*}} : $Int), loc "t.swift":4:5, scope [[SCOPE_11]]
20+
// CHECK: bb2:
21+
// CHECK-NEXT: scope [[SCOPE_9:[0-9]+]]
22+
// CHECK: br bb3({{.*}} : $Int), loc "t.swift":6:3, scope [[SCOPE_9]]
23+
// CHECK: bb3({{.*}} : $Int):
24+
25+
sil_stage canonical
26+
27+
import Builtin
28+
import Swift
29+
import SwiftShims
30+
31+
func g(_ x: Int) -> Int
32+
33+
public func f(_ x: Int) -> Int
34+
35+
sil_scope 1 { loc "t.swift":2:6 parent @$s1a1gyS2iF : $@convention(thin) (Int) -> Int }
36+
sil_scope 2 { loc "t.swift":2:25 parent 1 }
37+
sil_scope 3 { loc "t.swift":3:3 parent 2 }
38+
sil_scope 4 { loc "t.swift":3:14 parent 3 }
39+
40+
// g(_:)
41+
sil hidden @$s1a1gyS2iF : $@convention(thin) (Int) -> Int {
42+
// %0 "x" // users: %3, %1
43+
bb0(%0 : $Int):
44+
%2 = integer_literal $Builtin.Int64, 0, loc "t.swift":3:11, scope 3 // user: %4
45+
%3 = struct_extract %0 : $Int, #Int._value, loc "t.swift":3:9, scope 3 // user: %4
46+
%4 = builtin "cmp_slt_Int64"(%2 : $Builtin.Int64, %3 : $Builtin.Int64) : $Builtin.Int1, loc "t.swift":3:9, scope 3 // user: %5
47+
cond_br %4, bb1, bb2, loc "t.swift":3:9, scope 3 // id: %5
48+
49+
bb1: // Preds: bb0
50+
%6 = integer_literal $Builtin.Int64, 1, loc "t.swift":4:12, scope 4 // user: %7
51+
%7 = struct $Int (%6 : $Builtin.Int64), loc "t.swift":4:12, scope 4 // user: %8
52+
br bb3(%7 : $Int), loc "t.swift":4:5, scope 4 // id: %8
53+
54+
bb2: // Preds: bb0
55+
%9 = integer_literal $Builtin.Int64, 0, loc "t.swift":6:10, scope 2 // user: %10
56+
%10 = struct $Int (%9 : $Builtin.Int64), loc "t.swift":6:10, scope 2 // user: %11
57+
br bb3(%10 : $Int), loc "t.swift":6:3, scope 2 // id: %11
58+
59+
// %12 // user: %13
60+
bb3(%12 : $Int): // Preds: bb2 bb1
61+
return %12 : $Int, loc "t.swift":7:1, scope 2 // id: %13
62+
} // end sil function '$s1a1gyS2iF'
63+
64+
sil_scope 7 { loc "t.swift":9:13 parent @$s1a1fyS2iF : $@convention(thin) (Int) -> Int }
65+
sil_scope 8 { loc "t.swift":9:32 parent 7 }
66+
67+
// f(_:)
68+
sil @$s1a1fyS2iF : $@convention(thin) (Int) -> Int {
69+
// %0 "x" // users: %3, %1
70+
bb0(%0 : $Int):
71+
// function_ref g(_:)
72+
%2 = function_ref @$s1a1gyS2iF : $@convention(thin) (Int) -> Int, loc "t.swift":9:41, scope 8 // user: %3
73+
%3 = apply %2(%0) : $@convention(thin) (Int) -> Int, loc "t.swift":9:41, scope 8 // user: %4
74+
return %3 : $Int, loc "t.swift":9:34, scope 8 // id: %4
75+
} // end sil function '$s1a1fyS2iF'

0 commit comments

Comments
 (0)