[llvm] [LoongArch] Add option to disable scheduling of instructions with target flags (PR #178639)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 29 03:35:15 PST 2026


https://github.com/wangleiat created https://github.com/llvm/llvm-project/pull/178639

This patch adds a hidden command-line option
-loongarch-disable-reloc-sched. When enabled, isSafeToMove returns
false for instructions that have operands with target flags.

This effectively prevents code motion for instructions involved in
relocations, which is useful for debugging code generation issues
related to relocation sequences or scheduling boundaries.


>From 94aea768e35639cdd8db8cf4416c6ad16cdbafb0 Mon Sep 17 00:00:00 2001
From: wanglei <wanglei at loongson.cn>
Date: Thu, 29 Jan 2026 19:30:33 +0800
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.7
---
 .../Target/LoongArch/LoongArchInstrInfo.cpp   | 12 +++++
 .../CodeGen/LoongArch/disable-reloc-sched.ll  | 48 +++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 llvm/test/CodeGen/LoongArch/disable-reloc-sched.ll

diff --git a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp
index 29cd837dcbe06..2b163d2d62d3a 100644
--- a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp
@@ -20,9 +20,15 @@
 #include "llvm/CodeGen/StackMaps.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCInstBuilder.h"
+#include "llvm/Support/CommandLine.h"
 
 using namespace llvm;
 
+static cl::opt<bool> DisableRelocSched(
+    "loongarch-disable-reloc-sched",
+    cl::desc("Disable scheduling of instructions with target flags"),
+    cl::init(false), cl::Hidden);
+
 #define GET_INSTRINFO_CTOR_DTOR
 #include "LoongArchGenInstrInfo.inc"
 
@@ -382,6 +388,12 @@ bool LoongArchInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
 bool LoongArchInstrInfo::isSafeToMove(const MachineInstr &MI,
                                       const MachineBasicBlock *MBB,
                                       const MachineFunction &MF) const {
+  if (DisableRelocSched) {
+    for (const MachineOperand &MO : MI.operands())
+      if (MO.getTargetFlags())
+        return false;
+  }
+
   auto MII = MI.getIterator();
   auto MIE = MBB->end();
 
diff --git a/llvm/test/CodeGen/LoongArch/disable-reloc-sched.ll b/llvm/test/CodeGen/LoongArch/disable-reloc-sched.ll
new file mode 100644
index 0000000000000..0d23650a9e3c1
--- /dev/null
+++ b/llvm/test/CodeGen/LoongArch/disable-reloc-sched.ll
@@ -0,0 +1,48 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc --mtriple=loongarch64 -O3 < %s | FileCheck %s --check-prefix=DEFAULT
+; RUN: llc --mtriple=loongarch64 -O3 -loongarch-disable-reloc-sched < %s | FileCheck %s --check-prefix=NOSCHED
+
+ at g = external global i32
+
+define i32 @boundary_test(i32 %a, i32 %b) {
+; DEFAULT-LABEL: boundary_test:
+; DEFAULT:       # %bb.0: # %entry
+; DEFAULT-NEXT:    pcalau12i $a2, %got_pc_hi20(g)
+; DEFAULT-NEXT:    ld.d $a2, $a2, %got_pc_lo12(g)
+; DEFAULT-NEXT:    mul.d $a3, $a0, $a1
+; DEFAULT-NEXT:    ld.w $a2, $a2, 0
+; DEFAULT-NEXT:    addi.w $a1, $a1, 0
+; DEFAULT-NEXT:    addi.w $a0, $a0, 0
+; DEFAULT-NEXT:    div.w $a0, $a0, $a1
+; DEFAULT-NEXT:    add.d $a0, $a3, $a0
+; DEFAULT-NEXT:    add.w $a0, $a0, $a2
+; DEFAULT-NEXT:    ret
+;
+; NOSCHED-LABEL: boundary_test:
+; NOSCHED:       # %bb.0: # %entry
+; NOSCHED-NEXT:    mul.d $a2, $a0, $a1
+; NOSCHED-NEXT:    pcalau12i $a3, %got_pc_hi20(g)
+; NOSCHED-NEXT:    ld.d $a3, $a3, %got_pc_lo12(g)
+; NOSCHED-NEXT:    ld.w $a3, $a3, 0
+; NOSCHED-NEXT:    addi.w $a1, $a1, 0
+; NOSCHED-NEXT:    addi.w $a0, $a0, 0
+; NOSCHED-NEXT:    div.w $a0, $a0, $a1
+; NOSCHED-NEXT:    add.d $a0, $a2, $a0
+; NOSCHED-NEXT:    add.w $a0, $a0, $a3
+; NOSCHED-NEXT:    ret
+
+
+entry:
+  ; Mul is fast, but without scheduling boundary it might be scheduled after the long latency load address setup
+  %x = mul i32 %a, %b
+
+  ; Load global (pcalau12i) - this has target flags so it becomes a boundary with -loongarch-disable-reloc-sched
+  %v = load i32, ptr @g
+
+  ; Div is slow
+  %y = sdiv i32 %a, %b
+
+  %s1 = add i32 %x, %y
+  %s2 = add i32 %s1, %v
+  ret i32 %s2
+}



More information about the llvm-commits mailing list