[Statepoint] Asserion failure with fast-isel and non-opts.
Description
Here is a modificated test from llvm/test/Transforms/CodeGenPrepare/X86/statepoint-relocate.ll. The modification replaces ret i32 %ret by the ret i32 0.
target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
declare zeroext i1 @return_i1()
define i32 @test_sor_basic(i32 addrspace(1)* %base) gc "statepoint-example" {
entry:
%tok = call token (i64, i32, i1 ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_i1f(i64 0, i32 0, i1 ()* elementtype(i1 ()) @return_i1, i32 0, i32 0, i32 0, i32 0) ["gc-live"(i32 addrspace(1)* %base)]
%base-new = call i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %tok, i32 0, i32 0)
%ret = load i32, i32 addrspace(1)* %base-new
ret i32 0
}
declare token @llvm.experimental.gc.statepoint.p0f_i1f(i64, i32, i1 ()*, i32, i32, ...)
declare i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token, i32, i32)
During llc compilation with --fast-isel=true -O0 we fall down by the assertion:
$ llc t.ll --fast-isel=true -O0 -o t.asm
llc: /root/llvm-project/llvm/lib/CodeGen/SelectionDAG/StatepointLowering.cpp:106: void llvm::StatepointLoweringState::clear(): Assertion `PendingGCRelocateCalls.empty() && "cleared before statepoint sequence completed"' 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 --fast-isel=true -O0 <source>
1. Running pass 'Function Pass Manager' on module '<source>'.
2. Running pass 'X86 DAG->DAG Instruction Selection' on function '@test_sor_basic'
#0 0x00005559d32f011f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
#1 0x00005559d32edb4c SignalHandler(int) Signals.cpp:0:0
#2 0x00007f11cca6a3c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x143c0)
#3 0x00007f11cc53703b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4303b)
#4 0x00007f11cc516859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
#5 0x00007f11cc516729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
#6 0x00007f11cc528006 (/lib/x86_64-linux-gnu/libc.so.6+0x34006)
#7 0x00005559d30f8e03 (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x30f8e03)
#8 0x00005559d30ecb8d llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, true>, bool&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x30ecb8d)
#9 0x00005559d30edf73 llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x30edf73)
#10 0x00005559d30efd92 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (.part.1063) SelectionDAGISel.cpp:0:0
#11 0x00005559d1c2d7a5 (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) X86ISelDAGToDAG.cpp:0:0
#12 0x00005559d25fee16 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x25fee16)
#13 0x00005559d2ac6146 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2ac6146)
#14 0x00005559d2ac6499 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2ac6499)
#15 0x00005559d2ac6df9 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2ac6df9)
#16 0x00005559d0ba85d6 compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0
#17 0x00005559d0b00917 main (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0xb00917)
#18 0x00007f11cc5180b3 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b3)
#19 0x00005559d0b9ef9a _start (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0xb9ef9a)
Compiler returned: 139
Explanation
Brief investigation shows that llvm does not actually visit gc.relocate, because isFoldedOrDeadInstruction for load and gc.reloacte returns true and these instructions are not lowered by FastISel.
https://github.com/llvm/llvm-project/blob/939c57097ecd7ba7760d5c011a5034df17b5a943/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp#L1496-L1505
At the same time %base-new has uses, so we will remember them and later fall by the assertion.
https://github.com/llvm/llvm-project/blob/3f8e4169c1c390fd086658c1e51983ee61bff9bc/llvm/lib/CodeGen/SelectionDAG/StatepointLowering.h#L63-L70
https://github.com/llvm/llvm-project/blob/3f8e4169c1c390fd086658c1e51983ee61bff9bc/llvm/lib/CodeGen/SelectionDAG/StatepointLowering.cpp#L102-L107
Possible solution
There is an obvious solution, described in the patch below. But we're not sure that such approach is proper: gc.relocate will lower to instructions that read pointers from stack and it might generate reduntant code in final assembler, we dont have an example though. The example from description section does not contain a redundant stack manipulation, even if gc.relocate has been processed.
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index 2b63359c2b1b..39e8bf408688 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -1323,6 +1323,7 @@ static bool isFoldedOrDeadInstruction(const Instruction *I,
return !I->mayWriteToMemory() && // Side-effecting instructions aren't folded.
!I->isTerminator() && // Terminators aren't folded.
!isa<DbgInfoIntrinsic>(I) && // Debug instructions aren't folded.
+ !isa<GCRelocateInst>(I) && // gc.relocate instructions are't folded.
!I->isEHPad() && // EH pad instructions aren't folded.
!FuncInfo.isExportedInst(I); // Exported instrs must be computed.
}
We appreciate if you have any ideas for a propper fix such problem. CC @preames