Skip to content

Commit 8cd348c

Browse files
authored
[MIPS] Updated MIPS N calling conventions so that fp16 arguments no longer cause a crash (llvm#116569)
This PR fixes a bug introduced by llvm#110199, which causes any half float argument to crash the compiler on MIPS64. Currently compiling this bit of code with `llc -mtriple=mips64`: ``` define void @half_args(half %a) nounwind { entry: ret void } ``` Crashes with the following log: ``` LLVM ERROR: unable to allocate function argument #0 PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace. Stack dump: 0. Program arguments: llc -mtriple=mips64 1. Running pass 'Function Pass Manager' on module '<stdin>'. 2. Running pass 'MIPS DAG->DAG Pattern Instruction Selection' on function '@half_args' #0 0x000055a3a4013df8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x32d0df8) #1 0x000055a3a401199e llvm::sys::RunSignalHandlers() (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x32ce99e) rust-lang#2 0x000055a3a40144a8 SignalHandler(int) Signals.cpp:0:0 rust-lang#3 0x00007f00bde558c0 __restore_rt libc_sigaction.c:0:0 rust-lang#4 0x00007f00bdea462c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76 rust-lang#5 0x00007f00bde55822 gsignal ./signal/../sysdeps/posix/raise.c:27:6 rust-lang#6 0x00007f00bde3e4af abort ./stdlib/abort.c:81:7 rust-lang#7 0x000055a3a3f80e3c llvm::report_fatal_error(llvm::Twine const&, bool) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x323de3c) rust-lang#8 0x000055a3a2e20dfa (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x20dddfa) rust-lang#9 0x000055a3a2a34e20 llvm::MipsTargetLowering::LowerFormalArguments(llvm::SDValue, unsigned int, bool, llvm::SmallVectorImpl<llvm::ISD::InputArg> const&, llvm::SDLoc const&, llvm::SelectionDAG&, llvm::SmallVectorImpl<llvm::SDValue>&) const MipsISelLowering.cpp:0:0 rust-lang#10 0x000055a3a3d896a9 llvm::SelectionDAGISel::LowerArguments(llvm::Function const&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30466a9) rust-lang#11 0x000055a3a3e0b3ec llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30c83ec) rust-lang#12 0x000055a3a3e09e21 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30c6e21) rust-lang#13 0x000055a3a2aae1ca llvm::MipsDAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) MipsISelDAGToDAG.cpp:0:0 rust-lang#14 0x000055a3a3e07706 llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x30c4706) rust-lang#15 0x000055a3a3051ed6 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x230eed6) rust-lang#16 0x000055a3a35a3ec9 llvm::FPPassManager::runOnFunction(llvm::Function&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x2860ec9) rust-lang#17 0x000055a3a35ac3b2 llvm::FPPassManager::runOnModule(llvm::Module&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x28693b2) rust-lang#18 0x000055a3a35a499c llvm::legacy::PassManagerImpl::run(llvm::Module&) (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x286199c) rust-lang#19 0x000055a3a262abbb main (/home/davide/Ps2/rps2-tools/prefix/bin/llc+0x18e7bbb) rust-lang#20 0x00007f00bde3fc4c __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3 rust-lang#21 0x00007f00bde3fd05 call_init ./csu/../csu/libc-start.c:128:20 rust-lang#22 0x00007f00bde3fd05 __libc_start_main@GLIBC_2.2.5 ./csu/../csu/libc-start.c:347:5 rust-lang#23 0x000055a3a2624921 _start /builddir/glibc-2.39/csu/../sysdeps/x86_64/start.S:117:0 ``` This is caused by the fact that after the change, `f16`s are no longer lowered as `f32`s in calls. Two possible fixes are available: - Update calling conventions to properly support passing `f16` as integers. - Update `useFPRegsForHalfType()` to return `true` so that `f16` are still kept in `f32` registers, as before llvm#110199. This PR implements the first solution to not introduce any more ABI changes as llvm#110199 already did. As of what is the correct ABI for halfs, I don't think there is a correct answer. GCC doesn't support halfs on MIPS, and I couldn't find any information on old MIPS ABI manuals either.
1 parent 6aa80f0 commit 8cd348c

File tree

2 files changed

+503
-246
lines changed

2 files changed

+503
-246
lines changed

llvm/lib/Target/Mips/MipsCallingConv.td

+8-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class CCIfSubtargetNot<string F, CCAction A> : CCIfSubtarget<F, A, "!">;
2020

2121
/// Match if the original argument (before lowering) was a float.
2222
/// For example, this is true for i32's that were lowered from soft-float.
23-
class CCIfOrigArgWasNotFloat<CCAction A>
24-
: CCIf<"!static_cast<MipsCCState *>(&State)->WasOriginalArgFloat(ValNo)",
23+
class CCIfOrigArgWasFloat<CCAction A>
24+
: CCIf<"static_cast<MipsCCState *>(&State)->WasOriginalArgFloat(ValNo)",
2525
A>;
2626

2727
/// Match if the original argument (before lowering) was a 128-bit float (i.e.
@@ -134,12 +134,13 @@ def CC_MipsN : CallingConv<[
134134
CCIfSubtargetNot<"isLittle()",
135135
CCIfInReg<CCPromoteToUpperBitsInType<i64>>>>,
136136

137-
// All integers (except soft-float integers) are promoted to 64-bit.
138-
CCIfType<[i8, i16, i32], CCIfOrigArgWasNotFloat<CCPromoteToType<i64>>>,
137+
// First deal with f32s that got turned into i32s due to soft-float
138+
CCIfType<[i32],
139+
CCIfSubtarget<"useSoftFloat()",
140+
CCIfOrigArgWasFloat<CCDelegateTo<CC_MipsN_SoftFloat>>>>,
139141

140-
// The only i32's we have left are soft-float arguments.
141-
CCIfSubtarget<"useSoftFloat()", CCIfType<[i32],
142-
CCDelegateTo<CC_MipsN_SoftFloat>>>,
142+
// All remaining integers are promoted to 64-bit.
143+
CCIfType<[i8, i16, i32], CCPromoteToType<i64>>,
143144

144145
// Integer arguments are passed in integer registers.
145146
CCIfType<[i64], CCAssignToRegWithShadow<[A0_64, A1_64, A2_64, A3_64,

0 commit comments

Comments
 (0)