Skip to content

Commit

Permalink
Merge pull request #512 from hvdijk/disable-deferred-compilation
Browse files Browse the repository at this point in the history
Auto-disable deferred compilation.
  • Loading branch information
hvdijk authored Jul 29, 2024
2 parents a776d72 + a7bdb4e commit 538093a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace {{cookiecutter.target_name}} {
device_info = mux_device_info;
vectorizable = true;
dma_optimizable = true;
supports_deferred_compilation = false;
scalable_vector_support = {{cookiecutter.scalable_vector}};
kernel_debug = true;

Expand Down
9 changes: 5 additions & 4 deletions modules/compiler/include/compiler/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ struct Info {
/// @brief Mux device info that this compiler will target. Must not be
/// `nullptr`.
mux_device_info_t device_info = nullptr;
/// @brief Is `true` if the compiler supports deferred compilation (i.e.
/// compiler::Module::getKernel() and the compiler::Kernel class are
/// implemented), `false` otherwise.
bool supports_deferred_compilation = false;
/// @brief A semicolon-separated, null-terminated list with static lifetime
/// duration, of this Mux device's custom compile options.
///
Expand Down Expand Up @@ -131,6 +127,11 @@ struct Info {

return caps;
}

/// @brief Returns `true` if the compiler supports deferred compilation (i.e.
/// compiler::Module::getKernel() and the compiler::Kernel class are
/// implemented), `false` otherwise.
virtual bool supports_deferred_compilation() const { return false; }
};

/// @brief A functor which is called when a target wants to expose a compiler.
Expand Down
1 change: 0 additions & 1 deletion modules/compiler/riscv/source/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ RiscvInfo::RiscvInfo(mux_device_info_t mux_device_info,

vectorizable = false;
dma_optimizable = true;
supports_deferred_compilation = false;
kernel_debug = true;
}

Expand Down
6 changes: 6 additions & 0 deletions modules/compiler/targets/host/include/host/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ struct HostInfo : compiler::Info {

/// @brief Bitfield of all `host::arch`'s being targeted.
static uint8_t arches;

bool supports_deferred_compilation() const override;

private:
bool deferred_compilation_enabled;
mutable const char *deferred_compilation_warning;
};

} // namespace host
Expand Down
28 changes: 25 additions & 3 deletions modules/compiler/targets/host/source/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,24 @@ HostInfo::HostInfo(host::arch arch, host::os os,
// If we're instantiating a compiler for the current system, then we know it
// supports runtime compilation, otherwise it's a cross compiler.
device_info = host_device_info;
supports_deferred_compilation = host_device_info->native;
deferred_compilation_enabled = host_device_info->native;
deferred_compilation_warning = nullptr;

// JIT compilation is not yet supported on RISC-V
if (arch == host::arch::RISCV32 || arch == host::arch::RISCV64) {
supports_deferred_compilation = false;
deferred_compilation_enabled = false;
}

#if !defined(NDEBUG) || defined(CA_ENABLE_DEBUG_SUPPORT)
if (deferred_compilation_enabled) {
if (std::getenv("CA_HOST_DUMP_ASM")) {
deferred_compilation_warning =
"CA_HOST_DUMP_ASM requires non-deferred compilation";
deferred_compilation_enabled = false;
}
}
#endif

// If the user wants to override, let them. This may be useful for testing the
// non-deferred-compilation support, or may be useful for testing future LLVM
// improvements that may provide RISC-V JIT support.
Expand All @@ -83,7 +94,7 @@ HostInfo::HostInfo(host::arch arch, host::os os,
char *end;
const long val = std::strtol(env, &end, 10);
if (*env != '\0' && (val == 0 || val == 1) && *end == '\0') {
supports_deferred_compilation = val;
deferred_compilation_enabled = val;
}
}
#endif
Expand All @@ -105,6 +116,17 @@ HostInfo::HostInfo(host::arch arch, host::os os,
#endif
}

bool HostInfo::supports_deferred_compilation() const {
const bool enabled = deferred_compilation_enabled;
if (auto *warning = deferred_compilation_warning) {
(void)fprintf(stderr, "warning: %s. %s\n", warning,
enabled ? "Deferred compilation forced enabled."
: "Deferred compilation disabled.");
deferred_compilation_warning = nullptr;
}
return enabled;
}

std::unique_ptr<compiler::Target> HostInfo::createTarget(
compiler::Context *context, compiler::NotifyCallbackFn callback) const {
if (!context) {
Expand Down
2 changes: 1 addition & 1 deletion modules/compiler/targets/host/source/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ compiler::Result HostTarget::initWithBuiltins(
Features = std::move(NativeFeatures);
}

if (compiler_info->supports_deferred_compilation) {
if (compiler_info->supports_deferred_compilation()) {
llvm::orc::JITTargetMachineBuilder TMBuilder(triple);
TMBuilder.setCPU(CPU);
TMBuilder.setCodeGenOptLevel(multi_llvm::CodeGenOptLevel::Aggressive);
Expand Down
2 changes: 1 addition & 1 deletion modules/compiler/test/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ static inline std::string printDeviceName(
static inline std::vector<const compiler::Info *> deferrableCompilers() {
std::vector<const compiler::Info *> deferrable_compilers;
for (const compiler::Info *compiler : compiler::compilers()) {
if (compiler->supports_deferred_compilation) {
if (compiler->supports_deferred_compilation()) {
deferrable_compilers.emplace_back(compiler);
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/cl/source/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ bool cl::device_program::finalize(cl_device_id device) {

// If the compiler does not support deferred compilation, we get the final
// binary from the module and initialize a mux_kernel_cache.
if (!device->compiler_info->supports_deferred_compilation) {
if (!device->compiler_info->supports_deferred_compilation()) {
// Get the Mux binary.
auto binary_result = compiler_module.getOrCreateMuxBinary();
if (!binary_result) {
Expand Down Expand Up @@ -223,7 +223,7 @@ cl::device_program::createKernel(cl_device_id device,
} break;

case cl::device_program_type::COMPILER_MODULE: {
if (device->compiler_info->supports_deferred_compilation) {
if (device->compiler_info->supports_deferred_compilation()) {
compiler::Kernel *deferred_kernel =
compiler_module.module->getKernel(kernel_name);
if (!deferred_kernel) {
Expand Down

0 comments on commit 538093a

Please sign in to comment.