From a7bdb4e0222f9a53c1ccb78a7de862691a20891c Mon Sep 17 00:00:00 2001 From: Harald van Dijk Date: Mon, 29 Jul 2024 13:20:51 +0100 Subject: [PATCH] Auto-disable deferred compilation. CA_HOST_DUMP_ASM=1 and deferred compilation are incompatible. Our current behavior is that CA_HOST_DUMP_ASM is effectively silently ignored, making for a poor user experience. With this change, when CA_HOST_DUMP_ASM=1 is set and deferred compilation would normally be implicitly enabled, deferred compilation will be disabled and a warning is printed for it. If an explicit CA_HOST_DEFERRED_COMPILATION=1 is set, deferred compilation can be forced. This still works and results in no assembly being printed. --- .../source/info.cpp | 1 - modules/compiler/include/compiler/info.h | 9 +++--- modules/compiler/riscv/source/info.cpp | 1 - .../compiler/targets/host/include/host/info.h | 6 ++++ modules/compiler/targets/host/source/info.cpp | 28 +++++++++++++++++-- .../compiler/targets/host/source/target.cpp | 2 +- modules/compiler/test/common.h | 2 +- source/cl/source/program.cpp | 4 +-- 8 files changed, 40 insertions(+), 13 deletions(-) diff --git a/modules/compiler/cookie/{{cookiecutter.target_name}}/source/info.cpp b/modules/compiler/cookie/{{cookiecutter.target_name}}/source/info.cpp index 00d0065c1..4b6a8d784 100644 --- a/modules/compiler/cookie/{{cookiecutter.target_name}}/source/info.cpp +++ b/modules/compiler/cookie/{{cookiecutter.target_name}}/source/info.cpp @@ -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; diff --git a/modules/compiler/include/compiler/info.h b/modules/compiler/include/compiler/info.h index 1a1ced723..36a7468df 100644 --- a/modules/compiler/include/compiler/info.h +++ b/modules/compiler/include/compiler/info.h @@ -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. /// @@ -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. diff --git a/modules/compiler/riscv/source/info.cpp b/modules/compiler/riscv/source/info.cpp index 84b5e4bd4..aa18110b1 100644 --- a/modules/compiler/riscv/source/info.cpp +++ b/modules/compiler/riscv/source/info.cpp @@ -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; } diff --git a/modules/compiler/targets/host/include/host/info.h b/modules/compiler/targets/host/include/host/info.h index 0d9df3c4c..30b892d83 100644 --- a/modules/compiler/targets/host/include/host/info.h +++ b/modules/compiler/targets/host/include/host/info.h @@ -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 diff --git a/modules/compiler/targets/host/source/info.cpp b/modules/compiler/targets/host/source/info.cpp index 59c5d9801..f51b88735 100644 --- a/modules/compiler/targets/host/source/info.cpp +++ b/modules/compiler/targets/host/source/info.cpp @@ -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. @@ -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 @@ -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 HostInfo::createTarget( compiler::Context *context, compiler::NotifyCallbackFn callback) const { if (!context) { diff --git a/modules/compiler/targets/host/source/target.cpp b/modules/compiler/targets/host/source/target.cpp index 8c6064ef3..8f8783fa1 100644 --- a/modules/compiler/targets/host/source/target.cpp +++ b/modules/compiler/targets/host/source/target.cpp @@ -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); diff --git a/modules/compiler/test/common.h b/modules/compiler/test/common.h index 34a272f53..4319df6fd 100644 --- a/modules/compiler/test/common.h +++ b/modules/compiler/test/common.h @@ -319,7 +319,7 @@ static inline std::string printDeviceName( static inline std::vector deferrableCompilers() { std::vector deferrable_compilers; for (const compiler::Info *compiler : compiler::compilers()) { - if (compiler->supports_deferred_compilation) { + if (compiler->supports_deferred_compilation()) { deferrable_compilers.emplace_back(compiler); } } diff --git a/source/cl/source/program.cpp b/source/cl/source/program.cpp index fa72ac3c8..966837ff7 100644 --- a/source/cl/source/program.cpp +++ b/source/cl/source/program.cpp @@ -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) { @@ -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) {