Skip to content

Commit 2a78957

Browse files
authored
Merge pull request #66072 from apple/platform-ccc
Add frontend flag for explicitly setting ccc
2 parents de45ab1 + cc0ea25 commit 2a78957

File tree

7 files changed

+28
-5
lines changed

7 files changed

+28
-5
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/Basic/OptimizationMode.h"
2626
#include "swift/Config.h"
2727
#include "clang/Basic/PointerAuthOptions.h"
28+
#include "llvm/IR/CallingConv.h"
2829
// FIXME: This include is just for llvm::SanitizerCoverageOptions. We should
2930
// split the header upstream so we don't include so much.
3031
#include "llvm/Transforms/Instrumentation.h"
@@ -477,6 +478,9 @@ class IRGenOptions {
477478
/// function instead of to trap instructions.
478479
std::string TrapFuncName = "";
479480

481+
/// The calling convention used to perform non-swift calls.
482+
llvm::CallingConv::ID PlatformCCallingConvention;
483+
480484
IRGenOptions()
481485
: DWARFVersion(2),
482486
OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
@@ -517,7 +521,8 @@ class IRGenOptions {
517521
ColocateTypeDescriptors(true),
518522
UseRelativeProtocolWitnessTables(false), CmdArgs(),
519523
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
520-
TypeInfoFilter(TypeInfoDumpFilter::All) {
524+
TypeInfoFilter(TypeInfoDumpFilter::All),
525+
PlatformCCallingConvention(llvm::CallingConv::C) {
521526
#ifndef NDEBUG
522527
DisableRoundTripDebugTypes = false;
523528
#else

include/swift/Option/FrontendOptions.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,4 +1224,12 @@ def experimental_spi_only_imports :
12241224
def enable_ossa_complete_lifetimes :
12251225
Flag<["-"], "enable-ossa-complete-lifetimes">,
12261226
HelpText<"Require linear OSSA lifetimes after SILGen">;
1227+
1228+
def platform_c_calling_convention :
1229+
Separate<["-"], "experimental-platform-c-calling-convention">,
1230+
HelpText<"Which calling convention is used to perform non-swift calls. "
1231+
"Defaults to llvm's standard C calling convention.">;
1232+
def platform_c_calling_convention_EQ :
1233+
Joined<["-"], "experimental-platform-c-calling-convention=">,
1234+
Alias<platform_c_calling_convention>;
12271235
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,16 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
28072807
return true;
28082808
}
28092809

2810+
if (const Arg *A = Args.getLastArg(options::OPT_platform_c_calling_convention)) {
2811+
Opts.PlatformCCallingConvention =
2812+
llvm::StringSwitch<llvm::CallingConv::ID>(A->getValue())
2813+
.Case("c", llvm::CallingConv::C)
2814+
.Case("arm_apcs", llvm::CallingConv::ARM_APCS)
2815+
.Case("arm_aapcs", llvm::CallingConv::ARM_AAPCS)
2816+
.Case("arm_aapcs_vfp", llvm::CallingConv::ARM_AAPCS_VFP)
2817+
.Default(llvm::CallingConv::C);
2818+
}
2819+
28102820
return false;
28112821
}
28122822

lib/IRGen/GenCall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
322322
case SILFunctionTypeRepresentation::ObjCMethod:
323323
case SILFunctionTypeRepresentation::CXXMethod:
324324
case SILFunctionTypeRepresentation::Block:
325-
return llvm::CallingConv::C;
325+
return IGM.getOptions().PlatformCCallingConvention;
326326

327327
case SILFunctionTypeRepresentation::Method:
328328
case SILFunctionTypeRepresentation::WitnessMethod:

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3297,7 +3297,7 @@ llvm::Constant *swift::irgen::emitCXXConstructorThunkIfNeeded(
32973297
llvm::Function *thunk = llvm::Function::Create(
32983298
assumedFnType, llvm::Function::PrivateLinkage, name, &IGM.Module);
32993299

3300-
thunk->setCallingConv(llvm::CallingConv::C);
3300+
thunk->setCallingConv(IGM.getOptions().PlatformCCallingConvention);
33013301

33023302
llvm::AttrBuilder attrBuilder(IGM.getLLVMContext());
33033303
IGM.constructInitialFnAttributes(attrBuilder);

lib/IRGen/GenHeap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ emitHeapMetadataRefForUnknownHeapObject(IRGenFunction &IGF,
19781978
auto metadata = IGF.Builder.CreateCall(
19791979
IGF.IGM.getGetObjectClassFunctionPointer(), object);
19801980
metadata->setName(object->getName() + ".Type");
1981-
metadata->setCallingConv(llvm::CallingConv::C);
1981+
metadata->setCallingConv(IGF.IGM.getOptions().PlatformCCallingConvention);
19821982
metadata->setDoesNotThrow();
19831983
metadata->addFnAttr(llvm::Attribute::ReadOnly);
19841984
return metadata;

lib/IRGen/IRGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
570570
InvariantNode = llvm::MDNode::get(getLLVMContext(), {});
571571
DereferenceableID = getLLVMContext().getMDKindID("dereferenceable");
572572

573-
C_CC = llvm::CallingConv::C;
573+
C_CC = getOptions().PlatformCCallingConvention;
574574
// TODO: use "tinycc" on platforms that support it
575575
DefaultCC = SWIFT_DEFAULT_LLVM_CC;
576576
SwiftCC = llvm::CallingConv::Swift;

0 commit comments

Comments
 (0)