This is an archive of the discontinued LLVM Phabricator instance.

[NFC][AMDGPU] Cleanup the SIOptimizeExecMasking pass.
ClosedPublic

Authored by tsymalla on Jul 4 2022, 8:26 AM.

Details

Summary

This patch removes a bit of code duplication and
moves the v_cmpx optimization out of the
runOnMachineFunction pass.

Diff Detail

Event Timeline

tsymalla created this revision.Jul 4 2022, 8:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 4 2022, 8:26 AM
tsymalla requested review of this revision.Jul 4 2022, 8:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 4 2022, 8:26 AM
foad added a comment.Jul 4 2022, 8:40 AM

I would also suggest turning more static functions into const member functions, so you don't have to keep passing around ST and TRI and MRI and TII...

llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
86

Maybe add a new SIRegisterInfo::getEXEC, similar to the existing SIRegisterInfo::getVCC?

nhaehnle added inline comments.Jul 5 2022, 1:25 AM
llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
86

We talked about that a bit offline. I'm partial to having a "LaneMaskConstants" helper that gives access to a whole suite of wave-size dependent constants, i.e. physical register constants but also for the related opcodes.

tsymalla updated this revision to Diff 442252.Jul 5 2022, 3:54 AM

Addressing review comments.

tsymalla added inline comments.Jul 5 2022, 3:55 AM
llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
86

I decided to go with @foad 's approach for now.
When I start working on the helper you were talking about, I'll clean up the whole codebase, including the now-introduced getExec() function.

foad added inline comments.Jul 5 2022, 5:48 AM
llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
734

I don't understand the value of making this a separate class, instead of part of the main SIOptimizeExecMasking class.

foad added inline comments.Jul 5 2022, 6:04 AM
llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
734

Even if you do want a separate class there is no need for dynamically creating an instance. You could just have a local variable:

SIOptimizeExecMaskingUtils Utils(MF);
bool Changed = Utils.optimizeExecSequence(MF);
Changed |= Utils.optimizeVCmpxAndSaveexecSequence(MF);
tsymalla added inline comments.Jul 5 2022, 6:10 AM
llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
734

After doing so and adding all necessary members to the SIOptimizeExecMasking class, it felt very cluttered.
I can revert that if you want, but the idea was to have a class that consists of various references to the sub-objects (TII, TRI etc.). These rely on the MachineFunction being passed. So, to ensure that these references are actually set, I created a separate class which can only be constructed by passing a MachineFunction. If we would add these members (references) to SIOptimizeExecMasking, we would be required to initialize them in the constructor AFAIK, which is not possible because of the required MachineFunction which is only passed in runOnMachineFunction. So there is the choice of using a) a separate class, b) using pointers and adding nullptr-checks everywhere or c) just go the unsafe route. I preferred to do a), cleanly abstract the sub-objects and all corresponding logic away, have the member references being initialized in the constructor of the Utils class.

foad added inline comments.Jul 5 2022, 6:18 AM
llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
734

When you say "the unsafe route" you mean using pointers without adding nullptr checks? That is what I'd prefer, but I'm not going to insist. It looks like you are already doing that for ST in your utils class, which is a pointer with no nullptr checks.

If you are determine to keep the utils class I would prefer:

  • Make the instance of the utils class a local variable in the SIOptimizeExecMasking::runOnMachineFunction method. Currently you dynamically create an instance which will survive after runOnMachineFunction returns, which feels wrong.
  • Make MF a member variable of the utils class (as a reference not a pointer) so you don't have to keep passing it into the optimize* methods.
  • Make ST a reference not a pointer.
  • Perhaps replace the two optimize* methods with a single bool run() method.

Please just merge the two classes. There is no good reason at the moment to separate them.

Please just merge the two classes. There is no good reason at the moment to separate them.

Sure. Will do.

tsymalla added inline comments.Jul 5 2022, 6:45 AM
llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
734

Sure.

Bullet point 3 will not be possible at the moment. TargetSubtargetInfo (which GCNSubtarget subclasses) does not allow copying, and that is required for assignment of reference members as far as I recall.

I will merge the class with SIOptimizeExecMasking.

foad added inline comments.Jul 5 2022, 7:02 AM
llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
734

Bullet point 3 will not be possible at the moment. TargetSubtargetInfo (which GCNSubtarget subclasses) does not allow copying, and that is required for assignment of reference members as far as I recall.

You definitely don't want to copy it. Just initialize the reference here: SIOptimizeExecMaskingUtils(MachineFunction &MF) : ST(...)

(But this is moot if you're not going to use the utils class.)

tsymalla updated this revision to Diff 442306.Jul 5 2022, 7:48 AM

Address review comments, revert usage of the SIOptimizeExecMaskingUtils class.

foad added a comment.Jul 5 2022, 8:42 AM

Looks OK to me.

llvm/lib/Target/AMDGPU/SIOptimizeExecMasking.cpp
24

I have a slight preference for calling this field "MF" even if it makes runOnMachineFunction a little uglier.

tsymalla updated this revision to Diff 442455.Jul 6 2022, 1:10 AM

Addressing review change requests.

tsymalla marked an inline comment as done.Jul 6 2022, 1:11 AM
foad accepted this revision.Jul 6 2022, 1:45 AM

LGTM, thanks!

This revision is now accepted and ready to land.Jul 6 2022, 1:45 AM
This revision was landed with ongoing or failed builds.Jul 6 2022, 2:03 AM
This revision was automatically updated to reflect the committed changes.