You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ra...@apache.org on 2022/02/02 04:41:13 UTC

[arrow] branch master updated: ARROW-15514: [C++][Gandiva] Add flag to enable Gandiva Object Code

This is an automated email from the ASF dual-hosted git repository.

ravindra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 4e1c79e  ARROW-15514: [C++][Gandiva] Add flag to enable Gandiva Object Code
4e1c79e is described below

commit 4e1c79eab1b4ae3be3eafbb6f79c652c2462c237
Author: Anthony Louis <an...@dremio.com>
AuthorDate: Wed Feb 2 10:06:50 2022 +0530

    ARROW-15514: [C++][Gandiva] Add flag to enable Gandiva Object Code
    
    That PR adds a flag in C++ code to enable or disable the object code cache when Gandiva stores an expression.
    
    The reason for that is some expressions are crashing when the object code is enabled, we are identifying that.
    
    Closes #12308 from anthonylouisbsb/fixbug/add-flag-for-object-code
    
    Authored-by: Anthony Louis <an...@dremio.com>
    Signed-off-by: Pindikura Ravindra <ra...@dremio.com>
---
 cpp/src/gandiva/engine.h                |  2 ++
 cpp/src/gandiva/filter.cc               | 25 ++++++++++++++++++++++---
 cpp/src/gandiva/gandiva_object_cache.cc |  2 ++
 cpp/src/gandiva/gandiva_object_cache.h  |  2 ++
 cpp/src/gandiva/llvm_generator.cc       |  2 ++
 cpp/src/gandiva/llvm_generator.h        |  2 ++
 cpp/src/gandiva/projector.cc            | 31 ++++++++++++++++++++++++++-----
 7 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/cpp/src/gandiva/engine.h b/cpp/src/gandiva/engine.h
index 65250a9..2579804 100644
--- a/cpp/src/gandiva/engine.h
+++ b/cpp/src/gandiva/engine.h
@@ -57,10 +57,12 @@ class GANDIVA_EXPORT Engine {
   /// Optimise and compile the module.
   Status FinalizeModule();
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
   /// Set LLVM ObjectCache.
   void SetLLVMObjectCache(GandivaObjectCache& object_cache) {
     execution_engine_->setObjectCache(&object_cache);
   }
+#endif
 
   /// Get the compiled function corresponding to the irfunction.
   void* CompiledFunction(llvm::Function* irFunction);
diff --git a/cpp/src/gandiva/filter.cc b/cpp/src/gandiva/filter.cc
index 6a359b9..b7b2840 100644
--- a/cpp/src/gandiva/filter.cc
+++ b/cpp/src/gandiva/filter.cc
@@ -45,8 +45,12 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition,
   ARROW_RETURN_IF(configuration == nullptr,
                   Status::Invalid("Configuration cannot be null"));
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
   std::shared_ptr<Cache<ExpressionCacheKey, std::shared_ptr<llvm::MemoryBuffer>>> cache =
       LLVMGenerator::GetCache();
+#else
+  static Cache<ExpressionCacheKey, std::shared_ptr<Filter>> cache;
+#endif
 
   Condition conditionToKey = *(condition.get());
 
@@ -54,8 +58,9 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition,
 
   bool is_cached = false;
 
-  std::shared_ptr<llvm::MemoryBuffer> prev_cached_obj;
-  prev_cached_obj = cache->GetObjectCode(cache_key);
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
+
+  auto prev_cached_obj = cache->GetObjectCode(cache_key);
 
   // Verify if previous filter obj code was cached
   if (prev_cached_obj != nullptr) {
@@ -63,6 +68,15 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition,
   }
 
   GandivaObjectCache obj_cache(cache, cache_key);
+#else
+  auto prev_cached_obj = cache.GetObjectCode(cache_key);
+  // Verify if previous filter obj code was cached
+  if (prev_cached_obj != nullptr) {
+    *filter = prev_cached_obj;
+    filter->get()->SetBuiltFromCache(true);
+    return Status::OK();
+  }
+#endif
 
   // Build LLVM generator, and generate code for the specified expression
   std::unique_ptr<LLVMGenerator> llvm_gen;
@@ -73,8 +87,10 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition,
   ExprValidator expr_validator(llvm_gen->types(), schema);
   ARROW_RETURN_NOT_OK(expr_validator.Validate(condition));
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
   // Set the object cache for LLVM
   llvm_gen->SetLLVMObjectCache(obj_cache);
+#endif
 
   ARROW_RETURN_NOT_OK(llvm_gen->Build({condition}, SelectionVector::Mode::MODE_NONE));
 
@@ -82,6 +98,10 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition,
   *filter = std::make_shared<Filter>(std::move(llvm_gen), schema, configuration);
   filter->get()->SetBuiltFromCache(is_cached);
 
+#ifndef GANDIVA_ENABLE_OBJECT_CODE_CACHE
+  cache.PutObjectCode(cache_key, *filter);
+#endif
+
   return Status::OK();
 }
 
@@ -121,5 +141,4 @@ std::string Filter::DumpIR() { return llvm_generator_->DumpIR(); }
 void Filter::SetBuiltFromCache(bool flag) { built_from_cache_ = flag; }
 
 bool Filter::GetBuiltFromCache() { return built_from_cache_; }
-
 }  // namespace gandiva
diff --git a/cpp/src/gandiva/gandiva_object_cache.cc b/cpp/src/gandiva/gandiva_object_cache.cc
index e1ef8b6..e181aa3 100644
--- a/cpp/src/gandiva/gandiva_object_cache.cc
+++ b/cpp/src/gandiva/gandiva_object_cache.cc
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
 #include "gandiva/gandiva_object_cache.h"
 
 #include <utility>
@@ -51,3 +52,4 @@ std::unique_ptr<llvm::MemoryBuffer> GandivaObjectCache::getObject(const llvm::Mo
 }
 
 }  // namespace gandiva
+#endif  // GANDIVA_ENABLE_OBJECT_CODE_CACHE
diff --git a/cpp/src/gandiva/gandiva_object_cache.h b/cpp/src/gandiva/gandiva_object_cache.h
index 62042c7..c85d8a9 100644
--- a/cpp/src/gandiva/gandiva_object_cache.h
+++ b/cpp/src/gandiva/gandiva_object_cache.h
@@ -17,6 +17,7 @@
 
 #pragma once
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
 #if defined(_MSC_VER)
 #pragma warning(push)
 #pragma warning(disable : 4244)
@@ -52,3 +53,4 @@ class GandivaObjectCache : public llvm::ObjectCache {
   std::shared_ptr<Cache<ExpressionCacheKey, std::shared_ptr<llvm::MemoryBuffer>>> cache_;
 };
 }  // namespace gandiva
+#endif  // GANDIVA_ENABLE_OBJECT_CODE_CACHE
diff --git a/cpp/src/gandiva/llvm_generator.cc b/cpp/src/gandiva/llvm_generator.cc
index 4c81015..d9ead3e 100644
--- a/cpp/src/gandiva/llvm_generator.cc
+++ b/cpp/src/gandiva/llvm_generator.cc
@@ -47,6 +47,7 @@ Status LLVMGenerator::Make(std::shared_ptr<Configuration> config,
   return Status::OK();
 }
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
 std::shared_ptr<Cache<ExpressionCacheKey, std::shared_ptr<llvm::MemoryBuffer>>>
 LLVMGenerator::GetCache() {
   static std::shared_ptr<Cache<ExpressionCacheKey, std::shared_ptr<llvm::MemoryBuffer>>>
@@ -59,6 +60,7 @@ LLVMGenerator::GetCache() {
 void LLVMGenerator::SetLLVMObjectCache(GandivaObjectCache& object_cache) {
   engine_->SetLLVMObjectCache(object_cache);
 }
+#endif
 
 Status LLVMGenerator::Add(const ExpressionPtr expr, const FieldDescriptorPtr output) {
   int idx = static_cast<int>(compiled_exprs_.size());
diff --git a/cpp/src/gandiva/llvm_generator.h b/cpp/src/gandiva/llvm_generator.h
index 5d8f725..2b97777 100644
--- a/cpp/src/gandiva/llvm_generator.h
+++ b/cpp/src/gandiva/llvm_generator.h
@@ -50,12 +50,14 @@ class GANDIVA_EXPORT LLVMGenerator {
   static Status Make(std::shared_ptr<Configuration> config,
                      std::unique_ptr<LLVMGenerator>* llvm_generator);
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
   /// \brief Get the cache to be used for LLVM ObjectCache.
   static std::shared_ptr<Cache<ExpressionCacheKey, std::shared_ptr<llvm::MemoryBuffer>>>
   GetCache();
 
   /// \brief Set LLVM ObjectCache.
   void SetLLVMObjectCache(GandivaObjectCache& object_cache);
+#endif
 
   /// \brief Build the code for the expression trees for default mode with a LLVM
   /// ObjectCache. Each element in the vector represents an expression tree
diff --git a/cpp/src/gandiva/projector.cc b/cpp/src/gandiva/projector.cc
index a1c0f15..0192fe3 100644
--- a/cpp/src/gandiva/projector.cc
+++ b/cpp/src/gandiva/projector.cc
@@ -61,23 +61,38 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector& exprs,
   ARROW_RETURN_IF(configuration == nullptr,
                   Status::Invalid("Configuration cannot be null"));
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
   // see if equivalent projector was already built
   std::shared_ptr<Cache<ExpressionCacheKey, std::shared_ptr<llvm::MemoryBuffer>>> cache =
       LLVMGenerator::GetCache();
+#else
+  static Cache<ExpressionCacheKey, std::shared_ptr<Projector>> cache;
+#endif
 
   ExpressionCacheKey cache_key(schema, configuration, exprs, selection_vector_mode);
 
-  bool llvm_flag = false;
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
+  auto prev_cached_obj = cache->GetObjectCode(cache_key);
+#else
+  auto prev_cached_obj = cache.GetObjectCode(cache_key);
+#endif
 
-  std::shared_ptr<llvm::MemoryBuffer> prev_cached_obj;
-  prev_cached_obj = cache->GetObjectCode(cache_key);
+  bool was_cached = false;
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
   // Verify if previous projector obj code was cached
   if (prev_cached_obj != nullptr) {
-    llvm_flag = true;
+    was_cached = true;
   }
 
   GandivaObjectCache obj_cache(cache, cache_key);
+#else
+  if (prev_cached_obj != nullptr) {
+    *projector = prev_cached_obj;
+    projector->get()->SetBuiltFromCache(true);
+    return Status::OK();
+  }
+#endif
 
   // Build LLVM generator, and generate code for the specified expressions
   std::unique_ptr<LLVMGenerator> llvm_gen;
@@ -91,8 +106,10 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector& exprs,
     ARROW_RETURN_NOT_OK(expr_validator.Validate(expr));
   }
 
+#ifdef GANDIVA_ENABLE_OBJECT_CODE_CACHE
   // Set the object cache for LLVM
   llvm_gen->SetLLVMObjectCache(obj_cache);
+#endif
 
   ARROW_RETURN_NOT_OK(llvm_gen->Build(exprs, selection_vector_mode));
 
@@ -106,7 +123,11 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector& exprs,
   // Instantiate the projector with the completely built llvm generator
   *projector = std::shared_ptr<Projector>(
       new Projector(std::move(llvm_gen), schema, output_fields, configuration));
-  projector->get()->SetBuiltFromCache(llvm_flag);
+  projector->get()->SetBuiltFromCache(was_cached);
+
+#ifndef GANDIVA_ENABLE_OBJECT_CODE_CACHE
+  cache.PutObjectCode(cache_key, *projector);
+#endif
 
   return Status::OK();
 }