Index: include/llvm/Target/TargetMachine.h =================================================================== --- include/llvm/Target/TargetMachine.h +++ include/llvm/Target/TargetMachine.h @@ -217,7 +217,8 @@ /// Add target-specific function passes that should be run as early as /// possible in the optimization pipeline. Most TargetMachines have no such /// passes. - virtual void addEarlyAsPossiblePasses(PassManagerBase &) {} + virtual void addEarlyAsPossiblePasses(PassManagerBase &PM, + PassManagerType PMT = PMT_FunctionPassManager) {} /// These enums are meant to be passed into addPassesToEmitFile to indicate /// what type of file to emit, and returned by it to indicate what type of 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/AMDGPUTargetMachine.h =================================================================== --- lib/Target/AMDGPU/AMDGPUTargetMachine.h +++ lib/Target/AMDGPU/AMDGPUTargetMachine.h @@ -57,7 +57,7 @@ TargetLoweringObjectFile *getObjFileLowering() const override { return TLOF.get(); } - void addEarlyAsPossiblePasses(PassManagerBase &PM) override; + void addEarlyAsPossiblePasses(PassManagerBase &, PassManagerType) override; }; //===----------------------------------------------------------------------===// Index: lib/Target/AMDGPU/AMDGPUTargetMachine.cpp =================================================================== --- lib/Target/AMDGPU/AMDGPUTargetMachine.cpp +++ lib/Target/AMDGPU/AMDGPUTargetMachine.cpp @@ -199,7 +199,9 @@ FSAttr.getValueAsString(); } -void AMDGPUTargetMachine::addEarlyAsPossiblePasses(PassManagerBase &PM) { +void AMDGPUTargetMachine::addEarlyAsPossiblePasses(PassManagerBase &PM, + PassManagerType PMT) { + if (PMT != PMT_ModulePassManager) return; PM.add(createAMDGPUUnifyMetadataPass()); } 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.h =================================================================== --- lib/Target/NVPTX/NVPTXTargetMachine.h +++ lib/Target/NVPTX/NVPTXTargetMachine.h @@ -61,7 +61,8 @@ return TLOF.get(); } - void addEarlyAsPossiblePasses(PassManagerBase &PM) override; + void addEarlyAsPossiblePasses(PassManagerBase &PM, + PassManagerType PMT) override; TargetIRAnalysis getTargetIRAnalysis() override; }; // NVPTXTargetMachine. Index: lib/Target/NVPTX/NVPTXTargetMachine.cpp =================================================================== --- lib/Target/NVPTX/NVPTXTargetMachine.cpp +++ lib/Target/NVPTX/NVPTXTargetMachine.cpp @@ -176,7 +176,9 @@ return new NVPTXPassConfig(this, PM); } -void NVPTXTargetMachine::addEarlyAsPossiblePasses(PassManagerBase &PM) { +void NVPTXTargetMachine::addEarlyAsPossiblePasses(PassManagerBase &PM, + PassManagerType PMT) { + if (PMT != PMT_FunctionPassManager) return; PM.add(createNVVMReflectPass()); PM.add(createNVVMIntrRangePass(Subtarget.getSmVersion())); } 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 @@ -288,13 +288,20 @@ DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; // Add target-specific passes that need to run as early as possible. - if (TM) + if (TM) { Builder.addExtension( PassManagerBuilder::EP_EarlyAsPossible, [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) { - TM->addEarlyAsPossiblePasses(PM); + TM->addEarlyAsPossiblePasses(PM, PMT_FunctionPassManager); }); + Builder.addExtension( + PassManagerBuilder::EP_ModuleOptimizerEarly, + [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) { + TM->addEarlyAsPossiblePasses(PM, PMT_ModulePassManager); + }); + } + if (Coroutines) addCoroutinePassesToExtensionPoints(Builder);