Skip to content

Commit c5259ad

Browse files
committed
[region-isolation] Remove getExprForPartitionOp and instead just use SILLocation.
getExprForPartitionOp(...) just returned the expression from the loc of op.currentInst: SILInstruction *sourceInstr = op.getSourceInst(/*assertNonNull=*/true); Expr *expr = sourceInstr->getLoc().getAsASTNode<Expr>(); Instead of mucking around with exprs, just use the SILLocation from the SILInstruction. I also changed how we unique transfer instructions to just use the transfer instruction itself instead of the AST/Expr of the transfer instruction.
1 parent 389078d commit c5259ad

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

include/swift/SILOptimizer/Utils/PartitionUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ class PartitionOp {
200200
return sourceInst;
201201
}
202202

203+
SILLocation getSourceLoc() const { return getSourceInst(true)->getLoc(); }
204+
203205
Expr *getSourceExpr() const {
204206
return sourceExpr;
205207
}

lib/SILOptimizer/Mandatory/TransferNonSendable.cpp

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,6 @@ struct TermArgSources {
169169

170170
} // namespace
171171

172-
/// Used for generating informative diagnostics.
173-
static Expr *getExprForPartitionOp(const PartitionOp &op) {
174-
SILInstruction *sourceInstr = op.getSourceInst(/*assertNonNull=*/true);
175-
Expr *expr = sourceInstr->getLoc().getAsASTNode<Expr>();
176-
assert(expr && "PartitionOp's source location should correspond to"
177-
"an AST node");
178-
return expr;
179-
}
180-
181172
static bool isProjectedFromAggregate(SILValue value) {
182173
assert(value->getType().isAddress());
183174
UseDefChainVisitor visitor;
@@ -1611,9 +1602,9 @@ class TransferRequireAccumulator {
16111602
assert(false && "no transfers besides callsites implemented yet");
16121603

16131604
// Default to a more generic diagnostic if we can't find the callsite.
1614-
auto expr = getExprForPartitionOp(transferOp);
1605+
auto loc = transferOp.getSourceLoc();
16151606
auto diag = fn->getASTContext().Diags.diagnose(
1616-
expr->getLoc(), diag::transfer_yields_race, numDisplayed,
1607+
loc.getSourceLoc(), diag::transfer_yields_race, numDisplayed,
16171608
numDisplayed != 1, numHidden > 0, numHidden);
16181609
if (auto sourceExpr = transferOp.getSourceExpr())
16191610
diag.highlight(sourceExpr->getSourceRange());
@@ -1626,10 +1617,10 @@ class TransferRequireAccumulator {
16261617
// transfer...
16271618
if (numRequiresToProcess-- == 0)
16281619
break;
1629-
auto expr = getExprForPartitionOp(requireOp);
1620+
auto loc = requireOp.getSourceLoc();
16301621
fn->getASTContext()
1631-
.Diags.diagnose(expr->getLoc(), diag::possible_racy_access_site)
1632-
.highlight(expr->getSourceRange());
1622+
.Diags.diagnose(loc.getSourceLoc(), diag::possible_racy_access_site)
1623+
.highlight(loc.getSourceRange());
16331624
}
16341625
}
16351626
}
@@ -2065,18 +2056,15 @@ class PartitionAnalysis {
20652056
translator.sortUniqueNeverTransferredValues();
20662057
}
20672058

2068-
/// Track the AST exprs that have already had diagnostics emitted about.
2069-
llvm::DenseSet<Expr *> emittedExprs;
2070-
2071-
/// Check if a diagnostic has already been emitted for \p expr.
2072-
bool hasBeenEmitted(Expr *expr) {
2073-
if (auto castExpr = dyn_cast<ImplicitConversionExpr>(expr))
2074-
return hasBeenEmitted(castExpr->getSubExpr());
2059+
/// Track the transfer insts that have already had diagnostics emitted about.
2060+
llvm::DenseSet<SILInstruction *> emittedTransferInsts;
20752061

2076-
if (emittedExprs.contains(expr))
2077-
return true;
2078-
emittedExprs.insert(expr);
2079-
return false;
2062+
/// Returns true if a diagnostic has already been emitted for the transferred
2063+
/// instruction \p transferredInst.
2064+
///
2065+
/// If we return false, we insert \p transferredInst into emittedTransferInst.
2066+
bool hasBeenEmitted(SILInstruction *transferredInst) {
2067+
return !emittedTransferInsts.insert(transferredInst).second;
20802068
}
20812069

20822070
/// Once we have reached a fixpoint, this routine runs over all blocks again
@@ -2098,24 +2086,22 @@ class PartitionAnalysis {
20982086
Partition workingPartition = blockState.getEntryPartition();
20992087
PartitionOpEvaluator eval(workingPartition);
21002088
eval.failureCallback = /*handleFailure=*/
2101-
[&](const PartitionOp &partitionOp, TrackableValueID transferredVal,
2102-
SILInstruction *transferringInst) {
2103-
auto expr = getExprForPartitionOp(partitionOp);
2089+
[&](const PartitionOp &partitionOp, TrackableValueID transferredVal,
2090+
SILInstruction *transferringInst) {
2091+
// Ensure that multiple transfers at the same AST node are only
2092+
// entered once into the race tracer
2093+
if (hasBeenEmitted(partitionOp.getSourceInst(true)))
2094+
return;
21042095

2105-
// Ensure that multiple transfers at the same AST node are only
2106-
// entered once into the race tracer
2107-
if (hasBeenEmitted(expr))
2108-
return;
2109-
2110-
LLVM_DEBUG(llvm::dbgs()
2111-
<< " Emitting Use After Transfer Error!\n"
2112-
<< " ID: %%" << transferredVal << "\n"
2113-
<< " Rep: "
2114-
<< *translator.getValueForId(transferredVal)
2096+
LLVM_DEBUG(llvm::dbgs()
2097+
<< " Emitting Use After Transfer Error!\n"
2098+
<< " ID: %%" << transferredVal << "\n"
2099+
<< " Rep: "
2100+
<< *translator.getValueForId(transferredVal)
21152101
->getRepresentative());
21162102

21172103
raceTracer.traceUseOfTransferredValue(partitionOp, transferredVal);
2118-
};
2104+
};
21192105
eval.transferredNonTransferrableCallback =
21202106
[&](const PartitionOp &partitionOp, TrackableValueID transferredVal) {
21212107
LLVM_DEBUG(llvm::dbgs()
@@ -2124,9 +2110,9 @@ class PartitionAnalysis {
21242110
<< "Rep: "
21252111
<< *translator.getValueForId(transferredVal)
21262112
->getRepresentative());
2127-
auto expr = getExprForPartitionOp(partitionOp);
21282113
function->getASTContext().Diags.diagnose(
2129-
expr->getLoc(), diag::arg_region_transferred);
2114+
partitionOp.getSourceLoc().getSourceLoc(),
2115+
diag::arg_region_transferred);
21302116
};
21312117
eval.nonTransferrableElements = translator.getNeverTransferredValues();
21322118
eval.isActorDerivedCallback = [&](Element element) -> bool {

0 commit comments

Comments
 (0)