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

Add frontend flag for explicitly setting ccc #66072

merged 3 commits into from
Jun 19, 2023

Conversation

rauhul
Copy link
Member

@rauhul rauhul commented May 23, 2023

Adds a new swift-frontend flag to allow users to choose which calling convention is used to make c function calls. This hidden flag is called -experimental-platform-c-calling-convention.

This behavior is needed to workaround rdar://109431863 (Swift-frontend produces trapping llvm ir for non-trapping sil). The root cause of this issue is that IRGen always emits c function calls with llvm's default C calling convention. However clang may select a different (incompatible) calling convention for the function, eventually resulting--via InstCombine and SimplifyCFG--in a trap instead of the function call. This failure mode is most readily seen with the triple armv7em-apple-none-macho when attempting to call functions taking struct arguments. Example unoptimized ir below:

call void @bar([4 x i32] %17, i32 2), !dbg !109
...
define internal arm_aapcs_vfpcc void @bar(
  [4 x i32] %bar.coerce, i32 noundef %x)

In the future it would be better to use the clang importer or some other tool to determine the calling convention for each function instead of setting the calling convention frontend invocation wide.

Note: I don't know for sure whether or not clang should be explicitly annotating these functions with a calling convention instead of aliasing C to mean ARM_AAPCS_VFP for this particular combination of -target, -mfloat-abi, and -mcpu.

@rauhul rauhul added IRGen LLVM IR generation armv7 Architecture: ARMv7 Frontend Area → compiler: Frontend labels May 23, 2023
@xedin xedin removed their request for review May 23, 2023 17:04
Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll also want to change:

lib/IRGen/GenHeap.cpp:  metadata->setCallingConv(llvm::CallingConv::C);
lib/IRGen/IRGenModule.cpp:  C_CC = llvm::CallingConv::C;

@@ -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 ExperimentalPlatformCCallingConvention;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets drop the word Experimental.

rauhul added 2 commits June 15, 2023 21:52
Adds a new swift-frontend flag to allow users to choose which calling
convention is used to make c function calls. This hidden flag is called
`-experimental-platform-c-calling-convention`.

This behavior is needed to workaround rdar://109431863 (Swift-frontend
produces trapping llvm ir for non-trapping sil). The root cause of this
issue is that IRGen always emits c function calls with llvm's default C
calling convention. However clang may select a different (incompatible)
calling convention for the function, eventually resulting--via
InstCombine and SimplifyCFG--in a trap instead of the function call.
This failure mode is most readily seen with the triple
`armv7em-apple-none-macho` when attempting to call functions taking
struct arguments. Example unoptimized ir below:

```llvm-ir
call void @bar([4 x i32] %17, i32 2), !dbg !109
...
define internal arm_aapcs_vfpcc void @bar(
  [4 x i32] %bar.coerce, i32 noundef %x)
```

In the future it would be better to use the clang importer or some other
tool to determine the calling convention for each function instead of
setting the calling convention frontend invocation wide.

Note: I don't know for sure whether or not clang should be explicitly
annotating these functions with a calling convention instead of
aliasing C to mean ARM_AAPCS_VFP for this particular combination of
`-target`, `-mfloat-abi`, and `-mcpu`.
- Renames ExperimentalPlatformCCallingConvention to
  PlatformCCallingConvention.
- Removes non-arm calling convention support as this feature is working
  around a clang bug for some arm triples which we hope to see resolved.
- Removes misleading MetaVarName from platform-c-calling-convention
  argument.
- Replaces other uses of LLVM::CallingConv::C with
  IGM.getOptions().PlatformCCallingConvention().
@rauhul
Copy link
Member Author

rauhul commented Jun 16, 2023

@swift-ci please test

@rauhul
Copy link
Member Author

rauhul commented Jun 16, 2023

@swift-ci please smoke test

Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@rauhul rauhul merged commit 2a78957 into main Jun 19, 2023
@rauhul rauhul deleted the platform-ccc branch June 19, 2023 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
armv7 Architecture: ARMv7 Frontend Area → compiler: Frontend IRGen LLVM IR generation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants