[llvm] [RISC-V] Adjust trampoline code for branch control flow protection (PR #141949)

Jesse Huang via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 3 03:18:45 PDT 2025


================
@@ -8310,35 +8324,74 @@ SDValue RISCVTargetLowering::lowerINIT_TRAMPOLINE(SDValue Op,
   };
 
   SDValue OutChains[6];
-
-  uint32_t Encodings[] = {
-      // auipc t2, 0
-      // Loads the current PC into t2.
-      GetEncoding(MCInstBuilder(RISCV::AUIPC).addReg(RISCV::X7).addImm(0)),
-      // ld t0, 24(t2)
-      // Loads the function address into t0. Note that we are using offsets
-      // pc-relative to the first instruction of the trampoline.
-      GetEncoding(
-          MCInstBuilder(RISCV::LD).addReg(RISCV::X5).addReg(RISCV::X7).addImm(
-              FunctionAddressOffset)),
-      // ld t2, 16(t2)
-      // Load the value of the static chain.
-      GetEncoding(
-          MCInstBuilder(RISCV::LD).addReg(RISCV::X7).addReg(RISCV::X7).addImm(
-              StaticChainOffset)),
-      // jalr t0
-      // Jump to the function.
-      GetEncoding(MCInstBuilder(RISCV::JALR)
-                      .addReg(RISCV::X0)
-                      .addReg(RISCV::X5)
-                      .addImm(0))};
+  SDValue OutChainsLPAD[7];
----------------
jaidTw wrote:

Thanks.
The story is I did try it before and got a segfault. At first I guessed it was due to `getNode()` can't handle vector types, but it finally turns out that it was caused by a reference bug.

I changed
```
for (auto [Idx, OffsetValue] : llvm::enumerate(OffsetValues)){
```
to
```
for (auto OffsetValue : OffsetValues) {
```
because we no longer need the index using `SmallVector`, and apprently the later need a reference for changes to the array be effective. I'm now just wondering why the original structure binding form can work without a reference.

Anyway it's working now, so @rofirrim could you please take a look again? Thanks!

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


More information about the llvm-commits mailing list