Article View: comp.arch
Article #777Re: Large constants in RISC
From: BGB
Date: Mon, 16 Nov 2026 10:49
Date: Mon, 16 Nov 2026 10:49
340 lines
12899 bytes
12899 bytes
On 11/16/2026 1:24 AM, Anton Ertl wrote:
> BGB <cr88192@gmail.com> writes:
>> On 11/15/2026 10:08 AM, Anton Ertl wrote:
>>> The nice thing about the load is that its address is usually available
>>> right away. So the load operation is typically performed as soon as
>>> possible, and for a sequence
>>>
>>> load r1, d(rct)
>>> add r3=r1+r2
>>>
>>> where rct is the register pointing to the constant table, the result
>>> may be ready in r1 before r2 is ready.
>>>
>>> The bottom line is that this load typically does not cost latency.
>>>
>>
>> If loads have a 3 or 4 cycle latency and are placed directly before the
>> ALU op, on an in-order machine, one needs to pay for it...
>
> Sure. That's why you use compiler instruction scheduling for in-order
> machines. The address is available from at least the start of the
> function, and the load is known not to produce an exception, so the
> load can be scheduled up to the start of the function if desired.
> This would cost a register until the last use of the constant, but
> would save loads if the constant is used several times. In any case,
> the compiler can schedule the constant load as is convenient, and the
> only cost is the opportunity cost of not being able to use the
> instruction slot and load unit for something else, and the cost of
> occupying the register from the load to the last use (another
> opportunity cost).
>
OK.
>> Even on OoO machines (like x86), having a load directly followed by an
>> operation does often still have a visibly higher latency cost than if
>> they are spread out.
>
> Often? How often? Evidence?
>
> In any case, for loading a constant, scheduling the load earlier can
> be done without problem, as discussed above.
>
Had noted this behavior in some past benchmarks.
Saw the effect most visibly on a AMD FX / Piledriver based CPU though
with some ASM code. Reordering the instructions to put gaps between
loads and arithmetic a performance advantage, as did avoiding Load-Op
instruction forms in favor of separate load and op.
Had found it a little curious at the time. Like an amusing bit of trivia.
Along with how epic slow its integer division was.
Weirdly, my own CPU design seems to show many similar properties to the
Piledriver I had in terms of how optimization strategies work.
Though, granted, most of the time I am more operating at the level of
optimizing C code; which does somewhat obscure what might be going on at
the ISA level.
Well, people are free to disagree here...
>> Where:
>> Load -> ALU
>> Seemingly a lot cheaper than:
>> Load -> CMP -> Jcc
>
> If the branch is predicted correctly (the usual case), the cost of the
> latter is just that the instruction slots and functional units are
> occupied. And the difference from having the constant in the CMP
> instrction is just that the load unit is occupied, the other costs are
> the same.
>
OK.
To me it seems there was a difference, but I haven't fully figured out
what is going on exactly.
>> This case does appear to be a place where I am seeing a difference
>> between my current PC's Xen+ CPU and a Core i7 though, as the "Load ->
>> CMP -> Jcc" path appears to be cheaper on the i7 vs Xen+; but with the
>> relative cost of the 'Jcc' in isolation being higher on the i7.
>
> Unless you have compared these sequences in a very large variety of
> settings and done a statistic over the results to come to this
> conclusion, it is likely that you are seeing some other effect, rather
> than anything specific to these instruction sequences.
>
> Given that you write "a Core i7", as if this marketing name had any
> technical meaning, I doubt that you have done such a well-founded
> empirical evaluation. The "Core i7" name has been given to about 11
> different basic CPU designs, some of which contain cores with two
> different microarchitectures; and the same 11 CPU designs have also
> been given other names, such as "Core i5", "Core i3", "Xeon W", "Xeon
> E", "Pentium", or "Celeron"; not all names were used for all CPU
> designs, but in any case these marketing names don't tell us anything
> useful.
>
OK, more specifically the CPU was a "Kaby Lake / 8650U".
Intel just sort of plasters "Core i7" on everything, so presumably this
was meant as the main relevant detail, rather than that of it being a
Kaby-Lake based CPU from ~ 2017.
It came in a laptop, and I ran some of my benchmarks, and noted that
behaved differently than the "Ryzen 7 2700X" in my main PC on said
benchmarks (*).
I had to go fetch and turn on the laptop to see which specific CPU it
had though in this case.
*: Mostly, things like my RP2 decompressor being slower than LZ4 on the
laptop, but both nearly the same speed on my desktop.
And, trying some other variations on the LZ compressors using different
match decoding strategies.
The relative difference in branch-cost behavior seemed like the most
obvious possible explanation for the speed differences:
The RP2 decoder I was testing works primarily by a single load followed by:
tag=*(u64 *)cs;
if(!(tag&1))
{ cs+=2; mr=(tag>>1)&7; ml=((tag>>4)&7)+3; md=(tag>>7)&511; }
else if(!(tag&2))
{ ... }
else if(!(tag&4))
{ ... }
*(u64 *)ct=*(u64 *)cs;
ct+=mr; cs+=mr;
memlzcpyf(ct, ct-md, ml);
And LZ4 is more like:
tag=*cs++;
mr=(tag>>4)&15;
ml=(tag&15)+4;
if(mr==15)
{
v=*cs++; mr+=v;
while(v==255)
{ v=*cs++; mr+=v; }
}
memlzcpyf(ct, cs, mr);
ct+=mr; cs+=mr;
if(ml==19)
{
v=*cs++; ml+=v;
while(v==255)
{ v=*cs++; ml+=v; }
}
md=*(u16 *)cs;
cs+=2;
memlzcpyf(ct, ct-md, ml);
But, there is presumably some reason why the latter approach would be
faster on one CPU (Intel based), roughly same speed on another (AMD
based), and the former faster on my custom ISA.
Well, and the observation that the laptop doesn't really seem to like
code based on a lot of if/else decision trees.
Say (scaffolding but omitting logic for brevity):
tg=*(u32 *)cs;
if(!(tg&0x01))
{ ... }
else if(!(tg&0x02))
{
if(!(tg&0x0C))
{ ... }
else if(!(tg&0x04))
{ ... }
else if(!(tg&0x08))
{ ... }
else { ... }
}else if(!(tg&0x04))
{
if(!(tg&0x08)) { ... }
else { ... }
}else if(!(tg&0x08))
{
if(!(tg&0x10)) { ... }
else { ... }
}else if(!(tg&0x10))
{
if(!(tg&0x20)) { ... }
else { ... }
}
Yet, in some cases, this sort of thing is seemingly the faster strategy.
And, on other hardware, not so much.
Well, as opposed to all of the code having the same relative performance
rankings across target machines (with any per-machine differences
disappearing into the noise).
> By contrast, "Zen+" is a specific CPU design.
>
OK, but yeah, I guess if I just said Ryzen, it would be a little vague,
but yeah I guess I see the point.
I didn't really expect there would be that much difference between one
Intel CPU and another though, since it seems like they would mostly form
a more homogeneous landscape in this area.
Well, like, say, as opposed to AMD moving between microarchitectures
with more notable differences, Intel just sort of went from NetBurst to
Core and was like, "OK, everything is good now."
Then mostly tweaking the process node and other things.
Granted, I tend not to use as much Intel hardware.
>>> Concerning cache misses: If the code is executed often, the constant
>>> will be in the (L1) D-Cache. If there is a cache miss, the code has
>>> not been executed recently. Bottom line: Cache misses are rare,
>>> because caches work.
>>>
>>
>> However, the D$ puts it in conflict with whatever other data the program
>> is using, and D$ tends to be a much more volatile space than I$ IME.
>>
>> So, if the constants are inline, they are almost always going to hit if
>> the loop is already warm or similar. For D$, one is more at the mercy of
>> whether some of the working data knocked it out of the cache.
>
> With a typical 8-way or 12-way set-associative D-cache with pseudo-LRU
> replacement, and the constant being accessed in a hot loop, it would
> need multiple accesses to the same set between the accesses to the
> constant in order to evict the constant. Either the constant is not
> used that often, or it will stay in the D-cache.
>
> Of course, there is still the cost that the other data does not have
> that cache line available for its purposes. Nothing is totally free.
>
Does make me wonder what is typical in desktop chips.
Not much I had really seen seems to mention this.
Would have figured probably 4-way given going much more lands in
diminishing returns territory.
Though, looking it up, apparently 8-way is pretty typical for modernish
desktop-class chips (vs, say, 2-way on some less recent chips, like the
Phenom II and similar).
Well, and the CPU that I was running before getting the current one was
apparently 4-way associative.
Also appears like Intel may have often been going for higher cache
associativity than AMD here.
Like, apparently the Merom-2M was also 8-way; from around the same
time-frame as the Phenom II.
>> But, in any case, generic memory loads for constants kinda sucks with a
>> direct-mapped L1 cache.
>
> Direct-mapped caches suck. No qualification needed.
>
But, direct mapped caches are a lot cheaper (particularly on FPGAs), as
associativity seems to be fairly expensive.
>>>> This sort of thing can be a bigger saving though for things like
>>>> accessing global variables (where the existing options "generally
>>>> suck"). Typically, an unscaled Disp12 falls short of being able to
>>>> directly address all of the commonly-used global variables in a typical
>>>> program.
>>>
>>> How many globals does your program have.
>>>
>>
>> Going and quickly adding a stat for this (for plain global variables):
>> TestKern kernel:
>> Globals: tot=351 (2145B) dat=351(2161B) bss=0(0B)
>> Doom:
>> Globals: tot=777 (4468B) dat=777(4524B) bss=0(0B)
>> Hexen:
>> Globals: tot=766 (4228B) dat=766(4260B) bss=0(0B)
>> ROTT:
>> Globals: tot=723 (3656B) dat=723(3736B) bss=0(0B)
>> SWQuake:
>> Globals: tot=791 (4936B) dat=792(5208B) bss=0(0B)
>> GLQuake:
>> Globals: tot=956 (6744B) dat=957(6920B) bss=0(0B)
>> Quake 3 Arena (main program):
>> Globals: tot=980 (7080B) dat=980(7088B) bss=0(0B)
>> ...
>>
>> This is excluding all arrays and similar, so only counting the basic
>> primitive-type globals.
>>
>>
>> But, as noted, basically all of these examples are larger than 2048 bytes.
>
> Two fit in 4096 bytes (accessible with a 12-bit offset), and the rest
> fits in 8192 bytes (accessible with a 13-bit offset).
>
All are bigger than the +/- 2048 that RISC-V gives though.
And if more than 50% of the global variables are out of range of a
direct load/store op, this isn't ideal.
Granted, one can debate whether or not special-purpose load/store ops
with a 16-bit offset is overkill.
Like, a special "Disp16(GP)" addressing mode...
> - anton
Technical Details
Message-ID:
<11den39$3g047$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11d76uq$11cdc$1@paganini.bofh.team> <11d9d0d$1rsce$1@dont-email.me> <11d9tq8$1b9c1$2@paganini.bofh.team> <11dabct$26fdl$1@dont-email.me> <11dahac$2t74$1@gal.iecc.com> <11dakku$29j9n$1@dont-email.me> <2026Nov15.170804@mips.complang.tuwien.ac.at> <11dd5pn$32jg3$1@dont-email.me> <2026Nov16.082458@mips.complang.tuwien.ac.at>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: BGB <cr88192@gmail.com> Newsgroups: comp.arch Subject: Re: Large constants in RISC Date: Mon, 16 Nov 2026 04:49:12 -0600 Organization: A noiseless patient Spider Lines: 340 Message-ID: <11den39$3g047$1@dont-email.me> References: <11d76uq$11cdc$1@paganini.bofh.team> <11d9d0d$1rsce$1@dont-email.me> <11d9tq8$1b9c1$2@paganini.bofh.team> <11dabct$26fdl$1@dont-email.me> <11dahac$2t74$1@gal.iecc.com> <11dakku$29j9n$1@dont-email.me> <2026Nov15.170804@mips.complang.tuwien.ac.at> <11dd5pn$32jg3$1@dont-email.me> <2026Nov16.082458@mips.complang.tuwien.ac.at> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 16 Nov 2026 10:49:14 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3670151"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18rQGXrNRty5MzDth/lIwus2MsUAFmmwSY="; posting-host="7cb72988303a6b52400f6abc3a774d75" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:b9cPnMS7hXtSO+oMKShvJl4939I= sha256:BwcIJ1ZL+nNC4Nwf1EmddxqO44tf0SpPbV2TM/ybq5Q= sha1:HI/kXgXPV0tv0ku3iHmL4BMfMFg= sha256:bkx184W4JgTkpAypnUqkS9dC0Qsno0o5b/vo2iyO2kY= Content-Language: en-US In-Reply-To: <2026Nov16.082458@mips.complang.tuwien.ac.at>