Skip to content

Commit b994198

Browse files
kateinoigakukunrunner
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 c96bc37 commit b994198

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
@@ -3023,6 +3023,7 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30233023
}
30243024
}
30253025

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

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

@@ -3106,7 +3116,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
31063116
}
31073117
auto baseArg = entry->createFunctionArgument(baseArgTy);
31083118
SILValue indexPtrArg;
3109-
if (!indexes.empty()) {
3119+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3120+
if (!indexes.empty() || Target.isOSBinFormatWasm()) {
31103121
auto indexArgTy =
31113122
subSGF.silConv.getSILType(signature->getParameters()[1], signature,
31123123
subSGF.F.getTypeExpansionContext());
@@ -3196,6 +3207,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31963207
}
31973208
}
31983209

3210+
auto Target = SGM.getASTContext().LangOpts.Target;
31993211
auto genericSig =
32003212
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
32013213
: nullptr;
@@ -3204,6 +3216,14 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32043216
genericEnv = nullptr;
32053217
}
32063218

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

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

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

3403+
auto Target = SGM.getASTContext().LangOpts.Target;
33813404
auto genericSig =
33823405
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
33833406
: nullptr;
@@ -3387,6 +3410,14 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
33873410
genericEnv = nullptr;
33883411
}
33893412

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