1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

[NewPM][AMDGPU] Port amdgpu-printf-runtime-binding

And add to AMDGPU opt pipeline.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D94026
This commit is contained in:
Arthur Eubanks 2021-01-02 22:05:23 -08:00
parent e9433f5479
commit d5de361aab
5 changed files with 67 additions and 24 deletions

View File

@ -260,6 +260,11 @@ ModulePass *createAMDGPUPrintfRuntimeBinding();
void initializeAMDGPUPrintfRuntimeBindingPass(PassRegistry&);
extern char &AMDGPUPrintfRuntimeBindingID;
struct AMDGPUPrintfRuntimeBindingPass
: PassInfoMixin<AMDGPUPrintfRuntimeBindingPass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};
ModulePass* createAMDGPUUnifyMetadataPass();
void initializeAMDGPUUnifyMetadataPass(PassRegistry&);
extern char &AMDGPUUnifyMetadataID;

View File

@ -32,10 +32,12 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
using namespace llvm;
@ -44,8 +46,7 @@ using namespace llvm;
#define DWORD_ALIGN 4
namespace {
class LLVM_LIBRARY_VISIBILITY AMDGPUPrintfRuntimeBinding final
: public ModulePass {
class AMDGPUPrintfRuntimeBinding final : public ModulePass {
public:
static char ID;
@ -54,25 +55,36 @@ public:
private:
bool runOnModule(Module &M) override;
void getConversionSpecifiers(SmallVectorImpl<char> &OpConvSpecifiers,
StringRef fmt, size_t num_ops) const;
bool shouldPrintAsStr(char Specifier, Type *OpType) const;
bool
lowerPrintfForGpu(Module &M,
function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.addRequired<DominatorTreeWrapperPass>();
}
};
Value *simplify(Instruction *I, const TargetLibraryInfo *TLI) {
class AMDGPUPrintfRuntimeBindingImpl {
public:
AMDGPUPrintfRuntimeBindingImpl(
function_ref<const DominatorTree &(Function &)> GetDT,
function_ref<const TargetLibraryInfo &(Function &)> GetTLI)
: GetDT(GetDT), GetTLI(GetTLI) {}
bool run(Module &M);
private:
void getConversionSpecifiers(SmallVectorImpl<char> &OpConvSpecifiers,
StringRef fmt, size_t num_ops) const;
bool shouldPrintAsStr(char Specifier, Type *OpType) const;
bool lowerPrintfForGpu(Module &M);
Value *simplify(Instruction *I, const TargetLibraryInfo *TLI,
const DominatorTree *DT) {
return SimplifyInstruction(I, {*TD, TLI, DT});
}
const DataLayout *TD;
const DominatorTree *DT;
function_ref<const DominatorTree &(Function &)> GetDT;
function_ref<const TargetLibraryInfo &(Function &)> GetTLI;
SmallVector<CallInst *, 32> Printfs;
};
} // namespace
@ -95,12 +107,11 @@ ModulePass *createAMDGPUPrintfRuntimeBinding() {
}
} // namespace llvm
AMDGPUPrintfRuntimeBinding::AMDGPUPrintfRuntimeBinding()
: ModulePass(ID), TD(nullptr), DT(nullptr) {
AMDGPUPrintfRuntimeBinding::AMDGPUPrintfRuntimeBinding() : ModulePass(ID) {
initializeAMDGPUPrintfRuntimeBindingPass(*PassRegistry::getPassRegistry());
}
void AMDGPUPrintfRuntimeBinding::getConversionSpecifiers(
void AMDGPUPrintfRuntimeBindingImpl::getConversionSpecifiers(
SmallVectorImpl<char> &OpConvSpecifiers, StringRef Fmt,
size_t NumOps) const {
// not all format characters are collected.
@ -132,8 +143,8 @@ void AMDGPUPrintfRuntimeBinding::getConversionSpecifiers(
}
}
bool AMDGPUPrintfRuntimeBinding::shouldPrintAsStr(char Specifier,
Type *OpType) const {
bool AMDGPUPrintfRuntimeBindingImpl::shouldPrintAsStr(char Specifier,
Type *OpType) const {
if (Specifier != 's')
return false;
const PointerType *PT = dyn_cast<PointerType>(OpType);
@ -146,8 +157,7 @@ bool AMDGPUPrintfRuntimeBinding::shouldPrintAsStr(char Specifier,
return ElemIType->getBitWidth() == 8;
}
bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
Module &M, function_ref<const TargetLibraryInfo &(Function &)> GetTLI) {
bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
LLVMContext &Ctx = M.getContext();
IRBuilder<> Builder(Ctx);
Type *I32Ty = Type::getInt32Ty(Ctx);
@ -172,7 +182,8 @@ bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
}
if (auto I = dyn_cast<Instruction>(Op)) {
Value *Op_simplified = simplify(I, &GetTLI(*I->getFunction()));
Value *Op_simplified =
simplify(I, &GetTLI(*I->getFunction()), &GetDT(*I->getFunction()));
if (Op_simplified)
Op = Op_simplified;
}
@ -552,7 +563,7 @@ bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
return true;
}
bool AMDGPUPrintfRuntimeBinding::runOnModule(Module &M) {
bool AMDGPUPrintfRuntimeBindingImpl::run(Module &M) {
Triple TT(M.getTargetTriple());
if (TT.getArch() == Triple::r600)
return false;
@ -581,11 +592,31 @@ bool AMDGPUPrintfRuntimeBinding::runOnModule(Module &M) {
}
TD = &M.getDataLayout();
auto DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
DT = DTWP ? &DTWP->getDomTree() : nullptr;
return lowerPrintfForGpu(M);
}
bool AMDGPUPrintfRuntimeBinding::runOnModule(Module &M) {
auto GetDT = [this](Function &F) -> DominatorTree & {
return this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
};
auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
};
return lowerPrintfForGpu(M, GetTLI);
return AMDGPUPrintfRuntimeBindingImpl(GetDT, GetTLI).run(M);
}
PreservedAnalyses
AMDGPUPrintfRuntimeBindingPass::run(Module &M, ModuleAnalysisManager &AM) {
FunctionAnalysisManager &FAM =
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
auto GetDT = [&FAM](Function &F) -> DominatorTree & {
return FAM.getResult<DominatorTreeAnalysis>(F);
};
auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
return FAM.getResult<TargetLibraryAnalysis>(F);
};
bool Changed = AMDGPUPrintfRuntimeBindingImpl(GetDT, GetTLI).run(M);
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
}

View File

@ -502,6 +502,10 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB,
PM.addPass(AMDGPUUnifyMetadataPass());
return true;
}
if (PassName == "amdgpu-printf-runtime-binding") {
PM.addPass(AMDGPUPrintfRuntimeBindingPass());
return true;
}
return false;
});
PB.registerPipelineParsingCallback(
@ -552,6 +556,7 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB,
return;
PM.addPass(AMDGPUUnifyMetadataPass());
PM.addPass(AMDGPUPrintfRuntimeBindingPass());
if (InternalizeSymbols) {
PM.addPass(InternalizePass(mustPreserveGV));

View File

@ -1,6 +1,7 @@
; RUN: opt -mtriple=r600-- -amdgpu-printf-runtime-binding -mcpu=r600 -S < %s | FileCheck --check-prefix=FUNC --check-prefix=R600 %s
; RUN: opt -mtriple=amdgcn-- -amdgpu-printf-runtime-binding -mcpu=fiji -S < %s | FileCheck --check-prefix=FUNC --check-prefix=GCN %s
; RUN: opt -mtriple=amdgcn--amdhsa -amdgpu-printf-runtime-binding -mcpu=fiji -S < %s | FileCheck --check-prefix=FUNC --check-prefix=GCN %s
; RUN: opt -mtriple=amdgcn--amdhsa -passes=amdgpu-printf-runtime-binding -mcpu=fiji -S < %s | FileCheck --check-prefix=FUNC --check-prefix=GCN %s
; FUNC-LABEL: @test_kernel(
; R600-LABEL: entry

View File

@ -470,7 +470,8 @@ static bool shouldPinPassToLegacyPM(StringRef Pass) {
"amdgpu-lower-kernel-attributes",
"amdgpu-propagate-attributes-early",
"amdgpu-propagate-attributes-late",
"amdgpu-unify-metadata"};
"amdgpu-unify-metadata",
"amdgpu-printf-runtime-binding"};
for (const auto &P : PassNameExactToIgnore)
if (Pass == P)
return false;