[all-commits] [llvm/llvm-project] 622302: [RISCV] Ensure LPAD alignment for calls to returns...

Jerry Zhang Jian via All-commits all-commits at lists.llvm.org
Mon Jun 1 22:45:51 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 622302e847858a7a8ca5f2b360c67d7647689cc9
      https://github.com/llvm/llvm-project/commit/622302e847858a7a8ca5f2b360c67d7647689cc9
  Author: Jerry Zhang Jian <jerry.zhangjian at sifive.com>
  Date:   2026-06-02 (Tue, 02 Jun 2026)

  Changed paths:
    M llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
    M llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp
    M llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
    M llvm/lib/Target/RISCV/RISCVISelLowering.cpp
    M llvm/lib/Target/RISCV/RISCVIndirectBranchTracking.cpp
    M llvm/lib/Target/RISCV/RISCVInstrInfo.td
    A llvm/test/CodeGen/RISCV/lpad-setjmp-isel.ll
    A llvm/test/CodeGen/RISCV/lpad-setjmp-no-cfp.ll
    A llvm/test/CodeGen/RISCV/lpad-setjmp.ll
    M llvm/test/CodeGen/RISCV/lpad.ll

  Log Message:
  -----------
  [RISCV] Ensure LPAD alignment for calls to returns_twice functions (#177515)

When Zicfilp is enabled, all LPAD instructions must be 4-byte aligned,
including those following a returns_twice call. Linker relaxation can
convert calls to c.jal (RV32C) or cm.jalt (Zcmt), causing LPAD
misalignment.

This patch handles calls to returns_twice functions (e.g., setjmp) when
Zicfilp CFI is enabled:

1. In ISel (LowerCall), detect
CLI.CB->hasFnAttr(Attribute::ReturnsTwice)
   and select LPAD_CALL or LPAD_CALL_INDIRECT ISD opcodes. Custom ISel
   in RISCVISelDAGToDAG adds the landing pad label as a pseudo operand.

2. RISCVAsmPrinter::emitLpadAlignedCall handles assembly output: when
Zca
is enabled, emit .p2align 2 before the call. When linker relaxation is
   also enabled, wrap with .option push/exact/pop to prevent relaxation.
   The LPAD instruction is emitted immediately after the call.

3. RISCVMCCodeEmitter::expandFunctionCallLpad handles object output:
expands the pseudo to AUIPC+JALR+LPAD (direct) or JALR+LPAD (indirect),
   without R_RISCV_RELAX relocation.

Note: Both AsmPrinter and MCCodeEmitter need separate expansion logic
because GNU assembler doesn't yet support call.lpad/jalr.lpad syntax.

4. RISCVIndirectBranchTracking no longer needs to scan for returns_twice
   calls since LPAD is now emitted directly by the pseudo expansion.

The existing lpad.ll test is updated to add cf-protection-branch module
flag, as the test_returns_twice function was intended to test
CFI-enabled
behavior. Without this flag, LPAD insertion after setjmp would not be
triggered.

Known issue: When Large Code Model and Zicfilp are both enabled, direct
calls to returns_twice functions use SW_GUARDED_CALL which takes
priority
over LPAD_CALL. This combination is not yet handled and will be
addressed
in a follow-up patch.

Signed-off-by: Jerry Zhang Jian <jerry.zhangjian at sifive.com>



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list