Skip to content

Commit 5ec9faf

Browse files
authored
[LIBCLC] Teach prepare-builtins how to handle text based IR (#66993)
1 parent c0f8748 commit 5ec9faf

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

libclc/utils/prepare-builtins.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,37 @@
55
#include "llvm/Bitcode/ReaderWriter.h"
66
#endif
77

8+
#include "llvm/Config/llvm-config.h"
89
#include "llvm/IR/Function.h"
910
#include "llvm/IR/GlobalVariable.h"
1011
#include "llvm/IR/LLVMContext.h"
1112
#include "llvm/IR/Module.h"
13+
#include "llvm/IRReader/IRReader.h"
1214
#include "llvm/Support/CommandLine.h"
15+
#include "llvm/Support/ErrorOr.h"
16+
#include "llvm/Support/FileSystem.h"
1317
#include "llvm/Support/ManagedStatic.h"
1418
#include "llvm/Support/MemoryBuffer.h"
15-
#include "llvm/Support/FileSystem.h"
16-
#include "llvm/Support/raw_ostream.h"
17-
#include "llvm/Support/ErrorOr.h"
19+
#include "llvm/Support/SourceMgr.h"
1820
#include "llvm/Support/ToolOutputFile.h"
19-
#include "llvm/Config/llvm-config.h"
21+
#include "llvm/Support/raw_ostream.h"
2022

2123
#include <system_error>
2224

2325
using namespace llvm;
2426

27+
static ExitOnError ExitOnErr;
28+
2529
static cl::opt<std::string>
2630
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
2731

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

36+
static cl::opt<bool> TextualOut("S", cl::desc("Emit LLVM textual assembly"),
37+
cl::init(false));
38+
3239
int main(int argc, char **argv) {
3340
LLVMContext Context;
3441
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
@@ -45,17 +52,15 @@ int main(int argc, char **argv) {
4552
ErrorMessage = ec.message();
4653
} else {
4754
std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
48-
ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
55+
SMDiagnostic Err;
56+
std::unique_ptr<llvm::Module> MPtr =
4957
#if HAVE_LLVM > 0x0390
50-
expectedToErrorOrAndEmitErrors(Context,
51-
parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context));
58+
ExitOnErr(Expected<std::unique_ptr<llvm::Module>>(
59+
parseIR(BufferPtr.get()->getMemBufferRef(), Err, Context)));
5260
#else
53-
parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
61+
parseIR(BufferPtr.get()->getMemBufferRef(), Err, Context);
5462
#endif
55-
if (std::error_code ec = ModuleOrErr.getError())
56-
ErrorMessage = ec.message();
57-
58-
M = ModuleOrErr.get().release();
63+
M = MPtr.release();
5964
}
6065
}
6166

@@ -105,14 +110,16 @@ int main(int argc, char **argv) {
105110
exit(1);
106111
}
107112

113+
if (TextualOut)
114+
M->print(Out->os(), nullptr, true);
115+
else
108116
#if HAVE_LLVM >= 0x0700
109-
WriteBitcodeToFile(*M, Out->os());
117+
WriteBitcodeToFile(*M, Out->os());
110118
#else
111-
WriteBitcodeToFile(M, Out->os());
119+
WriteBitcodeToFile(M, Out->os());
112120
#endif
113121

114122
// Declare success.
115123
Out->keep();
116124
return 0;
117125
}
118-

0 commit comments

Comments
 (0)