Skip to content

[RISCV] Explicitly set FRM defs as non-dead to prevent their reordering with instructions that may use it #135176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20970,6 +20970,13 @@ RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,

void RISCVTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
SDNode *Node) const {
// If instruction defines FRM operand, conservatively set it as non-dead to
// express data dependency with FRM users and prevent incorrect instruction
// reordering.
if (auto *FRMDef = MI.findRegisterDefOperand(RISCV::FRM, /*TRI=*/nullptr)) {
FRMDef->setIsDead(false);
return;
}
// Add FRM dependency to any instructions with dynamic rounding mode.
int Idx = RISCV::getNamedOperandIdx(MI.getOpcode(), RISCV::OpName::frm);
if (Idx < 0) {
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -1941,9 +1941,11 @@ class SwapSysRegImm<SysReg SR, list<Register> Regs>
}

def ReadFRM : ReadSysReg<SysRegFRM, [FRM]>;
let hasPostISelHook = 1 in {
def WriteFRM : WriteSysReg<SysRegFRM, [FRM]>;
def WriteFRMImm : WriteSysRegImm<SysRegFRM, [FRM]>;
def SwapFRMImm : SwapSysRegImm<SysRegFRM, [FRM]>;
}

def WriteVXRMImm : WriteSysRegImm<SysRegVXRM, [VXRM]>;

Expand Down
8 changes: 4 additions & 4 deletions llvm/test/CodeGen/RISCV/frm-write-in-loop.ll
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ define double @foo(double %0, double %1, i64 %n) strictfp {
; CHECK-LABEL: foo:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: fmv.d.x fa5, zero
; CHECK-NEXT: fsrmi 3
; CHECK-NEXT: fsrmi 0
; CHECK-NEXT: .LBB0_1: # %loop
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
; CHECK-NEXT: fsrmi 3
; CHECK-NEXT: fadd.d fa5, fa5, fa0
; CHECK-NEXT: addi a0, a0, -1
; CHECK-NEXT: fsrmi 0
; CHECK-NEXT: fadd.d fa5, fa5, fa1
; CHECK-NEXT: beqz a0, .LBB0_1
; CHECK-NEXT: # %bb.2: # %exit
Expand Down Expand Up @@ -53,12 +53,12 @@ define double @bar(double %0, double %1, i64 %n) strictfp {
; CHECK-NEXT: fmv.d fs0, fa1
; CHECK-NEXT: fmv.d fs1, fa0
; CHECK-NEXT: fmv.d.x fa0, zero
; CHECK-NEXT: fsrmi 3
; CHECK-NEXT: fsrmi 0
; CHECK-NEXT: .LBB1_1: # %loop
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
; CHECK-NEXT: fsrmi 3
; CHECK-NEXT: fmv.d fa1, fs1
; CHECK-NEXT: call baz
; CHECK-NEXT: fsrmi 0
; CHECK-NEXT: fmv.d fa1, fs0
; CHECK-NEXT: call baz
; CHECK-NEXT: addi s0, s0, -1
Expand Down
Loading