Skip to content

Commit 6886d49

Browse files
authored
[RISCV] Add an option to enable CFIInstrInserter. (#164477)
1 parent f15b756 commit 6886d49

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,3 +2507,12 @@ void RISCVFrameLowering::inlineStackProbe(MachineFunction &MF,
25072507
}
25082508
}
25092509
}
2510+
2511+
int RISCVFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const {
2512+
return 0;
2513+
}
2514+
2515+
Register
2516+
RISCVFrameLowering::getInitialCFARegister(const MachineFunction &MF) const {
2517+
return RISCV::X2;
2518+
}

llvm/lib/Target/RISCV/RISCVFrameLowering.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class RISCVFrameLowering : public TargetFrameLowering {
2323
public:
2424
explicit RISCVFrameLowering(const RISCVSubtarget &STI);
2525

26+
int getInitialCFAOffset(const MachineFunction &MF) const override;
27+
Register getInitialCFARegister(const MachineFunction &MF) const override;
28+
2629
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
2730
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
2831

llvm/lib/Target/RISCV/RISCVTargetMachine.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ static cl::opt<bool>
103103
cl::desc("Enable Machine Pipeliner for RISC-V"),
104104
cl::init(false), cl::Hidden);
105105

106+
static cl::opt<bool> EnableCFIInstrInserter(
107+
"riscv-enable-cfi-instr-inserter",
108+
cl::desc("Enable CFI Instruction Inserter for RISC-V"), cl::init(false),
109+
cl::Hidden);
110+
106111
extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
107112
RegisterTargetMachine<RISCVTargetMachine> X(getTheRISCV32Target());
108113
RegisterTargetMachine<RISCVTargetMachine> Y(getTheRISCV64Target());
@@ -169,7 +174,7 @@ RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
169174
if (TT.isOSFuchsia() && !TT.isArch64Bit())
170175
report_fatal_error("Fuchsia is only supported for 64-bit");
171176

172-
setCFIFixup(true);
177+
setCFIFixup(!EnableCFIInstrInserter);
173178
}
174179

175180
const RISCVSubtarget *
@@ -578,6 +583,9 @@ void RISCVPassConfig::addPreEmitPass2() {
578583
addPass(createUnpackMachineBundles([&](const MachineFunction &MF) {
579584
return MF.getFunction().getParent()->getModuleFlag("kcfi");
580585
}));
586+
587+
if (EnableCFIInstrInserter)
588+
addPass(createCFIInstrInserter());
581589
}
582590

583591
void RISCVPassConfig::addMachineSSAOptimization() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; RUN: llc -mtriple=riscv64 -O3 \
2+
; RUN: -debug-pass=Structure < %s -o /dev/null 2>&1 | \
3+
; RUN: FileCheck %s --check-prefix=O3-WITHOUT-ENABLE-CFI-INSTR-INSERTER
4+
5+
; RUN: llc -mtriple=riscv64 -O3 \
6+
; RUN: --riscv-enable-cfi-instr-inserter=true \
7+
; RUN: -debug-pass=Structure < %s -o /dev/null 2>&1 | \
8+
; RUN: FileCheck %s --check-prefix=O3-ENABLE-CFI-INSTR-INSERTER
9+
10+
; RUN: llc -mtriple=riscv64 -O0 \
11+
; RUN: -debug-pass=Structure < %s -o /dev/null 2>&1 | \
12+
; RUN: FileCheck %s --check-prefix=O0-WITHOUT-ENABLE-CFI-INSTR-INSERTER
13+
14+
; RUN: llc -mtriple=riscv64 -O0 \
15+
; RUN: --riscv-enable-cfi-instr-inserter=true \
16+
; RUN: -debug-pass=Structure < %s -o /dev/null 2>&1 | \
17+
; RUN: FileCheck %s --check-prefix=O0-ENABLE-CFI-INSTR-INSERTER
18+
19+
; REQUIRES: asserts
20+
21+
; O3-WITHOUT-ENABLE-CFI-INSTR-INSERTER-LABEL: Pass Arguments:
22+
; NO-O3-WITHOUT-ENABLE-CFI-INSTR-INSERTER: Check CFA info and insert CFI instructions if needed
23+
; O3-WITHOUT-ENABLE-CFI-INSTR-INSERTER: Insert CFI remember/restore state instructions
24+
25+
; O3-ENABLE-CFI-INSTR-INSERTER-LABEL: Pass Arguments:
26+
; O3-ENABLE-CFI-INSTR-INSERTER: Check CFA info and insert CFI instructions if needed
27+
; NO-O3-ENABLE-CFI-INSTR-INSERTER: Insert CFI remember/restore state instructions
28+
29+
; O0-WITHOUT-ENABLE-CFI-INSTR-INSERTER-LABEL: Pass Arguments:
30+
; NO-O0-WITHOUT-ENABLE-CFI-INSTR-INSERTER: Check CFA info and insert CFI instructions if needed
31+
; O0-WITHOUT-ENABLE-CFI-INSTR-INSERTER: Insert CFI remember/restore state instructions
32+
33+
; O0-ENABLE-CFI-INSTR-INSERTER-LABEL: Pass Arguments:
34+
; O0-ENABLE-CFI-INSTR-INSERTER: Check CFA info and insert CFI instructions if needed
35+
; NO-O0-ENABLE-CFI-INSTR-INSERTER: Insert CFI remember/restore state instructions

0 commit comments

Comments
 (0)