[llvm] [llvm][RISCV] Implement Zilsd load/store pair optimization (PR #158640)

Sam Elliott via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 8 00:02:24 PDT 2025


================
@@ -427,10 +428,27 @@ RISCVLoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
 // Post reg-alloc zilsd pass implementation
 //===----------------------------------------------------------------------===//
 
+// Helper function to extract offset from load/store operands
+int64_t RISCVLoadStoreOpt::getLoadStoreOffset(const MachineInstr &MI) {
+  const MachineOperand &OffsetOp = MI.getOperand(2);
+
+  // Handle immediate offset
+  if (OffsetOp.isImm())
+    return OffsetOp.getImm();
+
+  // Handle symbolic operands with MO_LO flag (from MergeBaseOffset)
+  if (OffsetOp.getTargetFlags() & RISCVII::MO_LO)
+    if (OffsetOp.isGlobal() || OffsetOp.isCPI() || OffsetOp.isBlockAddress() ||
+        OffsetOp.isSymbol())
+      return OffsetOp.getOffset();
----------------
lenary wrote:

I am still not sure this is right.

If there is the MI equivalent of `lw a0, %lo(sym+8)(a1)`, I think the `getOffset()` is 8. This doesn't mean that `%lo(sym+4)` is necessarily 8 (it depends on `sym` as well, which only the linker knows). We can't merge `lw a0, %lo(sym+8)(a2)` with `lw a1, 12(a2)` (this may be impossible to get in the compiler, because both should have `%lo` or neither), but maybe we can merge `lw a0, %lo(sym+8)(a2)` with `lw a1, %lo(sym+12)(a2)`.

Maybe what is confusing the situation is that you're representing "couldn't understand an offset" with returning 0, when 0 is a valid (and not unlikely) offset.

I think this function would be clearer with a boolean return value for whether this understood an offset, and an out-parameter of the offset that was found? You might need another out parameter for the thing the offset is relative to in the `%lo` case.

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


More information about the llvm-commits mailing list