Skip to content

Commit 5339a92

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 adcd4e5 commit 5339a92

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
@@ -2987,6 +2987,7 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
29872987
}
29882988
}
29892989

2990+
auto Target = SGM.getASTContext().LangOpts.Target;
29902991
auto genericSig =
29912992
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
29922993
: nullptr;
@@ -2995,6 +2996,14 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
29952996
genericEnv = nullptr;
29962997
}
29972998

2999+
// Add empty generic type parameter to match function signature on WebAssembly
3000+
if (!genericSig && Target.isOSBinFormatWasm()) {
3001+
auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext());
3002+
auto sig = GenericSignature::get(param, { });
3003+
genericSig = CanGenericSignature(sig);
3004+
genericEnv = sig.getGenericEnvironment();
3005+
}
3006+
29983007
// Build the signature of the thunk as expected by the keypath runtime.
29993008
auto signature = [&]() {
30003009
CanType loweredBaseTy, loweredPropTy;
@@ -3010,7 +3019,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30103019
SmallVector<SILParameterInfo, 2> params;
30113020
params.push_back({loweredBaseTy, paramConvention});
30123021
auto &C = SGM.getASTContext();
3013-
if (!indexes.empty())
3022+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3023+
if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm())
30143024
params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(),
30153025
ParameterConvention::Direct_Unowned});
30163026

@@ -3070,7 +3080,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30703080
}
30713081
auto baseArg = entry->createFunctionArgument(baseArgTy);
30723082
SILValue indexPtrArg;
3073-
if (!indexes.empty()) {
3083+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3084+
if (!indexes.empty() || Target.isOSBinFormatWasm()) {
30743085
auto indexArgTy =
30753086
subSGF.silConv.getSILType(signature->getParameters()[1], signature,
30763087
subSGF.F.getTypeExpansionContext());
@@ -3160,6 +3171,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31603171
}
31613172
}
31623173

3174+
auto Target = SGM.getASTContext().LangOpts.Target;
31633175
auto genericSig =
31643176
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
31653177
: nullptr;
@@ -3168,6 +3180,14 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31683180
genericEnv = nullptr;
31693181
}
31703182

3183+
// Add empty generic type parameter to match function signature on WebAssembly
3184+
if (!genericSig && Target.isOSBinFormatWasm()) {
3185+
auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext());
3186+
auto sig = GenericSignature::get(param, { });
3187+
genericSig = CanGenericSignature(sig);
3188+
genericEnv = sig.getGenericEnvironment();
3189+
}
3190+
31713191
// Build the signature of the thunk as expected by the keypath runtime.
31723192
auto signature = [&]() {
31733193
CanType loweredBaseTy, loweredPropTy;
@@ -3193,7 +3213,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31933213
? ParameterConvention::Indirect_Inout
31943214
: paramConvention});
31953215
// indexes
3196-
if (!indexes.empty())
3216+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3217+
if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm())
31973218
params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(),
31983219
ParameterConvention::Direct_Unowned});
31993220

@@ -3249,7 +3270,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32493270
auto baseArg = entry->createFunctionArgument(baseArgTy);
32503271
SILValue indexPtrArg;
32513272

3252-
if (!indexes.empty()) {
3273+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3274+
if (!indexes.empty() || Target.isOSBinFormatWasm()) {
32533275
auto indexArgTy =
32543276
subSGF.silConv.getSILType(signature->getParameters()[2], signature,
32553277
subSGF.getTypeExpansionContext());
@@ -3342,6 +3364,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
33423364
return;
33433365
}
33443366

3367+
auto Target = SGM.getASTContext().LangOpts.Target;
33453368
auto genericSig =
33463369
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
33473370
: nullptr;
@@ -3351,6 +3374,14 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
33513374
genericEnv = nullptr;
33523375
}
33533376

3377+
// Add empty generic type parameter to match function signature on WebAssembly
3378+
if (!genericSig && Target.isOSBinFormatWasm()) {
3379+
auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext());
3380+
auto sig = GenericSignature::get(param, { });
3381+
genericSig = CanGenericSignature(sig);
3382+
genericEnv = sig.getGenericEnvironment();
3383+
}
3384+
33543385
auto &C = SGM.getASTContext();
33553386
auto unsafeRawPointerTy = C.getUnsafeRawPointerType()->getCanonicalType();
33563387
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)