[llvm] e8b9c19 - [RISCV] Don't truncate MaxAlignment to int when realigning the stack (#228225)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 19:24:05 PDT 2026


Author: Divyansh Yadav
Date: 2026-10-02T19:23:57-07:00
New Revision: e8b9c1990a691bee7b42db18936e1c2892de6e68

URL: https://github.com/llvm/llvm-project/commit/e8b9c1990a691bee7b42db18936e1c2892de6e68
DIFF: https://github.com/llvm/llvm-project/commit/e8b9c1990a691bee7b42db18936e1c2892de6e68.diff

LOG: [RISCV] Don't truncate MaxAlignment to int when realigning the stack (#228225)

emitPrologue casts MaxAlignment to int before checking whether the mask
fits in an ANDI immediate. With an alloca of align 4294967296 the cast
gives 0, the isInt<12> check passes, and we emit `andi sp, sp, 0`.
Casting to int64_t instead makes this case use the srli/slli sequence.

This is the patch Mitch Briles posted on the issue, but with int64_t
instead of long, since long is only 32 bits on Windows hosts.

The test is RV64 only. On RV32 this alignment already fails with "Should
only materialize 32-bit constants for RV32" while setting up the frame
(before and after this change), so it couldn't go into
stack-realignment.ll alongside the RV32 run lines.

Fixes #228084

Co-authored-by: Mitch Briles <mitchbriles at gmail.com>

Added: 
    llvm/test/CodeGen/RISCV/pr228084.ll

Modified: 
    llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
index 7d2539ab3f2da..ac31c070242d8 100644
--- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
@@ -1258,10 +1258,10 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
       Align MaxAlignment = MFI.getMaxAlign();
 
       const RISCVInstrInfo *TII = STI.getInstrInfo();
-      if (isInt<12>(-(int)MaxAlignment.value())) {
+      if (isInt<12>(-(int64_t)MaxAlignment.value())) {
         BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
             .addReg(SPReg)
-            .addImm(-(int)MaxAlignment.value())
+            .addImm(-(int64_t)MaxAlignment.value())
             .setMIFlag(MachineInstr::FrameSetup);
       } else {
         unsigned ShiftAmount = Log2(MaxAlignment);

diff  --git a/llvm/test/CodeGen/RISCV/pr228084.ll b/llvm/test/CodeGen/RISCV/pr228084.ll
new file mode 100644
index 0000000000000..3949ab44486fa
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/pr228084.ll
@@ -0,0 +1,39 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s | FileCheck %s
+
+;; Check that stack alignment >= 2^32 works correctly.
+
+define void @f() {
+; CHECK-LABEL: f:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    addi sp, sp, -2032
+; CHECK-NEXT:    .cfi_def_cfa_offset 2032
+; CHECK-NEXT:    sd ra, 2024(sp) # 8-byte Folded Spill
+; CHECK-NEXT:    sd s0, 2016(sp) # 8-byte Folded Spill
+; CHECK-NEXT:    .cfi_offset ra, -8
+; CHECK-NEXT:    .cfi_offset s0, -16
+; CHECK-NEXT:    addi s0, sp, 2032
+; CHECK-NEXT:    .cfi_def_cfa s0, 0
+; CHECK-NEXT:    li a0, 1
+; CHECK-NEXT:    slli a0, a0, 33
+; CHECK-NEXT:    addi a0, a0, -2032
+; CHECK-NEXT:    sub sp, sp, a0
+; CHECK-NEXT:    srli a0, sp, 32
+; CHECK-NEXT:    slli sp, a0, 32
+; CHECK-NEXT:    li a0, 1
+; CHECK-NEXT:    slli a0, a0, 32
+; CHECK-NEXT:    add a0, sp, a0
+; CHECK-NEXT:    sd zero, 0(a0)
+; CHECK-NEXT:    addi sp, s0, -2032
+; CHECK-NEXT:    .cfi_def_cfa sp, 2032
+; CHECK-NEXT:    ld ra, 2024(sp) # 8-byte Folded Reload
+; CHECK-NEXT:    ld s0, 2016(sp) # 8-byte Folded Reload
+; CHECK-NEXT:    .cfi_restore ra
+; CHECK-NEXT:    .cfi_restore s0
+; CHECK-NEXT:    addi sp, sp, 2032
+; CHECK-NEXT:    .cfi_def_cfa_offset 0
+; CHECK-NEXT:    ret
+  %1 = alloca i64, align 4294967296
+  store i64 0, ptr %1, align 4294967296
+  ret void
+}


        


More information about the llvm-commits mailing list