Skip to content

[SILOptimizer][DebugInfo] Preliminary support for DIExpression in SROA and Mem2Reg #38736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions docs/SIL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3505,6 +3505,7 @@ The operand must have loadable type.
debug-var-attr ::= 'let'
debug-var-attr ::= 'name' string-literal
debug-var-attr ::= 'argno' integer-literal
debug-var-attr ::= 'implicit'

::

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

If the '[poison]' flag is set, then all references within this debug
value will be overwritten with a sentinel at this point in the
Expand Down
18 changes: 18 additions & 0 deletions include/swift/SIL/DebugUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,24 @@ struct DebugVarCarryingInst {
}
llvm_unreachable("covered switch");
}

void setDebugVarScope(const SILDebugScope *NewDS) {
switch (kind) {
case Kind::Invalid:
llvm_unreachable("Invalid?!");
case Kind::DebugValue:
cast<DebugValueInst>(inst)->setDebugVarScope(NewDS);
break;
case Kind::DebugValueAddr:
cast<DebugValueAddrInst>(inst)->setDebugVarScope(NewDS);
break;
case Kind::AllocStack:
cast<AllocStackInst>(inst)->setDebugVarScope(NewDS);
break;
case Kind::AllocBox:
llvm_unreachable("Not implemented");
}
}
};

} // end namespace swift
Expand Down
38 changes: 27 additions & 11 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "swift/AST/ProtocolConformance.h"
#include "swift/SIL/BasicBlockUtils.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/Dominance.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILDebugScope.h"
Expand Down Expand Up @@ -245,6 +246,16 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
registerOpenedExistentialRemapping(archetypeTy, replacementTy);
}

// SILCloner will take care of debug scope on the instruction
// and this helper will remap the auxiliary debug scope too, if there is any.
void remapDebugVarInfo(DebugVarCarryingInst DbgVarInst) {
if (!DbgVarInst)
return;
auto VarInfo = DbgVarInst.getVarInfo();
if (VarInfo && VarInfo->Scope)
DbgVarInst.setDebugVarScope(getOpScope(VarInfo->Scope));
}

ProtocolConformanceRef getOpConformance(Type ty,
ProtocolConformanceRef conformance) {
// If we have open existentials to substitute, do so now.
Expand Down Expand Up @@ -748,9 +759,11 @@ SILCloner<ImplClass>::visitAllocStackInst(AllocStackInst *Inst) {
Loc = MandatoryInlinedLocation::getAutoGeneratedLocation();
VarInfo = None;
}
recordClonedInstruction(Inst, getBuilder().createAllocStack(
Loc, getOpType(Inst->getElementType()),
VarInfo, Inst->hasDynamicLifetime()));
auto *NewInst =
getBuilder().createAllocStack(Loc, getOpType(Inst->getElementType()),
VarInfo, Inst->hasDynamicLifetime());
remapDebugVarInfo(DebugVarCarryingInst(NewInst));
recordClonedInstruction(Inst, NewInst);
}

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

// Since we want the debug info to survive, we do not remap the location here.
SILDebugVariable VarInfo = *Inst->getVarInfo();
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(
Inst, getBuilder().createDebugValue(Inst->getLoc(),
getOpValue(Inst->getOperand()),
*Inst->getVarInfo(),
Inst->poisonRefs()));
auto *NewInst = getBuilder().createDebugValue(Inst->getLoc(),
getOpValue(Inst->getOperand()),
VarInfo, Inst->poisonRefs());
remapDebugVarInfo(DebugVarCarryingInst(NewInst));
recordClonedInstruction(Inst, NewInst);
}
template<typename ImplClass>
void
Expand All @@ -1249,11 +1263,13 @@ SILCloner<ImplClass>::visitDebugValueAddrInst(DebugValueAddrInst *Inst) {
return;

// Do not remap the location for a debug Instruction.
SILDebugVariable VarInfo = *Inst->getVarInfo();
SILValue OpValue = getOpValue(Inst->getOperand());
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(
Inst, getBuilder().createDebugValueAddr(Inst->getLoc(), OpValue,
*Inst->getVarInfo()));
auto *NewInst = getBuilder().createDebugValueAddr(Inst->getLoc(), OpValue,
*Inst->getVarInfo());
remapDebugVarInfo(DebugVarCarryingInst(NewInst));
recordClonedInstruction(Inst, NewInst);
}

#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
Expand Down
14 changes: 14 additions & 0 deletions include/swift/SIL/SILDebugInfoExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class SILDebugInfoExpression {
explicit SILDebugInfoExpression(llvm::ArrayRef<SILDIExprElement> EL)
: Elements(EL.begin(), EL.end()) {}

void clear() { Elements.clear(); }

size_t getNumElements() const { return Elements.size(); }

using iterator = typename decltype(Elements)::iterator;
Expand Down Expand Up @@ -151,6 +153,15 @@ class SILDebugInfoExpression {
Elements.push_back(Element);
}

void appendElements(llvm::ArrayRef<SILDIExprElement> NewElements) {
if (NewElements.size())
Elements.append(NewElements.begin(), NewElements.end());
}

void append(const SILDebugInfoExpression &Tail) {
appendElements(Tail.Elements);
}

/// The iterator for SILDIExprOperand
class op_iterator {
friend class SILDebugInfoExpression;
Expand Down Expand Up @@ -208,6 +219,9 @@ class SILDebugInfoExpression {

/// Return true if this expression is not empty
inline operator bool() const { return Elements.size(); }

/// Create a op_fragment expression
static SILDebugInfoExpression createFragment(VarDecl *Field);
};
} // end namespace swift
#endif
Loading