Skip to content

Commit 6c35202

Browse files
committed
Conditionalize usage of llvm::CallingConv::Swift on SWIFT_USE_SWIFTCALL macro
1 parent 39fa2f0 commit 6c35202

File tree

5 files changed

+38
-34
lines changed

5 files changed

+38
-34
lines changed

include/swift/Runtime/Config.h

+5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@
128128

129129
#define SWIFT_LLVM_CC_RegisterPreservingCC llvm::CallingConv::PreserveMost
130130

131+
#if SWIFT_USE_SWIFTCALL
132+
#define SWIFT_LLVM_CC_SwiftCC llvm::CallingConv::Swift
133+
#else
134+
#define SWIFT_LLVM_CC_SwiftCC llvm::CallingConv::C
135+
#endif
131136

132137
// If defined, it indicates that runtime function wrappers
133138
// should be used on all platforms, even they do not support

lib/IRGen/GenCall.cpp

+20-19
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "clang/CodeGen/ModuleBuilder.h"
2727
#include "swift/AST/GenericEnvironment.h"
2828
#include "swift/SIL/SILType.h"
29+
#include "swift/Runtime/Config.h"
2930
#include "llvm/IR/CallSite.h"
3031
#include "llvm/Support/Compiler.h"
3132

@@ -108,7 +109,7 @@ static void addInoutParameterAttributes(IRGenModule &IGM,
108109

109110
static llvm::CallingConv::ID getFreestandingConvention(IRGenModule &IGM) {
110111
// TODO: use a custom CC that returns three scalars efficiently
111-
return llvm::CallingConv::Swift;
112+
return SWIFT_LLVM_CC(SwiftCC);
112113
}
113114

114115
/// Expand the requirements of the given abstract calling convention
@@ -151,33 +152,33 @@ static void addIndirectResultAttributes(IRGenModule &IGM,
151152
attrs = attrs.addAttributes(IGM.LLVMContext, paramIndex + 1, resultAttrs);
152153
}
153154

154-
static void addSwiftSelfAttributes(IRGenModule &IGM,
155-
llvm::AttributeSet &attrs,
156-
unsigned argIndex) {
155+
void IRGenModule::addSwiftSelfAttributes(llvm::AttributeSet &attrs,
156+
unsigned argIndex) {
157+
if (!UseSwiftCC)
158+
return;
157159
static const llvm::Attribute::AttrKind attrKinds[] = {
158160
llvm::Attribute::SwiftSelf,
159161
};
160162
auto argAttrs =
161-
llvm::AttributeSet::get(IGM.LLVMContext, argIndex + 1, attrKinds);
162-
attrs = attrs.addAttributes(IGM.LLVMContext, argIndex + 1, argAttrs);
163+
llvm::AttributeSet::get(this->LLVMContext, argIndex + 1, attrKinds);
164+
attrs = attrs.addAttributes(this->LLVMContext, argIndex + 1, argAttrs);
163165
}
164166

165-
static void addSwiftErrorAttributes(IRGenModule &IGM,
166-
llvm::AttributeSet &attrs,
167-
unsigned argIndex) {
167+
void IRGenModule::addSwiftErrorAttributes(llvm::AttributeSet &attrs,
168+
unsigned argIndex) {
168169
// Don't add the swifterror attribute on ABI that don't pass it in a register.
169170
// We create a shadow stack location of the swifterror parameter for the
170171
// debugger on such platforms and so we can't mark the parameter with a
171172
// swifterror attribute.
172-
if (!IGM.IsSwiftErrorInRegister)
173+
if (!UseSwiftCC || !this->IsSwiftErrorInRegister)
173174
return;
174175

175176
static const llvm::Attribute::AttrKind attrKinds[] = {
176177
llvm::Attribute::SwiftError,
177178
};
178179
auto argAttrs =
179-
llvm::AttributeSet::get(IGM.LLVMContext, argIndex + 1, attrKinds);
180-
attrs = attrs.addAttributes(IGM.LLVMContext, argIndex + 1, argAttrs);
180+
llvm::AttributeSet::get(this->LLVMContext, argIndex + 1, attrKinds);
181+
attrs = attrs.addAttributes(this->LLVMContext, argIndex + 1, argAttrs);
181182
}
182183

183184
void irgen::addByvalArgumentAttributes(IRGenModule &IGM,
@@ -939,10 +940,10 @@ llvm::Type *SignatureExpansion::expandExternalSignatureTypes() {
939940
case clang::ParameterABI::Ordinary:
940941
break;
941942
case clang::ParameterABI::SwiftContext:
942-
addSwiftSelfAttributes(IGM, Attrs, getCurParamIndex());
943+
IGM.addSwiftSelfAttributes(Attrs, getCurParamIndex());
943944
break;
944945
case clang::ParameterABI::SwiftErrorResult:
945-
addSwiftErrorAttributes(IGM, Attrs, getCurParamIndex());
946+
IGM.addSwiftErrorAttributes(Attrs, getCurParamIndex());
946947
break;
947948
case clang::ParameterABI::SwiftIndirectResult:
948949
addIndirectResultAttributes(IGM, Attrs, getCurParamIndex(),claimSRet());
@@ -1115,7 +1116,7 @@ void SignatureExpansion::expandParameters() {
11151116
auto curLength = ParamIRTypes.size(); (void) curLength;
11161117

11171118
if (claimSelf())
1118-
addSwiftSelfAttributes(IGM, Attrs, curLength);
1119+
IGM.addSwiftSelfAttributes(Attrs, curLength);
11191120
expand(FnType->getSelfParameter());
11201121
assert(ParamIRTypes.size() == curLength + 1 &&
11211122
"adding 'self' added unexpected number of parameters");
@@ -1141,7 +1142,7 @@ void SignatureExpansion::expandParameters() {
11411142
};
11421143
if (needsContext()) {
11431144
if (claimSelf())
1144-
addSwiftSelfAttributes(IGM, Attrs, ParamIRTypes.size());
1145+
IGM.addSwiftSelfAttributes(Attrs, ParamIRTypes.size());
11451146
ParamIRTypes.push_back(IGM.RefCountedPtrTy);
11461147
}
11471148
}
@@ -1151,7 +1152,7 @@ void SignatureExpansion::expandParameters() {
11511152
// if we set the right attribute.
11521153
if (FnType->hasErrorResult()) {
11531154
if (claimError())
1154-
addSwiftErrorAttributes(IGM, Attrs, ParamIRTypes.size());
1155+
IGM.addSwiftErrorAttributes(Attrs, ParamIRTypes.size());
11551156
llvm::Type *errorType = IGM.getStorageType(
11561157
getSILFuncConventions().getSILType(FnType->getErrorResult()));
11571158
ParamIRTypes.push_back(errorType->getPointerTo());
@@ -1493,7 +1494,7 @@ void CallEmission::setFromCallee() {
14931494
assert(LastArgWritten > 0);
14941495
Args[--LastArgWritten] = errorResultSlot.getAddress();
14951496
addAttribute(LastArgWritten + 1, llvm::Attribute::NoCapture);
1496-
addSwiftErrorAttributes(IGF.IGM, Attrs, LastArgWritten);
1497+
IGF.IGM.addSwiftErrorAttributes(Attrs, LastArgWritten);
14971498

14981499
// Fill in the context pointer if necessary.
14991500
if (!contextPtr) {
@@ -1509,7 +1510,7 @@ void CallEmission::setFromCallee() {
15091510
&& "block function should not claimed to have data pointer");
15101511
assert(LastArgWritten > 0);
15111512
Args[--LastArgWritten] = contextPtr;
1512-
addSwiftSelfAttributes(IGF.IGM, Attrs, LastArgWritten);
1513+
IGF.IGM.addSwiftSelfAttributes(Attrs, LastArgWritten);
15131514
}
15141515
}
15151516

lib/IRGen/GenHeap.cpp

+2-15
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,6 @@ void irgen::emitDeallocatePartialClassInstance(IRGenFunction &IGF,
188188
{object, metadata, size, alignMask});
189189
}
190190

191-
/// TODO: Fix copy'and'paste of this function. Probably move it to IRGenFunction.h/.cpp?
192-
static void addSwiftSelfAttributes(IRGenModule &IGM,
193-
llvm::AttributeSet &attrs,
194-
unsigned argIndex) {
195-
static const llvm::Attribute::AttrKind attrKinds[] = {
196-
llvm::Attribute::SwiftSelf,
197-
};
198-
auto argAttrs =
199-
llvm::AttributeSet::get(IGM.LLVMContext, argIndex + 1, attrKinds);
200-
attrs = attrs.addAttributes(IGM.LLVMContext, argIndex + 1, argAttrs);
201-
}
202-
203191
/// Create the destructor function for a layout.
204192
/// TODO: give this some reasonable name and possibly linkage.
205193
static llvm::Function *createDtorFn(IRGenModule &IGM,
@@ -209,10 +197,9 @@ static llvm::Function *createDtorFn(IRGenModule &IGM,
209197
llvm::Function::PrivateLinkage,
210198
"objectdestroy", &IGM.Module);
211199
auto attrs = IGM.constructInitialAttributes();
212-
addSwiftSelfAttributes(IGM, attrs, 0);
200+
IGM.addSwiftSelfAttributes(attrs, 0);
213201
fn->setAttributes(attrs);
214-
// TODO: Introduce and use IGM.SwiftCC here.
215-
fn->setCallingConv(llvm::CallingConv::Swift);
202+
fn->setCallingConv(IGM.SwiftCC);
216203

217204
IRGenFunction IGF(IGM, fn);
218205
if (IGM.DebugInfo)

lib/IRGen/IRGenModule.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
388388
else
389389
RegisterPreservingCC = DefaultCC;
390390

391+
SwiftCC = SWIFT_LLVM_CC(SwiftCC);
392+
UseSwiftCC = (SwiftCC == llvm::CallingConv::Swift);
393+
391394
if (IRGen.Opts.DebugInfoKind > IRGenDebugInfoKind::None)
392395
DebugInfo = new IRGenDebugInfo(IRGen.Opts, *CI, *this, Module, SF);
393396

lib/IRGen/IRGenModule.h

+8
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ class IRGenModule {
476476
llvm::CallingConv::ID C_CC; /// standard C calling convention
477477
llvm::CallingConv::ID DefaultCC; /// default calling convention
478478
llvm::CallingConv::ID RegisterPreservingCC; /// lightweight calling convention
479+
llvm::CallingConv::ID SwiftCC; /// swift calling convention
480+
bool UseSwiftCC;
479481

480482
llvm::FunctionType *getAssociatedTypeMetadataAccessFunctionTy();
481483
llvm::FunctionType *getAssociatedTypeWitnessTableAccessFunctionTy();
@@ -997,6 +999,12 @@ private: \
997999
/// the binary.
9981000
void setTrueConstGlobal(llvm::GlobalVariable *var);
9991001

1002+
/// Add the swiftself attribute.
1003+
void addSwiftSelfAttributes(llvm::AttributeSet &attrs, unsigned argIndex);
1004+
1005+
/// Add the swifterror attribute.
1006+
void addSwiftErrorAttributes(llvm::AttributeSet &attrs, unsigned argIndex);
1007+
10001008
private:
10011009
llvm::Constant *getAddrOfLLVMVariable(LinkEntity entity,
10021010
Alignment alignment,

0 commit comments

Comments
 (0)