llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.92k stars 11.52k forks source link

`SelectionDAGLegalize` crash with assertions on `powi.f16`/`powi.f16.i32` #105747

Closed tgross35 closed 2 weeks ago

tgross35 commented 3 weeks ago

Source code:

target triple = "x86_64-unknown-linux-gnu"

define half @test_half_powi(half %self, i32 %n) {
start:
  %v = call half @llvm.powi.f16.i32(half %self, i32 %n)
  ret half %v
}

Crash (assertions required):

llc: /root/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp:4708: void {anonymous}::SelectionDAGLegalize::ConvertNodeToLibcall(llvm::SDNode*): Assertion `LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi."' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.  Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/llc -o /app/output.s -x86-asm-syntax=intel <source>
1.  Running pass 'Function Pass Manager' on module '<source>'.
2.  Running pass 'X86 DAG->DAG Instruction Selection' on function '@test_half_powi'
 #0 0x0000000003ae7028 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3ae7028)
 #1 0x0000000003ae479c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007b9927e42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007b9927e969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007b9927e42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007b9927e287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007b9927e2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007b9927e39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x000000000376d745 (anonymous namespace)::SelectionDAGLegalize::ConvertNodeToLibcall(llvm::SDNode*) LegalizeDAG.cpp:0:0
 #9 0x000000000377f1a6 (anonymous namespace)::SelectionDAGLegalize::LegalizeOp(llvm::SDNode*) (.part.0) LegalizeDAG.cpp:0:0
#10 0x0000000003782883 llvm::SelectionDAG::Legalize() (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3782883)
#11 0x0000000003899fb7 llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3899fb7)
#12 0x000000000389d1fa llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x389d1fa)
#13 0x000000000389e550 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x389e550)
#14 0x000000000388f3ef llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x388f3ef)
#15 0x0000000002a848e9 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#16 0x0000000003073ded llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3073ded)
#17 0x0000000003074181 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3074181)
#18 0x000000000307347c llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x307347c)
#19 0x000000000084693c compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0
#20 0x0000000000737a66 main (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x737a66)
#21 0x00007b9927e29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#22 0x00007b9927e29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#23 0x000000000083d92e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x83d92e)
Program terminated with signal: SIGSEGV
Compiler returned: 139

Link: https://llvm.godbolt.org/z/zhb1bxWK8

tgross35 commented 3 weeks ago

It seems like it is able to do the correct thing if assertions are disabled, this seems to produce the correct result in https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=1abe8952ee9d287a4f2b31c812e00743.

llvmbot commented 3 weeks ago

@llvm/issue-subscribers-backend-x86

Author: Trevor Gross (tgross35)

Source code: ```llvm target triple = "x86_64-unknown-linux-gnu" define half @test_half_powi(half %self, i32 %n) { start: %v = call half @llvm.powi.f16.i32(half %self, i32 %n) ret half %v } ``` Crash (assertions required): ```text llc: /root/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp:4708: void {anonymous}::SelectionDAGLegalize::ConvertNodeToLibcall(llvm::SDNode*): Assertion `LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi."' failed. PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace. Stack dump: 0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/llc -o /app/output.s -x86-asm-syntax=intel <source> 1. Running pass 'Function Pass Manager' on module '<source>'. 2. Running pass 'X86 DAG->DAG Instruction Selection' on function '@test_half_powi' #0 0x0000000003ae7028 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3ae7028) #1 0x0000000003ae479c SignalHandler(int) Signals.cpp:0:0 #2 0x00007b9927e42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520) #3 0x00007b9927e969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc) #4 0x00007b9927e42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476) #5 0x00007b9927e287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3) #6 0x00007b9927e2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b) #7 0x00007b9927e39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96) #8 0x000000000376d745 (anonymous namespace)::SelectionDAGLegalize::ConvertNodeToLibcall(llvm::SDNode*) LegalizeDAG.cpp:0:0 #9 0x000000000377f1a6 (anonymous namespace)::SelectionDAGLegalize::LegalizeOp(llvm::SDNode*) (.part.0) LegalizeDAG.cpp:0:0 #10 0x0000000003782883 llvm::SelectionDAG::Legalize() (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3782883) #11 0x0000000003899fb7 llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3899fb7) #12 0x000000000389d1fa llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x389d1fa) #13 0x000000000389e550 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x389e550) #14 0x000000000388f3ef llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x388f3ef) #15 0x0000000002a848e9 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0 #16 0x0000000003073ded llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3073ded) #17 0x0000000003074181 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3074181) #18 0x000000000307347c llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x307347c) #19 0x000000000084693c compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0 #20 0x0000000000737a66 main (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x737a66) #21 0x00007b9927e29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90) #22 0x00007b9927e29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40) #23 0x000000000083d92e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x83d92e) Program terminated with signal: SIGSEGV Compiler returned: 139 ``` Link: https://llvm.godbolt.org/z/zhb1bxWK8
tgross35 commented 3 weeks ago

^ to affirm the labels, this does not seem to crash on aarch64.

v01dXYZ commented 3 weeks ago

RTLIB::getFPLibCall doesn't support f16. The problem could arise with many other targets/Opcodes.

tgross35 commented 3 weeks ago

What is getFPLibCall expected to do in these cases? I don't think there are any platforms that would actually have external f16 functions.

tgross35 commented 3 weeks ago

Testing some other arches. At least the following seem to work without crash, even though I know some of them have f16 ABI issues:

v01dXYZ commented 2 weeks ago

SUCCESS

aarch64

Legalizing: t5: f16 = fpowi t2, t4
Trying to promote node
Creating new node: t9: f32 = fp_extend t2
Creating new node: t10: f32 = fpowi t9, t4
Creating constant: t11: i64 = TargetConstant<0>
Creating new node: t12: f16 = fp_round t10, TargetConstant:i64<0>
Successfully promoted node
 ... replacing: t5: f16 = fpowi t2, t4
     with:      t12: f16 = fp_round t10, TargetConstant:i64<0>

aarch64-pc-windows-msvc

Legalizing: t5: f16 = fpowi t2, t4
Trying to promote node
Creating new node: t9: f32 = fp_extend t2
Creating new node: t10: f32 = fpowi t9, t4
Creating constant: t11: i64 = TargetConstant<0>
Creating new node: t12: f16 = fp_round t10, TargetConstant:i64<0>
Successfully promoted node
 ... replacing: t5: f16 = fpowi t2, t4
     with:      t12: f16 = fp_round t10, TargetConstant:i64<0>

aarch64-unknown-linux-gnu

Legalizing: t5: f16 = fpowi t2, t4
Trying to promote node
Creating new node: t9: f32 = fp_extend t2
Creating new node: t10: f32 = fpowi t9, t4
Creating constant: t11: i64 = TargetConstant<0>
Creating new node: t12: f16 = fp_round t10, TargetConstant:i64<0>
Successfully promoted node
 ... replacing: t5: f16 = fpowi t2, t4
     with:      t12: f16 = fp_round t10, TargetConstant:i64<0>

i586-unknown-linux-gnu

Legalizing node: t7: f16 = fpowi t13, t5
Analyzing result type: f16
Soft promote half result 0: t7: f16 = fpowi t13, t5
Creating new node: t15: f32 = fp16_to_fp t14
Creating new node: t16: f32 = fpowi t15, t5
Creating new node: t17: i16 = fp_to_fp16 t16

i586-unknown-linux-musl

Legalizing node: t7: f16 = fpowi t13, t5
Analyzing result type: f16
Soft promote half result 0: t7: f16 = fpowi t13, t5
Creating new node: t15: f32 = fp16_to_fp t14
Creating new node: t16: f32 = fpowi t15, t5
Creating new node: t17: i16 = fp_to_fp16 t16

i686-unknown-linux-gnu

Legalizing node: t7: f16 = fpowi t13, t5
Analyzing result type: f16
Soft promote half result 0: t7: f16 = fpowi t13, t5
Creating new node: t15: f32 = fp16_to_fp t14
Creating new node: t16: f32 = fpowi t15, t5
Creating new node: t17: i16 = fp_to_fp16 t16

loongarch64-unknown-linux-gnu

Legalizing node: t8: f16 = fpowi t6, t7
Analyzing result type: f16
Promote float result 0: t8: f16 = fpowi t6, t7
Creating new node: t28: f32 = fpowi t14, t7

riscv64-unknown-linux-gnu

Legalizing node: t8: f16 = fpowi t6, t7
Analyzing result type: f16
Soft promote half result 0: t8: f16 = fpowi t6, t7
Creating new node: t14: f32 = fp16_to_fp t5
Creating new node: t15: f32 = fpowi t14, t7
Creating new node: t16: i16 = fp_to_fp16 t15

FAIL

x86_64

Assertion failed: LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi." (/home/v01dxyz/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp: ConvertNodeToLibcall: 4708)
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.  Program arguments: /home/v01dxyz/llvm-project/build.indvars_simplify/bin/llc faulty.ll --mtriple=x86_64 -debug -o=/dev/null
1.  Running pass 'Function Pass Manager' on module 'faulty.ll'.
2.  Running pass 'X86 DAG->DAG Instruction Selection' on function '@test_half_powi'
#0 0x00007f1acfcec809 PrintStackTraceSignalHandler(void*) /home/v01dxyz/llvm-project/llvm/lib/Support/Unix/Signals.inc:798:1
#1 0x00007f1acfce9a7f llvm::sys::RunSignalHandlers() /home/v01dxyz/llvm-project/llvm/lib/Support/Signals.cpp:105:20
#2 0x00007f1acfcebe31 SignalHandler(int) /home/v01dxyz/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
#3 0x00007f1adb7789be __restore_rt /builddir/musl-1.1.24/src/signal/x86_64/restore.s:6:0
Legalizing: t5: f16 = fpowi t2, t4
Trying to expand node
Cannot expand node
Trying to convert node to libcall

DEBUG-TIMEOUT

The following timeout ones with -debug. I don't know the reason. Nevertheless it's sucessful at legalisation.

arm-unknown-linux-gnueabi

Legalizing: t5: f16 = fpowi t2, t4
Trying to promote node
Creating new node: t9: f32 = fp_extend t2
Creating new node: t10: f32 = fpowi t9, t4
Creating constant: t11: i64 = TargetConstant<0>
Creating new node: t12: f16 = fp_round t10, TargetConstant:i64<0>
Successfully promoted node
 ... replacing: t5: f16 = fpowi t2, t4
     with:      t12: f16 = fp_round t10, TargetConstant:i64<0>

armv7-unknown-linux-gnueabihf

Legalizing: t5: f16 = fpowi t2, t4
Trying to promote node
Creating new node: t9: f32 = fp_extend t2
Creating new node: t10: f32 = fpowi t9, t4
Creating constant: t11: i64 = TargetConstant<0>
Creating new node: t12: f16 = fp_round t10, TargetConstant:i64<0>
Successfully promoted node
 ... replacing: t5: f16 = fpowi t2, t4
     with:      t12: f16 = fp_round t10, TargetConstant:i64<0>
v01dXYZ commented 2 weeks ago

Normally, f16 is promoted by default if f16 is not a legal type (cf TargetLoweringBase::computeRegisterProperties). A target with f16 being legal should set an operation action for FPOWI.

X86 with SSE2 makes f16 legal without doing the latter.

if you try your example llc -mattr=-sse2, it works (just to prove my point).

phoebewang commented 2 weeks ago

f16 without SSE2 has ABI issue, so it's not a valiad case. We have error out this in frontend https://godbolt.org/z/6K3j9n4G4