9 ms·
How to learn compilers: LLVM Edition
- andrewchambers 5y agoMore great things: - https://c9x.me/compile/ https://c9x.me/compile/ - https://github.com/vnmakarov/mir https://github.com/vnmakarov/mir
- gavinray 5y agoThank you for posting these. I had seen and read about both of these before, and had been blown away by both of them -- but since had forgotten they existed! (For MIR overview, I recommend reading the Redhat blogposts mentioned in README rather than the README itself) If I implement a language for learning purposes, I will implement it first in QBE and then MIR and then LLVM, and publish a retrospective analysis + comparison + thoughts. I think that would make really interesting reading!
- chrisaycock 5y agoI learned a lot about LLVM by looking at the compiler output from Clang: clang -emit-llvm -S sample.cpp The article mentions Clang's AST, which can also be emitted: clang -Xclang -ast-dump -fsyntax-only sample.cpp And for checking compiler outputs across lots of languages and implementations, there's always Matt Godbolt's Compiler Explorer: https://godbolt.org
- tester34 5y agosince here's many compiler hackers then I'd want to ask question: How do you distribute your frontend with LLVM? Let's say that I have lexer, parser and emitter written in e.g Haskell (random example) I emit LLVM IR and then I use LLVM to generate something other but the problem is, that I need to have LLVM binaries and I'd rather avoid telling people that want to contribute to my OSS project to install LLVM because it's painful process as hell So I thought about just adding "binaries" folder to my repo and put executables there, but the problem is that they're huge as hell! and also when you're on linux, then you don't need windows' binaries Another problem is that LLVM installer doesnt include all LLVM components that I need (llc and wasm-ld), so I gotta compile it and tell cmake (iirc) to generate those I thought about creating 2nd repo where there'd be all binaries compiles for all platforms: mac, linux, windows and after cloning my_repo1, then instruction would point to download specific binaries How you people do it?
- HowardStark 5y agoNot a compiler hacker and unfamiliar with the scene but is there a specific reason that `git-lfs` wouldn’t work? It’s the first thing that came to mind reading this. You can also pretty easily fetch specific objects as opposed to everything, so in your README you could direct contributors to only fetch specific binaries for given tasks.
- xrisk 5y agoYou can statically link LLVM, no problem. In fact, you never have to call any binaries specifically; just do it through code and everything should link at compile-time and become one big binary.
- 10000truths 5y agoThis is in fact what Zig does. Everything is statically linked into one binary that is used for compiling, linking, building, testing etc.
- jcranmer 5y agoWhen you have an LLVM frontend, what you generally do is have your driver run the optimization and code generation steps itself using the LLVM APIs rather than using opt/llc binaries to drive this step. That way, you don't need the LLVM binaries, just the libraries that you statically link into your executable. For example, all of the code in clang to do this is located in https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/BackendUtil.cpp https://github.com/llvm/llvm-project/blob/main/clang/lib/Cod...
- anonymousDan 5y agoI have to say personally I find general program analysis (e.g. for security) a much more interesting topic than most vanilla compiler courses. For example I recently came across this course by the maintainers of soot: https://youtube.com/playlist?list=PLamk8lFsMyPXrUIQm5naAQ08aK2ctv6gE https://youtube.com/playlist?list=PLamk8lFsMyPXrUIQm5naAQ08a... Any pointers to similar courses much appreciated!
- the_benno 5y agoAnders Moeller and Michael Schwartzbach's book [1] on static program analysis is a fantastic resource, with (I think) a great balance of theory and practice. If you want to get really deep into the theory of program analysis, Patrick Cousot just published an incredibly thorough book on abstract interpretation (just got my copy this week, so haven't fully explored enough to have much of an opinion on it as a pedagogical resource) [1] cs.au.dk/~amoeller/spa
- anonymousDan 5y agoThanks, yes I've read the Moeller book and I agree with your evaluation. I wasn't aware of the Cousot book though, I'll be sure to check it out.
- igotsideas 5y agoThank you for YouTube link. I would of never found this and it looks like gold.
- matt_d 5y agoMore program analysis & LLVM resources (books, courses, and talks): https://gist.github.com/MattPD/00573ee14bf85ccac6bed3c0678ddbef https://gist.github.com/MattPD/00573ee14bf85ccac6bed3c0678dd...
- CalChris 5y ago1. Getting Started with LLVM Core Libraries It's a bit dated (covers DAGISel rather than GlobalISel) but it gives a thorough introduction. 2. LLVM Developer Meeting tutorials These are really good although you'll have to put them in order yourself. They will be out of date, a little. LLVM is a moving target. Also, you don't have to go through every tutorial. For example, MLIR is not for me. 3. LLVM documentation I spent less time reading this than going through the Developer Meeting tutorials. I generally use it as a reference. 4. Discord, LLVM email list, git blame, LLVM Weekly ... because you will have questions. 5. MyFirstTypoFix (in the docs) ... when it comes time to submit a patch. 6. Mips backend If you're doing a backend, you will need a place to start. The LLVM documentation points you to the horribly out of date SPARC backend. Don't even touch that. AArch64 and x86 are very full featured and thus very complex (100 kloc+). Don't use those either. RISC-V is ok but concerns itself mostly with supporting new RISC-V features rather than keeping up to date with LLVM compiler services. Don't use that either although definitely work through Alex Bradbury's RISC-V backend tutorials. Read the Mips backend. It is actively maintained. It has good GlobalISel support almost on par with the flagship AArch64 and x86 backends. BTW, Chris Lattner is a super nice guy.
- jcranmer 5y ago> 4. Discord, LLVM email list, git blame and don't forget IRC!
- UncleOxidant 5y ago> LLVM Developer Meeting tutorials Are these all in one place or scattered about?
- CalChris 5y agoEither llvm.org under Developer Meetings or the LLVM Youtube channel. The advantage of llvm.org is that it has a lot of the PDFs for the presentations as well as some old, pre-Youtube tutorials.
- deleted 5y ago[deleted]
- Lamad123 5y agoYou could've mentioned Crafting Interpreters. It's an excellent book!!! (At least for the rest of us, mere mortals!)
- dlsa 5y agoI'd second this. Been a good read. Learnt a lot. Now I have a DSL for some really strange usecases and its been a blast. Never thought having an interpreter of my own would be so much fun.
- Lamad123 5y agoCool! I just started reading it about a week ago, and almost having that feeling I had when I first learned how to program.