[clang] [clang][CodeGen] Link postopt builtin bitcode for LLVM IR input (PR #246138)
Ivan Kelarev via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 12:05:57 PST 2026
https://github.com/ivankelarev created https://github.com/llvm/llvm-project/pull/246138
For LLVM IR input, `CodeGenAction::ExecuteAction` keeps the modules given with `-mlink-builtin-bitcode` in a local `BackendConsumer`, but calls `emitBackendOutput` without it. With `-mlink-builtin-bitcode-postopt` the link before the optimizations is skipped, and the `LinkInModulesPass` at the end of the pipeline gets a null consumer and links nothing, so the modules are silently dropped. The same options link them for source input.
The patch passes the consumer to `emitBackendOutput` for IR input, as `BackendConsumer::HandleTranslationUnit` does for source input.
>From fc05183dc2d61b7134d05ad58fe10d902e527134 Mon Sep 17 00:00:00 2001
From: Ivan Kelarev <ivan.kelarev at intel.com>
Date: Wed, 25 Nov 2026 12:01:45 -0800
Subject: [PATCH] [clang][CodeGen] Link postopt builtin bitcode for LLVM IR
input
For LLVM IR input, `CodeGenAction::ExecuteAction` keeps the modules given with `-mlink-builtin-bitcode` in a local `BackendConsumer`, but calls `emitBackendOutput` without it. With `-mlink-builtin-bitcode-postopt` the link before the optimizations is skipped, and the `LinkInModulesPass` at the end of the pipeline gets a null consumer and links nothing, so the modules are silently dropped. The same options link them for source input.
The patch passes the consumer to `emitBackendOutput` for IR input, as `BackendConsumer::HandleTranslationUnit` does for source input.
---
clang/lib/CodeGen/CodeGenAction.cpp | 2 +-
.../CodeGen/linking-bitcode-postopt-ir.ll | 33 +++++++++++++++++++
2 files changed, 34 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGen/linking-bitcode-postopt-ir.ll
diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp
index dd7ba0b741c857..50f711f8e83b44 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -622,7 +622,7 @@ void CodeGenAction::ExecuteAction() {
emitBackendOutput(CI, CI.getCodeGenOpts(), TheModule.get(), BA,
CI.getFileManager().getVirtualFileSystemPtr(),
- std::move(OS));
+ std::move(OS), &Result);
if (OptRecordFile)
OptRecordFile->keep();
}
diff --git a/clang/test/CodeGen/linking-bitcode-postopt-ir.ll b/clang/test/CodeGen/linking-bitcode-postopt-ir.ll
new file mode 100644
index 00000000000000..51ccba2467b8f4
--- /dev/null
+++ b/clang/test/CodeGen/linking-bitcode-postopt-ir.ll
@@ -0,0 +1,33 @@
+; Check that -mlink-builtin-bitcode-postopt links the builtin bitcode after the
+; optimizations when the input is LLVM IR, as it does for source input.
+
+; RUN: split-file %s %t
+; RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm-bc \
+; RUN: -o %t/lib.bc -x ir %t/lib.ll
+; RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -emit-llvm \
+; RUN: -mlink-builtin-bitcode %t/lib.bc -mlink-builtin-bitcode-postopt \
+; RUN: -o - -x ir %t/main.ll | FileCheck %s
+
+; CHECK: define {{.*}}i32 @main()
+; CHECK: call i32 @lib_func(i32 1)
+; CHECK: define {{.*}}i32 @lib_func(i32
+
+;--- main.ll
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @main() {
+entry:
+ %r = call i32 @lib_func(i32 1)
+ ret i32 %r
+}
+
+declare i32 @lib_func(i32)
+
+;--- lib.ll
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @lib_func(i32 %x) noinline {
+entry:
+ %r = add i32 %x, 41
+ ret i32 %r
+}
More information about the cfe-commits
mailing list