-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
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
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. |
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. |
c3a01e0
to
d04cfb5
Compare
There was a problem hiding this 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.
|
||
const ptr: [*]const u8 = @ptrCast(bitcode.ptr); | ||
try file.writeAll(ptr[0..(bitcode.len * 4)]); | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
The LLVM bitcode requires all type references in structs to be to earlier defined types. We make sure types are ordered in the builder itself in order to avoid having to iterate the types multiple times and changing the values of type indicies.
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. |
Closing abandoned draft. |
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 realWriter
.Blocks:
Bitcode writer TODO list: