[Mesa-dev] [PATCH 04/10] clover: Register an llvm diagnostic handler to catch internal compiler errors

Tom Stellard thomas.stellard at amd.com
Mon Oct 6 12:44:26 PDT 2014


---
 .../state_trackers/clover/llvm/invocation.cpp      | 30 ++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp b/src/gallium/state_trackers/clover/llvm/invocation.cpp
index 088039a..a1a54e0 100644
--- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
+++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
@@ -33,6 +33,8 @@
 #include <llvm/Linker.h>
 #else
 #include <llvm/Linker/Linker.h>
+#include <llvm/IR/DiagnosticInfo.h>
+#include <llvm/IR/DiagnosticPrinter.h>
 #endif
 #if HAVE_LLVM < 0x0303
 #include <llvm/DerivedTypes.h>
@@ -551,6 +553,22 @@ namespace {
    }
 } // End anonymous namespace
 
+#if HAVE_LLVM >= 0x0305
+
+static void diagnostic_handler(const llvm::DiagnosticInfo &di, void *err_string) {
+   if (di.getSeverity() == llvm::DS_Error) {
+      std::string message;
+      llvm::raw_string_ostream stream(message);
+      llvm::DiagnosticPrinterRawOStream dp(stream);
+      di.print(dp);
+      stream.flush();
+      std::string *out_string = (std::string*)err_string;
+      *out_string = message;
+   }
+}
+
+#endif
+
 module
 clover::compile_program_llvm(const compat::string &source,
                              enum pipe_shader_ir ir,
@@ -578,6 +596,11 @@ clover::compile_program_llvm(const compat::string &source,
    llvm::LLVMContext llvm_ctx;
    unsigned optimization_level;
 
+#if HAVE_LLVM >= 0x0305
+   std::string diagnostic_message;
+   llvm_ctx.setDiagnosticHandler(diagnostic_handler, &diagnostic_message);
+#endif
+
    // The input file name must have the .cl extension in order for the
    // CompilerInvocation class to recognize it as an OpenCL source file.
    llvm::Module *mod = compile(llvm_ctx, source, "input.cl", triple, processor,
@@ -607,5 +630,12 @@ clover::compile_program_llvm(const compat::string &source,
    // LLVM 3.6 and newer, the user takes ownership of the module.
    delete mod;
 #endif
+#if HAVE_LLVM >= 0x0305
+   if (diagnostic_message.size()) {
+      r_log = diagnostic_message;
+      throw build_error();
+   }
+#endif
+
    return m;
 }
-- 
1.8.5.5



More information about the mesa-dev mailing list