[llvm] [RISCV] Ensure LPAD alignment for calls to returns_twice functions (PR #177515)

Sam Elliott via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 22 22:49:01 PST 2026


================
@@ -273,6 +275,50 @@ bool RISCVAsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst,
 // instructions) auto-generated.
 #include "RISCVGenMCPseudoLowering.inc"
 
+// Emit a call to a returns_twice function with LPAD alignment handling.
+// When Zca is enabled, emit .p2align 2 (and .option exact if relaxation
+// is enabled) to prevent LPAD misalignment from c.jal/cm.jalt relaxation.
+void RISCVAsmPrinter::emitLpadAlignedCall(const MachineInstr &MI,
+                                          bool IsIndirect) {
+  const MCSubtargetInfo &MCSTI = getSubtargetInfo();
+  bool HasZca = MCSTI.hasFeature(RISCV::FeatureStdExtZca);
+  bool HasRelax = MCSTI.hasFeature(RISCV::FeatureRelax);
+
+  if (HasZca)
+    OutStreamer->emitCodeAlignment(Align(4), &MCSTI);
+
+  auto *RTS =
+      static_cast<RISCVTargetStreamer *>(OutStreamer->getTargetStreamer());
+  if (HasZca && HasRelax)
+    RTS->emitDirectiveOptionExact();
----------------
lenary wrote:

I think we can do it without a new relocation. Comment incoming.

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


More information about the llvm-commits mailing list