[libclc] [LIBCLC] Teach prepare-builtins how to handle text based IR (PR #66993)

Jakub Chlanda via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 21 02:52:01 PDT 2023


https://github.com/jchlanda created https://github.com/llvm/llvm-project/pull/66993

None

>From c37f854eb496937d0eb017b14bd8a9accefdab80 Mon Sep 17 00:00:00 2001
From: Jakub Chlanda <jakub at codeplay.com>
Date: Thu, 21 Sep 2023 10:48:33 +0100
Subject: [PATCH] [LIBCLC] Teach prepare-builtins how to handle text based IR

---
 libclc/utils/prepare-builtins.cpp | 37 ++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/libclc/utils/prepare-builtins.cpp b/libclc/utils/prepare-builtins.cpp
index 550b5971913f48a..ebdbc68cfee3b3c 100644
--- a/libclc/utils/prepare-builtins.cpp
+++ b/libclc/utils/prepare-builtins.cpp
@@ -5,23 +5,27 @@
 #include "llvm/Bitcode/ReaderWriter.h"
 #endif
 
+#include "llvm/Config/llvm-config.h"
 #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 ExitOnError ExitOnErr;
+
 static cl::opt<std::string>
 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
 
@@ -29,6 +33,9 @@ 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.
@@ -45,17 +52,15 @@ int main(int argc, char **argv) {
       ErrorMessage = ec.message();
     } else {
       std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
-      ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
+      SMDiagnostic Err;
+      std::unique_ptr<llvm::Module> MPtr =
 #if HAVE_LLVM > 0x0390
-          expectedToErrorOrAndEmitErrors(Context,
-          parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context));
+          ExitOnErr(Expected<std::unique_ptr<llvm::Module>>(
+              parseIR(BufferPtr.get()->getMemBufferRef(), Err, Context)));
 #else
-          parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
+          parseIR(BufferPtr.get()->getMemBufferRef(), Err, Context);
 #endif
-      if (std::error_code ec = ModuleOrErr.getError())
-        ErrorMessage = ec.message();
-
-      M = ModuleOrErr.get().release();
+      M = MPtr.release();
     }
   }
 
@@ -105,14 +110,16 @@ int main(int argc, char **argv) {
     exit(1);
   }
 
+  if (TextualOut)
+    M->print(Out->os(), nullptr, true);
+  else
 #if HAVE_LLVM >= 0x0700
-  WriteBitcodeToFile(*M, Out->os());
+    WriteBitcodeToFile(*M, Out->os());
 #else
-  WriteBitcodeToFile(M, Out->os());
+    WriteBitcodeToFile(M, Out->os());
 #endif
 
   // Declare success.
   Out->keep();
   return 0;
 }
-



More information about the cfe-commits mailing list