Skip to content

Commit 1e7dc51

Browse files
committed
[DebugInfo] Add salvageDebugInfo support for tuples
1 parent efa0908 commit 1e7dc51

File tree

3 files changed

+80
-18
lines changed

3 files changed

+80
-18
lines changed

Diff for: lib/SILOptimizer/Utils/InstOptUtils.cpp

+27-2
Original file line numberDiff line numberDiff line change
@@ -1844,8 +1844,8 @@ void swift::salvageDebugInfo(SILInstruction *I) {
18441844
}
18451845
// If a `struct` SIL instruction is "unwrapped" and removed,
18461846
// for instance, in favor of using its enclosed value directly,
1847-
// we need to make sure any of its related `debug_value` instruction
1848-
// is preserved.
1847+
// we need to make sure any of its related `debug_value` instructions
1848+
// are preserved.
18491849
if (auto *STI = dyn_cast<StructInst>(I)) {
18501850
auto STVal = STI->getResult(0);
18511851
llvm::ArrayRef<VarDecl *> FieldDecls =
@@ -1871,6 +1871,31 @@ void swift::salvageDebugInfo(SILInstruction *I) {
18711871
}
18721872
}
18731873
}
1874+
// Similarly, if a `tuple` SIL instruction is "unwrapped" and removed,
1875+
// we need to make sure any of its related `debug_value` instructions
1876+
// are preserved.
1877+
if (auto *TTI = dyn_cast<TupleInst>(I)) {
1878+
auto TTVal = TTI->getResult(0);
1879+
for (Operand *U : getDebugUses(TTVal)) {
1880+
auto *DbgInst = cast<DebugValueInst>(U->getUser());
1881+
auto VarInfo = DbgInst->getVarInfo();
1882+
if (!VarInfo)
1883+
continue;
1884+
TupleType *TT = TTI->getTupleType();
1885+
for (auto i : indices(TT->getElements())) {
1886+
SILDebugVariable NewVarInfo = *VarInfo;
1887+
auto FragDIExpr = SILDebugInfoExpression::createTupleFragment(TT, i);
1888+
NewVarInfo.DIExpr.append(FragDIExpr);
1889+
1890+
if (!NewVarInfo.Type)
1891+
NewVarInfo.Type = TTI->getType();
1892+
1893+
// Create a new debug_value
1894+
SILBuilder(TTI, DbgInst->getDebugScope())
1895+
.createDebugValue(DbgInst->getLoc(), TTI->getElement(i), NewVarInfo);
1896+
}
1897+
}
1898+
}
18741899

18751900
if (auto *IA = dyn_cast<IndexAddrInst>(I)) {
18761901
if (IA->getBase() && IA->getIndex())

Diff for: test/DebugInfo/constant_propagation.sil

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
11
// RUN: %target-sil-opt -enable-sil-verify-all -sil-print-debuginfo -diagnostic-constant-propagation %s | %FileCheck %s
2-
// REQUIRES: CPU=arm64 || CPU=x86_64
32

43
sil_stage canonical
54

65
import Builtin
76
import Swift
87
import SwiftShims
98

10-
func foo(x: Int, y: Int) -> Int
11-
12-
sil_scope 1 { loc "file.swift":1:6 parent @foo : $@convention(thin) (Int, Int) -> Int }
9+
sil_scope 1 { loc "file.swift":1:6 parent @foo : $@convention(thin) (Int64, Int64) -> Int64 }
1310

1411
// Test if debug_value got preserved when %16 is removed in favor of directly using %13
1512
// CHECK-LABEL: sil {{.*}} @foo
16-
sil hidden @foo : $@convention(thin) (Int, Int) -> Int {
17-
bb0(%0 : $Int, %1 : $Int):
13+
sil hidden @foo : $@convention(thin) (Int64, Int64) -> Int64 {
14+
bb0(%0 : $Int64, %1 : $Int64):
1815
%4 = integer_literal $Builtin.Int64, 87, loc "file.swift":2:17, scope 1
19-
%9 = struct_extract %0 : $Int, #Int._value, loc "file.swift":2:15, scope 1
16+
%9 = struct_extract %0 : $Int64, #Int64._value, loc "file.swift":2:15, scope 1
2017
%11 = integer_literal $Builtin.Int1, -1, loc "file.swift":2:15, scope 1
2118
// CHECK: %[[ADD:.+]] = builtin "sadd_with_overflow
2219
%12 = builtin "sadd_with_overflow_Int64"(%9 : $Builtin.Int64, %4 : $Builtin.Int64, %11 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1), loc "file.swift":2:15, scope 1
2320
// CHECK: (%[[RESULT:.+]], %{{.*}}) = destructure_tuple %[[ADD]]
2421
(%13, %14) = destructure_tuple %12 : $(Builtin.Int64, Builtin.Int1), loc "file.swift":2:15, scope 1
25-
%16 = struct $Int (%13 : $Builtin.Int64), loc "file.swift":2:15, scope 1
22+
%16 = struct $Int64 (%13 : $Builtin.Int64), loc "file.swift":2:15, scope 1
2623
// In addition to checking if `op_fragment` is generated, we're also checking if "z"'s declared
2724
// source location, as well as `debug_value`'s instruction source location are preserved.
28-
// CHECK: debug_value %[[RESULT]] : $Builtin.Int{{[0-9]+}}, let, name "z"
29-
// CHECK-SAME: type $Int
30-
// CHECK-SAME: expr op_fragment:#Int._value
25+
// CHECK: debug_value %[[RESULT]] : $Builtin.Int64, let, name "z"
26+
// CHECK-SAME: type $Int64
27+
// CHECK-SAME: expr op_fragment:#Int64._value
3128
// CHECK-SAME: loc "file.swift":2:9, scope 1
32-
debug_value %16 : $Int, let, name "z", loc "file.swift":2:9, scope 1
33-
%19 = struct_extract %16 : $Int, #Int._value, loc "file.swift":3:14, scope 1
34-
%20 = struct_extract %1 : $Int, #Int._value, loc "file.swift":3:14, scope 1
29+
debug_value %16 : $Int64, let, name "z", loc "file.swift":2:9, scope 1
30+
%19 = struct_extract %16 : $Int64, #Int64._value, loc "file.swift":3:14, scope 1
31+
%20 = struct_extract %1 : $Int64, #Int64._value, loc "file.swift":3:14, scope 1
3532
%21 = integer_literal $Builtin.Int1, -1, loc "file.swift":3:14, scope 1
3633
%22 = builtin "sadd_with_overflow_Int64"(%19 : $Builtin.Int64, %20 : $Builtin.Int64, %21 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1), loc "file.swift":3:14, scope 1
3734
(%23, %24) = destructure_tuple %22 : $(Builtin.Int64, Builtin.Int1), loc "file.swift":3:14, scope 1
38-
%26 = struct $Int (%23 : $Builtin.Int64), loc "file.swift":3:14, scope 1
39-
return %26 : $Int, loc "file.swift":3:5, scope 1
35+
%26 = struct $Int64 (%23 : $Builtin.Int64), loc "file.swift":3:14, scope 1
36+
return %26 : $Int64, loc "file.swift":3:5, scope 1
4037
} // end sil function 'foo'
4138

39+
sil_scope 2 { loc "file.swift":1:6 parent @bar : $@convention(thin) (Int64, Int64) -> Int64 }
40+
41+
// Test if debug_value got preserved when %5 is folded into %0, and %3 removed
42+
// CHECK-LABEL: sil {{.*}} @bar
43+
sil hidden @bar : $@convention(thin) (Int64, Int64) -> Int64 {
44+
bb0(%0 : $Int64, %1 : $Int64):
45+
%3 = tuple $(low: Int64, high: Int64) (%0, %1), loc "file.swift":14:5, scope 2
46+
// CHECK: debug_value %0 : $Int64, let, name "newValue"
47+
// CHECK-SAME: type $(low: Int64, high: Int64)
48+
// CHECK-SAME: expr op_tuple_fragment:$(low: Int64, high: Int64):0
49+
// CHECK-SAME: "file.swift":14:5, scope 2
50+
// CHECK: debug_value %1 : $Int64, let, name "newValue"
51+
// CHECK-SAME: type $(low: Int64, high: Int64)
52+
// CHECK-SAME: expr op_tuple_fragment:$(low: Int64, high: Int64):1
53+
// CHECK-SAME: "file.swift":14:5, scope 2
54+
debug_value %3 : $(low: Int64, high: Int64), let, name "newValue", argno 1, loc "file.swift":14:5, scope 2
55+
(%5, %6) = destructure_tuple %3 : $(low: Int64, high: Int64), loc "file.swift":15:5, scope 2
56+
return %5 : $Int64, loc "file.swift":15:5, scope 2
57+
} // end sil function 'bar'

Diff for: test/DebugInfo/property-setter-implicit-tuple.swift

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %target-swift-frontend %s -emit-ir -g -o - | %FileCheck %s
2+
3+
// This is a test for rdar://125939953 (Implicit variables are removed at Onone)
4+
5+
struct UInt128 {
6+
var low: UInt64
7+
var high: UInt64
8+
9+
var components: (low: UInt64, high: UInt64) {
10+
get {
11+
return (low, high)
12+
}
13+
// CHECK-LABEL: define {{.+}} @"$s4main7UInt128V10componentss6UInt64V3low_AF4hightvs"
14+
set {
15+
// CHECK: call void @llvm.dbg.declare(metadata ptr {{.+}}, metadata ![[NEW_VALUE:[0-9]+]]
16+
(self.low, self.high) = (newValue.high, newValue.low)
17+
}
18+
}
19+
}
20+
21+
//CHECK: ![[NEW_VALUE]] = !DILocalVariable(name: "newValue", arg: 1

0 commit comments

Comments
 (0)