diff --git a/llvm/lib/Target/AMDGPU/AMDGPU.h b/llvm/lib/Target/AMDGPU/AMDGPU.h --- a/llvm/lib/Target/AMDGPU/AMDGPU.h +++ b/llvm/lib/Target/AMDGPU/AMDGPU.h @@ -260,6 +260,11 @@ void initializeAMDGPUPrintfRuntimeBindingPass(PassRegistry&); extern char &AMDGPUPrintfRuntimeBindingID; +struct AMDGPUPrintfRuntimeBindingPass + : PassInfoMixin { + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); +}; + ModulePass* createAMDGPUUnifyMetadataPass(); void initializeAMDGPUUnifyMetadataPass(PassRegistry&); extern char &AMDGPUUnifyMetadataID; diff --git a/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp b/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp --- a/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp @@ -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 @@ #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 @@ private: bool runOnModule(Module &M) override; - void getConversionSpecifiers(SmallVectorImpl &OpConvSpecifiers, - StringRef fmt, size_t num_ops) const; - - bool shouldPrintAsStr(char Specifier, Type *OpType) const; - bool - lowerPrintfForGpu(Module &M, - function_ref GetTLI); void getAnalysisUsage(AnalysisUsage &AU) const override { AU.addRequired(); AU.addRequired(); } +}; + +class AMDGPUPrintfRuntimeBindingImpl { +public: + AMDGPUPrintfRuntimeBindingImpl( + function_ref GetDT, + function_ref GetTLI) + : GetDT(GetDT), GetTLI(GetTLI) {} + bool run(Module &M); + +private: + void getConversionSpecifiers(SmallVectorImpl &OpConvSpecifiers, + StringRef fmt, size_t num_ops) const; - Value *simplify(Instruction *I, const TargetLibraryInfo *TLI) { + 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 GetDT; + function_ref GetTLI; SmallVector Printfs; }; } // namespace @@ -95,12 +107,11 @@ } } // namespace llvm -AMDGPUPrintfRuntimeBinding::AMDGPUPrintfRuntimeBinding() - : ModulePass(ID), TD(nullptr), DT(nullptr) { +AMDGPUPrintfRuntimeBinding::AMDGPUPrintfRuntimeBinding() : ModulePass(ID) { initializeAMDGPUPrintfRuntimeBindingPass(*PassRegistry::getPassRegistry()); } -void AMDGPUPrintfRuntimeBinding::getConversionSpecifiers( +void AMDGPUPrintfRuntimeBindingImpl::getConversionSpecifiers( SmallVectorImpl &OpConvSpecifiers, StringRef Fmt, size_t NumOps) const { // not all format characters are collected. @@ -132,8 +143,8 @@ } } -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(OpType); @@ -146,8 +157,7 @@ return ElemIType->getBitWidth() == 8; } -bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu( - Module &M, function_ref GetTLI) { +bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) { LLVMContext &Ctx = M.getContext(); IRBuilder<> Builder(Ctx); Type *I32Ty = Type::getInt32Ty(Ctx); @@ -172,7 +182,8 @@ } if (auto I = dyn_cast(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 @@ 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 @@ } TD = &M.getDataLayout(); - auto DTWP = getAnalysisIfAvailable(); - DT = DTWP ? &DTWP->getDomTree() : nullptr; + + return lowerPrintfForGpu(M); +} + +bool AMDGPUPrintfRuntimeBinding::runOnModule(Module &M) { + auto GetDT = [this](Function &F) -> DominatorTree & { + return this->getAnalysis(F).getDomTree(); + }; auto GetTLI = [this](Function &F) -> TargetLibraryInfo & { return this->getAnalysis().getTLI(F); }; - return lowerPrintfForGpu(M, GetTLI); + return AMDGPUPrintfRuntimeBindingImpl(GetDT, GetTLI).run(M); +} + +PreservedAnalyses +AMDGPUPrintfRuntimeBindingPass::run(Module &M, ModuleAnalysisManager &AM) { + FunctionAnalysisManager &FAM = + AM.getResult(M).getManager(); + auto GetDT = [&FAM](Function &F) -> DominatorTree & { + return FAM.getResult(F); + }; + auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & { + return FAM.getResult(F); + }; + bool Changed = AMDGPUPrintfRuntimeBindingImpl(GetDT, GetTLI).run(M); + return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); } diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp --- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp @@ -502,6 +502,10 @@ PM.addPass(AMDGPUUnifyMetadataPass()); return true; } + if (PassName == "amdgpu-printf-runtime-binding") { + PM.addPass(AMDGPUPrintfRuntimeBindingPass()); + return true; + } return false; }); PB.registerPipelineParsingCallback( @@ -552,6 +556,7 @@ return; PM.addPass(AMDGPUUnifyMetadataPass()); + PM.addPass(AMDGPUPrintfRuntimeBindingPass()); if (InternalizeSymbols) { PM.addPass(InternalizePass(mustPreserveGV)); diff --git a/llvm/test/CodeGen/AMDGPU/opencl-printf.ll b/llvm/test/CodeGen/AMDGPU/opencl-printf.ll --- a/llvm/test/CodeGen/AMDGPU/opencl-printf.ll +++ b/llvm/test/CodeGen/AMDGPU/opencl-printf.ll @@ -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 diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp --- a/llvm/tools/opt/opt.cpp +++ b/llvm/tools/opt/opt.cpp @@ -470,7 +470,8 @@ "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;