News for hackers. Stuff that matters.
Re:Clang's -O0 output: branch displacement and size increase by userbinator on Friday April 26, 2024 @11:19PM (#40176837)
This reminds me of fasm, the only assembler immediately coming to mind that will do multi-pass branch optimisation by default. Most other assemblers either choose the long form always unless specified explicitly as "jmps" or "jmp short" (and then complain when the target turns out to be too far away), or the short form only if the destination is known when it's encountered (backwards jump).

I've long held the opinion that O0 on all the major compilers should be considered more like an O-1 because of the glaring stupidities it leaves in its output, which almost looks like it was pessimising instead of not optimising.

This article is also only the 2nd time I've seen "relaxation" used in this context. The first was https://news.ycombinator.com/item?id=10219007 over 8 years ago.

Re:Clang's -O0 output: branch displacement and size increase by dataflow on Saturday April 27, 2024 @12:22AM (#40177209)
Are you aware of -Og? It might be what you want.
Re:Clang's -O0 output: branch displacement and size increase by usefulcat on Saturday April 27, 2024 @10:42AM (#40180316)
I believe -Og is only meaningful for gcc. IIRC it’s the same as O1 for clang.
Re:Clang's -O0 output: branch displacement and size increase by o11c on Friday April 26, 2024 @11:30PM (#40176903)
Despite the fact that you say "all the major compilers", GCC and Clang make very different decisions for each optimization level.

In particular, GCC generates fairly debuggable code at all optimization levels, so there is less motivation for -O0 in the first place.

Re:Clang's -O0 output: branch displacement and size increase by MaskRay on Sunday April 28, 2024 @12:48PM (#40189877)
There is ongoing work to improve debuggability for optimized code. https://discourse.llvm.org/t/rfc-redefine-og-o1-and-add-a-ne...

``` Mode | Execution Time | Debuggability | Compile Time O0 | 1.0000 | 1.0000 | 1.0000 Og | 0.3439 | 0.5357 | 1.8630 O1 | 0.3082 | 0.4241 | 1.7880 O2g | 0.2823 | 0.4845 | 3.0420 O2 | 0.2514 | 0.3908 | 2.9380 ```

Re:Clang's -O0 output: branch displacement and size increase by ryukoposting on Friday April 26, 2024 @11:26PM (#40176871)
I'll second this. As a firmware dev, I almost never encounter situations where O0 gets me anything in terms of asm readability/debugging that O1 didn't already give me.
Re:Clang's -O0 output: branch displacement and size increase by mati365 on Saturday April 27, 2024 @04:04AM (#40178221)
Not the only. Mine assembler (and C compiler), written in TypeScript, does the same despite being painfully slow and useless.

https://github.com/Mati365/ts-c-compiler

Re:Clang's -O0 output: branch displacement and size increase by mkup on Saturday April 27, 2024 @06:31AM (#40178871)
NASM has an option (-Ox) to specify how many passes it should take trying to optimize near jumps for short jumps. I usually specify -O9.
Re:Clang's -O0 output: branch displacement and size increase by MaskRay on Sunday April 28, 2024 @02:04PM (#40190450)
Thanks for mentioning nasm.

Both GNU assembler and LLVM integrate assembler parse and match instructions only once. hey then store an internal representation in memory and perform fixed-point iteration. The section/fragment representation gives a lot of flexibility.

In contrast, nasm parses and matches instructions multiple times depending on the optimization level. It also assigns addresses during parsing and uses an ad-hoc method for JMP/JCC instructions. The end conditions of the fixed-point iteration algorithm (global_offset_changed and stall_count) seem unconventional. -O0 does not "relax all" short jumps to near jumps.

Re:Clang's -O0 output: branch displacement and size increase by lelandfe on Saturday April 27, 2024 @12:14PM (#40181079)
How amazingly HN that someone replied in those comments to your link of a 1978 paper with an anecdote of its author from the same year.
Re:Clang's -O0 output: branch displacement and size increase by pcwalton on Saturday April 27, 2024 @05:06PM (#40183553)
> This reminds me of fasm, the only assembler immediately coming to mind that will do multi-pass branch optimisation by default. Most other assemblers either choose the long form always unless specified explicitly as "jmps" or "jmp short" (and then complain when the target turns out to be too far away), or the short form only if the destination is known when it's encountered (backwards jump).

Not true, gas will do the relaxation by default.

> This article is also only the 2nd time I've seen "relaxation" used in this context.

It's been the standard term among toolchain developers for quite a while. I remember seeing it all over the linker in 2007 when working on ARM stuff.

Re:Clang's -O0 output: branch displacement and size increase by MaskRay on Friday April 26, 2024 @09:58PM (#40176432)
Thanks for posting:)
Re:Clang's -O0 output: branch displacement and size increase by zhouzhouyi on Saturday April 27, 2024 @03:47AM (#40178143)
When I debug a program, the first thing I do is to compile with "-O0", very nice to remove "-mrelax-all" as the default for -O0, because "-mrelax-all" increases both VM size and the file size.
Re:Clang's -O0 output: branch displacement and size increase by ezekiel68 on Saturday April 27, 2024 @03:02PM (#40182508)
> people generally care less about -O0 code size.

Right. A ~5% additional increase in debug artifact size is really not a high tax.