Skip to content

Commit 8fd70ec

Browse files
committed
[region-isolation] Create helpers for inferring name from value and infer name/root from value.
This just cleans up the code by not exposing the details from the VariableNameInferrer outside of the utilities. It also makes it easier to write conditional code that uses these helpers by returning an optional, so instead of having a long setup + an if statement, we just have an if statement optional check.
1 parent 7f43c3a commit 8fd70ec

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

lib/SILOptimizer/Mandatory/TransferNonSendable.cpp

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,34 @@ static Expr *inferArgumentExprFromApplyExpr(ApplyExpr *sourceApply,
8888
return foundExpr;
8989
}
9090

91+
static std::optional<Identifier> inferNameFromValue(SILValue value) {
92+
auto *fn = value->getFunction();
93+
if (!fn)
94+
return {};
95+
VariableNameInferrer::Options options;
96+
options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
97+
SmallString<64> resultingName;
98+
VariableNameInferrer inferrer(fn, options, resultingName);
99+
if (!inferrer.inferByWalkingUsesToDefsReturningRoot(value))
100+
return {};
101+
return fn->getASTContext().getIdentifier(resultingName);
102+
}
103+
104+
static std::optional<std::pair<Identifier, SILValue>>
105+
inferNameAndRootFromValue(SILValue value) {
106+
auto *fn = value->getFunction();
107+
if (!fn)
108+
return {};
109+
VariableNameInferrer::Options options;
110+
options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
111+
SmallString<64> resultingName;
112+
VariableNameInferrer inferrer(fn, options, resultingName);
113+
SILValue rootValue = inferrer.inferByWalkingUsesToDefsReturningRoot(value);
114+
if (!rootValue)
115+
return {};
116+
return {{fn->getASTContext().getIdentifier(resultingName), rootValue}};
117+
}
118+
91119
//===----------------------------------------------------------------------===//
92120
// MARK: Diagnostics
93121
//===----------------------------------------------------------------------===//
@@ -748,25 +776,19 @@ void UseAfterTransferDiagnosticInferrer::init(const Operand *op) {
748776
if (auto *sourceApply = loc.getAsASTNode<ApplyExpr>()) {
749777
// Before we do anything further, see if we can find a name and emit a name
750778
// error.
751-
SmallString<64> resultingName;
752-
VariableNameInferrer::Options options;
753-
options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
754-
VariableNameInferrer inferrer(op->getFunction(), options, resultingName);
755-
auto &astContext = op->getFunction()->getASTContext();
756-
if (auto rootValue =
757-
inferrer.inferByWalkingUsesToDefsReturningRoot(op->get())) {
758-
if (auto *svi = dyn_cast<SingleValueInstruction>(rootValue)) {
779+
if (auto rootValueAndName = inferNameAndRootFromValue(op->get())) {
780+
if (auto *svi =
781+
dyn_cast<SingleValueInstruction>(rootValueAndName->second)) {
759782
return appendUseInfo(UseDiagnosticInfo::forNamedIsolationCrossing(
760-
baseLoc, svi->getLoc(),
761-
astContext.getIdentifier(inferrer.getName()),
783+
baseLoc, svi->getLoc(), rootValueAndName->first,
762784
*sourceApply->getIsolationCrossing()));
763785
}
764786

765-
if (auto *fArg = dyn_cast<SILFunctionArgument>(rootValue)) {
787+
if (auto *fArg =
788+
dyn_cast<SILFunctionArgument>(rootValueAndName->second)) {
766789
return appendUseInfo(UseDiagnosticInfo::forNamedIsolationCrossing(
767790
baseLoc, RegularLocation(fArg->getDecl()->getLoc()),
768-
astContext.getIdentifier(inferrer.getName()),
769-
*sourceApply->getIsolationCrossing()));
791+
rootValueAndName->first, *sourceApply->getIsolationCrossing()));
770792
}
771793
}
772794

@@ -1105,13 +1127,8 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
11051127

11061128
// See if we can infer a name from the value.
11071129
SmallString<64> resultingName;
1108-
VariableNameInferrer::Options options;
1109-
options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
1110-
VariableNameInferrer inferrer(op->getFunction(), options, resultingName);
1111-
if (inferrer.inferByWalkingUsesToDefsReturningRoot(op->get())) {
1112-
auto &astContext = op->getFunction()->getASTContext();
1113-
diagnosticInfo = UseDiagnosticInfo::forNamed(
1114-
astContext.getIdentifier(inferrer.getName()), *isolation);
1130+
if (auto name = inferNameFromValue(op->get())) {
1131+
diagnosticInfo = UseDiagnosticInfo::forNamed(*name, *isolation);
11151132
return true;
11161133
}
11171134

0 commit comments

Comments
 (0)