Skip to content

Commit 0e3bca9

Browse files
committed
[debug-var] When comparing address debug_value against value debug_value remove the diexpr from the address SILDebugVariable.
Otherwise they will never compare the same. Also restore the test that was updated for the previous commit to its old state. I did this to ensure that each commit would compile successfully and so that I could show the test associated with this commit.
1 parent ae8bc91 commit 0e3bca9

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

include/swift/SIL/SILDebugVariable.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ struct SILDebugVariable {
6262
Scope == V.Scope && DIExpr == V.DIExpr;
6363
}
6464

65+
SILDebugVariable withoutDIExpr() const {
66+
auto result = *this;
67+
result.DIExpr = {};
68+
return result;
69+
}
70+
6571
bool isLet() const { return Name.size() && Constant; }
6672

6773
bool isVar() const { return Name.size() && !Constant; }

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,30 +167,36 @@ static void promoteDebugValueAddr(DebugValueInst *dvai, SILValue value,
167167
assert(dvai->getOperand()->getType().isLoadable(*dvai->getFunction()) &&
168168
"Unexpected promotion of address-only type!");
169169
assert(value && "Expected valid value");
170+
170171
// Avoid inserting the same debug_value twice.
172+
//
173+
// We remove the di expression when comparing since:
174+
//
175+
// 1. dvai is on will always have the deref diexpr since it is on addresses.
176+
//
177+
// 2. We are only trying to delete debug_var that are on values... values will
178+
// never have an op_deref meaning that the comparison will always fail and
179+
// not serve out purpose here.
180+
auto dvaiWithoutDIExpr = dvai->getVarInfo()->withoutDIExpr();
171181
for (auto *use : value->getUses()) {
172182
if (auto *dvi = dyn_cast<DebugValueInst>(use->getUser())) {
173-
// Since we're not comparing di-expression in
174-
// SILDebugVariable::operator==(), it's necessary to distinguish
175-
// debug_value w/ normal values from that with address-type values.
176-
if (!dvi->hasAddrVal() &&
177-
*dvi->getVarInfo() == *dvai->getVarInfo()) {
183+
if (!dvi->hasAddrVal() && *dvi->getVarInfo() == dvaiWithoutDIExpr) {
178184
deleter.forceDelete(dvai);
179185
return;
180186
}
181187
}
182188
}
183189

184-
auto VarInfo = *dvai->getVarInfo();
185190
// Drop op_deref if dvai is actually a debug_value instruction
191+
auto varInfo = *dvai->getVarInfo();
186192
if (isa<DebugValueInst>(dvai)) {
187-
auto &DIExpr = VarInfo.DIExpr;
188-
if (DIExpr)
189-
DIExpr.eraseElement(DIExpr.element_begin());
193+
auto &diExpr = varInfo.DIExpr;
194+
if (diExpr)
195+
diExpr.eraseElement(diExpr.element_begin());
190196
}
191197

192198
SILBuilderWithScope b(dvai, ctx);
193-
b.createDebugValue(dvai->getLoc(), value, std::move(VarInfo));
199+
b.createDebugValue(dvai->getLoc(), value, std::move(varInfo));
194200
deleter.forceDelete(dvai);
195201
}
196202

test/SILOptimizer/specialize_unconditional_checked_cast.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,9 @@ ArchetypeToConcreteConvertUInt8(t: c)
9797
ArchetypeToConcreteConvertUInt8(t: f)
9898

9999
// x -> x where x is not a class.
100-
//
101-
// TODO: Why is the optimizer cloning twice here.
102-
//
103100
// CHECK-LABEL: sil shared [noinline] @$s37specialize_unconditional_checked_cast31ArchetypeToConcreteConvertUInt8{{[_0-9a-zA-Z]*}}3Not{{.*}}Tg5 : $@convention(thin) (NotUInt8) -> NotUInt8 {
104101
// CHECK: bb0
105102
// CHECK-NEXT: debug_value %0
106-
// CHECK-NEXT: debug_value %0
107103
// CHECK-NEXT: return %0
108104

109105
// x -> y where y is a class but x is not.
@@ -124,7 +120,6 @@ ArchetypeToConcreteConvertUInt8(t: f)
124120
// CHECK-LABEL: sil shared [noinline] @$s37specialize_unconditional_checked_cast27ArchetypeToConcreteConvertC{{[_0-9a-zA-Z]*}}Tg5 : $@convention(thin) (@guaranteed C) -> @owned C {
125121
// CHECK: bb0([[ARG:%.*]] : $C)
126122
// CHECK-NEXT: debug_value [[ARG]]
127-
// CHECK-NEXT: debug_value [[ARG]]
128123
// CHECK-NEXT: strong_retain [[ARG]]
129124
// CHECK-NEXT: return [[ARG]]
130125

0 commit comments

Comments
 (0)