[llvm] [LoongArch] Pass 'half' in the lower 16 bits of an f32 value with F/D ABI (PR #109368)

Trevor Gross via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 28 01:38:59 PDT 2024


================
@@ -1354,6 +1358,40 @@ SDValue LoongArchTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
   return SDValue();
 }
 
+SDValue LoongArchTargetLowering::lowerFP_TO_FP16(SDValue Op,
+                                                 SelectionDAG &DAG) const {
+  // Custom lower to ensure the libcall return is passed in an FPR on hard
+  // float ABIs.
+  SDLoc DL(Op);
+  MakeLibCallOptions CallOptions;
+  SDValue Op0 = Op.getOperand(0);
+  SDValue Chain = SDValue();
+  RTLIB::Libcall LC = RTLIB::getFPROUND(Op0.getValueType(), MVT::f16);
+  SDValue Res;
+  std::tie(Res, Chain) =
+      makeLibCall(DAG, LC, MVT::f32, Op0, CallOptions, DL, Chain);
+  if (Subtarget.is64Bit())
+    return DAG.getNode(LoongArchISD::MOVFR2GR_S_LA64, DL, MVT::i64, Res);
+  return DAG.getBitcast(MVT::i32, Res);
+}
+
+SDValue LoongArchTargetLowering::lowerFP16_TO_FP(SDValue Op,
+                                                 SelectionDAG &DAG) const {
+  // Custom lower to ensure the libcall argument is passed in an FPR on hard
+  // float ABIs.
----------------
tgross35 wrote:

> In Rust's implementation, the argument of `__gnu_h2f_ieee` is `f16`, not `i16`.
> 
> https://github.com/rust-lang/compiler-builtins/blob/compiler_builtins-v0.1.126/src/float/extend.rs#L95-L97
> 
> ```rust
> pub extern "C" fn __gnu_h2f_ieee(a: f16) -> f32 {
>     extend(a)
> }
> ```

Just to address this (quite late) - aiui `__gnu_h2f_ieee` is only called on platforms where `f16` is passed as an integer so that doesn't matter here. If there is a float ABI then `__extendhfsf2`/`__truncsfhf2` is used instead.

There is something weird with the return value though, Rust's compiler-builtins and LLVM's compiler-rt both use `f32` but GCC uses `int`. (I think maybe GCC just never emits that libcall except on ARM, LLVM seems to use it a lot more).

https://github.com/llvm/llvm-project/pull/109368


More information about the llvm-commits mailing list