Skip to content

Conversation

@mgudim
Copy link
Contributor

@mgudim mgudim commented Oct 21, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2025

@llvm/pr-subscribers-backend-risc-v

Author: Mikhail Gudim (mgudim)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/164477.diff

3 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVFrameLowering.cpp (+9)
  • (modified) llvm/lib/Target/RISCV/RISCVFrameLowering.h (+3)
  • (modified) llvm/lib/Target/RISCV/RISCVTargetMachine.cpp (+9-1)
diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
index b37b7405a660f..19524d016c06f 100644
--- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
@@ -2507,3 +2507,12 @@ void RISCVFrameLowering::inlineStackProbe(MachineFunction &MF,
     }
   }
 }
+
+int RISCVFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const {
+  return 0;
+}
+
+Register
+RISCVFrameLowering::getInitialCFARegister(const MachineFunction &MF) const {
+  return RISCV::X2;
+}
diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.h b/llvm/lib/Target/RISCV/RISCVFrameLowering.h
index 6af63a4885f35..87980dfb09f96 100644
--- a/llvm/lib/Target/RISCV/RISCVFrameLowering.h
+++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.h
@@ -23,6 +23,9 @@ class RISCVFrameLowering : public TargetFrameLowering {
 public:
   explicit RISCVFrameLowering(const RISCVSubtarget &STI);
 
+  int getInitialCFAOffset(const MachineFunction &MF) const override;
+  Register getInitialCFARegister(const MachineFunction &MF) const override;
+
   void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
   void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
 
diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index f81b1e1260ee3..077dbcc7d9003 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -103,6 +103,11 @@ static cl::opt<bool>
                            cl::desc("Enable Machine Pipeliner for RISC-V"),
                            cl::init(false), cl::Hidden);
 
+static cl::opt<bool> EnableCFIInstrInserter(
+    "riscv-enable-cfi-instr-inserter",
+    cl::desc("Enable CFI Instruction Inserter for RISC-V"), cl::init(false),
+    cl::Hidden);
+
 extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
   RegisterTargetMachine<RISCVTargetMachine> X(getTheRISCV32Target());
   RegisterTargetMachine<RISCVTargetMachine> Y(getTheRISCV64Target());
@@ -169,7 +174,7 @@ RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
   if (TT.isOSFuchsia() && !TT.isArch64Bit())
     report_fatal_error("Fuchsia is only supported for 64-bit");
 
-  setCFIFixup(true);
+  setCFIFixup(!EnableCFIInstrInserter);
 }
 
 const RISCVSubtarget *
@@ -576,6 +581,9 @@ void RISCVPassConfig::addPreEmitPass2() {
   addPass(createUnpackMachineBundles([&](const MachineFunction &MF) {
     return MF.getFunction().getParent()->getModuleFlag("kcfi");
   }));
+
+  if (EnableCFIInstrInserter)
+    addPass(createCFIInstrInserter());
 }
 
 void RISCVPassConfig::addMachineSSAOptimization() {

@mgudim
Copy link
Contributor Author

mgudim commented Oct 21, 2025

this needs to be merged before

#164480

and

#164479

@mgudim mgudim force-pushed the enable_cfi_option branch from aad0c85 to bdd6628 Compare October 21, 2025 21:02
@mgudim mgudim changed the title Add an option to enable CFIInstrInserter. [RISCV] Add an option to enable CFIInstrInserter. Oct 21, 2025
@mgudim
Copy link
Contributor Author

mgudim commented Nov 6, 2025

ping

Copy link
Member

@lenary lenary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some tests showing what this ends up doing.

@ppenzin
Copy link
Contributor

ppenzin commented Nov 14, 2025

There was a test added in #164479, but there's no underlying support to enable multiple save locations yet and it is marked as XFAIL.

To illustrate what a follow up to this might look like, I've taken the next couple of commits and set up a draft PR #168159

@mgudim
Copy link
Contributor Author

mgudim commented Nov 15, 2025

@lenary @ppenzin I added a test

@ppenzin
Copy link
Contributor

ppenzin commented Nov 17, 2025

LGTM

@lenary
Copy link
Member

lenary commented Nov 17, 2025

I'm not actually sure the value of that test. What I'm trying to get an answer to is "why is this pass being added?", but I also haven't had the time to go and review what CFIInserter does and why - I know it's (by LLVM's standards) a fairly recent addition. (Your test shows CFI Inserter is being added, but not what effect that has, just the debug output from it being added)

If there are no (CodeGen) changes to existing tests, then I guess there's no problem and we can proceed, especially if it unblocks things we know are improvements.

@lenary
Copy link
Member

lenary commented Nov 17, 2025

I know it's (by LLVM's standards) a fairly recent addition

This is wrong. CFI Inserter is a lot older than I thought it was. I was confusing it with CFI Fixup, which is only 4 years old.

Copy link
Member

@lenary lenary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Sorry for taking a while to get to that decision.

@mgudim
Copy link
Contributor Author

mgudim commented Nov 18, 2025

@lenary

but I also haven't had the time to go and review what CFIInserter does and why

basically CFIInserter is needed because after code layout is finalized, it turns out that we may need more CFI instructions. For example:

%entry:
sd x18, x2, 2
.cfi_offset x18, 2
...
j %bb3

%bb2:
...
ret

%bb3:
sd x18, x2, 4
.cfi_offset x18, 4
j %bb2

The first .cfi_offset x18, 2 says that x18 is saved at offset 2 from cfa and this applies untill the next cfi instruction. In particular, it applies to the entry to %bb2. But this is wrong! The only way to get to %bb2 is from %bb3 where the cfi_offset x18, 4 applies.

What CFIInserter does is it compares CFI from layout predecessor to CFG predecessors and inserters more cfis if needed. In this case the result would look like this:

%entry:
sd x18, x2, 2
.cfi_offset x18, 2
...
j %bb3

%bb2:
.cfi_offset x18, 4 # added by CFIInserter
...
ret

%bb3:
sd x18, x2, 4
.cfi_offset x18, 4
j %bb2

It turns out that current CFIInserter implementation does not handle some cases. I created test cases for those in separate MRs (#164477 (comment)) Also I have fixes for these . So these are useful even without shrink wrapping changes which I want to make.

@mgudim mgudim merged commit 6886d49 into llvm:main Nov 18, 2025
10 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 18, 2025

LLVM Buildbot has detected a new failure on builder fuchsia-x86_64-linux running on fuchsia-debian-64-us-central1-a-1 while building llvm at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/11/builds/28266

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/fuchsia-linux.py ...' (failure)
...
  Expectedly Failed:    24 (0.05%)
[1463/1465] Linking CXX executable unittests/tools/llvm-exegesis/LLVMExegesisTests
[1464/1465] Running the LLVM regression tests
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:534: note: using ld.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/ld.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:534: note: using lld-link: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/lld-link
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:534: note: using ld64.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/ld64.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:534: note: using wasm-ld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/wasm-ld
-- Testing: 62193 tests, 60 workers --
Testing:  0.. 10.. 20.. 30
XPASS: LLVM :: CodeGen/RISCV/cfi-multiple-locations.mir (20158 of 62193)
******************** TEST 'LLVM :: CodeGen/RISCV/cfi-multiple-locations.mir' FAILED ********************
Exit Code: 0

Command Output (stdout):
--
# RUN: at line 1
/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/llc /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/RISCV/cfi-multiple-locations.mir -mtriple=riscv64  -run-pass=cfi-instr-inserter  -riscv-enable-cfi-instr-inserter=true
# executed command: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/llc /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/RISCV/cfi-multiple-locations.mir -mtriple=riscv64 -run-pass=cfi-instr-inserter -riscv-enable-cfi-instr-inserter=true

--

********************
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. 
********************
Unexpectedly Passed Tests (1):
  LLVM :: CodeGen/RISCV/cfi-multiple-locations.mir


Testing Time: 86.09s

Total Discovered Tests: 70355
  Skipped            :    48 (0.07%)
  Unsupported        : 19487 (27.70%)
  Passed             : 50748 (72.13%)
  Expectedly Failed  :    71 (0.10%)
  Unexpectedly Passed:     1 (0.00%)
FAILED: test/CMakeFiles/check-llvm /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/test/CMakeFiles/check-llvm 
cd /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/test && /usr/bin/python3.10 /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/./bin/llvm-lit -sv /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/test
ninja: build stopped: subcommand failed.
['ninja', '-C', '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h', 'check-llvm', 'check-clang', 'check-lld'] exited with return code 1.
@@@STEP_FAILURE@@@
Step 7 (check) failure: check (failure)
...
  Expectedly Failed:    24 (0.05%)
[1463/1465] Linking CXX executable unittests/tools/llvm-exegesis/LLVMExegesisTests
[1464/1465] Running the LLVM regression tests
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:534: note: using ld.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/ld.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:534: note: using lld-link: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/lld-link
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:534: note: using ld64.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/ld64.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:534: note: using wasm-ld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/wasm-ld
-- Testing: 62193 tests, 60 workers --
Testing:  0.. 10.. 20.. 30
XPASS: LLVM :: CodeGen/RISCV/cfi-multiple-locations.mir (20158 of 62193)
******************** TEST 'LLVM :: CodeGen/RISCV/cfi-multiple-locations.mir' FAILED ********************
Exit Code: 0

Command Output (stdout):
--
# RUN: at line 1
/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/llc /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/RISCV/cfi-multiple-locations.mir -mtriple=riscv64  -run-pass=cfi-instr-inserter  -riscv-enable-cfi-instr-inserter=true
# executed command: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/bin/llc /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/RISCV/cfi-multiple-locations.mir -mtriple=riscv64 -run-pass=cfi-instr-inserter -riscv-enable-cfi-instr-inserter=true

--

********************
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. 
********************
Unexpectedly Passed Tests (1):
  LLVM :: CodeGen/RISCV/cfi-multiple-locations.mir


Testing Time: 86.09s

Total Discovered Tests: 70355
  Skipped            :    48 (0.07%)
  Unsupported        : 19487 (27.70%)
  Passed             : 50748 (72.13%)
  Expectedly Failed  :    71 (0.10%)
  Unexpectedly Passed:     1 (0.00%)
FAILED: test/CMakeFiles/check-llvm /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/test/CMakeFiles/check-llvm 
cd /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/test && /usr/bin/python3.10 /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/./bin/llvm-lit -sv /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h/test
ninja: build stopped: subcommand failed.
['ninja', '-C', '/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-e91spn5h', 'check-llvm', 'check-clang', 'check-lld'] exited with return code 1.
program finished with exit code 0
elapsedTime=1353.456542

@Prabhuk
Copy link
Contributor

Prabhuk commented Nov 18, 2025

@mgudim -- we are seeing the test failures on build bot CodeGen/RISCV/cfi-multiple-locations.mir in our toolchain builders at Google as well https://luci-milo.appspot.com/ui/p/fuchsia/builders/prod/clang-linux-arm64/b8697867649418140369/overview

Can you please fix or revert?

@mgudim
Copy link
Contributor Author

mgudim commented Nov 18, 2025

@mgudim -- we are seeing the test failures on build bot CodeGen/RISCV/cfi-multiple-locations.mir in our toolchain builders at Google as well https://luci-milo.appspot.com/ui/p/fuchsia/builders/prod/clang-linux-arm64/b8697867649418140369/overview

Can you please fix or revert?

@Prabhuk sorry about breaking the build. Here's a fix: #168525 . If you review it, I'll merge it. Or you can merge it too

aadeshps-mcw pushed a commit to aadeshps-mcw/llvm-project that referenced this pull request Nov 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants