Skip to content

Commit 4156e09

Browse files
committed
[OpaqueValues] Fixed index of self argument.
Previously, SILFunction_getSelfArgumentIndex was directly using the function type rather than interacting with the function convention. Here, it is made to interact with the convention.
1 parent ab2364f commit 4156e09

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

lib/SIL/Utils/SILBridging.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,12 @@ SwiftInt SILFunction_numParameterArguments(BridgedFunction function) {
183183
}
184184

185185
SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function) {
186+
SILFunction *f = castToFunction(function);
187+
SILFunctionConventions conv(f->getConventionsInContext());
186188
CanSILFunctionType fTy = castToFunction(function)->getLoweredFunctionType();
187189
if (!fTy->hasSelfParam())
188190
return -1;
189-
return fTy->getNumParameters() + fTy->getNumIndirectFormalResults() - 1;
191+
return conv.getNumParameters() + conv.getNumIndirectSILResults() - 1;
190192
}
191193

192194
SwiftInt SILFunction_getNumSILArguments(BridgedFunction function) {

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "swift/Basic/TaggedUnion.h"
6969
#include "swift/SIL/SILArgumentArrayRef.h"
7070
#include "swift/SIL/SILBasicBlock.h"
71+
#include "swift/SIL/SILBridging.h"
7172
#include "swift/SIL/SILFunction.h"
7273
#include "swift/SIL/SILInstruction.h"
7374
#include "swift/SILOptimizer/PassManager/Passes.h"
@@ -216,6 +217,17 @@ struct VisitAdjacentReborrowsOfPhiTest : UnitTest {
216217
}
217218
};
218219

220+
// Arguments: NONE
221+
// Dumps: the index of the self argument of the current function
222+
struct FunctionGetSelfArgumentIndex : UnitTest {
223+
FunctionGetSelfArgumentIndex(UnitTestRunner *pass) : UnitTest(pass) {}
224+
void invoke(Arguments &arguments) override {
225+
auto index =
226+
SILFunction_getSelfArgumentIndex(BridgedFunction{getFunction()});
227+
llvm::errs() << "self argument index = " << index << "\n";
228+
}
229+
};
230+
219231
/// [new_tests] Add the new UnitTest subclass above this line.
220232

221233
class UnitTestRunner : public SILFunctionTransform {
@@ -249,6 +261,8 @@ class UnitTestRunner : public SILFunctionTransform {
249261
CanonicalizeOSSALifetimeTest)
250262
ADD_UNIT_TEST_SUBCLASS("visit-adjacent-reborrows-of-phi",
251263
VisitAdjacentReborrowsOfPhiTest)
264+
ADD_UNIT_TEST_SUBCLASS("function-get-self-argument-index",
265+
FunctionGetSelfArgumentIndex)
252266
/// [new_tests] Add the new mapping from string to subclass above this line.
253267

254268
#undef ADD_UNIT_TEST_SUBCLASS
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %target-sil-opt -enable-sil-opaque-values -unit-test-runner %s 2>&1 | %FileCheck %s
2+
3+
import Builtin
4+
5+
class C {}
6+
7+
// Verify that SILFunction_getSelfArgumentIndex respects SILFunctionConventions
8+
// and returns right value even in the presence of indirect results.
9+
//
10+
// CHECK-LABEL: begin running test 1 of {{[^,]+}} on f: function-get-self-argument-index
11+
// The correct value is 0 here when opaque values are enabled because the result
12+
// is just returned. It could mistakenly be calculated as 1 though because
13+
// after AddressLowering there will be a new argument at index 0 for the
14+
// indirect result.
15+
// CHECK: self argument index = 0
16+
// CHECK-LABEL: end running test 1 of {{[^,]+}} on f: function-get-self-argument-index
17+
sil [ossa] @f : $@convention(method) <T> (@guaranteed C) -> @out T {
18+
entry(%instance : @guaranteed $C):
19+
test_specification "function-get-self-argument-index"
20+
unreachable
21+
}

0 commit comments

Comments
 (0)