Skip to content

Commit dc9e645

Browse files
committed
[NFC] Use only one PassManager in SPIRVWriter
Factor out common code, in preparation of transitioning SPIRVWriter to the new PassManager.
1 parent 583d365 commit dc9e645

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5066,35 +5066,54 @@ bool isValidLLVMModule(Module *M, SPIRVErrorLog &ErrorLog) {
50665066
return true;
50675067
}
50685068

5069-
bool llvm::writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg) {
5070-
SPIRV::TranslatorOpts DefaultOpts;
5071-
// To preserve old behavior of the translator, let's enable all extensions
5072-
// by default in this API
5073-
DefaultOpts.enableAllExtensions();
5074-
return llvm::writeSpirv(M, DefaultOpts, OS, ErrMsg);
5075-
}
5069+
namespace {
5070+
5071+
bool runSpirvWriterPasses(Module *M, std::ostream *OS, std::string &ErrMsg,
5072+
const SPIRV::TranslatorOpts &Opts) {
5073+
// Perform the conversion and write the resulting SPIR-V if an ostream has
5074+
// been given; otherwise only perform regularization.
5075+
bool WriteSpirv = OS != nullptr;
50765076

5077-
bool llvm::writeSpirv(Module *M, const SPIRV::TranslatorOpts &Opts,
5078-
std::ostream &OS, std::string &ErrMsg) {
50795077
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule(Opts));
50805078
if (!isValidLLVMModule(M, BM->getErrorLog()))
50815079
return false;
50825080

50835081
legacy::PassManager PassMgr;
50845082
addPassesForSPIRV(PassMgr, Opts);
5085-
// Run loop simplify pass in order to avoid duplicate OpLoopMerge
5086-
// instruction. It can happen in case of continue operand in the loop.
5087-
if (hasLoopMetadata(M))
5088-
PassMgr.add(createLoopSimplifyPass());
5089-
PassMgr.add(createLLVMToSPIRVLegacy(BM.get()));
5083+
if (WriteSpirv) {
5084+
// Run loop simplify pass in order to avoid duplicate OpLoopMerge
5085+
// instruction. It can happen in case of continue operand in the loop.
5086+
if (hasLoopMetadata(M))
5087+
PassMgr.add(createLoopSimplifyPass());
5088+
PassMgr.add(createLLVMToSPIRVLegacy(BM.get()));
5089+
}
5090+
50905091
PassMgr.run(*M);
50915092

50925093
if (BM->getError(ErrMsg) != SPIRVEC_Success)
50935094
return false;
5094-
OS << *BM;
5095+
5096+
if (WriteSpirv)
5097+
*OS << *BM;
5098+
50955099
return true;
50965100
}
50975101

5102+
} // namespace
5103+
5104+
bool llvm::writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg) {
5105+
SPIRV::TranslatorOpts DefaultOpts;
5106+
// To preserve old behavior of the translator, let's enable all extensions
5107+
// by default in this API
5108+
DefaultOpts.enableAllExtensions();
5109+
return llvm::writeSpirv(M, DefaultOpts, OS, ErrMsg);
5110+
}
5111+
5112+
bool llvm::writeSpirv(Module *M, const SPIRV::TranslatorOpts &Opts,
5113+
std::ostream &OS, std::string &ErrMsg) {
5114+
return runSpirvWriterPasses(M, &OS, ErrMsg, Opts);
5115+
}
5116+
50985117
bool llvm::regularizeLlvmForSpirv(Module *M, std::string &ErrMsg) {
50995118
SPIRV::TranslatorOpts DefaultOpts;
51005119
// To preserve old behavior of the translator, let's enable all extensions
@@ -5105,12 +5124,5 @@ bool llvm::regularizeLlvmForSpirv(Module *M, std::string &ErrMsg) {
51055124

51065125
bool llvm::regularizeLlvmForSpirv(Module *M, std::string &ErrMsg,
51075126
const SPIRV::TranslatorOpts &Opts) {
5108-
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
5109-
if (!isValidLLVMModule(M, BM->getErrorLog()))
5110-
return false;
5111-
5112-
legacy::PassManager PassMgr;
5113-
addPassesForSPIRV(PassMgr, Opts);
5114-
PassMgr.run(*M);
5115-
return true;
5127+
return runSpirvWriterPasses(M, nullptr, ErrMsg, Opts);
51165128
}

0 commit comments

Comments
 (0)