From b8e40c044091057f463c2d877738031d5b18d668 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 9 Mar 2020 00:26:24 +0900 Subject: [PATCH 1/3] Revert thinToThick lowering from IRGen --- include/swift/Demangling/DemangleNodes.def | 1 - lib/Demangling/Context.cpp | 2 - lib/Demangling/Demangler.cpp | 5 +- lib/Demangling/NodePrinter.cpp | 9 ---- lib/Demangling/OldDemangler.cpp | 9 ---- lib/Demangling/OldRemangler.cpp | 5 -- lib/Demangling/Remangler.cpp | 5 -- lib/IRGen/GenFunc.cpp | 56 ---------------------- lib/IRGen/IRGenMangler.cpp | 15 ------ lib/IRGen/IRGenMangler.h | 1 - lib/IRGen/IRGenSIL.cpp | 52 -------------------- test/IRGen/indirect_func_call_wasm.sil | 33 ------------- 12 files changed, 1 insertion(+), 192 deletions(-) delete mode 100644 test/IRGen/indirect_func_call_wasm.sil diff --git a/include/swift/Demangling/DemangleNodes.def b/include/swift/Demangling/DemangleNodes.def index f13e126b4628e..4f11bec456290 100644 --- a/include/swift/Demangling/DemangleNodes.def +++ b/include/swift/Demangling/DemangleNodes.def @@ -201,7 +201,6 @@ CONTEXT_NODE(Structure) CONTEXT_NODE(Subscript) NODE(Suffix) NODE(ThinFunctionType) -NODE(ThinToThickForwarder) NODE(Tuple) NODE(TupleElement) NODE(TupleElementName) diff --git a/lib/Demangling/Context.cpp b/lib/Demangling/Context.cpp index 23f771ae22af4..7c2e986b3985f 100644 --- a/lib/Demangling/Context.cpp +++ b/lib/Demangling/Context.cpp @@ -88,7 +88,6 @@ bool Context::isThunkSymbol(llvm::StringRef MangledName) { MangledName = stripSuffix(MangledName); // First do a quick check if (MangledName.endswith("TA") || // partial application forwarder - MangledName.endswith("Tu")|| // thin-to-thick forwarder MangledName.endswith("Ta") || // ObjC partial application forwarder MangledName.endswith("To") || // swift-as-ObjC thunk MangledName.endswith("TO") || // ObjC-as-swift thunk @@ -108,7 +107,6 @@ bool Context::isThunkSymbol(llvm::StringRef MangledName) { case Node::Kind::NonObjCAttribute: case Node::Kind::PartialApplyObjCForwarder: case Node::Kind::PartialApplyForwarder: - case Node::Kind::ThinToThickForwarder: case Node::Kind::ReabstractionThunkHelper: case Node::Kind::ReabstractionThunk: case Node::Kind::ProtocolWitness: diff --git a/lib/Demangling/Demangler.cpp b/lib/Demangling/Demangler.cpp index e65fe4a4b924b..42dc1b29685a3 100644 --- a/lib/Demangling/Demangler.cpp +++ b/lib/Demangling/Demangler.cpp @@ -113,7 +113,6 @@ bool swift::Demangle::isFunctionAttr(Node::Kind kind) { case Node::Kind::DirectMethodReferenceAttribute: case Node::Kind::VTableAttribute: case Node::Kind::PartialApplyForwarder: - case Node::Kind::ThinToThickForwarder: case Node::Kind::PartialApplyObjCForwarder: case Node::Kind::OutlinedVariable: case Node::Kind::OutlinedBridgedMethod: @@ -550,8 +549,7 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName, while (NodePointer FuncAttr = popNode(isFunctionAttr)) { Parent->addChild(FuncAttr, *this); if (FuncAttr->getKind() == Node::Kind::PartialApplyForwarder || - FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder || - FuncAttr->getKind() == Node::Kind::ThinToThickForwarder) + FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder) Parent = FuncAttr; } for (Node *Nd : NodeStack) { @@ -2180,7 +2178,6 @@ NodePointer Demangler::demangleThunkOrSpecialization() { case 'd': return createNode(Node::Kind::DirectMethodReferenceAttribute); case 'a': return createNode(Node::Kind::PartialApplyObjCForwarder); case 'A': return createNode(Node::Kind::PartialApplyForwarder); - case 'u': return createNode(Node::Kind::ThinToThickForwarder); case 'm': return createNode(Node::Kind::MergedFunction); case 'X': return createNode(Node::Kind::DynamicallyReplaceableFunctionVar); case 'x': return createNode(Node::Kind::DynamicallyReplaceableFunctionKey); diff --git a/lib/Demangling/NodePrinter.cpp b/lib/Demangling/NodePrinter.cpp index d7113bdddaa8d..f45a5bd2de6f2 100644 --- a/lib/Demangling/NodePrinter.cpp +++ b/lib/Demangling/NodePrinter.cpp @@ -467,7 +467,6 @@ class NodePrinter { case Node::Kind::Subscript: case Node::Kind::Suffix: case Node::Kind::ThinFunctionType: - case Node::Kind::ThinToThickForwarder: case Node::Kind::TupleElement: case Node::Kind::TypeMangling: case Node::Kind::TypeMetadata: @@ -1234,14 +1233,6 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) { Printer << "@convention(thin) "; printFunctionType(nullptr, Node); return nullptr; - case Node::Kind::ThinToThickForwarder: - Printer << "thin-to-thick forwarder"; - - if (Node->hasChildren()) { - Printer << " for "; - printChildren(Node); - } - return nullptr; case Node::Kind::FunctionType: case Node::Kind::UncurriedFunctionType: printFunctionType(nullptr, Node); diff --git a/lib/Demangling/OldDemangler.cpp b/lib/Demangling/OldDemangler.cpp index a505241ccec8d..ebe2bec4e5823 100644 --- a/lib/Demangling/OldDemangler.cpp +++ b/lib/Demangling/OldDemangler.cpp @@ -364,15 +364,6 @@ class OldDemangler { DEMANGLE_CHILD_OR_RETURN(forwarder, Global); return forwarder; } - - // thin-to-thick thunks. - if (Mangled.nextIf("Pu")) { - Node::Kind kind = Node::Kind::ThinToThickForwarder; - auto forwarder = Factory.createNode(kind); - if (Mangled.nextIf("__T")) - DEMANGLE_CHILD_OR_RETURN(forwarder, Global); - return forwarder; - } // Top-level types, for various consumers. if (Mangled.nextIf('t')) { diff --git a/lib/Demangling/OldRemangler.cpp b/lib/Demangling/OldRemangler.cpp index f1f57c374ea6a..9f1fda4a99f90 100644 --- a/lib/Demangling/OldRemangler.cpp +++ b/lib/Demangling/OldRemangler.cpp @@ -574,11 +574,6 @@ void Remangler::mangleProtocolSelfConformanceDescriptor(Node *node) { mangleProtocol(node->begin()[0]); } -void Remangler::mangleThinToThickForwarder(Node *node) { - Buffer << "Pu__T"; - mangleSingleChildNode(node); // global -} - void Remangler::manglePartialApplyForwarder(Node *node) { Buffer << "PA__T"; mangleSingleChildNode(node); // global diff --git a/lib/Demangling/Remangler.cpp b/lib/Demangling/Remangler.cpp index 56f2ba18bb748..cb9b6e97e27e2 100644 --- a/lib/Demangling/Remangler.cpp +++ b/lib/Demangling/Remangler.cpp @@ -1738,11 +1738,6 @@ void Remangler::mangleOwningMutableAddressor(Node *node) { mangleAbstractStorage(node->getFirstChild(), "aO"); } -void Remangler::mangleThinToThickForwarder(Node *node) { - mangleChildNodesReversed(node); - Buffer << "Tu"; -} - void Remangler::manglePartialApplyForwarder(Node *node) { mangleChildNodesReversed(node); Buffer << "TA"; diff --git a/lib/IRGen/GenFunc.cpp b/lib/IRGen/GenFunc.cpp index 407fa80015ffe..5888b9b65a874 100644 --- a/lib/IRGen/GenFunc.cpp +++ b/lib/IRGen/GenFunc.cpp @@ -700,62 +700,6 @@ static unsigned findSinglePartiallyAppliedParameterIndexIgnoringEmptyTypes( return firstNonEmpty; } - -llvm::Function *irgen::getThinToThickForwarder(IRGenModule &IGM, - const Optional &staticFnPtr, - const CanSILFunctionType origType) { - auto origSig = IGM.getSignature(origType); - llvm::FunctionType *origFnTy = origSig.getType(); - auto origTy = origSig.getType()->getPointerTo(); - - llvm::SmallVector thunkParams; - - for (unsigned i = 0; i < origFnTy->getNumParams(); ++i) - thunkParams.push_back(origFnTy->getParamType(i)); - - thunkParams.push_back(IGM.RefCountedPtrTy); - - auto thunkType = llvm::FunctionType::get(origFnTy->getReturnType(), - thunkParams, - /*vararg*/ false); - - StringRef FnName; - if (staticFnPtr) - FnName = staticFnPtr->getPointer()->getName(); - - IRGenMangler Mangler; - std::string thunkName = Mangler.mangleThinToThickForwarder(FnName); - - - // FIXME: Maybe cache the thunk by function and closure types?. - llvm::Function *fwd = - llvm::Function::Create(thunkType, llvm::Function::InternalLinkage, - llvm::StringRef(thunkName), &IGM.Module); - - fwd->setAttributes(origSig.getAttributes()); - fwd->addAttribute(llvm::AttributeList::FirstArgIndex + origFnTy->getNumParams(), llvm::Attribute::SwiftSelf); - IRGenFunction IGF(IGM, fwd); - if (IGM.DebugInfo) - IGM.DebugInfo->emitArtificialFunction(IGF, fwd); - auto args = IGF.collectParameters(); - auto rawFnPtr = args.takeLast(); - - // It comes out of the context as an i8*. Cast to the function type. - rawFnPtr = IGF.Builder.CreateBitCast(rawFnPtr, origTy); - - auto fnPtr = FunctionPointer(rawFnPtr, origSig); - - auto result = IGF.Builder.CreateCall(fnPtr, args.claimAll()); - - // Return the result, if we have one. - if (result->getType()->isVoidTy()) - IGF.Builder.CreateRetVoid(); - else - IGF.Builder.CreateRet(result); - return fwd; -} - - /// Emit the forwarding stub function for a partial application. /// /// If 'layout' is null, there is a single captured value of diff --git a/lib/IRGen/IRGenMangler.cpp b/lib/IRGen/IRGenMangler.cpp index b231b588726a1..7dbac2eaf2f82 100644 --- a/lib/IRGen/IRGenMangler.cpp +++ b/lib/IRGen/IRGenMangler.cpp @@ -78,21 +78,6 @@ std::string IRGenMangler::manglePartialApplyForwarder(StringRef FuncName) { return finalize(); } -std::string IRGenMangler::mangleThinToThickForwarder(StringRef FuncName) { - if (FuncName.empty()) { - beginMangling(); - } else { - if (FuncName.startswith(MANGLING_PREFIX_STR)) { - Buffer << FuncName; - } else { - beginMangling(); - appendIdentifier(FuncName); - } - } - appendOperator("Tu"); - return finalize(); -} - SymbolicMangling IRGenMangler::withSymbolicReferences(IRGenModule &IGM, llvm::function_ref body) { diff --git a/lib/IRGen/IRGenMangler.h b/lib/IRGen/IRGenMangler.h index f972d4c7eedc4..2f79d355aa437 100644 --- a/lib/IRGen/IRGenMangler.h +++ b/lib/IRGen/IRGenMangler.h @@ -512,7 +512,6 @@ class IRGenMangler : public Mangle::ASTMangler { } std::string manglePartialApplyForwarder(StringRef FuncName); - std::string mangleThinToThickForwarder(StringRef FuncName); std::string mangleTypeForForeignMetadataUniquing(Type type) { return mangleTypeWithoutPrefix(type); diff --git a/lib/IRGen/IRGenSIL.cpp b/lib/IRGen/IRGenSIL.cpp index 77d7d3aee18dc..38577f4c8c4c7 100644 --- a/lib/IRGen/IRGenSIL.cpp +++ b/lib/IRGen/IRGenSIL.cpp @@ -4732,60 +4732,8 @@ void IRGenSILFunction::visit##KIND##Inst(swift::KIND##Inst *i) { \ #include "swift/AST/ReferenceStorage.def" #undef NOOP_CONVERSION - -static FunctionPointer -getLoweredFunctionPointer(IRGenSILFunction &IGF, SILValue v) { - LoweredValue &lv = IGF.getLoweredValue(v); - auto fnType = v->getType().castTo(); - - switch (lv.kind) { - case LoweredValue::Kind::ContainedAddress: - case LoweredValue::Kind::StackAddress: - case LoweredValue::Kind::DynamicallyEnforcedAddress: - case LoweredValue::Kind::OwnedAddress: - case LoweredValue::Kind::EmptyExplosion: - case LoweredValue::Kind::CoroutineState: - case LoweredValue::Kind::ObjCMethod: - llvm_unreachable("not a valid function"); - - case LoweredValue::Kind::FunctionPointer: { - return lv.getFunctionPointer(); - } - case LoweredValue::Kind::SingletonExplosion: { - llvm::Value *fnPtr = lv.getKnownSingletonExplosion(); - return FunctionPointer::forExplosionValue(IGF, fnPtr, fnType); - } - case LoweredValue::Kind::ExplosionVector: { - Explosion ex = lv.getExplosion(IGF, v->getType()); - llvm::Value *fnPtr = ex.claimNext(); - auto fn = FunctionPointer::forExplosionValue(IGF, fnPtr, fnType); - return fn; - } - } - llvm_unreachable("bad kind"); -} - void IRGenSILFunction::visitThinToThickFunctionInst( swift::ThinToThickFunctionInst *i) { - auto fn = getLoweredFunctionPointer(*this, i->getCallee()); - auto fnTy = i->getCallee()->getType().castTo(); - if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm && !fnTy->hasErrorResult()) { - Optional staticFn; - if (fn.isConstant()) staticFn = fn; - auto thunkFn = getThinToThickForwarder(IGM, staticFn, fnTy); - Explosion from = getLoweredExplosion(i->getOperand()); - Explosion to; - auto fnPtr = Builder.CreateBitCast(thunkFn, IGM.Int8PtrTy); - to.add(fnPtr); - llvm::Value *ctx = from.claimNext(); - if (fnTy->isNoEscape()) - ctx = Builder.CreateBitCast(ctx, IGM.OpaquePtrTy); - else - ctx = Builder.CreateBitCast(ctx, IGM.RefCountedPtrTy); - to.add(ctx); - setLoweredExplosion(i, to); - return; - } // Take the incoming function pointer and add a null context pointer to it. Explosion from = getLoweredExplosion(i->getOperand()); Explosion to; diff --git a/test/IRGen/indirect_func_call_wasm.sil b/test/IRGen/indirect_func_call_wasm.sil deleted file mode 100644 index f64cae7fcaa12..0000000000000 --- a/test/IRGen/indirect_func_call_wasm.sil +++ /dev/null @@ -1,33 +0,0 @@ -// RUN: %target-swift-frontend %s -emit-ir | %FileCheck %s - -// REQUIRES: CPU=wasm32 - -// CHECK-LABEL: define swiftcc void @closureToConvert() -// CHECK: entry: -// CHECK: ret void -// CHECK: } -sil @closureToConvert : $@convention(thin) () -> () { - %99 = tuple () - return %99 : $() -} -// CHECK-LABEL: define swiftcc void @testConvertFunc() -// CHECK: entry: -// CHECK: call swiftcc void @"$s{{.*}}Tu"(%swift.refcounted* swiftself bitcast (void ()* @closureToConvert to %swift.refcounted*)) -// CHECK: ret void -// CHECK-LABEL: } - -sil @testConvertFunc : $@convention(thin) () -> () { -bb0: - %f = function_ref @closureToConvert : $@convention(thin) () -> () - %cf = convert_function %f : $@convention(thin) () -> () to $@noescape @convention(thin) () -> () - %thick = thin_to_thick_function %cf : $@noescape @convention(thin) () -> () to $@noescape @callee_owned () -> () - %apply = apply %thick() : $@noescape @callee_owned () -> () - %99 = tuple () - return %99 : $() -} - -// CHECK-LABEL: define internal void @"$s{{.*}}Tu"(%swift.refcounted* swiftself %0) -// CHECK: entry: -// CHECK: %1 = bitcast %swift.refcounted* %0 to void ()* -// CHECK: call swiftcc void %1() -// CHECK: ret void From a4814df193095a76ed07dc120b424e88c244b6c8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 12 Mar 2020 01:18:56 +0000 Subject: [PATCH 2/3] Add swiftcc attribute for runtime functions called from Swift --- include/swift/Demangling/Demangle.h | 1 + include/swift/Runtime/HeapObject.h | 3 +++ lib/IRGen/GenBuiltin.cpp | 15 +++++++++++---- lib/IRGen/GenKeyPath.cpp | 7 ++++++- stdlib/public/SwiftShims/HeapObject.h | 8 ++++++++ stdlib/public/runtime/Demangle.cpp | 1 + stdlib/public/runtime/ErrorObject.h | 12 ++++++++++++ stdlib/public/runtime/ErrorObjectCommon.cpp | 10 +++++++++- stdlib/public/runtime/HeapObject.cpp | 5 +++++ stdlib/public/runtime/Numeric.cpp | 2 ++ .../runtime/RuntimeInvocationsTracking.cpp | 18 +++++++++--------- .../runtime/RuntimeInvocationsTracking.h | 18 +++++++++--------- 12 files changed, 76 insertions(+), 24 deletions(-) diff --git a/include/swift/Demangling/Demangle.h b/include/swift/Demangling/Demangle.h index ce3a4b5d439c3..30c31f55c2689 100644 --- a/include/swift/Demangling/Demangle.h +++ b/include/swift/Demangling/Demangle.h @@ -637,6 +637,7 @@ llvm::StringRef makeSymbolicMangledNameStringRef(const char *base); //// define what these will be. /// \returns the demangled name. Returns nullptr if the input String is not a /// Swift mangled name. +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT char *swift_demangle(const char *mangledName, size_t mangledNameLength, diff --git a/include/swift/Runtime/HeapObject.h b/include/swift/Runtime/HeapObject.h index 72998fbbf6193..53f4448d7c666 100644 --- a/include/swift/Runtime/HeapObject.h +++ b/include/swift/Runtime/HeapObject.h @@ -205,10 +205,13 @@ void swift_nonatomic_release_n(HeapObject *object, uint32_t n); // Refcounting observation hooks for memory tools. Don't use these. SWIFT_RUNTIME_EXPORT +SWIFT_CC(swift) size_t swift_retainCount(HeapObject *object); SWIFT_RUNTIME_EXPORT +SWIFT_CC(swift) size_t swift_unownedRetainCount(HeapObject *object); SWIFT_RUNTIME_EXPORT +SWIFT_CC(swift) size_t swift_weakRetainCount(HeapObject *object); /// Is this pointer a non-null unique reference to an object diff --git a/lib/IRGen/GenBuiltin.cpp b/lib/IRGen/GenBuiltin.cpp index c0f771259286b..d1f4a92a2dab2 100644 --- a/lib/IRGen/GenBuiltin.cpp +++ b/lib/IRGen/GenBuiltin.cpp @@ -398,10 +398,17 @@ if (Builtin.ID == BuiltinValueKind::id) { \ call->addAttribute(llvm::AttributeList::FirstArgIndex + 1, llvm::Attribute::ReadOnly); - auto attrs = call->getAttributes(); - IGF.IGM.addSwiftSelfAttributes(attrs, 0); - IGF.IGM.addSwiftErrorAttributes(attrs, 1); - call->setAttributes(attrs); + // Remove swiftself and swifterror attribute to match signature generated from + // RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, + // but the definition of swift_willThrow generated from the def file doesn't has those + // attributes due to the def file limitation. In WebAssembly context, these attributes are + // lowered as usual parameters, so this doesn't have any side effects. + if (IGF.IGM.TargetInfo.OutputObjectFormat != llvm::Triple::Wasm) { + auto attrs = call->getAttributes(); + IGF.IGM.addSwiftSelfAttributes(attrs, 0); + IGF.IGM.addSwiftErrorAttributes(attrs, 1); + call->setAttributes(attrs); + } IGF.Builder.CreateStore(llvm::ConstantPointerNull::get(IGF.IGM.ErrorPtrTy), errorBuffer); diff --git a/lib/IRGen/GenKeyPath.cpp b/lib/IRGen/GenKeyPath.cpp index 2f40aae09c7d2..da6db5cb0739e 100644 --- a/lib/IRGen/GenKeyPath.cpp +++ b/lib/IRGen/GenKeyPath.cpp @@ -278,7 +278,8 @@ getLayoutFunctionForComputedComponent(IRGenModule &IGM, auto layoutFn = llvm::Function::Create(fnTy, llvm::GlobalValue::PrivateLinkage, "keypath_get_arg_layout", IGM.getModule()); - + layoutFn->setCallingConv(IGM.SwiftCC); + { IRGenFunction IGF(IGM, layoutFn); if (IGM.DebugInfo) @@ -378,6 +379,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM, /*vararg*/ false); auto destroyFn = llvm::Function::Create(destroyType, llvm::GlobalValue::PrivateLinkage, "keypath_destroy", IGM.getModule()); + destroyFn->setCallingConv(IGM.SwiftCC); destroy = destroyFn; IRGenFunction IGF(IGM, destroyFn); @@ -426,6 +428,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM, /*vararg*/ false); auto copyFn = llvm::Function::Create(copyType, llvm::GlobalValue::PrivateLinkage, "keypath_copy", IGM.getModule()); + copyFn->setCallingConv(IGM.SwiftCC); copy = copyFn; IRGenFunction IGF(IGM, copyFn); @@ -539,6 +542,8 @@ getInitializerForComputedComponent(IRGenModule &IGM, auto initFn = llvm::Function::Create(fnTy, llvm::GlobalValue::PrivateLinkage, "keypath_arg_init", IGM.getModule()); + initFn->setCallingConv(IGM.SwiftCC); + { IRGenFunction IGF(IGM, initFn); diff --git a/stdlib/public/SwiftShims/HeapObject.h b/stdlib/public/SwiftShims/HeapObject.h index bc7a8637b5f19..e69d1072ce6e5 100644 --- a/stdlib/public/SwiftShims/HeapObject.h +++ b/stdlib/public/SwiftShims/HeapObject.h @@ -74,17 +74,25 @@ struct HeapObject { #ifdef __cplusplus extern "C" { #endif +#if __has_attribute(swiftcall) +#define SWIFT_CC_swift __attribute__((swiftcall)) +#else +#define SWIFT_CC_swift +#endif SWIFT_RUNTIME_STDLIB_API void _swift_instantiateInertHeapObject(void *address, const HeapMetadata *metadata); +SWIFT_CC_swift SWIFT_RUNTIME_STDLIB_API __swift_size_t swift_retainCount(HeapObject *obj); +SWIFT_CC_swift SWIFT_RUNTIME_STDLIB_API __swift_size_t swift_unownedRetainCount(HeapObject *obj); +SWIFT_CC_swift SWIFT_RUNTIME_STDLIB_API __swift_size_t swift_weakRetainCount(HeapObject *obj); diff --git a/stdlib/public/runtime/Demangle.cpp b/stdlib/public/runtime/Demangle.cpp index f082e47e015aa..ea33eb6839fcb 100644 --- a/stdlib/public/runtime/Demangle.cpp +++ b/stdlib/public/runtime/Demangle.cpp @@ -638,6 +638,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type, // NB: This function is not used directly in the Swift codebase, but is // exported for Xcode support and is used by the sanitizers. Please coordinate // before changing. +SWIFT_CC(swift) char *swift_demangle(const char *mangledName, size_t mangledNameLength, char *outputBuffer, diff --git a/stdlib/public/runtime/ErrorObject.h b/stdlib/public/runtime/ErrorObject.h index 5e0b702cad5d2..b39b1d3e8c0df 100644 --- a/stdlib/public/runtime/ErrorObject.h +++ b/stdlib/public/runtime/ErrorObject.h @@ -206,9 +206,21 @@ SWIFT_RUNTIME_STDLIB_API void swift_errorRelease(SwiftError *object); /// Breakpoint hook for debuggers. +#ifdef __wasm__ +// Notes: +// Remove swiftself and swifterror attribute to match signature generated from +// RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, +// but the definition of swift_willThrow generated from the def file doesn't has those +// attributes due to the def file limitation. In WebAssembly context, these attributes are +// lowered as usual parameters, so this doesn't have any side effects. +SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API +void swift_willThrow(void *unused, + SwiftError **object); +#else SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API void swift_willThrow(SWIFT_CONTEXT void *unused, SWIFT_ERROR_RESULT SwiftError **object); +#endif /// Halt in response to an error. SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN diff --git a/stdlib/public/runtime/ErrorObjectCommon.cpp b/stdlib/public/runtime/ErrorObjectCommon.cpp index e000603431aba..7f445db18fe40 100644 --- a/stdlib/public/runtime/ErrorObjectCommon.cpp +++ b/stdlib/public/runtime/ErrorObjectCommon.cpp @@ -27,9 +27,17 @@ using namespace swift; void (*swift::_swift_willThrow)(SwiftError *error); /// Breakpoint hook for debuggers, and calls _swift_willThrow if set. +#ifdef __wasm__ +// Notes: +// The reason of this ifdef is described in header file. +SWIFT_CC(swift) void +swift::swift_willThrow(void *unused, SwiftError **error) +#else SWIFT_CC(swift) void swift::swift_willThrow(SWIFT_CONTEXT void *unused, - SWIFT_ERROR_RESULT SwiftError **error) { + SWIFT_ERROR_RESULT SwiftError **error) +#endif +{ // Cheap check to bail out early, since we expect there to be no callbacks // the vast majority of the time. if (SWIFT_LIKELY(!_swift_willThrow)) diff --git a/stdlib/public/runtime/HeapObject.cpp b/stdlib/public/runtime/HeapObject.cpp index 2517f0349c1fd..5e6b522fb0c27 100644 --- a/stdlib/public/runtime/HeapObject.cpp +++ b/stdlib/public/runtime/HeapObject.cpp @@ -253,6 +253,7 @@ class BoxCacheEntry { static SimpleGlobalCache Boxes; +SWIFT_CC(swift) BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type, size_t alignMask) { auto *inlineBuffer = reinterpret_cast(buffer); @@ -278,6 +279,7 @@ BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type, } } +SWIFT_CC(swift) BoxPair swift::swift_allocBox(const Metadata *type) { // Get the heap metadata for the box. auto metadata = &Boxes.getOrInsert(type).first->Data; @@ -420,16 +422,19 @@ void swift::swift_nonatomic_release_n(HeapObject *object, uint32_t n) { object->refCounts.decrementAndMaybeDeinitNonAtomic(n); } +SWIFT_CC(swift) size_t swift::swift_retainCount(HeapObject *object) { if (isValidPointerForNativeRetain(object)) return object->refCounts.getCount(); return 0; } +SWIFT_CC(swift) size_t swift::swift_unownedRetainCount(HeapObject *object) { return object->refCounts.getUnownedCount(); } +SWIFT_CC(swift) size_t swift::swift_weakRetainCount(HeapObject *object) { return object->refCounts.getWeakCount(); } diff --git a/stdlib/public/runtime/Numeric.cpp b/stdlib/public/runtime/Numeric.cpp index ac3933e6b62c6..40421d0336ef4 100644 --- a/stdlib/public/runtime/Numeric.cpp +++ b/stdlib/public/runtime/Numeric.cpp @@ -50,10 +50,12 @@ static T convert(IntegerLiteral value) { return result; } +SWIFT_CC(swift) float swift::swift_intToFloat32(IntegerLiteral value) { return convert(value); } +SWIFT_CC(swift) double swift::swift_intToFloat64(IntegerLiteral value) { return convert(value); } diff --git a/stdlib/public/runtime/RuntimeInvocationsTracking.cpp b/stdlib/public/runtime/RuntimeInvocationsTracking.cpp index 959577589c4ba..7cceb8d6dc9b5 100644 --- a/stdlib/public/runtime/RuntimeInvocationsTracking.cpp +++ b/stdlib/public/runtime/RuntimeInvocationsTracking.cpp @@ -128,7 +128,7 @@ static uint16_t RuntimeFunctionCountersOffsets[] = { /// Public APIs /// Get the runtime object state associated with an object. -void _swift_getObjectRuntimeFunctionCounters( +SWIFT_CC(swift) void _swift_getObjectRuntimeFunctionCounters( HeapObject *object, RuntimeFunctionCountersState *result) { auto &theSentinel = RuntimeObjectStateCache.get(); StaticScopedReadLock lock(theSentinel.Lock); @@ -137,7 +137,7 @@ void _swift_getObjectRuntimeFunctionCounters( /// Set the runtime object state associated with an object from a provided /// state. -void _swift_setObjectRuntimeFunctionCounters( +SWIFT_CC(swift) void _swift_setObjectRuntimeFunctionCounters( HeapObject *object, RuntimeFunctionCountersState *state) { auto &theSentinel = RuntimeObjectStateCache.get(); StaticScopedWriteLock lock(theSentinel.Lock); @@ -146,14 +146,14 @@ void _swift_setObjectRuntimeFunctionCounters( /// Get the global runtime state containing the total numbers of invocations for /// each runtime function of interest. -void _swift_getGlobalRuntimeFunctionCounters( +SWIFT_CC(swift) void _swift_getGlobalRuntimeFunctionCounters( RuntimeFunctionCountersState *result) { StaticScopedReadLock lock(RuntimeGlobalFunctionCountersState.Lock); *result = RuntimeGlobalFunctionCountersState.State; } /// Set the global runtime state of function pointers from a provided state. -void _swift_setGlobalRuntimeFunctionCounters( +SWIFT_CC(swift) void _swift_setGlobalRuntimeFunctionCounters( RuntimeFunctionCountersState *state) { StaticScopedWriteLock lock(RuntimeGlobalFunctionCountersState.Lock); RuntimeGlobalFunctionCountersState.State = *state; @@ -162,19 +162,19 @@ void _swift_setGlobalRuntimeFunctionCounters( /// Return the names of the runtime functions being tracked. /// Their order is the same as the order of the counters in the /// RuntimeObjectState structure. All these strings are null terminated. -const char **_swift_getRuntimeFunctionNames() { +SWIFT_CC(swift) const char **_swift_getRuntimeFunctionNames() { return RuntimeFunctionNames; } /// Return the offsets of the runtime function counters being tracked. /// Their order is the same as the order of the counters in the /// RuntimeObjectState structure. -const uint16_t *_swift_getRuntimeFunctionCountersOffsets() { +SWIFT_CC(swift) const uint16_t *_swift_getRuntimeFunctionCountersOffsets() { return RuntimeFunctionCountersOffsets; } /// Return the number of runtime functions being tracked. -uint64_t _swift_getNumRuntimeFunctionCounters() { +SWIFT_CC(swift) uint64_t _swift_getNumRuntimeFunctionCounters() { return ID_LastRuntimeFunctionName; } @@ -202,7 +202,7 @@ void _swift_dumpObjectsRuntimeFunctionPointers() { /// Set mode for global runtime function counters. /// Return the old value of this flag. -int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { +SWIFT_CC(swift) int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { int oldMode = UpdateGlobalRuntimeFunctionCounters; UpdateGlobalRuntimeFunctionCounters = mode ? 1 : 0; return oldMode; @@ -210,7 +210,7 @@ int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { /// Set mode for per object runtime function counters. /// Return the old value of this flag. -int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) { +SWIFT_CC(swift) int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) { int oldMode = UpdatePerObjectRuntimeFunctionCounters; UpdatePerObjectRuntimeFunctionCounters = mode ? 1 : 0; return oldMode; diff --git a/stdlib/public/runtime/RuntimeInvocationsTracking.h b/stdlib/public/runtime/RuntimeInvocationsTracking.h index 74d609176563e..3005df2b79c7c 100644 --- a/stdlib/public/runtime/RuntimeInvocationsTracking.h +++ b/stdlib/public/runtime/RuntimeInvocationsTracking.h @@ -61,47 +61,47 @@ using RuntimeFunctionCountersUpdateHandler = /// Get the runtime object state associated with an object and store it /// into the result. -SWIFT_RUNTIME_EXPORT void +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_getObjectRuntimeFunctionCounters(HeapObject *object, RuntimeFunctionCountersState *result); /// Get the global runtime state containing the total numbers of invocations for /// each runtime function of interest and store it into the result. -SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters( +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters( swift::RuntimeFunctionCountersState *result); /// Return the names of the runtime functions being tracked. /// Their order is the same as the order of the counters in the /// RuntimeObjectState structure. -SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames(); +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames(); /// Return the offsets of the runtime function counters being tracked. /// Their order is the same as the order of the counters in the /// RuntimeFunctionCountersState structure. -SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets(); +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets(); /// Return the number of runtime functions being tracked. -SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters(); +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters(); /// Dump all per-object runtime function pointers. SWIFT_RUNTIME_EXPORT void _swift_dumpObjectsRuntimeFunctionPointers(); /// Set mode for global runtime function counters. /// Return the old value of this flag. -SWIFT_RUNTIME_EXPORT int +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int _swift_setPerObjectRuntimeFunctionCountersMode(int mode); /// Set mode for per object runtime function counters. /// Return the old value of this flag. -SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode); +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode); /// Set the global runtime state of function pointers from a provided state. -SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters( +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters( swift::RuntimeFunctionCountersState *state); /// Set the runtime object state associated with an object from a provided /// state. -SWIFT_RUNTIME_EXPORT void +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_setObjectRuntimeFunctionCounters(HeapObject *object, RuntimeFunctionCountersState *state); From 942e0f1f1f49c26e4700a704e89b8f98a99f7e00 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 14 Mar 2020 17:23:00 -0700 Subject: [PATCH 3/3] Fix typo --- lib/IRGen/GenBuiltin.cpp | 2 +- stdlib/public/runtime/ErrorObject.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/IRGen/GenBuiltin.cpp b/lib/IRGen/GenBuiltin.cpp index d1f4a92a2dab2..6223decbd9762 100644 --- a/lib/IRGen/GenBuiltin.cpp +++ b/lib/IRGen/GenBuiltin.cpp @@ -400,7 +400,7 @@ if (Builtin.ID == BuiltinValueKind::id) { \ // Remove swiftself and swifterror attribute to match signature generated from // RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, - // but the definition of swift_willThrow generated from the def file doesn't has those + // but the definition of swift_willThrow generated from the def file doesn't have those // attributes due to the def file limitation. In WebAssembly context, these attributes are // lowered as usual parameters, so this doesn't have any side effects. if (IGF.IGM.TargetInfo.OutputObjectFormat != llvm::Triple::Wasm) { diff --git a/stdlib/public/runtime/ErrorObject.h b/stdlib/public/runtime/ErrorObject.h index b39b1d3e8c0df..b67a80f8d1ed3 100644 --- a/stdlib/public/runtime/ErrorObject.h +++ b/stdlib/public/runtime/ErrorObject.h @@ -210,7 +210,7 @@ void swift_errorRelease(SwiftError *object); // Notes: // Remove swiftself and swifterror attribute to match signature generated from // RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, -// but the definition of swift_willThrow generated from the def file doesn't has those +// but the definition of swift_willThrow generated from the def file doesn't have those // attributes due to the def file limitation. In WebAssembly context, these attributes are // lowered as usual parameters, so this doesn't have any side effects. SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API