Skip to content

[SYCL][LIBCLC] Accept text-based IR in prepare builtins utils #11230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 27 additions & 40 deletions libclc/utils/prepare-builtins.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
#if HAVE_LLVM > 0x0390
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#else
#include "llvm/Bitcode/ReaderWriter.h"
#endif

#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/raw_ostream.h"

#include <system_error>

using namespace llvm;

static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
static ExitOnError ExitOnErr;

static cl::opt<std::string>
OutputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"));
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));

static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"));

static cl::opt<bool> TextualOut("S", cl::desc("Emit LLVM textual assembly"),
cl::init(false));

int main(int argc, char **argv) {
LLVMContext Context;
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.

cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");

Expand All @@ -40,22 +40,16 @@ int main(int argc, char **argv) {

{
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFile(InputFilename);
if (std::error_code ec = BufferOrErr.getError()) {
ErrorMessage = ec.message();
MemoryBuffer::getFile(InputFilename);
if (std::error_code Ec = BufferOrErr.getError()) {
ErrorMessage = Ec.message();
} else {
std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
#if HAVE_LLVM > 0x0390
expectedToErrorOrAndEmitErrors(Context,
parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context));
#else
parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
#endif
if (std::error_code ec = ModuleOrErr.getError())
ErrorMessage = ec.message();

M = ModuleOrErr.get().release();
SMDiagnostic Err;
std::unique_ptr<llvm::Module> MPtr =
ExitOnErr(Expected<std::unique_ptr<llvm::Module>>(
parseIR(BufferPtr.get()->getMemBufferRef(), Err, Context)));
M = MPtr.release();
}
}

Expand All @@ -73,7 +67,7 @@ int main(int argc, char **argv) {
// version. This may also report a different version than the user
// program is using. This should probably be uniqued when linking.
if (NamedMDNode *OCLVersion = M->getNamedMetadata("opencl.ocl.version"))
M->eraseNamedMetadata(OCLVersion);
M->eraseNamedMetadata(OCLVersion);

// wchar_size flag can cause a mismatch between libclc libraries and
// modules using them. Since wchar is not used by libclc we drop the flag
Expand Down Expand Up @@ -105,26 +99,19 @@ int main(int argc, char **argv) {
}

std::error_code EC;
#if HAVE_LLVM >= 0x0600
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
#else
std::unique_ptr<tool_output_file> Out(
new tool_output_file(OutputFilename, EC, sys::fs::OF_None));
#endif
if (EC) {
errs() << EC.message() << '\n';
exit(1);
}

#if HAVE_LLVM >= 0x0700
WriteBitcodeToFile(*M, Out->os());
#else
WriteBitcodeToFile(M, Out->os());
#endif
if (TextualOut)
M->print(Out->os(), nullptr, true);
else
WriteBitcodeToFile(*M, Out->os());

// Declare success.
Out->keep();
return 0;
}