Skip to content

Commit 30d34e9

Browse files
Merge pull request #371 from kateinoigakukun/katei/swiftcc-lowering-rebase
Lower Swiftcc by LLVM
2 parents f3de468 + 942e0f1 commit 30d34e9

24 files changed

+77
-216
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ llvm::StringRef makeSymbolicMangledNameStringRef(const char *base);
637637
//// define what these will be.
638638
/// \returns the demangled name. Returns nullptr if the input String is not a
639639
/// Swift mangled name.
640+
SWIFT_CC(swift)
640641
SWIFT_RUNTIME_EXPORT
641642
char *swift_demangle(const char *mangledName,
642643
size_t mangledNameLength,

include/swift/Demangling/DemangleNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ CONTEXT_NODE(Structure)
201201
CONTEXT_NODE(Subscript)
202202
NODE(Suffix)
203203
NODE(ThinFunctionType)
204-
NODE(ThinToThickForwarder)
205204
NODE(Tuple)
206205
NODE(TupleElement)
207206
NODE(TupleElementName)

include/swift/Runtime/HeapObject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,13 @@ void swift_nonatomic_release_n(HeapObject *object, uint32_t n);
205205

206206
// Refcounting observation hooks for memory tools. Don't use these.
207207
SWIFT_RUNTIME_EXPORT
208+
SWIFT_CC(swift)
208209
size_t swift_retainCount(HeapObject *object);
209210
SWIFT_RUNTIME_EXPORT
211+
SWIFT_CC(swift)
210212
size_t swift_unownedRetainCount(HeapObject *object);
211213
SWIFT_RUNTIME_EXPORT
214+
SWIFT_CC(swift)
212215
size_t swift_weakRetainCount(HeapObject *object);
213216

214217
/// Is this pointer a non-null unique reference to an object

lib/Demangling/Context.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ bool Context::isThunkSymbol(llvm::StringRef MangledName) {
8888
MangledName = stripSuffix(MangledName);
8989
// First do a quick check
9090
if (MangledName.endswith("TA") || // partial application forwarder
91-
MangledName.endswith("Tu")|| // thin-to-thick forwarder
9291
MangledName.endswith("Ta") || // ObjC partial application forwarder
9392
MangledName.endswith("To") || // swift-as-ObjC thunk
9493
MangledName.endswith("TO") || // ObjC-as-swift thunk
@@ -108,7 +107,6 @@ bool Context::isThunkSymbol(llvm::StringRef MangledName) {
108107
case Node::Kind::NonObjCAttribute:
109108
case Node::Kind::PartialApplyObjCForwarder:
110109
case Node::Kind::PartialApplyForwarder:
111-
case Node::Kind::ThinToThickForwarder:
112110
case Node::Kind::ReabstractionThunkHelper:
113111
case Node::Kind::ReabstractionThunk:
114112
case Node::Kind::ProtocolWitness:

lib/Demangling/Demangler.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ bool swift::Demangle::isFunctionAttr(Node::Kind kind) {
113113
case Node::Kind::DirectMethodReferenceAttribute:
114114
case Node::Kind::VTableAttribute:
115115
case Node::Kind::PartialApplyForwarder:
116-
case Node::Kind::ThinToThickForwarder:
117116
case Node::Kind::PartialApplyObjCForwarder:
118117
case Node::Kind::OutlinedVariable:
119118
case Node::Kind::OutlinedBridgedMethod:
@@ -550,8 +549,7 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName,
550549
while (NodePointer FuncAttr = popNode(isFunctionAttr)) {
551550
Parent->addChild(FuncAttr, *this);
552551
if (FuncAttr->getKind() == Node::Kind::PartialApplyForwarder ||
553-
FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder ||
554-
FuncAttr->getKind() == Node::Kind::ThinToThickForwarder)
552+
FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder)
555553
Parent = FuncAttr;
556554
}
557555
for (Node *Nd : NodeStack) {
@@ -2180,7 +2178,6 @@ NodePointer Demangler::demangleThunkOrSpecialization() {
21802178
case 'd': return createNode(Node::Kind::DirectMethodReferenceAttribute);
21812179
case 'a': return createNode(Node::Kind::PartialApplyObjCForwarder);
21822180
case 'A': return createNode(Node::Kind::PartialApplyForwarder);
2183-
case 'u': return createNode(Node::Kind::ThinToThickForwarder);
21842181
case 'm': return createNode(Node::Kind::MergedFunction);
21852182
case 'X': return createNode(Node::Kind::DynamicallyReplaceableFunctionVar);
21862183
case 'x': return createNode(Node::Kind::DynamicallyReplaceableFunctionKey);

lib/Demangling/NodePrinter.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ class NodePrinter {
467467
case Node::Kind::Subscript:
468468
case Node::Kind::Suffix:
469469
case Node::Kind::ThinFunctionType:
470-
case Node::Kind::ThinToThickForwarder:
471470
case Node::Kind::TupleElement:
472471
case Node::Kind::TypeMangling:
473472
case Node::Kind::TypeMetadata:
@@ -1234,14 +1233,6 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
12341233
Printer << "@convention(thin) ";
12351234
printFunctionType(nullptr, Node);
12361235
return nullptr;
1237-
case Node::Kind::ThinToThickForwarder:
1238-
Printer << "thin-to-thick forwarder";
1239-
1240-
if (Node->hasChildren()) {
1241-
Printer << " for ";
1242-
printChildren(Node);
1243-
}
1244-
return nullptr;
12451236
case Node::Kind::FunctionType:
12461237
case Node::Kind::UncurriedFunctionType:
12471238
printFunctionType(nullptr, Node);

lib/Demangling/OldDemangler.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,6 @@ class OldDemangler {
364364
DEMANGLE_CHILD_OR_RETURN(forwarder, Global);
365365
return forwarder;
366366
}
367-
368-
// thin-to-thick thunks.
369-
if (Mangled.nextIf("Pu")) {
370-
Node::Kind kind = Node::Kind::ThinToThickForwarder;
371-
auto forwarder = Factory.createNode(kind);
372-
if (Mangled.nextIf("__T"))
373-
DEMANGLE_CHILD_OR_RETURN(forwarder, Global);
374-
return forwarder;
375-
}
376367

377368
// Top-level types, for various consumers.
378369
if (Mangled.nextIf('t')) {

lib/Demangling/OldRemangler.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,11 +574,6 @@ void Remangler::mangleProtocolSelfConformanceDescriptor(Node *node) {
574574
mangleProtocol(node->begin()[0]);
575575
}
576576

577-
void Remangler::mangleThinToThickForwarder(Node *node) {
578-
Buffer << "Pu__T";
579-
mangleSingleChildNode(node); // global
580-
}
581-
582577
void Remangler::manglePartialApplyForwarder(Node *node) {
583578
Buffer << "PA__T";
584579
mangleSingleChildNode(node); // global

lib/Demangling/Remangler.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,11 +1738,6 @@ void Remangler::mangleOwningMutableAddressor(Node *node) {
17381738
mangleAbstractStorage(node->getFirstChild(), "aO");
17391739
}
17401740

1741-
void Remangler::mangleThinToThickForwarder(Node *node) {
1742-
mangleChildNodesReversed(node);
1743-
Buffer << "Tu";
1744-
}
1745-
17461741
void Remangler::manglePartialApplyForwarder(Node *node) {
17471742
mangleChildNodesReversed(node);
17481743
Buffer << "TA";

lib/IRGen/GenBuiltin.cpp

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

401-
auto attrs = call->getAttributes();
402-
IGF.IGM.addSwiftSelfAttributes(attrs, 0);
403-
IGF.IGM.addSwiftErrorAttributes(attrs, 1);
404-
call->setAttributes(attrs);
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+
}
405412

406413
IGF.Builder.CreateStore(llvm::ConstantPointerNull::get(IGF.IGM.ErrorPtrTy),
407414
errorBuffer);

lib/IRGen/GenFunc.cpp

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -700,62 +700,6 @@ static unsigned findSinglePartiallyAppliedParameterIndexIgnoringEmptyTypes(
700700
return firstNonEmpty;
701701
}
702702

703-
704-
llvm::Function *irgen::getThinToThickForwarder(IRGenModule &IGM,
705-
const Optional<FunctionPointer> &staticFnPtr,
706-
const CanSILFunctionType origType) {
707-
auto origSig = IGM.getSignature(origType);
708-
llvm::FunctionType *origFnTy = origSig.getType();
709-
auto origTy = origSig.getType()->getPointerTo();
710-
711-
llvm::SmallVector<llvm::Type *, 4> thunkParams;
712-
713-
for (unsigned i = 0; i < origFnTy->getNumParams(); ++i)
714-
thunkParams.push_back(origFnTy->getParamType(i));
715-
716-
thunkParams.push_back(IGM.RefCountedPtrTy);
717-
718-
auto thunkType = llvm::FunctionType::get(origFnTy->getReturnType(),
719-
thunkParams,
720-
/*vararg*/ false);
721-
722-
StringRef FnName;
723-
if (staticFnPtr)
724-
FnName = staticFnPtr->getPointer()->getName();
725-
726-
IRGenMangler Mangler;
727-
std::string thunkName = Mangler.mangleThinToThickForwarder(FnName);
728-
729-
730-
// FIXME: Maybe cache the thunk by function and closure types?.
731-
llvm::Function *fwd =
732-
llvm::Function::Create(thunkType, llvm::Function::InternalLinkage,
733-
llvm::StringRef(thunkName), &IGM.Module);
734-
735-
fwd->setAttributes(origSig.getAttributes());
736-
fwd->addAttribute(llvm::AttributeList::FirstArgIndex + origFnTy->getNumParams(), llvm::Attribute::SwiftSelf);
737-
IRGenFunction IGF(IGM, fwd);
738-
if (IGM.DebugInfo)
739-
IGM.DebugInfo->emitArtificialFunction(IGF, fwd);
740-
auto args = IGF.collectParameters();
741-
auto rawFnPtr = args.takeLast();
742-
743-
// It comes out of the context as an i8*. Cast to the function type.
744-
rawFnPtr = IGF.Builder.CreateBitCast(rawFnPtr, origTy);
745-
746-
auto fnPtr = FunctionPointer(rawFnPtr, origSig);
747-
748-
auto result = IGF.Builder.CreateCall(fnPtr, args.claimAll());
749-
750-
// Return the result, if we have one.
751-
if (result->getType()->isVoidTy())
752-
IGF.Builder.CreateRetVoid();
753-
else
754-
IGF.Builder.CreateRet(result);
755-
return fwd;
756-
}
757-
758-
759703
/// Emit the forwarding stub function for a partial application.
760704
///
761705
/// If 'layout' is null, there is a single captured value of

lib/IRGen/GenKeyPath.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ getLayoutFunctionForComputedComponent(IRGenModule &IGM,
278278

279279
auto layoutFn = llvm::Function::Create(fnTy,
280280
llvm::GlobalValue::PrivateLinkage, "keypath_get_arg_layout", IGM.getModule());
281-
281+
layoutFn->setCallingConv(IGM.SwiftCC);
282+
282283
{
283284
IRGenFunction IGF(IGM, layoutFn);
284285
if (IGM.DebugInfo)
@@ -378,6 +379,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM,
378379
/*vararg*/ false);
379380
auto destroyFn = llvm::Function::Create(destroyType,
380381
llvm::GlobalValue::PrivateLinkage, "keypath_destroy", IGM.getModule());
382+
destroyFn->setCallingConv(IGM.SwiftCC);
381383
destroy = destroyFn;
382384

383385
IRGenFunction IGF(IGM, destroyFn);
@@ -426,6 +428,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM,
426428
/*vararg*/ false);
427429
auto copyFn = llvm::Function::Create(copyType,
428430
llvm::GlobalValue::PrivateLinkage, "keypath_copy", IGM.getModule());
431+
copyFn->setCallingConv(IGM.SwiftCC);
429432
copy = copyFn;
430433

431434
IRGenFunction IGF(IGM, copyFn);
@@ -539,6 +542,8 @@ getInitializerForComputedComponent(IRGenModule &IGM,
539542

540543
auto initFn = llvm::Function::Create(fnTy,
541544
llvm::GlobalValue::PrivateLinkage, "keypath_arg_init", IGM.getModule());
545+
initFn->setCallingConv(IGM.SwiftCC);
546+
542547

543548
{
544549
IRGenFunction IGF(IGM, initFn);

lib/IRGen/IRGenMangler.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,6 @@ std::string IRGenMangler::manglePartialApplyForwarder(StringRef FuncName) {
7878
return finalize();
7979
}
8080

81-
std::string IRGenMangler::mangleThinToThickForwarder(StringRef FuncName) {
82-
if (FuncName.empty()) {
83-
beginMangling();
84-
} else {
85-
if (FuncName.startswith(MANGLING_PREFIX_STR)) {
86-
Buffer << FuncName;
87-
} else {
88-
beginMangling();
89-
appendIdentifier(FuncName);
90-
}
91-
}
92-
appendOperator("Tu");
93-
return finalize();
94-
}
95-
9681
SymbolicMangling
9782
IRGenMangler::withSymbolicReferences(IRGenModule &IGM,
9883
llvm::function_ref<void ()> body) {

lib/IRGen/IRGenMangler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ class IRGenMangler : public Mangle::ASTMangler {
512512
}
513513

514514
std::string manglePartialApplyForwarder(StringRef FuncName);
515-
std::string mangleThinToThickForwarder(StringRef FuncName);
516515

517516
std::string mangleTypeForForeignMetadataUniquing(Type type) {
518517
return mangleTypeWithoutPrefix(type);

lib/IRGen/IRGenSIL.cpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4732,60 +4732,8 @@ void IRGenSILFunction::visit##KIND##Inst(swift::KIND##Inst *i) { \
47324732
#include "swift/AST/ReferenceStorage.def"
47334733
#undef NOOP_CONVERSION
47344734

4735-
4736-
static FunctionPointer
4737-
getLoweredFunctionPointer(IRGenSILFunction &IGF, SILValue v) {
4738-
LoweredValue &lv = IGF.getLoweredValue(v);
4739-
auto fnType = v->getType().castTo<SILFunctionType>();
4740-
4741-
switch (lv.kind) {
4742-
case LoweredValue::Kind::ContainedAddress:
4743-
case LoweredValue::Kind::StackAddress:
4744-
case LoweredValue::Kind::DynamicallyEnforcedAddress:
4745-
case LoweredValue::Kind::OwnedAddress:
4746-
case LoweredValue::Kind::EmptyExplosion:
4747-
case LoweredValue::Kind::CoroutineState:
4748-
case LoweredValue::Kind::ObjCMethod:
4749-
llvm_unreachable("not a valid function");
4750-
4751-
case LoweredValue::Kind::FunctionPointer: {
4752-
return lv.getFunctionPointer();
4753-
}
4754-
case LoweredValue::Kind::SingletonExplosion: {
4755-
llvm::Value *fnPtr = lv.getKnownSingletonExplosion();
4756-
return FunctionPointer::forExplosionValue(IGF, fnPtr, fnType);
4757-
}
4758-
case LoweredValue::Kind::ExplosionVector: {
4759-
Explosion ex = lv.getExplosion(IGF, v->getType());
4760-
llvm::Value *fnPtr = ex.claimNext();
4761-
auto fn = FunctionPointer::forExplosionValue(IGF, fnPtr, fnType);
4762-
return fn;
4763-
}
4764-
}
4765-
llvm_unreachable("bad kind");
4766-
}
4767-
47684735
void IRGenSILFunction::visitThinToThickFunctionInst(
47694736
swift::ThinToThickFunctionInst *i) {
4770-
auto fn = getLoweredFunctionPointer(*this, i->getCallee());
4771-
auto fnTy = i->getCallee()->getType().castTo<SILFunctionType>();
4772-
if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm && !fnTy->hasErrorResult()) {
4773-
Optional<FunctionPointer> staticFn;
4774-
if (fn.isConstant()) staticFn = fn;
4775-
auto thunkFn = getThinToThickForwarder(IGM, staticFn, fnTy);
4776-
Explosion from = getLoweredExplosion(i->getOperand());
4777-
Explosion to;
4778-
auto fnPtr = Builder.CreateBitCast(thunkFn, IGM.Int8PtrTy);
4779-
to.add(fnPtr);
4780-
llvm::Value *ctx = from.claimNext();
4781-
if (fnTy->isNoEscape())
4782-
ctx = Builder.CreateBitCast(ctx, IGM.OpaquePtrTy);
4783-
else
4784-
ctx = Builder.CreateBitCast(ctx, IGM.RefCountedPtrTy);
4785-
to.add(ctx);
4786-
setLoweredExplosion(i, to);
4787-
return;
4788-
}
47894737
// Take the incoming function pointer and add a null context pointer to it.
47904738
Explosion from = getLoweredExplosion(i->getOperand());
47914739
Explosion to;

stdlib/public/SwiftShims/HeapObject.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,25 @@ struct HeapObject {
7474
#ifdef __cplusplus
7575
extern "C" {
7676
#endif
77+
#if __has_attribute(swiftcall)
78+
#define SWIFT_CC_swift __attribute__((swiftcall))
79+
#else
80+
#define SWIFT_CC_swift
81+
#endif
7782

7883
SWIFT_RUNTIME_STDLIB_API
7984
void _swift_instantiateInertHeapObject(void *address,
8085
const HeapMetadata *metadata);
8186

87+
SWIFT_CC_swift
8288
SWIFT_RUNTIME_STDLIB_API
8389
__swift_size_t swift_retainCount(HeapObject *obj);
8490

91+
SWIFT_CC_swift
8592
SWIFT_RUNTIME_STDLIB_API
8693
__swift_size_t swift_unownedRetainCount(HeapObject *obj);
8794

95+
SWIFT_CC_swift
8896
SWIFT_RUNTIME_STDLIB_API
8997
__swift_size_t swift_weakRetainCount(HeapObject *obj);
9098

stdlib/public/runtime/Demangle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
638638
// NB: This function is not used directly in the Swift codebase, but is
639639
// exported for Xcode support and is used by the sanitizers. Please coordinate
640640
// before changing.
641+
SWIFT_CC(swift)
641642
char *swift_demangle(const char *mangledName,
642643
size_t mangledNameLength,
643644
char *outputBuffer,

0 commit comments

Comments
 (0)