From cd60cc8817f8deeceb1d3161e1ed4625a326121c Mon Sep 17 00:00:00 2001 From: Pierre Moreau Date: Sat, 9 Jun 2018 15:12:08 +0200 Subject: [PATCH] Remove the _SPIRV_LLVM_API switch The default is changed from llvm::raw_ostream to std::ostream. Fixes https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/9 --- include/LLVMSPIRVLib.h | 6 +++--- lib/SPIRV/CMakeLists.txt | 5 ----- lib/SPIRV/OCL20To12.cpp | 1 - lib/SPIRV/OCL20ToSPIRV.cpp | 1 - lib/SPIRV/OCL21ToSPIRV.cpp | 1 - lib/SPIRV/OCLTypeToSPIRV.cpp | 1 - lib/SPIRV/OCLUtil.cpp | 1 - lib/SPIRV/SPIRVLowerBool.cpp | 1 - lib/SPIRV/SPIRVLowerConstExpr.cpp | 1 - lib/SPIRV/SPIRVLowerMemmove.cpp | 1 - lib/SPIRV/SPIRVLowerSPIRBlocks.cpp | 1 - lib/SPIRV/SPIRVReader.cpp | 1 - lib/SPIRV/SPIRVRegularizeLLVM.cpp | 1 - lib/SPIRV/SPIRVToOCL20.cpp | 1 - lib/SPIRV/SPIRVUtil.cpp | 1 - lib/SPIRV/SPIRVWriter.cpp | 3 +-- lib/SPIRV/SPIRVWriterPass.cpp | 6 +++--- lib/SPIRV/SPIRVWriterPass.h | 7 +++---- lib/SPIRV/TransOCLMD.cpp | 1 - lib/SPIRV/libSPIRV/SPIRVDebug.h | 7 ------- lib/SPIRV/libSPIRV/SPIRVModule.cpp | 6 +----- lib/SPIRV/libSPIRV/SPIRVUtil.h | 5 ----- tools/llvm-spirv/llvm-spirv.cpp | 22 ++++++++++++---------- 23 files changed, 23 insertions(+), 58 deletions(-) diff --git a/include/LLVMSPIRVLib.h b/include/LLVMSPIRVLib.h index 426a6b8cad..aacf8eec38 100644 --- a/include/LLVMSPIRVLib.h +++ b/include/LLVMSPIRVLib.h @@ -76,7 +76,7 @@ bool isSpirvBinary(std::string &Img); /// This function is not thread safe and should not be used in multi-thread /// applications unless guarded by a critical section. /// \returns true if succeeds. -bool convertSpirv(std::istream &IS, llvm::raw_ostream &OS, std::string &ErrMsg, +bool convertSpirv(std::istream &IS, std::ostream &OS, std::string &ErrMsg, bool FromText, bool ToText); /// \brief Convert SPIR-V between binary and internel text formats. @@ -95,7 +95,7 @@ namespace llvm { /// \brief Translate LLVM module to SPIRV and write to ostream. /// \returns true if succeeds. -bool writeSpirv(Module *M, raw_ostream &OS, std::string &ErrMsg); +bool writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg); /// \brief Load SPIRV from istream and translate to LLVM module. /// \returns true if succeeds. @@ -157,7 +157,7 @@ ModulePass *createTransOCLMD(); /// Create and return a pass that writes the module to the specified /// ostream. -ModulePass *createSPIRVWriterPass(raw_ostream &Str); +ModulePass *createSPIRVWriterPass(std::ostream &Str); } // namespace llvm diff --git a/lib/SPIRV/CMakeLists.txt b/lib/SPIRV/CMakeLists.txt index 26f81ca690..b4163066be 100644 --- a/lib/SPIRV/CMakeLists.txt +++ b/lib/SPIRV/CMakeLists.txt @@ -1,8 +1,3 @@ -option(SPIRV_USE_LLVM_API "Enable usage of LLVM API for libSPIRV." ON) -if(SPIRV_USE_LLVM_API) - add_definitions(-D_SPIRV_LLVM_API) -endif(SPIRV_USE_LLVM_API) - add_llvm_library(LLVMSPIRVLib LLVMToSPIRVDbgTran.cpp Mangler/FunctionDescriptor.cpp diff --git a/lib/SPIRV/OCL20To12.cpp b/lib/SPIRV/OCL20To12.cpp index 873d104537..69383bceba 100644 --- a/lib/SPIRV/OCL20To12.cpp +++ b/lib/SPIRV/OCL20To12.cpp @@ -48,7 +48,6 @@ #include "llvm/PassSupport.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/raw_ostream.h" using namespace llvm; using namespace SPIRV; diff --git a/lib/SPIRV/OCL20ToSPIRV.cpp b/lib/SPIRV/OCL20ToSPIRV.cpp index ed3d780c2b..fafc92510a 100644 --- a/lib/SPIRV/OCL20ToSPIRV.cpp +++ b/lib/SPIRV/OCL20ToSPIRV.cpp @@ -51,7 +51,6 @@ #include "llvm/Pass.h" #include "llvm/PassSupport.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" #include #include diff --git a/lib/SPIRV/OCL21ToSPIRV.cpp b/lib/SPIRV/OCL21ToSPIRV.cpp index fb3f17db8b..0cfc0b69ef 100644 --- a/lib/SPIRV/OCL21ToSPIRV.cpp +++ b/lib/SPIRV/OCL21ToSPIRV.cpp @@ -47,7 +47,6 @@ #include "llvm/Pass.h" #include "llvm/PassSupport.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" #include diff --git a/lib/SPIRV/OCLTypeToSPIRV.cpp b/lib/SPIRV/OCLTypeToSPIRV.cpp index 240bc7d668..2ef841bbb3 100644 --- a/lib/SPIRV/OCLTypeToSPIRV.cpp +++ b/lib/SPIRV/OCLTypeToSPIRV.cpp @@ -47,7 +47,6 @@ #include "llvm/Pass.h" #include "llvm/PassSupport.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" #include #include diff --git a/lib/SPIRV/OCLUtil.cpp b/lib/SPIRV/OCLUtil.cpp index 1572f949ce..9d25aac159 100644 --- a/lib/SPIRV/OCLUtil.cpp +++ b/lib/SPIRV/OCLUtil.cpp @@ -51,7 +51,6 @@ #include "llvm/PassSupport.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" using namespace llvm; using namespace SPIRV; diff --git a/lib/SPIRV/SPIRVLowerBool.cpp b/lib/SPIRV/SPIRVLowerBool.cpp index 6a044cb774..ac7819fc49 100644 --- a/lib/SPIRV/SPIRVLowerBool.cpp +++ b/lib/SPIRV/SPIRVLowerBool.cpp @@ -46,7 +46,6 @@ #include "llvm/PassSupport.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" using namespace llvm; using namespace SPIRV; diff --git a/lib/SPIRV/SPIRVLowerConstExpr.cpp b/lib/SPIRV/SPIRVLowerConstExpr.cpp index 0b4158e21e..ac25b31f87 100644 --- a/lib/SPIRV/SPIRVLowerConstExpr.cpp +++ b/lib/SPIRV/SPIRVLowerConstExpr.cpp @@ -52,7 +52,6 @@ #include "llvm/PassSupport.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" #include #include diff --git a/lib/SPIRV/SPIRVLowerMemmove.cpp b/lib/SPIRV/SPIRVLowerMemmove.cpp index 04be327db8..84a939cb51 100644 --- a/lib/SPIRV/SPIRVLowerMemmove.cpp +++ b/lib/SPIRV/SPIRVLowerMemmove.cpp @@ -47,7 +47,6 @@ #include "llvm/PassSupport.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" using namespace llvm; using namespace SPIRV; diff --git a/lib/SPIRV/SPIRVLowerSPIRBlocks.cpp b/lib/SPIRV/SPIRVLowerSPIRBlocks.cpp index 91d2aaded2..a12947ff16 100644 --- a/lib/SPIRV/SPIRVLowerSPIRBlocks.cpp +++ b/lib/SPIRV/SPIRVLowerSPIRBlocks.cpp @@ -57,7 +57,6 @@ #include "llvm/PassSupport.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/Utils/GlobalStatus.h" #include diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index eeceb30578..63889a7009 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -65,7 +65,6 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/raw_ostream.h" #include #include diff --git a/lib/SPIRV/SPIRVRegularizeLLVM.cpp b/lib/SPIRV/SPIRVRegularizeLLVM.cpp index 84f7492855..7df4e89466 100644 --- a/lib/SPIRV/SPIRVRegularizeLLVM.cpp +++ b/lib/SPIRV/SPIRVRegularizeLLVM.cpp @@ -52,7 +52,6 @@ #include "llvm/PassSupport.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" #include diff --git a/lib/SPIRV/SPIRVToOCL20.cpp b/lib/SPIRV/SPIRVToOCL20.cpp index 273b88ed76..ccce0b075b 100644 --- a/lib/SPIRV/SPIRVToOCL20.cpp +++ b/lib/SPIRV/SPIRVToOCL20.cpp @@ -48,7 +48,6 @@ #include "llvm/PassSupport.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" #include diff --git a/lib/SPIRV/SPIRVUtil.cpp b/lib/SPIRV/SPIRVUtil.cpp index 9f730e155f..61e68f690d 100644 --- a/lib/SPIRV/SPIRVUtil.cpp +++ b/lib/SPIRV/SPIRVUtil.cpp @@ -56,7 +56,6 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/ToolOutputFile.h" -#include "llvm/Support/raw_ostream.h" #include #include diff --git a/lib/SPIRV/SPIRVWriter.cpp b/lib/SPIRV/SPIRVWriter.cpp index 9549f69a3a..0f2458b2af 100644 --- a/lib/SPIRV/SPIRVWriter.cpp +++ b/lib/SPIRV/SPIRVWriter.cpp @@ -73,7 +73,6 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ToolOutputFile.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Utils.h" // loop-simplify pass #include @@ -1768,7 +1767,7 @@ void addPassesForSPIRV(legacy::PassManager &PassMgr) { PassMgr.add(createSPIRVLowerMemmove()); } -bool llvm::writeSpirv(Module *M, llvm::raw_ostream &OS, std::string &ErrMsg) { +bool llvm::writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg) { std::unique_ptr BM(SPIRVModule::createSPIRVModule()); legacy::PassManager PassMgr; addPassesForSPIRV(PassMgr); diff --git a/lib/SPIRV/SPIRVWriterPass.cpp b/lib/SPIRV/SPIRVWriterPass.cpp index 56ed29daf5..ffd0676045 100644 --- a/lib/SPIRV/SPIRVWriterPass.cpp +++ b/lib/SPIRV/SPIRVWriterPass.cpp @@ -27,10 +27,10 @@ PreservedAnalyses SPIRVWriterPass::run(Module &M) { namespace { class WriteSPIRVPass : public ModulePass { - raw_ostream &OS; // raw_ostream to print on + std::ostream &OS; // std::ostream to print on public: static char ID; // Pass identification, replacement for typeid - explicit WriteSPIRVPass(raw_ostream &O) : ModulePass(ID), OS(O) {} + explicit WriteSPIRVPass(std::ostream &O) : ModulePass(ID), OS(O) {} StringRef getPassName() const override { return "SPIRV Writer"; } @@ -45,6 +45,6 @@ class WriteSPIRVPass : public ModulePass { char WriteSPIRVPass::ID = 0; -ModulePass *llvm::createSPIRVWriterPass(raw_ostream &Str) { +ModulePass *llvm::createSPIRVWriterPass(std::ostream &Str) { return new WriteSPIRVPass(Str); } diff --git a/lib/SPIRV/SPIRVWriterPass.h b/lib/SPIRV/SPIRVWriterPass.h index 5fab0a07a0..b8005486bb 100644 --- a/lib/SPIRV/SPIRVWriterPass.h +++ b/lib/SPIRV/SPIRVWriterPass.h @@ -20,24 +20,23 @@ namespace llvm { class Module; class ModulePass; -class raw_ostream; class PreservedAnalyses; /// \brief Create and return a pass that writes the module to the specified /// ostream. Note that this pass is designed for use with the legacy pass /// manager. -ModulePass *createSPIRVWriterPass(raw_ostream &Str); +ModulePass *createSPIRVWriterPass(std::ostream &Str); /// \brief Pass for writing a module of IR out to a SPIRV file. /// /// Note that this is intended for use with the new pass manager. To construct /// a pass for the legacy pass manager, use the function above. class SPIRVWriterPass { - raw_ostream &OS; + std::ostream &OS; public: /// \brief Construct a SPIRV writer pass around a particular output stream. - explicit SPIRVWriterPass(raw_ostream &OS) : OS(OS) {} + explicit SPIRVWriterPass(std::ostream &OS) : OS(OS) {} /// \brief Run the SPIRV writer pass, and output the module to the selected /// output stream. diff --git a/lib/SPIRV/TransOCLMD.cpp b/lib/SPIRV/TransOCLMD.cpp index 3dc303e140..b03f2333ae 100644 --- a/lib/SPIRV/TransOCLMD.cpp +++ b/lib/SPIRV/TransOCLMD.cpp @@ -50,7 +50,6 @@ #include "llvm/PassSupport.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/raw_ostream.h" using namespace llvm; using namespace SPIRV; diff --git a/lib/SPIRV/libSPIRV/SPIRVDebug.h b/lib/SPIRV/libSPIRV/SPIRVDebug.h index c0f394ebe4..13b254a575 100644 --- a/lib/SPIRV/libSPIRV/SPIRVDebug.h +++ b/lib/SPIRV/libSPIRV/SPIRVDebug.h @@ -41,9 +41,6 @@ #define SPIRV_LIBSPIRV_SPIRVDEBUG_H #include "SPIRVUtil.h" -#ifdef _SPIRV_LLVM_API -#include "llvm/Support/Debug.h" -#endif #include namespace SPIRV { @@ -67,11 +64,7 @@ extern bool SPIRVDbgAbortOnError; // Output stream for SPIRV debug information. inline spv_ostream &spvdbgs() { -#ifdef _SPIRV_LLVM_API - return llvm::dbgs(); -#else return std::cerr; -#endif } #else diff --git a/lib/SPIRV/libSPIRV/SPIRVModule.cpp b/lib/SPIRV/libSPIRV/SPIRVModule.cpp index dcf787adf8..912c0b9ccf 100644 --- a/lib/SPIRV/libSPIRV/SPIRVModule.cpp +++ b/lib/SPIRV/libSPIRV/SPIRVModule.cpp @@ -1664,7 +1664,7 @@ bool isSpirvBinary(const std::string &Img) { #ifdef _SPIRV_SUPPORT_TEXT_FMT -bool convertSpirv(std::istream &IS, spv_ostream &OS, std::string &ErrMsg, +bool convertSpirv(std::istream &IS, std::ostream &OS, std::string &ErrMsg, bool FromText, bool ToText) { auto SaveOpt = SPIRVUseTextFormat; SPIRVUseTextFormat = FromText; @@ -1701,11 +1701,7 @@ bool convertSpirv(std::string &Input, std::string &Out, std::string &ErrMsg, return true; } std::istringstream IS(Input); -#ifdef _SPIRV_LLVM_API - llvm::raw_string_ostream OS(Out); -#else std::ostringstream OS; -#endif if (!convertSpirv(IS, OS, ErrMsg, FromText, ToText)) return false; Out = OS.str(); diff --git a/lib/SPIRV/libSPIRV/SPIRVUtil.h b/lib/SPIRV/libSPIRV/SPIRVUtil.h index fe90af08fe..794b76cf2f 100644 --- a/lib/SPIRV/libSPIRV/SPIRVUtil.h +++ b/lib/SPIRV/libSPIRV/SPIRVUtil.h @@ -40,13 +40,8 @@ #ifndef SPIRV_LIBSPIRV_SPIRVUTIL_H #define SPIRV_LIBSPIRV_SPIRVUTIL_H -#ifdef _SPIRV_LLVM_API -#include "llvm/Support/raw_ostream.h" -#define spv_ostream llvm::raw_ostream -#else #include #define spv_ostream std::ostream -#endif #include #include diff --git a/tools/llvm-spirv/llvm-spirv.cpp b/tools/llvm-spirv/llvm-spirv.cpp index 8ca1e4e643..537dc0e0d4 100644 --- a/tools/llvm-spirv/llvm-spirv.cpp +++ b/tools/llvm-spirv/llvm-spirv.cpp @@ -58,7 +58,6 @@ #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Signals.h" #include "llvm/Support/ToolOutputFile.h" -#include "llvm/Support/raw_ostream.h" #ifndef _SPIRV_SUPPORT_TEXT_FMT #define _SPIRV_SUPPORT_TEXT_FMT @@ -137,11 +136,16 @@ static int convertLLVMToSPIRV() { (SPIRV::SPIRVUseTextFormat ? kExt::SpirvText : kExt::SpirvBinary); } - llvm::StringRef OutFile(OutputFile); - std::error_code EC; std::string Err; - llvm::raw_fd_ostream OFS(OutFile, EC, llvm::sys::fs::F_None); - if (!writeSpirv(M.get(), OFS, Err)) { + bool Success = false; + if (OutputFile != "-") { + std::ofstream OutFile(OutputFile); + Success = writeSpirv(M.get(), OutFile, Err); + } else { + Success = writeSpirv(M.get(), std::cout, Err); + } + + if (!Success) { errs() << "Fails to save LLVM as SPIRV: " << Err << '\n'; return -1; } @@ -204,7 +208,7 @@ static int convertSPIRV() { } } - auto Action = [&](llvm::raw_ostream &OFS) { + auto Action = [&](std::ostream &OFS) { std::string Err; if (!SPIRV::convertSpirv(IFS, OFS, Err, ToBinary, ToText)) { errs() << "Fails to convert SPIR-V : " << Err << '\n'; @@ -213,12 +217,10 @@ static int convertSPIRV() { return 0; }; if (OutputFile != "-") { - std::error_code EC; - llvm::raw_fd_ostream OFS(llvm::StringRef(OutputFile), EC, - llvm::sys::fs::F_None); + std::ofstream OFS(OutputFile); return Action(OFS); } else - return Action(outs()); + return Action(std::cout); } #endif