Skip to content

Commit

Permalink
Remove the _SPIRV_LLVM_API switch
Browse files Browse the repository at this point in the history
The default is changed from llvm::raw_ostream to std::ostream.

Fixes KhronosGroup#9
  • Loading branch information
pierremoreau authored and StuartDBrady committed Jul 11, 2019
1 parent 0b93b98 commit cd60cc8
Show file tree
Hide file tree
Showing 23 changed files with 23 additions and 58 deletions.
6 changes: 3 additions & 3 deletions include/LLVMSPIRVLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions lib/SPIRV/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/OCL20To12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/OCL20ToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
#include "llvm/Pass.h"
#include "llvm/PassSupport.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"

#include <algorithm>
#include <set>
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/OCL21ToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#include "llvm/Pass.h"
#include "llvm/PassSupport.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"

#include <set>

Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/OCLTypeToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#include "llvm/Pass.h"
#include "llvm/PassSupport.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"

#include <iterator>
#include <set>
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/OCLUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/SPIRVLowerBool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/SPIRVLowerConstExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <list>
#include <set>
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/SPIRVLowerMemmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/SPIRVLowerSPIRBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
#include <cstdlib>
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/SPIRVRegularizeLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <set>

Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/SPIRVToOCL20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstring>

Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/SPIRVUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <functional>
#include <sstream>
Expand Down
3 changes: 1 addition & 2 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdlib>
Expand Down Expand Up @@ -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<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
legacy::PassManager PassMgr;
addPassesForSPIRV(PassMgr);
Expand Down
6 changes: 3 additions & 3 deletions lib/SPIRV/SPIRVWriterPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"; }

Expand All @@ -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);
}
7 changes: 3 additions & 4 deletions lib/SPIRV/SPIRVWriterPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion lib/SPIRV/TransOCLMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 0 additions & 7 deletions lib/SPIRV/libSPIRV/SPIRVDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
#define SPIRV_LIBSPIRV_SPIRVDEBUG_H

#include "SPIRVUtil.h"
#ifdef _SPIRV_LLVM_API
#include "llvm/Support/Debug.h"
#endif
#include <iostream>

namespace SPIRV {
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions lib/SPIRV/libSPIRV/SPIRVModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 0 additions & 5 deletions lib/SPIRV/libSPIRV/SPIRVUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ostream>
#define spv_ostream std::ostream
#endif

#include <algorithm>
#include <cassert>
Expand Down
22 changes: 12 additions & 10 deletions tools/llvm-spirv/llvm-spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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';
Expand All @@ -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

Expand Down

0 comments on commit cd60cc8

Please sign in to comment.