3 ms·
GCC code generation for C++ Weekly Ep 43 example
- harpocrates 10y agoIs there no way to indicate to GCC when you want something fully evaluated at compile-time? Maybe some pragmas? 98% of the time I probably don't care whether GCC does a good job optimizing this properly, but for the times I do care: what should I do?
- lultimouomo 10y agoc++ has constexpr for this. a constexpr expression will be evaluated at compile time if the result is used in a constant expression (i.e. assignd to an enum constant or a constexpr variable), or an error will be generated if it is not possible to do so (the expression does not fulfill the requirements to be constexpr).
- matt_d 10y agoA `constexpr` function storing result in a `constexpr` variable (you need both for the compile-time evaluation guarantee): http://en.cppreference.com/w/cpp/language/constexpr http://en.cppreference.com/w/cpp/language/constexpr An extra complication in this example is that `std::accumulate` currently lacks `constexpr` support: http://stackoverflow.com/questions/32395408/why-arent-stdalgorithms-constexpr-and-which-could-be http://stackoverflow.com/questions/32395408/why-arent-stdalg... There's been a proposal to relax this limitation: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0202r0.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p020... For now you could use a workaround in form of a range-based for loop: https://godbolt.org/g/aVEqlm https://godbolt.org/g/aVEqlm or even https://godbolt.org/g/GDrFr5 https://godbolt.org/g/GDrFr5 (homogeneous types). A fold expression is much cleaner, though (also supporting heterogeneous types): https://godbolt.org/g/Hrnkc1 https://godbolt.org/g/Hrnkc1
- andrepd 10y agoUse constexpr: http://en.cppreference.com/w/cpp/language/constexpr http://en.cppreference.com/w/cpp/language/constexpr You can't make sum constexpr in this case, though, since it calls a non-constexpr function, accumulate.
- krona 10y agoAfter a quick investigation, clang (3.9, x86-64) doesn't seem to suffer from the same problem: https://godbolt.org/g/Uvo7iO https://godbolt.org/g/Uvo7iO
- andreyv 10y agoWell, try to significantly increase the argument count in your example.
- tcbawo 10y agoCalling with 46 arguments seems to move this onto the stack: https://godbolt.org/g/tbKtGz https://godbolt.org/g/tbKtGz
- rurban 10y agoNo. It hits the constant folding threshold and calls the sum function with the args.
- DannyBee 10y agoUnlike GCC, clang will iterate gvn (what GCC calls fre, because in GCC, gvn is properly split into an analysis, and fre uses it to do full redundancy elimination) until it stops changing, and does so fairly early in the pipeline I wrote fre for GCC (and working on llvm's next gvn) and long story short, GCC is more powerful, but it doesn't always run early enough, and is not iterated. The general problem of detecting all herbrand equivalences is exponential time, even without trying to evaluate the code. So you can't win all the time anyway.
- haberman 10y agoThis surprises me. This makes it sound like inlining and vectorizztion are performed independently of constant folding. It seems like the compiler should be able to recognize when a function depends only on its inputs. Then constant folding should be able to inline / evaluate any code that can be compile-time evaluated, no matter how much code that is. It doesn't seem like this should be left up to chance.
- DannyBee 10y agoIt's likely just a typical pass ordering problem
- TwoBit 10y agoVC++ 2015 (the latest released version as of this writing) can't optimize this for any number of arguments, even just one.
- DannyBee 10y agoVC has always been good at code gen but not middle optimizations. Though they are working on it, this kind of thing takes years to build and tune well
- winter_blue 10y ago> VC has always been good at code gen Well, target code gen is the job of a compiler. Optimization is a plus. It's like saying VC is good at compiling, but not optimizing.
- DannyBee 10y agoi'm really not sure what point you are trying to make. Yes, VC is good at compiling but not optimizing. Given people expect both from their compilers these days ...