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

Emit LLVM bitcode without using LLVM #16804

Closed
wants to merge 10 commits into from
Closed

Conversation

antlilja
Copy link
Contributor

@antlilja antlilja commented Aug 13, 2023

This PR is a work in progress attempt to emit LLVM bitcode without using libllvm (#13265).

Bitcode can be emitted by running:
zig build-exe main.zig -fstrip -fno-libllvm -femit-llvm-bc=main.bc -fno-emit-bin

The Bitcode writer currently writes into its own ArrayList(u32) because there are times when we have to write to an earlier part of the stream. This could also be solved using a writer and a SeekableStream in order to avoid allocating all the bitcode in memory when we only need to write it to file. But I don't think this will won't work if we want to pipe the output to another process. Another option would be a hybrid approach where we only store a block until it is finished and then flush it to the real Writer.

Blocks:

  • Identification
  • Strtab
  • Module
    • Type
    • Function
    • Constants
    • ValueSymtab
    • ParamAttr
    • ParamAttrGroup
    • GlobarVar record
    • Function record (all emitted as declarations currently)
    • Alias record (aliasee value not emitted)

Bitcode writer TODO list:

  • Signed VBRs

@andrewrk
Copy link
Member

Buffering the entire bitcode module in memory is OK for this backend. Normally, the goal would be to make it behave more like the ELF linker code, using padding to do incremental linking, only writing the changed parts of the file. However, for the near future we will want Zig to use LLVMParseBitcodeInContext2 (which takes a memory buffer) in order to create the LLVM module. In this case, going through the file system would only be a waste of time, unless it was requested by -femit-llvm-bc, which is relatively rare.

But I don't think this will won't work if we want to pipe the output to another process.

Piping LLVM bitcode from the compiler to another process is not a supported use case, and not something that should drive any kind of compiler design or decision-making.

@andrewrk
Copy link
Member

One more note - you might want to do this work against the llvm17 branch. If LLVM releases on time, and we have the regressions fixed or worked around by then, that branch will get merged into master on September 5th.

@antlilja antlilja force-pushed the llvm-bc branch 2 times, most recently from c3a01e0 to d04cfb5 Compare August 17, 2023 14:13
Copy link
Member

@jacobly0 jacobly0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, I'm more than happy not to have to do this part myself.

src/codegen/llvm/Builder.zig Outdated Show resolved Hide resolved
src/codegen/llvm/Builder.zig Outdated Show resolved Hide resolved

const ptr: [*]const u8 = @ptrCast(bitcode.ptr);
try file.writeAll(ptr[0..(bitcode.len * 4)]);
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return;
if (emit_asm_path == null and emit_bin_path == null and emit_llvm_ir_path == null) return;

It will eventually be the frontend's responsibility to check if the requested emits are invalid, but for now, not creating an expected output file and returning success here will just confuse the build system.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree, that return was mostly for testing. I'll change it to handle it more reasonably.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I figured, feel free to not resolve it until this is ready for review.

@antlilja
Copy link
Contributor Author

I'm still working on this, I just got preoccupied with university stuff for a bit. I'm currently working on the instruction encoding and blocks. Just wanted to give an update so it doesn't seem like I've disappeared.

@andrewrk
Copy link
Member

andrewrk commented Jan 8, 2024

Closing abandoned draft.

@andrewrk andrewrk closed this Jan 8, 2024
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