forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 75
[AMDGPU] Use fmac_f64 in "if (cond) a -= c" #466
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
Open
vpykhtin
wants to merge
1
commit into
amd-staging
Choose a base branch
from
amd/dev/vpykhtin/fma64_cond_opt
base: amd-staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,887
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| #include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h" | ||
| #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h" | ||
| #include "llvm/CodeGen/MachineDominators.h" | ||
| #include "llvm/CodeGen/MachineLoopInfo.h" | ||
| #include "llvm/CodeGen/TargetPassConfig.h" | ||
| #include "llvm/IR/IntrinsicsAMDGPU.h" | ||
| #include "llvm/Target/TargetMachine.h" | ||
|
|
@@ -47,6 +48,7 @@ class AMDGPUPostLegalizerCombinerImpl : public Combiner { | |
| const AMDGPUPostLegalizerCombinerImplRuleConfig &RuleConfig; | ||
| const GCNSubtarget &STI; | ||
| const SIInstrInfo &TII; | ||
| const MachineLoopInfo *MLI; | ||
| // TODO: Make CombinerHelper methods const. | ||
| mutable AMDGPUCombinerHelper Helper; | ||
|
|
||
|
|
@@ -56,7 +58,7 @@ class AMDGPUPostLegalizerCombinerImpl : public Combiner { | |
| GISelValueTracking &VT, GISelCSEInfo *CSEInfo, | ||
| const AMDGPUPostLegalizerCombinerImplRuleConfig &RuleConfig, | ||
| const GCNSubtarget &STI, MachineDominatorTree *MDT, | ||
| const LegalizerInfo *LI); | ||
| const MachineLoopInfo *MLI, const LegalizerInfo *LI); | ||
|
|
||
| static const char *getName() { return "AMDGPUPostLegalizerCombinerImpl"; } | ||
|
|
||
|
|
@@ -113,6 +115,18 @@ class AMDGPUPostLegalizerCombinerImpl : public Combiner { | |
| // bits are zero extended. | ||
| bool matchCombine_s_mul_u64(MachineInstr &MI, unsigned &NewOpcode) const; | ||
|
|
||
| // Match conditional subtraction patterns for FMA optimization | ||
| struct ConditionalSubToFMAMatchInfo { | ||
| Register Cond; | ||
| Register C; | ||
| Register A; | ||
| }; | ||
|
|
||
| bool matchConditionalSubToFMA(MachineInstr &MI, | ||
| ConditionalSubToFMAMatchInfo &MatchInfo) const; | ||
| void applyConditionalSubToFMA(MachineInstr &MI, | ||
| const ConditionalSubToFMAMatchInfo &MatchInfo) const; | ||
|
|
||
| private: | ||
| #define GET_GICOMBINER_CLASS_MEMBERS | ||
| #define AMDGPUSubtarget GCNSubtarget | ||
|
|
@@ -131,9 +145,10 @@ AMDGPUPostLegalizerCombinerImpl::AMDGPUPostLegalizerCombinerImpl( | |
| MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC, | ||
| GISelValueTracking &VT, GISelCSEInfo *CSEInfo, | ||
| const AMDGPUPostLegalizerCombinerImplRuleConfig &RuleConfig, | ||
| const GCNSubtarget &STI, MachineDominatorTree *MDT, const LegalizerInfo *LI) | ||
| const GCNSubtarget &STI, MachineDominatorTree *MDT, | ||
| const MachineLoopInfo *MLI, const LegalizerInfo *LI) | ||
| : Combiner(MF, CInfo, TPC, &VT, CSEInfo), RuleConfig(RuleConfig), STI(STI), | ||
| TII(*STI.getInstrInfo()), | ||
| TII(*STI.getInstrInfo()), MLI(MLI), | ||
| Helper(Observer, B, /*IsPreLegalize*/ false, &VT, MDT, LI, STI), | ||
| #define GET_GICOMBINER_CONSTRUCTOR_INITS | ||
| #include "AMDGPUGenPostLegalizeGICombiner.inc" | ||
|
|
@@ -435,6 +450,112 @@ bool AMDGPUPostLegalizerCombinerImpl::matchCombine_s_mul_u64( | |
| return false; | ||
| } | ||
|
|
||
| // Match conditional subtraction patterns for FMA optimization. | ||
| // | ||
| // This function identifies patterns like: | ||
| // result = a - (cond ? c : 0.0) | ||
| // result = a + (cond ? -c : 0.0) | ||
| // result = a + (-(cond ? c : 0.0)) | ||
| // | ||
| // These can be converted to an efficient FMA: | ||
| // result = fma((cond ? -1.0, 0.0), c, a) | ||
| // | ||
| bool AMDGPUPostLegalizerCombinerImpl::matchConditionalSubToFMA( | ||
| MachineInstr &MI, ConditionalSubToFMAMatchInfo &MatchInfo) const { | ||
| // Only optimize f64 with FMAC support, and check VOPD constraints. | ||
| if (!MLI || !STI.shouldUseConditionalSubToFMAF64()) | ||
| return false; | ||
|
|
||
| Register DstReg = MI.getOperand(0).getReg(); | ||
| LLT Ty = MRI.getType(DstReg); | ||
| if (Ty != LLT::scalar(64)) | ||
| return false; | ||
|
|
||
| Register A = MI.getOperand(1).getReg(); | ||
| Register RHS = MI.getOperand(2).getReg(); | ||
| MachineInstr *RHSMI = MRI.getVRegDef(RHS); | ||
| if (!RHSMI) | ||
| return false; | ||
|
|
||
| // Returns true if SelMI is a valid select with false value = 0.0. | ||
| auto matchSelectWithZero = [this, &MI](MachineInstr *SelMI, Register &Cond, | ||
| Register &TrueVal) -> bool { | ||
| if (!SelMI || SelMI->getOpcode() != TargetOpcode::G_SELECT) | ||
| return false; | ||
|
|
||
| // Check if FalseVal is exactly 0.0. | ||
| Register FalseVal = SelMI->getOperand(3).getReg(); | ||
| auto FalseConst = getFConstantVRegValWithLookThrough(FalseVal, MRI); | ||
| if (!FalseConst || !FalseConst->Value.isExactlyValue(0.0)) | ||
| return false; | ||
|
|
||
| // Check if TrueVal is not constant. | ||
| auto TempTrueVal = SelMI->getOperand(2).getReg(); | ||
| auto TrueConst = getAnyConstantVRegValWithLookThrough(TempTrueVal, MRI); | ||
| if (TrueConst) | ||
| return false; | ||
|
|
||
| // Check if select and the add/sub are in same loop context. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: Consider adding a comment explaining why we skip loop-invariant selects: // Check if select and the add/sub are in same loop context.
// Even for loop-invariant select (which might get hoisted), we skip
// the optimization because it wouldn't provide benefit in the loop body
// (same 1 instruction, but worse register pressure: 2 vs 4+ registers).
if (MLI->getLoopFor(MI.getParent()) != MLI->getLoopFor(SelMI->getParent()))
return false;This will help future maintainers understand the reasoning. |
||
| if (MLI->getLoopFor(MI.getParent()) != MLI->getLoopFor(SelMI->getParent())) | ||
| return false; | ||
|
|
||
| TrueVal = TempTrueVal; | ||
| Cond = SelMI->getOperand(1).getReg(); | ||
| return true; | ||
| }; | ||
|
|
||
| Register Cond, C; | ||
| if (MI.getOpcode() == TargetOpcode::G_FSUB) { | ||
| // Pattern: fsub a, (select cond, c, 0.0) | ||
| if (matchSelectWithZero(RHSMI, Cond, C)) { | ||
| MatchInfo = {Cond, C, A}; | ||
| return true; | ||
| } | ||
| } else if (MI.getOpcode() == TargetOpcode::G_FADD) { | ||
| // Pattern 1: fadd a, (fneg (select cond, c, 0.0)) | ||
| if (RHSMI->getOpcode() == TargetOpcode::G_FNEG) { | ||
| Register SelReg = RHSMI->getOperand(1).getReg(); | ||
| MachineInstr *SelMI = MRI.getVRegDef(SelReg); | ||
| if (matchSelectWithZero(SelMI, Cond, C)) { | ||
| MatchInfo = {Cond, C, A}; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| // Pattern 2: fadd a, (select cond, (fneg c), 0.0) | ||
| if (matchSelectWithZero(RHSMI, Cond, C)) { | ||
| // Check if C is fneg | ||
| MachineInstr *CMI = MRI.getVRegDef(C); | ||
| if (CMI && CMI->getOpcode() == TargetOpcode::G_FNEG) { | ||
| C = CMI->getOperand(1).getReg(); | ||
| MatchInfo = {Cond, C, A}; | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| void AMDGPUPostLegalizerCombinerImpl::applyConditionalSubToFMA( | ||
| MachineInstr &MI, const ConditionalSubToFMAMatchInfo &MatchInfo) const { | ||
| Register Dst = MI.getOperand(0).getReg(); | ||
| LLT Ty = MRI.getType(Dst); | ||
|
|
||
| // Build: correction = select cond, -1.0, 0.0 | ||
| APFloat MinusOne = APFloat(-1.0); | ||
| APFloat Zero = APFloat(0.0); | ||
|
|
||
| Register MinusOneReg = B.buildFConstant(Ty, MinusOne).getReg(0); | ||
| Register ZeroReg = B.buildFConstant(Ty, Zero).getReg(0); | ||
| Register Correction = | ||
| B.buildSelect(Ty, MatchInfo.Cond, MinusOneReg, ZeroReg).getReg(0); | ||
|
|
||
| // Build: result = fma(correction, c, a) | ||
| B.buildFMA(Dst, Correction, MatchInfo.C, MatchInfo.A, MI.getFlags()); | ||
|
|
||
| MI.eraseFromParent(); | ||
| } | ||
|
|
||
| // Pass boilerplate | ||
| // ================ | ||
|
|
||
|
|
@@ -467,6 +588,8 @@ void AMDGPUPostLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const { | |
| if (!IsOptNone) { | ||
| AU.addRequired<MachineDominatorTreeWrapperPass>(); | ||
| AU.addPreserved<MachineDominatorTreeWrapperPass>(); | ||
| AU.addRequired<MachineLoopInfoWrapperPass>(); | ||
| AU.addPreserved<MachineLoopInfoWrapperPass>(); | ||
| } | ||
| MachineFunctionPass::getAnalysisUsage(AU); | ||
| } | ||
|
|
@@ -494,6 +617,8 @@ bool AMDGPUPostLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) { | |
| MachineDominatorTree *MDT = | ||
| IsOptNone ? nullptr | ||
| : &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); | ||
| MachineLoopInfo *MLI = | ||
| IsOptNone ? nullptr : &getAnalysis<MachineLoopInfoWrapperPass>().getLI(); | ||
|
|
||
| CombinerInfo CInfo(/*AllowIllegalOps*/ false, /*ShouldLegalizeIllegal*/ true, | ||
| LI, EnableOpt, F.hasOptSize(), F.hasMinSize()); | ||
|
|
@@ -503,7 +628,7 @@ bool AMDGPUPostLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) { | |
| // Legalizer performs DCE, so a full DCE pass is unnecessary. | ||
| CInfo.EnableFullDCE = false; | ||
| AMDGPUPostLegalizerCombinerImpl Impl(MF, CInfo, TPC, *VT, /*CSEInfo*/ nullptr, | ||
| RuleConfig, ST, MDT, LI); | ||
| RuleConfig, ST, MDT, MLI, LI); | ||
| return Impl.combineMachineInstrs(); | ||
| } | ||
|
|
||
|
|
@@ -513,6 +638,7 @@ INITIALIZE_PASS_BEGIN(AMDGPUPostLegalizerCombiner, DEBUG_TYPE, | |
| false) | ||
| INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) | ||
| INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy) | ||
| INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass) | ||
| INITIALIZE_PASS_END(AMDGPUPostLegalizerCombiner, DEBUG_TYPE, | ||
| "Combine AMDGPU machine instrs after legalization", false, | ||
| false) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider updating the comment to reflect that constant support is deferred: