Skip to content

Commit 06d0973

Browse files
authored
Merge pull request #18315 from atrick/fix-argument-convention
Fix several incorrect uses of ApplySite::getArgumentConvention.
2 parents 571b29b + 89ed064 commit 06d0973

File tree

7 files changed

+19
-39
lines changed

7 files changed

+19
-39
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,11 +1937,6 @@ class ApplyInstBase<Impl, Base, true>
19371937
return OperandValueArrayRef(opsWithoutSelf);
19381938
}
19391939

1940-
/// Return the SILArgumentConvention for the given applied argument index.
1941-
SILArgumentConvention getArgumentConvention(unsigned index) const {
1942-
return getSubstCalleeConv().getSILArgumentConvention(index);
1943-
}
1944-
19451940
Optional<SILResultInfo> getSingleResult() const {
19461941
auto SubstCallee = getSubstCalleeType();
19471942
if (SubstCallee->getNumAllResults() != 1)
@@ -7680,7 +7675,7 @@ class ApplySite {
76807675
}
76817676

76827677
/// Return the applied argument index for the given operand.
7683-
unsigned getArgumentIndex(const Operand &oper) const {
7678+
unsigned getAppliedArgIndex(const Operand &oper) const {
76847679
assert(oper.getUser() == Inst);
76857680
assert(isArgumentOperand(oper));
76867681

@@ -7719,21 +7714,16 @@ class ApplySite {
77197714
/// Note: Passing an applied argument index into SILFunctionConvention, as
77207715
/// opposed to a function argument index, is incorrect.
77217716
unsigned getCalleeArgIndex(const Operand &oper) const {
7722-
return getCalleeArgIndexOfFirstAppliedArg() + getArgumentIndex(oper);
7717+
return getCalleeArgIndexOfFirstAppliedArg() + getAppliedArgIndex(oper);
77237718
}
77247719

77257720
/// Return the SILArgumentConvention for the given applied argument operand.
77267721
SILArgumentConvention getArgumentConvention(Operand &oper) const {
77277722
unsigned calleeArgIdx =
7728-
getCalleeArgIndexOfFirstAppliedArg() + getArgumentIndex(oper);
7723+
getCalleeArgIndexOfFirstAppliedArg() + getAppliedArgIndex(oper);
77297724
return getSubstCalleeConv().getSILArgumentConvention(calleeArgIdx);
77307725
}
77317726

7732-
// FIXME: This is incorrect. It will be removed in the next commit.
7733-
SILArgumentConvention getArgumentConvention(unsigned index) const {
7734-
return getSubstCalleeConv().getSILArgumentConvention(index);
7735-
}
7736-
77377727
/// Return true if 'self' is an applied argument.
77387728
bool hasSelfArgument() const {
77397729
switch (Inst->getKind()) {

lib/SIL/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2709,7 +2709,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
27092709
auto isConsumingOrMutatingApplyUse = [](Operand *use) -> bool {
27102710
ApplySite apply(use->getUser());
27112711
assert(apply && "Not an apply instruction kind");
2712-
auto conv = apply.getArgumentConvention(use->getOperandNumber() - 1);
2712+
auto conv = apply.getArgumentConvention(*use);
27132713
switch (conv) {
27142714
case SILArgumentConvention::Indirect_In_Guaranteed:
27152715
return false;

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ static SILInstruction *getOnlyDestroy(CopyBlockWithoutEscapingInst *CB) {
339339

340340
// If this an apply use, only handle unowned parameters.
341341
if (auto Apply = FullApplySite::isa(Inst)) {
342-
SILArgumentConvention Conv =
343-
Apply.getArgumentConvention(Apply.getCalleeArgIndex(*Use));
342+
SILArgumentConvention Conv = Apply.getArgumentConvention(*Use);
344343
if (Conv != SILArgumentConvention::Direct_Unowned)
345344
return nullptr;
346345
continue;

lib/SILOptimizer/Mandatory/DiagnoseStaticExclusivity.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -876,13 +876,10 @@ static void checkForViolationsAtInstruction(SILInstruction &I,
876876
// SILVerifier to better pinpoint the offending pass.
877877
if (auto *PAI = dyn_cast<PartialApplyInst>(&I)) {
878878
ApplySite apply(PAI);
879-
if (llvm::any_of(range(apply.getNumArguments()),
880-
[apply](unsigned argIdx) {
881-
unsigned calleeIdx =
882-
apply.getCalleeArgIndexOfFirstAppliedArg() + argIdx;
883-
return apply.getArgumentConvention(calleeIdx)
884-
== SILArgumentConvention::Indirect_InoutAliasable;
885-
})) {
879+
if (llvm::any_of(apply.getArgumentOperands(), [apply](Operand &oper) {
880+
return apply.getArgumentConvention(oper)
881+
== SILArgumentConvention::Indirect_InoutAliasable;
882+
})) {
886883
checkNoEscapePartialApply(PAI);
887884
}
888885
}
@@ -1115,8 +1112,7 @@ static void checkAccessedAddress(Operand *memOper, StorageMap &Accesses) {
11151112
return;
11161113

11171114
if (auto apply = ApplySite::isa(memInst)) {
1118-
SILArgumentConvention conv =
1119-
apply.getArgumentConvention(apply.getCalleeArgIndex(*memOper));
1115+
SILArgumentConvention conv = apply.getArgumentConvention(*memOper);
11201116
// Captured addresses currently use the @inout_aliasable convention. They
11211117
// are considered an access at any call site that uses the closure. However,
11221118
// those accesses are never explictly protected by access markers. Instead,

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static bool partialApplyEscapes(SILValue V, bool examineApply) {
285285
SILModuleConventions ModConv(*V->getModule());
286286
llvm::SmallVector<Operand *, 32> Worklist(V->use_begin(), V->use_end());
287287
while (!Worklist.empty()) {
288-
auto *Op = Worklist.pop_back_val();
288+
Operand *Op = Worklist.pop_back_val();
289289

290290
// These instructions do not cause the address to escape.
291291
if (!useCaptured(Op))
@@ -301,15 +301,14 @@ static bool partialApplyEscapes(SILValue V, bool examineApply) {
301301
continue;
302302
}
303303

304-
if (auto *Apply = dyn_cast<ApplyInst>(User)) {
304+
if (auto Apply = FullApplySite::isa(User)) {
305305
// Applying a function does not cause the function to escape.
306-
if (Op->getOperandNumber() == 0)
306+
if (!Apply.isArgumentOperand(*Op))
307307
continue;
308308

309309
// apply instructions do not capture the pointer when it is passed
310310
// indirectly
311-
if (Apply->getArgumentConvention(Op->getOperandNumber() - 1)
312-
.isIndirectConvention())
311+
if (Apply.getArgumentConvention(*Op).isIndirectConvention())
313312
continue;
314313

315314
// Optionally drill down into an apply to see if the operand is

lib/SILOptimizer/Transforms/CopyForwarding.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,18 @@ static SILArgumentConvention getAddressArgConvention(ApplyInst *Apply,
152152
Operand *&Oper) {
153153
Oper = nullptr;
154154
auto Args = Apply->getArgumentOperands();
155-
llvm::Optional<unsigned> FoundArgIdx;
156155
for (auto ArgIdx : indices(Args)) {
157156
if (Args[ArgIdx].get() != Address)
158157
continue;
159158

160-
FoundArgIdx = ArgIdx;
161159
assert(!Oper && "Address can only be passed once as an indirection.");
162160
Oper = &Args[ArgIdx];
163161
#ifdef NDEBUG
164162
break;
165163
#endif
166164
}
167165
assert(Oper && "Address value not passed as an argument to this call.");
168-
return Apply->getArgumentConvention(FoundArgIdx.getValue());
166+
return ApplySite(Apply).getArgumentConvention(*Oper);
169167
}
170168

171169
/// If the given instruction is a store, return the stored value.
@@ -1633,10 +1631,10 @@ bool TempRValueOptPass::collectLoads(
16331631
return false;
16341632

16351633
case SILInstructionKind::ApplyInst: {
1636-
auto *AI = cast<ApplyInst>(user);
1637-
auto Convention = AI->getArgumentConvention(userOp->getOperandNumber() - 1);
1634+
ApplySite apply(user);
1635+
auto Convention = apply.getArgumentConvention(*userOp);
16381636
if (Convention.isGuaranteedConvention()) {
1639-
loadInsts.insert(AI);
1637+
loadInsts.insert(user);
16401638
return true;
16411639
}
16421640
LLVM_DEBUG(llvm::dbgs() << " Temp consuming use may write/destroy "

lib/SILOptimizer/Utils/Existential.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ SILValue swift::getAddressOfStackInit(AllocStackInst *ASI,
152152
}
153153
if (isa<ApplyInst>(User) || isa<TryApplyInst>(User)) {
154154
// Ignore function calls which do not write to the stack location.
155-
auto Idx =
156-
Use->getOperandNumber() - ApplyInst::getArgumentOperandNumber();
157-
auto Conv = FullApplySite(User).getArgumentConvention(Idx);
155+
auto Conv = FullApplySite(User).getArgumentConvention(*Use);
158156
if (Conv != SILArgumentConvention::Indirect_In &&
159157
Conv != SILArgumentConvention::Indirect_In_Guaranteed)
160158
return SILValue();

0 commit comments

Comments
 (0)