This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add the GlobalMerge pass (disabled by default)
ClosedPublic

Authored by asb on Jul 25 2022, 6:25 AM.

Details

Summary

Split out from D129178, this just adds the GlobalMerge tests (other than global-merge-minsize.ll which is testing a specific configuration of the pass when it's enabled) and exposes -riscv-enable-global-merge and doesn't enable it by default.

Note that the comment "// FIXME: Unify control over GlobalMerge." is copied from the Arm and AArch64 backends, which expose the same flag. Presumably the author is imagining some later refactoring that provides a target-independent flag.

Diff Detail

Event Timeline

asb created this revision.Jul 25 2022, 6:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 25 2022, 6:25 AM
asb requested review of this revision.Jul 25 2022, 6:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 25 2022, 6:25 AM
reames requested changes to this revision.Jul 25 2022, 8:41 AM
reames added inline comments.
llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
47

Please add the explicit cl::init(false) rather than relying on default initialization.

214

Can't this just be if (EnableGlobalMerge)?

llvm/test/CodeGen/RISCV/global-merge-offset.ll
3

The use of sed here is simply overly complicating things.

Please add a second test to this file which uses a separate global with an alternate size.

This revision now requires changes to proceed.Jul 25 2022, 8:41 AM
luismarques added inline comments.Jul 27 2022, 4:04 PM
llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
214

This seems to be used in various other places.

craig.topper added inline comments.Jul 27 2022, 4:10 PM
llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
47

It's not relying on default initialization. It's a cl::boolOrDefault option not a bool option. This due to the usage in the original patch where it has 3 behaviors.

214

It's not a bool option it's boolOrDefault option so it has 3 values not 2.

asb added a comment.Aug 1 2022, 8:53 AM

Re EnableGlobalMerge - I'm happy to make it just a bool option for this patch if that's preferable, but the follow-on patch that enables it should really change it back to the tri-value boolOrDefault in my opinion in order to match the equivalent option on AArch64 and Arm (and because the logic takes advantage of all three values). Let me know what you think.

llvm/test/CodeGen/RISCV/global-merge-offset.ll
3

The use of sed is because it's not very easy to construct a test (in a way that remains easily readable at least) that demonstrates the global merging behaviour.

The file I started with did:

@ga1 = dso_local global [510 x i32] zeroinitializer, align 4
@gi1 = dso_local global i32 0, align 4

; TODO: It would be better for codesize if the final store below was
; `sw a0, 0(a2)`.

define void @f_mergeable(i32 %a) nounwind {
; CHECK-LABEL: f_mergeable:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a1, %hi(ga1+2040)
; CHECK-NEXT:    sw a0, %lo(ga1+2040)(a1)
; CHECK-NEXT:    lui a1, %hi(.L_MergedGlobals)
; CHECK-NEXT:    sw a0, %lo(.L_MergedGlobals)(a1)
; CHECK-NEXT:    ret
  %ga1_end = getelementptr inbounds [510 x i32], ptr @ga1, i32 0, i64 510
  store i32 %a, ptr %ga1_end, align 4
  store i32 %a, ptr @gi1, align 4
  ret void
}

@ga2 = dso_local global [511 x i32] zeroinitializer, align 4
@gi2 = dso_local global i32 0, align 4

define void @f_unmergeable(i32 %a) nounwind {
; CHECK-LABEL: f_unmergeable:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lui a1, %hi(ga2+2040)
; CHECK-NEXT:    sw a0, %lo(ga2+2040)(a1)
; CHECK-NEXT:    lui a1, %hi(.L_MergedGlobals+4)
; CHECK-NEXT:    sw a0, %lo(.L_MergedGlobals+4)(a1)
; CHECK-NEXT:    ret
  %ga2_end = getelementptr inbounds [511 x i32], ptr @ga2, i32 0, i64 510
  store i32 %a, ptr %ga2_end, align 4
  store i32 %a, ptr @gi2, align 4
  ret void
}

But you can see the globals merging pass merged gi1 and gi2, making the test not so useful (room for improved heuristics in the pass too perhaps...).

craig.topper added inline comments.Aug 8 2022, 3:34 PM
llvm/test/CodeGen/RISCV/global-merge-offset.ll
3

So the options are sed or two test files? @reames which do you prefer?

asb added inline comments.Aug 8 2022, 11:02 PM
llvm/test/CodeGen/RISCV/global-merge-offset.ll
3

Yes, I think those are the feasible options.

reames accepted this revision.Aug 9 2022, 8:19 AM

LGTM

On the test question, decided I didn't really care. I think we probably could construct this without sed, but the marginal improvement in readability isn't worth further delay. I might take a shot at a follow up patch on that issue specifically, or I might not.

llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
47

I'm not familiar with usage of boolOrDefault. You can ignore my comment here if you believe boolOrDefault is a reasonable choice here. I'll defer to your judgement.

This revision is now accepted and ready to land.Aug 9 2022, 8:19 AM

Thanks for enabling this!

is it okay to merge this patch or are there pending dependencies?

This revision was landed with ongoing or failed builds.Sep 8 2022, 6:51 PM
This revision was automatically updated to reflect the committed changes.