[clang] [llvm] [llvm] Avoid resolving `.incbin` during symbol collection (PR #172920)
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 12 08:21:02 PST 2026
https://github.com/jansvoboda11 updated https://github.com/llvm/llvm-project/pull/172920
>From 32c3db2a5483a795b52a81732950ee6192f8a90b Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Thu, 18 Dec 2025 14:03:11 -0800
Subject: [PATCH 1/2] [llvm] Avoid resolving `.incbin` during symbol collection
---
clang/test/CodeGen/asm_incbin.c | 8 ++++++++
llvm/include/llvm/MC/MCParser/MCAsmParser.h | 4 ++++
llvm/lib/MC/MCParser/AsmParser.cpp | 3 +++
llvm/lib/Object/ModuleSymbolTable.cpp | 3 +++
4 files changed, 18 insertions(+)
create mode 100644 clang/test/CodeGen/asm_incbin.c
diff --git a/clang/test/CodeGen/asm_incbin.c b/clang/test/CodeGen/asm_incbin.c
new file mode 100644
index 0000000000000..9ad447a1dfc98
--- /dev/null
+++ b/clang/test/CodeGen/asm_incbin.c
@@ -0,0 +1,8 @@
+// RUN: split-file %s %t
+//--- foo.h
+//--- tu.c
+asm(".incbin \"foo.h\"");
+// RUN: cd %t
+// RUN: %clang -c -emit-llvm %t/tu.c -o %t/tu.ll
+// RUN: llvm-dis %t/tu.ll -o - | FileCheck %s
+// CHECK: module asm ".incbin \22foo.h\22"
diff --git a/llvm/include/llvm/MC/MCParser/MCAsmParser.h b/llvm/include/llvm/MC/MCParser/MCAsmParser.h
index 5d74b76592df9..678dc05d29b2a 100644
--- a/llvm/include/llvm/MC/MCParser/MCAsmParser.h
+++ b/llvm/include/llvm/MC/MCParser/MCAsmParser.h
@@ -150,6 +150,7 @@ class LLVM_ABI MCAsmParser {
bool HadError = false;
bool ShowParsedOperands = false;
+ bool ProcessIncbinFile = true;
public:
MCAsmParser(const MCAsmParser &) = delete;
@@ -176,6 +177,9 @@ class LLVM_ABI MCAsmParser {
bool getShowParsedOperands() const { return ShowParsedOperands; }
void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
+ bool getProcessIncbinFile() const { return ProcessIncbinFile; }
+ void setProcessIncbinFile(bool Value) { ProcessIncbinFile = Value; }
+
/// Run the parser on the input source buffer.
virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 429cdae1fa1b6..c9639fcdafe53 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -848,6 +848,9 @@ bool AsmParser::enterIncludeFile(const std::string &Filename) {
/// returns true on failure.
bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip,
const MCExpr *Count, SMLoc Loc) {
+ if (!getProcessIncbinFile())
+ return false;
+
std::string IncludedFile;
unsigned NewBuf =
SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
diff --git a/llvm/lib/Object/ModuleSymbolTable.cpp b/llvm/lib/Object/ModuleSymbolTable.cpp
index 9442becdb7d33..a8b0101187078 100644
--- a/llvm/lib/Object/ModuleSymbolTable.cpp
+++ b/llvm/lib/Object/ModuleSymbolTable.cpp
@@ -129,6 +129,9 @@ initializeRecordStreamer(const Module &M,
// AsmPrinter::doInitialization()).
Parser->setAssemblerDialect(InlineAsm::AD_ATT);
+ // The .incbin directive cannot introduce new symbols.
+ Parser->setProcessIncbinFile(false);
+
Parser->setTargetParser(*TAP);
if (Parser->Run(false))
return;
>From c1729e7531a9e5444bd3194bf5aac4f279173628 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Mon, 12 Jan 2026 08:19:17 -0800
Subject: [PATCH 2/2] ProcessIncbinFile -> SymbolScanningMode
---
llvm/include/llvm/MC/MCParser/MCAsmParser.h | 9 ++++++---
llvm/lib/MC/MCParser/AsmParser.cpp | 3 ++-
llvm/lib/Object/ModuleSymbolTable.cpp | 3 +--
3 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/llvm/include/llvm/MC/MCParser/MCAsmParser.h b/llvm/include/llvm/MC/MCParser/MCAsmParser.h
index 678dc05d29b2a..da5ef08381aed 100644
--- a/llvm/include/llvm/MC/MCParser/MCAsmParser.h
+++ b/llvm/include/llvm/MC/MCParser/MCAsmParser.h
@@ -150,7 +150,11 @@ class LLVM_ABI MCAsmParser {
bool HadError = false;
bool ShowParsedOperands = false;
- bool ProcessIncbinFile = true;
+
+ /// Flag tracking whether we're only interested in symbols, which allows us to
+ /// avoid some work (e.g. resolving .incbin directives).
+ // TODO: Adopt this in more places.
+ bool SymbolScanningMode = false;
public:
MCAsmParser(const MCAsmParser &) = delete;
@@ -177,8 +181,7 @@ class LLVM_ABI MCAsmParser {
bool getShowParsedOperands() const { return ShowParsedOperands; }
void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
- bool getProcessIncbinFile() const { return ProcessIncbinFile; }
- void setProcessIncbinFile(bool Value) { ProcessIncbinFile = Value; }
+ void setSymbolScanningMode(bool Value) { SymbolScanningMode = Value; }
/// Run the parser on the input source buffer.
virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index c9639fcdafe53..e87de484163b1 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -848,7 +848,8 @@ bool AsmParser::enterIncludeFile(const std::string &Filename) {
/// returns true on failure.
bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip,
const MCExpr *Count, SMLoc Loc) {
- if (!getProcessIncbinFile())
+ // The .incbin file cannot introduce new symbols.
+ if (SymbolScanningMode)
return false;
std::string IncludedFile;
diff --git a/llvm/lib/Object/ModuleSymbolTable.cpp b/llvm/lib/Object/ModuleSymbolTable.cpp
index a8b0101187078..ff0bc8b192de7 100644
--- a/llvm/lib/Object/ModuleSymbolTable.cpp
+++ b/llvm/lib/Object/ModuleSymbolTable.cpp
@@ -129,8 +129,7 @@ initializeRecordStreamer(const Module &M,
// AsmPrinter::doInitialization()).
Parser->setAssemblerDialect(InlineAsm::AD_ATT);
- // The .incbin directive cannot introduce new symbols.
- Parser->setProcessIncbinFile(false);
+ Parser->setSymbolScanningMode(true);
Parser->setTargetParser(*TAP);
if (Parser->Run(false))
More information about the cfe-commits
mailing list