From f6efe6f1ee42b47cca9c36c6ded0d1712ab7a3f1 Mon Sep 17 00:00:00 2001 From: Nemanja Ivanovic Date: Tue, 12 Mar 2024 16:48:03 +0100 Subject: [PATCH 1/3] [RISC-V] Only emit multiples of 16 as immediate for cm.push An immediate that isn't a multiple of 16 is meaningless and we should always emit the next higher multiple. Otherwise the value is simply truncated of course which leads to clobbering CSR's on the stack. --- llvm/lib/Target/RISCV/RISCVFrameLowering.cpp | 2 +- llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp index 8bac41372b5a83..4f3d915818d1ae 100644 --- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp @@ -555,7 +555,7 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF, FirstFrameSetup->getOpcode() == RISCV::CM_PUSH) { // Use available stack adjustment in push instruction to allocate additional // stack space. - uint64_t Spimm = std::min(StackSize, (uint64_t)48); + uint64_t Spimm = alignTo(std::min(StackSize, (uint64_t)48), 16); FirstFrameSetup->getOperand(1).setImm(Spimm); StackSize -= Spimm; } diff --git a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll index e5c2e0180ee0a6..738d1b07e2210c 100644 --- a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll +++ b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll @@ -3,7 +3,8 @@ define ptr @func(ptr %s, i32 %_c, ptr %incdec.ptr, i1 %0, i8 %conv14) #0 { ; RV32-LABEL: func: ; RV32: # %bb.0: # %entry -; RV32-NEXT: cm.push {ra, s0-s1}, -24 +; RV32-NEXT: cm.push {ra, s0-s1}, -32 +; RV32-NEXT: addi sp, sp, 8 ; RV32-NEXT: .cfi_def_cfa_offset 24 ; RV32-NEXT: .cfi_offset ra, -12 ; RV32-NEXT: .cfi_offset s0, -8 From 7a5cb38d3c3b68215fcf69b3d36cdd280c2ca2f3 Mon Sep 17 00:00:00 2001 From: Nemanja Ivanovic Date: Tue, 12 Mar 2024 16:54:00 +0100 Subject: [PATCH 2/3] Remove the unnecessary stack adjustment (as it's wrong). --- llvm/lib/Target/RISCV/RISCVFrameLowering.cpp | 2 +- llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp index 4f3d915818d1ae..ded712ae4251f9 100644 --- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp @@ -557,7 +557,7 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF, // stack space. uint64_t Spimm = alignTo(std::min(StackSize, (uint64_t)48), 16); FirstFrameSetup->getOperand(1).setImm(Spimm); - StackSize -= Spimm; + StackSize -= std::min(StackSize, Spimm); } if (StackSize != 0) { diff --git a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll index 738d1b07e2210c..c0eb92bed60970 100644 --- a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll +++ b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll @@ -4,7 +4,6 @@ define ptr @func(ptr %s, i32 %_c, ptr %incdec.ptr, i1 %0, i8 %conv14) #0 { ; RV32-LABEL: func: ; RV32: # %bb.0: # %entry ; RV32-NEXT: cm.push {ra, s0-s1}, -32 -; RV32-NEXT: addi sp, sp, 8 ; RV32-NEXT: .cfi_def_cfa_offset 24 ; RV32-NEXT: .cfi_offset ra, -12 ; RV32-NEXT: .cfi_offset s0, -8 From a22b64eb4becedbe46f3c59256c0e35e26a097e5 Mon Sep 17 00:00:00 2001 From: Nemanja Ivanovic Date: Wed, 13 Mar 2024 11:32:23 +0100 Subject: [PATCH 3/3] Fix cm.popret as well... --- llvm/lib/Target/RISCV/RISCVFrameLowering.cpp | 4 ++-- llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp index ded712ae4251f9..af90578a54b384 100644 --- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp @@ -777,9 +777,9 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF, MBBI->getOpcode() == RISCV::CM_POP) { // Use available stack adjustment in pop instruction to deallocate stack // space. - uint64_t Spimm = std::min(StackSize, (uint64_t)48); + uint64_t Spimm = alignTo(std::min(StackSize, (uint64_t)48), 16); MBBI->getOperand(1).setImm(Spimm); - StackSize -= Spimm; + StackSize -= std::min(StackSize, Spimm); } // Deallocate stack diff --git a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll index c0eb92bed60970..d3e25235835ec3 100644 --- a/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll +++ b/llvm/test/CodeGen/RISCV/zcmp-additional-stack.ll @@ -31,7 +31,7 @@ define ptr @func(ptr %s, i32 %_c, ptr %incdec.ptr, i1 %0, i8 %conv14) #0 { ; RV32-NEXT: lw a0, 4(sp) # 4-byte Folded Reload ; RV32-NEXT: sb a0, 0(s0) ; RV32-NEXT: mv a0, s1 -; RV32-NEXT: cm.popret {ra, s0-s1}, 24 +; RV32-NEXT: cm.popret {ra, s0-s1}, 32 entry: br label %while.body