News for hackers. Stuff that matters.
Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @04:39AM (#21934029)
The riscv people missed some serious opportunities for fundamental improvements, probably the last such opportunity in this generation.

Perhaps the biggest was that the comparison instruction should have had the condition being looked for in the instruction, and put the result in register 0 as a 0 or ~0 (= -1). Register renaming can then carry it through pipelines. Subsequent instructions may AND register 0 with other values, or subtract it from them, avoiding frequently mispredicted branches.

Re:RISC-V Stumbling Blocks by rwmj on Thursday January 02, 2020 @05:58AM (#21934401)
RISC-V is meant to be a simple, unsurprising, boring architecture, with any clever stuff (such as macro op fusion) being done at the microarchitecture level. It's not a project for researching new architectures.
Re:RISC-V Stumbling Blocks by beefhash on Thursday January 02, 2020 @06:14AM (#21934480)
I really wish it was just one tiny less bit simple. The RISC-V spec doesn't specify that multiplication instructions (if implemented) need to be constant-time. Which means RISC-V cores will need stupid hacks for cryptography like these: https://bearssl.org/ctmul.html
Re:RISC-V Stumbling Blocks by 0xcde4c3db on Thursday January 02, 2020 @09:46AM (#21935707)
A big part of the RISC-V story is the idea that microarchitecture decisions belong to the implementation and not the ISA specification. Specifying that multiplication must be constant-time would effectively be specifying that area-optimized cores aren't allowed to provide the multiplication instructions.
Re:RISC-V Stumbling Blocks by anoncake on Thursday January 02, 2020 @10:30AM (#21936146)
Couldn't this be solved by having constant-time and regular multiplication instructions?
Re:RISC-V Stumbling Blocks by justincormack on Thursday January 02, 2020 @11:04AM (#21936482)
Arm 8.4 has a constant time flag that makes most instructions constant time, without changing the instruction set. You could definitely add this to risc-v. Or just make a constant time implementation.
Re:RISC-V Stumbling Blocks by 0xcde4c3db on Thursday January 02, 2020 @10:33AM (#21936175)
Sure, but the constant-time instructions would presumably be defined in an extension along with other constant-time or crypto-optimized instructions.
Re:RISC-V Stumbling Blocks by rwmj on Thursday January 02, 2020 @06:39AM (#21934576)
It could be added in the future as a simple extension. (In fact I found at least two proposed crypto extensions with a simple search although neither mentions constant-time ops from a quick scan). There's a considerable penalty for all implementations if every multiply implementation has to be constant time.
Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @06:51AM (#21934629)
That is another problem.

The "optional extensions" have all the useful instructions in them, but typically just a couple of the instructions in each extension would provide most of its value.

So, I don't get popcount because it's in the bitmanip extension along with 10M transistors' worth of clever stuff I don't need. Similarly, each of the other extension sets.

It would be much more useful to define slices through all the defined ones, so you get either the absolute minimum core instructions, or add just the coremost of each extension set, or a more comfortable subset of each, or half of each, spiraling outward as transistor budget grows.

Re:RISC-V Stumbling Blocks by rwmj on Thursday January 02, 2020 @07:10AM (#21934743)
10 million transistors is a tenth of a square millimeter on current nodes, but that aside you really wouldn't want such a complicated system of "sub-extensions" because it would be a nightmare to write software for. Either the software would only work for very specific sub-extension mixes, or would require layers of ifuncs to deal with all combinations of missing sub-extension features.

The current system of fairly granular extensions, a useful base architecture, and profiles defining what extensions are guaranteed for particular application classes (eg "Unix server") is a nice compromise between extensibility and software/distro complexity.

Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @06:23AM (#21934508)
It is supposed to be the basis for a lot of future work, incorporating what we have learned thus far. But it doesn't, much. It seems not different from anything from the '80s, when we knew less.

If it is supposed to be simple, then great! Actually make it simple, instead of copying old complexities.

Re:RISC-V Stumbling Blocks by Symmetry on Thursday January 02, 2020 @09:21AM (#21935507)
If you're an academic doing research into new techniques for branch prediction or some other topic it can be very useful to have well tested designs as a starting point you can modify to try out your idea which are starting from a very basic place.

It isn't architecturally innovative, isn't meant to be, and isn't a good place to start if you're thinking about doing architectural innovation. A lot of people are talking up RISC-V like it'll solve every problem but there are many problems in both academia and the real world where it represents a solution.

Re:RISC-V Stumbling Blocks by rwmj on Thursday January 02, 2020 @06:36AM (#21934558)
What "old complexities" are you talking about? It's a very simple and clean architecture.
Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @06:41AM (#21934583)
No: it is just very familiar. It is like architectures we are used to, that we got used to because old designs were like that.
Re:RISC-V Stumbling Blocks by thethirdone on Thursday January 02, 2020 @05:16AM (#21934173)
I am not quite understanding the missed improvement you are talking about.

Are you saying that instead of (or in addition to) "branch if equal", "branch if less than", etc there should be a "set if equal", "set if less than", etc? In case you are, there is a "set if less than" instruction. The floating point extension does have a "set if equal" instruction despite there not being an integer equivalent.

Alternatively if you are complaining about 1 representing True rather than ~0, I don't think that matters much from a technical POV.

Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @06:08AM (#21934449)
The condition codes from comparison operations have always been a problem for out-of-order execution units. Feeding the result directly to the register bank sidesteps the problem. Conditional-move instructions have also been a problem, which this also sidesteps. Branch-register-zero and -nonzero instructions complete the set.

The extra step to get a comparison result into a register, and another to extend it to the whole register, is an inefficiency I would prefer to leave behind.

Re:RISC-V Stumbling Blocks by jleahy on Thursday January 02, 2020 @06:28AM (#21934525)
It's a single instruction to then subtract 1 from the result of SLT/SLTI, which then gives you 0xffffffff or 0x00000000 rather than 0 or 1, which you can use as a mask.

RISC-V was designed to be a good target for a C compiler, given that's what people almost always need. Compilers frequently see code line `int x = (a > b)` and SLT is perfect for that. Compilers don't do clever masking tricks to avoid branches, in general (there are a few hard-coded tricks like divide by signed constant).

Re:RISC-V Stumbling Blocks by bonzini on Thursday January 02, 2020 @08:14AM (#21935071)
> Compilers don't do clever masking tricks to avoid branches, in general

They actually do a lot of such tricks. Check out the GCC sources, for example tree-ssa-phiopt.c[1] and ifcvt.c[2]

[1] https://github.com/gcc-mirror/gcc/blob/master/gcc/tree-ssa-p...

[2] https://github.com/gcc-mirror/gcc/blob/master/gcc/ifcvt.c

Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @01:51PM (#21938393)
They do it a bit, but they leave a very great deal on the table. Clang will convert

  a = (b&m)|(c&~m)
into cmov, but gcc won't.
Re:RISC-V Stumbling Blocks by jleahy on Thursday January 02, 2020 @09:14AM (#21935466)
Well I was thinking of ifcvt when I mentioned a few special cases. Particularly things like minmax and abs are very specific.
Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @06:37AM (#21934563)
In other words, two more instructions, instead of zero.

That compilers fail to do clever masking tricks to avoid branches frequently results in 2x slower programs.

Re:RISC-V Stumbling Blocks by avianes on Thursday January 02, 2020 @07:53AM (#21934947)
No, as jleahy said, it cost a single instruction to transform 0 to 0b00..00 and 1 to 0b11..11 - SUB X, $0, X.

And your proposal - using the zero register as a flag register - breaks way more micro-optimizations (like the simple ones described in "The RISC-V Reader" book) than it allows

Re:RISC-V Stumbling Blocks by thethirdone on Thursday January 02, 2020 @06:42AM (#21934585)
> The condition codes from comparison operations have always been a problem for out-of-order execution units.

RISC-V doesn't have condition codes.

> The extra step to get a comparison result into a register, and another to extend it to the whole register, is an inefficiency I would prefer to leave behind.

Can you provide RISC-V assembly that demonstrates this problem? Common branching patterns in RISC-V are all a single instruction.

The core branching comparison instructions:

`a >= b` maps to `bge a, b, label`

`a = b` maps to `beq a, b, label`

`a != b` maps to `bne a, b, label`

`a < b` maps to `blt a, b, label`

Comparisons with 0 just use the x0 register and for <= and > just flip the order of the operands.

Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @07:06AM (#21934718)
I know it does not have condition codes. Instead, it has a mess of very complex compare-and-branch instructions.
Re:RISC-V Stumbling Blocks by Taniwha on Thursday January 02, 2020 @07:42AM (#21934899)
It has exactly 6 = != < >= and signed < >= - hardly "a mess" or particularly "complex"
Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @08:09AM (#21935040)
These do not seem, to you, complex instructions?

I boggle.

Re:RISC-V Stumbling Blocks by pjc50 on Thursday January 02, 2020 @10:17AM (#21935996)
Pretty much all architectures have those as a natural consequence of the way 2's complement arithmetic works? Even the tiny 6502 spends eight of its 256 opcodes on eight different branch-if-comparison opcodes.
Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @01:39PM (#21938252)
The RISC-V compare-and-branch instructions are objectively a great deal more complex than the 6502 instructions.
Re:RISC-V Stumbling Blocks by Taniwha on Thursday January 02, 2020 @03:01PM (#21939181)
No, compare and branch is kind of the expected instruction in an architecture that has no condition codes (mips has compare with 0 but essentially the same 6) - compare works well in an out of order pipe

If I was going to change anything I'd add compare eq/ne with an immediate

Re:RISC-V Stumbling Blocks by Annatar on Thursday January 02, 2020 @08:40AM (#21935235)
That is insane.
Re:RISC-V Stumbling Blocks by jabl on Thursday January 02, 2020 @08:25AM (#21935132)
You're saying that you want an isa where a branch is done something like

Cmp rx ry rz # compare rx ry, write result to rz

Jmple rz, ra, imm # jump +- ra+imm if rz < 0

Which would avoid the use of a implicit condition register, as well as the short offsets that a combined compare+jump suffers from?

Re:RISC-V Stumbling Blocks by ncmncm on Thursday January 02, 2020 @01:12PM (#21937991)
No. That is close to what existing chips do.

  r31 = (rx<=ry)? ~0:0
(or <, etc.), result in r31 implicitly. Followed by

  pc = r31? pc + n : pc
i.e. a single conditional branch instruction, for all conditions; or

  rx &= r31
effectively a conditional move instruction, or

  rx -= r31
effectively a conditional increment.

I.e., actually reduced, but more powerful. (Implicit destination followed by mov is free, because of register renaming.)

Decoding the RISC-V compare-and-branch instruction is a whole project.

Re:RISC-V Stumbling Blocks by avianes on Friday January 03, 2020 @08:57AM (#21945827)
> (Implicit destination followed by mov is free, because of register renaming.)

There is no need for an implicit destination register, just do it explicitly and you get the same result but without any magic and with more control. Register renaming can do the same job with an implicit or explicit register destination, there is no incidence on register dependencies.

> r31 = (rx<=ry)? ~0:0

Reading your first post I know that you want to use 0 and ~0 instead of the traditional 0 and 1 as the result of condition statements. I don't think that's a particularly great idea because: - there are so many places where you need 1 instead of ~0; - translating 1 into ~0 only costs one "SUB" instruction. If this feature is required for a specific domain, it's sufficient to add an extension with this feature or - more drastically - to add a macro-operation fusion step to detect this pattern.

> Decoding the RISC-V compare-and-branch instruction is a whole project.

RISC-V encoding is made to keep decoding as simple as possible. Have you ever checked the RISC-V encoding? It looks a bit confusing at the beginning to a human but if you think about the implementation, it immediately becomes clear.

In RISC-V ISA, we can criticize: the absence of a standard SIMD extension (with a big abstract vector extension instead) or the fact that some extensions require 3 read ports on the register bank instead of 2, which can make the implementation more complex. But it's trade-offs

Re:RISC-V Stumbling Blocks by ncmncm on Friday January 03, 2020 @03:00PM (#21949714)
Where do you ever need 1 instead of -1? If you are using it to increment something, -1 is just as good. Storing a bool value in memory is rarely done, vs. using and discarding it. But in use, having a value to AND with, without need to dink around first, makes a big difference, particularly when cmov instructions are lacking.

If the cost of negating before storing got to seem excessive, a single negate-and-store-word instruction could translate, but it would be used a very great deal less than negating the 1 to make a mask.

However simple it looks to decode the thicket of compare-and-branch instructions, decoding and executing exactly one conditional branch instruction that just checks for zero will be simpler and faster.

You still have the SET family to decode, but you need them anyway.

Re:RISC-V Stumbling Blocks by Taniwha on Thursday January 02, 2020 @04:17PM (#21939933)
9 lines of verilog in my decoder, 3 more in the branch unit - about the same as for, say jalr, or add immediate