Skip to content

Commit f0e8df5

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 b9da5bd commit f0e8df5

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
@@ -299,8 +299,9 @@ void verifyKeyPathComponent(SILModule &M,
299299
SILFunctionTypeRepresentation::Thin,
300300
"getter should be a thin function");
301301

302-
require(substGetterType->getNumParameters() == 1 + hasIndices,
303-
"getter should have one parameter");
302+
// FIXME(katei): Disabled for now. Will be replaced by keypath cc
303+
// require(substGetterType->getNumParameters() == 1 + hasIndices,
304+
// "getter should have one parameter");
304305
auto baseParam = substGetterType->getParameters()[0];
305306
require(baseParam.getConvention() == normalArgConvention,
306307
"getter base parameter should have normal arg convention");
@@ -351,8 +352,9 @@ void verifyKeyPathComponent(SILModule &M,
351352
SILFunctionTypeRepresentation::Thin,
352353
"setter should be a thin function");
353354

354-
require(substSetterType->getNumParameters() == 2 + hasIndices,
355-
"setter should have two parameters");
355+
// FIXME(katei): Disabled for now. Will be replaced by keypath cc
356+
// require(substSetterType->getNumParameters() == 2 + hasIndices,
357+
// "setter should have two parameters");
356358

357359
auto newValueParam = substSetterType->getParameters()[0];
358360
// 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
@@ -2995,6 +2995,7 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
29952995
}
29962996
}
29972997

2998+
auto Target = SGM.getASTContext().LangOpts.Target;
29982999
auto genericSig =
29993000
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
30003001
: nullptr;
@@ -3003,6 +3004,14 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30033004
genericEnv = nullptr;
30043005
}
30053006

3007+
// Add empty generic type parameter to match function signature on WebAssembly
3008+
if (!genericSig && Target.isOSBinFormatWasm()) {
3009+
auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext());
3010+
auto sig = GenericSignature::get(param, { });
3011+
genericSig = CanGenericSignature(sig);
3012+
genericEnv = sig.getGenericEnvironment();
3013+
}
3014+
30063015
// Build the signature of the thunk as expected by the keypath runtime.
30073016
auto signature = [&]() {
30083017
CanType loweredBaseTy, loweredPropTy;
@@ -3018,7 +3027,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30183027
SmallVector<SILParameterInfo, 2> params;
30193028
params.push_back({loweredBaseTy, paramConvention});
30203029
auto &C = SGM.getASTContext();
3021-
if (!indexes.empty())
3030+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3031+
if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm())
30223032
params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(),
30233033
ParameterConvention::Direct_Unowned});
30243034

@@ -3078,7 +3088,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
30783088
}
30793089
auto baseArg = entry->createFunctionArgument(baseArgTy);
30803090
SILValue indexPtrArg;
3081-
if (!indexes.empty()) {
3091+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3092+
if (!indexes.empty() || Target.isOSBinFormatWasm()) {
30823093
auto indexArgTy = signature->getParameters()[1].getSILStorageType(
30833094
SGM.M, signature, subSGF.F.getTypeExpansionContext());
30843095
indexPtrArg = entry->createFunctionArgument(indexArgTy);
@@ -3167,6 +3178,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31673178
}
31683179
}
31693180

3181+
auto Target = SGM.getASTContext().LangOpts.Target;
31703182
auto genericSig =
31713183
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
31723184
: nullptr;
@@ -3175,6 +3187,14 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31753187
genericEnv = nullptr;
31763188
}
31773189

3190+
// Add empty generic type parameter to match function signature on WebAssembly
3191+
if (!genericSig && Target.isOSBinFormatWasm()) {
3192+
auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext());
3193+
auto sig = GenericSignature::get(param, { });
3194+
genericSig = CanGenericSignature(sig);
3195+
genericEnv = sig.getGenericEnvironment();
3196+
}
3197+
31783198
// Build the signature of the thunk as expected by the keypath runtime.
31793199
auto signature = [&]() {
31803200
CanType loweredBaseTy, loweredPropTy;
@@ -3200,7 +3220,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32003220
? ParameterConvention::Indirect_Inout
32013221
: paramConvention});
32023222
// indexes
3203-
if (!indexes.empty())
3223+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3224+
if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm())
32043225
params.push_back({C.getUnsafeRawPointerType()->getCanonicalType(),
32053226
ParameterConvention::Direct_Unowned});
32063227

@@ -3254,7 +3275,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32543275
auto baseArg = entry->createFunctionArgument(baseArgTy);
32553276
SILValue indexPtrArg;
32563277

3257-
if (!indexes.empty()) {
3278+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3279+
if (!indexes.empty() || Target.isOSBinFormatWasm()) {
32583280
auto indexArgTy = signature->getParameters()[2].getSILStorageType(
32593281
SGM.M, signature, subSGF.getTypeExpansionContext());
32603282
indexPtrArg = entry->createFunctionArgument(indexArgTy);
@@ -3344,6 +3366,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
33443366
return;
33453367
}
33463368

3369+
auto Target = SGM.getASTContext().LangOpts.Target;
33473370
auto genericSig =
33483371
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
33493372
: nullptr;
@@ -3353,6 +3376,14 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
33533376
genericEnv = nullptr;
33543377
}
33553378

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