Skip to content

Commit c3390ec

Browse files
authored
Merge pull request #38736 from mshockwave/dev-sil-diexpr-optimize
[SILOptimizer][DebugInfo] Preliminary support for DIExpression in SROA and Mem2Reg
2 parents 2a83e6b + 9a8f2ed commit c3390ec

29 files changed

+554
-166
lines changed

docs/SIL.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,7 @@ The operand must have loadable type.
35053505
debug-var-attr ::= 'let'
35063506
debug-var-attr ::= 'name' string-literal
35073507
debug-var-attr ::= 'argno' integer-literal
3508+
debug-var-attr ::= 'implicit'
35083509

35093510
::
35103511

@@ -3520,12 +3521,13 @@ The operand must have loadable type.
35203521
There are a number of attributes that provide details about the source
35213522
variable that is being described, including the name of the
35223523
variable. For function and closure arguments ``argno`` is the number
3523-
of the function argument starting with 1. The advanced debug variable
3524-
attributes represent source locations and type of the source variable
3525-
when it was originally declared. It is useful when we're indirectly
3526-
associating the SSA value with the source variable (via di-expression,
3527-
for example) in which case SSA value's type is different from that of
3528-
source variable.
3524+
of the function argument starting with 1. A compiler-generated source
3525+
variable will be marked ``implicit`` and optimizers are free to remove
3526+
it even in -Onone. The advanced debug variable attributes represent source
3527+
locations and type of the source variable when it was originally declared.
3528+
It is useful when we're indirectly associating the SSA value with the
3529+
source variable (via di-expression, for example) in which case SSA value's
3530+
type is different from that of source variable.
35293531

35303532
If the '[poison]' flag is set, then all references within this debug
35313533
value will be overwritten with a sentinel at this point in the

include/swift/SIL/DebugUtils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,24 @@ struct DebugVarCarryingInst {
308308
}
309309
llvm_unreachable("covered switch");
310310
}
311+
312+
void setDebugVarScope(const SILDebugScope *NewDS) {
313+
switch (kind) {
314+
case Kind::Invalid:
315+
llvm_unreachable("Invalid?!");
316+
case Kind::DebugValue:
317+
cast<DebugValueInst>(inst)->setDebugVarScope(NewDS);
318+
break;
319+
case Kind::DebugValueAddr:
320+
cast<DebugValueAddrInst>(inst)->setDebugVarScope(NewDS);
321+
break;
322+
case Kind::AllocStack:
323+
cast<AllocStackInst>(inst)->setDebugVarScope(NewDS);
324+
break;
325+
case Kind::AllocBox:
326+
llvm_unreachable("Not implemented");
327+
}
328+
}
311329
};
312330

313331
} // end namespace swift

include/swift/SIL/SILCloner.h

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/AST/ProtocolConformance.h"
2121
#include "swift/SIL/BasicBlockUtils.h"
22+
#include "swift/SIL/DebugUtils.h"
2223
#include "swift/SIL/Dominance.h"
2324
#include "swift/SIL/SILBuilder.h"
2425
#include "swift/SIL/SILDebugScope.h"
@@ -245,6 +246,16 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
245246
registerOpenedExistentialRemapping(archetypeTy, replacementTy);
246247
}
247248

249+
// SILCloner will take care of debug scope on the instruction
250+
// and this helper will remap the auxiliary debug scope too, if there is any.
251+
void remapDebugVarInfo(DebugVarCarryingInst DbgVarInst) {
252+
if (!DbgVarInst)
253+
return;
254+
auto VarInfo = DbgVarInst.getVarInfo();
255+
if (VarInfo && VarInfo->Scope)
256+
DbgVarInst.setDebugVarScope(getOpScope(VarInfo->Scope));
257+
}
258+
248259
ProtocolConformanceRef getOpConformance(Type ty,
249260
ProtocolConformanceRef conformance) {
250261
// If we have open existentials to substitute, do so now.
@@ -748,9 +759,11 @@ SILCloner<ImplClass>::visitAllocStackInst(AllocStackInst *Inst) {
748759
Loc = MandatoryInlinedLocation::getAutoGeneratedLocation();
749760
VarInfo = None;
750761
}
751-
recordClonedInstruction(Inst, getBuilder().createAllocStack(
752-
Loc, getOpType(Inst->getElementType()),
753-
VarInfo, Inst->hasDynamicLifetime()));
762+
auto *NewInst =
763+
getBuilder().createAllocStack(Loc, getOpType(Inst->getElementType()),
764+
VarInfo, Inst->hasDynamicLifetime());
765+
remapDebugVarInfo(DebugVarCarryingInst(NewInst));
766+
recordClonedInstruction(Inst, NewInst);
754767
}
755768

756769
template<typename ImplClass>
@@ -1232,12 +1245,13 @@ SILCloner<ImplClass>::visitDebugValueInst(DebugValueInst *Inst) {
12321245
return;
12331246

12341247
// Since we want the debug info to survive, we do not remap the location here.
1248+
SILDebugVariable VarInfo = *Inst->getVarInfo();
12351249
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1236-
recordClonedInstruction(
1237-
Inst, getBuilder().createDebugValue(Inst->getLoc(),
1238-
getOpValue(Inst->getOperand()),
1239-
*Inst->getVarInfo(),
1240-
Inst->poisonRefs()));
1250+
auto *NewInst = getBuilder().createDebugValue(Inst->getLoc(),
1251+
getOpValue(Inst->getOperand()),
1252+
VarInfo, Inst->poisonRefs());
1253+
remapDebugVarInfo(DebugVarCarryingInst(NewInst));
1254+
recordClonedInstruction(Inst, NewInst);
12411255
}
12421256
template<typename ImplClass>
12431257
void
@@ -1249,11 +1263,13 @@ SILCloner<ImplClass>::visitDebugValueAddrInst(DebugValueAddrInst *Inst) {
12491263
return;
12501264

12511265
// Do not remap the location for a debug Instruction.
1266+
SILDebugVariable VarInfo = *Inst->getVarInfo();
12521267
SILValue OpValue = getOpValue(Inst->getOperand());
12531268
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1254-
recordClonedInstruction(
1255-
Inst, getBuilder().createDebugValueAddr(Inst->getLoc(), OpValue,
1256-
*Inst->getVarInfo()));
1269+
auto *NewInst = getBuilder().createDebugValueAddr(Inst->getLoc(), OpValue,
1270+
*Inst->getVarInfo());
1271+
remapDebugVarInfo(DebugVarCarryingInst(NewInst));
1272+
recordClonedInstruction(Inst, NewInst);
12571273
}
12581274

12591275
#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \

include/swift/SIL/SILDebugInfoExpression.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ class SILDebugInfoExpression {
123123
explicit SILDebugInfoExpression(llvm::ArrayRef<SILDIExprElement> EL)
124124
: Elements(EL.begin(), EL.end()) {}
125125

126+
void clear() { Elements.clear(); }
127+
126128
size_t getNumElements() const { return Elements.size(); }
127129

128130
using iterator = typename decltype(Elements)::iterator;
@@ -151,6 +153,15 @@ class SILDebugInfoExpression {
151153
Elements.push_back(Element);
152154
}
153155

156+
void appendElements(llvm::ArrayRef<SILDIExprElement> NewElements) {
157+
if (NewElements.size())
158+
Elements.append(NewElements.begin(), NewElements.end());
159+
}
160+
161+
void append(const SILDebugInfoExpression &Tail) {
162+
appendElements(Tail.Elements);
163+
}
164+
154165
/// The iterator for SILDIExprOperand
155166
class op_iterator {
156167
friend class SILDebugInfoExpression;
@@ -208,6 +219,9 @@ class SILDebugInfoExpression {
208219

209220
/// Return true if this expression is not empty
210221
inline operator bool() const { return Elements.size(); }
222+
223+
/// Create a op_fragment expression
224+
static SILDebugInfoExpression createFragment(VarDecl *Field);
211225
};
212226
} // end namespace swift
213227
#endif

0 commit comments

Comments
 (0)