Skip to content

Commit

Permalink
llvm: convert bitcode in static archives to object code
Browse files Browse the repository at this point in the history
In #106925, I enabled LTO for our LLVM build. This creates static
archives that contain LLVM bitcode instead of object code. This makes
the static archives more difficult to use, requiring workarounds such as

    https://github.com/Homebrew/homebrew-core/blob/c01f1794fc3decce04b71cae03966213fc7af34d/Formula/enzyme.rb#L30

and has caused problems for multiple downstream projects.

We can fix this by converting the bitcode into object code, which is
what Fedora does with their LLVM build. They also build their toolchain
with LTO.

Alternatively, we can disable LTO, but that foregoes significant
speedups we get from enabling it.

While we're here, let's add some test coverage for features that were
recently enabled that we don't test.

Fixes:

ziglang/zig#12923
halide/Halide#7055
mesonbuild/meson#10879
Homebrew/discussions#3666
  • Loading branch information
carlocab committed Oct 2, 2022
1 parent 2a020ee commit 6e8c5f7
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Formula/llvm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,24 @@ def install

# Install Emacs modes
elisp.install llvmpath.glob("utils/emacs/*.el") + share.glob("clang/*.el")

return if OS.linux? || !pgo_build

# Convert LTO-generated bitcode in our static archives to MachO. Adapted from Fedora:
# https://src.fedoraproject.org/rpms/redhat-rpm-config/blob/rawhide/f/brp-llvm-compile-lto-elf
lib.glob("*.a").each do |static_archive|
mktemp do
system bin/"llvm-ar", "x", static_archive
Pathname.glob("*.o").each do |bc_file|
file_type = Utils.safe_popen_read("file", bc_file)
next unless file_type.match?("LLVM bitcode")

system bin/"clang", "-fno-lto", "-Wno-unused-command-line-argument",
"-x", "ir", bc_file, "-c", "-o", bc_file
system bin/"llvm-ar", "r", static_archive, bc_file
end
end
end
end

def caveats
Expand Down Expand Up @@ -643,6 +661,17 @@ def caveats
assert_equal "int main() { printf(\"Hello world!\"); }\n",
shell_output("#{bin}/clang-format -style=google clangformattest.c")

# Test static analyzer
(testpath/"unreachable.c").write <<~EOS
unsigned int func(unsigned int a) {
unsigned int *z = 0;
if ((a & 1) && ((a & 1) ^1))
return *z; // unreachable
return 0;
}
EOS
system bin/"clang", "--analyze", "-Xanalyzer", "-analyzer-constraints=z3", "unreachable.c"

# This will fail if the clang bindings cannot find `libclang`.
with_env(PYTHONPATH: prefix/Language::Python.site_packages(python3)) do
system python3, "-c", <<~EOS
Expand All @@ -651,6 +680,10 @@ def caveats
EOS
end

# Check that lldb can use Python
lldb_script_interpreter_info = JSON.parse(shell_output("#{bin}/lldb --print-script-interpreter-info"))
assert_equal "python", lldb_script_interpreter_info["language"]

# Ensure LLVM did not regress output of `llvm-config --system-libs` which for a time
# was known to output incorrect linker flags; e.g., `-llibxml2.tbd` instead of `-lxml2`.
# On the other hand, note that a fully qualified path to `dylib` or `tbd` is OK, e.g.,
Expand Down

0 comments on commit 6e8c5f7

Please sign in to comment.