Skip to content

GH-50371: [C++][Gandiva] Fold common subexpressions before code generation#50372

Open
likun666661 wants to merge 1 commit into
apache:mainfrom
likun666661:gandiva-expression-cse
Open

GH-50371: [C++][Gandiva] Fold common subexpressions before code generation#50372
likun666661 wants to merge 1 commit into
apache:mainfrom
likun666661:gandiva-expression-cse

Conversation

@likun666661

@likun666661 likun666661 commented Jul 5, 2026

Copy link
Copy Markdown

Rationale for this change

This adds a conservative Gandiva expression-layer common subexpression folding pass before code generation. Repeated pure expression subtrees can otherwise be decomposed and lowered multiple times before LLVM sees the module.

The pass intentionally avoids cases where expression-level reuse can be unsafe, including functions that need execution context/function holders, can return errors, use result-null-internal handling, and boolean/if decomposition results with local validity bitmap side effects.

What changes are included in this PR?

  • Add expr_cse to fold safe repeated Gandiva expression subtrees before projector/filter code generation.
  • Add decomposition reuse for safe field, literal, and pure function nodes.
  • Expose unoptimized IR dumping for Gandiva tests when dump_ir is enabled.
  • Add IR-focused tests covering nested arithmetic, generated if, generated boolean, and nested between-style patterns.

Are these changes tested?

Yes.

$ git diff --check
$ pre-commit run --show-diff-on-failure --color=always --all-files cpp
$ PYTHON=/opt/homebrew/bin/python3 TZ=UTC ARROW_TEST_DATA=/Users/likun/workspace-for-apache-arrow/testing/data ninja -C cpp/build-gandiva unittest

The ninja ... unittest run completed with 100% tests passed, 0 tests failed out of 81.

AI-assisted contribution disclosure

This PR was prepared with AI assistance. I reviewed and tested the generated changes locally, including the Gandiva C++ tests and Arrow C++ pre-commit checks listed above.

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #50371 has no components, please add labels for components.

@kou

kou commented Jul 6, 2026

Copy link
Copy Markdown
Member

@dmitry-chirkov-dremio @lriggs @akravchukdremio @xxlaykxx Could you review this?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a conservative expression-layer common subexpression folding (CSE) pass in Gandiva before LLVM code generation, aiming to reduce redundant decomposition/codegen for repeated safe/pure subtrees while explicitly avoiding reuse in cases where Gandiva’s validity/bitmap side effects could affect correctness. It also adds the ability to dump unoptimized IR (pre-optimizer) to support targeted IR-based tests.

Changes:

  • Add expr_cse folding pass and apply it in both Projector::Make and Filter::Make before validation, cache-keying, and code generation.
  • Add decomposition-time reuse for safe nodes (fields, literals, and pure/native functions that don’t need context/holders and can’t error / use null-internal).
  • Add unoptimized IR dumping plumbing and new IR-focused regression tests.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
cpp/src/gandiva/tests/projector_test.cc Adds IR-string-based tests validating CSE behavior (optimized vs unoptimized IR) for repeated arithmetic, generated if, generated boolean, and nested between-like patterns.
cpp/src/gandiva/projector.h Exposes DumpUnoptimizedIR() on Projector.
cpp/src/gandiva/projector.cc Folds common subexpressions before building cache keys, validating, and building the LLVM module; wires DumpUnoptimizedIR().
cpp/src/gandiva/llvm_generator.h Exposes unoptimized_ir() passthrough.
cpp/src/gandiva/filter.cc Folds common subexpressions before cache-keying, validation, and building for filters.
cpp/src/gandiva/expr_decomposer.h / .cc Adds decomposition caching for reuse-safe nodes to avoid repeated decomposition work.
cpp/src/gandiva/expr_cse.h / .cc Introduces the conservative expression-tree folding pass and structural interning for safe nodes.
cpp/src/gandiva/engine.h / .cc Captures and exposes module IR before the optimizer pipeline when dump_ir is enabled.
cpp/src/gandiva/CMakeLists.txt Adds expr_cse.cc to the build.

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