Skip to content

Lower Swiftcc by LLVM #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/Demangling/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ CONTEXT_NODE(Structure)
CONTEXT_NODE(Subscript)
NODE(Suffix)
NODE(ThinFunctionType)
NODE(ThinToThickForwarder)
NODE(Tuple)
NODE(TupleElement)
NODE(TupleElementName)
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Runtime/HeapObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/Demangling/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 0 additions & 9 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 0 additions & 9 deletions lib/Demangling/OldDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
5 changes: 0 additions & 5 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
15 changes: 11 additions & 4 deletions lib/IRGen/GenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 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) {
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);
Expand Down
56 changes: 0 additions & 56 deletions lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,62 +700,6 @@ static unsigned findSinglePartiallyAppliedParameterIndexIgnoringEmptyTypes(
return firstNonEmpty;
}


llvm::Function *irgen::getThinToThickForwarder(IRGenModule &IGM,
const Optional<FunctionPointer> &staticFnPtr,
const CanSILFunctionType origType) {
auto origSig = IGM.getSignature(origType);
llvm::FunctionType *origFnTy = origSig.getType();
auto origTy = origSig.getType()->getPointerTo();

llvm::SmallVector<llvm::Type *, 4> 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
Expand Down
7 changes: 6 additions & 1 deletion lib/IRGen/GenKeyPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 0 additions & 15 deletions lib/IRGen/IRGenMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void ()> body) {
Expand Down
1 change: 0 additions & 1 deletion lib/IRGen/IRGenMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 0 additions & 52 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SILFunctionType>();

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<SILFunctionType>();
if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm && !fnTy->hasErrorResult()) {
Optional<FunctionPointer> 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;
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/SwiftShims/HeapObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions stdlib/public/runtime/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading