Skip to content
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

add -femit-llvm-bc CLI option and implement it, and improve -fcompiler-rt support #9440

Merged
merged 4 commits into from Jul 23, 2021

Conversation

andrewrk
Copy link
Member

@andrewrk andrewrk commented Jul 22, 2021

Emitting LLVM bitcode & improvements to stage2 LLVM backend

  • Added doc comments for std.Target.ObjectFormat enum
  • std.Target.oFileExt is removed because it is incorrect for Plan-9
    targets. Instead, use std.Target.ObjectFormat.fileExt and pass a
    CPU architecture.
  • Added Compilation.Directory.joinZ for when a null byte is desired.
  • Improvements to Compilation.create logic for computing use_llvm
    and reporting errors in contradictory flags. -femit-llvm-ir and
    -femit-llvm-bc will now imply -fLLVM.
  • Fix compilation when passing .bc files on the command line.
  • Improvements to the stage2 LLVM backend:
    • cleaned up error messages and error reporting. Properly bubble up
      some errors rather than dumping to stderr; others turn into panics.
    • properly call ZigLLVMCreateTargetMachine and
      ZigLLVMTargetMachineEmitToFile and implement calculation of the
      respective parameters (cpu features, code model, abi name, lto,
      tsan, etc).
    • LLVM module verification only runs in debug builds of the compiler
    • use LLVMDumpModule rather than printToString because in the case
      that we incorrectly pass a null pointer to LLVM it may crash during
      dumping the module and having it partially printed is helpful in
      this case.
    • support -femit-asm, -fno-emit-bin, -femit-llvm-ir, -femit-llvm-bc
    • Support LLVM backend when used with Mach-O and WASM linkers.

support -fcompiler-rt in conjunction with build-obj

When using build-exe or build-lib -dynamic, -fcompiler-rt means building
compiler-rt into a static library and then linking it into the executable.

When using build-lib, -fcompiler-rt means building compiler-rt into an
object file and then adding it into the static archive.

Before this commit, when using build-obj, zig would build compiler-rt
into an object file, and then on ELF, use lld -r to merge it into the
main object file. Other linker backends of LLD do not support -r to
merge objects, so this failed with error messages for those targets.

Now, -fcompiler-rt when used with build-obj acts as if the user puts
_ = @import("compiler_rt"); inside their root source file. The symbols
of compiler-rt go into the same compilation unit as the root source file.

This is hooked up for stage1 only for now. Once stage2 is capable of
building compiler-rt, it should be hooked up there as well.

Portable Executable is an executable format, not an object format.
Everywhere in the entire zig codebase, we treated coff and pe as if they
were the same. Remove confusion by not including pe in the
std.Target.ObjectFormat enum.
@andrewrk andrewrk changed the title add -femit-llvm-bc CLI option and implement it add -femit-llvm-bc CLI option and implement it, and improve -fcompiler-rt support Jul 23, 2021
 * Added doc comments for `std.Target.ObjectFormat` enum
 * `std.Target.oFileExt` is removed because it is incorrect for Plan-9
   targets. Instead, use `std.Target.ObjectFormat.fileExt` and pass a
   CPU architecture.
 * Added `Compilation.Directory.joinZ` for when a null byte is desired.
 * Improvements to `Compilation.create` logic for computing `use_llvm`
   and reporting errors in contradictory flags. `-femit-llvm-ir` and
   `-femit-llvm-bc` will now imply `-fLLVM`.
 * Fix compilation when passing `.bc` files on the command line.
 * Improvements to the stage2 LLVM backend:
   - cleaned up error messages and error reporting. Properly bubble up
     some errors rather than dumping to stderr; others turn into panics.
   - properly call ZigLLVMCreateTargetMachine and
     ZigLLVMTargetMachineEmitToFile and implement calculation of the
     respective parameters (cpu features, code model, abi name, lto,
     tsan, etc).
   - LLVM module verification only runs in debug builds of the compiler
   - use LLVMDumpModule rather than printToString because in the case
     that we incorrectly pass a null pointer to LLVM it may crash during
     dumping the module and having it partially printed is helpful in
     this case.
   - support -femit-asm, -fno-emit-bin, -femit-llvm-ir, -femit-llvm-bc
   - Support LLVM backend when used with Mach-O and WASM linkers.
When using `build-exe` or `build-lib -dynamic`, `-fcompiler-rt` means building
compiler-rt into a static library and then linking it into the executable.

When using `build-lib`, `-fcompiler-rt` means building compiler-rt into an
object file and then adding it into the static archive.

Before this commit, when using `build-obj`, zig would build compiler-rt
into an object file, and then on ELF, use `lld -r` to merge it into the
main object file. Other linker backends of LLD do not support `-r` to
merge objects, so this failed with error messages for those targets.

Now, `-fcompiler-rt` when used with `build-obj` acts as if the user puts
`_ = @import("compiler_rt");` inside their root source file. The symbols
of compiler-rt go into the same compilation unit as the root source file.

This is hooked up for stage1 only for now. Once stage2 is capable of
building compiler-rt, it should be hooked up there as well.
The include_compiler_rt stored in the bin file options means that we need
compiler-rt symbols *somehow*. However, in the context of using the stage1 backend
we need to tell stage1 to include compiler-rt only if stage1 is the place that
needs to provide those symbols. Otherwise the stage2 infrastructure will take care
of it in the linker, by putting compiler_rt.o into a static archive, or linking
compiler_rt.a against an executable. In other words we only want to set this flag
for stage1 if we are using build-obj.
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.

None yet

1 participant