[PATCH] D154364: [LTO] Allow library calls to be internalized in freestanding mode

Joseph Huber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 3 09:33:20 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: phosek, mcgrathr, peter.smith, paulkirth, MaskRay, jdoerfert, JonChesterfield.
Herald added subscribers: ormris, steven_wu, hiraditya, inglorion.
Herald added a project: All.
jhuber6 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Currently, we mark known library calls, (e.g. malloc / printf) as used
symbols because the linker expects these to be present. However, this
means that they cannot be removed through LTO. The current LTO
configuration allows for the `Freestanding` option which implies that we
should not emit library calls from the backen as we may not have them.
This patch extends that logic to also imply that we can internalize
these library calls signals if they are present, because in freestanding
mode they should bind to regular symbols.

Currently, this is important for my GPU `libc` project. We rely on LTO
linking for the GPU to optimize binaries and currently, certain symbols
cannot be internalized by the LTO pass because they match the known
library calls. This is intentional, but because the GPU is an unhosted
environment and everything is linked in statically we should be able to
internalize these.

This patch primarily just adds a new IR symbol table flag to indicate a
known library call. This is then used to special-case handle symbols
that are used but which can be internalized because of Freestanding
logic.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154364

Files:
  llvm/include/llvm/Object/IRSymtab.h
  llvm/lib/LTO/LTO.cpp
  llvm/lib/Object/IRSymtab.cpp


Index: llvm/lib/Object/IRSymtab.cpp
===================================================================
--- llvm/lib/Object/IRSymtab.cpp
+++ llvm/lib/Object/IRSymtab.cpp
@@ -274,6 +274,8 @@
 
   if (Used.count(GV) || IsPreservedSymbol)
     Sym.Flags |= 1 << storage::Symbol::FB_used;
+  if (IsPreservedSymbol)
+    Sym.Flags |= 1 << storage::Symbol::FB_libcall;
   if (GV->isThreadLocal())
     Sym.Flags |= 1 << storage::Symbol::FB_tls;
   if (GV->hasGlobalUnnamedAddr())
Index: llvm/lib/LTO/LTO.cpp
===================================================================
--- llvm/lib/LTO/LTO.cpp
+++ llvm/lib/LTO/LTO.cpp
@@ -667,7 +667,8 @@
     // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
     // regular object, is referenced from llvm.compiler.used/llvm.used, or was
     // already recorded as being referenced from a different partition.
-    if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
+    if (Res.LinkerRedefined || Res.VisibleToRegularObj ||
+        (Sym.isUsed() && !(Conf.Freestanding && Sym.isLibCall())) ||
         (GlobalRes.Partition != GlobalResolution::Unknown &&
          GlobalRes.Partition != Partition)) {
       GlobalRes.Partition = GlobalResolution::External;
Index: llvm/include/llvm/Object/IRSymtab.h
===================================================================
--- llvm/include/llvm/Object/IRSymtab.h
+++ llvm/include/llvm/Object/IRSymtab.h
@@ -114,6 +114,7 @@
     FB_format_specific,
     FB_unnamed_addr,
     FB_executable,
+    FB_libcall,
   };
 };
 
@@ -200,6 +201,7 @@
   bool isCommon() const { return (Flags >> S::FB_common) & 1; }
   bool isIndirect() const { return (Flags >> S::FB_indirect) & 1; }
   bool isUsed() const { return (Flags >> S::FB_used) & 1; }
+  bool isLibCall() const { return (Flags >> S::FB_libcall) & 1; }
   bool isTLS() const { return (Flags >> S::FB_tls) & 1; }
 
   bool canBeOmittedFromSymbolTable() const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154364.536801.patch
Type: text/x-patch
Size: 1938 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230703/1db5a868/attachment.bin>


More information about the llvm-commits mailing list