Index: lib/Target/AMDGPU/AMDGPU.h =================================================================== --- lib/Target/AMDGPU/AMDGPU.h +++ lib/Target/AMDGPU/AMDGPU.h @@ -90,7 +90,7 @@ ModulePass *createAMDGPUOpenCLImageTypeLoweringPass(); FunctionPass *createAMDGPUAnnotateUniformValues(); -FunctionPass* createAMDGPUUnifyMetadataPass(); +ModulePass* createAMDGPUUnifyMetadataPass(); void initializeAMDGPUUnifyMetadataPass(PassRegistry&); extern char &AMDGPUUnifyMetadataID; Index: lib/Target/AMDGPU/AMDGPUUnifyMetadata.cpp =================================================================== --- lib/Target/AMDGPU/AMDGPUUnifyMetadata.cpp +++ lib/Target/AMDGPU/AMDGPUUnifyMetadata.cpp @@ -30,21 +30,14 @@ } /// \brief Unify multiple OpenCL metadata due to linking. - class AMDGPUUnifyMetadata : public FunctionPass { + class AMDGPUUnifyMetadata : public ModulePass { public: static char ID; - explicit AMDGPUUnifyMetadata() : FunctionPass(ID) {}; + explicit AMDGPUUnifyMetadata() : ModulePass(ID) {}; private: - // This should really be a module pass but we have to run it as early - // as possible, so given function passes are executed first and - // TargetMachine::addEarlyAsPossiblePasses() expects only function passes - // it has to be a function pass. virtual bool runOnModule(Module &M); - // \todo: Convert to a module pass. - virtual bool runOnFunction(Function &F); - /// \brief Unify version metadata. /// \return true if changes are made. /// Assume the named metadata has operands each of which is a pair of @@ -117,7 +110,7 @@ "Unify multiple OpenCL metadata due to linking", false, false) -FunctionPass* llvm::createAMDGPUUnifyMetadataPass() { +ModulePass* llvm::createAMDGPUUnifyMetadataPass() { return new AMDGPUUnifyMetadata(); } @@ -143,7 +136,3 @@ return Changed; } - -bool AMDGPUUnifyMetadata::runOnFunction(Function &F) { - return runOnModule(*F.getParent()); -} Index: lib/Target/NVPTX/NVPTXTargetMachine.cpp =================================================================== --- lib/Target/NVPTX/NVPTXTargetMachine.cpp +++ lib/Target/NVPTX/NVPTXTargetMachine.cpp @@ -170,15 +170,43 @@ // Add passes that perform straight-line scalar optimizations. void addStraightLineScalarOptimizationPasses(); }; + +class NVPTXEarlyAsPossibleFunctionWrapperPass : public ModulePass { +public: + static char ID; + NVPTXEarlyAsPossibleFunctionWrapperPass(NVPTXTargetMachine &TM_) : + ModulePass(ID), TM(TM_) {} + + bool runOnModule(Module &M) override; + +private: + NVPTXTargetMachine &TM; +}; } // end anonymous namespace TargetPassConfig *NVPTXTargetMachine::createPassConfig(PassManagerBase &PM) { return new NVPTXPassConfig(this, PM); } +char NVPTXEarlyAsPossibleFunctionWrapperPass::ID = 0; + +bool NVPTXEarlyAsPossibleFunctionWrapperPass::runOnModule(Module &M) { + legacy::FunctionPassManager *FPM = new legacy::FunctionPassManager(&M); + + FPM->add(createNVVMReflectPass()); + FPM->add(createNVVMIntrRangePass(TM.getSubtargetImpl()->getSmVersion())); + + FPM->doInitialization(); + for (auto &F : M.functions()) + FPM->run(F); + FPM->doFinalization(); + delete FPM; + + return true; +} + void NVPTXTargetMachine::addEarlyAsPossiblePasses(PassManagerBase &PM) { - PM.add(createNVVMReflectPass()); - PM.add(createNVVMIntrRangePass(Subtarget.getSmVersion())); + PM.add(new NVPTXEarlyAsPossibleFunctionWrapperPass(*this)); } TargetIRAnalysis NVPTXTargetMachine::getTargetIRAnalysis() { Index: lib/Target/NVPTX/NVVMIntrRange.cpp =================================================================== --- lib/Target/NVPTX/NVVMIntrRange.cpp +++ lib/Target/NVPTX/NVVMIntrRange.cpp @@ -55,7 +55,8 @@ } FunctionPass *llvm::createNVVMIntrRangePass(unsigned int SmVersion) { - return new NVVMIntrRange(SmVersion); + return new NVVMIntrRange((NVVMIntrRangeSM.getNumOccurrences() > 0) ? + NVVMIntrRangeSM : SmVersion); } char NVVMIntrRange::ID = 0; Index: test/CodeGen/AMDGPU/unify-metadata.ll =================================================================== --- test/CodeGen/AMDGPU/unify-metadata.ll +++ test/CodeGen/AMDGPU/unify-metadata.ll @@ -14,10 +14,6 @@ ; ALL-DAG: ![[USED_EXT_1]] = !{!"cl_khr_fp16"} ; ALL-DAG: ![[USED_EXT_2]] = !{!"cl_doubles"} -define void @test() { - ret void -} - !opencl.ocl.version = !{!1, !0, !0, !0} !llvm.ident = !{!2, !2, !2, !2, !6} !opencl.used.extensions = !{!3, !3, !4, !5} Index: tools/opt/opt.cpp =================================================================== --- tools/opt/opt.cpp +++ tools/opt/opt.cpp @@ -287,14 +287,6 @@ Builder.SLPVectorize = DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; - // Add target-specific passes that need to run as early as possible. - if (TM) - Builder.addExtension( - PassManagerBuilder::EP_EarlyAsPossible, - [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) { - TM->addEarlyAsPossiblePasses(PM); - }); - if (Coroutines) addCoroutinePassesToExtensionPoints(Builder); @@ -674,6 +666,13 @@ if (OptLevelO3) AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); + // Add target-specific passes that need to run as early as possible. + if (TM) { + legacy::PassManager EarlyPasses; + TM->addEarlyAsPossiblePasses(EarlyPasses); + EarlyPasses.run(*M); + } + if (FPasses) { FPasses->doInitialization(); for (Function &F : *M)