by FullyFunctional · 4y ago · 0 comments
However, I do want to point out that a real issue (especially with legacy code) is the scaled address calculation with 32-bit unsigned values. Thankfully the Zba extension adds a number of instructions that help a lot, but still would require fusion to get complete parity with Arm64
For
int update(int *base, unsigned index) { return base[index]++; }
We get update:
sh2add.uw a1,a1,a0
lw a0,0(a1)
addiw a5,a0,1
sw a5,0(a1)
ret
Zba is included in the next Unix profile and will _likely_ be adopted eventually by all serious implementations.EDIT: grammar and spacing
Compiling your function with Godbolt, I get:
RISC-V (no Zba) Clang - 7 instructions - https://godbolt.org/z/7znnrzxKq
Arm64 Clang - 7 instructions - https://godbolt.org/z/Trv8scxad
Annoyingly I can't see the code size for the Arm64 case because no output is generated if I tick the "Compile to binary" option in "Output". I have to use GCC instead: RISC-V (no Zba) Clang - 20 bytes - https://godbolt.org/z/eWfPaorcj
Arm64 GCC - 24 bytes - https://godbolt.org/z/bzsPzov5h int get(int *base, unsigned index) {return base[index];}
Arm64: update:
ldr w0, [x0, w1, uxtw 2]
ret
RV64GC (vanilla): update:
slli a5,a1,32
srli a1,a5,30
add a0,a0,a1
lw a0,0(a0)
ret
RV64GC+Zba: update:
sh2add.uw a0,a1,a0
lw a0,0(a0)
ret
Arm64 is able to do some indexed loads in a single instruction that might take two in RISC-V w/Zba (and up to 4+ in regular RISC-V). However, calling that a win for Arm64 is not so clear as the more complicated addressing modes could become a critical timing path and/or require an extra pipeline stage. However, as a first approximation, for a superscalar dynamically scheduled implementation, fewer ops is better so I would say it's a slight win.I don't understand the obsession with bytes. 25% fewer bytes has only very marginally impact on a high-performance implementation and the variable length encoding has some horrendous complications (which is probably why Arm64 _dropped_ variable length instructions). Including compressed instruction in the Unix profile was the biggest mistake RISC-V did and I'll die on that hill.
ADD: Don't forget that every 32-bit instruction is currently wasting the lower two bits to allow for compressed, thus any gain from compress must be offset by the 6.25% tax that is forced upon it.
If you use a simple int for index:
slli a1,a1,2
add a0,a0,a1
lw a0,0(a0)
And isolating this code in a small function puts constraints on register allocation, but if we remove this constraint then we can write: slli a1,a1,2
add a1,a1,a0
lw a1,0(a1)
Which is very suitable for macro-op fusion and C extension> Including compressed instruction in the Unix profile was the biggest mistake RISC-V did and I'll die on that hill.
This is so wrong. The C extension is one of the great strengths of RISC-V, it is easy to decode, very suitable for macro-op fusion, and it gives a huge boost in code density
The sins of the original RISC-V was spending too much time looking at RV32 and not realizing how big a problem this is in practice. Zba (slipped in as it’s not really “bit manipulation”) fixes the worst of this.
ADD: The problem in this HN thread is the same reason we got compressed it in the first place. The vast majority of people aren’t doing high performance wide implementation so the true cost isn’t widely appreciated. The people holding the decision power certainly didn’t understand it. I really think you have to live it to understand it.
Instruction cache doesn't come for free, and is usually pretty small on most shipping processors. It's not a big deal for smaller benchmarks, but in real-world programs this can become a problem.
ADD: had RISC-V just disallowed instructions from spanning cache lines and disallowing jumping into the middle of instructions then almost all of the issues would have gone away. Sigh.
On the one hand we have byte count, with its obvious effect on cache space used. But to those of us who don't know, why is instruction count so important?
There's macro-op fusion, which admittedly would burn transistors that could be used for other things. Could you elaborate why it's not sufficient?
And then the fact that modern x86 does the opposite to macro-op fusion, by actually splitting up CISC instructions into micro-ops. Why is it so bad if they were more micro-ops to start with, if Intel chooses to do this?