calling the directory…
netmeeting ils
pinging…
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
On Sun, 22 Nov 2026 09:16:24 GMT...
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
int foo(int a, int b, int c, int d)
{
int r=a;
r+=b;
r+=c;
r+=d;
return r;
}
[~/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
Unlike clang, gcc performs no useful re-association for "open" code.
BTW, gcc will happily vectorize very short summation loops, even as
short as 4 integers, where vectorization almost certainly would be >pessimizing.
Michael S <already5chosen@yahoo.com> writes:
On Sun, 22 Nov 2026 09:16:24 GMT...
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
int foo(int a, int b, int c, int d)
{
int r=a;
r+=b;
r+=c;
r+=d;
return r;
}
[~/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
Unlike clang, gcc performs no useful re-association for "open" code.
And clang does not reassociate at all in this example. Do you have an example (without vectorization) where it reassociates.
BTW, gcc will happily vectorize very short summation loops, even as
short as 4 integers, where vectorization almost certainly would be >pessimizing.
Better measure that. Last year clang surprised me by vectorizing the following loop:
#include <stddef.h>
void axpy1(float a, float *x, float *y,
float *z, float *w, float *v,
size_t n)
{
size_t i;
for (i=0; i<n; i++) {
z[i] += a*x[i];
w[i] = x[i]+y[i];
v[i] = x[i]*z[i];
}
}
v, w, and z are not restrict, so clang has to check first that they do
not overlap. Still vectorization was profitable for n=5 or so. When
the compiler is told that v, w, and z are restrict, the vectorized
variant was faster for even lower n (also for gcc).
- anton
On 21/11/2026 15:54, Thomas Koenig wrote:
David Brown <david.brown@hesbynett.no> schrieb:
On 21/11/2026 15:01, Thomas Koenig wrote:
David Brown <david.brown@hesbynett.no> schrieb:
On 20/11/2026 19:57, Thomas Koenig wrote:
Anton Ertl <anton@mips.complang.tuwien.ac.at> schrieb:
Stephen Fuld <sfuld@alumni.cmu.edu.invalid> writes:
On 11/19/2026 10:00 AM, Thomas Koenig wrote:
Stephen Fuld <sfuld@alumni.cmu.edu.invalid> schrieb:
On 11/11/2026 10:53 PM, Thomas Koenig wrote:
Stephen Fuld <sfuld@alumni.cmu.edu.invalid> schrieb:
s = 0;
for (long int i=0; i<n; i++)
s = s + a[i];
[...]
Without additional effort from the compiler user, a loop like this can
be re-arranged using commutative and associative laws and any other
information the compiler has. I would not expect the compiler to handle >>> this differently for signed or unsigned types.
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.
That surprises me, but okay. There is certainly no reason why signed integer types cannot be treated as wrapping if it results in better code (assuming, of course, you don't have "-ftrapv" or relevant sanitizer
flags in effect).
I can't speak to the hardware complexity of the implementation. Perhaps that is a limiting factor. But barring that, and the mental effort required to "reorient" your thinking, I don't see why any new clean
sheet design would use SIMD instructions. Perhaps someone can explain
why it should.
On 11/9/2026 9:08 AM, Stephen Fuld wrote:
I issued the following "challenge".
I can't speak to the hardware complexity of the implementation. Perhaps >> that is a limiting factor. But barring that, and the mental effort
required to "reorient" your thinking, I don't see why any new clean
sheet design would use SIMD instructions. Perhaps someone can explain
why it should.
The results were, at least to me, quite enlightening. I think it might
be useful to summarize these responses. I group the types of problems
that people thought SIMD was better at than VVM into three areas.
The first group is exemplified by Anton bringing up the types of
problems where memory values are reused in the vector registers. He
gave matrix multiplication as an example. Terje presented an algorithm which others indicated was competitive with the SIMD method. Note that
I am not ruling out that there may be other problems that fit into this group but aren't amenable to the kind of solution Terje presented. So I count that as worst case, a "tie". But so far, not a compelling
argument for SIMD.
The second group is "reductions" of various types. Mostly, this came
down to the question of whether the operation on the given data type
could be parallelized or "reassociated". I think we have come down to
the following:
For operations on unsigned integers, or on signed integers with certain
compile options, and operations such as MAX, VVM has the advantage as it
can parallelize them and, perhaps with some additional design work by
Mitch, combine the partial results at the end, including handling sizes
that are not a integer multiple of the amount of parallelization. This seems like a big win for VVM.
For operations on signed integers where preserving possible internal overflows, etc. are required VVM can't parallelize them (signified by specifying 1 in the number of lanes field in the VEC instruction).
But
there is still a more modest advantage for VVM in that reading the data
from memory doesn't "scrub" the cache, and the loop termination test is
a single instruction.
For operations on floating point, with some "not yet filed" stuff, VVM has the advantage in that the operation can be parallelized without loss
of precision.
So overall, for this second group, I see no argument for SIMD.
The third group, and one we really haven't discussed in this thread
beyond Thomas' first mention is wide permutations. I wasn't familiar
with the relevant capabilities in AVX512, so I had to do a little
research. While My 66000 has a permute instruction, and perhaps if
preceded by a Double instruction modifier can be wider, I don't see how
it can compete with the 512 byte wide permutation in three cycles
provided by AVX512, nor the capability to do byte wide permutes where
the source isn't required to be a 8 bit boundary. So if these kinds of operations are used often, then SIMD may be a requirement.
Of course, I welcome comments, additions, subtractions (!), etc.--
Stephen Fuld <sfuld@alumni.cmu.edu.invalid> schrieb:
On 11/9/2026 9:08 AM, Stephen Fuld wrote:
I issued the following "challenge".
I can't speak to the hardware complexity of the implementation. Perhaps >> that is a limiting factor. But barring that, and the mental effort
required to "reorient" your thinking, I don't see why any new clean
sheet design would use SIMD instructions. Perhaps someone can explain >> why it should.
The results were, at least to me, quite enlightening. I think it might
be useful to summarize these responses. I group the types of problems that people thought SIMD was better at than VVM into three areas.
The first group is exemplified by Anton bringing up the types of
problems where memory values are reused in the vector registers. He
gave matrix multiplication as an example. Terje presented an algorithm which others indicated was competitive with the SIMD method. Note that
I am not ruling out that there may be other problems that fit into this group but aren't amenable to the kind of solution Terje presented. So I count that as worst case, a "tie". But so far, not a compelling
argument for SIMD.
I'm not really sure I understand, then. VVM always has to access
memory at cache latencies (4-5 cycles); register access does not
need that, and clever algorithms can resuse that data to good effect.
The second group is "reductions" of various types. Mostly, this came
down to the question of whether the operation on the given data type
could be parallelized or "reassociated". I think we have come down to
the following:
For operations on unsigned integers, or on signed integers with certain
compile options, and operations such as MAX, VVM has the advantage as it can parallelize them and, perhaps with some additional design work by Mitch, combine the partial results at the end, including handling sizes that are not a integer multiple of the amount of parallelization. This seems like a big win for VVM.
VVM is always at an advantage when the SIMDified and the size of
the hardware SIMD registers is unknown at compile and can leave
all the loop peeling and other assorted nastiness to hardware.
But a four-lane SIMD, for example, could do the same. It would
need a reduction step at the and, as well.
For operations on signed integers where preserving possible internal overflows, etc. are required VVM can't parallelize them (signified by specifying 1 in the number of lanes field in the VEC instruction).
That is an interesting question. Today's hardware does two's
complement, so under "as if" rule a compiler could just happily do
the summation in hardware and look at the end result; intermediate
overflows will just be ignored, and as long as the end result fits,
all is in order. A Fortran compiler calculating the SUM
intrinsic could just do that. A C compiler could also do that
if it chose to (and it seems that LLVM choses to do so at least
for the a + b + c + d case).
But
there is still a more modest advantage for VVM in that reading the data from memory doesn't "scrub" the cache, and the loop termination test is
a single instruction.
For operations on floating point, with some "not yet filed" stuff, VVM has the advantage in that the operation can be parallelized without loss of precision.
So overall, for this second group, I see no argument for SIMD.
If VVM can do reductions, yes.
The third group, and one we really haven't discussed in this thread
beyond Thomas' first mention is wide permutations. I wasn't familiar
with the relevant capabilities in AVX512, so I had to do a little research. While My 66000 has a permute instruction, and perhaps if preceded by a Double instruction modifier can be wider, I don't see how
it can compete with the 512 byte wide permutation in three cycles
provided by AVX512, nor the capability to do byte wide permutes where
the source isn't required to be a 8 bit boundary. So if these kinds of operations are used often, then SIMD may be a requirement.
That is what i think, too.
Those cases are not for auto-vectorization, you would have to
write them especially with the special SIMD capabilities in mind.
Of course, I welcome comments, additions, subtractions (!), etc.
Thomas Koenig <tkoenig@netcologne.de> posted:
Stephen Fuld <sfuld@alumni.cmu.edu.invalid> schrieb:
The third group, and one we really haven't discussed in this thread
beyond Thomas' first mention is wide permutations. I wasn't familiar
with the relevant capabilities in AVX512, so I had to do a little
research. While My 66000 has a permute instruction, and perhaps if
preceded by a Double instruction modifier can be wider, I don't see how >> > it can compete with the 512 byte wide permutation in three cycles
provided by AVX512, nor the capability to do byte wide permutes where
the source isn't required to be a 8 bit boundary. So if these kinds of >> > operations are used often, then SIMD may be a requirement.
That is what i think, too.
Many times the indexes of the loaded memory provide for the kind
of permutation, so you don't need the PERM as an instruction; it
is just something that the vVM loop-to-loop analyzer needs to
figure out--once figured out {easy when constant} the LD-buffer
is routed to FUs based on the permutation.
data: api.nasa.gov (APOD, NeoWs)
| Sysop: | cequra |
|---|---|
| Location: | Grimsby United Kingdom |
| Users: | 4 |
| Nodes: | 4 (0 / 4) |
| Uptime: | 498852:57:59 |
| Calls: | 197 |
| Files: | 8,821 |
| D/L today: |
29 files (9,432K bytes) |
| Messages: | 39,038 |