@@ -281,8 +281,11 @@ SILValue VariableNameInferrer::getRootValueForTemporaryAllocation(
281281
282282SILValue
283283VariableNameInferrer::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+
345354SILValue 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
750675void 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