[llvm] [RISCV] Add an option to enable CFIInstrInserter. (PR #164477)

Mikhail Gudim via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 17 23:46:14 PST 2025


mgudim wrote:

@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 (https://github.com/llvm/llvm-project/pull/164477#issuecomment-3429173460) Also I have fixes for these . So these are useful even without shrink wrapping changes which I want to make.

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


More information about the llvm-commits mailing list