From 198f77afc587c08baef8c904dd407c1b90dd6aaa Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 06:07:19 +0100 Subject: [PATCH 01/27] refactor(compilerlib): run compile() through runCompilation compile() built its context and ran the whole compilation itself. The compilation now runs in runCompilation, from the context it is given, which compileToModule will share. Refs #263 --- src/compilerlib/compiler.cpp | 120 +++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 56 deletions(-) diff --git a/src/compilerlib/compiler.cpp b/src/compilerlib/compiler.cpp index 025a8c15..91e4c656 100644 --- a/src/compilerlib/compiler.cpp +++ b/src/compilerlib/compiler.cpp @@ -1294,72 +1294,80 @@ namespace compilerlib return {true, mergeDiagnostics(ctx.driver_diagnostics, cc1_diags), {}}; } - } // namespace - - CT_NODISCARD CompileResult compile(const std::vector& input_args, OutputMode mode, - bool instrument) - { - CompileContext ctx(input_args, mode, instrument); - ArgBuilder argBuilder(ctx); - std::string error; + // The compilation `ctx` describes, from its arguments to its result. + CT_NODISCARD CompileResult runCompilation(CompileContext& ctx) + { + ArgBuilder argBuilder(ctx); + std::string error; - if (!argBuilder.build(error)) - return {false, std::move(error), {}}; + if (!argBuilder.build(error)) + return {false, std::move(error), {}}; - auto diags = createDriverDiagnostics(ctx); + auto diags = createDriverDiagnostics(ctx); - DriverSession driver(ctx, *diags); - std::unique_ptr comp = driver.buildCompilation(error); - if (!comp) - { - if (error.empty()) - error = ctx.dc.message.empty() ? "failed to build compilation" - : std::move(ctx.dc.message); - return {false, std::move(error), {}}; - } + DriverSession driver(ctx, *diags); + std::unique_ptr comp = driver.buildCompilation(error); + if (!comp) + { + if (error.empty()) + error = ctx.dc.message.empty() ? "failed to build compilation" + : std::move(ctx.dc.message); + return {false, std::move(error), {}}; + } - if (comp->getJobs().empty()) - return { - false, ctx.dc.message.empty() ? "no jobs to run" : std::move(ctx.dc.message), {}}; - - // Instrumented LTO is not supported (#163): the bitcode would be written without its - // summary, and the link-time pipeline would optimize the instrumented code again, - // which no test covers. The driver resolves -flto, -flto= and -fno-lto. - if (instrument && clang_compat::usesLto(*comp)) - return {false, - "error: ct: --instrument does not support -flto: compile instrumented code " - "without link-time optimization", - {}}; + if (comp->getJobs().empty()) + return {false, + ctx.dc.message.empty() ? "no jobs to run" : std::move(ctx.dc.message), + {}}; + + // Instrumented LTO is not supported (#163): the bitcode would be written without its + // summary, and the link-time pipeline would optimize the instrumented code again, + // which no test covers. The driver resolves -flto, -flto= and -fno-lto. + if (ctx.instrument && clang_compat::usesLto(*comp)) + return {false, + "error: ct: --instrument does not support -flto: compile instrumented code " + "without link-time optimization", + {}}; + + if (!ctx.instrument && ctx.mode == OutputMode::ToFile && + !ctx.runtimeConfig.optnone_enabled) + return runNonInstrumentedCompilation(ctx, driver, *comp); + + JobPlan plan = buildJobPlan(*comp); + if (!validateJobPlan(plan, ctx.mode, error)) + return {false, std::move(error), {}}; + + if (!ctx.dc.message.empty()) + { + ctx.driver_diagnostics = std::move(ctx.dc.message); + ctx.dc.message.clear(); + ctx.dc.os.flush(); + } - if (!instrument && mode == OutputMode::ToFile && !ctx.runtimeConfig.optnone_enabled) - return runNonInstrumentedCompilation(ctx, driver, *comp); + Cc1Runner cc1(ctx, *diags); + if (ctx.mode == OutputMode::ToMemory || ctx.mode == OutputMode::ToMemoryBitcode) + return cc1.runSingle(*plan.cc1Jobs.front()); - JobPlan plan = buildJobPlan(*comp); - if (!validateJobPlan(plan, mode, error)) - return {false, std::move(error), {}}; + if (plan.cc1Jobs.empty()) + { + Linker linker(hasArg(ctx.filtered_args, "-v")); + if (!linker.run(plan.otherJobs, error)) + return {false, mergeDiagnostics(ctx.driver_diagnostics, error), {}}; + return {true, mergeDiagnostics(ctx.driver_diagnostics, {}), {}}; + } - if (!ctx.dc.message.empty()) - { - ctx.driver_diagnostics = std::move(ctx.dc.message); - ctx.dc.message.clear(); - ctx.dc.os.flush(); + if (ctx.instrument) + return runInstrumentedToFile(ctx, cc1, plan, error); + return runPlainToFile(ctx, cc1, plan, error); } - Cc1Runner cc1(ctx, *diags); - if (mode == OutputMode::ToMemory || mode == OutputMode::ToMemoryBitcode) - return cc1.runSingle(*plan.cc1Jobs.front()); - - if (plan.cc1Jobs.empty()) - { - Linker linker(hasArg(ctx.filtered_args, "-v")); - if (!linker.run(plan.otherJobs, error)) - return {false, mergeDiagnostics(ctx.driver_diagnostics, error), {}}; - return {true, mergeDiagnostics(ctx.driver_diagnostics, {}), {}}; - } + } // namespace - if (instrument) - return runInstrumentedToFile(ctx, cc1, plan, error); - return runPlainToFile(ctx, cc1, plan, error); + CT_NODISCARD CompileResult compile(const std::vector& input_args, OutputMode mode, + bool instrument) + { + CompileContext ctx(input_args, mode, instrument); + return runCompilation(ctx); } } // namespace compilerlib From 897f3211d3c98e02510cd36d061bc526d39a252f Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 06:07:45 +0100 Subject: [PATCH 02/27] feat(compilerlib): compile to an llvm::Module in the caller's context The analyzers compile each source to textual IR or bitcode, then read it back into a module of their own: the module was written and parsed again for nothing. compileToModule(args, context, instrument) gives the module itself, created in the caller's context, with the arguments of an LLVM IR compilation (-S -emit-llvm or -c -emit-llvm). The output file is not written; the other files the arguments ask for, such as a dependency file, are. The module is the one compile() writes, instrumented or not. Code generation sets whether the context discards value names; the context's own setting is restored, since it applies to the modules read into it later, and so is the optimization record that Clang's action leaves on it before LLVM 22. Refs #263 --- include/compilerlib/compile_module.hpp | 40 +++++++++ src/compilerlib/clang_compat.hpp | 13 +++ src/compilerlib/compiler.cpp | 119 ++++++++++++++++++++++++- 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 include/compilerlib/compile_module.hpp diff --git a/include/compilerlib/compile_module.hpp b/include/compilerlib/compile_module.hpp new file mode 100644 index 00000000..e29d93ab --- /dev/null +++ b/include/compilerlib/compile_module.hpp @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: Apache-2.0 +#ifndef COMPILERLIB_COMPILE_MODULE_HPP +#define COMPILERLIB_COMPILE_MODULE_HPP + +#include "compilerlib/attributes.hpp" + +#include +#include + +#include +#include +#include + +namespace compilerlib +{ + + struct ModuleResult + { + bool success = false; + std::string diagnostics; + /// The module, in the context compileToModule was given; null when the compilation + /// failed. + std::unique_ptr module; + }; + + /// Compiles one source to the module that compile() writes, created in `context`, for a + /// host that works on the module itself: it is neither written as text or bitcode nor read + /// back. `args` are those of an LLVM IR compilation, `-S -emit-llvm` or `-c -emit-llvm`; + /// the output file is not written, and the other files the arguments ask for, such as a + /// dependency file, are. With `instrument`, the module is the instrumented one. + /// + /// Code generation sets options of `context` as it does with clang, except whether value + /// names are discarded, which is restored: modules read into `context` later keep theirs. + /// No other thread may use `context` during the compilation. + CT_NODISCARD ModuleResult compileToModule(const std::vector& args, + llvm::LLVMContext& context, bool instrument = false); + +} // namespace compilerlib + +#endif // COMPILERLIB_COMPILE_MODULE_HPP diff --git a/src/compilerlib/clang_compat.hpp b/src/compilerlib/clang_compat.hpp index c47168a6..a286ef18 100644 --- a/src/compilerlib/clang_compat.hpp +++ b/src/compilerlib/clang_compat.hpp @@ -68,6 +68,19 @@ namespace compilerlib::clang_compat return file; } + // Removes from `context` the record that Clang's frontend action set up on it, for + // -fsave-optimization-record: before LLVM 22, the action closes the file and leaves the + // streamer, which would write into it; LLVM 22's handle removes the streamer itself. + inline void releaseFrontendOptimizationRecord(llvm::LLVMContext& context) + { +#if LLVM_VERSION_MAJOR >= 22 + (void)context; +#else + context.setLLVMRemarkStreamer(nullptr); + context.setMainRemarkStreamer(nullptr); +#endif + } + // Removes the record's streamer from `context`, which must not write into `file` once // it is released. inline void releaseOptimizationRecord(llvm::LLVMContext& context, RemarkFile& file) diff --git a/src/compilerlib/compiler.cpp b/src/compilerlib/compiler.cpp index 91e4c656..b47e5abb 100644 --- a/src/compilerlib/compiler.cpp +++ b/src/compilerlib/compiler.cpp @@ -1,5 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 #include "compilerlib/compiler.h" +#include "compilerlib/compile_module.hpp" #include "compilerlib/attributes.hpp" #include "compilerlib/toolchain.hpp" @@ -75,6 +76,9 @@ namespace compilerlib class LocatingCodegenAction : public clang::EmitLLVMOnlyAction { public: + // The module is created in the given context, or in one of the action's own. + using clang::EmitLLVMOnlyAction::EmitLLVMOnlyAction; + emit::FunctionLocations takeFunctionLocations(void) { return std::move(functions_); @@ -223,6 +227,11 @@ namespace compilerlib std::string clang_sysroot; RuntimeArchives runtime_archives; llvm::IntrusiveRefCntPtr fs; + // compileToModule's context, where code generation creates the module, which the + // compilation then leaves in `module`; null for compile(), whose actions create + // contexts of their own. + llvm::LLVMContext* module_context = nullptr; + std::unique_ptr module; // Options of the driver's diagnostics engine, which must outlive it. clang_compat::DiagnosticOptionsStorage driver_diagnostic_options; DiagsSaver dc; @@ -699,7 +708,96 @@ namespace compilerlib return result; } + // compileToModule's job: the module stays in ctx_.module. + CompileResult runToModule(const clang::driver::Command& job) + { + CompileResult result; + if (!runOnClangStack([&] { result = runToModuleOnThisThread(job); })) + { + ctx_.module.reset(); + return {false, "compilation crashed", {}}; + } + return result; + } + private: + // What code generation changes in compileToModule's context, which the host + // keeps: whether value names are discarded, which would apply to the modules the + // host reads into it later, and the optimization record of + // -fsave-optimization-record, whose file is closed once the job ends. + class ModuleContextScope + { + public: + ModuleContextScope(llvm::LLVMContext& context, bool optimizationRecord) + : context_(context), discardValueNames_(context.shouldDiscardValueNames()), + optimizationRecord_(optimizationRecord) + { + } + + ModuleContextScope(const ModuleContextScope&) = delete; + ModuleContextScope& operator=(const ModuleContextScope&) = delete; + + ~ModuleContextScope() + { + context_.setDiscardValueNames(discardValueNames_); + if (optimizationRecord_) + clang_compat::releaseFrontendOptimizationRecord(context_); + } + + private: + llvm::LLVMContext& context_; + bool discardValueNames_; + bool optimizationRecord_; + }; + + CompileResult runToModuleOnThisThread(const clang::driver::Command& job) + { + auto ci = makeCompilerInstance(job.getArguments()); + if (!ci) + return {false, "failed to create compiler instance", {}}; + switch (ci->getFrontendOpts().ProgramAction) + { + case clang::frontend::EmitLLVM: + case clang::frontend::EmitBC: + break; + default: + return {false, + mergeDiagnostics(ctx_.driver_diagnostics, + "a module needs an LLVM IR compilation: pass -S " + "-emit-llvm or -c -emit-llvm"), + {}}; + } + Cc1TimeTrace trace(*ci, job.getExecutable()); + + std::string error; + auto keepModule = [&](std::unique_ptr module) -> bool + { + if (ctx_.instrument && !instrument(*module, error)) + return false; + ctx_.module = std::move(module); + return true; + }; + bool ok = false; + { + ModuleContextScope scope(*ctx_.module_context, + !ci->getCodeGenOpts().OptRecordFile.empty()); + ok = ctx_.runtimeConfig.optnone_enabled + ? runCodegenWithModule< + frontend::OptNoneAction>( + *ci, keepModule, error) + : runCodegenWithModule(*ci, keepModule, + error); + } + if (!ok) + { + ctx_.module.reset(); + return {false, mergeDiagnostics(ctx_.driver_diagnostics, error), {}}; + } + // Before the diagnostics are collected: writing the trace may report one. + trace.finish(); + return {true, mergeDiagnostics(ctx_.driver_diagnostics, ctx_.dc.message), {}}; + } + CT_NODISCARD bool runInstrumentedOnThisThread(const clang::driver::Command& job, std::string& error) { @@ -974,7 +1072,8 @@ namespace compilerlib options.OptRecordFile.clear(); } - Action action; + // compileToModule's context, or one of the action's own. + Action action(ctx_.module_context); resetDiagnostics(); if (!ci.ExecuteAction(action)) { @@ -1345,6 +1444,8 @@ namespace compilerlib } Cc1Runner cc1(ctx, *diags); + if (ctx.module_context) + return cc1.runToModule(*plan.cc1Jobs.front()); if (ctx.mode == OutputMode::ToMemory || ctx.mode == OutputMode::ToMemoryBitcode) return cc1.runSingle(*plan.cc1Jobs.front()); @@ -1370,6 +1471,22 @@ namespace compilerlib return runCompilation(ctx); } + CT_NODISCARD ModuleResult compileToModule(const std::vector& args, + llvm::LLVMContext& context, bool instrument) + { + // The modes of in-memory output: a single -cc1 job, and -c kept, which a bitcode + // compilation needs. + CompileContext ctx(args, OutputMode::ToMemoryBitcode, instrument); + ctx.module_context = &context; + CompileResult result = runCompilation(ctx); + ModuleResult moduleResult; + moduleResult.success = result.success && ctx.module != nullptr; + moduleResult.diagnostics = std::move(result.diagnostics); + if (moduleResult.success) + moduleResult.module = std::move(ctx.module); + return moduleResult; + } + } // namespace compilerlib namespace From c5a42e3dbefef6d516874c5542db21782cd31f81 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 06:07:45 +0100 Subject: [PATCH 03/27] test(compilerlib): check the modules compileToModule gives compileToModule gives the module that compile() writes as text, at -O0 and -O2, instrumented or not, and the module of a bitcode compilation, whose output file is not written and whose dependency file is. It fails on a compilation that is not to LLVM IR, reports the errors of a broken source, and leaves the context's setting of value names. Refs #263 --- test/unit/compile_test.cpp | 143 +++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index 4c6e7419..44cab45c 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -1,4 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 +#include "compilerlib/compile_module.hpp" #include "compilerlib/compiler.h" #include @@ -31,6 +32,7 @@ #include #include #include +#include #include #ifndef _WIN32 @@ -687,6 +689,147 @@ int main(void) EXPECT_NE(memory.diagnostics.find("missing"), std::string::npos) << memory.diagnostics; } + std::string printed(const llvm::Module& module) + { + std::string text; + llvm::raw_string_ostream stream(text); + module.print(stream, nullptr); + stream.flush(); + return text; + } + + // The text of `module` as the IR parser gives it back: a module read from text does not + // keep the order of each value's uses, which orders the predecessors listed for a block. + std::string normalizedText(const llvm::Module& module) + { + llvm::LLVMContext context; + llvm::SMDiagnostic error; + std::unique_ptr parsed = + llvm::parseAssemblyString(printed(module), error, context); + if (!parsed) + return "unreadable module: " + error.getMessage().str(); + parsed->setModuleIdentifier(module.getModuleIdentifier()); + return printed(*parsed); + } + + // An allocation, a loop and accesses for the instrumentation to rewrite. + constexpr const char* kModuleUnit = R"(#include + +int sum(int count) +{ + int* values = malloc(sizeof(int) * count); + int total = 0; + for (int i = 0; i < count; ++i) + { + values[i] = i; + total += values[i]; + } + free(values); + return total; +} +)"; + + // compileToModule gives the module that compile() writes as text, without the text. + class ModuleOutputTest : public CompileTest, + public ::testing::WithParamInterface> + { + }; + + TEST_P(ModuleOutputTest, IsTheTextualOutput) + { + const auto [instrument, optimization] = GetParam(); + const std::vector args{optimization, "-g", "-S", "-emit-llvm", + writeSource("unit.c", kModuleUnit)}; + compilerlib::CompileResult text = + compilerlib::compile(args, compilerlib::OutputMode::ToMemory, instrument); + ASSERT_TRUE(text.success) << text.diagnostics; + + llvm::LLVMContext context; + compilerlib::ModuleResult result = compilerlib::compileToModule(args, context, instrument); + ASSERT_TRUE(result.success) << result.diagnostics; + ASSERT_NE(result.module, nullptr); + EXPECT_EQ(&result.module->getContext(), &context); + EXPECT_EQ(result.diagnostics, text.diagnostics); + + llvm::LLVMContext textContext; + llvm::SMDiagnostic error; + std::unique_ptr parsed = + llvm::parseAssemblyString(text.llvmIR, error, textContext); + ASSERT_NE(parsed, nullptr) << error.getMessage().str(); + parsed->setModuleIdentifier(result.module->getModuleIdentifier()); + EXPECT_EQ(normalizedText(*result.module), normalizedText(*parsed)); + } + + INSTANTIATE_TEST_SUITE_P(Modes, ModuleOutputTest, + ::testing::Combine(::testing::Bool(), ::testing::Values("-O0", "-O2")), + [](const ::testing::TestParamInfo>& info) + { + return std::string(std::get<0>(info.param) ? "Instrumented" + : "Plain") + + (std::string(std::get<1>(info.param)) == "-O0" ? "O0" + : "O2"); + }); + + // A bitcode compilation gives its module too. Its output file is not written; the + // dependency file it asks for is. + TEST_F(CompileTest, ModuleOfABitcodeCompilation) + { + llvm::LLVMContext context; + compilerlib::ModuleResult result = + compilerlib::compileToModule({"-emit-llvm", "-c", writeSource("unit.c", kBitcodeUnit), + "-o", path("unit.bc"), "-MD", "-MF", path("unit.d")}, + context); + ASSERT_TRUE(result.success) << result.diagnostics; + ASSERT_NE(result.module, nullptr); + EXPECT_NE(result.module->getFunction("main"), nullptr); + EXPECT_FALSE(fs::exists(path("unit.bc"))); + EXPECT_NE(readFile(path("unit.d")).find("unit.c"), std::string::npos); + } + + TEST_F(CompileTest, ModuleNeedsAnIRCompilation) + { + llvm::LLVMContext context; + compilerlib::ModuleResult result = compilerlib::compileToModule( + {"-c", writeSource("unit.c", kBitcodeUnit), "-o", path("unit.o")}, context); + EXPECT_FALSE(result.success); + EXPECT_EQ(result.module, nullptr); + EXPECT_NE(result.diagnostics.find("-emit-llvm"), std::string::npos) << result.diagnostics; + EXPECT_FALSE(fs::exists(path("unit.o"))); + } + + TEST_F(CompileTest, ModuleReportsCompileErrors) + { + llvm::LLVMContext context; + compilerlib::ModuleResult result = compilerlib::compileToModule( + {"-S", "-emit-llvm", writeSource("broken.c", "int main(void) { return missing; }\n")}, + context); + EXPECT_FALSE(result.success); + EXPECT_EQ(result.module, nullptr); + EXPECT_NE(result.diagnostics.find("missing"), std::string::npos) << result.diagnostics; + } + + // Code generation names the values as -f[no-]discard-value-names asks, and the context + // keeps its own setting, which applies to the modules read into it later. + TEST_F(CompileTest, ModuleLeavesTheValueNamesSettingOfTheContext) + { + const std::string source = writeSource("unit.c", kBitcodeUnit); + for (bool discard : {false, true}) + { + llvm::LLVMContext context; + context.setDiscardValueNames(discard); + for (bool names : {false, true}) + { + compilerlib::ModuleResult result = compilerlib::compileToModule( + {names ? "-fno-discard-value-names" : "-fdiscard-value-names", "-S", + "-emit-llvm", source}, + context); + ASSERT_TRUE(result.success) << result.diagnostics; + EXPECT_EQ(printed(*result.module).find("%call ") != std::string::npos, names); + EXPECT_EQ(context.shouldDiscardValueNames(), discard); + } + } + } + // Like clang, a failed compilation leaves no output behind: neither a partial object // nor an earlier one that would look up to date. class FailedCompilationTest : public CompileTest, From 7f065f0ee5d6cc7f9f9668f38b533f77a0af55f4 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 06:08:10 +0100 Subject: [PATCH 04/27] docs(readme): describe compiling to a module Refs #263 --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 8c0121b9..78b4cdec 100644 --- a/README.md +++ b/README.md @@ -498,3 +498,25 @@ expects, and waits for it: a host can compile from a thread with a small stack, secondary thread on macOS, whose stack is 512 KiB (#204). The time-trace profiler a host runs on its own thread, started with `llvm::timeTraceProfilerInitialize`, therefore does not record the compilation. With `-ftime-trace`, each job writes its own trace file, as clang does. + +### Compiling to a module + +`compilerlib::compileToModule`, from `compilerlib/compile_module.hpp`, gives the module that +`compilerlib::compile` writes as text or bitcode, created in an `llvm::LLVMContext` of the host, +instead of writing it for the host to read it back: + +```cpp +#include + +llvm::LLVMContext context; +compilerlib::ModuleResult result = + compilerlib::compileToModule({"-g", "-c", "-emit-llvm", "unit.cpp"}, context); +if (result.success) + analyze(*result.module); +``` + +Its arguments are those of an LLVM IR compilation, `-S -emit-llvm` or `-c -emit-llvm`. The output +file is not written; the other files the arguments ask for, such as a dependency file, are. With +`instrument`, the module is the instrumented one. Code generation sets options of the context, as +it does with clang, except whether value names are discarded: the context keeps its own setting, +which applies to the modules read into it later. From cad1ccadb5d4c20d06a13457787078ca46845ac6 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 06:58:42 +0100 Subject: [PATCH 05/27] docs(changelog): note compiling to a module Refs #263 --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a114532..c38a584c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,15 @@ pull request. ## Unreleased +### Features + +- **Compiling to a module.** `compilerlib::compileToModule`, from + `compilerlib/compile_module.hpp`, gives the module that `compilerlib::compile` writes as text + or bitcode, created in an `llvm::LLVMContext` of the host, instead of writing it for the host + to read it back. Its arguments are those of an LLVM IR compilation; the output file is not + written, and the other files the arguments ask for, such as a dependency file, are. The + context keeps its own setting of value names (#265, #263). + ### Fixes - **A child of `fork()` and the vtable diagnostics, on Linux.** With `--ct-vtable-diag`, a From e66cf7d9fbb378a96310c56ba052bd596580fe80 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 06:58:43 +0100 Subject: [PATCH 06/27] docs(changelog): note the tests of compileToModule Refs #263 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c38a584c..a981cfd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -189,6 +189,10 @@ pull request. - When `sbrk()` fails in the sbrk test of the full allocation table, the test reports `errno`, the program break and the mapping above it, `RLIMIT_DATA`, and the memory of the process and of the system (#239, #233). +- Unit tests check that `compileToModule` gives the module that `compile` writes as text, at -O0 + and -O2, instrumented or not, and the module of a bitcode compilation, whose dependency file is + written and output file is not; that it fails on a compilation to an object and on a broken + source; and that the context keeps its setting of value names (#265, #263). - The Docker tests build and test the image in a job per architecture, each on a runner of its own architecture: arm64 on `ubuntu-24.04-arm` instead of under QEMU on an amd64 runner. Under QEMU's user-mode emulation, `sbrk()` failed now and then in the sbrk test of the full From 3dd3255d2621b7b94d324f6f6fda6d6634a62e04 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 06:58:43 +0100 Subject: [PATCH 07/27] docs(changelog): note the README section on modules Refs #263 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a981cfd7..c7c31f86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -221,6 +221,7 @@ pull request. ### Documentation +- The README describes compiling to a module with `compileToModule` (#265, #263). - `NOTICE` dates the copyright from 2025, when the code began, credits the CoreTrace contributors alongside Hugo Payet and links the repository. It keeps only these attribution notices, without the license header meant for source files (#248). From 683816469fabde56094c53a31f9ae47a3d98a78a Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 07:43:16 +0100 Subject: [PATCH 08/27] fix(compilerlib): keep value names in an LLVM 16 context given some With LLVM 16, destroying a module of compileToModule freed memory twice when the compilation named values and the context, restored after it, discarded names: LLVM 16's Value::setName("") does nothing in such a context, so Function::clearArguments leaves each argument's name in the function's symbol table, which frees it again. LLVM 17 removes the name. With LLVM 16, a context that discards names now keeps them after a compilation that names values. The Linux / LLVM 16 job of the CI aborted in ModuleLeavesTheValueNamesSettingOfTheContext. Refs #263 --- include/compilerlib/compile_module.hpp | 2 ++ src/compilerlib/compiler.cpp | 9 ++++++++- test/unit/compile_test.cpp | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/compilerlib/compile_module.hpp b/include/compilerlib/compile_module.hpp index e29d93ab..4e052a0c 100644 --- a/include/compilerlib/compile_module.hpp +++ b/include/compilerlib/compile_module.hpp @@ -31,6 +31,8 @@ namespace compilerlib /// /// Code generation sets options of `context` as it does with clang, except whether value /// names are discarded, which is restored: modules read into `context` later keep theirs. + /// With LLVM 16, a context that discards names keeps them after a compilation that names + /// values, which LLVM 16 could not destroy in a context that discards names. /// No other thread may use `context` during the compilation. CT_NODISCARD ModuleResult compileToModule(const std::vector& args, llvm::LLVMContext& context, bool instrument = false); diff --git a/src/compilerlib/compiler.cpp b/src/compilerlib/compiler.cpp index b47e5abb..da4984a6 100644 --- a/src/compilerlib/compiler.cpp +++ b/src/compilerlib/compiler.cpp @@ -739,7 +739,14 @@ namespace compilerlib ~ModuleContextScope() { - context_.setDiscardValueNames(discardValueNames_); +#if LLVM_VERSION_MAJOR < 17 + // LLVM 16 cannot destroy a value with a name in a context that discards + // names: Value::setName("") then leaves the name of a function's argument + // in the function's symbol table, which frees it a second time. A context + // that discards names keeps them, once a compilation gave its module some. + if (!discardValueNames_ || context_.shouldDiscardValueNames()) +#endif + context_.setDiscardValueNames(discardValueNames_); if (optimizationRecord_) clang_compat::releaseFrontendOptimizationRecord(context_); } diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index 44cab45c..e9e3fd04 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -825,7 +825,13 @@ int sum(int count) context); ASSERT_TRUE(result.success) << result.diagnostics; EXPECT_EQ(printed(*result.module).find("%call ") != std::string::npos, names); +#if LLVM_VERSION_MAJOR < 17 + // LLVM 16 could not destroy the module's named values once the context + // discards names again: the context keeps them. + EXPECT_EQ(context.shouldDiscardValueNames(), discard && !names); +#else EXPECT_EQ(context.shouldDiscardValueNames(), discard); +#endif } } } From 35dac7fb77b555d18d4bcd6c008cead7024df68f Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 07:43:16 +0100 Subject: [PATCH 09/27] docs(readme): note the value names an LLVM 16 context keeps Refs #263 --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 78b4cdec..1eb32b91 100644 --- a/README.md +++ b/README.md @@ -519,4 +519,6 @@ Its arguments are those of an LLVM IR compilation, `-S -emit-llvm` or `-c -emit- file is not written; the other files the arguments ask for, such as a dependency file, are. With `instrument`, the module is the instrumented one. Code generation sets options of the context, as it does with clang, except whether value names are discarded: the context keeps its own setting, -which applies to the modules read into it later. +which applies to the modules read into it later. With LLVM 16, a context that discards names keeps +them after a compilation that names values, since LLVM 16 cannot destroy those values in a context +that discards names. From 3c715b2907b44a72c9e8c273b082ad4f584576f2 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 12 Nov 2026 07:43:16 +0100 Subject: [PATCH 10/27] docs(changelog): note the value names an LLVM 16 context keeps Refs #263 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7c31f86..5477107a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,8 @@ pull request. or bitcode, created in an `llvm::LLVMContext` of the host, instead of writing it for the host to read it back. Its arguments are those of an LLVM IR compilation; the output file is not written, and the other files the arguments ask for, such as a dependency file, are. The - context keeps its own setting of value names (#265, #263). + context keeps its own setting of value names, except with LLVM 16, where a context that + discards them keeps them after a compilation that names values (#265, #263). ### Fixes From 6f99afa775cea77daf8082a0793fb6e1ff82412c Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:38 +0100 Subject: [PATCH 11/27] fix(compilerlib): restore a module's context after a recovered crash compileToModule restored the host's context from the job itself. Once the host enabled crash recovery, a crash leaves the job where it stood: the context kept the job's setting of value names. The context is now restored in runToModule, which the job always returns to. Refs #263 --- src/compilerlib/compiler.cpp | 47 ++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/compilerlib/compiler.cpp b/src/compilerlib/compiler.cpp index da4984a6..a8a1a55f 100644 --- a/src/compilerlib/compiler.cpp +++ b/src/compilerlib/compiler.cpp @@ -712,7 +712,10 @@ namespace compilerlib CompileResult runToModule(const clang::driver::Command& job) { CompileResult result; - if (!runOnClangStack([&] { result = runToModuleOnThisThread(job); })) + // On this frame, which the job leaves standing whatever happens to it, a + // crash that the host recovers included. + ModuleContextScope scope(*ctx_.module_context); + if (!runOnClangStack([&] { result = runToModuleOnThisThread(job, scope); })) { ctx_.module.reset(); return {false, "compilation crashed", {}}; @@ -721,22 +724,27 @@ namespace compilerlib } private: - // What code generation changes in compileToModule's context, which the host - // keeps: whether value names are discarded, which would apply to the modules the - // host reads into it later, and the optimization record of - // -fsave-optimization-record, whose file is closed once the job ends. + // What a job changes in compileToModule's context, which the host keeps: whether + // value names are discarded, which would apply to the modules the host reads into + // it later, and the optimization record of -fsave-optimization-record, whose file + // is closed once the job ends. class ModuleContextScope { public: - ModuleContextScope(llvm::LLVMContext& context, bool optimizationRecord) - : context_(context), discardValueNames_(context.shouldDiscardValueNames()), - optimizationRecord_(optimizationRecord) + explicit ModuleContextScope(llvm::LLVMContext& context) + : context_(context), discardValueNames_(context.shouldDiscardValueNames()) { } ModuleContextScope(const ModuleContextScope&) = delete; ModuleContextScope& operator=(const ModuleContextScope&) = delete; + // The job's frontend action sets up an optimization record on the context. + void expectOptimizationRecord() + { + optimizationRecord_ = true; + } + ~ModuleContextScope() { #if LLVM_VERSION_MAJOR < 17 @@ -754,10 +762,11 @@ namespace compilerlib private: llvm::LLVMContext& context_; bool discardValueNames_; - bool optimizationRecord_; + bool optimizationRecord_ = false; }; - CompileResult runToModuleOnThisThread(const clang::driver::Command& job) + CompileResult runToModuleOnThisThread(const clang::driver::Command& job, + ModuleContextScope& scope) { auto ci = makeCompilerInstance(job.getArguments()); if (!ci) @@ -784,17 +793,13 @@ namespace compilerlib ctx_.module = std::move(module); return true; }; - bool ok = false; - { - ModuleContextScope scope(*ctx_.module_context, - !ci->getCodeGenOpts().OptRecordFile.empty()); - ok = ctx_.runtimeConfig.optnone_enabled - ? runCodegenWithModule< - frontend::OptNoneAction>( - *ci, keepModule, error) - : runCodegenWithModule(*ci, keepModule, - error); - } + if (!ci->getCodeGenOpts().OptRecordFile.empty()) + scope.expectOptimizationRecord(); + const bool ok = + ctx_.runtimeConfig.optnone_enabled + ? runCodegenWithModule>( + *ci, keepModule, error) + : runCodegenWithModule(*ci, keepModule, error); if (!ok) { ctx_.module.reset(); From 4fe2efca0d4f062908343b9a034d3fbe825e00f2 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:39 +0100 Subject: [PATCH 12/27] fix(compilerlib): give a module's context its diagnostic handler back Clang's backend replaces the context's diagnostic handler with one that refers to the compilation, and returns without putting the previous one back when it cannot set up the optimization record or link a module in. After such a compileToModule, the host's handler was destroyed, and the next diagnostic of the context went to the compilation that had ended: a segmentation fault. The host's handler is now set aside for the job, which runs with LLVM's default one, and put back when the job ends. Refs #263 --- src/compilerlib/compiler.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/compilerlib/compiler.cpp b/src/compilerlib/compiler.cpp index a8a1a55f..6d0f1979 100644 --- a/src/compilerlib/compiler.cpp +++ b/src/compilerlib/compiler.cpp @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include #include @@ -726,14 +728,21 @@ namespace compilerlib private: // What a job changes in compileToModule's context, which the host keeps: whether // value names are discarded, which would apply to the modules the host reads into - // it later, and the optimization record of -fsave-optimization-record, whose file - // is closed once the job ends. + // it later, the diagnostic handler, and the optimization record of + // -fsave-optimization-record, whose file is closed once the job ends. + // + // Clang's backend replaces the handler with one that refers to the compilation, + // and returns without putting the previous one back when it cannot set up the + // optimization record or link a module in. The host's is set aside for the job, + // which runs with the handler a context of its own would have. class ModuleContextScope { public: explicit ModuleContextScope(llvm::LLVMContext& context) - : context_(context), discardValueNames_(context.shouldDiscardValueNames()) + : context_(context), discardValueNames_(context.shouldDiscardValueNames()), + diagnosticHandler_(context.getDiagnosticHandler()) { + context_.setDiagnosticHandler(std::make_unique()); } ModuleContextScope(const ModuleContextScope&) = delete; @@ -757,11 +766,13 @@ namespace compilerlib context_.setDiscardValueNames(discardValueNames_); if (optimizationRecord_) clang_compat::releaseFrontendOptimizationRecord(context_); + context_.setDiagnosticHandler(std::move(diagnosticHandler_)); } private: llvm::LLVMContext& context_; bool discardValueNames_; + std::unique_ptr diagnosticHandler_; bool optimizationRecord_ = false; }; From cdacb0663c0de86a0c9fc81664a7732b20584cb8 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:40 +0100 Subject: [PATCH 13/27] test(compilerlib): check a context's diagnostic handler after a module Refs #263 --- test/unit/compile_test.cpp | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index e9e3fd04..0a2ca179 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -836,6 +837,58 @@ int sum(int count) } } + // A diagnostic handler of the host, which counts what the context reports to it. + class CountingDiagnosticHandler final : public llvm::DiagnosticHandler + { + public: + explicit CountingDiagnosticHandler(int& reported) : reported_(reported) {} + + bool handleDiagnostics(const llvm::DiagnosticInfo&) override + { + ++reported_; + return true; + } + + private: + int& reported_; + }; + + // The diagnostic handler of the context is the host's. Clang's backend replaces it for + // the time it runs, with one that refers to the compilation, and returns without putting + // the host's back when it cannot set up the optimization record: compileToModule does. + class ModuleDiagnosticHandlerTest : public CompileTest + { + protected: + void SetUp() override + { + CompileTest::SetUp(); + auto handler = std::make_unique(reported_); + host_ = handler.get(); + context_.setDiagnosticHandler(std::move(handler)); + } + + // The context has the host's handler, and reports to it. + void expectTheHostHandler() + { + ASSERT_EQ(context_.getDiagHandlerPtr(), host_); + const int before = reported_; + context_.emitError("reported to the host"); + EXPECT_EQ(reported_, before + 1); + } + + int reported_ = 0; + llvm::LLVMContext context_; + const llvm::DiagnosticHandler* host_ = nullptr; + }; + + TEST_F(ModuleDiagnosticHandlerTest, IsTheHostsAfterACompilation) + { + compilerlib::ModuleResult result = compilerlib::compileToModule( + {"-O2", "-S", "-emit-llvm", writeSource("unit.c", kBitcodeUnit)}, context_); + ASSERT_TRUE(result.success) << result.diagnostics; + expectTheHostHandler(); + } + // Like clang, a failed compilation leaves no output behind: neither a partial object // nor an earlier one that would look up to date. class FailedCompilationTest : public CompileTest, From 694de858795e4b3d1e6922d9b668c4ff2d35851c Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:41 +0100 Subject: [PATCH 14/27] test(compilerlib): check the handler after a record that cannot open The compilation fails on an optimization record whose directory does not exist. The context must still have the host's handler, report to it, and take another compilation. Before the fix, the context was left with the handler of Clang's backend. Refs #263 --- test/unit/compile_test.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index 0a2ca179..f5ae7183 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -876,6 +876,36 @@ int sum(int count) EXPECT_EQ(reported_, before + 1); } + // A compilation that `failing` arguments make fail in Clang's backend leaves the + // host's handler, and the context then takes another compilation. + void expectTheHostHandlerAfter(const std::vector& failing, + const char* diagnostic, bool instrument = false) + { + const std::string source = writeSource("unit.c", kBitcodeUnit); + std::vector args{"-O2", "-S", "-emit-llvm", source}; + args.insert(args.end(), failing.begin(), failing.end()); + compilerlib::ModuleResult failed = + compilerlib::compileToModule(args, context_, instrument); + EXPECT_FALSE(failed.success); + EXPECT_EQ(failed.module, nullptr); + EXPECT_NE(failed.diagnostics.find(diagnostic), std::string::npos) << failed.diagnostics; + EXPECT_EQ(reported_, 0) << "the compilation reported to the host's handler"; + ASSERT_NO_FATAL_FAILURE(expectTheHostHandler()); + + compilerlib::ModuleResult result = compilerlib::compileToModule( + {"-O2", "-S", "-emit-llvm", source}, context_, instrument); + EXPECT_TRUE(result.success) << result.diagnostics; + EXPECT_NE(result.module, nullptr); + expectTheHostHandler(); + } + + // A record file in a directory that does not exist. + std::vector recordThatCannotBeOpened() const + { + return {"-fsave-optimization-record", + "-foptimization-record-file=" + path("missing/unit.opt.yaml")}; + } + int reported_ = 0; llvm::LLVMContext context_; const llvm::DiagnosticHandler* host_ = nullptr; @@ -889,6 +919,11 @@ int sum(int count) expectTheHostHandler(); } + TEST_F(ModuleDiagnosticHandlerTest, IsTheHostsAfterARecordFileThatCannotBeOpened) + { + expectTheHostHandlerAfter(recordThatCannotBeOpened(), "unit.opt.yaml"); + } + // Like clang, a failed compilation leaves no output behind: neither a partial object // nor an earlier one that would look up to date. class FailedCompilationTest : public CompileTest, From 9ad087ef6b7ec78b76111d5cb86bee714f7f55eb Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:41 +0100 Subject: [PATCH 15/27] test(compilerlib): check the handler after an unknown record format Refs #263 --- test/unit/compile_test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index f5ae7183..135047df 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -924,6 +924,11 @@ int sum(int count) expectTheHostHandlerAfter(recordThatCannotBeOpened(), "unit.opt.yaml"); } + TEST_F(ModuleDiagnosticHandlerTest, IsTheHostsAfterAnUnknownRecordFormat) + { + expectTheHostHandlerAfter({"-fsave-optimization-record=unknown"}, "unknown"); + } + // Like clang, a failed compilation leaves no output behind: neither a partial object // nor an earlier one that would look up to date. class FailedCompilationTest : public CompileTest, From 977a5e75a2da521aeda8ddfb9e9cc74b1edbeb79 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:42 +0100 Subject: [PATCH 16/27] test(compilerlib): check the handler after an instrumented failure Instrumented, compilerlib sets up the optimization record itself. This path already put the handler back. Refs #263 --- test/unit/compile_test.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index 135047df..6d0fe863 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -929,6 +929,13 @@ int sum(int count) expectTheHostHandlerAfter({"-fsave-optimization-record=unknown"}, "unknown"); } + // Instrumented, compilerlib sets up the record itself, after Clang's frontend. + TEST_F(ModuleDiagnosticHandlerTest, IsTheHostsAfterAnInstrumentedCompilationThatFails) + { + expectTheHostHandlerAfter(recordThatCannotBeOpened(), "unit.opt.yaml", + /*instrument=*/true); + } + // Like clang, a failed compilation leaves no output behind: neither a partial object // nor an earlier one that would look up to date. class FailedCompilationTest : public CompileTest, From cf37c7cf1b11d99b5c8934339519c3ad499ef002 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:42 +0100 Subject: [PATCH 17/27] test(compilerlib): check the handler after a module that cannot link The other early return of Clang's backend: a module given with -mlink-bitcode-file defines the unit's symbols again. Refs #263 --- test/unit/compile_test.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index 6d0fe863..deefeb70 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -936,6 +936,19 @@ int sum(int count) /*instrument=*/true); } + // The other way Clang's backend returns early: a module it has to link in does not + // link, here one that defines the unit's symbols again. + TEST_F(ModuleDiagnosticHandlerTest, IsTheHostsAfterAModuleThatCannotBeLinkedIn) + { + const std::string again = path("again.bc"); + compilerlib::CompileResult bitcode = compilerlib::compile( + {"-c", "-emit-llvm", writeSource("again.c", kBitcodeUnit), "-o", again}, + compilerlib::OutputMode::ToFile); + ASSERT_TRUE(bitcode.success) << bitcode.diagnostics; + expectTheHostHandlerAfter({"-Xclang", "-mlink-bitcode-file", "-Xclang", again}, + "multiply defined"); + } + // Like clang, a failed compilation leaves no output behind: neither a partial object // nor an earlier one that would look up to date. class FailedCompilationTest : public CompileTest, From decfc651ab9e3a546700da49463a904c929c2367 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:43 +0100 Subject: [PATCH 18/27] test(compilerlib): check a module's context after a recovered crash The source crashes Clang once the host enabled crash recovery. The context must keep the host's setting of value names and its diagnostic handler. Before the fix, it kept the job's setting of value names. Refs #263 --- test/unit/compile_test.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index deefeb70..aee2a2ab 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -949,6 +950,37 @@ int sum(int count) "multiply defined"); } + // Compiles a source that crashes Clang, once the host enabled crash recovery, in a context + // of the host, which keeps value names and has a diagnostic handler. Exits with 0 when the + // compilation failed and left the context as the host set it. + [[noreturn]] void crashInAModuleCompilationAndExit(const std::string& source) + { + llvm::CrashRecoveryContext::Enable(); + int reported = 0; + llvm::LLVMContext context; + auto handler = std::make_unique(reported); + const llvm::DiagnosticHandler* host = handler.get(); + context.setDiagnosticHandler(std::move(handler)); + const compilerlib::ModuleResult result = compilerlib::compileToModule( + {"-fdiscard-value-names", "-S", "-emit-llvm", source}, context); + if (result.success) + std::_Exit(1); + if (context.shouldDiscardValueNames()) + std::_Exit(2); + if (context.getDiagHandlerPtr() != host) + std::_Exit(3); + context.emitError("reported to the host"); + std::_Exit(reported == 1 ? 0 : 4); + } + + // A crash that the host recovers leaves the job where it stood, and nothing the job + // would have run on its way out runs: the context is restored all the same. + TEST_F(CompileTest, ModuleLeavesTheContextOfTheHostAfterARecoveredCrash) + { + const std::string source = writeSource("crash.c", "#pragma clang __debug crash\n"); + EXPECT_EXIT(crashInAModuleCompilationAndExit(source), ::testing::ExitedWithCode(0), ""); + } + // Like clang, a failed compilation leaves no output behind: neither a partial object // nor an earlier one that would look up to date. class FailedCompilationTest : public CompileTest, From 741f41fcf9d00505bec5edd89b48f54d6f38eddb Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:44 +0100 Subject: [PATCH 19/27] docs(compilerlib): note the diagnostic handler of a module's context Refs #263 --- include/compilerlib/compile_module.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/compilerlib/compile_module.hpp b/include/compilerlib/compile_module.hpp index 4e052a0c..e4be2da5 100644 --- a/include/compilerlib/compile_module.hpp +++ b/include/compilerlib/compile_module.hpp @@ -33,6 +33,9 @@ namespace compilerlib /// names are discarded, which is restored: modules read into `context` later keep theirs. /// With LLVM 16, a context that discards names keeps them after a compilation that names /// values, which LLVM 16 could not destroy in a context that discards names. + /// The diagnostic handler of `context` is set aside for the compilation, which does not + /// call it, and is the context's again once compileToModule returns: whether the + /// compilation succeeded, failed, or crashed and was recovered. /// No other thread may use `context` during the compilation. CT_NODISCARD ModuleResult compileToModule(const std::vector& args, llvm::LLVMContext& context, bool instrument = false); From 358b2e16e9d1d25e0f4d7b3fecbf7be65a25466c Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:44 +0100 Subject: [PATCH 20/27] docs(readme): note the diagnostic handler of a module's context Refs #263 --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1eb32b91..ebb16e55 100644 --- a/README.md +++ b/README.md @@ -521,4 +521,6 @@ file is not written; the other files the arguments ask for, such as a dependency it does with clang, except whether value names are discarded: the context keeps its own setting, which applies to the modules read into it later. With LLVM 16, a context that discards names keeps them after a compilation that names values, since LLVM 16 cannot destroy those values in a context -that discards names. +that discards names. The context's diagnostic handler is set aside for the compilation, which does +not call it, and is the context's again once `compileToModule` returns, whether the compilation +succeeded, failed, or crashed and was recovered. From 9c698417b5fa9438cdefb0fff40f1001cf535fd4 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:45 +0100 Subject: [PATCH 21/27] docs(changelog): note the diagnostic handler of a module's context Refs #263 --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5477107a..84df9dc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,9 @@ pull request. to read it back. Its arguments are those of an LLVM IR compilation; the output file is not written, and the other files the arguments ask for, such as a dependency file, are. The context keeps its own setting of value names, except with LLVM 16, where a context that - discards them keeps them after a compilation that names values (#265, #263). + discards them keeps them after a compilation that names values. It keeps its diagnostic + handler too, which the compilation does not call, whether the compilation succeeded, failed, + or crashed and was recovered (#265, #263). ### Fixes From 9ebd2dda5f7216dd86a07a065eed734a92977dbd Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 02:32:45 +0100 Subject: [PATCH 22/27] docs(changelog): note the tests of a module's context after a failure Refs #263 --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84df9dc5..e3e950b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -195,7 +195,10 @@ pull request. - Unit tests check that `compileToModule` gives the module that `compile` writes as text, at -O0 and -O2, instrumented or not, and the module of a bitcode compilation, whose dependency file is written and output file is not; that it fails on a compilation to an object and on a broken - source; and that the context keeps its setting of value names (#265, #263). + source; that the context keeps its setting of value names; and that it keeps its diagnostic + handler, reports to it and takes another compilation after one that Clang's backend ends + early, on an optimization record it cannot set up or a module it cannot link in, and after a + crash that the host recovers (#265, #263). - The Docker tests build and test the image in a job per architecture, each on a runner of its own architecture: arm64 on `ubuntu-24.04-arm` instead of under QEMU on an amd64 runner. Under QEMU's user-mode emulation, `sbrk()` failed now and then in the sbrk test of the full From 00e95370316a743cbe6106d63b646ac111c7328b Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 06:50:44 +0100 Subject: [PATCH 23/27] test(compilerlib): check modules compiled from several threads at once compileToModule from eight threads, each with a context and a project of its own, whose headers have the same name: each module comes from its own header, and the process's working directory stays where it is, instrumented or not. The test fails when the compilations share the process's file system, as they did before #262. Refs #263 --- test/unit/compile_test.cpp | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/unit/compile_test.cpp b/test/unit/compile_test.cpp index fb18dc6b..b8292c74 100644 --- a/test/unit/compile_test.cpp +++ b/test/unit/compile_test.cpp @@ -2517,6 +2517,48 @@ int once(int x) { return x; } EXPECT_EQ(directoryAfter, processDirectory); } + // compileToModule too, each thread with a context of its own: each module comes from + // its own project, and the process's working directory stays where it is. + TEST_P(ConcurrentCompilationTest, EachModuleComesFromItsOwnWorkingDirectory) + { + constexpr int kThreads = 8; + constexpr int kRounds = 3; + std::vector projects; + for (int i = 0; i < kThreads; ++i) + projects.push_back(writeProject("project" + std::to_string(i), 100 + i)); + const fs::path processDirectory = fs::current_path(); + + std::vector failures(kThreads); + std::vector threads; + for (int i = 0; i < kThreads; ++i) + { + threads.emplace_back( + [&, i] + { + const std::string expected = "ret i32 " + std::to_string(100 + i); + llvm::LLVMContext context; + for (int round = 0; round < kRounds && failures[i].empty(); ++round) + { + compilerlib::ModuleResult result = + compilerlib::compileToModule({"-working-directory", projects[i], + "-Iinc", "-S", "-emit-llvm", "unit.c"}, + context, /*instrument=*/GetParam()); + if (!result.success) + failures[i] = result.diagnostics; + else if (printed(*result.module).find(expected) == std::string::npos) + failures[i] = "no \"" + expected + "\" in\n" + printed(*result.module); + } + }); + } + for (std::thread& thread : threads) + thread.join(); + const fs::path directoryAfter = fs::current_path(); + fs::current_path(processDirectory); + for (int i = 0; i < kThreads; ++i) + EXPECT_EQ(failures[i], "") << "thread " << i; + EXPECT_EQ(directoryAfter, processDirectory); + } + INSTANTIATE_TEST_SUITE_P(Instrumentation, ConcurrentCompilationTest, ::testing::Bool(), [](const ::testing::TestParamInfo& info) { return info.param ? "Instrumented" : "Plain"; }); From f43dc01d84436e0ec218c649f563eee2daa922ee Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 06:50:44 +0100 Subject: [PATCH 24/27] docs(compilerlib): note the working directory of a module's compilation Refs #263 --- include/compilerlib/compile_module.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/compilerlib/compile_module.hpp b/include/compilerlib/compile_module.hpp index e4be2da5..b0012dd8 100644 --- a/include/compilerlib/compile_module.hpp +++ b/include/compilerlib/compile_module.hpp @@ -37,6 +37,9 @@ namespace compilerlib /// call it, and is the context's again once compileToModule returns: whether the /// compilation succeeded, failed, or crashed and was recovered. /// No other thread may use `context` during the compilation. + /// + /// `-working-directory` and compilations from several threads at once are as for + /// compile(), each compilation with a context of its own. CT_NODISCARD ModuleResult compileToModule(const std::vector& args, llvm::LLVMContext& context, bool instrument = false); From 9b80cedab120a4e0a27bf2bd0444aa8ef5c74533 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 06:50:44 +0100 Subject: [PATCH 25/27] docs(readme): note the working directory of a module's compilation Refs #263 --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6f9a33e8..8a53bf87 100644 --- a/README.md +++ b/README.md @@ -543,3 +543,6 @@ them after a compilation that names values, since LLVM 16 cannot destroy those v that discards names. The context's diagnostic handler is set aside for the compilation, which does not call it, and is the context's again once `compileToModule` returns, whether the compilation succeeded, failed, or crashed and was recovered. + +`-working-directory` and compilations from several threads at once are as for +`compilerlib::compile`, each compilation with a context of its own. From 729f9633201663584d468f3a0c36be0dc7833457 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 06:50:44 +0100 Subject: [PATCH 26/27] docs(changelog): note modules compiled from several threads Refs #263 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8e31bd3..473b882f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,8 @@ pull request. context keeps its own setting of value names, except with LLVM 16, where a context that discards them keeps them after a compilation that names values. It keeps its diagnostic handler too, which the compilation does not call, whether the compilation succeeded, failed, - or crashed and was recovered (#265, #263). + or crashed and was recovered. With `-working-directory`, it compiles from several threads + at once as `compile` does, each with a context of its own (#265, #263). ### Behaviour changes From 6eb4646bc7f58203e2d3645e299878d173a6caf9 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 06:50:45 +0100 Subject: [PATCH 27/27] docs(changelog): note the test of modules compiled from several threads Refs #263 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 473b882f..265766b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -223,7 +223,8 @@ pull request. source; that the context keeps its setting of value names; and that it keeps its diagnostic handler, reports to it and takes another compilation after one that Clang's backend ends early, on an optimization record it cannot set up or a module it cannot link in, and after a - crash that the host recovers (#265, #263). + crash that the host recovers; and that eight threads, each with a context and a working + directory of its own, each get the module of their own sources (#265, #263). - The Docker tests build and test the image in a job per architecture, each on a runner of its own architecture: arm64 on `ubuntu-24.04-arm` instead of under QEMU on an amd64 runner. Under QEMU's user-mode emulation, `sbrk()` failed now and then in the sbrk test of the full