From f21d307fe5319ebfe4876df04502d00afd4ae9d5 Mon Sep 17 00:00:00 2001 From: Weining Lu Date: Sat, 7 Jun 2025 15:10:24 +0800 Subject: [PATCH 1/3] [LoongArch] Precommit test case to show bug in LoongArchISelDagToDag The optimization level should not be restored into O2. (cherry picked from commit fcc82cfa9394b2bd4380acdcf0e2854caee5a47a) --- llvm/test/CodeGen/LoongArch/isel-optnone.ll | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 llvm/test/CodeGen/LoongArch/isel-optnone.ll diff --git a/llvm/test/CodeGen/LoongArch/isel-optnone.ll b/llvm/test/CodeGen/LoongArch/isel-optnone.ll new file mode 100644 index 000000000000..d44f1405d0c1 --- /dev/null +++ b/llvm/test/CodeGen/LoongArch/isel-optnone.ll @@ -0,0 +1,13 @@ +; REQUIRES: asserts +; RUN: llc %s -O0 -mtriple=loongarch64 -o /dev/null -debug-only=isel 2>&1 | FileCheck %s + +define void @fooOptnone() #0 { +; CHECK: Changing optimization level for Function fooOptnone +; CHECK: Before: -O2 ; After: -O0 + +; CHECK: Restoring optimization level for Function fooOptnone +; CHECK: Before: -O0 ; After: -O2 + ret void +} + +attributes #0 = { nounwind optnone noinline } -- Gitee From 0bab0e392ad340a3edbf83292157c61aa57dbdc2 Mon Sep 17 00:00:00 2001 From: Weining Lu Date: Sat, 7 Jun 2025 11:45:39 +0800 Subject: [PATCH 2/3] [LoongArch] Pass OptLevel to LoongArchDAGToDAGISel correctly Like many other targets did. And see RISCV for similar fix. Fix https://github.com/llvm/llvm-project/issues/143239 (cherry picked from commit 90a52f4942961a5c32afc69d69470c6b7e5bcb8a) --- llvm/lib/Target/LoongArch/LoongArch.h | 3 ++- llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.cpp | 5 +++-- llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.h | 5 +++-- llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp | 2 +- llvm/test/CodeGen/LoongArch/O0-pipeline.ll | 8 -------- llvm/test/CodeGen/LoongArch/isel-optnone.ll | 7 ++----- llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll | 1 + 7 files changed, 12 insertions(+), 19 deletions(-) diff --git a/llvm/lib/Target/LoongArch/LoongArch.h b/llvm/lib/Target/LoongArch/LoongArch.h index 09ca089c9115..a2267024a976 100644 --- a/llvm/lib/Target/LoongArch/LoongArch.h +++ b/llvm/lib/Target/LoongArch/LoongArch.h @@ -34,7 +34,8 @@ bool lowerLoongArchMachineOperandToMCOperand(const MachineOperand &MO, const AsmPrinter &AP); FunctionPass *createLoongArchExpandAtomicPseudoPass(); -FunctionPass *createLoongArchISelDag(LoongArchTargetMachine &TM); +FunctionPass *createLoongArchISelDag(LoongArchTargetMachine &TM, + CodeGenOpt::Level OptLevel); FunctionPass *createLoongArchPreRAExpandPseudoPass(); FunctionPass *createLoongArchExpandPseudoPass(); void initializeLoongArchDAGToDAGISelPass(PassRegistry &); diff --git a/llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.cpp b/llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.cpp index 01b2f720f902..535405022eda 100644 --- a/llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.cpp +++ b/llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.cpp @@ -412,6 +412,7 @@ bool LoongArchDAGToDAGISel::selectVSplatUimmPow2(SDValue N, // This pass converts a legalized DAG into a LoongArch-specific DAG, ready // for instruction scheduling. -FunctionPass *llvm::createLoongArchISelDag(LoongArchTargetMachine &TM) { - return new LoongArchDAGToDAGISel(TM); +FunctionPass *llvm::createLoongArchISelDag(LoongArchTargetMachine &TM, + CodeGenOpt::Level OptLevel) { + return new LoongArchDAGToDAGISel(TM, OptLevel); } diff --git a/llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.h b/llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.h index 5e3d6ccc3755..8149e0432126 100644 --- a/llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.h +++ b/llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.h @@ -28,8 +28,9 @@ public: LoongArchDAGToDAGISel() = delete; - explicit LoongArchDAGToDAGISel(LoongArchTargetMachine &TM) - : SelectionDAGISel(ID, TM) {} + explicit LoongArchDAGToDAGISel(LoongArchTargetMachine &TM, + CodeGenOpt::Level OptLevel) + : SelectionDAGISel(ID, TM, OptLevel) {} bool runOnMachineFunction(MachineFunction &MF) override { Subtarget = &MF.getSubtarget(); diff --git a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp index 0efc5e6ebb99..c683902331d9 100644 --- a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp +++ b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp @@ -167,7 +167,7 @@ void LoongArchPassConfig::addIRPasses() { } bool LoongArchPassConfig::addInstSelector() { - addPass(createLoongArchISelDag(getLoongArchTargetMachine())); + addPass(createLoongArchISelDag(getLoongArchTargetMachine(), getOptLevel())); return false; } diff --git a/llvm/test/CodeGen/LoongArch/O0-pipeline.ll b/llvm/test/CodeGen/LoongArch/O0-pipeline.ll index 84d235d78eb9..9786390687d6 100644 --- a/llvm/test/CodeGen/LoongArch/O0-pipeline.ll +++ b/llvm/test/CodeGen/LoongArch/O0-pipeline.ll @@ -35,15 +35,7 @@ ; CHECK-NEXT: Safe Stack instrumentation pass ; CHECK-NEXT: Insert stack protectors ; CHECK-NEXT: Module Verifier -; CHECK-NEXT: Dominator Tree Construction -; CHECK-NEXT: Basic Alias Analysis (stateless AA impl) -; CHECK-NEXT: Function Alias Analysis Results -; CHECK-NEXT: Natural Loop Information -; CHECK-NEXT: Post-Dominator Tree Construction -; CHECK-NEXT: Branch Probability Analysis ; CHECK-NEXT: Assignment Tracking Analysis -; CHECK-NEXT: Lazy Branch Probability Analysis -; CHECK-NEXT: Lazy Block Frequency Analysis ; CHECK-NEXT: LoongArch DAG->DAG Pattern Instruction Selection ; CHECK-NEXT: Finalize ISel and expand pseudo-instructions ; CHECK-NEXT: Local Stack Slot Allocation diff --git a/llvm/test/CodeGen/LoongArch/isel-optnone.ll b/llvm/test/CodeGen/LoongArch/isel-optnone.ll index d44f1405d0c1..4d2528a3148a 100644 --- a/llvm/test/CodeGen/LoongArch/isel-optnone.ll +++ b/llvm/test/CodeGen/LoongArch/isel-optnone.ll @@ -2,11 +2,8 @@ ; RUN: llc %s -O0 -mtriple=loongarch64 -o /dev/null -debug-only=isel 2>&1 | FileCheck %s define void @fooOptnone() #0 { -; CHECK: Changing optimization level for Function fooOptnone -; CHECK: Before: -O2 ; After: -O0 - -; CHECK: Restoring optimization level for Function fooOptnone -; CHECK: Before: -O0 ; After: -O2 +; CHECK-NOT: Changing optimization level for Function fooOptnone +; CHECK-NOT: Restoring optimization level for Function fooOptnone ret void } diff --git a/llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll b/llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll index 092da5aba2d9..ff242c2a0f98 100644 --- a/llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll +++ b/llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll @@ -39,6 +39,7 @@ define dso_local ptr @f(i32 noundef signext %i) "frame-pointer"="all" { ; CHECK-NEXT: b .LBB0_3 ; CHECK-NEXT: .LBB0_3: # %if.end ; CHECK-NEXT: ld.d $a0, $fp, -48 # 8-byte Folded Reload +; CHECK-NEXT: addi.w $a0, $a0, 0 ; CHECK-NEXT: ori $a1, $zero, 1 ; CHECK-NEXT: bne $a0, $a1, .LBB0_6 ; CHECK-NEXT: b .LBB0_4 -- Gitee From 140f96291d6ddbc8dad137fee389e532e5d5d5dd Mon Sep 17 00:00:00 2001 From: chenli Date: Sat, 14 Jun 2025 10:24:50 +0800 Subject: [PATCH 3/3] [LoongArch] Fix test cases after 0bab0e392ad3 --- llvm/test/CodeGen/LoongArch/shrinkwrap.ll | 4 +--- llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll | 12 ++++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/llvm/test/CodeGen/LoongArch/shrinkwrap.ll b/llvm/test/CodeGen/LoongArch/shrinkwrap.ll index 5f15dd2e7eaf..0323b56080f8 100644 --- a/llvm/test/CodeGen/LoongArch/shrinkwrap.ll +++ b/llvm/test/CodeGen/LoongArch/shrinkwrap.ll @@ -9,7 +9,6 @@ define void @eliminate_restore(i32 %n) nounwind { ; NOSHRINKW: # %bb.0: ; NOSHRINKW-NEXT: addi.d $sp, $sp, -16 ; NOSHRINKW-NEXT: st.d $ra, $sp, 8 # 8-byte Folded Spill -; NOSHRINKW-NEXT: # kill: def $r5 killed $r4 ; NOSHRINKW-NEXT: addi.w $a1, $a0, 0 ; NOSHRINKW-NEXT: ori $a0, $zero, 32 ; NOSHRINKW-NEXT: bltu $a0, $a1, .LBB0_2 @@ -52,9 +51,8 @@ define void @conditional_alloca(i32 %n) nounwind { ; NOSHRINKW-NEXT: st.d $ra, $sp, 24 # 8-byte Folded Spill ; NOSHRINKW-NEXT: st.d $fp, $sp, 16 # 8-byte Folded Spill ; NOSHRINKW-NEXT: addi.d $fp, $sp, 32 -; NOSHRINKW-NEXT: move $a1, $a0 -; NOSHRINKW-NEXT: st.d $a1, $fp, -24 # 8-byte Folded Spill ; NOSHRINKW-NEXT: addi.w $a1, $a0, 0 +; NOSHRINKW-NEXT: st.d $a0, $fp, -24 # 8-byte Folded Spill ; NOSHRINKW-NEXT: ori $a0, $zero, 32 ; NOSHRINKW-NEXT: bltu $a0, $a1, .LBB1_2 ; NOSHRINKW-NEXT: b .LBB1_1 diff --git a/llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll b/llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll index ff242c2a0f98..6fba703018f0 100644 --- a/llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll +++ b/llvm/test/CodeGen/LoongArch/spill-ra-without-kill.ll @@ -19,13 +19,13 @@ define dso_local ptr @f(i32 noundef signext %i) "frame-pointer"="all" { ; CHECK-NEXT: addi.d $fp, $sp, 48 ; CHECK-NEXT: .cfi_def_cfa 22, 0 ; CHECK-NEXT: st.d $ra, $fp, -40 # 8-byte Folded Spill -; CHECK-NEXT: move $a1, $a0 +; CHECK-NEXT: # kill: def $r5 killed $r4 +; CHECK-NEXT: st.w $a0, $fp, -28 ; CHECK-NEXT: pcalau12i $a0, %pc_hi20(calls) -; CHECK-NEXT: addi.d $a3, $a0, %pc_lo12(calls) -; CHECK-NEXT: ld.w $a0, $a3, 0 -; CHECK-NEXT: addi.d $a2, $a0, 1 -; CHECK-NEXT: st.w $a2, $a3, 0 -; CHECK-NEXT: st.w $a1, $fp, -28 +; CHECK-NEXT: addi.d $a2, $a0, %pc_lo12(calls) +; CHECK-NEXT: ld.w $a0, $a2, 0 +; CHECK-NEXT: addi.d $a1, $a0, 1 +; CHECK-NEXT: st.w $a1, $a2, 0 ; CHECK-NEXT: bnez $a0, .LBB0_2 ; CHECK-NEXT: b .LBB0_1 ; CHECK-NEXT: .LBB0_1: # %if.then -- Gitee