-
Notifications
You must be signed in to change notification settings - Fork 14.9k
AMDGPU: Refactor lowering of s_barrier to split barriers #154648
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,161 @@ | ||
//===-- AMDGPULowerIntrinsics.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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Lower intrinsics that would otherwise require separate handling in both | ||
// SelectionDAG and GlobalISel. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "AMDGPU.h" | ||
#include "AMDGPUTargetMachine.h" | ||
#include "GCNSubtarget.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/IntrinsicInst.h" | ||
#include "llvm/IR/IntrinsicsAMDGPU.h" | ||
#include "llvm/InitializePasses.h" | ||
|
||
#define DEBUG_TYPE "amdgpu-lower-intrinsics" | ||
nhaehnle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
class AMDGPULowerIntrinsicsImpl { | ||
public: | ||
Module &M; | ||
const AMDGPUTargetMachine &TM; | ||
|
||
AMDGPULowerIntrinsicsImpl(Module &M, const AMDGPUTargetMachine &TM) | ||
: M(M), TM(TM) {} | ||
|
||
bool run(); | ||
|
||
private: | ||
bool visitBarrier(IntrinsicInst &I); | ||
}; | ||
|
||
class AMDGPULowerIntrinsicsLegacy : public ModulePass { | ||
public: | ||
static char ID; | ||
|
||
AMDGPULowerIntrinsicsLegacy() : ModulePass(ID) {} | ||
|
||
bool runOnModule(Module &M) override; | ||
|
||
void getAnalysisUsage(AnalysisUsage &AU) const override { | ||
AU.addRequired<TargetPassConfig>(); | ||
AU.setPreservesCFG(); | ||
} | ||
}; | ||
|
||
template <class T> static void forEachCall(Function &Intrin, T Callback) { | ||
for (User *U : make_early_inc_range(Intrin.users())) { | ||
if (auto *CI = dyn_cast<IntrinsicInst>(U)) | ||
Callback(CI); | ||
} | ||
} | ||
|
||
} // anonymous namespace | ||
|
||
bool AMDGPULowerIntrinsicsImpl::run() { | ||
bool Changed = false; | ||
|
||
for (Function &F : M) { | ||
switch (F.getIntrinsicID()) { | ||
default: | ||
continue; | ||
case Intrinsic::amdgcn_s_barrier: | ||
case Intrinsic::amdgcn_s_barrier_signal: | ||
case Intrinsic::amdgcn_s_barrier_signal_isfirst: | ||
case Intrinsic::amdgcn_s_barrier_wait: | ||
forEachCall(F, [&](IntrinsicInst *II) { Changed |= visitBarrier(*II); }); | ||
break; | ||
} | ||
} | ||
|
||
return Changed; | ||
} | ||
|
||
// Optimize barriers and lower s_barrier to a sequence of split barrier | ||
// intrinsics. | ||
bool AMDGPULowerIntrinsicsImpl::visitBarrier(IntrinsicInst &I) { | ||
assert(I.getIntrinsicID() == Intrinsic::amdgcn_s_barrier || | ||
I.getIntrinsicID() == Intrinsic::amdgcn_s_barrier_signal || | ||
I.getIntrinsicID() == Intrinsic::amdgcn_s_barrier_signal_isfirst || | ||
I.getIntrinsicID() == Intrinsic::amdgcn_s_barrier_wait); | ||
|
||
const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(*I.getFunction()); | ||
bool IsSingleWaveWG = false; | ||
|
||
if (TM.getOptLevel() > CodeGenOptLevel::None) { | ||
unsigned WGMaxSize = ST.getFlatWorkGroupSizes(*I.getFunction()).second; | ||
IsSingleWaveWG = WGMaxSize <= ST.getWavefrontSize(); | ||
} | ||
|
||
IRBuilder<> B(&I); | ||
|
||
if (IsSingleWaveWG) { | ||
// Down-grade waits, remove split signals. | ||
if (I.getIntrinsicID() == Intrinsic::amdgcn_s_barrier || | ||
I.getIntrinsicID() == Intrinsic::amdgcn_s_barrier_wait) { | ||
B.CreateIntrinsic(B.getVoidTy(), Intrinsic::amdgcn_wave_barrier, {}); | ||
} else if (I.getIntrinsicID() == | ||
Intrinsic::amdgcn_s_barrier_signal_isfirst) { | ||
// If we're the only wave of the workgroup, we're always first. | ||
I.replaceAllUsesWith(B.getInt1(true)); | ||
} | ||
I.eraseFromParent(); | ||
return true; | ||
} | ||
|
||
if (I.getIntrinsicID() == Intrinsic::amdgcn_s_barrier && | ||
ST.hasSplitBarriers()) { | ||
// Lower to split barriers. | ||
Value *BarrierID_32 = B.getInt32(AMDGPU::Barrier::WORKGROUP); | ||
Value *BarrierID_16 = B.getInt16(AMDGPU::Barrier::WORKGROUP); | ||
B.CreateIntrinsic(B.getVoidTy(), Intrinsic::amdgcn_s_barrier_signal, | ||
{BarrierID_32}); | ||
B.CreateIntrinsic(B.getVoidTy(), Intrinsic::amdgcn_s_barrier_wait, | ||
{BarrierID_16}); | ||
I.eraseFromParent(); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
PreservedAnalyses AMDGPULowerIntrinsicsPass::run(Module &M, | ||
ModuleAnalysisManager &MAM) { | ||
AMDGPULowerIntrinsicsImpl Impl(M, TM); | ||
if (!Impl.run()) | ||
return PreservedAnalyses::all(); | ||
PreservedAnalyses PA; | ||
PA.preserveSet<CFGAnalyses>(); | ||
return PA; | ||
} | ||
|
||
bool AMDGPULowerIntrinsicsLegacy::runOnModule(Module &M) { | ||
auto &TPC = getAnalysis<TargetPassConfig>(); | ||
const AMDGPUTargetMachine &TM = TPC.getTM<AMDGPUTargetMachine>(); | ||
|
||
AMDGPULowerIntrinsicsImpl Impl(M, TM); | ||
return Impl.run(); | ||
} | ||
|
||
#define PASS_DESC "AMDGPU lower intrinsics" | ||
INITIALIZE_PASS_BEGIN(AMDGPULowerIntrinsicsLegacy, DEBUG_TYPE, PASS_DESC, false, | ||
false) | ||
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) | ||
INITIALIZE_PASS_END(AMDGPULowerIntrinsicsLegacy, DEBUG_TYPE, PASS_DESC, false, | ||
false) | ||
|
||
char AMDGPULowerIntrinsicsLegacy::ID = 0; | ||
|
||
ModulePass *llvm::createAMDGPULowerIntrinsicsLegacyPass() { | ||
return new AMDGPULowerIntrinsicsLegacy; | ||
} |
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
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.
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.
This isn't so much code that duplicating between globalisel and selection dag is a problem. Is a whole pass really needed for this one tiny case? Can we get this into one of the existing IR lowering passes (e.g. PreISelIntrinsicLowering)?
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.
PreISelIntrinsicLowering doesn't handle target-specific intrinsics. So is it better to teach that pass how to call into target hooks to handle target intrinsics, or to have a target-specific pass like in this patch?
Uh oh!
There was an error while loading. Please reload this page.
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've looked at this. We could perhaps add a hook to TargetTransformInfo, but it feels a bit out of place. PreISelIntrinsicLowering iterates over the functions of the module and looks at calls from there, which means that hooking anything into that pass would basically only re-use this loop over the functions of the module.
I can sort of see the point about breaking the module pass manager (although -- isn't it really the other way around? Breaking up a function pass manager hurts, but a function pass inside a module pass manager ought to be fairly unproblematic), and perhaps a larger point that iterating over all code just for this is a bit excessive.
So I'm going to change this into a module pass that iterates like PreISelIntrinsicLowering.
As for how much code this is: I ran into this precisely because it's about to become a whole lot more code in the near future. This change is simply trying to prepare upstream for that.