News for hackers. Stuff that matters.
Re:ARM or x86? ISA Doesn’t Matter by ip26 on Sunday May 14, 2023 @05:47PM (#35941616)
It's a trade-off between problems. Variable length instructions are not as trivial to decode wide, so you need more cleverness here. However, fixed length instructions decrease code density, which asks more of the instruction cache. Note Zen4 has a 32 KB L1 instruction cache while the M1 has a 192 KB L1 instruction cache, requiring extra cleverness here instead to handle the higher latency and area. Meanwhile, micro-op caches hide both problems.

The are ripple effects to consider as well. The large L1 caches of M1 (320 KB total) put capacity pressure on L2, towards larger sizes and/or away from inclusive policy. See the 12MB shared L2. Meanwhile, the narrower decode of Zen4 puts pressure on things like branch prediction accuracy & mispredict correction latency - if you predicted the wrong codepath, you can't catch up as quickly. See the large branch predictors on Zen4.

Re:ARM or x86? ISA Doesn’t Matter by cesarb on Sunday May 14, 2023 @06:11PM (#35941796)
> Note Zen4 has a 32 KB L1 instruction cache while the M1 has a 192 KB L1 instruction cache, requiring extra cleverness here instead to handle the higher latency and area.

There's another factor here: to have a low latency, the L1 cache has to be indexed by the bits which don't change when translating from virtual addresses to physical addresses. That makes it harder to have a larger low-latency L1 cache when the native page size is 4KiB (AMD/Intel) instead of 16KiB (Apple M1/M2).

That is, most of the "cleverness" allowing for a larger L1 instruction cache is simply a larger page size.

Re:ARM or x86? ISA Doesn’t Matter by mjevans on Sunday May 14, 2023 @06:46PM (#35942046)
If the instructions are on average 3-4x less dense (take about that much more space) than the trade-off in associativity granularity and corresponding increase in cache size are logical. The logical management circuits would be around the same size, though the number of memory cells and corresponding cost in silicon, power / thermal, and signal propagation / layout issues remain.
Re:ARM or x86? ISA Doesn’t Matter by rep_lodsb on Sunday May 14, 2023 @09:14PM (#35942902)
Since its instructions have to be aligned, ARM would also have only 12 usable address bits per page.
Re:ARM or x86? ISA Doesn’t Matter by brigade on Sunday May 14, 2023 @09:52PM (#35943074)
Pretty sure everyone uses a VIPT L1, what evidence is there that it's PIPT?

(also what do you believe happens with 4k pages on M1, since it does support those, or why did the A7 have larger caches than Zen4 a decade ago, which was well before 16k pages)

Re:ARM or x86? ISA Doesn’t Matter by codedokode on Sunday May 14, 2023 @06:27PM (#35941916)
x86 also uses 2-address instructions which means that you often need to use moves between registers (additional instructions), example: [1]. ARM uses 3-address instructions.

Also, x86 code is compact, but not as compact as in era of 8080 [2] - here addition and multiplication require 3 bytes each, 6 bytes total. To my surprise, ARM has an add-multiply instruction and it uses just 4 bytes (instead of 8) [3].

And RISC-V uses 6 bytes because of shortened instruction for addition [4]

Of course, this simple function cannot be a replacement for proper analysis, but it seems that x86 code is not significantly denser.

Also to my great disappoitment none of those CPUs has checked overflow for arithmetic operation.

[1] https://godbolt.org/z/jsoccE5jv

[2] https://godbolt.org/z/jTMs1MEzh

[3] https://godbolt.org/z/nGb8qKcxe

[4] https://godbolt.org/z/x9c115crY

Re:ARM or x86? ISA Doesn’t Matter by BeeOnRope on Sunday May 14, 2023 @07:03PM (#35942145)
x86-64 is not even a particularly compact encoding even compared to contemporary fixed length encodings. The inherent advantage of variable length encoding is largely cancelled out by wasted encoding space for legacy cruft.

Aarch64 is roughly on par for encoding density.

Re:ARM or x86? ISA Doesn’t Matter by kazinator on Sunday May 14, 2023 @07:23PM (#35942257)
It's incredible how large the code is when you compile some trivial piece of C with GCC for x86 or amd64, compared to one's memory of working with C on 386 boxes.

What? I just wrote a simple main and a couple of small helper functions. How can we be past 20 kB of .text section?

Re:ARM or x86? ISA Doesn’t Matter by throwaway2037 on Monday May 15, 2023 @02:49AM (#35944594)
Can you share a sample with Godbolt? It might foster some good discussion here.
Re:ARM or x86? ISA Doesn’t Matter by userbinator on Sunday May 14, 2023 @07:24PM (#35942259)
Of course, this simple function cannot be a replacement for proper analysis, but it seems that x86 code is not significantly denser.

Look at the demoscene for an example of dense x86 code, especially in the sub-1K categories. They routinely achieve code densities for x86 that no compilers I know of can get close to, and AFAIK I have not seen the same happen with ARM, nor MIPS or any other well-known RISC.

3-address instructions improve code density only in situations where both source operands are needed later.

Re:ARM or x86? ISA Doesn’t Matter by brigade on Sunday May 14, 2023 @07:59PM (#35942511)
What percentage of code that a CPU will run over its lifetime is demoscene code? Heck, even of just simple hand-optimized assembly a CPU is likely to encounter, what percentage is not vector code? Because x86 vector code typically averages more than 4 bytes per instruction, and I have a suspicion that at least five nines of scalar instructions a CPU executes were generated by a compiler.
Re:ARM or x86? ISA Doesn’t Matter by userbinator on Sunday May 14, 2023 @10:02PM (#35943117)
I mention that to point out the code density limits of x86 are much higher than what measurements using compiler output will show, while on the other hand I haven't seen the same for ARM and suspect that one can't really get much better than compiler output for it or other RISCs.

Having had to patch binaries on multiple occasions by inserting instructions, it is definitely not hard to do so for x86 as one can easily find "slack" that the compiler left behind[1], but I once had to do it for a MIPS binary, and it was definitely not easy to squeeze in the few extra instructions I needed inline; I ended up having to detour to another area with jumps instead.

Here's an old paper where the authors tried to optimise for code density manually, and you can consistently see x86 beating ARM and MIPS:

https://web.eece.maine.edu/~vweaver/papers/iccd09/iccd09_den...

[1] See https://news.ycombinator.com/item?id=15720923 for an example.

Re:ARM or x86? ISA Doesn’t Matter by brigade on Monday May 15, 2023 @07:31AM (#35946540)
Yeah if code size is the only metric you care about. The second link is an excellent example of code you do not want a compiler to generate by default. Like, besides all the well-known performance pitfalls of microcoded instructions, jeczx is unfusable on I think all relevant CPUs, so it’s both an additional uop and an additional cycle of latency over a tst/jz sequence.
Re:ARM or x86? ISA Doesn’t Matter by saagarjha on Tuesday May 16, 2023 @05:28AM (#35959447)
Five nines is really high. I don't think this is true, probably because language runtimes have hot paths that are typically implemented by hand. If we drop "scalar" then of course you're dropping below even two nines because of the implementation of str* and mem*.
Re:ARM or x86? ISA Doesn’t Matter by rwmj on Monday May 15, 2023 @05:03AM (#35945428)
They use a lot of the original 8087 FP instructions (very dense because it's basically a bytecode stack machine). Plus tricks like deriving constants from bytes in the code segment. And you can assume the contents of registers when you enter the code.

Pyrit, a ray tracing demo in 256 bytes, does all of that: https://www.pouet.net/prod.php?which=78045

You probably wouldn't want your general purpose compiler doing this sort of thing! The resulting code would be suboptimal and fragile.

Re:ARM or x86? ISA Doesn’t Matter by drpixie on Sunday May 14, 2023 @10:42PM (#35943355)
> Also to my great disappoitment none of those CPUs has checked overflow for arithmetic operation.

Yes - we're stuck with the historically convenient collection of operations and types. Our CPUs are amazingly fast but very little smarter than decades ago.

Backwards-compatibility pretty much locks us into the same logical space. At least when people were designing new and amazing computers using actual wire (!) they felt free to implement what they thought would be useful, not just copy the existing model.

Re:ARM or x86? ISA Doesn’t Matter by renox on Monday May 15, 2023 @04:19AM (#35945188)
The only CPU I know which had integer overflow detection was MIPS. RISC V is somewhat based on MIPS but they removed this part :-(
Re:ARM or x86? ISA Doesn’t Matter by smallstepforman on Monday May 15, 2023 @06:21AM (#35945968)
6502/6510 had integer add overflow …
Re:ARM or x86? ISA Doesn’t Matter by Findecanor on Monday May 15, 2023 @10:14AM (#35948480)
Indeed it is weird to see the trend in hardware to remove overflow checking when the trend in programming languages is to have more.

While MIPS can trap on overflow, most architectures can at least set flags for conditional branch. PowerPC OR's the overflow flag into another flag, so you can choose to check that flag for overflow first after a block of calculations.

I really want Mill to become successful. It has "NaR" (Not A Result) - like NaN but for integers. For every arithmetic instruction, there is has a variant that can set NaR. Like a NaN it is carried through the data-flow, and there's a trap first when it reaches a store or conditional instruction.

Re:ARM or x86? ISA Doesn’t Matter by saagarjha on Tuesday May 16, 2023 @05:30AM (#35959464)
Modern CPUs are plenty fast at doing overflow checks in software.
Re:ARM or x86? ISA Doesn’t Matter by Someone on Monday May 15, 2023 @03:01AM (#35944662)
> To my surprise, ARM has an add-multiply instruction

Fused multiply-add is (https://en.m.wikipedia.org/wiki/Multiply%E2%80%93accumulate_...) is extremely useful and part of IEEE754-2008.

Because of that, it would surprise me if a modern CPU with floating point wouldn’t have it.

> And RISC-V uses 6 bytes because of shortened instruction for addition

That Wikipedia page claims RISC-V has fused multiply-add, so that may be compiler inefficiency. An answer to https://stackoverflow.com/questions/57248403/why-fused-multi... says gcc can generate it at -O3)

Re:ARM or x86? ISA Doesn’t Matter by Findecanor on Monday May 15, 2023 @07:36AM (#35946573)
I think the parent poster might have been referring to the integer multiply-add instruction.

In fact, 64-bit ARM does not have a regular MUL instruction. "MUL" is an alias to a MADD with the zero register as addend.

Flipping one parameter bit in the instruction word changes a MADD into a MSUB. That instruction is used to compute the remainder after a division. Bigger ARM cores (such as Apple M1) perform macro-op fusion of DIV and MSUB to deliver both quotient and remainder in one operation.

Anyway. I thought that was pretty clever by ARM's designers.

Re:ARM or x86? ISA Doesn’t Matter by pclmulqdq on Sunday May 14, 2023 @05:52PM (#35941660)
ARM used to find itself on the wrong side of this tradeoff in the era of 4-wide x86 decode units and 4-6 wide ARM decoders. They lost too much perf to cache size for the decoder width to make up for it.

It's unclear to me if they will pull ahead on the perf/area game with the era of 8-wide x86 decoders coming.

Re:ARM or x86? ISA Doesn’t Matter by crest on Sunday May 14, 2023 @10:33PM (#35943277)
The M1 uses 16kiB pages to reduce TLB pressure and get more non-aliasing cache tag bits.
Re:ARM or x86? ISA Doesn’t Matter by sliken on Tuesday May 16, 2023 @05:51PM (#35968238)
Can use 16k pages. M1 also can use 4k pages.
Re:ARM or x86? ISA Doesn’t Matter by audunw on Monday May 15, 2023 @07:48AM (#35946686)
> However, fixed length instructions decrease code density, which asks more of the instruction cache.

The problem is that x86 has a lot of legacy that wastes the potential gain of variable length encoding. If I remember correctly, the code density of 64-bit x86 isn't really significantly better than 64-bit ARM, despite ARM dropping support for Thumb.

RISC-V is going to be interesting in that regard because with the latest revisions of the compressed instruction format, it can potentially beat both ARM and x86 on real-world code density, while still being easier to decode.

Re:ARM or x86? ISA Doesn’t Matter by nayuki on Monday May 15, 2023 @02:39PM (#35952681)
> Variable length instructions are not as trivial to decode wide, so you need more cleverness here. However, fixed length instructions decrease code density

This is true in theory but I question how much it applies to current CPUs. A problem I see is that the x86 instruction set was designed in the 1970s with corresponding assumptions about instruction frequency, speed of memory, number of CPU cycles, pipelining (or lack thereof), addressing modes, etc. Even if the variable-length encoding was close to optimal back then, there's no way it's remotely optimal given all the extensions to the instruction set, the change from hand-coded assembly to compiler-generated code, and many more seismic shifts in the ISA. So I think x86's variable-length instructions now have all the downsides of complicated decoding as well as the downsides of suboptimal code density. But you also can't reshuffle all the instructions and addressing modes without breaking backward compatibility.