Skip to content

Commit e1023bc

Browse files
committed
[DebugInfo] PATCH 2/3: Duplicate logics regarding debug_value_addr
This patch replace all in-memory objects of DebugValueAddrInst with DebugValueInst + op_deref, and duplicates logics that handles DebugValueAddrInst with the latter. All related check in the tests have been updated as well. Note that this patch neither remove the DebugValueAddrInst class nor remove `debug_value_addr` syntax in the test inputs.
1 parent 33ec01a commit e1023bc

File tree

71 files changed

+357
-150
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+357
-150
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ class SILBuilder {
927927
DebugValueInst *createDebugValue(SILLocation Loc, SILValue src,
928928
SILDebugVariable Var,
929929
bool poisonRefs = false);
930-
DebugValueAddrInst *createDebugValueAddr(SILLocation Loc, SILValue src,
931-
SILDebugVariable Var);
930+
DebugValueInst *createDebugValueAddr(SILLocation Loc, SILValue src,
931+
SILDebugVariable Var);
932932

933933
/// Create a debug_value_addr if \p src is an address; a debug_value if not.
934934
SILInstruction *emitDebugDescription(SILLocation Loc, SILValue src,

include/swift/SIL/SILDebugInfoExpression.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ class SILDebugInfoExpression {
162162
appendElements(Tail.Elements);
163163
}
164164

165+
void prependElements(llvm::ArrayRef<SILDIExprElement> NewElements) {
166+
Elements.insert(Elements.begin(),
167+
NewElements.begin(), NewElements.end());
168+
}
169+
170+
void eraseElement(const_iterator It) {
171+
Elements.erase(It);
172+
}
173+
165174
/// The iterator for SILDIExprOperand
166175
class op_iterator {
167176
friend class SILDebugInfoExpression;

include/swift/SIL/SILInstruction.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4672,6 +4672,8 @@ class DebugValueInst final
46724672
static DebugValueInst *create(SILDebugLocation DebugLoc, SILValue Operand,
46734673
SILModule &M, SILDebugVariable Var,
46744674
bool poisonRefs);
4675+
static DebugValueInst *createAddr(SILDebugLocation DebugLoc, SILValue Operand,
4676+
SILModule &M, SILDebugVariable Var);
46754677

46764678
SIL_DEBUG_VAR_SUPPLEMENT_TRAILING_OBJS_IMPL()
46774679

@@ -4712,6 +4714,17 @@ class DebugValueInst final
47124714
return getOperand()->getType().isAddress();
47134715
}
47144716

4717+
/// An utility to check if \p I is DebugValueInst and
4718+
/// whether it's associated with address type SSA value.
4719+
static DebugValueInst *hasAddrVal(SILInstruction *I) {
4720+
auto *DVI = dyn_cast_or_null<DebugValueInst>(I);
4721+
return DVI && DVI->hasAddrVal()? DVI : nullptr;
4722+
}
4723+
4724+
/// Whether the attached di-expression (if there is any) starts
4725+
/// with `op_deref`.
4726+
bool exprStartsWithDeref() const;
4727+
47154728
/// True if all references within this debug value will be overwritten with a
47164729
/// poison sentinel at this point in the program. This is used in debug builds
47174730
/// when shortening non-trivial value lifetimes to ensure the debugger cannot

lib/IRGen/IRGenSIL.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,14 +4770,11 @@ void IRGenSILFunction::visitDebugValueInst(DebugValueInst *i) {
47704770
} else
47714771
return;
47724772

4773-
// Calculate the indirection
4774-
IndirectionKind Indirection = DirectValue;
4775-
if (IsInCoro)
4776-
Indirection = IsAddrVal ? CoroIndirectValue : CoroDirectValue;
4777-
else if (IsAddrVal && !isa<AllocStackInst>(SILVal))
4778-
Indirection = IndirectValue;
4779-
4780-
// Put the value into a stack slot at -Onone.
4773+
// Since debug_value is expressing indirection explicitly via op_deref,
4774+
// we're not using either IndirectValue or CoroIndirectValue here.
4775+
IndirectionKind Indirection = IsInCoro? CoroDirectValue : DirectValue;
4776+
4777+
// Put the value into a shadow-copy stack slot at -Onone.
47814778
llvm::SmallVector<llvm::Value *, 8> Copy;
47824779
if (IsAddrVal)
47834780
Copy.emplace_back(

lib/SIL/IR/SILBuilder.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,20 +569,19 @@ void SILBuilder::emitDestructureValueOperation(
569569
DebugValueInst *SILBuilder::createDebugValue(SILLocation Loc, SILValue src,
570570
SILDebugVariable Var,
571571
bool poisonRefs) {
572-
assert(isLoadableOrOpaque(src->getType()));
573572
// Debug location overrides cannot apply to debug value instructions.
574573
DebugLocOverrideRAII LocOverride{*this, None};
575574
return insert(
576575
DebugValueInst::create(getSILDebugLocation(Loc), src, getModule(), Var,
577576
poisonRefs));
578577
}
579578

580-
DebugValueAddrInst *SILBuilder::createDebugValueAddr(SILLocation Loc,
581-
SILValue src,
582-
SILDebugVariable Var) {
579+
DebugValueInst *SILBuilder::createDebugValueAddr(SILLocation Loc,
580+
SILValue src,
581+
SILDebugVariable Var) {
583582
// Debug location overrides cannot apply to debug addr instructions.
584583
DebugLocOverrideRAII LocOverride{*this, None};
585-
return insert(DebugValueAddrInst::create(getSILDebugLocation(Loc), src,
584+
return insert(DebugValueInst::createAddr(getSILDebugLocation(Loc), src,
586585
getModule(), Var));
587586
}
588587

lib/SIL/IR/SILInstructions.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,29 @@ DebugValueInst *DebugValueInst::create(SILDebugLocation DebugLoc,
357357
return ::new (buf) DebugValueInst(DebugLoc, Operand, Var, poisonRefs);
358358
}
359359

360+
DebugValueInst *DebugValueInst::createAddr(SILDebugLocation DebugLoc,
361+
SILValue Operand, SILModule &M,
362+
SILDebugVariable Var) {
363+
// For alloc_stack, debug_value is used to annotate the associated
364+
// memory location, so we shouldn't attach op_deref.
365+
if (!isa<AllocStackInst>(Operand))
366+
Var.DIExpr.prependElements(
367+
{SILDIExprElement::createOperator(SILDIExprOperator::Dereference)});
368+
void *buf = allocateDebugVarCarryingInst<DebugValueInst>(M, Var);
369+
return ::new (buf) DebugValueInst(DebugLoc, Operand, Var,
370+
/*poisonRefs=*/false);
371+
}
372+
373+
bool DebugValueInst::exprStartsWithDeref() const {
374+
if (!NumDIExprOperands)
375+
return false;
376+
377+
llvm::ArrayRef<SILDIExprElement> DIExprElements(
378+
getTrailingObjects<SILDIExprElement>(), NumDIExprOperands);
379+
return DIExprElements.front().getAsOperator()
380+
== SILDIExprOperator::Dereference;
381+
}
382+
360383
DebugValueAddrInst::DebugValueAddrInst(SILDebugLocation DebugLoc,
361384
SILValue Operand, SILDebugVariable Var)
362385
: UnaryInstructionBase(DebugLoc, Operand),

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,10 @@ swift::getSingleInitAllocStackUse(AllocStackInst *asi,
16291629
if (destroyingUses)
16301630
destroyingUses->push_back(use);
16311631
continue;
1632+
case SILInstructionKind::DebugValueInst:
1633+
if (cast<DebugValueInst>(user)->hasAddrVal())
1634+
continue;
1635+
break;
16321636
case SILInstructionKind::DeallocStackInst:
16331637
case SILInstructionKind::LoadBorrowInst:
16341638
case SILInstructionKind::DebugValueAddrInst:

lib/SIL/Utils/MemoryLocations.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ bool MemoryLocations::analyzeLocationUsesRecursively(SILValue V, unsigned locIdx
362362
if (!cast<LoadBorrowInst>(user)->getUsersOfType<BranchInst>().empty())
363363
return false;
364364
break;
365+
case SILInstructionKind::DebugValueInst:
366+
if (cast<DebugValueInst>(user)->hasAddrVal())
367+
break;
368+
return false;
365369
case SILInstructionKind::InjectEnumAddrInst:
366370
case SILInstructionKind::SelectEnumAddrInst:
367371
case SILInstructionKind::ExistentialMetatypeInst:

lib/SIL/Verifier/MemoryLifetimeVerifier.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,11 @@ void MemoryLifetimeVerifier::checkBlock(SILBasicBlock *block, Bits &bits) {
613613
case SILInstructionKind::DebugValueAddrInst:
614614
requireBitsSet(bits, I.getOperand(0), &I);
615615
break;
616+
case SILInstructionKind::DebugValueInst:
617+
if (cast<DebugValueInst>(&I)->hasAddrVal() &&
618+
cast<DebugValueInst>(&I)->exprStartsWithDeref())
619+
requireBitsSet(bits, I.getOperand(0), &I);
620+
break;
616621
case SILInstructionKind::UncheckedTakeEnumDataAddrInst: {
617622
// Note that despite the name, unchecked_take_enum_data_addr does _not_
618623
// "take" the payload of the Swift.Optional enum. This is a terrible

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ struct ImmutableAddressUseVerifier {
545545
case SILInstructionKind::SwitchEnumAddrInst:
546546
case SILInstructionKind::SelectEnumAddrInst:
547547
break;
548+
case SILInstructionKind::DebugValueInst:
549+
if (cast<DebugValueInst>(inst)->hasAddrVal())
550+
break;
551+
else {
552+
llvm::errs() << "Unhandled, unexpected instruction: " << *inst;
553+
llvm_unreachable("invoking standard assertion failure");
554+
}
548555
case SILInstructionKind::AddressToPointerInst:
549556
// We assume that the user is attempting to do something unsafe since we
550557
// are converting to a raw pointer. So just ignore this use.

lib/SILOptimizer/Analysis/AccessSummaryAnalysis.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ void AccessSummaryAnalysis::processArgument(FunctionInfo *info,
124124
processPartialApply(info, argumentIndex, cast<PartialApplyInst>(user),
125125
operand, order);
126126
break;
127+
case SILInstructionKind::DebugValueInst:
128+
if (DebugValueInst::hasAddrVal(user))
129+
break;
130+
LLVM_FALLTHROUGH;
127131
default:
128132
// FIXME: These likely represent scenarios in which we're not generating
129133
// begin access markers. Ignore these for now. But we really should

lib/SILOptimizer/Analysis/MemoryBehavior.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ static bool hasEscapingUses(SILValue address, int &numChecks) {
374374
case SILInstructionKind::EndAccessInst:
375375
// Those instructions have no result and cannot escape the address.
376376
break;
377+
case SILInstructionKind::DebugValueInst:
378+
if (DebugValueInst::hasAddrVal(user))
379+
break;
380+
return true;
377381
case SILInstructionKind::ApplyInst:
378382
case SILInstructionKind::TryApplyInst:
379383
case SILInstructionKind::BeginApplyInst:

lib/SILOptimizer/Mandatory/CapturePromotion.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ class ClosureCloner : public SILClonerWithScopes<ClosureCloner> {
313313

314314
SILValue getProjectBoxMappedVal(SILValue operandValue);
315315

316+
void visitDebugValueInst(DebugValueInst *inst);
316317
void visitDebugValueAddrInst(DebugValueAddrInst *inst);
317318
void visitDestroyValueInst(DestroyValueInst *inst);
318319
void visitStructElementAddrInst(StructElementAddrInst *inst);
@@ -581,6 +582,17 @@ void ClosureCloner::visitDebugValueAddrInst(DebugValueAddrInst *inst) {
581582
}
582583
SILCloner<ClosureCloner>::visitDebugValueAddrInst(inst);
583584
}
585+
/// Doing the same thing as ClosureCloner::visitDebugValueAddrInst. Only used
586+
/// when transitioning away from debug_value_addr.
587+
void ClosureCloner::visitDebugValueInst(DebugValueInst *inst) {
588+
if (inst->hasAddrVal())
589+
if (SILValue value = getProjectBoxMappedVal(inst->getOperand())) {
590+
getBuilder().setCurrentDebugScope(getOpScope(inst->getDebugScope()));
591+
getBuilder().createDebugValue(inst->getLoc(), value, *inst->getVarInfo());
592+
return;
593+
}
594+
SILCloner<ClosureCloner>::visitDebugValueInst(inst);
595+
}
584596

585597
/// Handle a destroy_value instruction during cloning of a closure; if it is a
586598
/// destroy_value of a promoted box argument, then it is replaced with a
@@ -855,6 +867,7 @@ getPartialApplyArgMutationsAndEscapes(PartialApplyInst *pai,
855867
}
856868

857869
if (isa<DebugValueAddrInst>(addrUser) ||
870+
DebugValueInst::hasAddrVal(addrUser) ||
858871
isa<MarkFunctionEscapeInst>(addrUser) || isa<EndAccessInst>(addrUser)) {
859872
return false;
860873
}

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ bool SILCombiner::optimizeStackAllocatedEnum(AllocStackInst *AS) {
514514
case SILInstructionKind::InjectEnumAddrInst:
515515
// We'll check init_enum_addr below.
516516
break;
517+
case SILInstructionKind::DebugValueInst:
518+
if (DebugValueInst::hasAddrVal(user))
519+
break;
520+
return false;
517521
case SILInstructionKind::InitEnumDataAddrInst: {
518522
auto *ieda = cast<InitEnumDataAddrInst>(user);
519523
auto *el = ieda->getElement();
@@ -599,6 +603,12 @@ bool SILCombiner::optimizeStackAllocatedEnum(AllocStackInst *AS) {
599603
eraseInstFromFunction(*svi);
600604
break;
601605
}
606+
case SILInstructionKind::DebugValueInst:
607+
if (DebugValueInst::hasAddrVal(user)) {
608+
eraseInstFromFunction(*user);
609+
break;
610+
}
611+
LLVM_FALLTHROUGH;
602612
default:
603613
llvm_unreachable("unexpected alloc_stack user");
604614
}

0 commit comments

Comments
 (0)