🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #882

Re: vector length CSR?

#882
From: Anton Ertl
Date: Sun, 22 Nov 2026 09:16
101 lines
4359 bytes
Thomas Koenig <tkoenig@netcologne.de> writes:
>gcc treats wrapping and non-wrapping differently.  Here's a function
>from gcc/tree-ssa-reassoc.cc:
>
>/* Returns true if we can reassociate operations of TYPE.
>   That is for integral or non-saturating fixed-point types, and for
>   floating point type when associative-math is enabled.  */
>
>static bool
>can_reassociate_type_p (tree type)
>{
>  if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
>      || NON_SAT_FIXED_POINT_TYPE_P (type)
>      || (flag_associative_math && FLOAT_TYPE_P (type)))
>    return true;
>  return false;
>}
>
>Reassociation is possible with -frwapv, not possible with
>normal semantics.

Let's see how well it works (gcc-12):

[~/tmp:173584] cat reassoc.c
int foo(int a, int b, int c, int d)
{
  int r=a;
  r+=b;
  r+=c;
  r+=d;
  return r;
}
[~/tmp:173585] gcc -O3 -c reassoc.c && objdump -d reassoc.o

reassoc.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <foo>:
   0:   01 f7                   add    %esi,%edi
   2:   01 d7                   add    %edx,%edi
   4:   8d 04 0f                lea    (%rdi,%rcx,1),%eax
   7:   c3                      ret
[~/tmp:173586] gcc -fwrapv -O3 -c reassoc.c && objdump -d reassoc.o

reassoc.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <foo>:
   0:   01 ca                   add    %ecx,%edx
   2:   01 f2                   add    %esi,%edx
   4:   8d 04 3a                lea    (%rdx,%rdi,1),%eax
   7:   c3                      ret

So gcc by default does not reassociate, and in the -fwrapv case
reassociates, but does so in a way that does not reduce the tree
height, but that corresponds to

c+=d;
c+=b;
return c+a;

What's the reason for that?  "Relevant" programs where the earlier
parameters are on the critical path and the later ones are not?

Let's see how clang-14 does:

[~/tmp:173587] clang -O3 -c reassoc.c && objdump -d reassoc.o

reassoc.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <foo>:
   0:   8d 04 37                lea    (%rdi,%rsi,1),%eax
   3:   01 d0                   add    %edx,%eax
   5:   01 c8                   add    %ecx,%eax
   7:   c3                      ret
[~/tmp:173588] clang -fwrapv -O3 -c reassoc.c && objdump -d reassoc.o

reassoc.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <foo>:
   0:   8d 04 37                lea    (%rdi,%rsi,1),%eax
   3:   01 d0                   add    %edx,%eax
   5:   01 c8                   add    %ecx,%eax
   7:   c3                      ret

No reassociation with either flag.

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID: <2026Nov22.101624@mips.complang.tuwien.ac.at>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11cpqih$acf5$1@dont-email.me> <11csull$1ef6e$1@dont-email.me> <2026Nov9.230104@mips.complang.tuwien.ac.at> <11cvcr4$28k7g$1@dont-email.me> <11d3npt$3mmho$1@dont-email.me> <11dn936$2ebc2$1@dont-email.me> <11dndfd$2fr9j$1@dont-email.me> <11dngb5$2he51$1@dont-email.me> <2026Nov19.203832@mips.complang.tuwien.ac.at> <11dq56a$3f094$1@dont-email.me> <11ds0io$12f8$1@dont-email.me> <11ds88c$46jd$1@dont-email.me> <11ds95m$4co4$1@dont-email.me> <11dsbag$5a43$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: vector length CSR?
Date: Sun, 22 Nov 2026 09:16:24 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 101
Message-ID: <2026Nov22.101624@mips.complang.tuwien.ac.at>
References: <11cpqih$acf5$1@dont-email.me> <11csull$1ef6e$1@dont-email.me> <2026Nov9.230104@mips.complang.tuwien.ac.at> <11cvcr4$28k7g$1@dont-email.me> <11d3npt$3mmho$1@dont-email.me> <11dn936$2ebc2$1@dont-email.me> <11dndfd$2fr9j$1@dont-email.me> <11dngb5$2he51$1@dont-email.me> <2026Nov19.203832@mips.complang.tuwien.ac.at> <11dq56a$3f094$1@dont-email.me> <11ds0io$12f8$1@dont-email.me> <11ds88c$46jd$1@dont-email.me> <11ds95m$4co4$1@dont-email.me> <11dsbag$5a43$1@dont-email.me>
Injection-Date: Sun, 22 Nov 2026 09:29:36 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="865973"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/NJI74xeI5ivz24hmTtuuq"; posting-host="f30d6f48c39d6c9405387112f2d4bed3"
Cancel-Lock: sha1:4QKTbFCaV54fOD06frJedszpFEE= sha256:22HaQvPvQCSwnWS/LJ+iG7jtMRZrxY2Oyk6lF5UoF4U=
	sha1:u6Goo2eUTXgNqL7JCej8hNMEmho= sha256:AzJXAyrzBoOzW7Gokkx50hinKiPdFXcMMeRMYmFXKnY=
X-newsreader: xrn 10.11