Vedran Miletić <ved...@miletic.net> writes:

> The commit 8e430ff8b060b4e8e922bae24b3c57837da6ea77 support for LLVM
> 3.9 and older versionsin  Clover. This patch restores it and refactors
> the support using Clover compatibility layer for LLVM.
>
> Signed-off-by: Vedran Miletić <ved...@miletic.net>
> ---
>  .../state_trackers/clover/llvm/codegen/bitcode.cpp |  9 ++----
>  src/gallium/state_trackers/clover/llvm/compat.hpp  | 35 
> ++++++++++++++++++++++
>  2 files changed, 37 insertions(+), 7 deletions(-)
>
> diff --git a/src/gallium/state_trackers/clover/llvm/codegen/bitcode.cpp 
> b/src/gallium/state_trackers/clover/llvm/codegen/bitcode.cpp
> index 5dcc4f8..4b4ae41 100644
> --- a/src/gallium/state_trackers/clover/llvm/codegen/bitcode.cpp
> +++ b/src/gallium/state_trackers/clover/llvm/codegen/bitcode.cpp
> @@ -32,6 +32,7 @@
>  ///
>  
>  #include "llvm/codegen.hpp"
> +#include "llvm/compat.hpp"
>  #include "llvm/metadata.hpp"
>  #include "core/error.hpp"
>  #include "util/algorithm.hpp"
> @@ -99,13 +100,7 @@ clover::llvm::parse_module_library(const module &m, 
> ::llvm::LLVMContext &ctx,
>     auto mod = ::llvm::parseBitcodeFile(::llvm::MemoryBufferRef(
>                                          as_string(m.secs[0].data), " "), 
> ctx);
>  
> -   if (::llvm::Error err = mod.takeError()) {
> -      std::string msg;
> -      ::llvm::handleAllErrors(std::move(err), [&](::llvm::ErrorInfoBase 
> &EIB) {
> -         msg = EIB.message();
> -         fail(r_log, error(CL_INVALID_PROGRAM), msg.c_str());
> -      });
> -   }
> +   compat::handle_module_error(mod, r_log);
>  
>     return std::unique_ptr<::llvm::Module>(std::move(*mod));
>  }
> diff --git a/src/gallium/state_trackers/clover/llvm/compat.hpp 
> b/src/gallium/state_trackers/clover/llvm/compat.hpp
> index a963cff..b29100f 100644
> --- a/src/gallium/state_trackers/clover/llvm/compat.hpp
> +++ b/src/gallium/state_trackers/clover/llvm/compat.hpp
> @@ -39,6 +39,11 @@
>  #include <llvm/Linker/Linker.h>
>  #include <llvm/Transforms/IPO.h>
>  #include <llvm/Target/TargetMachine.h>
> +#if HAVE_LLVM >= 0x0400
> +#include <llvm/Support/Error.h>
> +#else
> +#include <llvm/Support/ErrorOr.h>
> +#endif
>  
>  #if HAVE_LLVM >= 0x0307
>  #include <llvm/IR/LegacyPassManager.h>
> @@ -53,6 +58,14 @@
>  #include <clang/Frontend/CodeGenOptions.h>
>  #include <clang/Frontend/CompilerInstance.h>
>  
> +#if HAVE_LLVM >= 0x0307
> +#include <memory>
> +#endif
> +
> +namespace llvm {
> +   class Module;
> +}
> +
>  namespace clover {
>     namespace llvm {
>        namespace compat {
> @@ -158,6 +171,28 @@ namespace clover {
>  #else
>           const auto default_reloc_model = ::llvm::Reloc::Default;
>  #endif
> +
> +#if HAVE_LLVM >= 0x0400
> +         typedef ::llvm::Expected<std::unique_ptr<::llvm::Module>> 
> bitcode_module;
> +#elif HAVE_LLVM >= 0x0307
> +         typedef ::llvm::ErrorOr<std::unique_ptr<::llvm::Module>> 
> bitcode_module;
> +#else
> +         typedef ::llvm::ErrorOr<::llvm::Module *> bitcode_module;
> +#endif
> +

You could avoid the preprocessor conditionals above (and also the
previous hunk) by making the function below a template parameterized on
the module type and let the compiler deduce the correct type by itself
(see attachment).

> +         inline void
> +         handle_module_error(bitcode_module &mod, std::string &r_log) {
> +#if HAVE_LLVM >= 0x0400
> +            if (::llvm::Error err = mod.takeError()) {
> +               ::llvm::handleAllErrors(std::move(err), 
> [&](::llvm::ErrorInfoBase &EIB) {
> +                  fail(r_log, error(CL_INVALID_PROGRAM), 
> EIB.message().c_str());
> +               });
> +            }
> +#else
> +            if (!mod)
> +               fail(r_log, error(CL_INVALID_PROGRAM), 
> mod.getError().message());
> +#endif

To improve the usefulness/complexity ratio of this helper, let's not
hard-code any error handling policy here, instead just call an error
handler function provided as argument (see attachment) so the specific
error code and message are in control of the caller.  With the attached
patch squashed in, patch is:

Reviewed-by: Francisco Jerez <curroje...@riseup.net>

> +         }
>        }
>     }
>  }
> -- 
> 2.7.4

diff --git a/src/gallium/state_trackers/clover/llvm/codegen/bitcode.cpp b/src/gallium/state_trackers/clover/llvm/codegen/bitcode.cpp
index 4b4ae41..d09207b 100644
--- a/src/gallium/state_trackers/clover/llvm/codegen/bitcode.cpp
+++ b/src/gallium/state_trackers/clover/llvm/codegen/bitcode.cpp
@@ -100,7 +100,9 @@ clover::llvm::parse_module_library(const module &m, ::llvm::LLVMContext &ctx,
    auto mod = ::llvm::parseBitcodeFile(::llvm::MemoryBufferRef(
                                         as_string(m.secs[0].data), " "), ctx);
 
-   compat::handle_module_error(mod, r_log);
+   compat::handle_module_error(mod, [&](const std::string &s) {
+         fail(r_log, error(CL_INVALID_PROGRAM), s);
+      });
 
    return std::unique_ptr<::llvm::Module>(std::move(*mod));
 }
diff --git a/src/gallium/state_trackers/clover/llvm/compat.hpp b/src/gallium/state_trackers/clover/llvm/compat.hpp
index b29100f..81592ce 100644
--- a/src/gallium/state_trackers/clover/llvm/compat.hpp
+++ b/src/gallium/state_trackers/clover/llvm/compat.hpp
@@ -58,14 +58,6 @@
 #include <clang/Frontend/CodeGenOptions.h>
 #include <clang/Frontend/CompilerInstance.h>
 
-#if HAVE_LLVM >= 0x0307
-#include <memory>
-#endif
-
-namespace llvm {
-   class Module;
-}
-
 namespace clover {
    namespace llvm {
       namespace compat {
@@ -172,25 +164,16 @@ namespace clover {
          const auto default_reloc_model = ::llvm::Reloc::Default;
 #endif
 
+         template<typename M, typename F> void
+         handle_module_error(M &mod, const F &f) {
 #if HAVE_LLVM >= 0x0400
-         typedef ::llvm::Expected<std::unique_ptr<::llvm::Module>> bitcode_module;
-#elif HAVE_LLVM >= 0x0307
-         typedef ::llvm::ErrorOr<std::unique_ptr<::llvm::Module>> bitcode_module;
-#else
-         typedef ::llvm::ErrorOr<::llvm::Module *> bitcode_module;
-#endif
-
-         inline void
-         handle_module_error(bitcode_module &mod, std::string &r_log) {
-#if HAVE_LLVM >= 0x0400
-            if (::llvm::Error err = mod.takeError()) {
-               ::llvm::handleAllErrors(std::move(err), [&](::llvm::ErrorInfoBase &EIB) {
-                  fail(r_log, error(CL_INVALID_PROGRAM), EIB.message().c_str());
-               });
-            }
+            if (::llvm::Error err = mod.takeError())
+               ::llvm::handleAllErrors(std::move(err), [&](::llvm::ErrorInfoBase &eib) {
+                     f(eib.message());
+                  });
 #else
             if (!mod)
-               fail(r_log, error(CL_INVALID_PROGRAM), mod.getError().message());
+               f(mod.getError().message());
 #endif
          }
       }

Attachment: signature.asc
Description: PGP signature

_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to