Skip to content

Commit bf43ee0

Browse files
Merge pull request #61585 from nate-chandler/opaque-values/1/20221014
[OpaqueValues] Fixed index of self argument.
2 parents 0d0be26 + ca43d71 commit bf43ee0

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
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: 15 additions & 2 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"
@@ -224,6 +225,17 @@ struct DumpFunction : UnitTest {
224225
void invoke(Arguments &arguments) override { getFunction()->dump(); }
225226
};
226227

228+
// Arguments: NONE
229+
// Dumps: the index of the self argument of the current function
230+
struct FunctionGetSelfArgumentIndex : UnitTest {
231+
FunctionGetSelfArgumentIndex(UnitTestRunner *pass) : UnitTest(pass) {}
232+
void invoke(Arguments &arguments) override {
233+
auto index =
234+
SILFunction_getSelfArgumentIndex(BridgedFunction{getFunction()});
235+
llvm::errs() << "self argument index = " << index << "\n";
236+
}
237+
};
238+
227239
/// [new_tests] Add the new UnitTest subclass above this line.
228240

229241
class UnitTestRunner : public SILFunctionTransform {
@@ -238,10 +250,9 @@ class UnitTestRunner : public SILFunctionTransform {
238250
llvm::errs() << components[index];
239251
if (index != size - 1) {
240252
llvm::errs() << ", ";
241-
} else {
242-
llvm::errs() << "\n";
243253
}
244254
}
255+
llvm::errs() << "\n";
245256
}
246257

247258
template <typename Doit>
@@ -260,6 +271,8 @@ class UnitTestRunner : public SILFunctionTransform {
260271
CanonicalizeOSSALifetimeTest)
261272
ADD_UNIT_TEST_SUBCLASS("visit-adjacent-reborrows-of-phi",
262273
VisitAdjacentReborrowsOfPhiTest)
274+
ADD_UNIT_TEST_SUBCLASS("function-get-self-argument-index",
275+
FunctionGetSelfArgumentIndex)
263276
/// [new_tests] Add the new mapping from string to subclass above this line.
264277

265278
#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)