Skip to content

Conversation

wangleiat
Copy link
Contributor

Add loongarch-enable-merge-offset option to allow disabling the
MergeBaseOffset pass when using optimization.

Created using spr 1.3.5-bogner
@llvmbot
Copy link
Member

llvmbot commented Sep 28, 2025

@llvm/pr-subscribers-backend-loongarch

Author: wanglei (wangleiat)

Changes

Add loongarch-enable-merge-offset option to allow disabling the
MergeBaseOffset pass when using optimization.


Full diff: https://github.com/llvm/llvm-project/pull/161063.diff

2 Files Affected:

  • (modified) llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp (+6-1)
  • (added) llvm/test/CodeGen/LoongArch/merge-offset-option.ll (+24)
diff --git a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
index d0a8ababe8e58..c5e26c106b5df 100644
--- a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
@@ -57,6 +57,11 @@ static cl::opt<bool>
                            cl::desc("Enable the loop data prefetch pass"),
                            cl::init(false));
 
+static cl::opt<bool>
+    EnableMergeBaseOffset("loongarch-enable-merge-offset",
+                          cl::desc("Enable the merge base offset pass"),
+                          cl::init(true), cl::Hidden);
+
 static Reloc::Model getEffectiveRelocModel(const Triple &TT,
                                            std::optional<Reloc::Model> RM) {
   return RM.value_or(Reloc::Static);
@@ -214,7 +219,7 @@ void LoongArchPassConfig::addMachineSSAOptimization() {
 
 void LoongArchPassConfig::addPreRegAlloc() {
   addPass(createLoongArchPreRAExpandPseudoPass());
-  if (TM->getOptLevel() != CodeGenOptLevel::None)
+  if (TM->getOptLevel() != CodeGenOptLevel::None && EnableMergeBaseOffset)
     addPass(createLoongArchMergeBaseOffsetOptPass());
 }
 
diff --git a/llvm/test/CodeGen/LoongArch/merge-offset-option.ll b/llvm/test/CodeGen/LoongArch/merge-offset-option.ll
new file mode 100644
index 0000000000000..e5351a6589cf7
--- /dev/null
+++ b/llvm/test/CodeGen/LoongArch/merge-offset-option.ll
@@ -0,0 +1,24 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc --mtriple=loongarch64 -mattr=+d --relocation-model=static -O1 \
+; RUN:     < %s | FileCheck %s --check-prefix=MERGE
+; RUN: llc --mtriple=loongarch64 -mattr=+d --relocation-model=static -O1 \
+; RUN:     --loongarch-enable-merge-offset=false < %s | FileCheck %s --check-prefix=NO_MERGE
+
+@g = dso_local global i32 zeroinitializer, align 4
+
+define void @foo() nounwind {
+; MERGE-LABEL: foo:
+; MERGE:       # %bb.0:
+; MERGE-NEXT:    pcalau12i $a0, %pc_hi20(g)
+; MERGE-NEXT:    ld.w $zero, $a0, %pc_lo12(g)
+; MERGE-NEXT:    ret
+;
+; NO_MERGE-LABEL: foo:
+; NO_MERGE:       # %bb.0:
+; NO_MERGE-NEXT:    pcalau12i $a0, %pc_hi20(g)
+; NO_MERGE-NEXT:    addi.d $a0, $a0, %pc_lo12(g)
+; NO_MERGE-NEXT:    ld.w $zero, $a0, 0
+; NO_MERGE-NEXT:    ret
+  %v = load volatile i32, ptr @g
+  ret void
+}

Copy link
Member

@heiher heiher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@wangleiat wangleiat merged commit cac0635 into main Sep 29, 2025
11 checks passed
@wangleiat wangleiat deleted the users/wangleiat/spr/loongarch-add-option-for-merge-base-offset-pass-3 branch September 29, 2025 01:02
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Sep 29, 2025
Add `loongarch-enable-merge-offset` option to allow disabling the
`MergeBaseOffset` pass when using optimization.

Reviewers: SixWeining, heiher

Reviewed By: SixWeining, heiher

Pull Request: llvm/llvm-project#161063
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
Add `loongarch-enable-merge-offset` option to allow disabling the
`MergeBaseOffset` pass when using optimization.

Reviewers: SixWeining, heiher

Reviewed By: SixWeining, heiher

Pull Request: llvm#161063
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants