News for hackers. Stuff that matters.
Re:“It is never a compiler error” by revelation on Tuesday November 14, 2017 @07:04PM (#15700445)
clang/llvm makes the gcc developers look like amateurs when it comes to reckless optimizations for that extra percent in the synthetic benchmark. My goto approach for when something breaks in clang but works in gcc is to disable optimization, and when I can narrow it down to a single offending function, that gets optnone slapped on it and done.
Re:“It is never a compiler error” by wyldfire on Tuesday November 14, 2017 @07:39PM (#15700602)
> reckless

... is in the eye of the beholder. They exploit every corner of undefined behavior to the benefit of performance. Indeed other compilers might be "safer."

Keep in mind that they also provide you with UBSan to help you detect when you're doing it wrong.

Re:“It is never a compiler error” by ajross on Tuesday November 14, 2017 @09:57PM (#15701147)
That argument would make more sense if they actually were faster, which they often aren't. Last I checked (and I don't keep up with this, to be fair) they still lagged gcc on most macrobenchmarks on x86.
Re:“It is never a compiler error” by davidcuddeback on Tuesday November 14, 2017 @10:15PM (#15701205)
> they still lagged gcc

Isn't the "they" in this thread gcc, or did I misread something?

Re:“It is never a compiler error” by ajross on Tuesday November 14, 2017 @10:32PM (#15701253)
I took "clang/llvm may be reckless, but they're fast and have UBsan", to which I replied "gcc is still faster".
Re:“It is never a compiler error” by davidcuddeback on Tuesday November 14, 2017 @10:38PM (#15701275)
Oh, I thought the GP was accusing GCC devs of reckless optimizations. But UBSan is a clang project, so maybe you're interpretation makes more sense. Very confusing thread. :)
Re:“It is never a compiler error” by wyldfire on Tuesday November 14, 2017 @10:57PM (#15701343)
Sorry, I should've been more explicit when I responded to the original clang vs gcc comment.

Note that UBSan is (mostly? totally? originally?) a Google project and it's been ported to both clang and gcc.

Re:“It is never a compiler error” by skgoa on Thursday November 16, 2017 @03:33AM (#15711066)
I mean, if you use GCC does recklessly optimize away (some) security/safety code. This is one of the reasons why we don't use it for code we ship. I work in a safety-conscious industry, where an overeager compiler can literally kill people.
Re:“It is never a compiler error” by ensiferum on Wednesday November 15, 2017 @09:50AM (#15704099)
Umh.. the compiler writers don't "exploit undefined behaviour".

The compiler writers write the compiler with the expectation that the program being compiled is correct i.e. to the specification.

Now when the program fails to be correct, optimizations can result in "weird" results, which is not really incorrect since the input was incorrect.

Re:“It is never a compiler error” by revelation on Tuesday November 14, 2017 @07:46PM (#15700630)
UBSan catches undefined behavior at runtime - so by definition everything it reports was not used by Clang to fold your program to a constant.
Re:“It is never a compiler error” by gpderetta on Tuesday November 14, 2017 @07:51PM (#15700660)
When the sanitizer is on, the optimizer (modulo bugs of course) does not remove the checks.
Re:“It is never a compiler error” by kllrnohj on Tuesday November 14, 2017 @07:59PM (#15700708)
> when I can narrow it down to a single offending function, that gets optnone slapped on it and done

If you're able to narrow it down that far why not then fix your bug?

Re:“It is never a compiler error” by revelation on Tuesday November 14, 2017 @08:10PM (#15700748)
Well, the last case I can recall where I had to resort to this used inline NEON assembly for what is essentially a fancy 2D memcpy. With optimization on textures were corrupted. I spent an hour diffing objdump output and got nowhere.

Essentially, it was not worth more of my time to figure out how aggressive optimizations were breaking the parts of a memory bandwidth constrained function that aren't worth optimizing in the first place.

For GCC inline assembly means "take your grubby hands off". For clang it's essentially an invitation to do whatever.

Re:“It is never a compiler error” by kllrnohj on Wednesday November 15, 2017 @12:50PM (#15705817)
> For GCC inline assembly means "take your grubby hands off". For clang it's essentially an invitation to do whatever.

That's not true at all. Clang respects the clobbers entry on inline assembly and does not do whatever it wants. See GCC's documentation on it: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobber...

Based off of your description you probably just needed to add the "memory" clobber to tell the compiler that any registers it might have kept are no longer valid.