• Re: vector length CSR?

    From Michael S@already5chosen@yahoo.com to comp.arch on Sun Nov 22 21:55:11 2026
    From Newsgroup: comp.arch

    On Sun, 22 Nov 2026 09:16:24 GMT
    anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:

    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

    Unlike clang, gcc performs no useful re-association for "open" code.
    The uselessness is independent, of presence/absence of -fwrapv
    flags. More so, it is independent of signed vs unsigned.

    gcc does re-associate in useful manner when vecorizing loops.
    Again, it performs it independently of -fwrapv or from signness.

    See examples in my post below.

    BTW, gcc will happily vectorize very short summation loops, even as
    short as 4 integers, where vectorization almost certainly would be
    pessimizing.




    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.arch on Sun Nov 22 22:04:35 2026
    From Newsgroup: comp.arch

    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
    --
    'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
    Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From Michael S@already5chosen@yahoo.com to comp.arch on Mon Nov 23 00:47:10 2026
    From Newsgroup: comp.arch

    On Sun, 22 Nov 2026 22:04:35 GMT
    anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:

    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.


    int sum4(int a, int b, int c, int d)
    {
    return a + b + c + d;
    }

    Windows ABI
    sum4: # @sum4
    # %bb.0:
    # kill: def $r9d killed $r9d
    def $r9
    # kill: def $r8d killed $r8d
    def $r8
    # kill: def $edx killed $edx
    def $rdx
    # kill: def $ecx killed $ecx
    def $rcx
    addl %edx, %ecx
    leal (%r8,%r9), %eax
    addl %ecx, %eax
    retq


    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


    In your example there is higher amount of work in the inner loop.
    I am talking about really minimal case:
    int sum4v(const int* a)
    {
    int sum = 0;
    for (int i = 0; i < 4; ++i)
    sum += a[i];
    return sum;
    }





    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From aph@aph@littlepinkcloud.invalid to comp.arch on Tue Nov 24 09:28:30 2026
    From Newsgroup: comp.arch

    David Brown <david.brown@hesbynett.no> wrote:
    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).

    No pass within GCC (and probably any other compiler) may introduce
    undefined behaviour where there was none in the program. That is
    because a later pass might detect the introduced UB and miscompile it,
    or even drop it onto the floor altogether.

    Andrew.
    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From Stephen Fuld@sfuld@alumni.cmu.edu.invalid to comp.arch on Tue Nov 24 12:12:53 2026
    From Newsgroup: comp.arch

    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
    (e-mail address disguised to prevent spam)
    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From Thomas Koenig@tkoenig@netcologne.de to comp.arch on Tue Nov 24 21:07:21 2026
    From Newsgroup: comp.arch

    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 SINDified and the size of
    the hardware SIMD registers is unknown at comile 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.
    --
    This USENET posting was made without artificial intelligence,
    artificial impertinence, artificial arrogance, artificial stupidity,
    artificial flavorings or artificial colorants.
    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From MitchAlsup@user5857@newsgrouper.org.invalid to comp.arch on Tue Nov 24 22:22:28 2026
    From Newsgroup: comp.arch

    Thomas Koenig <tkoenig@netcologne.de> posted:

    Stephen Fuld <sfuld@alumni.cmu.edu.invalid> schrieb:
    On 11/9/2026 9:08 AM, Stephen Fuld wrote:

    By the way the first v in vVM is lower case.


    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.

    My guess is that when the execution window is big enough to absorb
    the cache latency, then there is little advantage to SIMD (if any).

    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.

    Of somewhat higher importance is that the compile time SIMD-width
    is a burden on SIMD that vVM does not have. If todays vVM is 256-
    bits wide and tomorrows' is 124-bits wide--that same code is just
    as optimal today as it is tomorrow.

    But a four-lane SIMD, for example, could do the same. It would
    need a reduction step at the and, as well.

    Just a note: a 4-lane DP FP SIMD pipeline is 256-bits wide, and can
    handle {4-DP FP, 8 SP FP, 16 HP FP, 4 Dint, 8 Wint, 16 Hint, 32 Bint}.

    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).

    I see exception handling migrating from "take a trap" to "make flow
    control exception-branches" and leave the whole mess to SW. How
    much faster is {Branch-on-integer-arithmetic-anomaly Anom, label}
    than taking the trap, having trap handling setup a signal handler
    setup an exception frame on application stack, and calling signal
    handler.

    Newer exception experiments are looking to optimize TRY-THROW-CATCH
    with various SW conventions and stacked data.

    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.

    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.

    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.

    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From Thomas Koenig@tkoenig@netcologne.de to comp.arch on Wed Nov 25 06:56:43 2026
    From Newsgroup: comp.arch

    MitchAlsup <user5857@newsgrouper.org.invalid> schrieb:
    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.

    This will work for some cases, but not for _real_ high-performance applications.

    Take a high-speed JSON parser such as https://gitlab.ethz.ch/extra_projects/fastjson or simdjson. The
    shuffling, masking etc is not possible with VVM in its current
    state - everything would have to travel through memory, with
    its latencies, and that would kill performance. I also do
    not see a way to do equivalent things with VVM.

    This kind of code will probably never be emitted by
    auto-vectorization, this is for people who write assembly or
    compiler intrinsics specific to the particular architecture,
    which is just glorified assembler where the compiler does the
    register allocation.

    Once the reduction problem is solved, I think VVM will be superior
    for the vast majority of code, but some very important niches
    remain.
    --
    This USENET posting was made without artificial intelligence,
    artificial impertinence, artificial arrogance, artificial stupidity,
    artificial flavorings or artificial colorants.
    --- Synchronet 3.21e-Linux NewsLink 1.2