Skip to content

GH-55505: Gandiva: normalize Darwin host triple to macosx so exp10 libcall is available - #55506

Open
lriggs wants to merge 2 commits into
apache:mainfrom
lriggs:gh-55505
Open

lriggs wants to merge 2 commits into
apache:mainfrom
lriggs:gh-55505

Conversation

@lriggs

@lriggs lriggs commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

An incorrect target machine setting leads to an undefined function reference in LLVM resulting in a crash for MacOS.

Gandiva builds its JIT target machine from llvm::sys::getDefaultTargetTriple(), which on macOS is an unversioned/kernel-versioned arm64-apple-darwin. LLVM only enables macOS libcalls such as __exp10 for the macosx OS name. With optimization off, pow(10, x) (used by get_scale_multiplier for scales outside 0..19) is
lowered to llvm.exp10, and the backend finds no libcall for it.

What changes are included in this PR?

Changes to the target machine configuration on Mac. Unit test.

Are these changes tested?

Yes manual with a repro and unit test.

Are there any user-facing changes?

No.

This PR contains a "Critical Fix". Fixes a potential crash on MacOs when optimization is disabled for llvm.

Was AI used for this PR?

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

…p10 libcall is available

An incorrect target machine setting leads to an undefined function reference in LLVM resulting in a crash.

LLVM's default host triple names the OS "darwin", but LLVM only enables
macOS libcalls (e.g. __exp10 for llvm.exp10) for the "macosx" OS. Fix
the naming of the target machine.
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #55505 has been automatically assigned in GitHub to PR creator.

Comment thread cpp/src/gandiva/engine.cc

// getMacOSXVersion() leaves the minor unset for darwin20+; print it as "11.0", not
// "11".
const unsigned minor = version.getMinor().value_or(0);

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.

getMinor() returns llvm::Optional on LLVM 14, which has no value_or(). CMake still accepts those versions, and this function compiles on every platform. Let's use the portable form.

const auto minor = version.getMinor();
const unsigned minor_value = minor ? *minor : 0;

Comment thread cpp/src/gandiva/engine.cc
// kernel->macOS mapping is acceptable here.
llvm::VersionTuple version = min_version;
llvm::VersionTuple converted;
if (triple.getMacOSXVersion(converted) && converted >= min_version) {

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.

This conversion can advertise a newer macOS than the machine actually runs. With LLVM 21.1.8, darwin27.2.0 converts to macosx28.0 on my macOS 27.2.1 machine. Although that enables exp10, the target version affects other libcall availability decisions too. Could we instead take the macOS OS/version component from Gandiva’s embedded bitcode target triple, after validating its platform and architecture? Clang already records that target. I tested that approach with LLVM 21.1.8, and it fixes the reported cases without the approximate kernel-version conversion.

int32_t scale);

protected:
// Compiles round(float64, int32) through the Gandiva JIT and evaluates it on the

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.

nit: One of my favorite things to do is test-only first commit that fails the CI however here I still don't know what runs on pull requests before the green light from the overseer

Comment thread cpp/src/gandiva/engine.cc
extern const unsigned char kPrecompiledBitcode[];
extern const size_t kPrecompiledBitcodeSize;

llvm::Triple NormalizeDarwinTriple(llvm::Triple triple) {

@dmitry-chirkov-dremio dmitry-chirkov-dremio Nov 13, 2026 •

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.

Part of me wants to do something like this which is conditioned on llvm/llvm-project#210674 making it into LLVM 24 (and then us supporting it). Thoughts?

This is a bit wider change to clean up assumptions of the past since we are here anyway. This would replace the manual CPU/feature setup with LLVM’s detectHost(), but preserves the commit’s opt-out by clearing CPU/features when target_host_cpu(false), and adds the macOS triple correction.

Result<llvm::orc::JITTargetMachineBuilder> MakeTargetMachineBuilder(
    const Configuration& conf) {
  auto detected = llvm::orc::JITTargetMachineBuilder::detectHost();
  ARROW_ASSIGN_OR_RAISE(
      auto jtmb, AsArrowResult(detected, "Could not detect host: "));

  if (!conf.target_host_cpu()) {
    jtmb.setCPU("");
    jtmb.setFeatures("");
  }

#if defined(__APPLE__) && LLVM_VERSION_MAJOR < 24
  auto& triple = jtmb.getTargetTriple();
  if (triple.getOS() == llvm::Triple::Darwin) {
    char version[64]{};
    size_t size = sizeof(version);
    if (sysctlbyname("kern.osproductversion", version, &size,
                    nullptr, 0) != 0) {
      return arrow::internal::IOErrorFromErrno(
          errno, "sysctlbyname failed for 'kern.osproductversion'");
    }
    triple.setOSName(std::string("macosx") + version);
  }
#endif

  jtmb.setRelocationModel(llvm::Reloc::PIC_);

#if LLVM_VERSION_MAJOR >= 18
  using CodeGenOptLevel = llvm::CodeGenOptLevel;
#else
  using CodeGenOptLevel = llvm::CodeGenOpt::Level;
#endif
  jtmb.setCodeGenOptLevel(
      conf.optimize() ? CodeGenOptLevel::Aggressive : CodeGenOptLevel::None);

  return jtmb;
}

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