17 ms·
Zig's New Relationship with LLVM
- butterisgood 6y agoI’ve often wanted to try out Zig at work but the dependency on the latest LLVM meant doing that first. Building LLVM from scratch on our platform takes more resources (including time) than I normally have available (LLDB needs like 6GB of RAM to link?) So, I welcome this news!
- dnautics 6y agoI thing zig ships with the relevant prebuilt llvm if you don't mind being tied to a release.
- deleted 6y ago[deleted]
- AnIdiotOnTheNet 6y agoZig releases are available in statically compiled form, no dependencies, no compilation necessary.
- butterisgood 6y agoNote I mentioned I wanted it for our platform... there is no pre-built Zig for it - despite being BSD based.
- eggy 6y agoAside from using software that relies on LLVM, I am not at all familiar with its internals. A fellow on another forum was ranting against using LLVM as a backend for security reasons pointing out its sheer size and the fact that it is a "US controlled software stack" [1]. Don't get me wrong, I think he has a very fringe and suspicious outlook on LLVM and the US, but it is a big codebase that would be hard to audit for such things. Would Zig ever be fully independent and auditable by a small team when the break from LLVM is made? Just curious, and I would love input on the LLVM comspiracy topic. [1] https://www.mail-archive.com/picolisp@software-lab.de/msg09579.html https://www.mail-archive.com/picolisp@software-lab.de/msg095...
- higerordermap 6y ago90% of your computing stack is US controlled code. What's so different about LLVM LLVM monoculture is pretty bad though.. and its codebase quality is not very good either. But that's another issue.
- Arelius 6y agoEven if your build system isn't set up for it, you could potentially do a cross-compiled build for just LLVM your target architecture, then just rely on those binaries?
- butterisgood 6y agoThat’s a really interesting idea... Got a new spare time project to try out!
- IshKebab 6y agoThis looks amazing, as do many of Zig's features (e.g. comptime). It's hard to go back to memory errors after Rust, but I hope Zig becomes popular enough with some people (those that don't care about security? maybe game developers?) that it influences future languages.
- pjmlp 6y agoUnless we get "Unreal/Unity for Zig" not really.
- AnIdiotOnTheNet 6y agoWhile many developers do use these kinds of pre-built all-in-one game engines for various reasons (and that's great because it means more people can make games without the work of building an engine), there are still many many game developers who write their own "engine" for a wide variety of reasons.
- gameswithgo 6y agoZig has a lot of features that make it really nice for game development. Easy and no overhead C interop of course, good runtime performance, fast compilation. While it doesn't have as much safety as Rust it is at least a half step between C and Rust, with option types and better management of undefined behavior. Zig probably makes more sense as a gamedev language than Rust for single player/non networked games, where a memory mistake isn't leading to leaked data or server exploits.
- dnautics 6y agoSafety is a much more complicated topic than "does my language guard against memory safety", so I wouldn't slyly dismiss zig as "only good for games".
- ikskuh 6y agoZig is also designed around safety and security. It's not as obvious as with Rust, but it's still there. Zig is way easier to verify than rust code (no hidden control flow, no hidden allocations that might fail, ...) which means a security expert can read a single function (without knowing more about the code) and can reason about that function. This does not work in languages like C++ and Rust where RAII is a common pattern and code will be implicitly be executed when some objects go out of scope. Yes, sure. Rust does a lot of verification work already, but so do tools like cppcheck and other verification tools and i'm sure those will emerge for Zig as well. Zig (in contrast to C or Rust) has no warnings. Behaviours that might not be OK are compile errors and cannot be ignored. There are also plans (as in "not implemented yet") to forbid declaring unused variables and such so even this gets a compile error. This makes Zig also easy to reason about (seeing a variable declaration means that it's guaranteed to be used later)
- higerordermap 6y agoI always like innovations in programming tooling. To me zig sounds like a system language written by system programmers, while rust is more influenced from FP and webdev communities, which is reflected in both tooling and language. To people asking why zig when there's rust, both are nice in different ways.
- kristoff_it 6y agoI think Zig will reserve a few interesting surprises also in the webdev world, it just needs a bit more time to get there.
- sam0x17 6y agoIf anything, Zig will have much fewer restrictions (no borrow checker) than Rust when it comes to web dev, once Zig matures to the point where there is a good standard HTTP client and server implementation and a package manager. I'm a fan of Rust for things like a missile control system or a stock exchange, but not, say, writing a CRM. I'd gladly write a CRM in Zig however, given the proper library support and tooling. Why? productivity. In my experience it just takes 2-3x longer to get the same thing done in Rust vs. something like Zig/Crystal/Go and certainly vs. Ruby/Python/etc. For extremely mission critical code, this is fine, but for most startups I would argue it probably isn't appropriate. Definitely staying tuned as Zig develops.
- ikskuh 6y agoOne experience i can share with Zig and webdev is this: I was writing a emulator (and later: a compiler) in Zig which was originally written for PC. But i got the idea that hosting this stuff on the web page would be cool, so i learned how to do Wasm. The result was for both project a glue file with ~150 LOC Zig and ~150 JS and both compiler and emulator ran in the browser, and porting both projects took less than an afternoon of time.
- sam0x17 6y agoVery cool. In my case, I have my eye on Zig as the target language for some web framework ideas I've had for a while, and I'm just waiting for some library maturity at this point before I get started.
- xvilka 6y agoI hope they will make it also reproducible [1] and easily bootstrappable [2], for easier porting to new platforms. [1] https://reproducible-builds.org/ https://reproducible-builds.org/ [2] https://bootstrappable.org/ https://bootstrappable.org/
- kristoff_it 6y agoQuick bootstrapping is one of the goals for 1.0. Related issue: https://github.com/ziglang/zig/issues/6378 https://github.com/ziglang/zig/issues/6378
- ifreund 6y agoIncremental debug builds will of course not be reproducible. Release builds will be.
- tom_mellior 6y agoI don't understand the "in-place binary patching" bit. Does it literally mean that the compiler opens the existing binary in a write-but-do-not truncate mode, leaves most bits in place, and only changes others in certain places? Is this expected to be that much faster than writing the whole binary anew? What other benefits is this supposed to have? The article doesn't seem to say. Also, how does this relate to the observation just above that "the right-most box is always invalidated ... the final executable is always re-linked from scratch ... since the final executable depends on everything else"? Does linking somehow get faster or easier with in-place patching?
- kristoff_it 6y ago> Does it literally mean that the compiler opens the existing binary in a write-but-do-not truncate mode, leaves most bits in place, and only changes others in certain places? Yes. > Does it literally mean that the compiler opens the existing binary in a write-but-do-not truncate mode, leaves most bits in place, and only changes others in certain places? I'm not an expert, but I believe the point is mostly about other computation required for "normal" linking, not just writing fewer bytes to disk. > What other benefits is this supposed to have? It makes the edit-compile-run loop faster when developing. > Does linking somehow get faster or easier with in-place patching? That's the idea. When code isn't position independent, moving a chunk of code requires recomputing a bunch of offsets, and similarly the linker needs to do more work when you don't have an indirect way of referring to functions etc.
- ikskuh 6y agoYes, you understood that right. The existing executable file is not written completly anew. And yes, it will be faster. The way Zig does this is by only recompiling and modifying the parts of the executable where the source has changed. Imagine a project like chromium, where your executable is 150 MB large. Now you change a single function. Classic linking would require the whole linking process and the write of 150 MB of code. Zig will only compile and patch a single function (when lucky: less than 1kB) and emit this. Less I/O, less overall work to do.
- 6y ago
- aidenn0 6y agoWith PIC, incremental compilation, and function calls through a GOT, can Zig patch a running binary as well?
- ifreund 6y agoYou're totally right that at least 90% of the infrastructure is there. Andrew's been threatening to implement hot code swapping in a live stream but has been busy with other tasks it seems.
- aidenn0 6y agoWith the proper editor integration, that would sell me on Zig; that's easily in my top 5 features that keeps me with Lisp.
- pedrow 6y agoThis is interesting stuff. Could you comment about optimization? LLVM has put a lot of effort into optimizing code at various levels, including the LTO stuff. Will Zig match all this by itself, or will a 'Release mode' still use LLVM to produce a final version?
- kristoff_it 6y agoLLVM is still going to be used for release builds. As of now, the self-hosted backend is dedicated to producing debug builds without optimizations (and with a few extra runtime inefficiencies, as that's the price for faster in-place binary replacement).
- nick_ 6y agoThanks, that's the answer to my question :) Do you know if there has been any plans to implement this build-time optimization upstream in LLVM itself? Perhaps after it's been completed within Zig.
- alexnask 6y agoWhile I am not terribly familiar with the LLVM codebase, this kind of improvement would probably require massive internal changes. One example of this is the fact that codegen needs to be aware of the linker to emit correct code depending on the type of executable that is being generated, since different strategies can and are used for different formats (for example, currently the x86_64 backend will generate different instructions for function calls on ELF and PE/COFF vs MachO). I would totally get behind LLVM or other compiler toolchains working towards this kind of functionality, but this is hardly something Zig developers could implement in a massive project such as LLVM (which is precisely why we are building our own :))
- nick_ 6y agoVery interesting. It makes a lot of sense. Do you have a read on how well using the LLVM interpreter modes fill this gap? EDIT: To be clear, I mean the gap of slow build performance in the feedback look of code-build-check.
- 10000truths 6y agoIf build times are fast enough, it just might be feasible to create a robust compile-as-you-type plugin for your favorite editor, for interactive compile-time error checking. It's the one thing I always liked about using Java IDEs.
- alexnask 6y agoSupporting some kind of semantic server that will provide type information and information about comptime computations is one of the goals of the self hosted compiler. Currently no work has been done on this as the focus is on the incremental linkers and codegen but I personally think we should be able to make a pretty solid tool that will not only provide langserv capabilities but also allow us to inspect and debug metaprograms with ease :)
- underdeserver 6y agoZig's looking like a really beautiful language to try out.
- benesch 6y agoYes, yes, yes, yes, yes. I'm going to make the kind of prediction that will set me up to be the laughingstock of HN in a few years. I think in-place binary patching will be the single most consequential development in build toolchains in the last twenty years; the most consequential development since a graduate student at the University of Illinois named Chris Lattner decided to embark on LLVM. If Zig is successful in this endeavor, by the end of this decade I expect that in-place binary patching will be a feature that comes standard with most build toolchains. When you reason about build systems from first principles, it becomes abundantly clear that our current approach to linking is nuts. In both my current job and my last job, linking was by far the bottleneck in the development cycle, and a nearly unfixable one. Nothing puts a damper on getting into the zone like a sixty second link. Change a string literal somewhere? Wait sixty seconds. Fix a one-line bug in the test you just wrote? Wait sixty seconds. Change the guts of a function, but not its public interface? Wait sixty seconds. Compilation is massively parallel, so if your compiler is slow, you have options. You can buy a bigger computer with more cores. You can outsource your compilation to the cloud. You can arrange your code cleverly to minimize dependencies, so changing one package doesn't result in invalidating too many downstream packages. But if linking is slow, you're pretty stuck. Linking always happens, linking always happens last, and linking always depends on the output of all previous steps. Linking is rarely parallel and rarely incremental. You can switch from macOS to Linux, because Apple skimps on their linker. You can switch from the standard GNU linker (bfd) to the new GNU linker (gold) [0], which has both built-in parallelism and an incremental mode. But gold is only 20-30% faster. I'm not excited by a 30% speedup. Saving 30% on a 60s link still means you have a brutal 42s link; still plenty long to kick you out of the flow. A 30% speedup is the kind of speedup that's erased in a year when your project is 30% larger. What I'm excited about is a speedup the size of several orders of magnitude! That's the magic that Zig is striving for here. Change a string literal? Bam! Your binary has already been relinked, before your finger even rolled off the return key. This is the kind of thing that has the potential to change our relationship with compiled languages forever. The differences between a dynamic language like Python and a compiled language like Zig become mightly slim when recompiling a large Zig program becomes as fast as restarting a Python program. And the only way to get there is to redesign the compiler toolchain from start to finish. You need everything from the design of the programming language down to a custom linker to cooperate. Binary formats like ELF are not designed to be hot-swappable like this; they're designed to be write-once. The same goes for debugging information like DWARF. They've developed some very clever tricks to make this work. I wouldn't be surprised if one day there's a custom, non-ELF binary format specifically for the use of incremental toolchains. Huge props to Andy Kelley and the Zig team for having the vision and the guts to embark on this quest. (That reminds me: I am long overdue on setting up a sponsorship for the Zig Software Foundation [1] to fund this sort of work!) [0]: https://en.wikipedia.org/wiki/Gold_(linker) https://en.wikipedia.org/wiki/Gold_(linker) [1]: https://ziglang.org/zsf/ https://ziglang.org/zsf/
- gravitas 6y agoI see zig users commenting here -- the "getting started" guide at https://ziglearn.org/ https://ziglearn.org/ immediately fails with the installable zig binary from Arch (0.6.0) due to https://github.com/ziglang/zig/issues/5683 https://github.com/ziglang/zig/issues/5683 I'd never heard of zig until now, but as a community having top level "how to get started" website using features only available from the Master branch of github is... not a good way to get people into your language. It'd be nice if your getting started guide only used features from the tagged latest version at least... (I get that it's moving fast, but geez this is a Hello, World sample that fails)
- gravitas 6y agoSimilarly, I rolled back to the official docs from Master and the hello.zig from there is similarly nonop using the tagged 0.6.0 build: https://ziglang.org/documentation/master/ https://ziglang.org/documentation/master/ Please, please - make a Hello, World which is not so intrinsically tied to random features on Master to just work across all the iterations. Hello, World is supposed to be the simplest, non-breaking easy to compile no esoteric compiler feature thing in any programming language in the world.
- kristoff_it 6y agoUse std.debug.warn and you'll be fine with 0.6.0 and master. Right now there's no strict and methodical update path from one version to the next, but generally speaking changes to the language get special-cased into zig fmt. For example the current `anytype` type has replaced `var` and running zig fmt on a project I hadn't updated yet changed all references. That said, Hello World, when taken seriously, it's not that simple of a program and I'm working on a talk for Zig SHOWTIME[1] about it, titled "Advanced Hello World in Zig". The TLDR is that Zig never had an easy builtin print function because it's important to let the programmer be precise about whether they want locking (or not) or buffering (or not). That's why the easy print function is in the debug namespace. [1] https://zig.show https://zig.show
- gravitas 6y ago
- snicker7 6y agoIncrementally patching binaries is obviously very cool. But is it possible that such functionality could be added to LLVM proper instead of just Zig?
- txdv 6y agoi think you need to start out with support for that, adding it in hinsight is so much harder
- pron 6y agoZig is one of the most interesting languages I've seen in a very long time, and possibly the first radical breakthrough in low-level programming design in decades. Maybe it will become a big success and maybe it will tank, but after having two visions for low-level programming -- that of C's "syntax-sugared Assembly", or that of C++'s "zero-cost abstractions" whose low-level, low-abstraction code appears high level on the page, once you get all the pieces to fit (Rust, while introducing the ingenious borrow checking, still firmly follows C++'s design philosophy and can be seen as a C++ that checks your work) -- Zig shows a third way by rethinking, from the ground up, how low-level programming could and should be done. It manages to address all of C++'s biggest shortcomings, which, in my view, are 1. language complexity, 2. compilation time, 3. safety -- in this order -- and it does so in a language that is arguably simpler than C, can be fully learned in a day or two (although the full implications of the design might take longer to sink in), and also inspires a new approach to partial evaluation, replacing generics and value templates, concepts/traits/typeclasses, constexprs/procedural macros, macros (or, at least, the "good parts" of macros) and conditional compilation with a single, simple feature.
- skohan 6y agoI really like the Zig approach of improving the tooling for manual memory management rather than replacing manual memory management. I think Rust is a great, worthy language, and for a lot of use-cases it makes sense to optimize for trying to just put everything on the stack, but there are a lot of other cases where what you really want to do is own allocation by yourself instead of trusting it to the compiler. I haven't tried Zig, but I really want to see if it can deliver on this.
- whatshisface 6y agoRust has manual allocation with Box<T>, it's just not as automatic as Zig. Rust's manual allocation pain is constant-factor overhead: typing out the type signatures takes longer every time you use it, but the complexity doesn't grow beyond that. I don't enjoy typing Box everywhere, but it's not that bad. https://doc.rust-lang.org/std/boxed/index.html https://doc.rust-lang.org/std/boxed/index.html
- pabs3 6y agoI wonder how this will affect Zig's bootstrappability from source. https://bootstrappable.org/ https://bootstrappable.org/
- AndyKelley 6y agohttps://github.com/ziglang/zig-bootstrap https://github.com/ziglang/zig-bootstrap
- pabs3 6y agoHmm, since the LLVM/Clang/Zig files in that repo are unmodified, why not just move the zig-bootstrap build script to a bootstrap script in the zig repository?
- AndyKelley 6y agowhat purpose would that serve?
- pabs3 6y agoSeems simpler to have one repo for building zig no matter whether you want a bootstrap build or a from-zig build.
- wycy 6y agoIs a patched in-place binary identical to the binary that would be generated from a clean compilation?
- kevsim 6y agoDoesn't sound like it. Seems like it appends the new versions of the functions and updates the global offset table.
- alexnask 6y agoNot quite, the linkers (at least ELF and PE which I am familiar with) will "free" parts of the file (when a block needs to be reallocated because the decl code gets too big, or when a decl is removed) and reuse them when they can. But in general, the final executable will not be the same, this is correct.
- kevsim 6y agoThanks for clarifying!
- alexnask 6y agoNo, some parts of the binary file will be unused in the final executable if you keep updating it incrementally. The linkers will do what they can to reuse all previously freed blocks of the file, but you can imagine a scenario where a function is updated but does not fit in the block that was previously allocated for it. In this case, a new block of the file will be allocated for this function. If you then add a smaller function, it will occupy some portion of the old block. You can keep adding things into this block until some minimum amount of memory is reached (and as long as the declarations fit in there, obviously). Repeating this process for all new declarations and for all the modifications of declarations, you can imagine some parts of the files will remain unused. I hope this makes sense :)
- wyldfire 6y agoIn the video, Kelley refers to Tracy [1]. I hadn't heard of it before but it looks interesting. [1] https://github.com/wolfpld/tracy https://github.com/wolfpld/tracy
- AndyKelley 6y agoI love Tracy, highly recommend it, and it's another great project worth sponsoring: https://github.com/sponsors/wolfpld https://github.com/sponsors/wolfpld
- schaefer 6y agoWow, that profiler that he’s using looks slick! Definitely going to check it out!
- wyldfire 6y agoIt's a very cool feature. But I wonder -- indirection via GOT sounds expensive. Could I turn it off for some critical/inner loop code?
- wtetzner 6y agoIt sounds like this is only intended for debug builds.
- agentultra 6y agoI started messing around with zig in earnest https://github.com/agentultra/zig8 https://github.com/agentultra/zig8 and I have to say that the development experience is great so far. The compile times are fast, the tooling is minimal in the unix sense, and it's been nice to play around with. Highlights: - compile-time evaluation - integrated testing through scoped blocks - prevents some implicit casts that we take for granted in C - catches allocation errors - explicit error return handling I appreciate it trying to make a better C.
- ifreund 6y agoCompile times of the stage1 zig compiler may seem fast compared to e.g. rust or C++ but they are slow compared to what the WIP stage2 compiler can do :)
- agentultra 6y agoI can't complain as I mostly write Haskell these days and trade off compile time for other things. That sounds excellent though, I'll check it out when it lands.
- dasb 6y agoI want to like Zig, but I just hate the {var | const } identifier : type syntax. I know it's an extremely superficial observation, but it just doesn't feel good to me.
- degurechaff 6y agomany new language put type in last part of declaration. rust, go, typescript, and other
- samatman 6y agoEverything old is new again! The style dates back to Pascal. I think it's better, personally, and that might be because Pascal was my first programming language, but I don't care, I'm just happy it's back in fashion. As a side note, I was quite sure that it dated back to Algol 60, but looked it up for this comment, and nope! Type came before variable.
- AndyKelley 6y agoenjoy ;) https://www.youtube.com/watch?v=880uR25pP5U https://www.youtube.com/watch?v=880uR25pP5U
- higerordermap 6y agoI am a programmer and I make very scientific decisions. I don't like this language because syntax is not like C.
- adenozine 6y agoWhen zig can handle line endings from this century, maybe. It's kinda crazy there's software that bothers the user to restrict CRLF. I tried zig and this was the first issue. I thought I needed to go find my AOL discs or something.
- biryani_chicken 6y agoWhat do you mean? I've used zig on both linux and windows and never had line ending issues.
- adenozine 6y ago``` .\zig.exe build-exe .\hello.zig .\hello.zig:1:28: error: invalid carriage return, only '\n' line endings are supported ``` I just re-downloaded the 0.6 branch, and I get this error on latest Windows. It's just one click in VS Code, but then I have to think about this every time I go between different files I might be working on. It's NUTS to me that the compiler doesn't just regex them out or something, I can't justify using a technology that won't take even the smallest step for UX. Imagine if you needed to set some character set or line-ending to bang out a python script, that'd be crazy, right? So, why should anybody bother doing it for Zig? I want to like Zig, but I'm just not motivated to with an experience like this. I've been doing Python for a while, and I got my eyes on Nim for the next generation of languages.
- jhoechtl 6y agoZig is the legit successor to C++ , not Rust. In a sense that it will feed your family and get you a job in the future. Sure, Rust is even smarter and a savy Rust hacker is likely to run circles around the herds of mediocre Zig hackers. But Zig hackers will come in legions while Rust hackers will die in beauty.
- 29athrowaway 6y agoI really need to try Zig and Nim. Are these good for server development?
- fluffything 6y agoWhy is this faster than dynamic linking ?