[llvm] ae4fc80 - [RISCV] When using global merging, don't enable merging of external globals by default (#115484)

via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 9 02:02:53 PST 2024


Author: Alex Bradbury
Date: 2024-11-09T10:02:50Z
New Revision: ae4fc80574cfbbf2b2b53f2728cd785db76e9e69

URL: https://github.com/llvm/llvm-project/commit/ae4fc80574cfbbf2b2b53f2728cd785db76e9e69
DIFF: https://github.com/llvm/llvm-project/commit/ae4fc80574cfbbf2b2b53f2728cd785db76e9e69.diff

LOG: [RISCV] When using global merging, don't enable merging of external globals by default (#115484)

AArch64 left this disabled after seeing some cases of slightly worse
codegen that weren't tracked down, so I suggest as a path to
incrementally moving towards enable globals merging we follow suit, and
evaluate turning on later.

This patch disables merging of external globals, but also adds a flag to
override that. This reduces churn in test cases, simplifies benchmarking
runs, and this flag can be removed later.

A follow-on PR enables the globals merging pass by default (and as it's
based on this commit, merging of external globals is disabled just as
they are for AArch64).

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
    llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-nonzero.ll
    llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-zero.ll
    llvm/test/CodeGen/RISCV/global-merge-minsize.ll
    llvm/test/CodeGen/RISCV/global-merge-offset.ll
    llvm/test/CodeGen/RISCV/global-merge.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index a5afbcfd79710f..6a97755c279a29 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -53,6 +53,13 @@ static cl::opt<cl::boolOrDefault>
     EnableGlobalMerge("riscv-enable-global-merge", cl::Hidden,
                       cl::desc("Enable the global merge pass"));
 
+static cl::opt<bool> ForceEnableGlobalMergeExternalGlobals(
+    "riscv-force-enable-global-merge-external-globals", cl::Hidden,
+    cl::init(false),
+    cl::desc(
+        "If the global merge pass is enabled, force enable global merging of "
+        "external globals (overriding any logic that might disable it)"));
+
 static cl::opt<bool>
     EnableMachineCombiner("riscv-enable-machine-combiner",
                           cl::desc("Enable the machine combiner pass"),
@@ -472,7 +479,8 @@ bool RISCVPassConfig::addPreISel() {
   if (EnableGlobalMerge == cl::BOU_TRUE) {
     addPass(createGlobalMergePass(TM, /* MaxOffset */ 2047,
                                   /* OnlyOptimizeForSize */ false,
-                                  /* MergeExternalByDefault */ true));
+                                  /* MergeExternalByDefault */
+                                  ForceEnableGlobalMergeExternalGlobals));
   }
 
   return false;

diff  --git a/llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-nonzero.ll b/llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-nonzero.ll
index c5471389302124..39c677ac20b3a5 100644
--- a/llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-nonzero.ll
+++ b/llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-nonzero.ll
@@ -1,8 +1,9 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc -mtriple=riscv64 -riscv-enable-global-merge -verify-machineinstrs < %s \
-; RUN:   | FileCheck %s -check-prefix=SMALL-DATA
+; RUN: llc -mtriple=riscv64 -riscv-enable-global-merge -riscv-force-enable-global-merge-external-globals \
+; RUN:   -verify-machineinstrs < %s | FileCheck %s -check-prefix=SMALL-DATA
 ; RUN: llc -mtriple=riscv64 -riscv-enable-global-merge -global-merge-min-data-size=0 \
-; RUN:    -verify-machineinstrs < %s | FileCheck %s -check-prefix=MINSIZE
+; RUN:   -riscv-force-enable-global-merge-external-globals -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s -check-prefix=MINSIZE
 
 @ig1 = internal global i32 0, align 4
 @ig2 = internal global i32 0, align 4

diff  --git a/llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-zero.ll b/llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-zero.ll
index 8e4d72af00ebce..d2b714577db9a8 100644
--- a/llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-zero.ll
+++ b/llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-zero.ll
@@ -1,8 +1,9 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc -mtriple=riscv32 -riscv-enable-global-merge -verify-machineinstrs < %s \
-; RUN:   | FileCheck %s -check-prefix=SMALL-DATA
+; RUN: llc -mtriple=riscv32 -riscv-enable-global-merge -riscv-force-enable-global-merge-external-globals \
+; RUN:   -verify-machineinstrs < %s | FileCheck %s -check-prefix=SMALL-DATA
 ; RUN: llc -mtriple=riscv64 -riscv-enable-global-merge -global-merge-min-data-size=5 \
-; RUN:    -verify-machineinstrs < %s | FileCheck %s -check-prefix=MINSIZE
+; RUN:   -riscv-force-enable-global-merge-external-globals  -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s -check-prefix=MINSIZE
 
 @ig1 = internal global i32 0, align 4
 @ig2 = internal global i32 0, align 4

diff  --git a/llvm/test/CodeGen/RISCV/global-merge-minsize.ll b/llvm/test/CodeGen/RISCV/global-merge-minsize.ll
index e405425832acbb..696d163bdcb2c4 100644
--- a/llvm/test/CodeGen/RISCV/global-merge-minsize.ll
+++ b/llvm/test/CodeGen/RISCV/global-merge-minsize.ll
@@ -1,8 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc -mtriple=riscv32 -riscv-enable-global-merge -verify-machineinstrs < %s \
-; RUN:   | FileCheck %s -check-prefix=RV32
+; RUN: llc -mtriple=riscv32 -riscv-enable-global-merge -riscv-force-enable-global-merge-external-globals \
+; RUN:   -verify-machineinstrs < %s | FileCheck %s -check-prefix=RV32
 ; RUN: llc -mtriple=riscv32 -riscv-enable-global-merge -global-merge-min-data-size=5 \
-; RUN:   -verify-machineinstrs < %s | FileCheck %s -check-prefix=RV32-MINSIZE
+; RUN:   -riscv-force-enable-global-merge-external-globals -verify-machineinstrs < %s | FileCheck %s -check-prefix=RV32-MINSIZE
 
 @ig1 = internal global i32 0, align 4
 @ig2 = internal global i32 0, align 4

diff  --git a/llvm/test/CodeGen/RISCV/global-merge-offset.ll b/llvm/test/CodeGen/RISCV/global-merge-offset.ll
index 13afcba181719e..0c0881ddf28737 100644
--- a/llvm/test/CodeGen/RISCV/global-merge-offset.ll
+++ b/llvm/test/CodeGen/RISCV/global-merge-offset.ll
@@ -1,12 +1,12 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
 ; RUN: sed 's/ArrSize/100/g' %s | llc -mtriple=riscv32 -riscv-enable-global-merge \
-; RUN:   -verify-machineinstrs | FileCheck %s
+; RUN:   -riscv-force-enable-global-merge-external-globals -verify-machineinstrs | FileCheck %s
 ; RUN: sed 's/ArrSize/100/g' %s | llc -mtriple=riscv64 -riscv-enable-global-merge \
-; RUN:   -verify-machineinstrs | FileCheck %s
+; RUN:   -riscv-force-enable-global-merge-external-globals -verify-machineinstrs | FileCheck %s
 ; RUN: sed 's/ArrSize/101/g' %s | llc -mtriple=riscv32 -riscv-enable-global-merge \
-; RUN:   -verify-machineinstrs | FileCheck %s -check-prefix=CHECK-TOOBIG
+; RUN:   -riscv-force-enable-global-merge-external-globals -verify-machineinstrs | FileCheck %s -check-prefix=CHECK-TOOBIG
 ; RUN: sed 's/ArrSize/101/g' %s | llc -mtriple=riscv64 -riscv-enable-global-merge \
-; RUN:   -verify-machineinstrs | FileCheck %s -check-prefix=CHECK-TOOBIG
+; RUN:   -riscv-force-enable-global-merge-external-globals -verify-machineinstrs | FileCheck %s -check-prefix=CHECK-TOOBIG
 
 ; This test demonstrates that the MaxOffset is set correctly for RISC-V by
 ; constructing an input that is at the limit and comparing.

diff  --git a/llvm/test/CodeGen/RISCV/global-merge.ll b/llvm/test/CodeGen/RISCV/global-merge.ll
index 20379ee2e7dacd..633ba719c6a305 100644
--- a/llvm/test/CodeGen/RISCV/global-merge.ll
+++ b/llvm/test/CodeGen/RISCV/global-merge.ll
@@ -3,6 +3,12 @@
 ; RUN:   | FileCheck %s
 ; RUN: llc -mtriple=riscv64 -riscv-enable-global-merge -verify-machineinstrs < %s \
 ; RUN:   | FileCheck %s
+; RUN: llc -mtriple=riscv32 -riscv-enable-global-merge \
+; RUN:     -riscv-force-enable-global-merge-external-globals -verify-machineinstrs < %s \
+; RUN:   | FileCheck -check-prefix=CHECK-WEXTERN %s
+; RUN: llc -mtriple=riscv64 -riscv-enable-global-merge \
+; RUN:     -riscv-force-enable-global-merge-external-globals -verify-machineinstrs < %s \
+; RUN:   | FileCheck -check-prefix=CHECK-WEXTERN %s
 
 @ig1 = internal global i32 0, align 4
 @ig2 = internal global i32 0, align 4
@@ -21,9 +27,21 @@ define void @f1(i32 %a) nounwind {
 ; CHECK-NEXT:    sw a0, %lo(.L_MergedGlobals)(a1)
 ; CHECK-NEXT:    addi a1, a1, %lo(.L_MergedGlobals)
 ; CHECK-NEXT:    sw a0, 4(a1)
-; CHECK-NEXT:    sw a0, 8(a1)
-; CHECK-NEXT:    sw a0, 12(a1)
+; CHECK-NEXT:    lui a1, %hi(eg1)
+; CHECK-NEXT:    sw a0, %lo(eg1)(a1)
+; CHECK-NEXT:    lui a1, %hi(eg2)
+; CHECK-NEXT:    sw a0, %lo(eg2)(a1)
 ; CHECK-NEXT:    ret
+;
+; CHECK-WEXTERN-LABEL: f1:
+; CHECK-WEXTERN:       # %bb.0:
+; CHECK-WEXTERN-NEXT:    lui a1, %hi(.L_MergedGlobals)
+; CHECK-WEXTERN-NEXT:    sw a0, %lo(.L_MergedGlobals)(a1)
+; CHECK-WEXTERN-NEXT:    addi a1, a1, %lo(.L_MergedGlobals)
+; CHECK-WEXTERN-NEXT:    sw a0, 4(a1)
+; CHECK-WEXTERN-NEXT:    sw a0, 8(a1)
+; CHECK-WEXTERN-NEXT:    sw a0, 12(a1)
+; CHECK-WEXTERN-NEXT:    ret
   store i32 %a, ptr @ig1, align 4
   store i32 %a, ptr @ig2, align 4
   store i32 %a, ptr @eg1, align 4


        


More information about the llvm-commits mailing list