Skip to content

Commit 51ef67d

Browse files
committed
[variable-name-utils] Refactor inferName/inferNameAndRoot helpers onto VariableNameInferrer.
I have been using these in TransferNonSendable and they are useful in terms of reducing the amount of code that one has to type to use this API. I am going to need to use it in SILIsolationInfo, so it makes sense to move it into SILOptimizer/Utils. NFCI.
1 parent d8f39f7 commit 51ef67d

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

include/swift/SILOptimizer/Utils/VariableNameUtils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ class VariableNameInferrer {
192192

193193
StringRef getName() const { return resultingString; }
194194

195+
/// Given a specific SILValue, construct a VariableNameInferrer and use it to
196+
/// attempt to infer an identifier for the value.
197+
static std::optional<Identifier> inferName(SILValue value);
198+
199+
/// Given a specific SILValue, construct a VariableNameInferrer and use it to
200+
/// attempt to infer an identifier for the value and a named value.
201+
static std::optional<std::pair<Identifier, SILValue>>
202+
inferNameAndRoot(SILValue value);
203+
195204
private:
196205
void drainVariableNamePath();
197206
void popSingleVariableName();

lib/SILOptimizer/Mandatory/TransferNonSendable.cpp

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,6 @@ static Expr *inferArgumentExprFromApplyExpr(ApplyExpr *sourceApply,
9292
return foundExpr;
9393
}
9494

95-
static std::optional<Identifier> inferNameFromValue(SILValue value) {
96-
auto *fn = value->getFunction();
97-
if (!fn)
98-
return {};
99-
VariableNameInferrer::Options options;
100-
options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
101-
SmallString<64> resultingName;
102-
VariableNameInferrer inferrer(fn, options, resultingName);
103-
if (!inferrer.inferByWalkingUsesToDefsReturningRoot(value))
104-
return {};
105-
return fn->getASTContext().getIdentifier(resultingName);
106-
}
107-
108-
static std::optional<std::pair<Identifier, SILValue>>
109-
inferNameAndRootFromValue(SILValue value) {
110-
auto *fn = value->getFunction();
111-
if (!fn)
112-
return {};
113-
VariableNameInferrer::Options options;
114-
options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
115-
SmallString<64> resultingName;
116-
VariableNameInferrer inferrer(fn, options, resultingName);
117-
SILValue rootValue = inferrer.inferByWalkingUsesToDefsReturningRoot(value);
118-
if (!rootValue)
119-
return {};
120-
return {{fn->getASTContext().getIdentifier(resultingName), rootValue}};
121-
}
122-
12395
//===----------------------------------------------------------------------===//
12496
// MARK: Diagnostics
12597
//===----------------------------------------------------------------------===//
@@ -693,7 +665,8 @@ bool UseAfterTransferDiagnosticInferrer::initForIsolatedPartialApply(
693665
emittedDiagnostic = true;
694666

695667
auto &state = transferringOpToStateMap.get(transferOp);
696-
if (auto rootValueAndName = inferNameAndRootFromValue(transferOp->get())) {
668+
if (auto rootValueAndName =
669+
VariableNameInferrer::inferNameAndRoot(transferOp->get())) {
697670
diagnosticEmitter.emitNamedIsolationCrossingDueToCapture(
698671
RegularLocation(std::get<0>(p).getLoc()), rootValueAndName->first,
699672
state.isolationInfo, std::get<2>(p));
@@ -815,7 +788,7 @@ void UseAfterTransferDiagnosticInferrer::infer() {
815788

816789
// First try to do the named diagnostic if we can find a name.
817790
if (auto rootValueAndName =
818-
inferNameAndRootFromValue(transferOp->get())) {
791+
VariableNameInferrer::inferNameAndRoot(transferOp->get())) {
819792
return diagnosticEmitter.emitNamedUseOfStronglyTransferredValue(
820793
baseLoc, rootValueAndName->first);
821794
}
@@ -841,7 +814,8 @@ void UseAfterTransferDiagnosticInferrer::infer() {
841814
if (auto *sourceApply = loc.getAsASTNode<ApplyExpr>()) {
842815
// Before we do anything further, see if we can find a name and emit a name
843816
// error.
844-
if (auto rootValueAndName = inferNameAndRootFromValue(transferOp->get())) {
817+
if (auto rootValueAndName =
818+
VariableNameInferrer::inferNameAndRoot(transferOp->get())) {
845819
auto &state = transferringOpToStateMap.get(transferOp);
846820
return diagnosticEmitter.emitNamedIsolationCrossingError(
847821
baseLoc, rootValueAndName->first, state.isolationInfo,
@@ -1178,7 +1152,7 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
11781152

11791153
// See if we can infer a name from the value.
11801154
SmallString<64> resultingName;
1181-
if (auto varName = inferNameFromValue(op->get())) {
1155+
if (auto varName = VariableNameInferrer::inferName(op->get())) {
11821156
diagnosticEmitter.emitNamedFunctionArgumentApplyStronglyTransferred(
11831157
loc, *varName);
11841158
return true;
@@ -1218,7 +1192,7 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
12181192

12191193
// See if we can infer a name from the value.
12201194
SmallString<64> resultingName;
1221-
if (auto name = inferNameFromValue(op->get())) {
1195+
if (auto name = VariableNameInferrer::inferName(op->get())) {
12221196
diagnosticEmitter.emitNamedIsolation(loc, *name, *isolation);
12231197
return true;
12241198
}
@@ -1266,7 +1240,7 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
12661240
"All result info must be the same... if that changes... update "
12671241
"this code!");
12681242
SmallString<64> resultingName;
1269-
if (auto name = inferNameFromValue(op->get())) {
1243+
if (auto name = VariableNameInferrer::inferName(op->get())) {
12701244
diagnosticEmitter.emitNamedTransferringReturn(loc, *name);
12711245
return true;
12721246
}

lib/SILOptimizer/Utils/VariableNameUtils.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,34 @@ void VariableNameInferrer::drainVariableNamePath() {
758758
}
759759
}
760760

761+
std::optional<Identifier> VariableNameInferrer::inferName(SILValue value) {
762+
auto *fn = value->getFunction();
763+
if (!fn)
764+
return {};
765+
VariableNameInferrer::Options options;
766+
options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
767+
SmallString<64> resultingName;
768+
VariableNameInferrer inferrer(fn, options, resultingName);
769+
if (!inferrer.inferByWalkingUsesToDefsReturningRoot(value))
770+
return {};
771+
return fn->getASTContext().getIdentifier(resultingName);
772+
}
773+
774+
std::optional<std::pair<Identifier, SILValue>>
775+
VariableNameInferrer::inferNameAndRoot(SILValue value) {
776+
auto *fn = value->getFunction();
777+
if (!fn)
778+
return {};
779+
VariableNameInferrer::Options options;
780+
options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
781+
SmallString<64> resultingName;
782+
VariableNameInferrer inferrer(fn, options, resultingName);
783+
SILValue rootValue = inferrer.inferByWalkingUsesToDefsReturningRoot(value);
784+
if (!rootValue)
785+
return {};
786+
return {{fn->getASTContext().getIdentifier(resultingName), rootValue}};
787+
}
788+
761789
//===----------------------------------------------------------------------===//
762790
// MARK: Tests
763791
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)