Skip to content

[ORC] Refactor member-loading in StaticLibraryDefinitionGenerator. #141546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2025

Conversation

lhames
Copy link
Contributor

@lhames lhames commented May 27, 2025

This refactor was motivated by two issues identified in out-of-tree builds:

  1. Some implementations of the VisitMembersFunction utility (often used to implement -all_load semantics, e.g. in the in-tree loadAllObjectFileMembers method) were assuming that buffers for archive members were null-terminated, which they are not in general. This was triggering occasional assertions.

  2. Archives may include multiple members with the same file name, e.g. if constructed by appending files with the same name: % llvm-ar crs libfoo.a foo.o % llvm-ar q libfoo.a foo.o % llvm-ar t libfoo.a foo.o foo.o

While confusing, these members may be safe to link (provided that they're
individually valid and don't define duplicate symbols). In ORC however the
archive member name may be used to construct an ORC initializer symbol, which
must also be unique. In that case the duplicate member names lead to a
duplicate definition error even if the members define unrelated symbols.

In addition to these bugs, StaticLibraryDefinitionGenerator had grown a collection of all member buffers (ObjectFilesMap), a BumpPtrAllocator that was redundantly storing synthesized archive member names (these are copied into the MemoryBuffers created for each Object, but were never freed in the allocator), and a set of COFF-specific import files.

To fix the bugs above and simplify StaticLibraryDefinitionGenerator this patch makes the following changes:

  1. StaticLibraryDefinitionGenerator::VisitMembersFunction is generalized to take a reference to the containing archive, and the index of the member within the archive. It now returns an Expected indicating whether the member visited should be treated as loadable, not loadable, or as invalidating the entire archive.
  2. A static StaticLibraryDefinitionGenerator::createMemberBuffer method is added which creates MemoryBuffers with unique names of the form <archive-name>[<index>](<member-name>). This defers construction of member names until they're loaded, allowing the BumpPtrAllocator (with its redundant name storage) to be removed.
  3. The ObjectFilesMap (symbol name -> memory-buffer-ref) is replaced with a SymbolToMemberIndexMap (symbol name -> index) which should be smaller and faster to construct.
  4. The 'loadability' result from VisitMemberFunctions is now taken into consideration when building the SymbolToMemberIndexMap so that members that have already been loaded / filtered out can be skipped, and do not take up any ongoing space.
  5. The COFF ImportedDynamicLibraries member is moved out into the COFFImportFileScanner utility, which can be used as a VisitMemberFunction.

This fixes the bugs described above; and should lower memory consumption slightly, especially for archives with many files and / or symbol where most files are eventually loaded.

lhames added 2 commits May 27, 2025 15:58
This refactor was motivated by two issues identified in out-of-tree builds:

1. Some implementations of the VisitMembersFunction utility (often used to
   implement -all_load semantics, e.g. in the in-tree loadAllObjectFileMembers
   method) were assuming that buffers for archive members were null-terminated,
   which they are not in general. This was triggering occasional assertions.

2. Archives may include multiple members with the same file name, e.g. if
   constructed by appending files with the same name:
     % llvm-ar crs libfoo.a foo.o
     % llvm-ar q libfoo.a foo.o
     % llvm-ar t libfoo.a
     foo.o
     foo.o

  While confusing, these members may be safe to link (provided that they're
  individually valid and don't define duplicate symbols). In ORC however the
  archive member name may be used to construct an ORC initializer symbol, which
  must also be unique. In that case the duplicate member names lead to a
  duplicate definition error even if the members define unrelated symbols.

In addition to these bugs, StaticLibraryDefinitionGenerator had grown a
collection of all member buffers (ObjectFilesMap), a BumpPtrAllocator that was
redundantly storing synthesized archive member names (these are copied into the
MemoryBuffers created for each Object, but were never freed in the allocator),
and a set of COFF-specific import files.

To fix the bugs above and simplify StaticLibraryDefinitionGenerator this patch
makes the following changes:

1. StaticLibraryDefinitionGenerator::VisitMembersFunction is generalized to
   take a reference to the containing archive, and the index of the member
   within the archive. It now returns an Expected<bool> indicating whether
   the member visited should be treated as loadable, not loadable, or as
   invalidating the entire archive.
2. A static StaticLibraryDefinitionGenerator::createMemberBuffer method is
   added which creates MemoryBuffers with unique names of the form
   `<archive-name>[<index>](<member-name>)`. This defers construction of
   member names until they're loaded, allowing the BumpPtrAllocator (with its
   redundant name storage) to be removed.
3. The ObjectFilesMap (symbol name -> memory-buffer-ref) is replaced with a
   SymbolToMemberIndexMap (symbol name -> index) which should be smaller and
   faster to construct.
4. The 'loadability' result from VisitMemberFunctions is now taken into
   consideration when building the SymbolToMemberIndexMap so that members that
   have already been loaded / filtered out can be skipped, and do not take up
   any ongoing space.
5. The COFF ImportedDynamicLibraries member is moved out into the
   COFFImportFileScanner utility, which can be used as a VisitMemberFunction.

This fixes the bugs described above; and should lower memory consumption
slightly, especially for archives with many files and / or symbol where most
files are eventually loaded.
Should fix the testcases on Windows.
@lhames lhames merged commit 6fa8657 into llvm:main May 27, 2025
11 checks passed
@lhames lhames deleted the static-library-definition-generator-refactor branch May 27, 2025 10:58
@llvm-ci
Copy link
Collaborator

llvm-ci commented May 27, 2025

LLVM Buildbot has detected a new failure on builder clang-ppc64-aix running on aix-ppc64 while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/64/builds/3857

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'lit :: timeout-hang.py' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 13
not env -u FILECHECK_OPTS "/home/llvm/llvm-external-buildbots/workers/env/bin/python3.11" /home/llvm/llvm-external-buildbots/workers/aix-ppc64/clang-ppc64-aix/llvm-project/llvm/utils/lit/lit.py -j1 --order=lexical Inputs/timeout-hang/run-nonexistent.txt  --timeout=1 --param external=0 | "/home/llvm/llvm-external-buildbots/workers/env/bin/python3.11" /home/llvm/llvm-external-buildbots/workers/aix-ppc64/clang-ppc64-aix/build/utils/lit/tests/timeout-hang.py 1
# executed command: not env -u FILECHECK_OPTS /home/llvm/llvm-external-buildbots/workers/env/bin/python3.11 /home/llvm/llvm-external-buildbots/workers/aix-ppc64/clang-ppc64-aix/llvm-project/llvm/utils/lit/lit.py -j1 --order=lexical Inputs/timeout-hang/run-nonexistent.txt --timeout=1 --param external=0
# .---command stderr------------
# | lit.py: /home/llvm/llvm-external-buildbots/workers/aix-ppc64/clang-ppc64-aix/llvm-project/llvm/utils/lit/lit/main.py:72: note: The test suite configuration requested an individual test timeout of 0 seconds but a timeout of 1 seconds was requested on the command line. Forcing timeout to be 1 seconds.
# `-----------------------------
# executed command: /home/llvm/llvm-external-buildbots/workers/env/bin/python3.11 /home/llvm/llvm-external-buildbots/workers/aix-ppc64/clang-ppc64-aix/build/utils/lit/tests/timeout-hang.py 1
# .---command stdout------------
# | Testing took as long or longer than timeout
# `-----------------------------
# error: command failed with exit status: 1

--

********************


@llvm-ci
Copy link
Collaborator

llvm-ci commented May 27, 2025

LLVM Buildbot has detected a new failure on builder clang-arm64-windows-msvc running on linaro-armv8-windows-msvc-04 while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/161/builds/6215

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: 1200 seconds without output running [b'ninja', b'check-all'], attempting to kill
******************** TEST 'LLVM :: ExecutionEngine/JITLink/Generic/all-load-multifile-archive.test' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
rm -rf C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp && mkdir -p C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp
# executed command: rm -rf 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp'
# executed command: mkdir -p 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp'
# RUN: at line 2
c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llc.exe -filetype=obj -o C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/foo.o C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\llvm-project\llvm\test\ExecutionEngine\JITLink\Generic/Inputs/foo-ret-0.ll
# executed command: 'c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llc.exe' -filetype=obj -o 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/foo.o' 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\llvm-project\llvm\test\ExecutionEngine\JITLink\Generic/Inputs/foo-ret-0.ll'
# RUN: at line 3
c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llc.exe -filetype=obj -o C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/bar.o C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\llvm-project\llvm\test\ExecutionEngine\JITLink\Generic/Inputs/bar-ret-0.ll
# executed command: 'c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llc.exe' -filetype=obj -o 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/bar.o' 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\llvm-project\llvm\test\ExecutionEngine\JITLink\Generic/Inputs/bar-ret-0.ll'
# RUN: at line 4
c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llvm-ar.exe crs C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/libFoo.a C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/foo.o C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/bar.o
# executed command: 'c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llvm-ar.exe' crs 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/libFoo.a' 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/foo.o' 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/bar.o'
# RUN: at line 5
c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llc.exe -filetype=obj -o C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/main.o C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\llvm-project\llvm\test\ExecutionEngine\JITLink\Generic/Inputs/main-ret-0.ll
# executed command: 'c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llc.exe' -filetype=obj -o 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/main.o' 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\llvm-project\llvm\test\ExecutionEngine\JITLink\Generic/Inputs/main-ret-0.ll'
# RUN: at line 6
c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llvm-jitlink.exe -noexec -all_load -show-init-es C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/main.o -LC:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp -lFoo      | c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\filecheck.exe C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\llvm-project\llvm\test\ExecutionEngine\JITLink\Generic\all-load-multifile-archive.test
# executed command: 'c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\llvm-jitlink.exe' -noexec -all_load -show-init-es 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/main.o' '-LC:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp' -lFoo
# .---command stderr------------
# | llvm-jitlink error: Unsupported target machine architecture in COFF object C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\test\ExecutionEngine\JITLink\Generic\Output\all-load-multifile-archive.test.tmp/main.o: ARM64
# `-----------------------------
# error: command failed with exit status: 1
# executed command: 'c:\users\tcwg\llvm-worker\clang-arm64-windows-msvc\build\bin\filecheck.exe' 'C:\Users\tcwg\llvm-worker\clang-arm64-windows-msvc\llvm-project\llvm\test\ExecutionEngine\JITLink\Generic\all-load-multifile-archive.test'

--

********************


@ceseo
Copy link
Contributor

ceseo commented May 29, 2025

@lhames this seems to be breaking the Windows on Arm bots. Could you please check?

@lhames
Copy link
Contributor Author

lhames commented May 30, 2025

@lhames this seems to be breaking the Windows on Arm bots. Could you please check?

Thanks for the heads up! I've marked these tests as unsupported in fe40f97. That should fix this failures.

sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
…vm#141546)

This refactor was motivated by two bugs identified in out-of-tree
builds:

1. Some implementations of the VisitMembersFunction type (often used to	
implement special loading semantics, e.g. -all_load or -ObjC) were assuming
that buffers for archive members were null-terminated, which they are not in
general. This was triggering occasional assertions.

2. Archives may include multiple members with the same file name, e.g.
when constructed by appending files with the same name:
  % llvm-ar crs libfoo.a foo.o
  % llvm-ar q libfoo.a foo.o
  % llvm-ar t libfoo.a foo.o
  foo.o

   While confusing, these members may be safe to link (provided that they're
   individually valid and don't define duplicate symbols). In ORC however, the
   archive member name may be used to construct an ORC initializer symbol,
   which must also be unique. In that case the duplicate member names lead to a
   duplicate definition error even if the members define unrelated symbols.

In addition to these bugs, StaticLibraryDefinitionGenerator had grown a
collection of all member buffers (ObjectFilesMap), a BumpPtrAllocator
that was redundantly storing synthesized archive member names (these are
copied into the MemoryBuffers created for each Object, but were never
freed in the allocator), and a set of COFF-specific import files.

To fix the bugs above and simplify StaticLibraryDefinitionGenerator this
patch makes the following changes:

1. StaticLibraryDefinitionGenerator::VisitMembersFunction is generalized
   to take a reference to the containing archive, and the index of the
   member within the archive. It now returns an Expected<bool> indicating
   whether the member visited should be treated as loadable, not loadable,
   or as invalidating the entire archive.
2. A static StaticLibraryDefinitionGenerator::createMemberBuffer method
   is added which creates MemoryBuffers with unique names of the form
   `<archive-name>[<index>](<member-name>)`. This defers construction of
   member names until they're loaded, allowing the BumpPtrAllocator (with
   its redundant name storage) to be removed.
3. The ObjectFilesMap (symbol name -> memory-buffer-ref) is replaced
   with a SymbolToMemberIndexMap (symbol name -> index) which should be
   smaller and faster to construct.
4. The 'loadability' result from VisitMemberFunctions is now taken into
   consideration when building the SymbolToMemberIndexMap so that members
   that have already been loaded / filtered out can be skipped, and do not
   take up any ongoing space.
5. The COFF ImportedDynamicLibraries member is moved out into the
   COFFImportFileScanner utility, which can be used as a
   VisitMemberFunction.

This fixes the bugs described above; and should lower memory consumption
slightly, especially for archives with many files and / or symbol where
most files are eventually loaded.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants