Skip to content

Add frontend flag for explicitly setting ccc #66072

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
merged 3 commits into from
Jun 19, 2023
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
7 changes: 6 additions & 1 deletion include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "swift/Basic/OptimizationMode.h"
#include "swift/Config.h"
#include "clang/Basic/PointerAuthOptions.h"
#include "llvm/IR/CallingConv.h"
// FIXME: This include is just for llvm::SanitizerCoverageOptions. We should
// split the header upstream so we don't include so much.
#include "llvm/Transforms/Instrumentation.h"
Expand Down Expand Up @@ -477,6 +478,9 @@ class IRGenOptions {
/// function instead of to trap instructions.
std::string TrapFuncName = "";

/// The calling convention used to perform non-swift calls.
llvm::CallingConv::ID PlatformCCallingConvention;

IRGenOptions()
: DWARFVersion(2),
OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
Expand Down Expand Up @@ -517,7 +521,8 @@ class IRGenOptions {
ColocateTypeDescriptors(true),
UseRelativeProtocolWitnessTables(false), CmdArgs(),
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
TypeInfoFilter(TypeInfoDumpFilter::All) {
TypeInfoFilter(TypeInfoDumpFilter::All),
PlatformCCallingConvention(llvm::CallingConv::C) {
#ifndef NDEBUG
DisableRoundTripDebugTypes = false;
#else
Expand Down
8 changes: 8 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -1224,4 +1224,12 @@ def experimental_spi_only_imports :
def enable_ossa_complete_lifetimes :
Flag<["-"], "enable-ossa-complete-lifetimes">,
HelpText<"Require linear OSSA lifetimes after SILGen">;

def platform_c_calling_convention :
Separate<["-"], "experimental-platform-c-calling-convention">,
HelpText<"Which calling convention is used to perform non-swift calls. "
"Defaults to llvm's standard C calling convention.">;
def platform_c_calling_convention_EQ :
Joined<["-"], "experimental-platform-c-calling-convention=">,
Alias<platform_c_calling_convention>;
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]
10 changes: 10 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,16 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
return true;
}

if (const Arg *A = Args.getLastArg(options::OPT_platform_c_calling_convention)) {
Opts.PlatformCCallingConvention =
llvm::StringSwitch<llvm::CallingConv::ID>(A->getValue())
.Case("c", llvm::CallingConv::C)
.Case("arm_apcs", llvm::CallingConv::ARM_APCS)
.Case("arm_aapcs", llvm::CallingConv::ARM_AAPCS)
.Case("arm_aapcs_vfp", llvm::CallingConv::ARM_AAPCS_VFP)
.Default(llvm::CallingConv::C);
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
case SILFunctionTypeRepresentation::ObjCMethod:
case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::Block:
return llvm::CallingConv::C;
return IGM.getOptions().PlatformCCallingConvention;

case SILFunctionTypeRepresentation::Method:
case SILFunctionTypeRepresentation::WitnessMethod:
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3297,7 +3297,7 @@ llvm::Constant *swift::irgen::emitCXXConstructorThunkIfNeeded(
llvm::Function *thunk = llvm::Function::Create(
assumedFnType, llvm::Function::PrivateLinkage, name, &IGM.Module);

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

llvm::AttrBuilder attrBuilder(IGM.getLLVMContext());
IGM.constructInitialFnAttributes(attrBuilder);
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ emitHeapMetadataRefForUnknownHeapObject(IRGenFunction &IGF,
auto metadata = IGF.Builder.CreateCall(
IGF.IGM.getGetObjectClassFunctionPointer(), object);
metadata->setName(object->getName() + ".Type");
metadata->setCallingConv(llvm::CallingConv::C);
metadata->setCallingConv(IGF.IGM.getOptions().PlatformCCallingConvention);
metadata->setDoesNotThrow();
metadata->addFnAttr(llvm::Attribute::ReadOnly);
return metadata;
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
InvariantNode = llvm::MDNode::get(getLLVMContext(), {});
DereferenceableID = getLLVMContext().getMDKindID("dereferenceable");

C_CC = llvm::CallingConv::C;
C_CC = getOptions().PlatformCCallingConvention;
// TODO: use "tinycc" on platforms that support it
DefaultCC = SWIFT_DEFAULT_LLVM_CC;
SwiftCC = llvm::CallingConv::Swift;
Expand Down