[llvm] [LTO][Legacy] Add new C APIs to query undefined symbols in assembly (PR #145413)

Steven Wu via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 23 14:51:56 PDT 2025


https://github.com/cachemeifyoucan updated https://github.com/llvm/llvm-project/pull/145413

>From ae086533f71ecc2a2f5e248afd53a5ac667a5694 Mon Sep 17 00:00:00 2001
From: Steven Wu <stevenwu at apple.com>
Date: Mon, 23 Jun 2025 14:39:25 -0700
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.6
---
 llvm/include/llvm-c/lto.h                | 17 ++++++++++++++++-
 llvm/include/llvm/LTO/legacy/LTOModule.h |  8 ++++++++
 llvm/tools/llvm-lto/llvm-lto.cpp         |  2 ++
 llvm/tools/lto/lto.cpp                   |  9 +++++++++
 llvm/tools/lto/lto.exports               |  2 ++
 5 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm-c/lto.h b/llvm/include/llvm-c/lto.h
index 5ceb02224d2bb..91195b0fe9458 100644
--- a/llvm/include/llvm-c/lto.h
+++ b/llvm/include/llvm-c/lto.h
@@ -46,7 +46,7 @@ typedef bool lto_bool_t;
  * @{
  */
 
-#define LTO_API_VERSION 29
+#define LTO_API_VERSION 30
 
 /**
  * \since prior to LTO_API_VERSION=3
@@ -286,6 +286,21 @@ lto_module_get_symbol_name(lto_module_t mod, unsigned int index);
 extern lto_symbol_attributes
 lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
 
+/**
+ * Returns the number of asm undefined symbols in the object module.
+ *
+ * \since prior to LTO_API_VERSION=30
+ */
+extern unsigned int lto_module_get_num_asm_undef_symbols(lto_module_t mod);
+
+/**
+ * Returns the name of the ith asm undefined symbol in the object module.
+ *
+ * \since prior to LTO_API_VERSION=30
+ */
+extern const char *lto_module_get_asm_undef_symbol_name(lto_module_t mod,
+                                                        unsigned int index);
+
 /**
  * Returns the module's linker options.
  *
diff --git a/llvm/include/llvm/LTO/legacy/LTOModule.h b/llvm/include/llvm/LTO/legacy/LTOModule.h
index 2b6a8734e78f6..e4d52ac067a6d 100644
--- a/llvm/include/llvm/LTO/legacy/LTOModule.h
+++ b/llvm/include/llvm/LTO/legacy/LTOModule.h
@@ -143,6 +143,14 @@ struct LTOModule {
     return StringRef();
   }
 
+  uint32_t getAsmUndefSymbolCount() { return _asm_undefines.size(); }
+
+  StringRef getAsmUndefSymbolName(uint32_t index) {
+    if (index < _asm_undefines.size())
+      return _asm_undefines[index];
+    return StringRef();
+  }
+
   const GlobalValue *getSymbolGV(uint32_t index) {
     if (index < _symbols.size())
       return _symbols[index].symbol;
diff --git a/llvm/tools/llvm-lto/llvm-lto.cpp b/llvm/tools/llvm-lto/llvm-lto.cpp
index 21953ee98d6a0..05e9502e3abbe 100644
--- a/llvm/tools/llvm-lto/llvm-lto.cpp
+++ b/llvm/tools/llvm-lto/llvm-lto.cpp
@@ -475,6 +475,8 @@ static void testLTOModule(const TargetOptions &Options) {
         printLTOSymbolAttributes(Module->getSymbolAttributes(I));
         outs() << "\n";
       }
+      for (int I = 0, E = Module->getAsmUndefSymbolCount(); I != E; ++I)
+        outs() << Module->getAsmUndefSymbolName(I) << "    { asm extern }\n";
     }
     if (QueryHasCtorDtor)
       outs() << Filename
diff --git a/llvm/tools/lto/lto.cpp b/llvm/tools/lto/lto.cpp
index bb64d42ccced1..467a4da27dcd8 100644
--- a/llvm/tools/lto/lto.cpp
+++ b/llvm/tools/lto/lto.cpp
@@ -322,6 +322,15 @@ lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
   return unwrap(mod)->getSymbolAttributes(index);
 }
 
+unsigned int lto_module_get_num_asm_undef_symbols(lto_module_t mod) {
+  return unwrap(mod)->getAsmUndefSymbolCount();
+}
+
+const char *lto_module_get_asm_undef_symbol_name(lto_module_t mod,
+                                                 unsigned int index) {
+  return unwrap(mod)->getAsmUndefSymbolName(index).data();
+}
+
 const char* lto_module_get_linkeropts(lto_module_t mod) {
   return unwrap(mod)->getLinkerOpts().data();
 }
diff --git a/llvm/tools/lto/lto.exports b/llvm/tools/lto/lto.exports
index 4164c3919a97f..850e159b725a3 100644
--- a/llvm/tools/lto/lto.exports
+++ b/llvm/tools/lto/lto.exports
@@ -14,6 +14,8 @@ lto_module_get_macho_cputype
 lto_module_get_num_symbols
 lto_module_get_symbol_attribute
 lto_module_get_symbol_name
+lto_module_get_num_asm_undef_symbols
+lto_module_get_asm_undef_symbol_name
 lto_module_get_target_triple
 lto_module_set_target_triple
 lto_module_is_object_file

>From a0ec13a56f0b274a0759ebfc9e81a52ff47bfbc5 Mon Sep 17 00:00:00 2001
From: Steven Wu <stevenwu at apple.com>
Date: Mon, 23 Jun 2025 14:51:46 -0700
Subject: [PATCH 2/2] Add test case

Created using spr 1.3.6
---
 llvm/test/LTO/AArch64/module-asm.ll | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 llvm/test/LTO/AArch64/module-asm.ll

diff --git a/llvm/test/LTO/AArch64/module-asm.ll b/llvm/test/LTO/AArch64/module-asm.ll
new file mode 100644
index 0000000000000..321c7890e9df0
--- /dev/null
+++ b/llvm/test/LTO/AArch64/module-asm.ll
@@ -0,0 +1,21 @@
+; RUN: llvm-as %s -o %t.o
+; RUN: llvm-lto %t.o --list-symbols-only | FileCheck %s
+
+; CHECK: ___foo    { function defined hidden }
+; CHECK: ___bar    { function defined default }
+; CHECK: _foo    { data defined default }
+; CHECK: ___foo    { asm extern }
+
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
+target triple = "arm64-apple-macosx12.0.0"
+
+module asm ".globl _foo"
+module asm "_foo = ___foo"
+
+define hidden i32 @__foo() {
+  ret i32 0
+}
+
+define i32 @__bar() {
+  ret i32 0
+}



More information about the llvm-commits mailing list