Repository navigation
Conversation
…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.
|
|
|
|
||
| // getMacOSXVersion() leaves the minor unset for darwin20+; print it as "11.0", not | ||
| // "11". | ||
| const unsigned minor = version.getMinor().value_or(0); |
There was a problem hiding this comment.
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;| // kernel->macOS mapping is acceptable here. | ||
| llvm::VersionTuple version = min_version; | ||
| llvm::VersionTuple converted; | ||
| if (triple.getMacOSXVersion(converted) && converted >= min_version) { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
| extern const unsigned char kPrecompiledBitcode[]; | ||
| extern const size_t kPrecompiledBitcodeSize; | ||
|
|
||
| llvm::Triple NormalizeDarwinTriple(llvm::Triple triple) { |
There was a problem hiding this comment.
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;
}
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:
Reviewed before submission by: