Skip to content

Commit 3d85f47

Browse files
committed
[variable-name-utils] Store a std::variant instead of a pointer union and add the ability to append StringRef.
CONTEXT: This code works by building up a stack of SIL values of values that need to be transformed into a StringRef as part of generating our name path and then as a second phase performs the conversion of those values to StringRef as we pop from the stack. This is the first in a string of commits that are going to refactor VariableNameUtils so that the stack will only contain StringRef instead of SIL entities. This will be accomplished by moving the SIL value -> StringRef code from the combining part of the algorithm (where we drain the stack) to the construction of the stack. The reason why I am doing this is two fold: 1. By just storing StringRef into the stack I am simplifying the code. Today as mentioned above in the context, we gather up the SILValue we want to process and then just convert them to StringRef. This means that any time one has to add a new instruction, one has to update two different pieces of code. By trafficking in StringRef instead, one only has to update one piece of code. 2. I want to add some simple code that allows for us to get names from closures which would require me to recurse. I am nervous about putting values/instructions from different functions in the same data structure. Today it is safe, but it is bad practice. Instead, by just using StringRef in the stack, I can avoid this problem.
1 parent 3e21fc6 commit 3d85f47

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

include/swift/SILOptimizer/Utils/VariableNameUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class VariableNameInferrer {
109109
///
110110
/// Has to be a small vector since we push/pop the last segment start. This
111111
/// lets us speculate when processing phis.
112-
VariableNamePathArray<PointerUnion<SILInstruction *, SILValue>, 4>
112+
VariableNamePathArray<std::variant<SILInstruction *, SILValue, StringRef>, 4>
113113
variableNamePath;
114114

115115
/// The root value of our string.

lib/SILOptimizer/Utils/VariableNameUtils.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,13 @@ StringRef VariableNameInferrer::getNameFromDecl(Decl *d) {
655655
void VariableNameInferrer::popSingleVariableName() {
656656
auto next = variableNamePath.pop_back_val();
657657

658-
if (auto *inst = next.dyn_cast<SILInstruction *>()) {
658+
if (std::holds_alternative<StringRef>(next)) {
659+
resultingString += std::get<StringRef>(next);
660+
return;
661+
}
662+
663+
if (std::holds_alternative<SILInstruction *>(next)) {
664+
auto *inst = std::get<SILInstruction *>(next);
659665
if (auto i = DebugVarCarryingInst(inst)) {
660666
resultingString += i.getName();
661667
return;
@@ -722,25 +728,27 @@ void VariableNameInferrer::popSingleVariableName() {
722728
return;
723729
}
724730

725-
auto value = next.get<SILValue>();
726-
if (auto *fArg = dyn_cast<SILFunctionArgument>(value)) {
727-
resultingString += fArg->getDecl()->getBaseName().userFacingName();
728-
return;
729-
}
731+
if (std::holds_alternative<SILValue>(next)) {
732+
auto value = std::get<SILValue>(next);
733+
if (auto *fArg = dyn_cast<SILFunctionArgument>(value)) {
734+
resultingString += fArg->getDecl()->getBaseName().userFacingName();
735+
return;
736+
}
730737

731-
if (auto *dti = dyn_cast_or_null<DestructureTupleInst>(
732-
value->getDefiningInstruction())) {
733-
llvm::raw_svector_ostream stream(resultingString);
734-
stream << *dti->getIndexOfResult(value);
735-
return;
736-
}
738+
if (auto *dti = dyn_cast_or_null<DestructureTupleInst>(
739+
value->getDefiningInstruction())) {
740+
llvm::raw_svector_ostream stream(resultingString);
741+
stream << *dti->getIndexOfResult(value);
742+
return;
743+
}
737744

738-
if (auto *dsi = dyn_cast_or_null<DestructureStructInst>(
739-
value->getDefiningInstruction())) {
740-
unsigned index = *dsi->getIndexOfResult(value);
741-
resultingString +=
742-
getNameFromDecl(dsi->getStructDecl()->getStoredProperties()[index]);
743-
return;
745+
if (auto *dsi = dyn_cast_or_null<DestructureStructInst>(
746+
value->getDefiningInstruction())) {
747+
unsigned index = *dsi->getIndexOfResult(value);
748+
resultingString +=
749+
getNameFromDecl(dsi->getStructDecl()->getStoredProperties()[index]);
750+
return;
751+
}
744752
}
745753

746754
resultingString += "<unknown decl>";

0 commit comments

Comments
 (0)