Skip to content

Commit 2dd1ef2

Browse files
authored
Merge pull request swiftlang#75759 from gottesmm/variable-name-utils-refactor
[variable-name-utils] Change internal representation to use StringRef instead of a SILValue
2 parents 8edb57c + 0039b28 commit 2dd1ef2

File tree

2 files changed

+67
-129
lines changed

2 files changed

+67
-129
lines changed

include/swift/SILOptimizer/Utils/VariableNameUtils.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,14 @@ class VariableNameInferrer {
122122
bool empty() const { return !insertionPointIndex; }
123123
};
124124

125+
/// ASTContext for forming identifiers when we need to.
126+
ASTContext &astContext;
127+
125128
/// The stacklist that we use to print out variable names.
126129
///
127130
/// Has to be a small vector since we push/pop the last segment start. This
128131
/// lets us speculate when processing phis.
129-
VariableNamePathArray<PointerUnion<SILInstruction *, SILValue>, 4>
130-
variableNamePath;
132+
VariableNamePathArray<StringRef, 4> variableNamePath;
131133

132134
/// The root value of our string.
133135
///
@@ -145,12 +147,13 @@ class VariableNameInferrer {
145147

146148
public:
147149
VariableNameInferrer(SILFunction *fn, SmallString<64> &resultingString)
148-
: variableNamePath(), resultingString(resultingString) {}
150+
: astContext(fn->getASTContext()), variableNamePath(),
151+
resultingString(resultingString) {}
149152

150153
VariableNameInferrer(SILFunction *fn, Options options,
151154
SmallString<64> &resultingString)
152-
: variableNamePath(), resultingString(resultingString), options(options) {
153-
}
155+
: astContext(fn->getASTContext()), variableNamePath(),
156+
resultingString(resultingString), options(options) {}
154157

155158
/// Attempts to infer a name from just uses of \p searchValue.
156159
///
@@ -209,6 +212,8 @@ class VariableNameInferrer {
209212

210213
StringRef getName() const { return resultingString; }
211214

215+
SWIFT_DEBUG_DUMP { llvm::dbgs() << getName() << '\n'; }
216+
212217
/// Given a specific SILValue, construct a VariableNameInferrer and use it to
213218
/// attempt to infer an identifier for the value.
214219
static std::optional<Identifier> inferName(SILValue value);
@@ -227,7 +232,6 @@ class VariableNameInferrer {
227232

228233
private:
229234
void drainVariableNamePath();
230-
void popSingleVariableName();
231235

232236
/// Finds the SILValue that either provides the direct debug information or
233237
/// that has a debug_value user that provides the name of the value.
@@ -246,6 +250,15 @@ class VariableNameInferrer {
246250
/// DebugVariable provided name, attempt to find a root value from its
247251
/// initialization.
248252
SILValue getRootValueForTemporaryAllocation(AllocationInst *allocInst);
253+
254+
StringRef getStringRefForIndex(unsigned index) const {
255+
llvm::SmallString<64> indexString;
256+
{
257+
llvm::raw_svector_ostream stream(indexString);
258+
stream << index;
259+
}
260+
return astContext.getIdentifier(indexString).str();
261+
}
249262
};
250263

251264
} // namespace swift

lib/SILOptimizer/Utils/VariableNameUtils.cpp

Lines changed: 48 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,11 @@ SILValue VariableNameInferrer::getRootValueForTemporaryAllocation(
281281

282282
SILValue
283283
VariableNameInferrer::findDebugInfoProvidingValue(SILValue searchValue) {
284+
// NOTE: This should only return a non-empty SILValue if we actually have a
285+
// full path (including base name) in the variable name path.
284286
if (!searchValue)
285287
return SILValue();
288+
286289
LLVM_DEBUG(llvm::dbgs() << "Searching for debug info providing value for: "
287290
<< searchValue);
288291
ValueSet valueSet(searchValue->getFunction());
@@ -342,6 +345,12 @@ static BeginBorrowInst *hasOnlyBorrowingNonDestroyUse(SILValue searchValue) {
342345
return result;
343346
}
344347

348+
namespace {
349+
350+
constexpr StringLiteral UnknownDeclString = "<unknown decl>";
351+
352+
} // namespace
353+
345354
SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
346355
SILValue searchValue, ValueSet &visitedValues) {
347356
assert(searchValue);
@@ -362,7 +371,7 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
362371
if (auto *use = getAnyDebugUse(searchValue)) {
363372
if (auto debugVar = DebugVarCarryingInst(use->getUser())) {
364373
assert(debugVar.getKind() == DebugVarCarryingInst::Kind::DebugValue);
365-
variableNamePath.push_back(use->getUser());
374+
variableNamePath.push_back(debugVar.getName());
366375

367376
// We return the value, not the debug_info.
368377
return searchValue;
@@ -385,7 +394,7 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
385394
if (auto debugVar = DebugVarCarryingInst(debugUse->getUser())) {
386395
assert(debugVar.getKind() ==
387396
DebugVarCarryingInst::Kind::DebugValue);
388-
variableNamePath.push_back(debugUse->getUser());
397+
variableNamePath.push_back(debugVar.getName());
389398

390399
// We return the value, not the debug_info.
391400
return searchValue;
@@ -399,7 +408,7 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
399408
if (auto *debugUse = getAnyDebugUse(bbi)) {
400409
if (auto debugVar = DebugVarCarryingInst(debugUse->getUser())) {
401410
assert(debugVar.getKind() == DebugVarCarryingInst::Kind::DebugValue);
402-
variableNamePath.push_back(debugUse->getUser());
411+
variableNamePath.push_back(debugVar.getName());
403412

404413
// We return the value, not the debug_info.
405414
return searchValue;
@@ -426,10 +435,15 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
426435
return SILValue();
427436
}
428437

429-
variableNamePath.push_back(allocInst);
438+
variableNamePath.push_back(DebugVarCarryingInst(allocInst).getName());
430439
return allocInst;
431440
}
432441

442+
if (auto *abi = dyn_cast<AllocBoxInst>(searchValue)) {
443+
variableNamePath.push_back(DebugVarCarryingInst(abi).getName());
444+
return abi;
445+
}
446+
433447
// If we have a store_borrow, always look at the dest. We are going to see
434448
// if we can determine if dest is a temporary alloc_stack.
435449
if (auto *sbi = dyn_cast<StoreBorrowInst>(searchValue)) {
@@ -438,7 +452,7 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
438452
}
439453

440454
if (auto *globalAddrInst = dyn_cast<GlobalAddrInst>(searchValue)) {
441-
variableNamePath.push_back(globalAddrInst);
455+
variableNamePath.push_back(VarDeclCarryingInst(globalAddrInst).getName());
442456
return globalAddrInst;
443457
}
444458

@@ -448,44 +462,44 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
448462
}
449463

450464
if (auto *rei = dyn_cast<RefElementAddrInst>(searchValue)) {
451-
variableNamePath.push_back(rei);
465+
variableNamePath.push_back(VarDeclCarryingInst(rei).getName());
452466
searchValue = rei->getOperand();
453467
continue;
454468
}
455469

456470
if (auto *sei = dyn_cast<StructExtractInst>(searchValue)) {
457-
variableNamePath.push_back(sei);
471+
variableNamePath.push_back(getNameFromDecl(sei->getField()));
458472
searchValue = sei->getOperand();
459473
continue;
460474
}
461475

462476
if (auto *uedi = dyn_cast<UncheckedEnumDataInst>(searchValue)) {
463-
variableNamePath.push_back(uedi);
477+
variableNamePath.push_back(getNameFromDecl(uedi->getElement()));
464478
searchValue = uedi->getOperand();
465479
continue;
466480
}
467481

468482
if (auto *tei = dyn_cast<TupleExtractInst>(searchValue)) {
469-
variableNamePath.push_back(tei);
483+
variableNamePath.push_back(getStringRefForIndex(tei->getFieldIndex()));
470484
searchValue = tei->getOperand();
471485
continue;
472486
}
473487

474488
if (auto *sei = dyn_cast<StructElementAddrInst>(searchValue)) {
475-
variableNamePath.push_back(sei);
489+
variableNamePath.push_back(getNameFromDecl(sei->getField()));
476490
searchValue = sei->getOperand();
477491
continue;
478492
}
479493

480494
if (auto *tei = dyn_cast<TupleElementAddrInst>(searchValue)) {
481-
variableNamePath.push_back(tei);
495+
variableNamePath.push_back(getStringRefForIndex(tei->getFieldIndex()));
482496
searchValue = tei->getOperand();
483497
continue;
484498
}
485499

486-
if (auto *e = dyn_cast<UncheckedTakeEnumDataAddrInst>(searchValue)) {
487-
variableNamePath.push_back(e);
488-
searchValue = e->getOperand();
500+
if (auto *utedai = dyn_cast<UncheckedTakeEnumDataAddrInst>(searchValue)) {
501+
variableNamePath.push_back(getNameFromDecl(utedai->getElement()));
502+
searchValue = utedai->getOperand();
489503
continue;
490504
}
491505

@@ -494,31 +508,32 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
494508
// them and add the case to the variableNamePath.
495509
if (auto *e = dyn_cast<EnumInst>(searchValue)) {
496510
if (e->hasOperand()) {
497-
variableNamePath.push_back(e);
511+
variableNamePath.push_back(getNameFromDecl(e->getElement()));
498512
searchValue = e->getOperand();
499513
continue;
500514
}
501515
}
502516

503517
if (auto *dti = dyn_cast_or_null<DestructureTupleInst>(
504518
searchValue->getDefiningInstruction())) {
505-
// Append searchValue, so we can find the specific tuple index.
506-
variableNamePath.push_back(searchValue);
519+
variableNamePath.push_back(
520+
getStringRefForIndex(*dti->getIndexOfResult(searchValue)));
507521
searchValue = dti->getOperand();
508522
continue;
509523
}
510524

511525
if (auto *dsi = dyn_cast_or_null<DestructureStructInst>(
512526
searchValue->getDefiningInstruction())) {
513-
// Append searchValue, so we can find the specific struct field.
514-
variableNamePath.push_back(searchValue);
527+
unsigned index = *dsi->getIndexOfResult(searchValue);
528+
variableNamePath.push_back(
529+
getNameFromDecl(dsi->getStructDecl()->getStoredProperties()[index]));
515530
searchValue = dsi->getOperand();
516531
continue;
517532
}
518533

519534
if (auto *fArg = dyn_cast<SILFunctionArgument>(searchValue)) {
520-
if (fArg->getDecl()) {
521-
variableNamePath.push_back({fArg});
535+
if (auto *decl = fArg->getDecl()) {
536+
variableNamePath.push_back(decl->getBaseName().userFacingName());
522537
return fArg;
523538
}
524539
}
@@ -546,15 +561,19 @@ SILValue VariableNameInferrer::findDebugInfoProvidingValueHelper(
546561

547562
auto getNamePathComponentFromCallee = [&](FullApplySite call) -> SILValue {
548563
// Use the name of the property being accessed if we can get to it.
549-
if (isa<FunctionRefBaseInst>(call.getCallee()) ||
550-
isa<MethodInst>(call.getCallee())) {
551-
if (call.getSubstCalleeType()->hasSelfParam()) {
564+
if (call.getSubstCalleeType()->hasSelfParam()) {
565+
if (auto *f = dyn_cast<FunctionRefBaseInst>(call.getCallee())) {
566+
if (auto dc = f->getInitiallyReferencedFunction()->getDeclContext()) {
567+
variableNamePath.push_back(getNameFromDecl(dc->getAsDecl()));
568+
return call.getSelfArgument();
569+
}
570+
}
571+
572+
if (auto *mi = dyn_cast<MethodInst>(call.getCallee())) {
552573
variableNamePath.push_back(
553-
call.getCallee()->getDefiningInstruction());
574+
getNameFromDecl(mi->getMember().getDecl()));
554575
return call.getSelfArgument();
555576
}
556-
557-
return SILValue();
558577
}
559578

560579
return SILValue();
@@ -650,101 +669,7 @@ StringRef VariableNameInferrer::getNameFromDecl(Decl *d) {
650669
}
651670
}
652671

653-
return "<unknown decl>";
654-
}
655-
656-
void VariableNameInferrer::popSingleVariableName() {
657-
auto next = variableNamePath.pop_back_val();
658-
659-
if (auto *inst = next.dyn_cast<SILInstruction *>()) {
660-
if (auto i = DebugVarCarryingInst(inst)) {
661-
resultingString += i.getName();
662-
return;
663-
}
664-
665-
if (auto i = VarDeclCarryingInst(inst)) {
666-
resultingString += i.getName();
667-
return;
668-
}
669-
670-
if (auto f = dyn_cast<FunctionRefBaseInst>(inst)) {
671-
if (auto dc = f->getInitiallyReferencedFunction()->getDeclContext()) {
672-
resultingString += getNameFromDecl(dc->getAsDecl());
673-
return;
674-
}
675-
676-
resultingString += "<unknown decl>";
677-
return;
678-
}
679-
680-
if (auto m = dyn_cast<MethodInst>(inst)) {
681-
resultingString += getNameFromDecl(m->getMember().getDecl());
682-
return;
683-
}
684-
685-
if (auto *sei = dyn_cast<StructExtractInst>(inst)) {
686-
resultingString += getNameFromDecl(sei->getField());
687-
return;
688-
}
689-
690-
if (auto *tei = dyn_cast<TupleExtractInst>(inst)) {
691-
llvm::raw_svector_ostream stream(resultingString);
692-
stream << tei->getFieldIndex();
693-
return;
694-
}
695-
696-
if (auto *uedi = dyn_cast<UncheckedEnumDataInst>(inst)) {
697-
resultingString += getNameFromDecl(uedi->getElement());
698-
return;
699-
}
700-
701-
if (auto *sei = dyn_cast<StructElementAddrInst>(inst)) {
702-
resultingString += getNameFromDecl(sei->getField());
703-
return;
704-
}
705-
706-
if (auto *tei = dyn_cast<TupleElementAddrInst>(inst)) {
707-
llvm::raw_svector_ostream stream(resultingString);
708-
stream << tei->getFieldIndex();
709-
return;
710-
}
711-
712-
if (auto *uedi = dyn_cast<UncheckedTakeEnumDataAddrInst>(inst)) {
713-
resultingString += getNameFromDecl(uedi->getElement());
714-
return;
715-
}
716-
717-
if (auto *ei = dyn_cast<EnumInst>(inst)) {
718-
resultingString += getNameFromDecl(ei->getElement());
719-
return;
720-
}
721-
722-
resultingString += "<unknown decl>";
723-
return;
724-
}
725-
726-
auto value = next.get<SILValue>();
727-
if (auto *fArg = dyn_cast<SILFunctionArgument>(value)) {
728-
resultingString += fArg->getDecl()->getBaseName().userFacingName();
729-
return;
730-
}
731-
732-
if (auto *dti = dyn_cast_or_null<DestructureTupleInst>(
733-
value->getDefiningInstruction())) {
734-
llvm::raw_svector_ostream stream(resultingString);
735-
stream << *dti->getIndexOfResult(value);
736-
return;
737-
}
738-
739-
if (auto *dsi = dyn_cast_or_null<DestructureStructInst>(
740-
value->getDefiningInstruction())) {
741-
unsigned index = *dsi->getIndexOfResult(value);
742-
resultingString +=
743-
getNameFromDecl(dsi->getStructDecl()->getStoredProperties()[index]);
744-
return;
745-
}
746-
747-
resultingString += "<unknown decl>";
672+
return UnknownDeclString;
748673
}
749674

750675
void VariableNameInferrer::drainVariableNamePath() {
@@ -753,7 +678,7 @@ void VariableNameInferrer::drainVariableNamePath() {
753678

754679
// Walk backwards, constructing our string.
755680
while (true) {
756-
popSingleVariableName();
681+
resultingString += variableNamePath.pop_back_val();
757682

758683
if (variableNamePath.empty())
759684
return;

0 commit comments

Comments
 (0)