Skip to content
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

ABI for float builtins depends on target features #112885

Open
beetrees opened this issue Oct 18, 2024 · 6 comments
Open

ABI for float builtins depends on target features #112885

beetrees opened this issue Oct 18, 2024 · 6 comments
Labels
ABI Application Binary Interface clang:headers Headers provided by Clang, e.g. for intrinsics floating-point Floating-point math

Comments

@beetrees
Copy link
Contributor

When LLVM needs to call a builtin (such as __truncdfhf2 or __addtf3), it will use whatever ABI is the default for the target features of the function that is currently being compiled. This means that on targets where the ABI of float types is target-feature dependant (e.g. the ABI of half changes of 32-bit x86 depending on whether sse2 is enabled, and the ABI of fp128 changes on PowerPC depending on whether vsx is enabled), LLVM will call the same builtin function using two different incompatible ABIs.

The simplest solution to this seems to be extending the target-abi module flag to allow frontends to specify which float ABI gets used for function calls independent of the target features of a function. Other solutions include allowing the frontend to override the names of builtin functions on a per-function-being-compiled level.

Related Rust issues: rust-lang/rust#131819, rust-lang/rust#125109

@RalfJung
Copy link
Contributor

The simplest solution to this seems to be extending the target-abi module flag to allow frontends to specify which float ABI gets used for function calls independent of the target features of a function.

I would argue this is a per-call-site decision, see also #70563.

IIUC, the most problematic situation here is code that is generally built for i386 without any SSE support, but then has some fast-paths for when SSE2 is available. That code will presumably be linked with a compiler_rt built without SSE support, but float operations inside the functions that have SSE2 enabled will expect the callee to use the SSE register. If call could encode which ABI it should use, then libcall lowering could synthesize calls that use the right registers, ignoring the target features enabled for the current function.

@nikic
Copy link
Contributor

nikic commented Oct 18, 2024

@RalfJung For calls to builtins, there is usually no call at the IR level. The call is inserted as part of legalizing a (non-call) IR operation.

@RalfJung
Copy link
Contributor

RalfJung commented Oct 18, 2024

Isn't that happening on some different IR? I'm not familiar with the lower ends of LLVM -- I heard there's Machine IR there.

If this is never in any IR it should be easier -- the legalization "just" has to pass through the information which ABI to use, to make it clear that this should not be adjusted to whatever the target features of the current function are. (I'm sure it's not actually easy, the devil is always in the details.)

@EugeneZelenko EugeneZelenko added clang:headers Headers provided by Clang, e.g. for intrinsics ABI Application Binary Interface floating-point Floating-point math and removed new issue labels Oct 18, 2024
@arsenm
Copy link
Contributor

arsenm commented Oct 19, 2024

The legalization call will be inserted in SelectionDAG or gMIR

@tgross35
Copy link
Contributor

For reference both gcc and clang just reject _Float16 with -mno-sse2 , and neither seem to support __float128 or _Float128 on PowerPC.

@beetrees
Copy link
Contributor Author

Both GCC and Clang support _Float128/__float128 on PowerPC when vsx is enabled. Both require the -mfloat128 option to enable it: Clang will automatically enable the vsx feature when -mfloat128 is used whereas GCC will emit an error if -mfloat128 is used and vsx is not enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ABI Application Binary Interface clang:headers Headers provided by Clang, e.g. for intrinsics floating-point Floating-point math
Projects
None yet
Development

No branches or pull requests

6 participants