-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[WIP][AMDGPU] Enable "amdgpu-uniform-intrinsic-combine" pass in pipeline. #128687
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
PankajDwivedi-25
wants to merge
18
commits into
llvm:main
Choose a base branch
from
PankajDwivedi-25:users/pkd-25/enable-uniform-intrinsic-combine
base: main
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.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c2f5258
[WIP][AMDGPU] combine uniform AMDGPU lane Intrinsics
PankajDwivedi-25 91dd6d9
refactored and updated intrinsics handling
PankajDwivedi-25 c98a967
removed redundant casting
PankajDwivedi-25 ef78ece
refactored, added more test
PankajDwivedi-25 d3ab828
integrated in pipeline, more test added
PankajDwivedi-25 f5edd78
removed unused gfx checks
PankajDwivedi-25 242afb1
added pass to llc pipeline, more test added
PankajDwivedi-25 69dd0aa
handled ballot with icmp for trivial waterfall
PankajDwivedi-25 be7a5a2
updated test
PankajDwivedi-25 16bc3a8
Fix: use isDivergentUse instead isUniform
PankajDwivedi-25 b3c4e93
addressed reviews
PankajDwivedi-25 94a4787
pull the ballot argument to all the match users
PankajDwivedi-25 7d0af28
Match and replace icmp ballot,0 with XOR
PankajDwivedi-25 809ffb5
Rebase: resolve merge
PankajDwivedi-25 11f6c0b
remove undef test
PankajDwivedi-25 13023a4
added pass to llc pipeline
PankajDwivedi-25 b4e8b93
Update test checks
PankajDwivedi-25 a6aef83
expectedly failing test, illegal COPY introduction by isel
PankajDwivedi-25 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
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
164 changes: 164 additions & 0 deletions
164
llvm/lib/Target/AMDGPU/AMDGPUUniformIntrinsicCombine.cpp
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,164 @@ | ||||||
//===-- AMDGPUUniformIntrinsicCombine.cpp ---------------------------------===// | ||||||
// | ||||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||
// See https://llvm.org/LICENSE.txt for license information. | ||||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||
// | ||||||
//===----------------------------------------------------------------------===// | ||||||
// | ||||||
/// \file | ||||||
/// This pass simplifies certain intrinsic calls when the arguments are uniform. | ||||||
//===----------------------------------------------------------------------===// | ||||||
|
||||||
#include "AMDGPU.h" | ||||||
#include "GCNSubtarget.h" | ||||||
#include "llvm/Analysis/DomTreeUpdater.h" | ||||||
#include "llvm/Analysis/LoopInfo.h" | ||||||
#include "llvm/Analysis/ScalarEvolution.h" | ||||||
#include "llvm/Analysis/TargetLibraryInfo.h" | ||||||
#include "llvm/Analysis/UniformityAnalysis.h" | ||||||
#include "llvm/CodeGen/TargetPassConfig.h" | ||||||
#include "llvm/IR/IRBuilder.h" | ||||||
#include "llvm/IR/InstIterator.h" | ||||||
#include "llvm/IR/InstVisitor.h" | ||||||
#include "llvm/IR/IntrinsicsAMDGPU.h" | ||||||
#include "llvm/IR/PatternMatch.h" | ||||||
#include "llvm/InitializePasses.h" | ||||||
#include "llvm/Target/TargetMachine.h" | ||||||
#include "llvm/Transforms/Utils/BasicBlockUtils.h" | ||||||
|
||||||
#define DEBUG_TYPE "amdgpu-uniform-intrinsic-combine" | ||||||
|
||||||
using namespace llvm; | ||||||
using namespace llvm::AMDGPU; | ||||||
using namespace llvm::PatternMatch; | ||||||
|
||||||
namespace { | ||||||
class AMDGPUUniformIntrinsicCombineLegacy : public FunctionPass { | ||||||
public: | ||||||
static char ID; | ||||||
AMDGPUUniformIntrinsicCombineLegacy() : FunctionPass(ID) { | ||||||
initializeAMDGPUUniformIntrinsicCombineLegacyPass( | ||||||
*PassRegistry::getPassRegistry()); | ||||||
} | ||||||
bool runOnFunction(Function &F) override; | ||||||
void getAnalysisUsage(AnalysisUsage &AU) const override { | ||||||
AU.setPreservesCFG(); | ||||||
AU.addRequired<UniformityInfoWrapperPass>(); | ||||||
AU.addRequired<TargetPassConfig>(); | ||||||
} | ||||||
}; | ||||||
|
||||||
class AMDGPUUniformIntrinsicCombineImpl | ||||||
: public InstVisitor<AMDGPUUniformIntrinsicCombineImpl> { | ||||||
private: | ||||||
const UniformityInfo *UI; | ||||||
bool optimizeUniformIntrinsicInst(IntrinsicInst &II) const; | ||||||
|
||||||
public: | ||||||
AMDGPUUniformIntrinsicCombineImpl() = delete; | ||||||
AMDGPUUniformIntrinsicCombineImpl(const UniformityInfo *UI) : UI(UI) {} | ||||||
bool run(Function &F); | ||||||
}; | ||||||
} // namespace | ||||||
|
||||||
char AMDGPUUniformIntrinsicCombineLegacy::ID = 0; | ||||||
char &llvm::AMDGPUUniformIntrinsicCombineLegacyPassID = | ||||||
AMDGPUUniformIntrinsicCombineLegacy::ID; | ||||||
|
||||||
bool AMDGPUUniformIntrinsicCombineLegacy::runOnFunction(Function &F) { | ||||||
if (skipFunction(F)) { | ||||||
return false; | ||||||
} | ||||||
const UniformityInfo *UI = | ||||||
&getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo(); | ||||||
return AMDGPUUniformIntrinsicCombineImpl(UI).run(F); | ||||||
} | ||||||
|
||||||
PreservedAnalyses | ||||||
AMDGPUUniformIntrinsicCombinePass::run(Function &F, | ||||||
FunctionAnalysisManager &AM) { | ||||||
const auto *UI = &AM.getResult<UniformityInfoAnalysis>(F); | ||||||
bool IsChanged = AMDGPUUniformIntrinsicCombineImpl(UI).run(F); | ||||||
|
||||||
if (!IsChanged) { | ||||||
return PreservedAnalyses::all(); | ||||||
} | ||||||
PreservedAnalyses PA; | ||||||
PA.preserve<DominatorTreeAnalysis>(); | ||||||
PA.preserve<LoopAnalysis>(); | ||||||
PA.preserve<ScalarEvolutionAnalysis>(); | ||||||
PA.preserve<UniformityInfoAnalysis>(); | ||||||
PA.preserve<TargetLibraryAnalysis>(); | ||||||
return PA; | ||||||
} | ||||||
|
||||||
bool AMDGPUUniformIntrinsicCombineImpl::run(Function &F) { | ||||||
bool IsChanged{false}; | ||||||
|
||||||
// Iterate over each instruction in the function to get the desired intrinsic | ||||||
// inst to check for optimization. | ||||||
for (Instruction &I : make_early_inc_range(instructions(F))) { | ||||||
if (auto *Intrinsic = dyn_cast<IntrinsicInst>(&I)) { | ||||||
IsChanged |= optimizeUniformIntrinsicInst(*Intrinsic); | ||||||
} | ||||||
} | ||||||
return IsChanged; | ||||||
} | ||||||
|
||||||
bool AMDGPUUniformIntrinsicCombineImpl::optimizeUniformIntrinsicInst( | ||||||
IntrinsicInst &II) const { | ||||||
llvm::Intrinsic::ID IID = II.getIntrinsicID(); | ||||||
|
||||||
switch (IID) { | ||||||
case Intrinsic::amdgcn_permlane64: | ||||||
case Intrinsic::amdgcn_readfirstlane: | ||||||
case Intrinsic::amdgcn_readlane: { | ||||||
Value *Src = II.getArgOperand(0); | ||||||
// Check if the argument use is divergent | ||||||
if (UI->isDivergentUse(II.getOperandUse(0))) | ||||||
return false; | ||||||
LLVM_DEBUG(dbgs() << "Replacing " << II << " with " << *Src << "\n"); | ||||||
II.replaceAllUsesWith(Src); | ||||||
return true; | ||||||
} | ||||||
case Intrinsic::amdgcn_ballot: { | ||||||
Value *Src = II.getArgOperand(0); | ||||||
if (UI->isDivergentUse(II.getOperandUse(0))) | ||||||
return false; | ||||||
LLVM_DEBUG(dbgs() << "Found uniform ballot intrinsic: " << II << "\n"); | ||||||
|
||||||
bool Changed = false; | ||||||
for (User *U : make_early_inc_range(II.users())) { | ||||||
if (auto *ICmp = dyn_cast<ICmpInst>(U)) { | ||||||
Value *Op0 = ICmp->getOperand(0); | ||||||
Value *Op1 = ICmp->getOperand(1); | ||||||
|
||||||
if (ICmp->getPredicate() == ICmpInst::ICMP_EQ && | ||||||
((Op0 == &II && match(Op1, m_Zero())) || | ||||||
(Op1 == &II && match(Op0, m_Zero())))) { | ||||||
|
||||||
IRBuilder<> Builder(ICmp); | ||||||
Value *Xor = Builder.CreateXor(Src, Builder.getTrue()); | ||||||
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.
Suggested change
|
||||||
LLVM_DEBUG(dbgs() << "Replacing with XOR: " << *Xor << "\n"); | ||||||
ICmp->replaceAllUsesWith(Xor); | ||||||
Changed = true; | ||||||
} | ||||||
} | ||||||
} | ||||||
return Changed; | ||||||
} | ||||||
} | ||||||
return false; | ||||||
} | ||||||
|
||||||
INITIALIZE_PASS_BEGIN(AMDGPUUniformIntrinsicCombineLegacy, DEBUG_TYPE, | ||||||
"AMDGPU uniformIntrinsic Combine", false, false) | ||||||
INITIALIZE_PASS_DEPENDENCY(UniformityInfoWrapperPass) | ||||||
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) | ||||||
INITIALIZE_PASS_END(AMDGPUUniformIntrinsicCombineLegacy, DEBUG_TYPE, | ||||||
"AMDGPU uniformIntrinsic Combine", false, false) | ||||||
|
||||||
FunctionPass *llvm::createAMDGPUUniformIntrinsicCombineLegacyPass() { | ||||||
return new AMDGPUUniformIntrinsicCombineLegacy(); | ||||||
} |
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
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
Oops, something went wrong.
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.
I think it would be enough to only check
Op1
, since only the rhs of the comparison should be the constant.You could even match the whole comparison:
match(U, m_ICmp(CmpInst::ICMP_EQ, m_Specific(&II), m_Zero()))