[llvm] [GlobalMerge]Prefer use global-merge-max-offset instead of the target-specific constant offset. (PR #165591)

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 29 09:23:27 PDT 2025


https://github.com/hstk30-hw created https://github.com/llvm/llvm-project/pull/165591

In the Dhrystone benchmark, I find some adjacent global not be merged, on the contrary the GCC's anchor optimize is work. Use global-merge-max-offset to set the max offset can yield similar results (still slightly different, at least we can control the offset).

>From dfe52c56e3a45698570307a4e6876b562944dabe Mon Sep 17 00:00:00 2001
From: hstk30-hw <hanwei62 at huawei.com>
Date: Wed, 29 Oct 2025 23:57:58 +0800
Subject: [PATCH] Prefer use global-merge-max-offset instead of the
 target-specific constant offset.

In the Dhrystone benchmark, I find some adjacent global not be merged, on
the contrary the GCC's anchor optimize is work. Use
global-merge-max-offset to set the max offset can yield similar results
(still slightly different)
---
 llvm/lib/CodeGen/GlobalMerge.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/GlobalMerge.cpp b/llvm/lib/CodeGen/GlobalMerge.cpp
index e58d7e344c28b..3683cbe596eda 100644
--- a/llvm/lib/CodeGen/GlobalMerge.cpp
+++ b/llvm/lib/CodeGen/GlobalMerge.cpp
@@ -772,6 +772,7 @@ Pass *llvm::createGlobalMergePass(const TargetMachine *TM, unsigned Offset,
   bool MergeConstAggressive = GlobalMergeAllConst.getNumOccurrences() > 0
                                   ? GlobalMergeAllConst
                                   : MergeConstAggressiveByDefault;
-  return new GlobalMerge(TM, Offset, OnlyOptimizeForSize, MergeExternal,
+  unsigned PreferOffset = GlobalMergeMaxOffset ? GlobalMergeMaxOffset : Offset;
+  return new GlobalMerge(TM, PerferOffset, OnlyOptimizeForSize, MergeExternal,
                          MergeConstant, MergeConstAggressive);
 }



More information about the llvm-commits mailing list