Slashdot Mirror


Developing In C/C++? Why You Should Consider Clang Over GCC (dice.com)

Nerval's Lobster writes: The idea with Clang, a compiler front-end for C, C++, Objective-C++, and Objective-C, and LLVM (a compiler infrastructure) is that you can mix the compiler front-end with a targeted back-end and end up with highly portable and efficient compiler. Clang can perform static analysis of your code, and lets you write tools that give you information about a program. Although many developers prefer developing in C/C++ using GCC, developer David Bolton (in a new Dice article) makes an argument for why you should switch to Clang. While GCC is probably still best when it comes to speed, he argues, Clang is improving release by release, and features tools that developers could find useful.

152 of 255 comments (clear)

  1. Translation by RightwingNutjob · · Score: 2, Insightful

    While GCC is probably still best when it comes to speed, he argues, Clang is improving release by release, and features tools that developers could find useful

    Translation: "network tran--I mean speed is a feature that most of our users don't need, so it's not in our development plan"

    1. Re:Translation by ShanghaiBill · · Score: 5, Informative

      Translation: "network tran--I mean speed is a feature that most of our users don't need, so it's not in our development plan"

      No. Clang produces faster code. What TFA means is that GCC compiles faster. But if slightly faster compiles are that important, just turn off the optimizer, or buy a faster computer. How much time does a modern developer spend waiting for the compiler to finish? For me, it is less than 1%. Far more important is the better error messages, warnings, and static analysis from Clang. Those save me way more time than the speed of the compiler.

    2. Re:Translation by Anonymous Coward · · Score: 1, Funny

      Is this sarcasm? Please tell me this is sarcasm and you intentionally got it backwards.

    3. Re:Translation by Shados · · Score: 1

      How much time does a modern developer spend waiting for the compiler to finish

      In C/C++? All the freagin time. Large applications can take -forever- to compile, even if its not a clean compile.

    4. Re:Translation by Anonymous Coward · · Score: 1

      > No. Clang produces faster code [phoronix.com].

      Engineering optimization versus Technology breakthrough.

      > What TFA means is that GCC compiles faster.

      I believe OP is being sarcastic. "Our users" means the developers for whom compiling speeds are of utmost importance ("premature optimization" etc.) So, faster code is not the main issue here.

      But being paranoid, I'd like to ask... are we safe with Clang regarding those "special compilations" of which we've been warned by Ken Thompson? (search "Trusting Trust")

    5. Re:Translation by ShanghaiBill · · Score: 4, Informative

      Large applications can take -forever- to compile, even if its not a clean compile.

      Many complaints about slow compiles are due to dysfunctional workflow. You should be using precompiled headers, and set up your makefiles for multiple cores. But most importantly, you should be using TDD to develop and debug code outside of the application. So instead of compiling and linking the entire application repeatedly as you track down a bug, you only integrate after all your unit tests are written and passed in isolation.

      But, anyway, it isn't clear that Gcc actually compiles faster than Clang. Here are some benchmarks, and the results are mixed. Sometimes Clang is faster, sometimes Gcc is faster. For a large app, they would likely be about even in compile time. But Clang would likely generate better code, and almost certainly generate more helpful errors/warnings/analysis.

      I can't see any rational reason to prefer Gcc.

    6. Re:Translation by HiThere · · Score: 2

      If you're dubious about "special compilation", then use GCC to compile clang. (It's not sufficient, but using a different compiler to do the compilation of the compiler is a necessary step.)

      --

      I think we've pushed this "anyone can grow up to be president" thing too far.
    7. Re:Translation by Anonymous Coward · · Score: 1

      When compiling a finished product (for actual use) I care about run time. But when I'm actually doing development compile time is infinitely more important, particularly if the code base is more than moderately large. Compilation of large C++ projects takes a freaking eternity

      You have a serious problem with how your solution builds then. Hell, we had this problem licked way back with "MAKE" on Unix, where you only had to build the parts you touched and unless you modified a central header file it would figure out the minimum amount of stuff you needed to build, and link. Usually the longest part of the build was the link, and when you got tired of that, we'd start moving things off into shared libraries so you didn't have to write the whole executable statically linked. Then adding the -j x (where x was # of CPU's you had times 2) to the command line on MAKE would usually move things along faster.

      Now days, if you are wasting much time building your project... I'd consider the problem to be how you choose to build stuff, something is wrong there....

    8. Re:Translation by epyT-R · · Score: 2

      No. Sometimes Clang produces faster code. Read the rest of that review.

    9. Re:Translation by Anonymous Coward · · Score: 1

      Some people work on projects larger than Flappy Bird.

    10. Re:Translation by Anonymous Coward · · Score: 1

      Precompiled headers and multicore compilation aren't always enough. I've worked on projects that were so big that the compiler would actually run out of heap space trying to compile them.

    11. Re: Translation by Anonymous Coward · · Score: 5, Funny

      And what kind of garbage programmers are you that need to compile the same code multiple times? Write the code correctly the first time and only compile once.

    12. Re:Translation by mikael · · Score: 2

      I've seen cross-compile builds of large code bases of 2 million+ lines take 10 minutes. Change one header file of a low-level module, and the whole stack has to be rebuilt. Just because BaseClassA has had a new boolean flag added, DerivedClassB to DerivedClassJ have to be recompiled along with container classes, interface classes, then the GUI layer with custom widgets, the plugins for third-party editors, the dynamic linked libraries, and finally the application itself.

      --
      Vintage computer adverts: http://www.vintageadbrowser.com/computers-and-software-ads
    13. Re:Translation by dev54335 · · Score: 1

      > Compile time can be greatly reduced by careful header file usage. Instructions can be found in the latest version of Effective C++ by Scott Meyers. When you work on a 10-20 year old enterprise code base this assumption is false. And its not easy to refactor, due to the cost of testing. So faster compiler is a must for faster development, g++/gcc just wins.

    14. Re:Translation by jcr · · Score: 1

      If you want faster compiles, put your sources and your build folder on a RAM disk. For me, it shortens five minute builds to 30 seconds or less.

      -jcr

      --
      The only title of honor that a tyrant can grant is "Enemy of the State."
    15. Re:Translation by davester666 · · Score: 1

      Time to break that puppy up into two '.cp' files.

      --
      Sleep your way to a whiter smile...date a dentist!
    16. Re:Translation by Yokaze · · Score: 3

      If you are running into that issue often, consider the Pimpl-pattern.

      As always, C++ doesn't take the decision away from you. If you cannot live with the overhead in runtime from one pointer indirection, you have to live with the other downside.

      From my standpoint and experience, most compile time issues were due to people putting things into the headers out of convenience.
      In order to save some forward declarations or pimpl implementations, things were put into the header, which caused constant recompiles,and long compile times.

      --
      "Between strong and weak, between rich and poor [...], it is freedom which oppresses and the law which sets free"
    17. Re: Translation by kthreadd · · Score: 2

      GCC runs native on Windows. It's maintained by the mingw64 project and works really well.

    18. Re:Translation by gargleblast · · Score: 1

      ... sources ... on a RAM disk.

      Whenever anybody loses a fair chunk of a day's work due to a crash / power failure / other dumbass mistake, I always advise them "it's easier the second time around."

      Nobody ever appreciates the advice.

    19. Re:Translation by gargleblast · · Score: 1

      Oh yeah. Get an SSD.

    20. Re: Translation by Anonymous Coward · · Score: 1

      Wrong Adam, but he's right. Cheshire cats, pImpl, not pretending to know better than the compiler about inlining.

      Also not putting code in headers, making interfaces lean. Restricting the public interface, using simplex types in place of complex types at interface boundaries. Minimize compile file depth and preprocessor and object file sizes. Reduce dependency to maximize parallel build efficiency by utilizing more parallel compiles.

      Control structural entropy with active process and attention to detail. No hacking.

      These are the same processes that lead to highly testable and verifiable designs.

    21. Re:Translation by Darinbob · · Score: 4, Insightful

      Apple migrated over to Clang, but they left a bastardized GCC in it's place as a "transition". The preprocessor swears up and down that it's GCC but if you use inline assembler it barfs. So it's basically lying. It's a GCC front end and LLVM back-end, and the solution is to manually install a real GCC in its place (easier than porting the code, easier than ifdef OSX vs Linux, etc). And it's a major pain in the ass for new developers to set up a development environment, and inevitably someone eventually screws up their PATH to pick up the broken compiler...

      Anyway screw it. Clang may be ok, but there's production code that assumes GCC, it's cross-platform code so having stability on x86 doesn't necessarily mean anything about all those other CPUs that GCC supports well. Ie, GCC supports ARM, and ARM supports GCC, so if I go to the team and say "hey guys, I'm going to mix things up for a few months and try out a different compiler, things might break unexpectedly but in the long run you might like it, what do you think?" then I won't get positive feedback.

      Until there are drawbacks to GCC there's no much reason to change.

    22. Re:Translation by Darinbob · · Score: 2

      C++ (in GCC at least) is vastly slower than C, for the same input source. That's without doing the complicated stuff. You can speed a lot of with parallel builds, but a lot of people don't set up their build systems to make that easy (just passing -j to a recursive make can break).

      A decade ago I had an 18 hour build. So bad that people would start shouting if someone tweaked a major header file. Much of the slowdown was from the commercial compiler, and part of the slowdown was by using Visual Studio as a build system which called out to the external compiler. Swap to GCC+make and the build times got down to 20 minutes.

      Also for builds, don't forget the continuous builds that are common these days. Any source code change checked in kicks off a build. You want to see the results of that sooner than later, so you don't get the phone call a few hours later asking you to fix the build. Then the nightly builds will often rebuild every permutation of the project which can take a very long time.

    23. Re:Translation by Darinbob · · Score: 2

      Number 1 is important. The primary reason Apple is supporting Clang is that they got a snit over the new GPL license.

      Now Clang/LLVM do work on more than just PC architectures, but there aren't as many developers and users for them compared to GCC users. So the more users you have the more feedback, the more feedback the better the end product. Especially with something like ARM with lots of processor and instruction set variants you really want to make sure you're not inadvertently the beta tester for the compiler support for your chip.

    24. Re: Translation by Darinbob · · Score: 3, Funny

      I keep telling the users that, run the program correctly the first time and you shouldn't have to run it a second time!

    25. Re:Translation by jcr · · Score: 1

      I don't get daily crashes or power losses where I live. Do you?

      My usual work flow is to copy my project from the SSD to the RAM disk in the morning, work on it and commit changes periodically to my local Git repo. If I were paranoid like you apparently are, I could just add a commit action upon every successful build.

      -jcr

      --
      The only title of honor that a tyrant can grant is "Enemy of the State."
    26. Re:Translation by Anonymous Coward · · Score: 1

      > I can't see any rational reason to prefer Gcc.

      1) Long term Freedom. The importance of which can not and should not be understated.

      And exactly how is it better for your freedom to avoid compiler with an actual free licence and stick to the one with a fascist "either you're with us or you're against us" mindset?

    27. Re: Translation by loufoque · · Score: 1

      >us vs. them
      That guy is such an extremist and so out of touch with reality that it's actually a funny read.

    28. Re:Translation by TheRaven64 · · Score: 4, Insightful

      Apple, whose most profitable product lines all include ARM chips, has a large compiler team working on Clang and LLVM. Google, which doesn't ship any ARM products, but does have a huge investment in the ARM ecosystem, also employs a lot of people contributing to LLVM. They contributed most of the ARMv8 back end. ARM also has several large teams working on LLVM (I think that they've largely consolidated them now - they used to have separate C, OpenCL-ARM, and OpenCL-Mali teams all working largely independently on LLVM) and is now (as the other poster says) using LLVM for their proprietary toolchain (which is mostly expensive because of the debugging / tracing tools), so they're contributing a lot. There are a lot more ARM developers working on LLVM than clang. IBM has people working on PowerPC support, Imagination Tech on MIPS support. SPARC is largely volunteers these days, but Oracle seems to be killing SPARC as a thing anyone else might care about.

      --
      I am TheRaven on Soylent News
    29. Re:Translation by TheRaven64 · · Score: 2

      C++. For efficiency, you often need to put small methods in the headers. If they're templates, then you don't have an option if you want to share them between compilation units. Change a template in a header and you can easily trigger recompilation of a few hundred C++ files.

      --
      I am TheRaven on Soylent News
    30. Re:Translation by TheRaven64 · · Score: 1

      Have you benchmarked it recently? I tried this on our build machines (32 cores, 256GB of RAM, 512GB SSD split mostly as L2ARC with a small amount as log device, 3TB spinning rust in a ZFS mirror) a while ago. The amount of I/O that actually goes to the spinning rust is tiny (object files are written out, but never read back), pretty much all I/O comes from the buffer cache. Putting it on a RAM drive reduced the writes, but there's no synchronisation required for them so they never became a bottleneck. In a couple of cases (some of our builds produce about 40-50GB of object code), the RAM drive made things slower because now you had a copy in the buffer cache and a copy in the RAM drive, whereas previously the authoritative copy is on the spinning rust and the copy that you actually used was on the RAM drive, so using a RAM drive triggered swapping, whereas not using one just had some bursts of 100MB/s streaming writes to the disk.

      TL;DR: If you've got enough RAM, the OS should be able to satisfy reads from RAM irrespective of what the backing store is.

      --
      I am TheRaven on Soylent News
    31. Re: Translation by DrXym · · Score: 2
      It works really well for portable source code and where everything you link to comes from the same compiler. It's not so good if you're trying to link to some dynamic or static lib which was produced for MSVC because memory management and exception handling are incompatible. Unfortunately this happens quite a bit on Windows. IDE support isn't particularly good by comparison to MSVC either, particularly interactive debugging.

      Clang for Windows is taking an approach which is somewhat bizarre but could potentially achieve better compatibility with MSVC - you drop the clang binaries on a system which already has Visual Studio and it compiles code against the same headers and libs. As such it already supports a lot of VC++ extensions, keywords and name mangling. You can even run Visual Studio and develop against clang from there and run MSBuild with clang.

      The flipside, is if you have MSVC installed (a perfectly good compiler suite) then why earth do you need Clang? I think Clang needs to be a standalone package, perhaps using the MingW headers, even if it is capable of using MSVC headers / libs if you have them installed. Then someone can choose what level of compatibility they want.

    32. Re:Translation by TheRaven64 · · Score: 1

      GCC supports ARM, and ARM supports GCC

      ARM has a lot more people working on LLVM than they do on GCC and they're slowly transitioning everything across. Their customers fell into two categories: ones that don't care about the license and ones that won't touch GPLv3. If you want a compiler with long-term support for ARM, then GCC probably isn't your best bet.

      --
      I am TheRaven on Soylent News
    33. Re: Translation by DrXym · · Score: 1
      PImpls are a pain to maintain. This pattern might serve a purpose where you have an interface into your code that you expect other people to cleanly use your header without suffering all the stuff your impl needs. The flipside is if your class changes a lot or isn't that big, you suddenly have twice as much code to maintain and twice as many method signatures to modify each time they change.

      I wouldn't go anywhere near this pattern for self contained code.

    34. Re:Translation by Anonymous Coward · · Score: 2, Insightful

      1) Long term Freedom. The importance of which can not and should not be understated.

      Except gcc doesn't give you that. The FSF keeps changing the goalposts on what constitutes "free" software (see GPLv2 vs GPLv3, for instance), and every time they do it, they lose developers who can no longer adapt to their ideology. Clang is FREE. Sure, someone might fork it, someone might embrace&extend it, this does not mean I will in the future be unable to use the Clang code I have on my hard-drive right now. And it's a compiler. It compiles code, to a standard. Lock-in is not an issue.

      You are pushing a concept of theoretical freedom which doesn't even give people the practical freedoms they actually need, against a different concept of freedom which is for all practical considerations completely unencumbered.

      Choose your compiler based on ideology if you like. The computer industry wasn't created on that basis, and doesn't work on that basis.

    35. Re:Translation by DamonHD · · Score: 1

      Been there, done that (because of a sysamdin who thought that doing diagrams with pretty pictures was MUCH more important than doing backups for any of my team for many weeks, on one occasion) and you know what?

      You're right!

      Rgds

      Damon

      PS. Not that I advocate it as normal workflow for the sake of everyone's blood pressure for a start!

      --
      http://m.earth.org.uk/
    36. Re:Translation by BlackHawk-666 · · Score: 1

      Given that's a massive proportion of all the markets I ever wish to program for, I will accept this terrible stricture and forge on in terrible silence.

      --
      All those moments will be lost in time, like tears in rain.
    37. Re:Translation by fche · · Score: 2

      "Clang is also portable code, which gcc absolutely is not." ... Er, gcc has been portable to cpu architectures and os platforms you probably haven't even heard of, in decades you perhaps weren't even born yet. It's probably one of the most portable pieces of software. (It pays for that in many ways.)

    38. Re:Translation by david_thornley · · Score: 1

      Optimization is almost certainly going to be better inside a translation unit than between two translation units. That could be important under certain circumstances. This is one of those cases where there may not be a good answer, but at least memory is cheap.

      --
      "When you have eliminated the unacceptable, whatever is left, however improbable, must be the truthiness" - Holmes
    39. Re:Translation by 0100010001010011 · · Score: 1

      which doesn't ship any ARM products

      Other than the Nexus product line.

    40. Re: Translation by spongman · · Score: 1

      i'd be interested to know how fast Clang compiles Petzond compared to msvc. including re-compiling with simple change with/without PCH & incremental linking enabled.

    41. Re:Translation by davester666 · · Score: 1

      whoosh.

      --
      Sleep your way to a whiter smile...date a dentist!
    42. Re:Translation by Darinbob · · Score: 1

      Does Apple care about ARM chips other than what's in their phones? Probably not. But still, Clang/LLVM is too new, if you want stability right now you stick to GCC instead of jumping ship because some PC users are in love with something else.

      In the embedded world it's a massive hassle to upgrade compilers even to a new version, you need buy off from everyone on the team, you still need to keep the old ones around to support older releases, you have to put the new tools on every dev and build machine, and you're never allocated any time to do this.

    43. Re: Translation by Darinbob · · Score: 1

      Hmm, what? GCC can be built natively on windows thus it is a native app, it can be built without cygwin libraries.

    44. Re:Translation by tepples · · Score: 1

      Then use smaller translation units for debug builds and larger ones for release builds. If you use a laptop or compact desktop for your debug builds, you may have to use a full-size desktop or server for release builds.

    45. Re:Translation by tepples · · Score: 1

      Sadly, there are many tests that pass when software is built with GCC but fail (sometimes even segfault) when the same software is built with clang.

      Have you reported each such test to the respective project's bug tracker?

    46. Re:Translation by tepples · · Score: 1

      First focus on quickly achieving correctness, and then add efficiency once the program is correct.

    47. Re:Translation by TheRaven64 · · Score: 1

      That's fine, as long as your program never has any changes to the requirements after the 1.0 release. i.e. in no code ever.

      --
      I am TheRaven on Soylent News
    48. Re:Translation by TheRaven64 · · Score: 1

      Does Apple care about ARM chips other than what's in their phones?

      No, but that still covers ARMv7 and ARMv8. The amount of microarchitectural tuning for specific cores within these families is a very small part of the code. And, as I said, Apple is not the only company throwing resources at the ARM back end. Google and ARM are both spending a lot of resources improving LLVM. So is Qualcomm, so is Linaro, and so is Samsung.

      But still, Clang/LLVM is too new

      You realise that LLVM is over a decade old and Clang is almost this old? And that both are more actively developed than GCC and have been for the last few years? How old does a compiler have to be before it is not 'too new' in your estimation?

      if you want stability right now you stick to GCC instead of jumping ship because some PC users are in love with something else.

      By 'some PC users', I presume you mean the toolchain group and senior management at ARM Ltd.? You know, the ones who have replaced ARMcc with Clang in their toolchain and are recommending LLVM for all of their customers?

      --
      I am TheRaven on Soylent News
    49. Re:Translation by david_thornley · · Score: 1

      So I test something different from what I put into production? Nothing could possibly go wrong that way, yes.

      --
      "When you have eliminated the unacceptable, whatever is left, however improbable, must be the truthiness" - Holmes
    50. Re:Translation by tepples · · Score: 1

      Perform early testing, while the program is under rapid development, with smaller translation units, and later testing, after most of the functionality has become stable, with larger translation units. That's not the same as shipping untested binaries. Also run both the program with smaller translation units and the program with larger translation units in parallel, and consider it a defect if there is any difference in behavior between the two other than execution time.

  2. Or compile on everything by Anonymous Coward · · Score: 3, Insightful

    Compile in both. And visual studio too if possible. Enable all warnings. They are all good at identifying different categories of problems and code that compiles cleanly under multiple compilers will prove easier to maintain.

  3. Strongly recommend Clang by Anonymous Coward · · Score: 3, Informative

    I strongly recommend Clang. It's speed is about on par with GCC, but the output and syntax checking is so much better. I tried it a bit a few years back and I have been hooked on Clang since. Using GCC feels like a big step backward now because it's so fussy and its warnings are so cryptic in comparison.

    1. Re:Strongly recommend Clang by lgw · · Score: 1

      I strongly recommend Clang. It's speed is about on par with GCC, but the output and syntax checking is so much better. I tried it a bit a few years back and I have been hooked on Clang since. Using GCC feels like a big step backward now because it's so fussy and its warnings are so cryptic in comparison.

      Last time I looked, Clang didn't recognize idiomatic "rotate", e.g.:
      (x << 3) | (x >> 29)
      That should compile to a single opcode, dammit. Anyone know if that problem is still there.

      --
      Socialism: a lie told by totalitarians and believed by fools.
    2. Re:Strongly recommend Clang by angel'o'sphere · · Score: 1

      On what processor would that be a single opcode? I'm curious.

      --
      Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
    3. Re:Strongly recommend Clang by flargleblarg · · Score: 2

      Actually, that should compile to a single opcode only if x is a 32-bit unsigned integer.

    4. Re:Strongly recommend Clang by nctritech · · Score: 2

      Assuming 'uint32_t x' and ignoring any needed shuffling to load 'x' into the appropriate processor register, any 32-bit processor with a native bit rotation instruction.

    5. Re:Strongly recommend Clang by mikael · · Score: 1

      You can put function calls inside a switch statement but outside the case statements in gcc. Does that happen in Clang? Even old Borland C++ compilers from 1990's would catch that one.

      --
      Vintage computer adverts: http://www.vintageadbrowser.com/computers-and-software-ads
    6. Re:Strongly recommend Clang by Anonymous Coward · · Score: 2, Informative

      $ cat rol3.c

      #include <stdint.h>
      uint32_t rol3(uint32_t x) {
          return (x << 3) | (x >> 29);
      }

      $ gcc -c rol3.c
      $ objdump -d rol3.o

      0000000000000000 <rol3>:
        0: 55 push %rbp
        1: 48 89 e5 mov %rsp,%rbp
        4: 89 7d fc mov %edi,-0x4(%rbp)
        7: 8b 45 fc mov -0x4(%rbp),%eax
        a: c1 c0 03 rol $0x3,%eax
        d: 5d pop %rbp
        e: c3 retq

      $ gcc -O1 -c rol3.c
      $ objdump -d rol3.o

      0000000000000000 <rol3>:
        0: 89 f8 mov %edi,%eax
        2: c1 c0 03 rol $0x3,%eax
        5: c3 retq

      $ clang -c rol3.c
      $ objdump -d rol3.o

      0000000000000000 <rol3>:
        0: 55 push %rbp
        1: 48 89 e5 mov %rsp,%rbp
        4: 89 7d fc mov %edi,-0x4(%rbp)
        7: 8b 7d fc mov -0x4(%rbp),%edi
        a: c1 e7 03 shl $0x3,%edi
        d: 8b 45 fc mov -0x4(%rbp),%eax
        10: c1 e8 1d shr $0x1d,%eax
        13: 09 c7 or %eax,%edi
        15: 89 f8 mov %edi,%eax
        17: 5d pop %rbp
        18: c3 retq

      $ clang -O1 -c rol3.c
      $ objdump -d rol3.o

      0000000000000000 <rol3>:
        0: c1 c7 03 rol $0x3,%edi
        3: 89 f8 mov %edi,%eax
        5: c3 retq

      tl;dr: gcc uses the rol instruction with default -O0, but clang doesn't use it until you turn on -O1.

      Your comment has too few characters per line (currently 18.1).Your comment has too few characters per line (currently 18.7).Your comment has too few characters per line (currently 19.6).Your comment has too few characters per line (currently 20.6).Your comment has too few characters per line (currently 21.6).Your comment has too few characters per line (currently 22.5).

    7. Re:Strongly recommend Clang by serviscope_minor · · Score: 1

      fussy? What do you mean? They both pretty much support the entire standard very well. If gcc is failing to accept code, you're probably doing something non standard tha clang has as an extension.

      --
      SJW n. One who posts facts.
    8. Re:Strongly recommend Clang by BlackHawk-666 · · Score: 1

      SHL and SHR...been a long time since I used them, but pretty sure they will still be around.

      --
      All those moments will be lost in time, like tears in rain.
    9. Re:Strongly recommend Clang by angel'o'sphere · · Score: 1

      You missed the parent:
      He claimed that instant of
      LDC R1 #value1
      LDC R2 #value2
      SHR R1,something
      SHL R2,something
      OR R3,R1,R2

      You can do the above in a single opcode ... it is beyond me how he would do that or if that is possible because the constants in his example are "special".

      Assuming the registers R1 and R2 already hold the values in question: you need minimum 3 opcodes or a special architecture like ARM, but on an ARM I believe you minimum would need 2 opcodes.

      --
      Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
    10. Re:Strongly recommend Clang by angel'o'sphere · · Score: 1

      But you fail to give an example ;D
      Which opcode, on an x86 e.g. can rotate left and right simultaniously and OR the two results together: simultaniously? Feel free to pick a different architecture. Not even on an ARM you can do that in one opcode.

      --
      Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
    11. Re:Strongly recommend Clang by nctritech · · Score: 1

      I see the problem. GP didn't write the complete bitwise rotation idiom but I knew what they meant whereas you're evaluating it exactly as written. This is what I'm referring to and what GP was almost certainly trying to refer to as well. https://en.wikipedia.org/wiki/...

    12. Re:Strongly recommend Clang by angel'o'sphere · · Score: 1

      Ah, I'm coming closer to it. But I have not grasped it yet.
      I shift left 3 times and or the result with the same register shifted right 29 times, both together is a rotate? Never thought about that. I have to test that on paper, does not really look plausible on the first attempt.

      --
      Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
    13. Re:Strongly recommend Clang by nctritech · · Score: 1

      Sounds like you've got the gist of it. C only has a bitwise shift (where the end that the shift would "empty" is filled with zeroes for unsigned integers), not a bitwise rotate (ex. 00001011 rotated right by 2 becomes 11000010), so the closest thing to saying "bitwise rotate this" is using a set of expressions that would have the same behavior. Since a bitwise rotate is the same as splitting the binary number into two parts (of sizes determined by the desired rotation amount) you just have to create the two split numbers, then OR them back together. The Wikipedia example tries to be somewhat universally workable (using CHAR_BIT, sizeof(), etc. to make it work with virtually any unsigned integer type without modification) which is why it's a bit confusing. A simplified example:

      If the value in a variable 'uint8_t x' is 10010011 and you want to bitwise rotate it right by 3 bits (to yield the result 01110010), you need to first derive the partial values [000]10010 and 011[00000], then OR them together.

      The right-shift operator '>>3' will create the [000]10010 for us without any extra work.

      We can generate the 011[00000] by doing a left shift '<<5' in the same way but with the reverse number of bits (calculated as BIT_WIDTH_OF_TYPE - left_shift_count to get 8 - 3 = 5).

      Thus, 'x right-rotated by 3' for an 8-bit unsigned integer can be stated most simply (and inflexibly) in C as (x << 3) | (x >> 5), and the compiler will recognize the "two opposite shifts with a total shift of the variable type width ORed together" and emit a single bitwise rotate instruction (like 'r1 ROR r2') for the actual work rather than "load to r1; rotate r1; load to r2; rotate r2; r2 OR r1." Any unsigned integer can be rotated in the same way. Relevant x86 assembly: https://en.wikibooks.org/wiki/...

      Does that help?

    14. Re:Strongly recommend Clang by lgw · · Score: 1

      ROR ROL

      Those are the x86 "rotate" instructions. They work like SHR and SHL, except the "rotate back in" the bits that were shifted out. They're important for a variety of CRC, RNG, and crypto coding.

      --
      Socialism: a lie told by totalitarians and believed by fools.
    15. Re:Strongly recommend Clang by lgw · · Score: 1

      Awesome, please mod up. That's a great, detailed answer.

      --
      Socialism: a lie told by totalitarians and believed by fools.
    16. Re:Strongly recommend Clang by angel'o'sphere · · Score: 1

      I know what ROR and ROL is, facepalm.

      But I don't graps (and I did not dig into it, yet) why an SHL 3 and SHR 29 should be a rotate left if the results are ored together. It is not visible to me ... but as written in another post, I make me a paper example and see how it works :D

      And: assuming the compiler grasps that means: you assume the programmer of the compiler has put a special "if" somewhere in the code just looking for this idiom.
      However, it is beyond me, why C/C++ has not introduced tripple less and triple greater for ROL and ROR as Java e.f for ROL.

      --
      Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
    17. Re:Strongly recommend Clang by nctritech · · Score: 1

      I'm completely fine with trolls as long as they're not trolling me very hard. ;-) There is often a fine line between trolling and innocent asking of questions regarding something the asker isn't familiar with. Also, a lot of C programmers don't ever have to write code that uses bitwise rotation (for example, I'd be surprised if there were bitwise rotations in a text editor or a text processing program), so even if I got trolled the information may get someone else's mental gears cranking.

    18. Re:Strongly recommend Clang by nctritech · · Score: 1

      C/C++ are slow to evolve. It looks like at least this one proposal to add the various rotation operators to C/C++ has been authored and it has lots of good information. Your working example of "SHL 3 OR SHR 29 = ROL 3" is obvious when you scribble it on paper: a left rotate is a left shift where the bits that get pushed off the left and "discarded" instead appear on the right in the same order they fell off, so the 32-bit number 10000000000000000000000000000000 -> ROL 3 -> 00000000000000000000000000000100. Picture an arrow going from the "fall off" side back around to the "shift in zeroes" side you're used to seeing with the << operator and it makes more sense.

      As for how the compiler "just knows" that this specific construct should become a ROL/ROR instruction, it's not that the compiler has a "special IF" statement on the front-end, but rather has to do with how compilers convert source code into object code. When the compiler breaks down our rotation idiom, it sees it as a series of simpler distinct steps with anonymous temporary variables, and a built-in optimizer can recognize the rotation sequence from there. This is what the compiler sees (see three-address code) when it converts the expression '(x > 29)' into its component parts:

      t1 := x << 3
      t2 := x >> 29
      t3 := t1 | t2

      Even a simple peephole optimizer can easily spot this short sequence of fundamental operations (left shift + right shift, the shift amounts equal the width of the variable type, ORed together -> ROL) and convert it to something like:

      t1 := x ROL 3

      ...which is extremely simple to convert to assembler as i.e. ROL 3, EAX. One more important thing about rotations: if you rotate by the width of the unsigned integer type you're working with, you'll get the exact same number, and for any integer X of given width W,

      X ROL n == X ROR (W - n)

      So ROL 3 and ROR 29 are identical operations when performed on a 32-bit wide integer. That's part of why the C idiom works: a shift is a "rotate" that throws away the bits that "fall off" and zeroes the bits that would be missing, so by ORing two exactly opposing shifts together, you are manually grafting the bits that fell off one side back onto the other side.

  4. What do they mean by speed? by BitterOak · · Score: 1

    When people say gcc beats clang on speed, so they mean compile speed or run-time speed?

    --
    If I can be modded down for being a troll, can I be modded up for being an orc, or a balrog?
    1. Re:What do they mean by speed? by Chris+Mattern · · Score: 4, Funny

      If I can be modded down for being a troll, can I be modded up for being an orc, or a balrog?

      Nope, those still get you modded down. To be modded up, you'll need to be an elf or a hobbit...

    2. Re:What do they mean by speed? by BitterOak · · Score: 1

      If I can be modded down for being a troll, can I be modded up for being an orc, or a balrog?

      Nope, those still get you modded down. To be modded up, you'll need to be an elf or a hobbit...

      I hear you can also get modded up for being an ent, but it can take years.

      --
      If I can be modded down for being a troll, can I be modded up for being an orc, or a balrog?
  5. Neat thing by mveloso · · Score: 1

    One neat thing about LLVM/clang is that theoretically you could provide your application as IR files, which can be flattened to machine-specfic code on the fly by a platform-specific backend.

    I think at some point it was fast enough to generate that code on the fly. I vaguely remember Apple doing that with its OpenGL stuff.

    1. Re:Neat thing by sribe · · Score: 1

      I vaguely remember Apple doing that with its OpenGL stuff.

      CoreImage transforms.

    2. Re:Neat thing by Pseudonym · · Score: 1

      I think at some point it was fast enough to generate that code on the fly.

      Many interpreters are embedding LLVM for JIT these days.

      --
      sub f{($f)=@_;print"$f(q{$f});";}f(q{sub f{($f)=@_;print"$f(q{$f});";}f});
    3. Re:Neat thing by PhrostyMcByte · · Score: 1

      One neat thing about LLVM/clang is that theoretically you could provide your application as IR files, which can be flattened to machine-specfic code on the fly by a platform-specific backend.

      Is this a relatively new feature? The last time I looked at LLVM IR for this purpose, there was no general "pointer-sized" data type that could accomplish this. It would be really cool if this were no longer the case.

    4. Re:Neat thing by complete+loony · · Score: 1

      Chrome's PNaCl defines a stable sub-set of LLVM IR, targeting a generic little-endian 32-bit CPU.

      --
      09F91102 no, 455FE104 nope, F190A1E8 uh-uh, 7A5F8A09 that's not it, C87294CE no. Ah! 452F6E403CDF10714E41DFAA257D313F.
    5. Re:Neat thing by Anonymous Coward · · Score: 1

      They also use it to shrink iOS app distribution. Developers upload IR code to the app store, the app store dynamically recompiles for the user's device and pushes down a custom binary.

    6. Re:Neat thing by TheRaven64 · · Score: 1

      The grandparent is partly right. You can do this for microarchitectural optimisations. I believe that Apple now does this for their app store: if you upload the app as LLVM IR then they will generate versions specialised for each of the ARM cores that they've shipped. This can be quite a big difference (costs of different vector operations on different cores means that you can have quite different decisions in the autovectoriser, for example). You can't do it for different architectures, even if LLVM IR were perfect for the task, if you start with a C-like language. By the time that you've run the C preprocessor, your source code is no longer architecture agnostic. Things like va_list, intptr_t and htons are defined in such a way that encodes pointer size, endian, and variadic calling convention. Things like jmpbuf and ucontext_t encode the architecture fairly precisely.

      --
      I am TheRaven on Soylent News
  6. Use both by Anonymous Coward · · Score: 2, Interesting

    To make sure my code is not using any compiler features I compile my code on both.

  7. Intermediate formats by unixisc · · Score: 1

    Does Clang compile to intermediate formats, such as Bytecode or any other formats? On the GCC side, I believe that the FSF (read RMS) is opposed to it

    1. Re:Intermediate formats by KiloByte · · Score: 1

      You mean, like GIMPLE?

      --
      The creatures outside looked from Alt-Right to Antifa; but already it was impossible to say which was which.
    2. Re:Intermediate formats by dmoen · · Score: 2

      Clang can compile to the LLVM "IR" format, which is a mostly machine-independent Intermediate Representation. Kind of like bytecode.

      The IR file format has two variations: a human readable text format, and a more compact binary format.

      Given an IR file, you can optimize it, which produces another IR file, or you can compile it into an object file.

      --
      I have written a truly remarkable program which this sig is too small to contain.
    3. Re:Intermediate formats by TheRaven64 · · Score: 1

      LLVM "IR" format, which is a mostly machine-independent Intermediate Representation

      LLVM IR is target agnostic, not target independent. The format itself is the same across all targets, but various things (e.g. calling conventions - how you split structs between registers and the stack) will be different for different targets.

      --
      I am TheRaven on Soylent News
  8. License by danbob999 · · Score: 2

    Isn't LLVM/clang all about the license (non-GPL)? Otherwise if clang is good, then we should fork it and make a GPL version of it.

    1. Re:License by dmoen · · Score: 4, Informative

      No, LLVM/clang is all about having a superior architecture to GCC, so that a lot of new applications become possible. One of the key ideas is that the optimizer and code generator are libraries with a C++ API. One cool application of this is that you can use the LLVM library to implement a JIT compiler for your interpreted language: you generate the machine code directly into memory (instead of to a file), then execute it.

      LLVM has many more developers than GCC, and is evolving and improving more quickly than GCC can. This is because of the licence: it turns out that corporations like Apple are more willing to provide developer resources for this open source project if the licence isn't copyleft. For this particular project, this means that the BSD license is more successful than the GPL. Of course, there are other projects for which the GPL produces better results in the real world.

      If you want to make a GPL fork of LLVM just for the pure pleasure of fucking over the original project due their heresy in choosing a license you don't approve of, well, good luck with that.

      --
      I have written a truly remarkable program which this sig is too small to contain.
    2. Re:License by Anonymous Coward · · Score: 1, Insightful

      JIT compiled binaries are no good for the end user. For fucks sake, distribute native binaries for your shit.

    3. Re:License by phantomfive · · Score: 4, Interesting

      This is because of the licence: it turns out that corporations like Apple are more willing to provide developer resources for this open source project if the licence isn't copyleft.

      That's not really true, Apple contributed to GCC for years before LLVM. The problem was that Apple wanted features, and GCC wouldn't allow those features to be built. So finally Apple just gave up and built their own.

      This is the rule of running an open source project (and Linus does a really good job of it): give users what they want, or the users will leave.

      --
      "First they came for the slanderers and i said nothing."
    4. Re:License by tlhIngan · · Score: 3, Informative

      This is because of the licence: it turns out that corporations like Apple are more willing to provide developer resources for this open source project if the licence isn't copyleft.

      That's not really true, Apple contributed to GCC for years before LLVM. The problem was that Apple wanted features, and GCC wouldn't allow those features to be built. So finally Apple just gave up and built their own.

      This is the rule of running an open source project (and Linus does a really good job of it): give users what they want, or the users will leave.

      True, but Apple could also see the writing on the wall with the GPLv3 and what that could mean to their use of the compiler. Which is also why they poured a lot of effort into LLVM and wrote clang. This effort dated way back too - the earliest versions of OS X that started coming out with LLVM as an option was around the 10.4 era.

      A lot of companies saw what the GPLv3 was bringing to the table, and they didn't like it, so they moved on. Some, like Apple, realized they could stick around and fight with GCC and GPLv3, or work on an alternative.

      GCC is deliberately obtuse as well - Apple wanted a nice front end to help parse and recompile code in real time, and GCC is intentionally twisty to prevent that (and if you want to write a parser for code, why not reuse what the compiler actually uses?). LLVM is more modular and makes it easier to integrate with an IDE

      And in the end, I think having the diversity we do in compilers is refreshing - even if you hate LLVM with a passion, at least it's also brought forth development on GCC, which seems to have a history of stalling in development before someone else decides to fork it, revamp things, and have the fork become the official GCC.

      FYI - the very last checkin to GCC by Apple was related to Grand Central Dispatch to the Obj-C compiler - basically about blocks.

    5. Re:License by Bananenrepublik · · Score: 1

      Let's not forget that originally, Steve Jobs tried to screw over the GCC project by making a proprietary closed-source fork of it.
      Flag as Inappropriate

      No, he didn't.

      He tried to claim that NeXT's own code in the Objective-C front end was proprietary. He was more than happy to distribute the rest of GCC freely, but wanted to keep Objective-C closed. He failed.

      Which is exactly the same thing as the OP said, once one uses precise language: the GPL's requirements also apply to the Objective C frontend precisely because the compiler as a whole is a single program (program = "copyrightable work licenced under this license").

    6. Re:License by TheRaven64 · · Score: 1

      The problem was that Apple wanted features, and GCC wouldn't allow those features to be built. So finally Apple just gave up and built their own.

      This was not the issue. Apple maintained their own branch of GCC in the FSF's svn repository for a long time. Lots of features (e.g. blocks support, some Objective-C features) never made it back into the FSF's branch, but this was not a problem for Apple. The problem was that GCC could not be used, for both technical and legal reasons, in some of the places where Apple wanted to use it. Syntax highlighting was one simple example. GCC does constant folding in the front end, so by the time that you have anything like an AST, you can't distinguish between 3+4 and 7 in the source code - that made working out how to colour the tokens difficult. Even if this were addressed, you could only embed GCC in a GPLv3 IDE, and Apple didn't want to have to release XCode under GPLv3.

      --
      I am TheRaven on Soylent News
    7. Re:License by TheDarkMaster · · Score: 1

      Why GCC is deliberately obtuse?

      --
      Religion: The greatest weapon of mass destruction of all time
    8. Re:License by BitZtream · · Score: 1

      Apple started LLVM due to GPL v3. If you know anything about why they switched, that much is obvious ... being that they flat out said thats why they were switching among other things.

      --
      Persistent Volume manager for Kubernetes - https://github.com/dwimsey/openshift-pvmanager
    9. Re:License by devent · · Score: 1

      You have cherry picked one comment from Richard Stallman, which was just his personal ideology. David Edelsohn replied this:
      https://gcc.gnu.org/ml/gcc/201...

      GCC is working toward re-factoring its code base toward a more
      compositional approach for "toolification". One can look at
      presentations from the recent GNU Cauldron 2013 for discussion of the
      topic. David Malcolm also has created patches for the GCC backend to be used as a JIT.
      The assertions that FSF policy prevents technical development and
      innovation simply is not true.

      Maybe GCC was "deliberately obtuse", but as I see it, steps are taken to change that and enhance GCC for tool chains and to be used a library just as LVMM is.

      --
      http://www.mueller-public.de - My site http://www.anr-institute.com/ - Advanced Natural Research Institute
    10. Re:License by Tough+Love · · Score: 1

      It seems to be exactly what happened. A "front end" is clearly a "derived work".

      --
      When all you have is a hammer, every problem starts to look like a thumb.
    11. Re:License by Tough+Love · · Score: 1

      Does "development" of GCC these days consist of anything other than backporting improvements from LLVM and slapping GPLv3 on them?

      It certainly does, however you draw attention to one of the principal superpowers of copyleft.

      --
      When all you have is a hammer, every problem starts to look like a thumb.
    12. Re:License by Tough+Love · · Score: 1

      huh?

      --
      When all you have is a hammer, every problem starts to look like a thumb.
    13. Re:License by Tough+Love · · Score: 1

      Anybody with a clue could have told Jobs the same thing, and he did end up respecting the GPL then he did nothing more than respect the law.

      --
      When all you have is a hammer, every problem starts to look like a thumb.
    14. Re:License by gerddie · · Score: 1

      Depends, for instance llvm is used as GLSL compiler, and hence, it can select the target platform based on the available hardware. Likewise POCL uses LLVM to compile OpenCL kernels optimized for the hardware the kernels are supposed to run on.

    15. Re:License by gerddie · · Score: 1

      Or everything-and-its-cat now depending on systemd.

      It is interesting that you bring up systemd in a GPL discussion. Their was some discussion about how the IPC infrastructure of systemd can effectively be used to circumvent the GPL by providing a GPL wrapper to a GPL library/program that provides a RPC interface and then use this interface from a non-free program remotely. Since no direct linking is involved, this is actually legal, and the a legal way to prevent this is licensing the code under the AGPL

  9. Mixed backend languages is recipe for subtle bugs by Ungrounded+Lightning · · Score: 2

    From up-close-and-personal experience with Objective C and C++ (also Smalltalk), these languages have substantially different semantics regarding class identity (primarily: what version of overridden member functions you get) during construction and destruction. I wouldn't be surprised if Objectivce-C++ had yet another semantics, pulling "features" from both, and I have no clue about LVMM.)

    Building a frontend to compile to them, interchangeably, is a recipe for subtle bugs, if the frontend doesn't deal with these issues. Efficiency may go out the window if the frontend tries to impose one language's construction/destruction semantics on another. Doing so also brings up the issue of which semantics the compiler exposes to the programmer - each has its good and bad points, and each enables different - and incompatible - useful programming features.

    I'd be interested in what (if anything) Clang has done about this issue. (Unfortunately, I'm busy on other stuff, so I'll have to depend on others to elucidate this.)

    --
    Bantam Dominique roosters crow a four-note song. Once you've heard it as "Happy BIRTHday" you can't NOT hear it that way
  10. Developing in C/C++ by Anonymous Coward · · Score: 1

    First of all, C and C++ are two different and separate languages.

    Second of all, if you are developing in either C or C++, you should develop in a different language.

    1. Re:Developing in C/C++ by HiThere · · Score: 1

      Second of all, if you are developing in either C or C++, you should develop in a different language.

      Depends on what you're doing. There are purposes for which C is the optimal language. I suspect that there are also purposes for which C++ is the optimal language. (In neither case can the purpose include handling unicode strings.)

      --

      I think we've pushed this "anyone can grow up to be president" thing too far.
    2. Re:Developing in C/C++ by epyT-R · · Score: 1

      Oh. What language is that? javascript?

    3. Re:Developing in C/C++ by Marginal+Coward · · Score: 1

      Second of all, if you are developing in either C or C++, you should develop in a different language.

      OK, I'll bite. C and C++ are commonly used for real-time embedded applications, primarily because of speed. Do you have a better language for that? Remember, it has to compile on the processor you're actually using. My current project uses a Blackfin DSP processor by Analog Devices. The toolset that Analog provides supports only C, C++, and assembly. I don't expect them to provide Java, .Net, PHP, Haskell, Python or [your favorite language here] anytime soon.

    4. Re:Developing in C/C++ by serviscope_minor · · Score: 2

      My current project uses a Blackfin DSP processor by Analog Devices. The toolset that Analog provides supports only C, C++, and assembly. I don't expect them to provide Java, .Net, PHP, Haskell, Python or [your favorite language here] anytime soon.

      You can run Linux on a blackfin. I'd strongly expect you could easily run Python or PHP on it. Not sure why you'd want to.

      --
      SJW n. One who posts facts.
    5. Re:Developing in C/C++ by david_thornley · · Score: 1

      C++ typically provides fast code with excellent abstraction capabilities. It's also much safer than C, since modern C++ eliminates a lot of C memory bugs (when used according to reasonable standards, anyway).

      --
      "When you have eliminated the unacceptable, whatever is left, however improbable, must be the truthiness" - Holmes
    6. Re: Developing in C/C++ by HiThere · · Score: 1

      Sorry, but many languages have decent unicode string handling. A partial list is:
      Python, Ruby (with a gem), D, and even Vala. I'm sure there are others, and Vala proves that C *could* be adapted to have decent unicode string handling. It just doesn't. Possibly in the case of C it would interfere with its minimalism, which is crucial for many of its uses. In C++ the only reason that I can imagine is that it might interfere with backwards compatibility. But the kludge they settled on is just atrocious.

      --

      I think we've pushed this "anyone can grow up to be president" thing too far.
  11. Except RHEL doesn't have clang by halfdan+the+black · · Score: 1

    Sure, I can spend who knows how much time trying to get it to compile myself and install it my home dir, but then I can't ship binaries compiled with clang.

    And I've never had much luck getting clang to compile using gcc c++ stdlib and link against the gnu c++ libs. I suppose I could just statically link against c++ stdlib though.

    1. Re: Except RHEL doesn't have clang by kthreadd · · Score: 1

      Problems? You can yum install it. https://rhn.redhat.com/errata/... But that's an old version. They are up to GCC 5 now.

    2. Re:Except RHEL doesn't have clang by TheRaven64 · · Score: 1

      Recent LLVM / Clang needs a moderately compliant C++11 compiler to work. I think that means gcc 4.8 or newer, though it could be 4.9. There are some weird things that you sometimes hit with g++ / libstdc++ and C++11 (I was recently bitten by the fact that initialiser lists don't seem to work with std::tuple), but LLVM has buildbots that try to build it with gcc and so avoids these features.

      --
      I am TheRaven on Soylent News
  12. Evidence? by mveloso · · Score: 3, Interesting

    I would think that if these subtle bugs exist they'd be caught by the test suites. Do you have any actual evidence of these subtle bugs?

    1. Re:Evidence? by aberglas · · Score: 1

      No, the trouble is that you just cannot implement an efficient garbage collector in C/++. So Java and .Net do their own compiling. C is only efficient if you think "C".

  13. Auto-vectorization by iamacat · · Score: 1

    Once I checked, clang was not good at generating vector instructions from simple loops. Unless it has improved lately, that would be a show stopper for a class of code.

    1. Re:Auto-vectorization by TheRaven64 · · Score: 1

      It's been hugely improved in the last couple of releases. Linaro did quite a bit of work on the vectoriser, as did Apple.

      --
      I am TheRaven on Soylent News
  14. Comparison by dawilcox · · Score: 1

    I've done POSIX C++ programming for five and a half years. I compile on my mac using clang and on my ubuntu machine using g++. Here's the differences: 1. Clang seems to have better error messages. 2. Of course, clang is usable using libraries (rtags for emacs lets you do ctags but way better for example). 3. Run speed is about the same for optimized build between the two. They're within 1% for our application using the benchmarks for it. 4. Compile speed is a bit faster using clang. I like having two big compilers that compete with each other. It forces both to try harder and to become better compilers in the end.

    1. Re:Comparison by friedmud · · Score: 1

      Exactly this. We do the very same.

      However, Clang is significantly faster for our application. I would do some timings for you right now... but it takes too damn long to compile the entire stack with GCC ;-) Last time I checked it was on the order of 25% faster. Over a whole day of compiling that can make a big difference.

  15. Graph by JBMcB · · Score: 1

    So you could write a tool against LLVM that looks to see which templates are causing problems, maybe output the usage graph to graphviz to help refactor your code...

    --
    My Other Computer Is A Data General Nova III.
  16. Both GCC and Clang by ortholattice · · Score: 1

    I periodically run both just to get the warnings that each is able to provide. And I clean up all warnings, even if I know that they are harmless, so I won't miss a blunder buried in the noise. Turning on all warnings has saved me from having to chase down more bugs than I care to admit. gcc *.c -o xxx -O -Wall -Wextra -Wmissing-prototypes \ -Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-align \ -Wredundant-decls -Wnested-externs -Winline \ -Wno-long-long -Wconversion -Wstrict-prototypes -ansi -pedantic clang *.c -o xxx -Wall -Wextra Any other warnings you'd recommend?

    1. Re:Both GCC and Clang by ortholattice · · Score: 1

      Damn, picked wrong formatting.

      gcc *.c -o xxx -O -Wall -Wextra -Wmissing-prototypes \ -Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-align \ -Wredundant-decls -Wnested-externs -Winline \ -Wno-long-long -Wconversion -Wstrict-prototypes -ansi -pedantic

      clang *.c -o xxx -Wall -Wextra

  17. Why is this modded informative? by Ixpath · · Score: 1

    If you bother to actually follow the link it shows gcc producing faster code except in the artificial benchmarks he cherry-picked on page two.

    1. Re:Why is this modded informative? by Anonymous Coward · · Score: 1

      If you bother to actually follow the link it shows gcc producing slower code except in the artificial benchmarks he cherry-picked on page two.

      Fixed that for you.

    2. Re:Why is this modded informative? by Darinbob · · Score: 1

      It's a Dice article. It's therefore intentionally click bait.

  18. G++ versus Clang++ by ShakaUVM · · Score: 4, Informative

    >better error messages, warnings, and static analysis from Clang. Those save me way more time than the speed of the compiler.

    This is the truth. Ain't nobody got time to read through a hundred lines of error messages to track down a single bug.

    As a simple test, I wrote the following code which has a simple mistake (using an integer instead of an int array in a range-based for loop):

    test.cc:

    #include
    int main() {
            int x = 0;
            for (int i: x) std::cout constexpr const _Tp* std::end(std::initializer_list)
              end(initializer_list __ils) noexcept"

    2 lines for a single straightforward error in clang++ 3.7 (with colors even!):

    "test2.cc:5:12: error: invalid range expression of type 'int'; no viable 'begin' function available
                    for (int i: x) std::cout i;"

    I currently use g++ with new programmers, and will be switching soon to clang++.

    Plus Clang does cool things like interoperate with things like YouCompleteMe to do real time compiling and error message generation for syntax completion in VIM.

    1. Re:G++ versus Clang++ by ShakaUVM · · Score: 2

      (Sigh, Slashdot. Stop eating code snippets in Plain Old Text mode. This is a place for geeks.)

      Here's the code that generated the bug, without Slashcode mangling it:

      int x;
      for (int i: x) std::cout i;

      109 lines of error messages in g++, 2 lines in clang++

    2. Re:G++ versus Clang++ by Darinbob · · Score: 1

      My previous job we used C++ with template heavy usage and I ended up being the unofficial translator of G++ error messages. This is really the only part of GCC I dislike (and it's only on the C++ side which I'm not using now).

    3. Re:G++ versus Clang++ by serviscope_minor · · Score: 2

      If you're marvelling over clang producing coloured error messages, then you're using an old version of gcc. Try switching to something in the new gcc-5 series. Contrary to popular belief, it's quite slow, but not hard to compile. Also, many distros have gcc-5 packages available.

      PS, both versions of your code got mangled, so I can't see what you're referring to precisely.

      --
      SJW n. One who posts facts.
    4. Re:G++ versus Clang++ by david_thornley · · Score: 1

      I'd be willing to bet that there's supposed to be less-than or greater-than symbols in there. Slashcode helpfully treats them as some sort of html tag, and ignores what it doesn't recognize.

      --
      "When you have eliminated the unacceptable, whatever is left, however improbable, must be the truthiness" - Holmes
    5. Re:G++ versus Clang++ by ShakaUVM · · Score: 1

      You didn't enable c++11 mode. I mentioned the build flags in my original post, but Slashdot ate those as well.

      Try this:
      g++ -std=c++11 test.cc
      clang++ -std=c++11 test.cc

    6. Re:G++ versus Clang++ by ShakaUVM · · Score: 1

      I posted the code in a followup to my own post. It ate my build flags and I guess my version numbers as well if you didn't see them. g++4.8.4 (which is the current default g++ in Ubuntu 14.04) and clang++3.7.1.

      Basically, the bug is trying to use an integer instead of an array of integers in a range-based for loop, which is something people do occasionally by mistake. In g++, they go crosseyed trying to parse the error messages from this, as it spits out over a hundred lines of template substitution errors. Clang just says you can do a range based loop with an int.

      int x = 0;
      for (int i : x) cout i;

    7. Re:G++ versus Clang++ by serviscope_minor · · Score: 1

      I posted the code in a followup to my own post. It ate my build flags and I guess my version numbers as well if you didn't see them. g++4.8.4 (which is the current default g++ in Ubuntu 14.04) and clang++3.7.1.

      Ubuntu 14.04 has a gcc-5 PPA, which I use as gcc 5 has better C++14 support than 4.8.4, not to mention colourful error messages :)

      In g++, they go crosseyed trying to parse the error messages from this, as it spits out over a hundred lines of template substitution errors. Clang just says you can do a range based loop with an int.

      This seems to have improved a great deal: it tells you (correctly) that begin() and end() are missing. If you like to engage in C++ fuckery, you can overload begin() and end() for ints to confuse people, much like going bonkers with operator overloading.

      But then it suggests an alternative which is pointelss. It's a much shorter error message than a list of every substitution failure it had before. They should probably special case it further, but it's much better than it was.

      --
      SJW n. One who posts facts.
    8. Re:G++ versus Clang++ by ShakaUVM · · Score: 1

      That's good to know, thanks!

      I'll wait for gcc5 to be added to the main repository, though, and use clang in the meantime.

    9. Re:G++ versus Clang++ by serviscope_minor · · Score: 1

      I'll wait for gcc5 to be added to the main repository, though, and use clang in the meantime.

      I don't think they do that: 14.04 is meant to be LTS so they won't change the compiler version except for critical patches. The gcc-5 package allows it to coexist with the system compiler (the binary is /usr/bin/g++-5) quite happily.

      16.04 will get gcc 5, I strongly expect.

      --
      SJW n. One who posts facts.
  19. Re:Mixed backend languages is recipe for subtle bu by windwalkr · · Score: 2

    From up-close-and-personal experience with Objective C and C++ (also Smalltalk), these languages have substantially different semantics regarding class identity (primarily: what version of overridden member functions you get) during construction and destruction. I wouldn't be surprised if Objectivce-C++ had yet another semantics, pulling "features" from both, and I have no clue about LVMM.)

    Objective-C++ mixes the syntax of the two languages, and allows you to use either a C++ object or an Objective-C object at will, however it does not make C++ objects into Objective-C objects or vice versa. Any semantics relating to C++ objects still applies to C++ objects, and no additional semantics are implied.

    In short: things work as you'd expect, and there are no hidden gotchas.

    The only real complexities are what happens when you embed a C++ object as a member inside an Objective-C object (this doesn't change the semantics of the C++ object itself, but obviously may change the point at which the whole thing is destroyed) and what happens when you reference an Objective-C object from within a C++ object (some of the automatic refcounting syntactic sugar goes away and you have to actually understand what you're doing.) These don't introduce difficulties for the compiler, but could potentially be confusing for the programmer.

    I'm sure clang has its fair share of bugs, and I'm sure that GCC does also, that's just the nature of any complex codebase. The shared backend isn't really a contributing factor, any more so than them both emitting x86 machine code is a contributing factor.

  20. What planet are you on? by Etcetera · · Score: 1

    I care how fast my compiler compiles (as long as it's not insane) about as much as I care how fast my system boots (as long as it's not insane).

    Seriously. Whatever planet you weird devs came from, can you return to it? (And take Lennart with you?)... Spend your free time re-optimizing your cut-and-paste chef recipes or something, and leave the engineering to the rest of us?

    1. Re:What planet are you on? by keltor · · Score: 1

      I have a 3.5mil LoC codebase that takes roughly 15 minutes to compile with gcc and about 8 minutes with clang. That's 7 minutes of my life every few hours. I'm the architect, so most of my changes require a full recompile. I'll save a couple of dozen hours per year TYVM.

    2. Re:What planet are you on? by serviscope_minor · · Score: 1

      Where is the time spent?

      You should probably get someone to fork out for a 64 core build machine for you for nice, fast parallel builds.

      --
      SJW n. One who posts facts.
    3. Re:What planet are you on? by BitZtream · · Score: 1

      I'm the architect, so most of my changes require a full recompile.

      No. You aren't. An architect isn't writing code. And you'd be a shitty one if you were, since its a pretty shitty time spent if you're waiting for the entire system to compile for changes instead of having a CI system, unit tests and integration tests running on an automated system for you.

      Don't try to brag like you're awesome, you're post shows you aren't.

      --
      Persistent Volume manager for Kubernetes - https://github.com/dwimsey/openshift-pvmanager
  21. just a warning by Gravis+Zero · · Score: 2

    while clang is probably fine for most projects, if your project needs -std=c++14 then you should be warned that sometimes Clang can't properly deduce (even when you are explicit) variadic template parameters.

    for example, this code will not compile on Clang no matter what but works fine with G++.

    template<typename... ArgTypes> using deeper = void(*)(ArgTypes...);
    template<typename... ArgTypes> void shallow(deeper<ArgTypes...> arg) { }

    int main(int argc, char* argv[])
    {
        deeper<int> arg;
        shallow<int>(arg);
        return 0;
    }

    --
    Anons need not reply. Questions end with a question mark.
  22. Clang was written by author of Swift by johnrpenner · · Score: 1

    the guy who developed clang - Chris Lattner - also wrote swift.

    he first modernized the architecture of the compiler, and then wrote a better language on top of it that is capaable of doing system level things like C++ with a cleaner semantic style.

    ++

    1. Re:Clang was written by author of Swift by Anonymous Coward · · Score: 1

      Did he wrote that better language before or after he wrote Swift?

  23. Re:Linus Torvalds by BitZtream · · Score: 1

    ... you mean code written specifically for GCC doesn't work as well in every case under a different compiler without the same shitty GCC quirks?!

    WHAT IS THIS YOU SPAKE?

    --
    Persistent Volume manager for Kubernetes - https://github.com/dwimsey/openshift-pvmanager
  24. My suggestion by jgotts · · Score: 1

    How about writing code that runs well on both compilers?

  25. Parallel make when a program produces 2 files by tepples · · Score: 1

    You should be using precompiled headers

    Until the headers themselves change, causing a cascade throughout all source files that instantiate a class or its subclasses because of C++'s fragile base class tendency.

    and set up your makefiles for multiple cores.

    There are known limits of GNU Make when you have a single program that spits out more than one file. For example, a graphic may be converted for use on a particular platform by decomposing it into a tile file and a map file. Or a collection of documents may be compressed into a static dictionary and a (separate) archive of the compressed documents. The problem is that Make will often try to run the program twice at once, once to produce the tile file and once for the map file, wasting CPU. What's the typical workaround? Is it a good practice to have the program generate an archive file containing both files, and then generate the tile file and map file by extracting them from the archive file?

    Besides, link-time whole-program optimization doesn't parallelize nearly as well.

    So instead of compiling and linking the entire application repeatedly as you track down a bug, you only integrate after all your unit tests are written and passed in isolation.

    Which doesn't help when the bug is in the integration.

  26. uint32_t by tepples · · Score: 1

    For any type with a size less than 29 bits the behavior is undefined. [...] For signed types it is implementation defined.

    But what is it for uint32_t x = 0xdeadbeef;?

  27. shared_ptr by tepples · · Score: 1

    you just cannot implement an efficient garbage collector in C/++.

    Then what's std::shared_ptr if not an efficient reference counting garbage collector?

    1. Re:shared_ptr by bensch128 · · Score: 1

      Then what's std::shared_ptr if not an efficient reference counting garbage collector?

      If there are circular references, then shared_ptr won't help you.
      That being said, there are garbage collectors in C++ that are implemented in the memory allocation system but they aren't great.

    2. Re:shared_ptr by bensch128 · · Score: 1

      Erk, nevermind. I just realized that you were being sarcastic....

    3. Re:shared_ptr by tepples · · Score: 1

      In practice, how hard is it to avoid circular references using a combination of std::shared_ptr and std::weak_ptr? Besides, if you have circular references in any other language, in what order should the language call the objects' finalizers? Not calling them at all breaks the RAII idiom. Or what idiom is superior to RAII for ownership whose lifetime exceeds that of a single try statement?

  28. If only C and C++ compilers exist for an ISA by tepples · · Score: 1

    The fact that you have to install a cross-compiler for that particular (language, architecture) pair. For some less popular architectures, compilers accepting languages other than a handful (C++, C, and possibly Fortran) happen not to exist.