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

Roger Ferrer Ibáñez via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 2 05:03:59 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];
----------------
rofirrim wrote:

A simplification suggestion for these two arrays:

I used a static array originally but maybe we can move to a single `SmallVector` now? Something like:

```
SmallVector<SDValue, 7> OutChains;
```

Then remove `OutChainsUsed` and in the loops below, rather than doing (like I originally did)

```
OutChainsUsed[Idx] = ...
```

you could do

```
OutChains.push_back(...)
```

and right before the final creation of the `ISD::TokenFactor`, `assert` that the size of the `OutChains` is 6 or 7 based on `HasCFBranch`.

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


More information about the llvm-commits mailing list