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

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


The following commit(s) were added to refs/heads/main by this push:
     new c124bb55d9 GH-45696: [C++][Gandiva] Accept LLVM 20.1 (#45697)
c124bb55d9 is described below

commit c124bb55d993daca93742ce896869ab3101dccbb
Author: Sutou Kouhei <[email protected]>
AuthorDate: Mon Mar 10 20:05:01 2025 +0900

    GH-45696: [C++][Gandiva] Accept LLVM 20.1 (#45697)
    
    ### Rationale for this change
    
    LLVM 20.1 was released.
    
    ### What changes are included in this PR?
    
    * Accept LLVM 20.1
    * Don't use deprecated API
    
    ### Are these changes tested?
    
    Yes.
    
    ### Are there any user-facing changes?
    
    Yes.
    * GitHub Issue: #45696
    
    Authored-by: Sutou Kouhei <[email protected]>
    Signed-off-by: Sutou Kouhei <[email protected]>
---
 cpp/CMakeLists.txt                    |  1 +
 cpp/src/gandiva/decimal_ir.cc         | 15 ++++++++++++---
 cpp/src/gandiva/engine.cc             |  8 ++++++++
 cpp/src/gandiva/engine.h              |  3 +++
 cpp/src/gandiva/function_ir_builder.h |  3 +++
 cpp/src/gandiva/llvm_generator.cc     |  2 +-
 cpp/src/gandiva/llvm_generator.h      |  6 ++++++
 cpp/src/gandiva/llvm_types.h          |  4 +++-
 8 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt
index f2500b3a72..9405a2eeca 100644
--- a/cpp/CMakeLists.txt
+++ b/cpp/CMakeLists.txt
@@ -160,6 +160,7 @@ set(ARROW_DOC_DIR "share/doc/${PROJECT_NAME}")
 set(BUILD_SUPPORT_DIR "${CMAKE_SOURCE_DIR}/build-support")
 
 set(ARROW_LLVM_VERSIONS
+    "20.1"
     "19.1"
     "18.1"
     "17.0"
diff --git a/cpp/src/gandiva/decimal_ir.cc b/cpp/src/gandiva/decimal_ir.cc
index b22e7ad5b5..cac2cb0338 100644
--- a/cpp/src/gandiva/decimal_ir.cc
+++ b/cpp/src/gandiva/decimal_ir.cc
@@ -79,13 +79,22 @@ void DecimalIR::AddGlobals(Engine* engine) {
   globalScaleMultipliers->setAlignment(LLVM_ALIGN(16));
 }
 
+#if LLVM_VERSION_MAJOR < 20
+namespace {
+inline llvm::Function* getOrInsertDeclaration(llvm::Module* M, 
llvm::Intrinsic::ID id,
+                                              llvm::ArrayRef<llvm::Type*> Tys) 
{
+  return llvm::Intrinsic::getDeclaration(M, id, Tys);
+}
+}  // namespace
+#endif
+
 // Lookup intrinsic functions
 void DecimalIR::InitializeIntrinsics() {
-  sadd_with_overflow_fn_ = llvm::Intrinsic::getDeclaration(
+  sadd_with_overflow_fn_ = getOrInsertDeclaration(
       module(), llvm::Intrinsic::sadd_with_overflow, types()->i128_type());
   DCHECK_NE(sadd_with_overflow_fn_, nullptr);
 
-  smul_with_overflow_fn_ = llvm::Intrinsic::getDeclaration(
+  smul_with_overflow_fn_ = getOrInsertDeclaration(
       module(), llvm::Intrinsic::smul_with_overflow, types()->i128_type());
   DCHECK_NE(smul_with_overflow_fn_, nullptr);
 
@@ -541,7 +550,7 @@ llvm::Value* 
DecimalIR::ValueWithOverflow::AsStruct(DecimalIR* decimal_ir) const
 void DecimalIR::AddTrace(const std::string& fmt, std::vector<llvm::Value*> 
args) {
   DCHECK(enable_ir_traces_);
 
-  auto ir_str = ir_builder()->CreateGlobalStringPtr(fmt);
+  auto ir_str = CreateGlobalStringPtr(fmt);
   args.insert(args.begin(), ir_str);
   ir_builder()->CreateCall(module()->getFunction("printf"), args, "trace");
 }
diff --git a/cpp/src/gandiva/engine.cc b/cpp/src/gandiva/engine.cc
index 244e1a45bd..3f637cf65d 100644
--- a/cpp/src/gandiva/engine.cc
+++ b/cpp/src/gandiva/engine.cc
@@ -321,6 +321,14 @@ Status Engine::LoadFunctionIRs() {
   return Status::OK();
 }
 
+llvm::Constant* Engine::CreateGlobalStringPtr(const std::string& string) {
+  auto gloval_variable = ir_builder()->CreateGlobalString(string);
+  auto zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*context()), 0);
+  llvm::Constant* indices[] = {zero, zero};
+  return 
llvm::ConstantExpr::getInBoundsGetElementPtr(gloval_variable->getValueType(),
+                                                      gloval_variable, 
indices);
+}
+
 /// factory method to construct the engine.
 Result<std::unique_ptr<Engine>> Engine::Make(
     const std::shared_ptr<Configuration>& conf, bool cached,
diff --git a/cpp/src/gandiva/engine.h b/cpp/src/gandiva/engine.h
index 3a69500e38..3c8914a7b4 100644
--- a/cpp/src/gandiva/engine.h
+++ b/cpp/src/gandiva/engine.h
@@ -90,6 +90,9 @@ class GANDIVA_EXPORT Engine {
   /// Load the function IRs that can be accessed in the module.
   Status LoadFunctionIRs();
 
+  // Create a global string as a pointer with "i8*" type.
+  llvm::Constant* CreateGlobalStringPtr(const std::string& string);
+
  private:
   Engine(const std::shared_ptr<Configuration>& conf,
          std::unique_ptr<llvm::orc::LLJIT> lljit,
diff --git a/cpp/src/gandiva/function_ir_builder.h 
b/cpp/src/gandiva/function_ir_builder.h
index 388f558407..274684dada 100644
--- a/cpp/src/gandiva/function_ir_builder.h
+++ b/cpp/src/gandiva/function_ir_builder.h
@@ -39,6 +39,9 @@ class FunctionIRBuilder {
   llvm::Module* module() { return engine_->module(); }
   llvm::LLVMContext* context() { return engine_->context(); }
   llvm::IRBuilder<>* ir_builder() { return engine_->ir_builder(); }
+  llvm::Constant* CreateGlobalStringPtr(const std::string& string) {
+    return engine_->CreateGlobalStringPtr(string);
+  }
 
   /// Build an if-else block.
   llvm::Value* BuildIfElse(llvm::Value* condition, llvm::Type* return_type,
diff --git a/cpp/src/gandiva/llvm_generator.cc 
b/cpp/src/gandiva/llvm_generator.cc
index e0223c4d04..43f9ed324e 100644
--- a/cpp/src/gandiva/llvm_generator.cc
+++ b/cpp/src/gandiva/llvm_generator.cc
@@ -723,7 +723,7 @@ void LLVMGenerator::Visitor::Visit(const LiteralDex& dex) {
     case arrow::Type::BINARY: {
       const std::string& str = std::get<std::string>(dex.holder());
 
-      value = ir_builder()->CreateGlobalStringPtr(str.c_str());
+      value = CreateGlobalStringPtr(str);
       len = types->i32_constant(static_cast<int32_t>(str.length()));
       break;
     }
diff --git a/cpp/src/gandiva/llvm_generator.h b/cpp/src/gandiva/llvm_generator.h
index 0c532998e8..a60e2bf6b2 100644
--- a/cpp/src/gandiva/llvm_generator.h
+++ b/cpp/src/gandiva/llvm_generator.h
@@ -96,6 +96,9 @@ class GANDIVA_EXPORT LLVMGenerator {
 
   llvm::LLVMContext* context() { return engine_->context(); }
   llvm::IRBuilder<>* ir_builder() { return engine_->ir_builder(); }
+  llvm::Constant* CreateGlobalStringPtr(const std::string& string) {
+    return engine_->CreateGlobalStringPtr(string);
+  }
 
   /// Visitor to generate the code for a decomposed expression.
   class Visitor : public DexVisitor {
@@ -137,6 +140,9 @@ class GANDIVA_EXPORT LLVMGenerator {
 
     llvm::IRBuilder<>* ir_builder() { return generator_->ir_builder(); }
     llvm::Module* module() { return generator_->module(); }
+    llvm::Constant* CreateGlobalStringPtr(const std::string& string) {
+      return generator_->CreateGlobalStringPtr(string);
+    }
 
     // Generate the code to build the combined validity (bitwise and) from the
     // vector of validities.
diff --git a/cpp/src/gandiva/llvm_types.h b/cpp/src/gandiva/llvm_types.h
index 7768a7f7e4..3541e5e404 100644
--- a/cpp/src/gandiva/llvm_types.h
+++ b/cpp/src/gandiva/llvm_types.h
@@ -55,7 +55,9 @@ class GANDIVA_EXPORT LLVMTypes {
 
   llvm::Type* double_type() { return llvm::Type::getDoubleTy(context_); }
 
-  llvm::PointerType* ptr_type(llvm::Type* type) { return type->getPointerTo(); 
}
+  llvm::PointerType* ptr_type(llvm::Type* type) {
+    return llvm::PointerType::get(type, 0);
+  }
 
   llvm::PointerType* i8_ptr_type() { return ptr_type(i8_type()); }
 

Reply via email to