Skip to content

Commit 16c2996

Browse files
kateinoigakukunAnka
authored andcommitted
[wasm][SIL/IRGen] HACK: Always forward extra argument to match callee and caller signature
This is a legacy solution for the KeyPath calling convention problem described in https://forums.swift.org/t/wasm-support/16087/21. This patch will be replaced by swiftlang#66273
1 parent 71a61c4 commit 16c2996

File tree

5 files changed

+77
-10
lines changed

5 files changed

+77
-10
lines changed

lib/IRGen/GenKeyPath.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ getAccessorForComputedComponent(IRGenModule &IGM,
224224
componentArgsBuf = params.claimNext();
225225
// Pass the argument pointer down to the underlying function, if it
226226
// wants it.
227-
if (hasSubscriptIndices) {
227+
// Always forward extra argument to match callee and caller signature on WebAssembly
228+
if (hasSubscriptIndices || IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
228229
forwardedArgs.add(componentArgsBuf);
229230
}
230231
break;
@@ -250,6 +251,10 @@ getAccessorForComputedComponent(IRGenModule &IGM,
250251
forwardingSubs,
251252
&ignoreWitnessMetadata,
252253
forwardedArgs);
254+
} else if (IGM.Triple.isOSBinFormatWasm()) {
255+
// wasm: Add null swift.type pointer to match signature even when there is
256+
// no generic environment.
257+
forwardedArgs.add(llvm::ConstantPointerNull::get(IGM.TypeMetadataPtrTy));
253258
}
254259
auto fnPtr =
255260
FunctionPointer::forDirect(IGM, accessorFn, /*secondaryValue*/ nullptr,

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,9 @@ void verifyKeyPathComponent(SILModule &M,
304304
SILFunctionTypeRepresentation::Thin,
305305
"getter should be a thin function");
306306

307-
require(substGetterType->getNumParameters() == 1 + hasIndices,
308-
"getter should have one parameter");
307+
// FIXME(katei): Disabled for now. Will be replaced by keypath cc
308+
// require(substGetterType->getNumParameters() == 1 + hasIndices,
309+
// "getter should have one parameter");
309310
auto baseParam = substGetterType->getParameters()[0];
310311
require(baseParam.getConvention() == normalArgConvention,
311312
"getter base parameter should have normal arg convention");
@@ -356,8 +357,9 @@ void verifyKeyPathComponent(SILModule &M,
356357
SILFunctionTypeRepresentation::Thin,
357358
"setter should be a thin function");
358359

359-
require(substSetterType->getNumParameters() == 2 + hasIndices,
360-
"setter should have two parameters");
360+
// FIXME(katei): Disabled for now. Will be replaced by keypath cc
361+
// require(substSetterType->getNumParameters() == 2 + hasIndices,
362+
// "setter should have two parameters");
361363

362364
auto newValueParam = substSetterType->getParameters()[0];
363365
// TODO: This should probably be unconditionally +1 when we

lib/SILGen/SILGenExpr.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,6 +3024,7 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30243024
}
30253025
}
30263026

3027+
auto Target = SGM.getASTContext().LangOpts.Target;
30273028
auto genericSig =
30283029
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
30293030
: nullptr;
@@ -3032,6 +3033,14 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30323033
genericEnv = nullptr;
30333034
}
30343035

3036+
// Add empty generic type parameter to match function signature on WebAssembly
3037+
if (!genericSig && Target.isOSBinFormatWasm()) {
3038+
auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext());
3039+
auto sig = GenericSignature::get(param, { });
3040+
genericSig = CanGenericSignature(sig);
3041+
genericEnv = sig.getGenericEnvironment();
3042+
}
3043+
30353044
// Build the signature of the thunk as expected by the keypath runtime.
30363045
auto signature = [&]() {
30373046
CanType loweredBaseTy, loweredPropTy;
@@ -3047,7 +3056,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30473056
SmallVector<SILParameterInfo, 2> params;
30483057
params.push_back({loweredBaseTy, paramConvention});
30493058
auto &C = SGM.getASTContext();
3050-
if (!indexes.empty())
3059+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3060+
if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm())
30513061
params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(),
30523062
ParameterConvention::Direct_Unowned});
30533063

@@ -3107,7 +3117,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
31073117
}
31083118
auto baseArg = entry->createFunctionArgument(baseArgTy);
31093119
SILValue indexPtrArg;
3110-
if (!indexes.empty()) {
3120+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3121+
if (!indexes.empty() || Target.isOSBinFormatWasm()) {
31113122
auto indexArgTy =
31123123
subSGF.silConv.getSILType(signature->getParameters()[1], signature,
31133124
subSGF.F.getTypeExpansionContext());
@@ -3197,6 +3208,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31973208
}
31983209
}
31993210

3211+
auto Target = SGM.getASTContext().LangOpts.Target;
32003212
auto genericSig =
32013213
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
32023214
: nullptr;
@@ -3205,6 +3217,14 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32053217
genericEnv = nullptr;
32063218
}
32073219

3220+
// Add empty generic type parameter to match function signature on WebAssembly
3221+
if (!genericSig && Target.isOSBinFormatWasm()) {
3222+
auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext());
3223+
auto sig = GenericSignature::get(param, { });
3224+
genericSig = CanGenericSignature(sig);
3225+
genericEnv = sig.getGenericEnvironment();
3226+
}
3227+
32083228
// Build the signature of the thunk as expected by the keypath runtime.
32093229
auto signature = [&]() {
32103230
CanType loweredBaseTy, loweredPropTy;
@@ -3230,7 +3250,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32303250
? ParameterConvention::Indirect_Inout
32313251
: paramConvention});
32323252
// indexes
3233-
if (!indexes.empty())
3253+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3254+
if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm())
32343255
params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(),
32353256
ParameterConvention::Direct_Unowned});
32363257

@@ -3286,7 +3307,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32863307
auto baseArg = entry->createFunctionArgument(baseArgTy);
32873308
SILValue indexPtrArg;
32883309

3289-
if (!indexes.empty()) {
3310+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3311+
if (!indexes.empty() || Target.isOSBinFormatWasm()) {
32903312
auto indexArgTy =
32913313
subSGF.silConv.getSILType(signature->getParameters()[2], signature,
32923314
subSGF.getTypeExpansionContext());
@@ -3379,6 +3401,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
33793401
return;
33803402
}
33813403

3404+
auto Target = SGM.getASTContext().LangOpts.Target;
33823405
auto genericSig =
33833406
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
33843407
: nullptr;
@@ -3388,6 +3411,14 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
33883411
genericEnv = nullptr;
33893412
}
33903413

3414+
// Add empty generic type parameter to match function signature on WebAssembly
3415+
if (!genericSig && Target.isOSBinFormatWasm()) {
3416+
auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext());
3417+
auto sig = GenericSignature::get(param, { });
3418+
genericSig = CanGenericSignature(sig);
3419+
genericEnv = sig.getGenericEnvironment();
3420+
}
3421+
33913422
auto &C = SGM.getASTContext();
33923423
auto unsafeRawPointerTy = C.getUnsafeRawPointerType()->getCanonicalType();
33933424
auto boolTy = C.getBoolType()->getCanonicalType();

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,13 @@ bool SILCombiner::tryOptimizeKeypathKVCString(ApplyInst *AI,
505505
}
506506

507507
bool SILCombiner::tryOptimizeKeypath(ApplyInst *AI) {
508+
// FIXME(katei): Disable for WebAssembly for now because
509+
// KeyPath cc is unstable and KeyPathProjector hask violates
510+
// some assert assumptions
511+
SILModule &M = AI->getModule();
512+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm())
513+
return false;
514+
508515
if (SILFunction *callee = AI->getReferencedFunctionOrNull()) {
509516
return tryOptimizeKeypathApplication(AI, callee);
510517
}
@@ -550,6 +557,13 @@ bool SILCombiner::tryOptimizeKeypath(ApplyInst *AI) {
550557
/// %addr = struct_element_addr/ref_element_addr %root_object
551558
/// // use %inout_addr
552559
bool SILCombiner::tryOptimizeInoutKeypath(BeginApplyInst *AI) {
560+
// FIXME(katei): Disable for WebAssembly for now because
561+
// KeyPath cc is unstable and KeyPathProjector hask violates
562+
// some assert assumptions
563+
SILModule &M = AI->getModule();
564+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm())
565+
return false;
566+
553567
// Disable in OSSA because KeyPathProjector is not fully ported
554568
if (AI->getFunction()->hasOwnership())
555569
return false;

lib/SILOptimizer/Utils/KeyPathProjector.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,22 @@ class GettablePropertyProjector : public ComponentProjector {
233233
assert(getter->getConventions().getNumSILArguments());
234234

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

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

0 commit comments

Comments
 (0)