6 ms·
Am I the only one who wants to see a split into a "fast compile" mode and a "spend hours making every optimization possible" mode? Most code is executed a lot
by londons_explore 7y ago
Am I the only one who wants to see a split into a "fast compile" mode and a "spend hours making every optimization possible" mode?
Most code is executed a lot more frequently than it is compiled, so if I can get a 1% speed increase with a 100x compile slowdown, I'll take it.
I don't want to see good PR's that improve LLVM delayed simply because they cause a speed regression.
- worldsayshi 7y agoYeah this seems like two very different use cases. Stating the obvious: when debugging I want as fast builds as possible. When shipping I want as fast software as possible.
- bflesch 7y agoThe only problem being if you want to debug the optimized code ;)
- khalladay 7y agoThis is true a lot in games - often the "debug" version of a game is literally too slow to reproduce issues, so you have to work with the optimized builds. ...even if the actual debugging would be much easier with a debug build.
- bluGill 7y agoUnderstood. However even then I would hope that games have unit tests and other module tests that can run just a subset of code and not the entire game. When I'm writing code I want to know it builds and the basics work before I run the entire program. I work on seasonal programs. I can only do fully realistic tests in the month of April (depending on the weather it shifts a few weeks) , meaning my window to test is closed for the next 11 months. I have learned to find ways to run my code in less realistic situations.
- steerablesafe 7y agoThere are also those bugs that only manifest in the release build.
- bluGill 7y agoThat happens, but fortunately not often. Most problems show up in either optimized or unoptimized code. If it is in optimized code only it is a compiler bug (rare but I have seen it) or a race condition that won't show up in the debugger either. (there are a few more choices but the above are the big ones in my experience)
- compiler-guy 7y agoIn optimized code, it is far more likely that one has a programming error in one's source code that an aggressive optimization revealed, rather than a bug in an industrial compiler. For example, an uninitialized variable that ends up sharing a memory location with some other variable whose life-time doesn't overlap in optimized mode rather than having its own memory location without optimization. That one is caught be a warning, but there are thousands of possible errors that can be exposed this way. Compilers have bugs all the time, but code in a write-test-debug-cycle typically has many more.
- ludamad 7y agoWhich is why equally aggressive debug checks are important
- deleted 7y ago[deleted]
- egwor 7y agoThis should be an optimisation that you choose to enable, though, right? It might be prohibitively expensive to run the compile for other reasons (time to do a deploy for a fix, time and cost to run builds for CI etc.) so it has to be something that can be dialled up/down.
- simfoo 7y ago-O3 should include those expensive optimizations. If you want fast builds with minimal optimizations, go with -O1
- 1980phipsi 7y agoDoes clang have -O0?
- GuB-42 7y agoYes (clang11 documentation) -O0, -O1, -O2, -O3, -Ofast, -Os, -Oz, -Og, -O, -O4 Specify which optimization level to use: -O0 Means “no optimization”: this level compiles the fastest and generates the most debuggable code. -O1 Somewhere between -O0 and -O2. -O2 Moderate level of optimization which enables most optimizations. -O3 Like -O2, except that it enables optimizations that take longer to perform or that may generate larger code (in an attempt to make the program run faster). -Ofast Enables all the optimizations from -O3 along with other aggressive optimizations that may violate strict compliance with language standards. -Os Like -O2 with extra optimizations to reduce code size. -Oz Like -Os (and thus -O2), but reduces code size further. -Og Like -O1. In future versions, this option might disable different optimizations in order to improve debuggability. -O Equivalent to -O2. -O4 and higher Currently equivalent to -O3
- embrassingstuff 7y agoHow many people would.use a cloud compiler? Let's set aside thecnicalities and assume it a real X5 improvements and all the files are mirrored seamlessly.
- mhh__ 7y agoKen Thompson would be rolling in his grave if not for him being alive. I could see this having some use in an internal manner for things like game development where you have absolutely enormous codebases (as an abstraction above compile servers and similar).
- londons_explore 7y agoMBED already pioneered the cloud compiler. Their compiler did optimizations to produce (slightly) better code than any compiler you could buy. A lot of users were uneasy with it.
- compressedgas 7y agoMany already use one called distcc.
- boomlinde 7y agoIt's only at link time that object will depend on another, so you don't need a new compiler to support a "cloud" use case where any number of machines can work on compilation.
- cwzwarich 7y agoA lot of large C++ projects use link-time optimization, since it's the easiest way to get predictably good performance.
- mrich 7y agoIf you are referring to a caching distributed compile cluster, most companies with large codebases are using one (or should). It helps a lot and can make the difference between taking half a day for a scratch make (i.e. unusable) to getting down to 10 min. There are open-source ones but I'm also aware of at least two internal, custom-developed ones.
- bjoli 7y agoGuile scheme is getting a "baseline- compiler for that reason: it will compile fast, but not do any of the fancy CPS things that the regular compiler does.
- fnord123 7y agoFor fast compiles, in clang, -fsyntax-only parses as quick as possible. Then you have -O{0,1,2,3s} for various levels of optimization. As for the long baking compile, you have a concept of super compilation which will check the branches and determine if there are any places where a function is called with constants and partially evaluate (similar to partial application / prameter binding, but you get a newly compiled function) the function with those constants frozen. But then it has to determine if the branch elision makes it worth dropping the other versions of the function. It's a cool topic that I researched a lot about a decade ago but I think it's not an area with a lot of active interest in AOT compilers. https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.50.8214 https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.50.... http://wiki.c2.com/?SuperCompiler http://wiki.c2.com/?SuperCompiler https://en.wikipedia.org/wiki/Stalin_(Scheme_implementation) https://en.wikipedia.org/wiki/Stalin_(Scheme_implementation)
- pfdietz 7y ago> Most code is executed a lot more frequently than it is compiled, so if I can get a 1% speed increase with a 100x compile slowdown, I'll take it. Is that really true? I'd have thought most code outside inner loops benefits almost negligibly from optimization.
- jrochkind1 7y agoIt may benefit negligibly from optimization -- or rather, the overall program may benefit negligibly from optimizing that code site -- but it's still gotta be executed a lot more frequently than it is compiled right? Unless it has only one or two users, who both compile it all the time, or something.
- bluGill 7y agoI don't have time to figure out the correct tradeoff for optimization for each tiny code section of code. Just let the compiler do everything and it might be too much but it won't be too little. Besides, little things add up even outside of tight loops. Slow code in rarely used areas is still noticeable when that rare thing happens.
- pfdietz 7y agoSo, you don't have time to profile to see where the hotspots are, but you care about how fast your program is? Mkay.
- jeffbee 7y agoThat really depends on your program. Many servers have sprawling, flat profiles. Most workloads are not computational fluid dynamics. A perspective on hot/cold code ratio is found in "AsmDB: Understanding and Mitigating Front-End Stalls in Warehouse-Scale Computers" https://storage.googleapis.com/pub-tools-public-publication-data/pdf/e52f61fd2c51e8962305120548581efacbc06ffc.pdf https://storage.googleapis.com/pub-tools-public-publication-...
- sesuximo 7y agoGood question. It's worth asking whether optimizations significantly help "most" code. There are some optimizations that make a big difference everywhere: - consolidating pure function calls. This can save arbitrary amounts of time. Classic example is strlen being called many times. I know you're not asking about inner loops, but this can change the complexity of loops which is a big big win. - mem2reg. Kind of self explanatory. Registers are fast but we write C and C++ using addressable objects. Most compilers make a decent effort here even with optimizations turned off. - global variable numbering. This allows loads and stores to be removed/moved. Often prevents cache misses or puts them off until the end of a function when it doesn't block execution. - strength reduction. Turning your divisions into shifts. Turning your && into &. Etc. It is not unusual for these peephole changes to save 10s of cycles per instruction replaced. These are also really fast optimizations (mem2reg can be extremely slow if "optimal", but the heuristic versions that everyone uses are quick). If you know you won't care, you can mark the function as cold. That said, the compiler might ignore you and decide you cannot possibly really want to disable something like mem2reg.
- wallnuss 7y agoIt all depends on which optimizations you enable and LLVM is very flexible, albeit sometimes you still spend 20% of your time in ISel (Instruction Selection)...
- fluffything 7y agoYou can already spend as much as you'd like on optimizations if you are using LLVM. Just use a superoptimizer [0]: clang -Xclang -load -Xclang libsouperPass.so -mllvm -z3-path=/usr/bin/z3 , or increase the inlining heuristics..., or just create your own optimization strategy like rustc does [1], or... LLVM is super configurable, so you can make it do whatever you want. Clang defaults are tuned for the optimizations that give you the most bang for the time you put in, while still being able to compile a Web browser like Chrome or Firefox, or a whole Linux distribution packages, in a reasonable amount of time. If you don't care about how long compile-times take, then you are not a "target" clang user, but you can just pass clang extra arguments like those mentioned above, or even fork it to add your own -Oeternity option that takes in your project and tries to compile it for a millenia on a super computer for that little extra 0.00001% reduction in code size, at best. Because often, code compiled with -O3 is slower than code compiled with -O2. Because "more optimizations" do not necessarily mean "faster code" like you seem tobe suggesting. [0]: https://github.com/google/souper https://github.com/google/souper [1]: https://github.com/rust-lang/rust/blob/master/src/rustllvm/PassWrapper.cpp#L713 https://github.com/rust-lang/rust/blob/master/src/rustllvm/P...
- CalChris 7y agoHas anyone ever done a Pareto frontier analysis on this? Specifically, this would be for speed rather than size.
- fluffything 7y agoWhat would you expect to learn and how would you do this? I can't imagine anything useful coming out of it.
- noch 7y ago> Because often, code compiled with -O3 is slower than code compiled with -O2. Because "more optimizations" do not necessarily mean "faster code" like you seem tobe suggesting. Indeed, due to patterns of resource use and availability (e.g. the memory wall[0]), compiler optimisations can only solve 1-10% of performance problems a piece of code might have. To paraphrase Mike Acton: The vast majority of your code consists of things the compiler can't reason about.[1] [0]: https://en.m.wikipedia.org/wiki/Random-access_memory#Memory_wall https://en.m.wikipedia.org/wiki/Random-access_memory#Memory_... [1]: [Mike Acton, "How to Write Code the Compiler Can Actually Optimize", GDC2015.](https://m.youtube.com/watch?feature=youtu.be&v=x61H6qEtK08&t=20m https://m.youtube.com/watch?feature=youtu.be&v=x61H6qEtK08&t...)