Skip to content

Commit 335c349

Browse files
Merge pull request #1709 from kateinoigakukun/katei/fix-keypath-opt
[WASM] Fix KeyPath optimization bug around signature match
2 parents 353dcf2 + 81751d9 commit 335c349

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/IRGen/GenKeyPath.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ getAccessorForComputedComponent(IRGenModule &IGM,
250250
forwardingSubs,
251251
&ignoreWitnessMetadata,
252252
forwardedArgs);
253+
} else if (IGM.Triple.isOSBinFormatWasm()) {
254+
// wasm: Add null swift.type pointer to match signature even when there is
255+
// no generic environment.
256+
forwardedArgs.add(llvm::ConstantPointerNull::get(IGM.TypeMetadataPtrTy));
253257
}
254258
auto fnPtr = FunctionPointer::forDirect(IGM, accessorFn,
255259
accessor->getLoweredFunctionType());

lib/SILOptimizer/Utils/KeyPathProjector.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,22 @@ class GettablePropertyProjector : public ComponentProjector {
237237
assert(getter->getArguments().size() == 2 + target.isOSBinFormatWasm());
238238

239239
auto ref = builder.createFunctionRef(loc, getter);
240-
builder.createApply(loc, ref, subs, {addr, parentValue});
240+
241+
std::vector<SILValue> args{addr, parentValue};
242+
// FIXME(wasm): For wasm, KeyPath getter always take indices parameter
243+
// to match callee and caller signature. So need to pass stub pointer.
244+
// See also: getOrCreateKeyPathSetter and getOrCreateKeyPathGetter
245+
if (builder.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
246+
auto IntTy = SILType::getBuiltinIntegerType(32, builder.getASTContext());
247+
auto UnsafeRawPointerTy = SILType::getRawPointerType(builder.getASTContext());
248+
auto zeroVal = SILValue(builder.createIntegerLiteral(loc, IntTy, 0));
249+
auto stackBuffer = SILValue(builder.createAllocStack(loc, IntTy));
250+
builder.createStore(loc, zeroVal, stackBuffer, StoreOwnershipQualifier::Unqualified);
251+
auto nonePointer = builder.createUncheckedAddrCast(loc, stackBuffer, UnsafeRawPointerTy);
252+
args.push_back(SILValue(nonePointer));
253+
}
254+
255+
builder.createApply(loc, ref, subs, args);
241256

242257
// If we were previously accessing a class member, we're done now.
243258
insertEndAccess(beginAccess, builder);

0 commit comments

Comments
 (0)