Skip to content

Commit 9b559ca

Browse files
committed
[RISCV] Explicitly set FRM defs as non-dead to prevent their reordering with instructions that may use it
1 parent c24c643 commit 9b559ca

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20926,6 +20926,15 @@ RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
2092620926

2092720927
void RISCVTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
2092820928
SDNode *Node) const {
20929+
// If instruction defines FRM operand, conservatively set it as non-dead to
20930+
// express data dependency with FRM users and prevent incorrect instruction
20931+
// reordering (these defs are marked as dead because at the moment of
20932+
// instruction emission they actually don't have any uses - they are added
20933+
// later in this hook).
20934+
if (auto *FRMDef = MI.findRegisterDefOperand(RISCV::FRM, /*TRI=*/nullptr)) {
20935+
FRMDef->setIsDead(false);
20936+
return;
20937+
}
2092920938
// Add FRM dependency to any instructions with dynamic rounding mode.
2093020939
int Idx = RISCV::getNamedOperandIdx(MI.getOpcode(), RISCV::OpName::frm);
2093120940
if (Idx < 0) {

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,9 +1941,11 @@ class SwapSysRegImm<SysReg SR, list<Register> Regs>
19411941
}
19421942

19431943
def ReadFRM : ReadSysReg<SysRegFRM, [FRM]>;
1944+
let hasPostISelHook = 1 in {
19441945
def WriteFRM : WriteSysReg<SysRegFRM, [FRM]>;
19451946
def WriteFRMImm : WriteSysRegImm<SysRegFRM, [FRM]>;
19461947
def SwapFRMImm : SwapSysRegImm<SysRegFRM, [FRM]>;
1948+
}
19471949

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

llvm/test/CodeGen/RISCV/frm-write-in-loop.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ define double @foo(double %0, double %1, i64 %n) strictfp {
77
; CHECK-LABEL: foo:
88
; CHECK: # %bb.0: # %entry
99
; CHECK-NEXT: fmv.d.x fa5, zero
10-
; CHECK-NEXT: fsrmi 3
11-
; CHECK-NEXT: fsrmi 0
1210
; CHECK-NEXT: .LBB0_1: # %loop
1311
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
12+
; CHECK-NEXT: fsrmi 3
1413
; CHECK-NEXT: fadd.d fa5, fa5, fa0
14+
; CHECK-NEXT: fsrmi 0
1515
; CHECK-NEXT: addi a0, a0, -1
1616
; CHECK-NEXT: fadd.d fa5, fa5, fa1
1717
; CHECK-NEXT: beqz a0, .LBB0_1

0 commit comments

Comments
 (0)