binary16 float-point arithmetic crashes LLVM on s390x

Bug #2075491 reported by Zixing Liu
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Ubuntu on IBM z Systems
New
Medium
bugproxy
llvm-toolchain-18 (Ubuntu)
New
Medium
Unassigned

Bug Description

binary16 float-point arithmetic crashes LLVM on s390x due to the LLVM backend being unable to lower the LLVM IR to the machine code.

You can find a short reproducer below:

define noundef zeroext i1 @test_func(half noundef %f) unnamed_addr {
start:
  %self = fdiv half %f, 0xHC700
  %_4 = bitcast half %self to i16
  %_0 = icmp slt i16 %_4, 0
  ret i1 %_0
}

You can save this LLVM IR text to a file called `test-ir.ll` and build it using `clang test-ir.ll -c -o test-ir.o` on a s390x device and watch LLVM explodes with:

warning: overriding the module target triple with s390x-unknown-linux-gnu [-Woverride-module]
fatal error: error in backend: Cannot select: 0x6005f020ae20: i32 = fp_to_fp16 0x6005f020adb0
  0x6005f020adb0: f32 = fdiv 0x6005f020af70, 0x6005f020b4b0
    0x6005f020af70: f32 = fp16_to_fp 0x6005f020b520
      0x6005f020b520: i32 = fp_to_fp16 0x6005f020a9c0
        0x6005f020a9c0: f32,ch = CopyFromReg 0x6005f01e5220, Register:f32 %0
          0x6005f020a950: f32 = Register %0
    0x6005f020b4b0: f32,ch = load<(load (s32) from constant-pool)> 0x6005f01e5220, 0x6005f020b280, undef:i64
      0x6005f020b280: i64 = SystemZISD::PCREL_WRAPPER TargetConstantPool:i64<float -7.000000e+00> 0
        0x6005f020b1a0: i64 = TargetConstantPool<float -7.000000e+00> 0
      0x6005f020b130: i64 = undef
In function: test_func

Frank Heimes (fheimes)
tags: added: reverse-proxy-bugzilla s390x
Changed in ubuntu-z-systems:
assignee: nobody → bugproxy (bugproxy)
Changed in llvm-toolchain-18 (Ubuntu):
importance: Undecided → Medium
Changed in ubuntu-z-systems:
importance: Undecided → Medium
bugproxy (bugproxy)
tags: added: architecture-s39064 bugnameltc-208024 severity-high targetmilestone-inin---
Revision history for this message
Zixing Liu (liushuyu-011) wrote :

After some digging, I think the s390x is missing a fallback ISel path for the f16 type. Here's my preliminary fix based on PPC64 logic (may not be entirely correct):

diff --git a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
index 383393914a16..a2351b205bdb 100644
--- a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
@@ -529,6 +529,14 @@ SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
       setOperationAction(ISD::FSINCOS, VT, Expand);
       setOperationAction(ISD::FREM, VT, Expand);
       setOperationAction(ISD::FPOW, VT, Expand);
+ setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
+ setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
+ setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
+ setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
+ setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
+ setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
+ setTruncStoreAction(MVT::f64, MVT::f16, Expand);
+ setTruncStoreAction(MVT::f32, MVT::f16, Expand);

       // Special treatment.
       setOperationAction(ISD::IS_FPCLASS, VT, Custom);

Revision history for this message
Zixing Liu (liushuyu-011) wrote :
Revision history for this message
Ubuntu Foundations Team Bug Bot (crichton) wrote :

The attachment "0001-fix-f16-load-store-s390x.patch" seems to be a patch. If it isn't, please remove the "patch" flag from the attachment, remove the "patch" tag, and if you are a member of the ~ubuntu-reviewers, unsubscribe the team.

[This is an automated message performed by a Launchpad user owned by ~brian-murray, for any issues please contact him.]

tags: added: patch
Revision history for this message
Zixing Liu (liushuyu-011) wrote :

Rust upstream pointed me to https://github.com/llvm/llvm-project/issues/97981. It seems to affect PowerPC as well.

Revision history for this message
Zixing Liu (liushuyu-011) wrote :
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.