https://github.com/AdityaSinha149 created https://github.com/llvm/llvm-project/pull/229741
This PR adds an option to reload `LinkModules`. In incremental mode (clang-repl), multiple TUs use the same `BackendConsumer`, but `LinkModules` are consumed after the first TU is processed, so they need to be reloaded for every TU. It is not called yet; a follow-up for HIP support in clang-repl will use it. This PR also adds a test that checks a library is linked into a first TU, not into a second, and linked again after the reload. >From 56f30a4141bf86d40822b5fa0797a6a59c20b9dc Mon Sep 17 00:00:00 2001 From: AdityaSinha149 <[email protected]> Date: Wed, 7 Oct 2026 17:19:51 +0530 Subject: [PATCH] [clang][CodeGen] Add reloadLinkModules to reload link modules for incremental compilation --- clang/include/clang/CodeGen/CodeGenAction.h | 3 +++ clang/lib/CodeGen/BackendConsumer.h | 8 ++++++++ clang/lib/CodeGen/CodeGenAction.cpp | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/clang/include/clang/CodeGen/CodeGenAction.h b/clang/include/clang/CodeGen/CodeGenAction.h index 84fa4549d5033..e7d89306154d1 100644 --- a/clang/include/clang/CodeGen/CodeGenAction.h +++ b/clang/include/clang/CodeGen/CodeGenAction.h @@ -63,6 +63,9 @@ class CodeGenAction : public ASTFrontendAction { CodeGenerator *getCodeGenerator() const; + /// Reload the -mlink-builtin-bitcode modules into the backend consumer. + void reloadLinkModules(CompilerInstance &CI); + BackendConsumer *BEConsumer = nullptr; }; diff --git a/clang/lib/CodeGen/BackendConsumer.h b/clang/lib/CodeGen/BackendConsumer.h index 5337a5f7a1b4a..490be03bf14f7 100644 --- a/clang/lib/CodeGen/BackendConsumer.h +++ b/clang/lib/CodeGen/BackendConsumer.h @@ -76,6 +76,14 @@ class BackendConsumer : public ASTConsumer { // Links each entry in LinkModules into our module. Returns true on error. bool LinkInModules(llvm::Module *M); + + /// Reloads a set of modules to link in. LinkInModules() consume the modules, + /// so incremental compilation(clang-repl) needs to reload the modules for + /// every TU. + void setLinkModules(SmallVector<LinkModule, 4> LMs) { + LinkModules = std::move(LMs); + } + /// Create an llvm::DiagnosticHandler that routes LLVM backend diagnostics /// through this consumer's clang diagnostics. std::unique_ptr<llvm::DiagnosticHandler> createDiagnosticHandler(); diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index 1abd8b0455a19..0511ddf596247 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -365,6 +365,15 @@ CodeGenerator *CodeGenAction::getCodeGenerator() const { return BEConsumer->getCodeGenerator(); } +void CodeGenAction::reloadLinkModules(CompilerInstance &CI) { + if (!BEConsumer) + return; + SmallVector<LinkModule, 4> LMs; + if (clang::loadLinkModules(CI, *VMContext, LMs)) + return; + BEConsumer->setLinkModules(std::move(LMs)); +} + bool CodeGenAction::BeginSourceFileAction(CompilerInstance &CI) { if (CI.getFrontendOpts().GenReducedBMI) CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
