Skip to content

Commit 3055180

Browse files
Merge pull request #686 from kateinoigakukun/katei/revert-thin-to-thick
Revert thunk emission code completely
2 parents 2d6e6f8 + 834952d commit 3055180

File tree

6 files changed

+27
-50
lines changed

6 files changed

+27
-50
lines changed

lib/IRGen/GenBuiltin.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -398,17 +398,10 @@ if (Builtin.ID == BuiltinValueKind::id) { \
398398
call->addAttribute(llvm::AttributeList::FirstArgIndex + 1,
399399
llvm::Attribute::ReadOnly);
400400

401-
// Remove swiftself and swifterror attribute to match signature generated from
402-
// RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself,
403-
// but the definition of swift_willThrow generated from the def file doesn't have those
404-
// attributes due to the def file limitation. In WebAssembly context, these attributes are
405-
// lowered as usual parameters, so this doesn't have any side effects.
406-
if (IGF.IGM.TargetInfo.OutputObjectFormat != llvm::Triple::Wasm) {
407-
auto attrs = call->getAttributes();
408-
IGF.IGM.addSwiftSelfAttributes(attrs, 0);
409-
IGF.IGM.addSwiftErrorAttributes(attrs, 1);
410-
call->setAttributes(attrs);
411-
}
401+
auto attrs = call->getAttributes();
402+
IGF.IGM.addSwiftSelfAttributes(attrs, 0);
403+
IGF.IGM.addSwiftErrorAttributes(attrs, 1);
404+
call->setAttributes(attrs);
412405

413406
IGF.Builder.CreateStore(llvm::ConstantPointerNull::get(IGF.IGM.ErrorPtrTy),
414407
errorBuffer);

lib/IRGen/GenFunc.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ namespace irgen {
5454
CanSILFunctionType origType, CanSILFunctionType substType,
5555
CanSILFunctionType outType, Explosion &out, bool isOutlined);
5656

57-
58-
llvm::Function *getThinToThickForwarder(IRGenModule &IGM,
59-
const Optional<FunctionPointer> &staticFnPtr,
60-
const CanSILFunctionType origType);
6157
} // end namespace irgen
6258
} // end namespace swift
6359

lib/IRGen/IRGenModule.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,17 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
553553
AtomicBoolAlign = Alignment(ClangASTContext->getTypeSize(atomicBoolTy));
554554
}
555555

556+
// On WebAssembly, tail optional arguments are not allowed because wasm requires
557+
// callee and caller signature should be same. So LLVM adds dummy arguments for
558+
// swiftself and swifterror. If there is swiftself but there isn't swifterror in
559+
// a swiftcc function or invocation, then LLVM adds dummy swifterror parameter or
560+
// argument. To count up how many dummy arguments should be added, we need to mark
561+
// it as swifterror even though it's not in register.
562+
//
563+
// TODO: Before sending patch, please rename `IsSwiftErrorInRegister` to `ShouldUseSwiftError`
556564
IsSwiftErrorInRegister =
557565
clang::CodeGen::swiftcall::isSwiftErrorLoweredInRegister(
558-
ClangCodeGen->CGM());
566+
ClangCodeGen->CGM()) || TargetInfo.OutputObjectFormat == llvm::Triple::Wasm;
559567

560568
#ifndef NDEBUG
561569
sanityCheckStdlib(*this);
@@ -761,6 +769,19 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
761769
fn->addAttributes(llvm::AttributeList::FunctionIndex, buildFnAttr);
762770
fn->addAttributes(llvm::AttributeList::ReturnIndex, buildRetAttr);
763771
fn->addParamAttrs(0, buildFirstParamAttr);
772+
773+
// Add swiftself and swifterror attributes only when swift_willThrow
774+
// swift_willThrow is defined in RuntimeFunctions.def, but due to the
775+
// DSL limitation, arguments attributes are not set.
776+
// On the other hand, caller of swift_willThrow assumes that it's attributed
777+
// with swiftself and swifterror.
778+
// This mismatch of attributes would be issue when lowering to WebAssembly.
779+
// While lowering, LLVM count up how many dummy params are necssary to match
780+
// callee and caller signature. So we need to add them correctly.
781+
if (functionName == "swift_willThrow") {
782+
fn->addParamAttr(0, Attribute::AttrKind::SwiftSelf);
783+
fn->addParamAttr(1, Attribute::AttrKind::SwiftError);
784+
}
764785
}
765786

766787
return cache;

lib/SIL/IR/TypeLowering.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,23 +2885,10 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
28852885
return ABIDifference::NeedsThunk;
28862886
}
28872887

2888-
// There is no ABI compatibility between non-throws and throws on WebAssembly,
2889-
// so need thunk.
2890-
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2891-
if (!fnTy1->hasErrorResult() && fnTy2->hasErrorResult()) {
2892-
return ABIDifference::NeedsThunk;
2893-
}
2894-
}
2895-
28962888
auto rep1 = fnTy1->getRepresentation(), rep2 = fnTy2->getRepresentation();
28972889
if (rep1 != rep2) {
28982890
if (rep1 == SILFunctionTypeRepresentation::Thin &&
28992891
rep2 == SILFunctionTypeRepresentation::Thick) {
2900-
// There is no ABI compatibility between thin and thick on WebAssembly,
2901-
// so need thunk.
2902-
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2903-
return ABIDifference::NeedsThunk;
2904-
}
29052892
if (DifferentFunctionTypesHaveDifferentRepresentation) {
29062893
// FIXME: check whether the representations are compatible modulo
29072894
// context

stdlib/public/runtime/ErrorObject.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,21 +206,9 @@ SWIFT_RUNTIME_STDLIB_API
206206
void swift_errorRelease(SwiftError *object);
207207

208208
/// Breakpoint hook for debuggers.
209-
#ifdef __wasm__
210-
// Notes:
211-
// Remove swiftself and swifterror attribute to match signature generated from
212-
// RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself,
213-
// but the definition of swift_willThrow generated from the def file doesn't have those
214-
// attributes due to the def file limitation. In WebAssembly context, these attributes are
215-
// lowered as usual parameters, so this doesn't have any side effects.
216-
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
217-
void swift_willThrow(void *unused,
218-
SwiftError **object);
219-
#else
220209
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
221210
void swift_willThrow(SWIFT_CONTEXT void *unused,
222211
SWIFT_ERROR_RESULT SwiftError **object);
223-
#endif
224212

225213
/// Halt in response to an error.
226214
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN

stdlib/public/runtime/ErrorObjectCommon.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,9 @@ using namespace swift;
2727
void (*swift::_swift_willThrow)(SwiftError *error);
2828

2929
/// Breakpoint hook for debuggers, and calls _swift_willThrow if set.
30-
#ifdef __wasm__
31-
// Notes:
32-
// The reason of this ifdef is described in header file.
33-
SWIFT_CC(swift) void
34-
swift::swift_willThrow(void *unused, SwiftError **error)
35-
#else
3630
SWIFT_CC(swift) void
3731
swift::swift_willThrow(SWIFT_CONTEXT void *unused,
38-
SWIFT_ERROR_RESULT SwiftError **error)
39-
#endif
40-
{
32+
SWIFT_ERROR_RESULT SwiftError **error) {
4133
// Cheap check to bail out early, since we expect there to be no callbacks
4234
// the vast majority of the time.
4335
if (SWIFT_LIKELY(!_swift_willThrow))

0 commit comments

Comments
 (0)