Skip to content

[GlobalMerge] Add MinSize feature to the GlobalMerge Pass. #93686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/include/llvm/CodeGen/GlobalMerge.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct GlobalMergeOptions {
// functions), see the code that passes in the offset in the ARM backend
// for more information.
unsigned MaxOffset = 0;
// The minimum size in bytes of each global that should considered in merging.
unsigned MinSize = 0;
bool GroupByUse = true;
bool IgnoreSingleUse = true;
bool MergeConst = false;
Expand Down
22 changes: 21 additions & 1 deletion llvm/lib/CodeGen/GlobalMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ static cl::opt<cl::boolOrDefault>
EnableGlobalMergeOnExternal("global-merge-on-external", cl::Hidden,
cl::desc("Enable global merge pass on external linkage"));

static cl::opt<unsigned>
GlobalMergeMinDataSize("global-merge-min-data-size",
cl::desc("The minimum size in bytes of each global "
"that should considered in merging."),
cl::init(0), cl::Hidden);

STATISTIC(NumMerged, "Number of globals merged");

namespace {
Expand Down Expand Up @@ -198,6 +204,19 @@ class GlobalMerge : public FunctionPass {
}

bool doInitialization(Module &M) override {
auto GetSmallDataLimit = [](Module &M) -> std::optional<uint64_t> {
Metadata *SDL = M.getModuleFlag("SmallDataLimit");
if (!SDL)
return std::nullopt;
return mdconst::extract<ConstantInt>(SDL)->getZExtValue();
};
if (GlobalMergeMinDataSize.getNumOccurrences())
Opt.MinSize = GlobalMergeMinDataSize;
else if (auto SDL = GetSmallDataLimit(M); SDL && *SDL > 0)
Opt.MinSize = *SDL + 1;
else
Opt.MinSize = 0;

GlobalMergeImpl P(TM, Opt);
return P.run(M);
}
Expand Down Expand Up @@ -670,7 +689,8 @@ bool GlobalMergeImpl::run(Module &M) {
continue;

Type *Ty = GV.getValueType();
if (DL.getTypeAllocSize(Ty) < Opt.MaxOffset) {
TypeSize AllocSize = DL.getTypeAllocSize(Ty);
if (AllocSize < Opt.MaxOffset && AllocSize >= Opt.MinSize) {
if (TM &&
TargetLoweringObjectFile::getKindForGlobal(&GV, *TM).isBSS())
BSSGlobals[{AddressSpace, Section}].push_back(&GV);
Expand Down
48 changes: 48 additions & 0 deletions llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-nonzero.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
; 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 -global-merge-min-data-size=0 \
; RUN: -verify-machineinstrs < %s | FileCheck %s -check-prefix=MINSIZE

@ig1 = internal global i32 0, align 4
@ig2 = internal global i32 0, align 4

@eg1 = dso_local global i32 0, align 4
@eg2 = dso_local global i32 0, align 4

; This test shows that GlobalDataMinSize is set to SmallDataLimit + 1 when
; SmallDataLimit module flag is set as non-zero, and that global-merge-min-data-size
; overrides the small data limit.

define void @f1(i32 %a) nounwind {
; SMALL-DATA-LABEL: f1:
; SMALL-DATA: # %bb.0:
; SMALL-DATA-NEXT: lui a1, %hi(ig1)
; SMALL-DATA-NEXT: sw a0, %lo(ig1)(a1)
; SMALL-DATA-NEXT: lui a1, %hi(ig2)
; SMALL-DATA-NEXT: sw a0, %lo(ig2)(a1)
; SMALL-DATA-NEXT: lui a1, %hi(eg1)
; SMALL-DATA-NEXT: sw a0, %lo(eg1)(a1)
; SMALL-DATA-NEXT: lui a1, %hi(eg2)
; SMALL-DATA-NEXT: sw a0, %lo(eg2)(a1)
; SMALL-DATA-NEXT: ret
;
; MINSIZE-LABEL: f1:
; MINSIZE: # %bb.0:
; MINSIZE-NEXT: lui a1, %hi(.L_MergedGlobals)
; MINSIZE-NEXT: sw a0, %lo(.L_MergedGlobals)(a1)
; MINSIZE-NEXT: addi a1, a1, %lo(.L_MergedGlobals)
; MINSIZE-NEXT: sw a0, 4(a1)
; MINSIZE-NEXT: sw a0, 8(a1)
; MINSIZE-NEXT: sw a0, 12(a1)
; MINSIZE-NEXT: ret
store i32 %a, ptr @ig1, align 4
store i32 %a, ptr @ig2, align 4
store i32 %a, ptr @eg1, align 4
store i32 %a, ptr @eg2, align 4
ret void
}


!llvm.module.flags = !{!0}
!0 = !{i32 8, !"SmallDataLimit", i32 8}
49 changes: 49 additions & 0 deletions llvm/test/CodeGen/RISCV/global-merge-minsize-smalldata-zero.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
; 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=riscv64 -riscv-enable-global-merge -global-merge-min-data-size=5 \
; RUN: -verify-machineinstrs < %s | FileCheck %s -check-prefix=MINSIZE

@ig1 = internal global i32 0, align 4
@ig2 = internal global i32 0, align 4

@eg1 = dso_local global i32 0, align 4
@eg2 = dso_local global i32 0, align 4


; This test shows that GlobalDataMinSize is set to 0 when SmallDataLimit module
; flag is set to zero, and that the global-merge-min-data-size option overrides
; the small data limit.

define void @f1(i32 %a) nounwind {
; SMALL-DATA-LABEL: f1:
; SMALL-DATA: # %bb.0:
; SMALL-DATA-NEXT: lui a1, %hi(.L_MergedGlobals)
; SMALL-DATA-NEXT: sw a0, %lo(.L_MergedGlobals)(a1)
; SMALL-DATA-NEXT: addi a1, a1, %lo(.L_MergedGlobals)
; SMALL-DATA-NEXT: sw a0, 4(a1)
; SMALL-DATA-NEXT: sw a0, 8(a1)
; SMALL-DATA-NEXT: sw a0, 12(a1)
; SMALL-DATA-NEXT: ret
;
; MINSIZE-LABEL: f1:
; MINSIZE: # %bb.0:
; MINSIZE-NEXT: lui a1, %hi(ig1)
; MINSIZE-NEXT: sw a0, %lo(ig1)(a1)
; MINSIZE-NEXT: lui a1, %hi(ig2)
; MINSIZE-NEXT: sw a0, %lo(ig2)(a1)
; MINSIZE-NEXT: lui a1, %hi(eg1)
; MINSIZE-NEXT: sw a0, %lo(eg1)(a1)
; MINSIZE-NEXT: lui a1, %hi(eg2)
; MINSIZE-NEXT: sw a0, %lo(eg2)(a1)
; MINSIZE-NEXT: ret
store i32 %a, ptr @ig1, align 4
store i32 %a, ptr @ig2, align 4
store i32 %a, ptr @eg1, align 4
store i32 %a, ptr @eg2, align 4
ret void
}


!llvm.module.flags = !{!0}
!0 = !{i32 8, !"SmallDataLimit", i32 0}
40 changes: 40 additions & 0 deletions llvm/test/CodeGen/RISCV/global-merge-minsize.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
; 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 -global-merge-min-data-size=5 \
; RUN: -verify-machineinstrs < %s | FileCheck %s -check-prefix=RV32-MINSIZE

@ig1 = internal global i32 0, align 4
@ig2 = internal global i32 0, align 4

@eg1 = dso_local global i32 0, align 4
@eg2 = dso_local global i32 0, align 4

define void @f1(i32 %a) nounwind {
; RV32-LABEL: f1:
; RV32: # %bb.0:
; RV32-NEXT: lui a1, %hi(.L_MergedGlobals)
; RV32-NEXT: sw a0, %lo(.L_MergedGlobals)(a1)
; RV32-NEXT: addi a1, a1, %lo(.L_MergedGlobals)
; RV32-NEXT: sw a0, 4(a1)
; RV32-NEXT: sw a0, 8(a1)
; RV32-NEXT: sw a0, 12(a1)
; RV32-NEXT: ret
;
; RV32-MINSIZE-LABEL: f1:
; RV32-MINSIZE: # %bb.0:
; RV32-MINSIZE-NEXT: lui a1, %hi(ig1)
; RV32-MINSIZE-NEXT: sw a0, %lo(ig1)(a1)
; RV32-MINSIZE-NEXT: lui a1, %hi(ig2)
; RV32-MINSIZE-NEXT: sw a0, %lo(ig2)(a1)
; RV32-MINSIZE-NEXT: lui a1, %hi(eg1)
; RV32-MINSIZE-NEXT: sw a0, %lo(eg1)(a1)
; RV32-MINSIZE-NEXT: lui a1, %hi(eg2)
; RV32-MINSIZE-NEXT: sw a0, %lo(eg2)(a1)
; RV32-MINSIZE-NEXT: ret
store i32 %a, ptr @ig1, align 4
store i32 %a, ptr @ig2, align 4
store i32 %a, ptr @eg1, align 4
store i32 %a, ptr @eg2, align 4
ret void
}
Loading