Skip to content

Commit 4700978

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 cf8f844 commit 4700978

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 = signature->getParameters()[1].getSILStorageType(
30753086
SGM.M, signature, subSGF.F.getTypeExpansionContext());
30763087
indexPtrArg = entry->createFunctionArgument(indexArgTy);
@@ -3159,6 +3170,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31593170
}
31603171
}
31613172

3173+
auto Target = SGM.getASTContext().LangOpts.Target;
31623174
auto genericSig =
31633175
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
31643176
: nullptr;
@@ -3167,6 +3179,14 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31673179
genericEnv = nullptr;
31683180
}
31693181

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

@@ -3246,7 +3267,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32463267
auto baseArg = entry->createFunctionArgument(baseArgTy);
32473268
SILValue indexPtrArg;
32483269

3249-
if (!indexes.empty()) {
3270+
// Always take indexes parameter to match callee and caller signature on WebAssembly
3271+
if (!indexes.empty() || Target.isOSBinFormatWasm()) {
32503272
auto indexArgTy = signature->getParameters()[2].getSILStorageType(
32513273
SGM.M, signature, subSGF.getTypeExpansionContext());
32523274
indexPtrArg = entry->createFunctionArgument(indexArgTy);
@@ -3336,6 +3358,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
33363358
return;
33373359
}
33383360

3361+
auto Target = SGM.getASTContext().LangOpts.Target;
33393362
auto genericSig =
33403363
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
33413364
: nullptr;
@@ -3345,6 +3368,14 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
33453368
genericEnv = nullptr;
33463369
}
33473370

3371+
// Add empty generic type parameter to match function signature on WebAssembly
3372+
if (!genericSig && Target.isOSBinFormatWasm()) {
3373+
auto param = GenericTypeParamType::get(false, 0, 0, SGM.getASTContext());
3374+
auto sig = GenericSignature::get(param, { });
3375+
genericSig = CanGenericSignature(sig);
3376+
genericEnv = sig.getGenericEnvironment();
3377+
}
3378+
33483379
auto &C = SGM.getASTContext();
33493380
auto unsafeRawPointerTy = C.getUnsafeRawPointerType()->getCanonicalType();
33503381
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)