🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #659

Re: vector length CSR?

#659
From: Anton Ertl
Date: Thu, 12 Nov 2026 08:21
77 lines
3693 bytes
Thomas Koenig <tkoenig@netcologne.de> writes:
>Stephen Fuld <sfuld@alumni.cmu.edu.invalid> schrieb:
>
>> It seems the 
>> advantage of an SIMD approach is the large set of addressable registers 
>> that can be used to keep data readily available for future use.
>
>Plus, you can do wide permutes, and it can also help with reduction.
>Having mask for reducing operations also helps (or a length field).
>
>There is one other thing that VVM does not do natively: Reductions
>on commutative operators.

Associativity is key (and FP addition and multiplication are mostly
commutative, but not associative).

>Consider the implementation of a simple
>SUM intrinsic of FORTRAN.  If you write the assembly equivalent of
>
>  s = 0;
>  for (long int i=0; i<n; i++)
>    s = s + a[i];
>
>then VVM will add a loop-carried dependency.  For faster code
>on long arrays one can unroll the loop, like
>
>  tmp[0] = 0;
>  tmp[1] = 0;
>  tmp[2] = 0;
>  tmp[3] = 0;
>  n_end = (n / 4) * 4;
>  for (long int i = 0; i < n_nend; i++)
>    {
>      tmp[0] = tmp[0] + a[i];
>      tmp[1] = tmp[1] + a[i+1];
>      tmp[2] = tmp[2] + a[i+2];
>      tmp[3] = tmp[3] + a[i+3];
>    }
>  for (long int i = 0; i < n % 4; i++)
>    tmp[i] = tmp[i] + a[i+n_end];
>
>  s = tmp[0] + tmp[1] + tmp[2] + tmp[3];
>
>(hope I got this right)

You confused yourself with trying to use an increment by 1 (why?).
Better do the loop as follows:

long i;
for (i = 0; i < n+1-4; i+=4) {
  ... unrolled body ...
}
for (; i<n; i++) {
  ... plain body ...
}

Another option for the unrolled body would be:

  tmp = a[i]+b[i]+c[i]+d[i];
  r = r + tmp;

This variant does not require commutativity, but still associativity,
and it has the same loop-carried latency (on an OoO CPU).

But with SIMD, doing parallel adds like you did is a good idea, and
you can also implement my idea in addition (or unroll more) to perform
as many SIMD operations per loop-carried latency as the hardware has
resources for.  What I have seen from gcc and/or clang is that they
use more unrolling.

AFAIK VVM will not auto-vectorize your code, so reduction is another
case for which VVM does not have a story AFAICS.

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID: <2026Nov12.092155@mips.complang.tuwien.ac.at>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.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> <sgu0flpndaqltpdbft40fghmi7vk78q2hs@4ax.com> <11cq2v7$dh5b$1@dont-email.me> <8jk1fl182pskj3fsmp4ju6qf420pgl0htg@4ax.com> <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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.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: Thu, 12 Nov 2026 08:21:55 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 77
Message-ID: <2026Nov12.092155@mips.complang.tuwien.ac.at>
References: <11cpqih$acf5$1@dont-email.me> <sgu0flpndaqltpdbft40fghmi7vk78q2hs@4ax.com> <11cq2v7$dh5b$1@dont-email.me> <8jk1fl182pskj3fsmp4ju6qf420pgl0htg@4ax.com> <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>
Injection-Date: Thu, 12 Nov 2026 08:38:31 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3956764"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+AOXDyIA+GfTMS+1CsWKQB"; posting-host="6d7b862d87f7a03985454cab60ca3edf"
Cancel-Lock: sha1:Cg/wkUYrJ2Ly+DPw9XetwHgYCiY= sha256:BFTgqJYJ7bWciOIeuJL+lTcKoZdaKS01qYlDVArnfRs=
	sha1:BnzGr96yLdpklG8+m3yR5XPqHLk= sha256:qDvO4hx+wFUW0aCzNWxEp/fud9p9Lw4nUugZFzQuLP0=
X-newsreader: xrn 10.11