Skip to content

GH-25025: [C++] Move non core compute kernels into separate shared library - #45618

Closed
raulcd wants to merge 3 commits into
apache:mainfrom
raulcd:GH-25025
Closed

raulcd wants to merge 3 commits into
apache:mainfrom
raulcd:GH-25025

Conversation

@raulcd

@raulcd raulcd commented Feb 24, 2025 •

Copy link
Copy Markdown
Member

This is just a draft investigating some possible approaches.

Rationale for this change

TBD

What changes are included in this PR?

TBD

Are these changes tested?

Yes on CI.

Are there any user-facing changes?

TODO: Expand once the approach is clearer
Yes.

This PR includes breaking changes to public APIs.

Comment thread cpp/src/arrow/acero/CMakeLists.txt Resolved
Comment thread cpp/src/arrow/CMakeLists.txt Resolved
Comment thread cpp/src/arrow/acero/CMakeLists.txt Resolved
@raulcd

raulcd commented Feb 27, 2025

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g cpp

@github-actions

Copy link
Copy Markdown

Revision: 7240ca253191e6309e8335d0c190100f193bd4fe

Submitted crossbow builds: ursacomputing/crossbow @ actions-906afe6414

Task Status
example-cpp-minimal-build-static GitHub Actions
example-cpp-minimal-build-static-system-dependency GitHub Actions
example-cpp-tutorial GitHub Actions
test-alpine-linux-cpp GitHub Actions
test-build-cpp-fuzz GitHub Actions
test-conda-cpp GitHub Actions
test-conda-cpp-meson GitHub Actions
test-conda-cpp-valgrind GitHub Actions
test-cuda-cpp-ubuntu-22.04-cuda-11.7.1 GitHub Actions
test-debian-12-cpp-amd64 GitHub Actions
test-debian-12-cpp-i386 GitHub Actions
test-fedora-39-cpp GitHub Actions
test-ubuntu-22.04-cpp GitHub Actions
test-ubuntu-22.04-cpp-20 GitHub Actions
test-ubuntu-22.04-cpp-bundled GitHub Actions
test-ubuntu-22.04-cpp-emscripten GitHub Actions
test-ubuntu-22.04-cpp-no-threading GitHub Actions
test-ubuntu-24.04-cpp GitHub Actions
test-ubuntu-24.04-cpp-bundled-offline GitHub Actions
test-ubuntu-24.04-cpp-gcc-13-bundled GitHub Actions
test-ubuntu-24.04-cpp-gcc-14 GitHub Actions
test-ubuntu-24.04-cpp-minimal-with-formats GitHub Actions
test-ubuntu-24.04-cpp-thread-sanitizer GitHub Actions

@raulcd

raulcd commented Feb 28, 2025

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-ubuntu-22.04-cpp-emscripten

@github-actions

Copy link
Copy Markdown

Revision: 98b46082009932b78e1c715cc8f96417408bf5ea

Submitted crossbow builds: ursacomputing/crossbow @ actions-bb75d4faa6

Task Status
test-ubuntu-22.04-cpp-emscripten GitHub Actions

@raulcd

raulcd commented Mar 3, 2025

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g cpp

@github-actions

github-actions Bot commented Mar 3, 2025

Copy link
Copy Markdown

Revision: b46eb0b292eaa9e27e1c04ceaf9b4a26603db1c6

Submitted crossbow builds: ursacomputing/crossbow @ actions-c444380c6c

Task Status
example-cpp-minimal-build-static GitHub Actions
example-cpp-minimal-build-static-system-dependency GitHub Actions
example-cpp-tutorial GitHub Actions
test-alpine-linux-cpp GitHub Actions
test-build-cpp-fuzz GitHub Actions
test-conda-cpp GitHub Actions
test-conda-cpp-meson GitHub Actions
test-conda-cpp-valgrind GitHub Actions
test-cuda-cpp-ubuntu-22.04-cuda-11.7.1 GitHub Actions
test-debian-12-cpp-amd64 GitHub Actions
test-debian-12-cpp-i386 GitHub Actions
test-fedora-39-cpp GitHub Actions
test-ubuntu-22.04-cpp GitHub Actions
test-ubuntu-22.04-cpp-20 GitHub Actions
test-ubuntu-22.04-cpp-bundled GitHub Actions
test-ubuntu-22.04-cpp-emscripten GitHub Actions
test-ubuntu-22.04-cpp-no-threading GitHub Actions
test-ubuntu-24.04-cpp GitHub Actions
test-ubuntu-24.04-cpp-bundled-offline GitHub Actions
test-ubuntu-24.04-cpp-gcc-13-bundled GitHub Actions
test-ubuntu-24.04-cpp-gcc-14 GitHub Actions
test-ubuntu-24.04-cpp-minimal-with-formats GitHub Actions
test-ubuntu-24.04-cpp-thread-sanitizer GitHub Actions

@raulcd

raulcd commented Mar 3, 2025

Copy link
Copy Markdown
Member Author

@kou @pitrou this is still far from ready but all the C++ jobs are currently successful with this initial PoC. The only failure is the example-cpp-tutorial. I don't seem to be able to add to the public API the new RegisterComputeKernels function. I've tried creating a new api.h for kernels, adding the include <arrow/compute/kernels/registry.h> directly and changing the namespace to a completely new one arrow::kernels but it always fails to find the new function. An example of the failure. I took a look on others of our public apis, like substrait but I can't seem to find what am I missing to expose the new function, could you help with any pointer on where the issue is coming from / how to solve?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add #pragma once?

Suggested change
#include "arrow/compute/registry.h"
#pragma once
#include "arrow/compute/registry.h"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved this to kernels/api.h where I've added #pragma once shouldn't be necessary here now

Comment thread cpp/src/arrow/compute/visibility.h Resolved
Comment thread cpp/src/arrow/compute/CMakeLists.txt Resolved
@kou

kou commented Mar 4, 2025

Copy link
Copy Markdown
Member

could you help with any pointer on where the issue is coming from / how to solve?

#45618 (comment) is the answer of this.

@pitrou

pitrou commented Apr 1, 2025

Copy link
Copy Markdown
Member

I've tried a couple of things but I can't seem to make it work adding the static initializer on the code file instead of the header.

This all seems very complicated to me. You should only need something like:

namespace {

bool RegisterComputeKernels() {
  auto registry = GetFunctionRegistry();

  internal::RegisterScalarArithmetic(registry);
  // etc.
  return true;  // dummy return value, must return something
}

bool g_kernels_registered = RegisterComputeKernels();

}

(various spellings of this are possible, but the semantics would be the same)

@raulcd

raulcd commented Apr 1, 2025 •

Copy link
Copy Markdown
Member Author

This all seems very complicated to me. You should only need something like:

I also thought that should work but, if I understand correctly, this diff should do the trick:

diff --git a/cpp/src/arrow/compute/kernels/api.h b/cpp/src/arrow/compute/kernels/api.h
index 3385d4cc80..8a22c6c3ca 100644
--- a/cpp/src/arrow/compute/kernels/api.h
+++ b/cpp/src/arrow/compute/kernels/api.h
@@ -25,5 +25,3 @@
 /// @}
 
 #include "arrow/compute/kernels/registry.h"  // IWYU pragma: export
-
-static arrow::Status g_kernels_registered = arrow::compute::RegisterComputeKernels();
diff --git a/cpp/src/arrow/compute/kernels/registry.cc b/cpp/src/arrow/compute/kernels/registry.cc
index fe9c2df724..d9c899bec4 100644
--- a/cpp/src/arrow/compute/kernels/registry.cc
+++ b/cpp/src/arrow/compute/kernels/registry.cc
@@ -33,11 +33,11 @@
 namespace arrow {
 namespace compute {
 
-Status RegisterComputeKernels() {
+bool RegisterComputeKernels() {
   auto registry = GetFunctionRegistry();
   // TODO: Do we have a different way to avoid double registration?
   if (registry->GetFunction("abs").ok()) {
-    return Status::OK();
+    return true;
   }
   // Register additional kernels on libarrow_compute
   // Scalar functions
@@ -78,7 +78,9 @@ Status RegisterComputeKernels() {
   internal::RegisterScalarAggregateTDigest(registry);
   internal::RegisterScalarAggregateVariance(registry);
 
-  return Status::OK();
+  return true;
 }
+
+static bool g_kernels_registered = RegisterComputeKernels();
 }  // namespace compute
 }  // namespace arrow
diff --git a/cpp/src/arrow/compute/kernels/registry.h b/cpp/src/arrow/compute/kernels/registry.h
index 02c002126a..075bd92002 100644
--- a/cpp/src/arrow/compute/kernels/registry.h
+++ b/cpp/src/arrow/compute/kernels/registry.h
@@ -19,10 +19,9 @@
 #include "arrow/compute/registry.h"
 
 #include "arrow/compute/visibility.h"
-#include "arrow/status.h"
 
 namespace arrow::compute {
 
-ARROW_COMPUTE_EXPORT Status RegisterComputeKernels();
+ARROW_COMPUTE_EXPORT bool RegisterComputeKernels();
 
 }  // namespace arrow::compute

but on this case, the kernels are not registered when linking libarrow_compute and tests just fail because the kernels are not registered, example:

'_error_or_value26.status()' failed with Key error: No function registered with name: round_temporal
/home/raulcd/code/arrow/cpp/src/arrow/compute/exec.cc:1367  ctx->func_registry()->GetFunction(func_name)
/home/raulcd/code/arrow/cpp/src/arrow/compute/kernels/test_util_internal.cc:87: Failure
Failed

edit: added static to the variable (same output)

@pitrou

pitrou commented Apr 1, 2025

Copy link
Copy Markdown
Member

Well, is libarrow_compute.dll loaded somewhere?

@raulcd

raulcd commented Apr 1, 2025 •

Copy link
Copy Markdown
Member Author

What do you mean? We are linking it and we have the tests including the headers for kernels/api.h which are part of libarrow_compute.so
That's why ended up adding the static variable to the header file in the first place

@kou

kou commented Apr 15, 2025

Copy link
Copy Markdown
Member

It seems that we need --no-as-needed linker flags. If we don't specify it, libarrow_compute.so isn't linked to test files such as arrow-compute-aggregate-test.

This is not a good code but this works on Debian GNU/Linux with g++ 14.2.0:

diff --git a/cpp/src/arrow/compute/kernels/CMakeLists.txt b/cpp/src/arrow/compute/kernels/CMakeLists.txt
index 3e5239be96..d79482e629 100644
--- a/cpp/src/arrow/compute/kernels/CMakeLists.txt
+++ b/cpp/src/arrow/compute/kernels/CMakeLists.txt
@@ -151,6 +151,9 @@ add_arrow_compute_test(aggregate_test
                        SOURCES
                        aggregate_test.cc
                        EXTRA_LINK_LIBS
+                       "-Wl,--push-state,--no-as-needed"
+                       arrow_compute_shared
+                       "-Wl,--pop-state"
                        arrow_compute_kernels_testing
                        arrow_compute_testing
                        Boost::headers)
diff --git a/cpp/src/arrow/compute/kernels/api.h b/cpp/src/arrow/compute/kernels/api.h
index 3385d4cc80..deaf82626f 100644
--- a/cpp/src/arrow/compute/kernels/api.h
+++ b/cpp/src/arrow/compute/kernels/api.h
@@ -23,7 +23,3 @@
 /// \defgroup compute-kernels Extra compute kernels
 /// @{
 /// @}
-
-#include "arrow/compute/kernels/registry.h"  // IWYU pragma: export
-
-static arrow::Status g_kernels_registered = arrow::compute::RegisterComputeKernels();
diff --git a/cpp/src/arrow/compute/kernels/registry.cc b/cpp/src/arrow/compute/kernels/registry.cc
index fe9c2df724..d62d25f74f 100644
--- a/cpp/src/arrow/compute/kernels/registry.cc
+++ b/cpp/src/arrow/compute/kernels/registry.cc
@@ -33,11 +33,11 @@
 namespace arrow {
 namespace compute {
 
-Status RegisterComputeKernels() {
+void RegisterComputeKernels() {
   auto registry = GetFunctionRegistry();
   // TODO: Do we have a different way to avoid double registration?
   if (registry->GetFunction("abs").ok()) {
-    return Status::OK();
+    return;
   }
   // Register additional kernels on libarrow_compute
   // Scalar functions
@@ -77,8 +77,10 @@ Status RegisterComputeKernels() {
   internal::RegisterScalarAggregateQuantile(registry);
   internal::RegisterScalarAggregateTDigest(registry);
   internal::RegisterScalarAggregateVariance(registry);
-
-  return Status::OK();
 }
+
+RegistryInitializer::RegistryInitializer() { RegisterComputeKernels(); }
+
+RegistryInitializer RegistryInitializer::instance;
 }  // namespace compute
 }  // namespace arrow
diff --git a/cpp/src/arrow/compute/kernels/registry.h b/cpp/src/arrow/compute/kernels/registry.h
index 02c002126a..2d9d95240e 100644
--- a/cpp/src/arrow/compute/kernels/registry.h
+++ b/cpp/src/arrow/compute/kernels/registry.h
@@ -23,6 +23,11 @@
 
 namespace arrow::compute {
 
-ARROW_COMPUTE_EXPORT Status RegisterComputeKernels();
+ARROW_COMPUTE_EXPORT void RegisterComputeKernels();
+class ARROW_COMPUTE_EXPORT RegistryInitializer {
+  public:
+    RegistryInitializer();
+    static RegistryInitializer instance;
+};
 
 }  // namespace arrow::compute

Comment thread cpp/src/arrow/compute/CMakeLists.txt Outdated
Comment on lines 89 to 106

@raulcd raulcd Apr 22, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't been able to make this work for WIN32 yet, I'll keep investigating how to force the linker to not optimize out arrow_compute_shared and force adding it on WIN32. I am unsure this is what we want to do. From what I understand the linker is allowed to skip libraries not used by the end result and as libarrow_compute_shared is never used its optimized out, that's why we have to use the --no-as-needed / -force-load / while-archive flags. From my understanding, having the static initializer there and never calling anything explicitly on libarrow_compute will make anyone that tries to use Compute and link against Arrow to require those flags to be set making the use of arrow_compute quite cumbersome.
At this point I think the explicit function call to RegisterComputeKernels is a better approach as it's much simpler from a user stand-point.
@kou @pitrou any thoughts on this?
I am trying to investigate as this is new for me :) and I haven't found much info about those flags or how to use them on the different platforms. A couple of links issues around those (not super relevant):
https://discourse.cmake.org/t/add-linker-archive-options-before-and-after-a-target/2373
https://github.com/PixarAnimationStudios/OpenUSD/pull/2284/files

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point I think the explicit function call to RegisterComputeKernels is a better approach as it's much simpler from a user stand-point.

Well, that's a good point. I wasn't aware that using a static initializer would have such pitfalls.

raulcd added 3 commits April 24, 2025 14:13
…red library

Fix build for some benchmarks and examples
Only link arrow_compute to benchmark if we are building benchmarks
Rename arrow_compute target to arrow_compute_core
Try fixing arrow_compute_core target for Windows
Remove ARROW_EXPORT from arrow_compute in order to fix inconsistent linkage on Windows
Remove ARROW_EXPORT from arrow_compute in order to fix inconsistent linkage on Windows 2
Remove ARROW_EXPORT from arrow_compute in order to fix inconsistent linkage on Windows 3
Link arrow_compute with libarrow
Some code reorganization and add ARROW_EXPORT to required function
Add duplicated codegen_internal to arrow_compute
Remove some more ARROW_EXPORT (this will have to be reverted)
Add ArrowCompute dependency to ArrowAcery and interface libs
Define ARROW_COMPUTE_EXPORT and add it to some of the deleted ARROW_EXPORT. Fix a typo too
Fix wrongly added ARROW_EXPORT for ARROW_COMPUTE_EXPORT
Add some more missing ARROW_COMPUTE_EXPORT
Export symbols for kernel registration and move outside of internal namespace
Move problematic function to compute/codegen_internal instead of compute/kernels/codegen_internal
Add ARROW_EXPORT ot FirstType
Remove duplicated symbol, expose new required symbols
Add missing header
Fix linked libs for arrow_compute
Failed attempt to fix tutorial examples
Fix meson build
Fix correctly installing headers for kernels
Register Compute Kernels for pyarrow when importing pyarrow.compute
Fix visibility.h and correct api
Add registration of kernels to R to get tests passing
Fix wrong rebase
… RegisterComputeKernels

Fix python build and tests
Fix lint, ruby and some example tests
Try fixing R Windows build
Try adding arrow_compute as dependency for Apache Arrow GLib
Revert "Try adding arrow_compute as dependency for Apache Arrow GLib"
This reverts commit 40e818aa4ffc6f161fd18f64aff4f5deffca3fc4.
Stop using arrow/compute/kernels/test_util_internal.h and use arrow/compute/kernels/api.h on tests instead
Add correct pkg-config flags for static builds
Add missing flags on configure_dev on configure.win for R Windows
…o-as-needed linker flags

Try adding --whole-archive instead of --no-as-needed for MinGW
More linker flags
Try removing Windows flags
Try adding /WHOLEARCHIVE LINK_FLAGS on MSVC
Try using target_link_options instead of set_property LINK_FLAGS for MSVC and try WIN32 not MSVC flags
Try using generator expression to retrieve filename for arrow_compute_shared for /WHOLEARCHIVE flag
Add missing multi value arg for function
Add cmake_verbose logs
Maybe if I pass the flags they are added :)
Remove leading whitespace
Revert back now that the flags are being passed
Add EXTRA_LINK_OPTIONS to function definition
arrow_compute_shared.lib does not exist, try arrow_compute.lib
What about a combination?
Trigger pre-commit
@raulcd

raulcd commented Apr 29, 2025

Copy link
Copy Markdown
Member Author

I am going to open a new PR dropping the last two commits and going back to the manual registration. We won't have to do this for the different bindings as they can register the kernels automatically but this will have to be done by users of Arrow compute.

@raulcd

raulcd commented Jun 16, 2025

Copy link
Copy Markdown
Member Author

Closing this PR as this was already merged using a different approach.

@raulcd raulcd closed this Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants