ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.85k stars 2.55k forks source link

self-hosted compiler: ship it! #89

Closed andrewrk closed 2 years ago

andrewrk commented 8 years ago

This is a tracking issue for when we can completely replace stage1 with self-hosted in official builds of Zig.

andrewrk commented 7 years ago

~When we release zig 1.0.0, we'll actually have the self hosted compiler built and ready to go, passing all tests. Both projects will be maintained until 2.0.0.~

andrewrk commented 7 years ago

There are some compelling reasons to self-host:

We still want the bootstrapping process to be simple though. So here's another proposal. We get a self-hosted compiler going right now. It's the official zig compiler. However the C++ implementation must be able to build the official zig compiler. As long as that remains true, bootstrapping is 1 step process.

andrewrk commented 6 years ago

Things I Want to Improve in the Self-Hosted Compiler

Performance and Caching

Representation of Types and Values

ConstExprValue right now has a lot of footguns built into it, and it wastes memory. The new data layout should accomplish these things:

phase commented 6 years ago

We'll have LLVM spitting out .o files before the last source file has been tokenized.

How will this work with name resolution? You can't compile a file that depends on a file that hasn't been parsed yet.

andrewrk commented 6 years ago

Here's an example:

You can see from this example we would get better parallelism if we prioritized analysis of functions since that creates jobs for the pipeline - it would make thread 2 have something to do while thread 1 analyzes baz(). But this should illustrate the idea.

ghost commented 6 years ago

0.3.0 ... seemed so close 😃

andrewrk commented 6 years ago

Yeah. I couldn't make the deadline. 0.3.0 is two weeks away and I think those two weeks can best be spent on:

ghost commented 6 years ago

building master always anyway 👍

ghost commented 6 years ago

Something that I would like to see with a self-hosted compiler is the ability to import the compiler as a library within my application to compile and link new code while running. For example, https://github.com/anael-seghezzi/CToy embeds tcc and provides a creative coding environment that does not require a restart when modifying code.

Is such a thing possible already when importing std.build?

andrewrk commented 6 years ago

Something that I would like to see with a self-hosted compiler is the ability to import the compiler as a library within my application to compile and link new code while running.

Unfortunately that's not ever going to be possible, because of the LLVM and clang dependency. Zig compiler ships with LLVM and clang libraries built into the zig compiler. And Zig supports cross compiling for many targets. To import the compiler as a library would require that LLVM and clang were available in source form (written in zig) so that they could be cross compiled for the target. To give up LLVM/clang and code our own code generator in zig would be giving up state-of-the-art optimizations and a very active community of people working on it.

However, the parts of the compiler that do not depend on LLVM and clang are available in the standard library, for example the parser and formatter. std.zig.parse and std.zig.render.

As for a coding environment that does not require a restart when modifying code, see #68.

marler8997 commented 5 years ago

Having LLVM/Clang written in C++ shouldn't prevent the compiler from being used as a library. Just like zig programs can link to and use C libraries even though they aren't in Zig.

Tetralux commented 5 years ago

To import the compiler as a library would require that LLVM and clang were available in source form

I think they meant a library as in a DLL, SO or archive, etc.

momumi commented 4 years ago

Something that I would like to see with a self-hosted compiler is the ability to import the compiler as a library within my application to compile and link new code while running.

It would be cool if you could take the idea of a comptime function and extended it to a just-in-time function. So you could take a function fn regex(comptime re: []const u8, str: []u8) bool, give it a runtime-known string for re, and then libzig.so would give you a function pointer to a JIT compiled version that your program could call.

It'd be a pretty niche feature, so I'm not suggesting it should be included. However, I don't think it's been done before in a statically compiled language, so it'd be really unique feature if it wasn't too hard to implement. Although I don't know too much about compilers, so maybe I'm grossly underestimating the difficult.

yozachar commented 2 years ago

Hi, coming from https://kristoff.it/blog/zig-new-relationship-llvm/#when-is-it-going-to-be-ready, I do not understand why https://github.com/ziglang/zig/projects/2 is closed.

Avokadoen commented 2 years ago

@andrewrk I would like to add zig_vulkan to the third party list. It's a gpgpu ray tracing voxel renderer which I am making for my master thesis.

mlarouche commented 2 years ago

I would like to add the following to the third party list:

Thanks :)

mattnite commented 2 years ago

I would like to add the following:

leecannon commented 2 years ago

I think zls https://github.com/zigtools/zls should be added to the list of third party projects to check.

sin-ack commented 2 years ago

I nominate https://github.com/sin-ack/zigself. While not particularly interesting it does do some funky stuff regarding pointer alignment which might be worth taking a look at.

kuon commented 2 years ago

I would like to add the following:

* https://github.com/ZigEmbeddedGroup/microzig (`zig build -Drelease-small`)

I am now working with zig on some STM32 MCU and binary size is absolutely critical. Is there a binary size comparison/test suite to ensure common uses cases do not regress? I'd be happy to test the firmware code I am working on for that if it can help. Just poke me on zig discord.

floooh commented 2 years ago

Hi, I'd like to add the sokol-zig bindings to the list of thirdparty projects, it doesn't use any fancy Zig features, but it's a mixed C/Zig project where the C code talks to system-level APIs on Windows, Linux and macOS:

https://github.com/floooh/sokol-zig

...when running zig build -fno-stage1 I get a couple of interesting errors which I don't quite understand:

/Users/floh/projects/sokol-zig/src/sokol/app.zig:316:29: error: comptime call of extern function
    return sapp_color_format();
                            ^
/Users/floh/projects/sokol-zig/src/sokol/app_gfx_glue.zig:6:41: note: called from here
        .color_format = sapp.colorFormat(),
                                        ^
/Users/floh/projects/sokol-zig/src/examples/clear.zig:14:33: note: called from here
        .context = sgapp.context()
                                ^
/Users/floh/projects/sokol-zig/src/sokol/app.zig:430:12: error: extern function cannot be generic
pub extern fn sapp_run([*c]const Desc) void;

Cheers!

(PS: sokol-zig bindings might also be interesting to test ABI conformance on various platforms, for instance on macOS for Intel Macs I had to pad small structs (<16 bytes) so that they become bigger than 16 bytes, otherwise there would be corruption when passing or returning structs by values between Zig and C)

Vexu commented 2 years ago

error: comptime call of extern function

Those are caused by stage1 function pointer semantics, s/?fn/?*const fn/ should fix that.

floooh commented 2 years ago

@Vexu thanks! That's getting me a bit further :) I hit a compiler crash later though (and in the stage1 compiler the code compiles using the new function pointer syntax, but I'm getting a weird crash the first time a C callback function is called. I'll put this info into a reminder ticket in the bindings for now, until I have a bit more time digging deeper :)

PS: correction: at least the simple "clear screen sample" works with the function pointer changes in the stage2 compiler! The other, more complex samples currently show various problems, from a compiler crash to "runs but only renders a blank screen".

The stage1 compiler seems to produce broken code for the new function pointer syntax though, when calling into a C callback function I get a hard crash.

Anyway, it looks promising :)

Vexu commented 2 years ago

In stage1 *const fn is a double pointer, if you want your code to work with both versions you need to check @import("builtin").zig_backend.

andrewrk commented 2 years ago

Hi @floooh, thanks for stopping by. I added sokol to the list of projects. Definitely appreciate the testing but it might be best to wait until all those other boxes are ticked before giving it a serious go - I don't want you to waste your time finding issues that we already know about :-)

andrewrk commented 2 years ago

Hello everybody,

The self-hosted compiler is now the default compiler. I have marked the 0.10.0 milestone with the due date of September 21, 2022.

Please take a look at the upgrade guide for help deciding when to upgrade and how to upgrade.

I will personally be working on the existing bug reports that are affecting third party projects, and then continue working with the projects to get them fully upgraded.

RobertWHurst commented 2 years ago

Congratulations @andrewrk! I know you and your team have been working diligently to make this happen for a long time. It's a serious milestone. Nicely done 🎉

lin72h commented 2 years ago

Just witness a historical event, pure awesome

hako commented 2 years ago

major congrats @andrewrk and team for shipping this huge milestone for Zig! ⚡ 🚢

malcolmstill commented 2 years ago

I have a table of function pointers which stage2 tells me to change to const* (which makes sense). But I then use those function pointers with @call(.{ .modifier = .always_tail } and I get the error error: modifier 'always_tail' requires a comptime-known function. Not sure what I need to do to fix that?

See https://github.com/malcolmstill/foxwren/pull/139

kuon commented 2 years ago

Maybe we could create a specific issue template for bug occurring due to the upgrade? I don't think it will be efficient to make this one longer.

david4r4 commented 2 years ago

I have a table of function pointers which stage2 tells me to change to const* (which makes sense). But I then use those function pointers with @call(.{ .modifier = .always_tail } and I get the error error: modifier 'always_tail' requires a comptime-known function. Not sure what I need to do to fix that?

See malcolmstill/foxwren#139

Yes, I agree with @kuon. You should discuss it in another issue or in any community channel. Just to give you a hint, basically you can't tail a function pointer call because you have no idea what body the function has.

By the way, congrats to the Zig team and the whole community! :)

malcolmstill commented 2 years ago

@kuon @davidgm94 will do, thanks.