Skip to content

[AMDGPU] Improve MIR pattern for FMinFMaxLegacy combine. NFC. #90968

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
May 5, 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
5 changes: 3 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUCombine.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def fmin_fmax_legacy_matchdata : GIDefMatchData<"FMinFMaxLegacyInfo">;
let Predicates = [HasFminFmaxLegacy] in
def fcmp_select_to_fmin_fmax_legacy : GICombineRule<
(defs root:$select, fmin_fmax_legacy_matchdata:$matchinfo),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I supposed to list $fcmp in the defs since I am passing it into my match function? Or doesn't that count as an "external" use of $fcmp?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I prefer to only list the $root. defs is probably definitions and not def in SSA.

(match (wip_match_opcode G_SELECT):$select,
[{ return matchFMinFMaxLegacy(*${select}, ${matchinfo}); }]),
(match (G_FCMP $cond, $pred, $lhs, $rhs):$fcmp,
(G_SELECT f32:$dst, $cond, $true, $false):$select,
[{ return matchFMinFMaxLegacy(*${select}, *${fcmp}, ${matchinfo}); }]),
(apply [{ applySelectFCmpToFMinToFMaxLegacy(*${select}, ${matchinfo}); }])>;


Expand Down
18 changes: 8 additions & 10 deletions llvm/lib/Target/AMDGPU/AMDGPUPostLegalizerCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class AMDGPUPostLegalizerCombinerImpl : public Combiner {
};

// TODO: Make sure fmin_legacy/fmax_legacy don't canonicalize
bool matchFMinFMaxLegacy(MachineInstr &MI, FMinFMaxLegacyInfo &Info) const;
bool matchFMinFMaxLegacy(MachineInstr &MI, MachineInstr &FCmp,
FMinFMaxLegacyInfo &Info) const;
void applySelectFCmpToFMinToFMaxLegacy(MachineInstr &MI,
const FMinFMaxLegacyInfo &Info) const;

Expand Down Expand Up @@ -159,17 +160,14 @@ bool AMDGPUPostLegalizerCombinerImpl::tryCombineAll(MachineInstr &MI) const {
}

bool AMDGPUPostLegalizerCombinerImpl::matchFMinFMaxLegacy(
MachineInstr &MI, FMinFMaxLegacyInfo &Info) const {
// FIXME: Type predicate on pattern
if (MRI.getType(MI.getOperand(0).getReg()) != LLT::scalar(32))
return false;

Register Cond = MI.getOperand(1).getReg();
if (!MRI.hasOneNonDBGUse(Cond) ||
!mi_match(Cond, MRI,
m_GFCmp(m_Pred(Info.Pred), m_Reg(Info.LHS), m_Reg(Info.RHS))))
MachineInstr &MI, MachineInstr &FCmp, FMinFMaxLegacyInfo &Info) const {
if (!MRI.hasOneNonDBGUse(FCmp.getOperand(0).getReg()))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to do this check in the MIR pattern?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Other uses could have sneaked in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sneaked in after the autogenerated matcher runs but before this match function is called? That doesn't sound right.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could:

GFCmp *Cmp = cast<GFCmp>(&FCmp);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The G_FCMP could be used by other MIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The G_FCMP could be used by other MIs.

I know. I would like to write something in the pattern to check for this, instead of writing C++ to check for this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open an issue?

return false;

Info.Pred =
static_cast<CmpInst::Predicate>(FCmp.getOperand(1).getPredicate());
Info.LHS = FCmp.getOperand(2).getReg();
Info.RHS = FCmp.getOperand(3).getReg();
Register True = MI.getOperand(2).getReg();
Register False = MI.getOperand(3).getReg();

Expand Down