6 ms·
Understanding GCC Builtins to Develop Better Tools [pdf]
- teddyh 7y agoNot a builtin, but a GCC feature which has improved my C code hugely is __attribute__((cleanup)). It is used to specify a destructor for a local variable; when the variable goes out of scope the function is called with a pointer to the variable as an argument. Example: { __attribute__((cleanup(foo))) int a; … /* foo(&a) will implicitly be called here */ }
- std_throwaway 7y agoIt's always funny when C people rediscover parts of C++. Yet they can never admit to it.
- teddyh 7y agoI write in C only when I need to; in my case I have a limited runtime environment available, so I can't use some other language (which I might otherwise prefer) with a larger runtime requirement, and I also already know C, but not C++. Also, none of the libraries I have to use have C++ APIs. Had I already known C++, and if there were C++ APIs available for the libraries I need, then sure, I might have used C++, but as it is, the results (a C++ program but full of C-style programming because of the APIs) would not be worth the effort of learning C++. C++ also imposes a cost; that of reducing possible contributors to those people who already know C++ – knowledge of C is simply more common than C++.
- comex 7y agoYour other points are valid, but you can use C++ without any more of a runtime than C needs. The main feature that needs runtime support is exceptions, but it’s common enough to use C++ with exceptions disabled even outside embedded. There’s also global constructors, if you’re not already using them from C with GNU extensions; those can be disabled too if needed, but it’s easy enough to make sure the constructors get invoked (most crt0s should do this out of the box).
- teddyh 7y agoYes, the limited runtime is what is prohibiting me from using, say, Python. Runtime-wise, C++ would be fine. It’s the other reasons I listed which makes me not use C++ in this case.
- pietroglyph 7y agoIs C++ -> C interop your only reason for avoiding C++? If it is then you might reconsider C++, because calling C code from C++ is usually as easy as wrapping the #include in an extern "C" block.
- teddyh 7y agoI know it’s easy to call C functions from C++. That’s not the issue. The issue is that since all the API functions are in C, the code of my program will mold itself around those APIs, and my program will therefore have a C “shape”, so to speak. And the small pieces that remain C++-shaped might not be large enough or provide enough architectural benefit for the drawbacks of C++ to be worth it.
- jart 7y agoLearning C++ is easy. What's hard is being able to afford a computer that can compile it in a reasonable amount of time. I've seen projects written in C / Assembly that build over 9,000 objects in about 20 seconds on just one PC, whereas more than a few C++ codebases I've seen in the past couple years, that can be about as long as it takes to build just one file. I honestly suspect the only folks really writing C++ consistently these days, on things like LLVM, are the ones who work at huge companies, with literal supercomputers devoted to compiling their code, which would prevent them from ever encountering the latency issues that exclude poor folk such as myself from any hope of ever hacking on their codebase. It's sort of like that scene of Agent Smith talking to Neo in the interrogation room: "What good is the source code... if you're unable to build?"
- kvakil 7y agoMaybe take a look at gg [0]? It seems to solve the problem of slow compilation quite nicely. [0] https://github.com/StanfordSNR/gg https://github.com/StanfordSNR/gg
- TazeTSchnitzel 7y agoWhy should I have to outsource compilation to Someone Else's Computer?
- pjmlp 7y agoVisual C++ with pre-compiled headers, incremental compilation and linking turned on, does a pretty good job. https://devblogs.microsoft.com/cppblog/improved-linker-fundamentals-in-visual-studio-2019/ https://devblogs.microsoft.com/cppblog/improved-linker-funda... Another tip, use binary libraries and don't build everything from scratch. Modularize the application into their own libraries as well. Speaking of modules, they are part of C++20, with VC++ and clang adapting their implementations, gcc ramping up its implementation, and everyone else that cares about ISO C++20 also ramping up theirs. Fun fact, Swift and Rust codebases aren't that fast to compile when compared against C++ code that isn't doing any crazy template metaprogramming.
- shaklee3 7y ago
- std_throwaway 7y agoC++ is perfectly able to interface and interact with C libraries. Also it doesn't necessarily produce larger or slower binaries. Virtually everything is optional. The only hard part about C++ is to identify the good parts (those that are useful for your project) and to not use the other parts. Then you can do "C with templates" or "C with simple classes that have proper RAII" but you don't need exceptions. The argument that I keep hearing is kind of the "slippery slope" that if one allows a little bit of C++ the rest will come creeping and with it will come all complexity. This shows that project management is weak and tries to deal with this weakness by selecting tools with limited capability. A method that seems to work reasonably well in cases like the Linux kernel.
- hyperman1 7y agoUnfortunately, for any human activity at scale, project management is weak. You can get good quality with small teams, but when more than 100 people are together, you can be sure that someone is going to commit every athrocity they can get away with. And then major damage happens, even if all the other people do aperfect job. See Boeing recently. See chernobyl. See any major disaster. So having tooling that limit your ability to do damage, even at the cost of pain for power users, is one of the few ways to keep humanity in check.
- teddyh 7y agoUnfortunately, in order to pick the good parts of C++ which are appropriate for your project, you first have to know all of C++.
- therein 7y agoDidn't know this existed. The closest thing I can think of similar to this is `defer` of Golang.
- jart 7y agoIt'd be great if the GCC devs adopted a process for introducing new builtins, that required code review, unit testing, and justification of use-cases. Some of the GCC builtin wrappers are outstanding, e.g. __builtin_memcpy on two-power sizes, since it can do things that wouldn't be possible otherwise. Some GCC builtins, e.g. __builtin_va_start, can take away flexibility since it locks into the core codebase nontrivial code generation that's suboptimal in certain cases. Most of the builtins are neutral, e.g. atomics api, which provides 42 different ways to say asm("mov"), 13 different ways to say asm("xchg"), and twenty different ways to asm("lock cmpxchg"). Other GCC builtins, e.g. __builtin_ia32_rdrand32_step, don't even have the multi-architecture benefit, only seem to exist because some folks disagree with the asm() keyword, and is naturally problematic from a security standpoint when the GCC devs get it wrong. What's great about builtins like asm() is it's one simple general-purpose abstraction that puts the power of so many compiler internals into the hands of the user at once, rather than being another narrowly-focused case-by-case feature you need to beseech the core dev team to get.
- deleted 7y ago[deleted]
- earenndil 7y agoThe main problem with assembly, afaik, is that gcc doesn't know how to optimize even very simple assembly.
- canarypilot 7y agoGCC treats inline assembly as an opaque blob of text It knows nothing about, plus some register constraints it knows something about. It will “optimise” assembly if you’ve passed the right registers as input/output, and haven’t marked it volatile and memory clobbering. Simple things like eliminating it if it is dead code, but this probably isn’t the optimisation you were looking for! Depending on who you ask, this is a feature, not a defect. Inline assembly is your last line of defence against a compiler which thinks it knows better than you. Clang/llvm go a step further and do parse and optimize your inline assembly; that makes me uncomfortable, but I’ve always subscribed to the “don’t touch user asm” mindset for compiler development. Your main benefit of builtins over assembly is that no matter how hard I tried I couldn’t get the suggested replacement given by the parent for __atomic_compare_exchange to assemble on my arm64 server!
- TazeTSchnitzel 7y agoIt surprises me more of these haven't become standardised features. __builtin_expect is very common (but as a compiler hint I somewhat understand its absence), __builtin_clz is arguably a missing feature.
- jart 7y agoI suspect (but haven't verified) that modern compilers are actually implementing __builtin_expect() as a no-op. Much better alternatives exist today, e.g. GCC 8+ __cold__ and profile guided optimizations, which don't have a negative impact on code readability.