Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 54 additions & 29 deletions cpp/src/gandiva/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
#include <unordered_set>
#include <utility>

#if defined(__APPLE__)
# include <sys/sysctl.h>
# include <cerrno>
#endif

#include <arrow/util/io_util.h>
#include <arrow/util/logging_internal.h>

Expand Down Expand Up @@ -124,8 +129,9 @@ namespace {

std::once_flag llvm_init_once_flag;
bool llvm_init = false;
llvm::StringRef cpu_name;
std::vector<std::string> cpu_attrs;
// The host target (triple, CPU and features), detected once in Engine::InitOnce().
Result<llvm::orc::JITTargetMachineBuilder> host_target =
Status::Invalid("LLVM host target not initialized");
std::once_flag register_exported_funcs_flag;

template <typename T>
Expand All @@ -139,23 +145,52 @@ arrow::Result<T> AsArrowResult(llvm::Expected<T>& expected,
return std::move(expected.get());
}

#if defined(__APPLE__) && LLVM_VERSION_MAJOR < 24
// The host triple names the OS "darwin<kernel version>", but before LLVM 24 the macOS
// libcalls (e.g. __exp10 for llvm.exp10) are only enabled for the "macosx" OS.
// clang's driver rewrites the triple; the JIT does not, so do it here.
// https://github.com/llvm/llvm-project/pull/210674 fixes this in LLVM.
Status UseMacOSXTriple(llvm::Triple* triple) {
if (triple->getOS() != llvm::Triple::Darwin) {
return Status::OK();
}
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);
return Status::OK();
}
#endif

Result<llvm::orc::JITTargetMachineBuilder> DetectHostTarget() {
auto detected = llvm::orc::JITTargetMachineBuilder::detectHost();
ARROW_ASSIGN_OR_RAISE(auto jtmb, AsArrowResult(detected, "Could not detect host: "));
#if defined(__APPLE__) && LLVM_VERSION_MAJOR < 24
ARROW_RETURN_NOT_OK(UseMacOSXTriple(&jtmb.getTargetTriple()));
#endif
return jtmb;
}

Result<llvm::orc::JITTargetMachineBuilder> MakeTargetMachineBuilder(
const Configuration& conf) {
llvm::orc::JITTargetMachineBuilder jtmb(
(llvm::Triple(llvm::sys::getDefaultTargetTriple())));
jtmb.setRelocationModel(llvm::Reloc::PIC_);
if (conf.target_host_cpu()) {
jtmb.setCPU(cpu_name.str());
jtmb.addFeatures(cpu_attrs);
ARROW_RETURN_NOT_OK(host_target.status());
// Copy: host_target is shared by all engines.
llvm::orc::JITTargetMachineBuilder jtmb = *host_target;
if (!conf.target_host_cpu()) {
jtmb.setCPU("");
jtmb.setFeatures("");
}
jtmb.setRelocationModel(llvm::Reloc::PIC_);
#if LLVM_VERSION_MAJOR >= 18
using CodeGenOptLevel = llvm::CodeGenOptLevel;
#else
using CodeGenOptLevel = llvm::CodeGenOpt::Level;
#endif
const auto opt_level =
conf.optimize() ? CodeGenOptLevel::Aggressive : CodeGenOptLevel::None;
jtmb.setCodeGenOptLevel(opt_level);
jtmb.setCodeGenOptLevel(conf.optimize() ? CodeGenOptLevel::Aggressive
: CodeGenOptLevel::None);
return jtmb;
}

Expand Down Expand Up @@ -330,25 +365,15 @@ void Engine::InitOnce() {
llvm::InitializeNativeTargetDisassembler();
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);

cpu_name = llvm::sys::getHostCPUName();
#if LLVM_VERSION_MAJOR >= 19
auto host_features = llvm::sys::getHostCPUFeatures();
const bool have_host_features = true;
#else
llvm::StringMap<bool> host_features;
const auto have_host_features = llvm::sys::getHostCPUFeatures(host_features);
#endif
std::string cpu_attrs_str;
if (have_host_features) {
for (auto& f : host_features) {
std::string attr = f.second ? std::string("+") + f.first().str()
: std::string("-") + f.first().str();
cpu_attrs.push_back(attr);
cpu_attrs_str += " " + attr;
}
host_target = DetectHostTarget();
if (host_target.ok()) {
ARROW_LOG(INFO) << "Detected target triple: " << host_target->getTargetTriple().str();
ARROW_LOG(INFO) << "Detected CPU Name : " << host_target->getCPU();
ARROW_LOG(INFO) << "Detected CPU Features: ["
<< host_target->getFeatures().getString() << "]";
} else {
ARROW_LOG(WARNING) << host_target.status().ToString();
}
ARROW_LOG(INFO) << "Detected CPU Name : " << cpu_name.str();
ARROW_LOG(INFO) << "Detected CPU Features: [" << cpu_attrs_str << "]";
llvm_init = true;
}

Expand Down
15 changes: 15 additions & 0 deletions cpp/src/gandiva/engine_llvm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,19 @@ TEST_F(TestEngine, TestAddOptimised) {
EXPECT_EQ(add_func(my_array, 5), 17);
}

TEST_F(TestEngine, TestAddWithoutHostCpu) {
// target_host_cpu=false compiles for the generic CPU of the host triple
auto conf = TestConfiguration();
conf->target_host_cpu(false);
ASSERT_OK_AND_ASSIGN(engine, Engine::Make(conf, false));

std::string fn_name = BuildVecAdd(engine.get());
ASSERT_OK(engine->FinalizeModule());
ASSERT_OK_AND_ASSIGN(auto fn_ptr, engine->CompiledFunction(fn_name));
auto add_func = reinterpret_cast<add_vector_func_t>(fn_ptr);

int64_t my_array[] = {1, 3, -5, 8, 10};
EXPECT_EQ(add_func(my_array, 5), 17);
}

} // namespace gandiva
95 changes: 95 additions & 0 deletions cpp/src/gandiva/tests/decimal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ class TestDecimal : public ::testing::Test {
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

// inputs. Unlike the native tests in extended_math_ops_test.cc, this compiles the whole
// round_float64_int32, including the powl(10, scale) fallback in get_scale_multiplier,
// which LLVM lowers to exp10. That is what the Darwin triple fix in engine.cc covers.
// The decimal128 round tests in decimal_test.cc use integer arithmetic instead and
// never reach exp10.
void EvaluateRoundFloat64(const std::vector<double>& values,
const std::vector<int32_t>& scales, bool optimize,
std::shared_ptr<arrow::Array>* result);

// Runs the case with IR optimization off and on, and checks the result each time.
// Without the Darwin triple fix, compiling round(float64, int32) aborts the process
// on macOS ("no libcall available for fexp10"), whatever the scale values are.
void CheckRoundFloat64(const std::vector<double>& values,
const std::vector<int32_t>& scales,
const std::vector<double>& expected_values);

arrow::MemoryPool* pool_;
};

Expand All @@ -67,6 +84,50 @@ std::vector<Decimal128> TestDecimal::MakeDecimalVector(std::vector<std::string>
return ret;
}

void TestDecimal::EvaluateRoundFloat64(const std::vector<double>& values,
const std::vector<int32_t>& scales, bool optimize,
std::shared_ptr<arrow::Array>* result) {
auto field_value = arrow::field("value", arrow::float64());
auto field_scale = arrow::field("scale", arrow::int32());
auto schema = arrow::schema({field_value, field_scale});
auto field_result = arrow::field("result", arrow::float64());

auto round_fn = TreeExprBuilder::MakeFunction(
"round",
{TreeExprBuilder::MakeField(field_value), TreeExprBuilder::MakeField(field_scale)},
arrow::float64());
auto expr = TreeExprBuilder::MakeExpression(round_fn, field_result);
auto configuration = ConfigurationBuilder().build(optimize);

std::shared_ptr<Projector> projector;
auto status = Projector::Make(schema, {expr}, configuration, &projector);
ASSERT_TRUE(status.ok()) << status.message();

std::vector<bool> valid(values.size(), true);
auto in_batch = arrow::RecordBatch::Make(
schema, static_cast<int>(values.size()),
{MakeArrowArrayFloat64(values, valid), MakeArrowArrayInt32(scales, valid)});

arrow::ArrayVector outputs;
status = projector->Evaluate(*in_batch, pool_, &outputs);
ASSERT_TRUE(status.ok()) << status.message();
ASSERT_EQ(outputs.size(), 1u);
*result = outputs.at(0);
}

void TestDecimal::CheckRoundFloat64(const std::vector<double>& values,
const std::vector<int32_t>& scales,
const std::vector<double>& expected_values) {
auto expected = MakeArrowArrayFloat64(expected_values,
std::vector<bool>(expected_values.size(), true));
for (bool optimize : {false, true}) {
SCOPED_TRACE(optimize ? "optimize=true" : "optimize=false");
std::shared_ptr<arrow::Array> result;
ASSERT_NO_FATAL_FAILURE(EvaluateRoundFloat64(values, scales, optimize, &result));
EXPECT_ARROW_ARRAY_EQUALS(expected, result);
}
}

TEST_F(TestDecimal, CastLargeBatch) {
// Allocating decimal return slots in the record loop can exhaust the stack
// when optimization is disabled. A single int32-to-decimal cast is sufficient.
Expand Down Expand Up @@ -1349,4 +1410,38 @@ TEST_F(TestDecimal, TestCastDecimalVarCharInvalidInputInvalidOutput) {
ASSERT_NOT_OK(status);
ASSERT_THAT(status.message(), ::testing::HasSubstr("not a valid decimal128 number"));
}

TEST_F(TestDecimal, TestRoundFloat64ScaleInLookupTable) {
// Scales served by the lookup table
CheckRoundFloat64({1.26, 2.34}, {1, 1}, {1.3, 2.3});
}

TEST_F(TestDecimal, TestRoundFloat64ScaleAtTableBoundary) {
// The first scale outside the lookup table (20); values come back unchanged
CheckRoundFloat64({1.26, -1.26}, {20, 20}, {1.26, -1.26});
}

TEST_F(TestDecimal, TestRoundFloat64LargeScale) {
// Scales well above the table need no rounding
CheckRoundFloat64({1.26, 2.34, -1.26}, {25, 30, 25}, {1.26, 2.34, -1.26});
}

TEST_F(TestDecimal, TestRoundFloat64NegativeScale) {
// Negative scales are also outside the table; these inputs round to 0
CheckRoundFloat64({1.26, 2.34}, {-2, -1}, {0.0, 0.0});
}

TEST_F(TestDecimal, TestRoundFloat64MixedScalesInOneBatch) {
// Table path (1), exp10 path (25, -1) and the table's last entry (19) in one batch
CheckRoundFloat64({1.26, 1.26, 2.5, 1.26}, {1, 25, -1, 19}, {1.3, 1.26, 0.0, 1.26});
}

TEST_F(TestDecimal, TestRoundFloat64LargeValuesSkipRounding) {
// |x| >= 2^52 / 10^scale is returned unchanged (the DX-90291 overflow guard)
CheckRoundFloat64({1e300, -1e300, 4.5e15}, {1, 1, 1}, {1e300, -1e300, 4.5e15});
}

TEST_F(TestDecimal, TestRoundFloat64ZeroValue) {
CheckRoundFloat64({0.0, 0.0, 0.0}, {0, 25, -3}, {0.0, 0.0, 0.0});
}
} // namespace gandiva
Loading