Thread View: comp.arch
100 messages
100 total messages
Page 2 of 2
Started by Waldek Hebisch
Fri, 13 Nov 2026 14:30
Page 2 of 2 • 100 total messages
Re: Large constants in RISC
Author: 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
Re: Large constants in RISC
Author: BGB
Date: Mon, 16 Nov 2026 11:09
Date: Mon, 16 Nov 2026 11:09
67 lines
3764 bytes
3764 bytes
On 11/16/2026 2:04 AM, Anton Ertl wrote: > BGB <cr88192@gmail.com> writes: >> Or, if one happens to use something like a rarely-used global variable >> (in the static-count sense) in the middle of a loop in the middle of the >> hot path (which is where this sort of thing is seemingly most prone to >> show up sometimes). >> >> So, sometimes it is better to be defensive and mostly address the last >> few percent as a precautionary effort against slow-cases randomly >> showing up in the hot path. >> >> >> Well, at least excluding programmer-level "everyone already knows this >> is slow" things, like integer divide and modulo. >> >> >> Pretty much everyone seems to expect things like integer constants and >> global variables to be nearly free though. > > What makes you think that everybody expects accesses to global > variables to be fast? > There is a lot of code around that seems to keep a lot of active state in global variables (rather than using structs or passing stuff using function arguments). Say, for example, most of the configuration state for drawing spans and columns in the Doom style engines, etc. They probably wouldn't have done it this way if they expected global variables to be slow. Well, most extreme example being the "Build Engine" (from Apogee / 3D Realms), but, errm, I haven't really felt up to trying to port this. > Certainly my take has been for decades that anything that's not in > registers is slow. And that global variables are not in registers, in > general. > Well, admittedly a fair amount of the code I am dealing with was originally written in decades past. But, admittedly, some of the code I had used as my major test cases, was actually code I had tried (and initially failed) to understand when I was learning programming back in elementary school. So, it is possible that things like Doom and similar don't reflect particularly modern programming sensibilities. And, to admit, I more often put everything into big structs and pass pointers to the structs around in function arguments. Usually this is because it is often nice to be able to have multiple instances of things, which doesn't work so well when everything is held in global variables. > - anton
Re: Large constants in RISC
Author: MitchAlsup
Date: Mon, 16 Nov 2026 18:50
Date: Mon, 16 Nov 2026 18:50
33 lines
2096 bytes
2096 bytes
EricP <ThatWouldBeTelling@thevillage.com> posted:
> On 2026-Nov-15 11:08, Anton Ertl wrote:
--------------
> If I was using the constant table approach I would:
>
> - define for LD and ST instructions that if the base register was
> the zero register (R0 or R31) then it means use the 64-bit RIP.
I used Rbase == R0 -> IP
> - add scaled index LD and ST instructions with the following format:
> opcode 6b Rs1_base 5b Rs2_index 5b Rd/Rs3 5b data_type 3b scale 3b offset 5b
> Data types are signed integer: B, H, W, L float: S, D
I used Rindex == R0 -> no indexing
> This can access anywhere in 64b address space in 2 instructions:
I can access anywhere in 64-bit VAS in 1 instruction.
> - use a small RIPrel offset to load the 64b RIPrel offset from the table
> = use the scaled index LD/ST with scale = 0, index = offset, and base RIP
> to address all of memory.
>
> For RIP-relative 64b addressing:
>
> LD.L r1 = [RIP + table_offset]
> LD.dt r2 = [RIP + r1<<0]
LDs Rd,[Rbase,Rindex<<s,#table_offset]
Re: Large constants in RISC
Author: MitchAlsup
Date: Mon, 16 Nov 2026 18:52
Date: Mon, 16 Nov 2026 18:52
104 lines
5325 bytes
5325 bytes
EricP <ThatWouldBeTelling@thevillage.com> posted: > On 2026-Nov-15 14:12, EricP wrote: > > On 2026-Nov-15 11:08, Anton Ertl wrote: > >> BGB <cr88192@gmail.com> writes: > >>> However, if counting clock-cycles, a prefix is a win: > >>> Prefix + Op: Zero cycle overhead for the missed constant; > >>> Load + Op: May add 3 or 4 clock cycles. > >>> > >>> So, would need to speculatively load the immediate values several > >>> instructions in advance, and hope that there are no extra cache misses. > >> > >> 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 I was using the constant table approach I would: > > > > - define for LD and ST instructions that if the base register was > > the zero register (R0 or R31) then it means use the 64-bit RIP. > > > > - add scaled index LD and ST instructions with the following format: > > opcode 6b Rs1_base 5b Rs2_index 5b Rd/Rs3 5b data_type 3b scale 3b offset 5b > > Data types are signed integer: B, H, W, L float: S, D > > > > This can access anywhere in 64b address space in 2 instructions: > > - use a small RIPrel offset to load the 64b RIPrel offset from the table > > = use the scaled index LD/ST with scale = 0, index = offset, and base RIP > > to address all of memory. > > > > For RIP-relative 64b addressing: > > > > LD.L r1 = [RIP + table_offset] > > LD.dt r2 = [RIP + r1<<0] > > > > For absolute 64b addressing, set the base register to the constant and > > the index register to the zero register (which equals 0 NOT the RIP): > > > > LD.L r1 = [RIP + table_offset] > > LD.dt r2 = [r1 + r0<<0] > > > >> 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. > >> > >>> 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. > >> > >> In order to be able to address the globals of a program with a single > >> global pointer, the linker needs to do quite a bit of work. Maybe the > >> RISC-V linker does. For the Alpha every compilation unit has its own > >> constant and global table as far as I remember (and I think the same > >> register is used for both on the Alpha). > >> > >> - anton > > > > Alpha put the constant table just above the entry to the routine > > running backwards and aligned the routine on a cache line. > > On entry it would do a BR rd,+0 to copy the PC into rd > > then they use that as a base to access the constant table. > > > > However neither Alpha or RV have > Oops I hit send instead of save. > > However neither Alpha or RV have the scaled index address mode > that saves having to manually ADD the offset to the RIP for each LD or ST. > > The method I suggest above > > LD.L r1 = [RIP + table_offset] > LD.dt r2 = [RIP + r1<<0] > > has the drawback that the RIPrel offset to the same memory location will be > different for each LD/ST instruction, requiring multiple constant loads. Not when you substitute #large_displacement for the first LD. > Instead one can use a Load Effective Address LEA instruction with a > base of RIP to copy the RIP into a dest register, and have a single 64b > offset for each variable relative to that fixed base. > > LEA r1 = [RIP] > LD.L r2 = [r1 + table_offset] > LD.dt r3 = [r1 + r2<<0] > > and reuse r1 base and r2 offset multiple times: > > ST.dt [r1 + r2<<0] = r3 > >
Re: Large constants in RISC
Author: MitchAlsup
Date: Mon, 16 Nov 2026 19:08
Date: Mon, 16 Nov 2026 19:08
166 lines
7152 bytes
7152 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) posted:
> Thomas Koenig <tkoenig@netcologne.de> writes:
> >Anton Ertl <anton@mips.complang.tuwien.ac.at> schrieb:
> >> Thomas Koenig <tkoenig@netcologne.de> writes:
---------------------
> <2025Jul1.180809@mips.complang.tuwien.ac.at>.
>
> anton@cfarm121:~/bigconstants$ cat main.c
> #include <math.h>
> extern int a, b;
> extern double x, y;
> extern void foo(void);
>
> int main()
> {
> foo();
> x = lgamma(y);
> a += signgam;
> a += b;
> return 0;
> }
main:
ENTER R0,R0,#0
CALX [IP,,#GOT[foo]]
LDD R1,[IP,,#y]
CALL lgamma
LDSW R2,[ip,,#signam]
LDSW R3,[ip,,#a]
ADD R2,R2,R3
LDSW R3,[IP,,#b]
ADD R2,R2,R3
STW R2,[ip,,#a]
MOV R1,#0
EXIT R0,R0,#0
> anton@cfarm121:~/bigconstants$ cat foo.c
> int a, b;
> double x, y;
> void foo(void)
> {
> }
foo:
RET
> anton@cfarm121:~/bigconstants$ gcc -Wall -O -FPIC main.c foo.c -lm
> anton@cfarm121:~/bigconstants$ objdump -d a.out
> ...
> 00000000000009f4 <.main>:
> 9f4: 7c 08 02 a6 mflr r0
> 9f8: f8 01 00 10 std r0,16(r1)
> 9fc: f8 21 ff 91 stdu r1,-112(r1)
> a00: 48 00 00 79 bl a78 <.foo>
> a04: 60 00 00 00 nop
> a08: 60 00 00 00 nop
> a0c: 39 22 81 90 addi r9,r2,-32368
> a10: c8 29 00 00 lfd f1,0(r9)
> a14: 4b ff fe 0d bl 820 <0000001a.plt_call.lgamma@@GLIBC_2.23>
> a18: e8 41 00 28 ld r2,40(r1)
> a1c: 60 00 00 00 nop
> a20: 39 22 81 98 addi r9,r2,-32360
> a24: d8 29 00 00 stfd f1,0(r9)
> a28: 60 00 00 00 nop
> a2c: 39 42 81 a4 addi r10,r2,-32348
> a30: 81 2a 00 00 lwz r9,0(r10)
> a34: 60 00 00 00 nop
> a38: e9 02 80 28 ld r8,-32728(r2)
> a3c: 81 08 00 00 lwz r8,0(r8)
> a40: 7d 29 42 14 add r9,r9,r8
> a44: 60 00 00 00 nop
> a48: 39 02 81 a0 addi r8,r2,-32352
> a4c: 81 08 00 00 lwz r8,0(r8)
> a50: 7d 29 42 14 add r9,r9,r8
> a54: 91 2a 00 00 stw r9,0(r10)
> a58: 38 60 00 00 li r3,0
> a5c: 38 21 00 70 addi r1,r1,112
> a60: e8 01 00 10 ld r0,16(r1)
> a64: 7c 08 03 a6 mtlr r0
> a68: 4e 80 00 20 blr
> a6c: 00 00 00 00 .long 0x0
> a70: 00 00 00 01 .long 0x1
> a74: 80 00 00 00 lwz r0,0(0)
>
> 0000000000000a78 <.foo>:
> a78: 4e 80 00 20 blr
>
> anton@cfarm121:~/bigconstants$ gcc -Wall -O -FPIC -mcpu=power10 main.c foo.c -lm
> anton@cfarm121:~/bigconstants$ objdump -d a.out
> 0000000000000a40 <.main>:
> a40: 7c 08 02 a6 mflr r0
> a44: f8 01 00 10 std r0,16(r1)
> a48: f8 21 ff 91 stdu r1,-112(r1)
> a4c: 48 00 00 79 bl ac4 <.foo>
> a50: 60 00 00 00 nop
> a54: 06 10 00 01 pla r9,128572 # 20090 <y>
> a58: 39 20 f6 3c
> a5c: c8 29 00 00 lfd f1,0(r9)
> a60: 4b ff fd e1 bl 840 <0000001a.plt_call.lgamma@@GLIBC_2.23>
> a64: e8 41 00 28 ld r2,40(r1)
> a68: 06 10 00 01 pla r9,128560 # 20098 <x>
> a6c: 39 20 f6 30
> a70: d8 29 00 00 stfd f1,0(r9)
> a74: 06 10 00 01 pla r9,128560 # 200a4 <a>
> a78: 39 20 f6 30
> a7c: 80 c9 00 00 lwz r6,0(r9)
> a80: 04 10 00 01 pld r10,128168 # 1ff28 [signgam@got]
> a84: e5 40 f4 a8
> a88: 80 ea 00 00 lwz r7,0(r10)
> a8c: 06 10 00 01 pla r10,128532 # 200a0 <b>
> a90: 39 40 f6 14
> a94: 81 0a 00 00 lwz r8,0(r10)
> a98: 7d 47 32 14 add r10,r7,r6
> a9c: 7d 4a 42 14 add r10,r10,r8
> aa0: 91 49 00 00 stw r10,0(r9)
> aa4: 38 60 00 00 li r3,0
> aa8: 38 21 00 70 addi r1,r1,112
> aac: e8 01 00 10 ld r0,16(r1)
> ab0: 7c 08 03 a6 mtlr r0
> ab4: 4e 80 00 20 blr
> ab8: 00 00 00 00 .long 0x0
> abc: 00 00 00 01 .long 0x1
> ac0: 80 00 00 00 lwz r0,0(0)
>
> 0000000000000ac4 <.foo>:
> ac4: 4e 80 00 20 blr
>
> In this case the difference does not impress me. But of course this
> is not a case that would benefit from the availability of r2 for other
> purposes.
>
> For comparison, here's what I get on ARM A64:
>
> 0000000000000864 <main>:
> 864: a9bf7bfd stp x29, x30, [sp, #-16]!
> 868: 910003fd mov x29, sp
> 86c: 94000017 bl 8c8 <foo>
> 870: 90000080 adrp x0, 10000 <__FRAME_END__+0xf550>
> 874: f947e400 ldr x0, [x0, #4040]
> 878: fd400000 ldr d0, [x0]
> 87c: 97ffffad bl 730 <lgamma@plt>
> 880: 90000080 adrp x0, 10000 <__FRAME_END__+0xf550>
> 884: f947d400 ldr x0, [x0, #4008]
> 888: fd000000 str d0, [x0]
> 88c: 90000081 adrp x1, 10000 <__FRAME_END__+0xf550>
> 890: f947e021 ldr x1, [x1, #4032]
> 894: b9400020 ldr w0, [x1]
> 898: 90000082 adrp x2, 10000 <__FRAME_END__+0xf550>
> 89c: f947ec42 ldr x2, [x2, #4056]
> 8a0: b9400042 ldr w2, [x2]
> 8a4: 0b020000 add w0, w0, w2
> 8a8: 90000082 adrp x2, 10000 <__FRAME_END__+0xf550>
> 8ac: f947d042 ldr x2, [x2, #4000]
> 8b0: b9400042 ldr w2, [x2]
> 8b4: 0b020000 add w0, w0, w2
> 8b8: b9000020 str w0, [x1]
> 8bc: 52800000 mov w0, #0x0 // #0
> 8c0: a8c17bfd ldp x29, x30, [sp], #16
> 8c4: d65f03c0 ret
>
> 00000000000008c8 <foo>:
> 8c8: d65f03c0 ret
> 8cc: d503201f nop
>
> - anton
Re: Large constants in RISC
Author: BGB
Date: Mon, 16 Nov 2026 19:50
Date: Mon, 16 Nov 2026 19:50
86 lines
5087 bytes
5087 bytes
On 11/15/2026 1:08 PM, MitchAlsup wrote: > anton@mips.complang.tuwien.ac.at (Anton Ertl) posted: > >> Robert Finch <robfi680@gmail.com> writes: >>> One ends up with constants in the data cache. May be slower than having >>> constants inline from the I$. One thought it to have instructions that >>> can load constants using the I$. >> >> There is no data path from the I-cache to the load unit in existing >> CPUs. I also expect that if you add such a data path, it will be >> slower, because the I-cache is not close to the LSU, but close to the >> decoder and the branch predictor. So that idea is not going to fly. > > You do NOT need said path, what you need is for the constant to show up > from the instruction stream and be parse-able from that instruction > stream easily. Given really small (1-wide) low ends, one might use a > 4-word aligned fetch--higher end machines will need correspondingly > wider Fetch and more prediction accuracy. > Yeah. I suspect on those ISA's where an "@PC+" addressing mode or similar was used to encode constant loads, this was more likely by invoking magic in the fetch and decode, than *actually* loading from and adjusting PC... Also common is for ISA's to have a special "Disp(PC)" or "(PC,Disp)" addressing mode or similar. RISC-V has neither. It is almost half-tempting though to redefine using X1 as a base register as a way to encode PC-rel loads. But, this would be non-standard and potentially controversial. Would allow the "have a small blob of constants after the current function with PC-rel loads" strategy though. While X0 might otherwise seem like an obvious choice, there are non-zero amounts of code (mostly in the RV32 microcontroller space) which use X0 to address the first and last 2K of address space (Say, 2K of ROM, and the other 2K being a mix of a small RAM area and some common MMIO ports). Granted, a lot of these sorts of devices also tend to ignore the high bits of the address, shrinking the size of the address space down to 16 or 20 bits or so (like, don't need 32 bits of address space when the RAM is measured in kB...). This would leave X1 addressing as a more obvious choice because it is "otherwise kinda useless" (or, at least, more useless than X0). Though, in my case, X0/R0 does still encode PC-rel for XG3 ops (carried over from XG1 and XG2). In this case, X1 also encoded GP, but this is in-effect redundant with using X3 (which is currently mostly what BGBCC uses). X1 encodes Zero for Abs64. Would be wonky to have X0 and X1 effectively swapped between the RV64 and XG3 encodings, but, alas... But, then again, not particularly inclined to add PC-rel Disp12 to RV as its only real use-case is something BGBCC doesn't really do. Well, contrast with PC-Rel Disp33, which can access ".rodata" and similar, or use LEA to load pointers to string literals, making these cases a little more useful. Can't currently encode these in RV space, but one can instead use AUIPC+ADDI or AUIPC+LW or similar in these cases (higher latency, but the same number of instruction words). >> The better approach is to have the data in the instructions, and >> extract it during instruction decoding, like all the immediate >> operands in all instruction sets around do. Or to have the data in >> data memory and access it with a load that works with the D-cache, >> like Alpha and others do. >> >>> ARM has shifted constant instruction too. >> >> Not in ARM A64. But ARM A64 has an interesting encoding for certain >> bit patterns. >> >> - anton
Re: Large constants in RISC
Author: Waldek Hebisch
Date: Tue, 17 Nov 2026 05:07
Date: Tue, 17 Nov 2026 05:07
151 lines
8640 bytes
8640 bytes
Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
> quadibloc@invalid.com (John Savard) writes:
>>On Sat, 14 Nov 2026 19:10:17 GMT, anton@mips.complang.tuwien.ac.at
>>(Anton Ertl) wrote:
>>>antispam@fricas.org (Waldek Hebisch) writes:
>>>>I read what he wrote and I do not doubt that code having constants
>>>>in instructions is faster.
>>>
>>>AMD64 has large constants (or at least better support for large
>>>constants than ARM A64), yet according to reviews ARM A64
>>>implementations form Apple and IIRC also Qualcomm offer better
>>>performance than the best AMD64 implementations.
> ...
>>However, support for large constants, while it contributes to better
>>performance, is just one factor out of many. AMD64 has a baroque
>>instruction set, so in an ARM A64 chip, a larger proportion of the
>>transistors are doing useful work.
>
> And included in the "baroque" instruction set are instructions with
> 32-bit immediate operands, and a mov instruction with a 64-bit
> immediate operand.
>
> In any case, there is no empirical evidence "that having [large]
> constants in instructions is faster." And the weak evidence that we
> currently have, points in the other direction. So we have every
> reason to doubt that claim.
>
>>When power and heat are the
>>limiting factors, that should matter.
>
> Power and heat have been limiting factors since about 2005, if not
> earlier. In 2006, Apple switched from PowerPC (a RISC) to Intel's
> "baroque" IA-32 and later AMD64 offerings.
>
>>"AMD64 has a huge market for powerful desktop computers and servers,
>>while the only big market for ARM A64 is smartphones, and yet
>>according to reviews..."
>>
>>...because, at least up until very recently, Intel has had so much
>>money to spend that it could afford to engineer its chips so cleverly
>>as to make up for the CISC versus RISC disadvantage.
>
> In the late 1990s, AMD was a much smaller company than Intel, and also
> smaller than the larger members of the AIM (Apple, IBM, Motorola)
> consortium, and the "baroqueness" of IA-32 played a bigger role in the
> CPUs of the day, yet AMD managed to beat Intel in the GHz race and was
> very competetive in the following years, while the offerings of the
> AIM consortium were slow and only kept afloat by the faith in the
> church of Apple. And eventually Apple switched to Intel.
>
> It's not clear why the ARM A64 microarchtitects manage to outcompete
> AMD64 these days, while the AIM microarchitects could not do that in
> the early 2000s. Maybe there is something in the instruction sets
> that plays a role when you do 10-wide microarchitecture that was not
> an issue for 3-wide microarchitectures in the early 2000s.
I think that economic factors worked differently than you suggest.
AMD and Intel were hardware companies serving large software
market Power/PPC software market was much smaller and fragmented.
Given size of PPC software market really large investments in
hardware were hard to justify. Now "phone" market seem to be
larger than PC market and Apple has a lot of money, so serious
investment in chips make sense.
I also wonder how many microachitectures are there. My possibly
worong understanding was that at turn of century Intel had two
microarchitectural efforts, that is Pentum 4 intended as new
thing and continuation of Pentium 2/3 and that AMD concentrated
on just one microarchitecure. Intel got Pentum 4 wrong while
AMD succeded. Later with Core/Core 2 Intel got it right
while AMD had trouble. My impression was that various models
were relatively small variation of the same microarchitecure.
I compared performance of several low-end PC class processors
and I see wild variation of performance, which suggest that
now there are several microarchitecures. Still, it is likely
that there is very small number of microarchitecures for top
performing models. Possibly 1 microarchitecure in normal
time and 2 when there is transition between microarchitecures.
If that is true one can expect essentially random fluctuations
of comparative performance, depending on which team gets more
aspects right.
So beside underlying architecture there are two factors that
can explain good performance of ARM64: one in large influx of
money, the other is luck.
> Maybe Intel's APX (32 GPRs, 3-address encoding) is a reaction to Apple
> M1 having better IPC, and maybe we will see better IPC on Intel
> machines with APX. I would not bet on it, though.
When looking at technical factors I think that one aspect is
how much work is done by a single microinstruction. The other
is how much resources go into single microinstruction. That
is microinstruction which reads many registers and writes
many registers may do a lot of work, but needs more execution
resources. So, determining right complexity of
microinstructions needs tricky compromise. This compromise
is affected by architecture. For example, decoders may
fuse load and index increment into autoindexing, but in
case of architectural preincrement exception is just at
start of the instruction, while in fused case exception in
in the middle of synthetised instruction. I am not sure
how much this specific case matters, but it illustrates
that even with fusion and splitting architectural instruction
set forces differences at microarchitectural level. ARM64
is relatively new instruction set, so ARM should have
looked at interactions between architecture and
microarchitecture and optimize them.
I have impression that ARM64 code is bigger, so probably
also needs more instructions. But I would expect more
possibilites for fusing and since instructions have fixed
length and more regular encoding fusing should be easier
to implement than for AMD64. So number of microinstructions
to execute may be smaller. IIUC AMD64 for fast decoding
depends on predecode bits in cache. Assuming single
predecode bit per byte, AMD64 probably needs more cache
bits than ARM64.
Concerning specific features, ARM64 sets flags only on
demand, which reduces dependencies between instructions.
Autoincrement/autodecrement allow doing more work in
single instruction than AMD64.
More registers help, old statistics suggest that only
a little, but modern compilers perform a lot of
inlining, which increases instruction use. Also,
more architectural registers help in increasing
parallelizm at source level.
Concerning constants, by using ADRP in many cases it is
possible to get effect of 32-bit PC-relative address using
2 instructions. ARM64 can load 32 bit constant into
register using 2 instructions and can load 64 bit constant
into register using 4 intruction sequence. 2 instructions
are easily fusable, fusing 4 intructions needs more effort
but it is not clear if it is harder than decoding AMD64
instructions. So from my point of view main drawback
is need to use extra register, but since ARM64 has
significantly more registers this can not tip the balace
in favour of AMD64.
Let me add that I do not expect large performace improvement
from my proposal. Rather, I think that it is cheap to
implement and small variation of what some RISC-s already
do. I do expect some performance gain, and I hope that cost
to benefit ratio is reasonable, but I think that other
factors have more influence on performance.
For me main advantage is simplification of software.
--
Waldek Hebisch
Architectural influence on performance (was: Large constants in RISC)
Author: Anton Ertl
Date: Tue, 17 Nov 2026 06:35
Date: Tue, 17 Nov 2026 06:35
322 lines
17213 bytes
17213 bytes
antispam@fricas.org (Waldek Hebisch) writes: >Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote: >> In the late 1990s, AMD was a much smaller company than Intel, and also >> smaller than the larger members of the AIM (Apple, IBM, Motorola) >> consortium, and the "baroqueness" of IA-32 played a bigger role in the >> CPUs of the day, yet AMD managed to beat Intel in the GHz race and was >> very competetive in the following years, while the offerings of the >> AIM consortium were slow and only kept afloat by the faith in the >> church of Apple. And eventually Apple switched to Intel. >> >> It's not clear why the ARM A64 microarchtitects manage to outcompete >> AMD64 these days, while the AIM microarchitects could not do that in >> the early 2000s. Maybe there is something in the instruction sets >> that plays a role when you do 10-wide microarchitecture that was not >> an issue for 3-wide microarchitectures in the early 2000s. > >I think that economic factors worked differently than you suggest. >AMD and Intel were hardware companies serving large software >market Power/PPC software market was much smaller and fragmented. >Given size of PPC software market really large investments in >hardware were hard to justify. You may be right. Apple went through hard times in the late 1990s, shut down the clone market, and probably asked Motorola for even lower prices than usual, and actually Motorola was expelled from the AIM alliance, and according to Wikipedia forced to stop making PowerPC chips for a while in 1998-1999, which probably reduced Motorola's investment to 0, and one can imagine the kind of long-term effects this has had from dissolution of the development teams. In any case, Motorola apparently focused on making PowerPCs for the embedded market, rather than trying to compete with AMD and Intel. Also, the ideas that PowerPC-based machines would take over from IA-32-based machines (PReP, CHRP) did not work out as intended, reducing IBM's interest in AIM. But IBM still went for high-performance CPUs for their AS/400 and RS/6000 families, in particular Power4. Bot IIRC Power4 was better then Intel and AMD chips in SPECfp (possibly due to a more expensive memory subsystem), but worse in SPECint. IBM also succeeded at getting Power(PC) into the PlayStation 3 and other systems with the Cell chip, and into the Xbox 360, both markets where high performance counts. So between these markets, there was quite a bit of money in the Power(PC) architecture. The PC market may have been bigger, but AMD's share was probably smaller, and yet they managed to achieve competetive performance, while IBM could not followup in the next generation of game consoles (but still sells Power Systems for IBM i (the AS/400 OS), AIX, and Linux), and the next two game console generations went to AMD, despite their weakness in the heavy machinery times (2011-2017). >I compared performance of several low-end PC class processors >and I see wild variation of performance, which suggest that >now there are several microarchitecures. It is well-known that Intel has two families of microarchitectures, which turned into the P-cores and E-Cores of Alder Lake (e.g., Core i7-12700), and AMD now has one family (Zen..Zen6), with some variations in maximum clock rate since Zen4 and variations in configuration since Zen5. Also there are different cache configurations for the same microarchitecture. Depending on what you measure on what machines, you may be seeing cache effects, clock rate effects, differences between successive members of the same family, differences between configurations, or differences between microarchitectures. Apple has P-cores and E-cores, which may be different microarchitecture families, or different configurations of the same basic microarchitecture family. Qualcomm has just the Oryon family for now. ARM now sells 4 kinds of cores (Nano, Pro, Premium, Ultra), where the Nano is an in-order microarchitecture, and the others are OoO. The OoO ones may be configurations of the same basic microarchitecture for different area and power budgets, or maybe they split their OoO architecture into different families at one point. Only the Apple microarchitectures and Qualcomm are in the laptop market; Nvidia is entering it with ARM-based microarchitectures in the RTX Spark CPU (9-10 X925 + 9-10 X725, predecessors of the C1 and C2 line), which is slightly slower in single-core performance than the fastest AMD CPU and slightly faster than the fastest Intel CPU in the review <https://www.computerbase.de/artikel/notebooks/nvidia-rtx-spark-n1x-test.99679/seite-3>. >Still, it is likely >that there is very small number of microarchitecures for top >performing models. Possibly 1 microarchitecure in normal >time and 2 when there is transition between microarchitecures. >If that is true one can expect essentially random fluctuations >of comparative performance, depending on which team gets more >aspects right. According to <https://www.computerbase.de/artikel/notebooks/nvidia-rtx-spark-n1x-test.99679/seite-3>, we see the following single-core (actually single-threaded) ratings (larger is better): 844 M6 (Apple 2026) 763 M5 (Apple 2025) 663 X2E-84-100 (Qualcomm 2026) 583 Ryzen AI 9 HX 370 (AMD 2024) 576 RTX Spark (ARM 2024) 565 Core Ultra X7 358H (Intel 2026) The fact that two ARM A64 microarchitectures beat all of the AMD64 microarchitectures, and in case of Apple by a wide margin, and a third ARM A64 microarchitecture is head-to-head with the best Intel and AMD microarchtiectures looks to be more than a random fluctuation to me. AFAICT, AMD has not dropped the ball, and neither has Intel (Intel has manufacturing problems, but works around that by manufacturing at TSMC). Given that, there may be some systematic thing at work here that makes it harder to speed up Intel and AMD microarchitectures than ARM A64 microarchitectures. >So beside underlying architecture there are two factors that >can explain good performance of ARM64: one in large influx of >money, the other is luck. There is still enough money in the AMD64 laptop, desktop and server markets for AMD and Intel to fund microarchitectural developments, even two teams in the case of Intel. >> Maybe Intel's APX (32 GPRs, 3-address encoding) is a reaction to Apple >> M1 having better IPC, and maybe we will see better IPC on Intel >> machines with APX. I would not bet on it, though. > >When looking at technical factors I think that one aspect is >how much work is done by a single microinstruction. The other >is how much resources go into single microinstruction. That >is microinstruction which reads many registers and writes >many registers may do a lot of work, but needs more execution >resources. So, determining right complexity of >microinstructions needs tricky compromise. This compromise >is affected by architecture. For example, decoders may >fuse load and index increment into autoindexing, but in >case of architectural preincrement exception is just at >start of the instruction, while in fused case exception in >in the middle of synthetised instruction. I am not sure >how much this specific case matters, but it illustrates >that even with fusion and splitting architectural instruction >set forces differences at microarchitectural level. This particular case is probably not relevant, because I have not read about Intel or AMD doing such fusion even for the post-increment case. What Intel has done, however, is to perform small (up to 11 bits of range, with a total sum of 13 bits) constant adds in the renamer with a perceived latency of 0. >ARM64 >is relatively new instruction set, so ARM should have >looked at interactions between architecture and >microarchitecture and optimize them. They certainly designed it for using a single instruction width (32 bits), and putting as much functionality in these 32 bits as can be efficiently implemented. The fact that some of their instructions read and/or write a significant number of registers looks like a possible problem, but obviously the microarchitects have overcome that. >I have impression that ARM64 code is bigger, so probably >also needs more instructions. Text size numbers from <2025Jun17.161742@mips.complang.tuwien.ac.at>: |Debian numbers from <2024Jan4.101941@mips.complang.tuwien.ac.at>: | | bash grep gzip | 595204 107636 46744 armhf | 599832 101102 46898 riscv64 | 796501 144926 57729 amd64 <--- | 829776 134784 56868 arm64 <--- | 853892 152068 61124 i386 | 891128 158544 68500 armel | 892688 168816 64664 s390x | 1020720 170736 71088 mips64el | 1168104 194900 83332 ppc64el | | |NetBSD numbers from <2025Mar4.093916@mips.complang.tuwien.ac.at>: | | libc ksh pax ed | 1102054 124726 66218 26226 riscv-riscv32 | 1077192 127050 62748 26550 riscv-riscv64 | 1030288 150686 79852 31492 mvme68k | 779393 155764 75795 31813 vax | 1302254 171505 83249 35085 amd64 <--- | 1229032 178332 89180 36876 evbarm-aarch64 <--- | 1539052 179055 82280 34717 amd64-daily <--- | 1374961 184458 96971 37218 i386 | 1247476 185792 96728 42028 evbarm-earmv7hf | 1333952 187452 96328 39472 sparc | 1586608 204032 106896 45408 evbppc | 1536144 204320 106768 43232 hppa | 1397024 216832 109792 48512 sparc64 | 1538536 222336 107776 44912 evbmips-mips64eb | 1623952 243008 122096 50640 evbmips-mipseb | 1689920 251376 120672 51168 alpha | 2324752 2259984 1378000 ia64 So the text sizes seem to be pretty similar between ARM A64 and AMD64. One would have to compare the number of executed instructions for the same task to get a better idea about the effectiveness per instruction. >But I would expect more >possibilites for fusing and since instructions have fixed >length and more regular encoding fusing should be easier >to implement than for AMD64. So number of microinstructions >to execute may be smaller. IIUC AMD64 for fast decoding >depends on predecode bits in cache. Assuming single >predecode bit per byte, AMD64 probably needs more cache >bits than ARM64. In K7 (1999) and its offspring, AMD stored predecode bits in the caches, reusing ECC bits for parity and predecoding. But AFAIK that has not been done by anyone for a while. AMD Zen...Zen6 cores and Intel P cores since Sandy bridge (2011) use a microop cache (and already the Pentium 4 family did that), but the Intel E-Cores do not use that, but instead decode instructions with three 3-wide decoders at the same time. AMD has introduced 2 4-wide decoders and managed to get them to work on the same thread with Zen6, but that's only needed on a microop cache miss. High-end ARM cores used to have microop caches, but are abandoning them in recent years, probably because they no longer decode ARM A32, ARM T32 (variable-length) and ARM A64, but just ARM A64. Decoding a variable-length instruction set with many instruction lengths certainly costs, but Intel and AMD seem to know how to deal with it, so it's probably not what holds back the performance. >Concerning specific features, ARM64 sets flags only on >demand, which reduces dependencies between instructions. The fact that more instructions write registers in AMD64 mainly means that more rename resources have to be spent on flags on AMD64 than on ARM A64. Another disadvantage of AMD64 in this area is that it has instructions which do not set C, O, or both, meaning that C, O and NZP all have to be renamed separately. I have read that Intel's E-Cores only have one set of rename registers for flags. I wonder how that works; dependence chains between partial-update instructions until finally a full-update instruction comes along that breaks the chain? Anyway, they seem to have some working ideas here, because Skymont has a good IPC (I have measured a better IPC than Lion Cove (the P-core of the same generation) once. Intel and AMD also know how to deal with flags, but it may indeed be the case that the combined load of having to implement variable-length instructions and AMD64 flag semantics means that they cannot advance as easily as the ARM A64 microarchitects. Now if ARM A64 had not designed the flags into their architecture, but put them into the GPRs <https://www.complang.tuwien.ac.at/anton/tmp/carry.pdf>, they would not have to do rename resources for the flags at all, which might have helped them getting an even greater lead. >Autoincrement/autodecrement allow doing more work in >single instruction than AMD64. It appears that the renamer is the bottleneck (that's certainly the idea behind microarchitectural top-down analysis), and that the number of written registers is what costs in the renamer (branches are free, and xchg reg, reg has half throughput even when the renamer handles it, and on Zen3-5). In such a scenario auto-increment/decrement don't help, and if these addressing modes seduce programmers or compiler to uses more auto-increment rather than fewer larger increments (e.g., when pushing a series of items on a stack), that could be a disadvantage. I have not measured the performance of auto-increment addressing on ARM A64 implementations, though. >Concerning constants, by using ADRP in many cases it is >possible to get effect of 32-bit PC-relative address using >2 instructions. ARM64 can load 32 bit constant into >register using 2 instructions and can load 64 bit constant >into register using 4 intruction sequence. 2 instructions >are easily fusable, fusing 4 intructions needs more effort >but it is not clear if it is harder than decoding AMD64 >instructions. So from my point of view main drawback >is need to use extra register, but since ARM64 has >significantly more registers this can not tip the balace >in favour of AMD64. The large constants also mean that the micorops become wider (or the microarchitecture breaks them into pieces again). PC-relative addresses mean that 64-bit addresses have to be embedded into the microops. Or alternatively, one needs to represent the PC in the rename register file, or have a data path from wherever the PC resides in the ROB (for mispreduction and exception handling) to the functional units. Concerning your idea, it seems to me that it is better to use a fixed, but otherwise regular register than a new register. That avoids the need to save the new register across exceptions and interrupts. And it is better to make the clearing explicit rather than implicit (e.g., have an instruction that sets the middle bits and sets all the other bits to zero, and an instruction that ors the immediate operand with the high bits of the fixed register). This costs encoding space, but the register can be used for other things when it is not used for a large constant. Alternatively, if you are really tight on encoding space, use just one opcode, make every instruction that reads from the constant register implicitly clear the register (I assume that one can do that in the renamer without requiring the resources of writing to a register), and have one instruction that shifts the register left by 26 bits and inserts the immediate bits of the instruction into bit 12-37. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: EricP
Date: Tue, 17 Nov 2026 14:38
Date: Tue, 17 Nov 2026 14:38
48 lines
2663 bytes
2663 bytes
On 2026-Nov-16 13:50, MitchAlsup wrote: > EricP <ThatWouldBeTelling@thevillage.com> posted: > >> On 2026-Nov-15 11:08, Anton Ertl wrote: > -------------- >> If I was using the constant table approach I would: >> >> - define for LD and ST instructions that if the base register was >> the zero register (R0 or R31) then it means use the 64-bit RIP. > > I used Rbase == R0 -> IP > >> - add scaled index LD and ST instructions with the following format: >> opcode 6b Rs1_base 5b Rs2_index 5b Rd/Rs3 5b data_type 3b scale 3b offset 5b >> Data types are signed integer: B, H, W, L float: S, D > > I used Rindex == R0 -> no indexing I considered that but decided scaled indexed was distinct enough to be worthy of its own instruction format. >> This can access anywhere in 64b address space in 2 instructions: > > I can access anywhere in 64-bit VAS in 1 instruction. Yes but this is working with fixed 32-bit instructions. The combination of two features, rBase == R0 => RIP and having scaled index LD/ST add the constant, that compacts RVs 5 instruction sequence into 2. > >> - use a small RIPrel offset to load the 64b RIPrel offset from the table >> = use the scaled index LD/ST with scale = 0, index = offset, and base RIP >> to address all of memory. >> >> For RIP-relative 64b addressing: >> >> LD.L r1 = [RIP + table_offset] >> LD.dt r2 = [RIP + r1<<0] > > LDs Rd,[Rbase,Rindex<<s,#table_offset] Yep, no question large immediates is better.
Re: Large constants in RISC
Author: EricP
Date: Tue, 17 Nov 2026 15:18
Date: Tue, 17 Nov 2026 15:18
117 lines
5971 bytes
5971 bytes
On 2026-Nov-16 13:52, MitchAlsup wrote: > EricP <ThatWouldBeTelling@thevillage.com> posted: > >> On 2026-Nov-15 14:12, EricP wrote: >>> On 2026-Nov-15 11:08, Anton Ertl wrote: >>>> BGB <cr88192@gmail.com> writes: >>>>> However, if counting clock-cycles, a prefix is a win: >>>>> Prefix + Op: Zero cycle overhead for the missed constant; >>>>> Load + Op: May add 3 or 4 clock cycles. >>>>> >>>>> So, would need to speculatively load the immediate values several >>>>> instructions in advance, and hope that there are no extra cache misses. >>>> >>>> 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 I was using the constant table approach I would: >>> >>> - define for LD and ST instructions that if the base register was >>> the zero register (R0 or R31) then it means use the 64-bit RIP. >>> >>> - add scaled index LD and ST instructions with the following format: >>> opcode 6b Rs1_base 5b Rs2_index 5b Rd/Rs3 5b data_type 3b scale 3b offset 5b >>> Data types are signed integer: B, H, W, L float: S, D >>> >>> This can access anywhere in 64b address space in 2 instructions: >>> - use a small RIPrel offset to load the 64b RIPrel offset from the table >>> = use the scaled index LD/ST with scale = 0, index = offset, and base RIP >>> to address all of memory. >>> >>> For RIP-relative 64b addressing: >>> >>> LD.L r1 = [RIP + table_offset] >>> LD.dt r2 = [RIP + r1<<0] >>> >>> For absolute 64b addressing, set the base register to the constant and >>> the index register to the zero register (which equals 0 NOT the RIP): >>> >>> LD.L r1 = [RIP + table_offset] >>> LD.dt r2 = [r1 + r0<<0] >>> >>>> 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. >>>> >>>>> 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. >>>> >>>> In order to be able to address the globals of a program with a single >>>> global pointer, the linker needs to do quite a bit of work. Maybe the >>>> RISC-V linker does. For the Alpha every compilation unit has its own >>>> constant and global table as far as I remember (and I think the same >>>> register is used for both on the Alpha). >>>> >>>> - anton >>> >>> Alpha put the constant table just above the entry to the routine >>> running backwards and aligned the routine on a cache line. >>> On entry it would do a BR rd,+0 to copy the PC into rd >>> then they use that as a base to access the constant table. >>> >>> However neither Alpha or RV have >> Oops I hit send instead of save. >> >> However neither Alpha or RV have the scaled index address mode >> that saves having to manually ADD the offset to the RIP for each LD or ST. >> >> The method I suggest above >> >> LD.L r1 = [RIP + table_offset] >> LD.dt r2 = [RIP + r1<<0] >> >> has the drawback that the RIPrel offset to the same memory location will be >> different for each LD/ST instruction, requiring multiple constant loads. > > Not when you substitute #large_displacement for the first LD. Sure, you could do a Load Effective Address LEA to calculate the address once into r1 then use it multiple times. That costs one extra instruction but would save the instruction space for storing multiple 8 byte immediate RIPrel offsets. But the challenge is to optimize fixed 32-bit LD/ST instruction sequences. I would also like to make the GOT Global Offset Table unnecessary but that requires changing the way EXE/DLL files are mapped. > >> Instead one can use a Load Effective Address LEA instruction with a >> base of RIP to copy the RIP into a dest register, and have a single 64b >> offset for each variable relative to that fixed base. >> >> LEA r1 = [RIP] >> LD.L r2 = [r1 + table_offset] >> LD.dt r3 = [r1 + r2<<0] >> >> and reuse r1 base and r2 offset multiple times: >> >> ST.dt [r1 + r2<<0] = r3 >> >>
Re: Architectural influence on performance (was: Large constants in RISC)
Author: John Savard
Date: Tue, 17 Nov 2026 16:02
Date: Tue, 17 Nov 2026 16:02
20 lines
2264 bytes
2264 bytes
On Tue, 17 Nov 2026 06:35:43 GMT, anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote: >But IBM still went for high-performance CPUs for their AS/400 and >RS/6000 families, in particular Power4. Bot IIRC Power4 was better >then Intel and AMD chips in SPECfp (possibly due to a more expensive >memory subsystem), but worse in SPECint. > >IBM also succeeded at getting Power(PC) into the PlayStation 3 and >other systems with the Cell chip, and into the Xbox 360, both markets >where high performance counts. In connection with this, it's useful to remember that Apple's publicly stated reason for switching to Intel from the PowerPC was that its suppliers were failing to provide PowerPC chips that worked well in laptop computers. So it wasn't for want of performance on the desktop. John Savard
Re: Architectural influence on performance (was: Large constants in RISC)
Author: Anton Ertl
Date: Tue, 17 Nov 2026 16:45
Date: Tue, 17 Nov 2026 16:45
25 lines
2741 bytes
2741 bytes
quadibloc@invalid.com (John Savard) writes: >In connection with this, it's useful to remember that Apple's publicly >stated reason for switching to Intel from the PowerPC was that its >suppliers were failing to provide PowerPC chips that worked well in >laptop computers. > >So it wasn't for want of performance on the desktop. The desktop offerings did not lag as much in performance, but they were not that great, eiter. Here's our LaTeX benchmark (smaller is better): - iBook G4 12", 1066MHz 7447A, 512KB L2, Debian Sarge GNU/Linux 2.62 '04 - PowerMac G5, 2000MHz PPC970, Gentoo Linux PPC64 1.47 '03 - Pentium 4 2.66GHz, 512KB L2, Debian 3.1 1.31 '02 - Pentium M 1.5GHz (IBM R51), Debian 3.1 1.112 '03 - Athlon 64 3200+, 2000MHz, 1MB L2, Fedora Core 1 (64-bit) 0.76 '03 So when the PowerMac G5 arrived in June 2003, the Pentium 4 2.66GHz (and actually 3.06GHz, but I have no results for that) and Pentium M 1.5GHz were already there, and the Athlon64 was released in September. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: MitchAlsup
Date: Tue, 17 Nov 2026 18:21
Date: Tue, 17 Nov 2026 18:21
39 lines
2798 bytes
2798 bytes
antispam@fricas.org (Waldek Hebisch) posted: > Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote: > > quadibloc@invalid.com (John Savard) writes: > >>On Sat, 14 Nov 2026 19:10:17 GMT, anton@mips.complang.tuwien.ac.at > >>(Anton Ertl) wrote: > >>>antispam@fricas.org (Waldek Hebisch) writes: ---------------------- > > > > It's not clear why the ARM A64 microarchtitects manage to outcompete > > AMD64 these days, while the AIM microarchitects could not do that in > > the early 2000s. Maybe there is something in the instruction sets > > that plays a role when you do 10-wide microarchitecture that was not > > an issue for 3-wide microarchitectures in the early 2000s. > > I think that economic factors worked differently than you suggest. > AMD and Intel were hardware companies serving large software > market Power/PPC software market was much smaller and fragmented. This is the cubic dollars argument. > Given size of PPC software market really large investments in > hardware were hard to justify. Now "phone" market seem to be > larger than PC market and Apple has a lot of money, so serious > investment in chips make sense. > > I also wonder how many microachitectures are there. At any instant, about 12. > My possibly > worong understanding was that at turn of century Intel had two > microarchitectural efforts, that is Pentum 4 intended as new > thing and continuation of Pentium 2/3 and that AMD concentrated > on just one microarchitecure. AMD tried at least 3 times to "do" a low power µArch. All 3 times they ran into manpower/money issues and had to cancel the LP to concentrate on the GBOoO.
Re: Architectural influence on performance (was: Large constants in RISC)
Author: MitchAlsup
Date: Tue, 17 Nov 2026 18:25
Date: Tue, 17 Nov 2026 18:25
24 lines
2168 bytes
2168 bytes
quadibloc@invalid.com (John Savard) posted: > On Tue, 17 Nov 2026 06:35:43 GMT, anton@mips.complang.tuwien.ac.at > (Anton Ertl) wrote: > > >But IBM still went for high-performance CPUs for their AS/400 and > >RS/6000 families, in particular Power4. Bot IIRC Power4 was better > >then Intel and AMD chips in SPECfp (possibly due to a more expensive > >memory subsystem), but worse in SPECint. > > > >IBM also succeeded at getting Power(PC) into the PlayStation 3 and > >other systems with the Cell chip, and into the Xbox 360, both markets > >where high performance counts. > > In connection with this, it's useful to remember that Apple's publicly > stated reason for switching to Intel from the PowerPC was that its > suppliers were failing to provide PowerPC chips that worked well in > laptop computers. At a price Apple was willing to pay... > So it wasn't for want of performance on the desktop. > > John Savard
Re: Architectural influence on performance
Author: Stefan Monnier
Date: Wed, 18 Nov 2026 21:35
Date: Wed, 18 Nov 2026 21:35
17 lines
2561 bytes
2561 bytes
Anton Ertl [2026-11-17 16:45:09] wrote: > quadibloc@invalid.com (John Savard) writes: >>In connection with this, it's useful to remember that Apple's publicly >>stated reason for switching to Intel from the PowerPC was that its >>suppliers were failing to provide PowerPC chips that worked well in >>laptop computers. >>So it wasn't for want of performance on the desktop. > The desktop offerings did not lag as much in performance, but they > were not that great, eiter. Here's our LaTeX benchmark (smaller is better): IIRC the issue was that the desktop market was simply not nearly as important as the laptop market (for Apple): they could have stuck with PowerPC if the performance was good for laptops even it wasn't great for desktops, but not the other way around. === Stefan
Re: Large constants in RISC
Author: aph
Date: Thu, 19 Nov 2026 09:42
Date: Thu, 19 Nov 2026 09:42
31 lines
2847 bytes
2847 bytes
John Savard <quadibloc@invalid.com> wrote: > On Sat, 14 Nov 2026 19:10:17 GMT, anton@mips.complang.tuwien.ac.at > (Anton Ertl) wrote: >>antispam@fricas.org (Waldek Hebisch) writes: >>>I read what he wrote and I do not doubt that code having constants >>>in instructions is faster. >> >>AMD64 has large constants (or at least better support for large >>constants than ARM A64), yet according to reviews ARM A64 >>implementations form Apple and IIRC also Qualcomm offer better >>performance than the best AMD64 implementations. > > Why do you say "yet", given that support for large constants is not > something that mitigates against performance? > > Oh, I'm sorry: I misread what you wrote. I assumed ARM A64 had the > better support for large constants, and didn't see the letters on the > screen in front of me. In a way, it does. Most large constants I see are addresses. AArch64 (almost) always has a 48-bit user space, so except for floating-point doubles and the very occasional large integer, constant loads look like mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 which is three 32-bit instructions, 12 bytes. x86-64 movabs is 10 bytes, so in practice the difference doesn't really matter, and there are more productive ways to look for code density improvements. Andrew.
Re: Large constants in RISC
Author: Stefan Monnier
Date: Thu, 19 Nov 2026 13:59
Date: Thu, 19 Nov 2026 13:59
15 lines
2162 bytes
2162 bytes
aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: > In a way, it does. Most large constants I see are addresses. > AArch64 (almost) always has a 48-bit user space, so except for > floating-point doubles and the very occasional large integer, constant > loads look like > > mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 Interesting. I would have expected that most AArch64 code is position independent (e.g. for address space randomization purposes), so I wonder in which circumstances we end up with such instruction sequences to build hardcoded addresses. === Stefan
Re: Large constants in RISC
Author: Scott Lurndal
Date: Thu, 19 Nov 2026 23:19
Date: Thu, 19 Nov 2026 23:19
16 lines
1880 bytes
1880 bytes
Stefan Monnier <monnier@iro.umontreal.ca> writes: >aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >> In a way, it does. Most large constants I see are addresses. >> AArch64 (almost) always has a 48-bit user space, so except for >> floating-point doubles and the very occasional large integer, constant >> loads look like >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 > >Interesting. I would have expected that most AArch64 code is position >independent (e.g. for address space randomization purposes), so I wonder >in which circumstances we end up with such instruction sequences to >build hardcoded addresses. Consider the case where one is creating the 64-bit address (virtual or physical) of a hardware component, or a VDSO.
Re: Large constants in RISC
Author: Michael S
Date: Fri, 20 Nov 2026 10:26
Date: Fri, 20 Nov 2026 10:26
24 lines
2629 bytes
2629 bytes
On Thu, 19 Nov 2026 23:19:31 GMT scott@slp53.sl.home (Scott Lurndal) wrote: > Stefan Monnier <monnier@iro.umontreal.ca> writes: > >aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: > >> In a way, it does. Most large constants I see are addresses. > >> AArch64 (almost) always has a 48-bit user space, so except for > >> floating-point doubles and the very occasional large integer, > >> constant loads look like > >> > >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 > > > >Interesting. I would have expected that most AArch64 code is > >position independent (e.g. for address space randomization > >purposes), so I wonder in which circumstances we end up with such > >instruction sequences to build hardcoded addresses. > > Consider the case where one is creating the 64-bit address (virtual or > physical) of a hardware component, or a VDSO. The vast majority of hardware addresses are not absolute addresses, but offsets relative to dynamically assigned PCI BARs. Therefore, constants larger than 30 bits are rare. Even 24 bits are uncommon.
Re: Large constants in RISC
Author: Scott Lurndal
Date: Fri, 20 Nov 2026 15:23
Date: Fri, 20 Nov 2026 15:23
32 lines
2755 bytes
2755 bytes
Michael S <already5chosen@yahoo.com> writes: >On Thu, 19 Nov 2026 23:19:31 GMT >scott@slp53.sl.home (Scott Lurndal) wrote: > >> Stefan Monnier <monnier@iro.umontreal.ca> writes: >> >aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >> >> In a way, it does. Most large constants I see are addresses. >> >> AArch64 (almost) always has a 48-bit user space, so except for >> >> floating-point doubles and the very occasional large integer, >> >> constant loads look like >> >> >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 >> > >> >Interesting. I would have expected that most AArch64 code is >> >position independent (e.g. for address space randomization >> >purposes), so I wonder in which circumstances we end up with such >> >instruction sequences to build hardcoded addresses. >> >> Consider the case where one is creating the 64-bit address (virtual or >> physical) of a hardware component, or a VDSO. > >The vast majority of hardware addresses are not absolute addresses, but >offsets relative to dynamically assigned PCI BARs. Therefore, constants >larger than 30 bits are rare. Even 24 bits are uncommon. I work today with systems that support > 32-bit PCI express BARs. Note that from the host perspective, the base address programmed in the PCI-Express/PCI BARs is 64-bit. While the _offset_ to a register may be smaller than 32-bits (I've worked with devices that have larger BARs), the absoluate host address once the BAR has been mapped into the physical address space is 64-bits.
Re: Large constants in RISC
Author: Anton Ertl
Date: Fri, 20 Nov 2026 16:01
Date: Fri, 20 Nov 2026 16:01
21 lines
2320 bytes
2320 bytes
Stefan Monnier <monnier@iro.umontreal.ca> writes: >aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >> In a way, it does. Most large constants I see are addresses. >> AArch64 (almost) always has a 48-bit user space, so except for >> floating-point doubles and the very occasional large integer, constant >> loads look like >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 > >Interesting. I would have expected that most AArch64 code is position >independent (e.g. for address space randomization purposes), so I wonder >in which circumstances we end up with such instruction sequences to >build hardcoded addresses. You can support ASLR without PIC when you generate your code at run-time (i.e., a load-and-go or JIT compiler). - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: EricP
Date: Fri, 20 Nov 2026 20:47
Date: Fri, 20 Nov 2026 20:47
24 lines
2224 bytes
2224 bytes
On 2026-Nov-19 08:59, Stefan Monnier wrote: > aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >> In a way, it does. Most large constants I see are addresses. >> AArch64 (almost) always has a 48-bit user space, so except for >> floating-point doubles and the very occasional large integer, constant >> loads look like >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 > > Interesting. I would have expected that most AArch64 code is position > independent (e.g. for address space randomization purposes), so I wonder > in which circumstances we end up with such instruction sequences to > build hardcoded addresses. > > > === Stefan An application or OS can require that certain data structures are mapped at predefined virtual addresses. For example, an OS could define that the page tables are mapped at 0xC00...00. The user mode OS interface could define that an int64 system clock is mapped read-only at a particular virtual address so it can be read without a system call.
Re: Large constants in RISC
Author: MitchAlsup
Date: Fri, 20 Nov 2026 21:01
Date: Fri, 20 Nov 2026 21:01
40 lines
2732 bytes
2732 bytes
EricP <ThatWouldBeTelling@thevillage.com> posted: > On 2026-Nov-19 08:59, Stefan Monnier wrote: > > aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: > >> In a way, it does. Most large constants I see are addresses. > >> AArch64 (almost) always has a 48-bit user space, so except for > >> floating-point doubles and the very occasional large integer, constant > >> loads look like > >> > >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 > > > > Interesting. I would have expected that most AArch64 code is position > > independent (e.g. for address space randomization purposes), so I wonder > > in which circumstances we end up with such instruction sequences to > > build hardcoded addresses. > > > > > > === Stefan > > An application or OS can require that certain data structures > are mapped at predefined virtual addresses. > For example, an OS could define that the page tables are mapped at 0xC00...00. > The user mode OS interface could define that an int64 system clock is mapped > read-only at a particular virtual address so it can be read without a system call. This just gave me an idea:: a) when walking the page tables (TLB miss) b) and you are at the (smallest) page level and you get another PTP ...instead of a PTE, then c) this PTP accesses a PTE which provides for accessing a single c.1) Double Word or c.2) Single line Making such access/PTE not blead address space to where it is not desired. So, you can access the timer/time but not the surrounding control registers. You saw it here first!
Re: Large constants in RISC
Author: Scott Lurndal
Date: Fri, 20 Nov 2026 21:14
Date: Fri, 20 Nov 2026 21:14
48 lines
3273 bytes
3273 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes: >EricP <ThatWouldBeTelling@thevillage.com> posted: > >> On 2026-Nov-19 08:59, Stefan Monnier wrote: >> > aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >> >> In a way, it does. Most large constants I see are addresses. >> >> AArch64 (almost) always has a 48-bit user space, so except for >> >> floating-point doubles and the very occasional large integer, constant >> >> loads look like >> >> >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 >> > >> > Interesting. I would have expected that most AArch64 code is position >> > independent (e.g. for address space randomization purposes), so I wonder >> > in which circumstances we end up with such instruction sequences to >> > build hardcoded addresses. >> > >> > >> > === Stefan >> >> An application or OS can require that certain data structures >> are mapped at predefined virtual addresses. >> For example, an OS could define that the page tables are mapped at 0xC00...00. >> The user mode OS interface could define that an int64 system clock is mapped >> read-only at a particular virtual address so it can be read without a system call. > >This just gave me an idea:: > >a) when walking the page tables (TLB miss) >b) and you are at the (smallest) page level and you get another PTP >...instead of a PTE, >then >c) this PTP accesses a PTE which provides for accessing a single >c.1) Double Word >or >c.2) Single line > >Making such access/PTE not blead address space to where it is not desired. >So, you can access the timer/time but not the surrounding control registers. > >You saw it here first! It still burns a TLB entry. The way it works on linux is that the VDSO (Virtual Dynamic Shared Object) occupies one or more readonly page(s) in the virtual address space, which is both shared by all processes and updated by the kernel periodically. User mode accesses to the VDSO simply load from the specified VDSO address. It may even be possible to use the GLOBAL flag in the PTE to reduce TLB pressure.
Re: Large constants in RISC
Author: EricP
Date: Fri, 20 Nov 2026 23:17
Date: Fri, 20 Nov 2026 23:17
47 lines
3171 bytes
3171 bytes
On 2026-Nov-20 16:01, MitchAlsup wrote: > EricP <ThatWouldBeTelling@thevillage.com> posted: > >> On 2026-Nov-19 08:59, Stefan Monnier wrote: >>> aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >>>> In a way, it does. Most large constants I see are addresses. >>>> AArch64 (almost) always has a 48-bit user space, so except for >>>> floating-point doubles and the very occasional large integer, constant >>>> loads look like >>>> >>>> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 >>> >>> Interesting. I would have expected that most AArch64 code is position >>> independent (e.g. for address space randomization purposes), so I wonder >>> in which circumstances we end up with such instruction sequences to >>> build hardcoded addresses. >>> >>> >>> === Stefan >> >> An application or OS can require that certain data structures >> are mapped at predefined virtual addresses. >> For example, an OS could define that the page tables are mapped at 0xC00...00. >> The user mode OS interface could define that an int64 system clock is mapped >> read-only at a particular virtual address so it can be read without a system call. > > This just gave me an idea:: > > a) when walking the page tables (TLB miss) > b) and you are at the (smallest) page level and you get another PTP > ...instead of a PTE, > then > c) this PTP accesses a PTE which provides for accessing a single > c.1) Double Word > or > c.2) Single line > > Making such access/PTE not blead address space to where it is not desired. > So, you can access the timer/time but not the surrounding control registers. > > You saw it here first! I meant the software clock in memory. There are two clocks, a hardware and a software clock that the OS controls the adjustments HW clock can drift so OS adjusts its memory copy according to its own algorithm.
Re: Large constants in RISC
Author: Stefan Monnier
Date: Sat, 21 Nov 2026 03:23
Date: Sat, 21 Nov 2026 03:23
46 lines
3626 bytes
3626 bytes
On 2026-Nov-19 08:59, Stefan Monnier wrote: > aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >> In a way, it does. Most large constants I see are addresses. >> AArch64 (almost) always has a 48-bit user space, so except for >> floating-point doubles and the very occasional large integer, constant >> loads look like >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 > Interesting. I would have expected that most AArch64 code is position > independent (e.g. for address space randomization purposes), so I wonder > in which circumstances we end up with such instruction sequences to > build hardcoded addresses. EricP [2026-11-20 15:47:57] wrote: > An application or OS can require that certain data structures > are mapped at predefined virtual addresses. I find it hard to believe that these thingies would account for a large portion of large constants. Scott Lurndal [2026-11-19 23:19:31] wrote: > Consider the case where one is creating the 64-bit address (virtual or > physical) of a hardware component, or a VDSO. Similarly, for hardware addresses what I see in U-Boot and Linux code is that such addresses are usually relegated to device-tree files so should presumably not appear in the actual instruction stream. Do you know of a concrete case where it accounts for a large portion of large constants? Anton Ertl [2026-11-20 16:01:31] wrote: > You can support ASLR without PIC when you generate your code at > run-time (i.e., a load-and-go or JIT compiler). Hmm... didn't think of that. Indeed this could lead to actual addresses embedded in the code. Then again, I'd expect most such JIT setups to want to keep most of those addresses in data rather than in the code so the GC can more easily trace through them. So, we now have 3 suggested circumstances, but it's not clear if these occur enough to explain Andrew's experience. Andrew, do you know what causes all those large constant addresses you're seeing? === Stefan
Re: Large constants in RISC
Author: Terje Mathisen
Date: Sat, 21 Nov 2026 07:53
Date: Sat, 21 Nov 2026 07:53
96 lines
4790 bytes
4790 bytes
EricP wrote:
> On 2026-Nov-20 16:01, MitchAlsup wrote:
>> EricP <ThatWouldBeTelling@thevillage.com> posted:
>>
>>> On 2026-Nov-19 08:59, Stefan Monnier wrote:
>>>> aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote:
>>>>> In a way, it does. Most large constants I see are addresses.
>>>>> AArch64 (almost) always has a 48-bit user space, so except for
>>>>> floating-point doubles and the very occasional large integer, constant
>>>>> loads look like
>>>>>
>>>>> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32
>>>>
>>>> Interesting. I would have expected that most AArch64 code is position
>>>> independent (e.g. for address space randomization purposes), so I
>>>> wonder
>>>> in which circumstances we end up with such instruction sequences to
>>>> build hardcoded addresses.
>>>>
>>>>
>>>> === Stefan
>>>
>>> An application or OS can require that certain data structures
>>> are mapped at predefined virtual addresses.
>>> For example, an OS could define that the page tables are mapped at
>>> 0xC00...00.
>>> The user mode OS interface could define that an int64 system clock is
>>> mapped
>>> read-only at a particular virtual address so it can be read without a
>>> system call.
>>
>> This just gave me an idea::
>>
>> a) when walking the page tables (TLB miss)
>> b) and you are at the (smallest) page level and you get another PTP
>> ...instead of a PTE,
>> then
>> c) this PTP accesses a PTE which provides for accessing a single
>> c.1) Double Word
>> or
>> c.2) Single line
>>
>> Making such access/PTE not blead address space to where it is not
>> desired.
>> So, you can access the timer/time but not the surrounding control
>> registers.
>>
>> You saw it here first!
>
> I meant the software clock in memory. There are two clocks,
> a hardware and a software clock that the OS controls the adjustments
> HW clock can drift so OS adjusts its memory copy according to its own
> algorithm.
That's just one way to do it:
FreeBSD had the first optimal (as in minimum resources for maximum
precisiion) implementation, written by phk of NTP and FreeBSD fame, more
recently the Varnish cache.
The design is to keep the sw clock in shareable memory, along with a
user-space function to retrieve the current time:
(seconds, nanoseconds) = readclock()
implemented minimally as
for (;;) {
n = system.nanoseconds; // All of these are marked volatile
s = system.seconds;
t = system.lasttick;
// Avoids reading during update
if (n == system.nanoseconds && s == system.seconds) break;
}
ticks = rdtsc();
// Calculate the offset from last tick:
delta = ticks - t;
nsdelta = (delta * system.scaling_factor) >> system.scaling_shift;
n += nsdelta;
while n >= 1000000000 {
s++;
n -= 1000000000;
}
return (s, n);
--
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"
Re: Large constants in RISC
Author: aph
Date: Sat, 21 Nov 2026 10:33
Date: Sat, 21 Nov 2026 10:33
23 lines
2625 bytes
2625 bytes
Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote:
>> In a way, it does. Most large constants I see are addresses.
>> AArch64 (almost) always has a 48-bit user space, so except for
>> floating-point doubles and the very occasional large integer, constant
>> loads look like
>>
>> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32
>
> Interesting. I would have expected that most AArch64 code is position
> independent (e.g. for address space randomization purposes), so I wonder
> in which circumstances we end up with such instruction sequences to
> build hardcoded addresses.
It depends on the language. In a simple ahead-of-time-compiled
language like C that may well mostly be true, but in an Object-Oriented
language it's more complicated. Code is PIC, but still contains things
like pointer type checks, and these refer to structures at absolute
addresses. Also, address constants in code can point to data, as in
if (a == some_well_known_object) { ... }
Andrew.
Re: Large constants in RISC
Author: aph
Date: Sat, 21 Nov 2026 10:38
Date: Sat, 21 Nov 2026 10:38
20 lines
2447 bytes
2447 bytes
Stefan Monnier <monnier@iro.umontreal.ca> wrote: > aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >> In a way, it does. Most large constants I see are addresses. >> AArch64 (almost) always has a 48-bit user space, so except for >> floating-point doubles and the very occasional large integer, constant >> loads look like >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 > > Interesting. I would have expected that most AArch64 code is position > independent (e.g. for address space randomization purposes), so I wonder > in which circumstances we end up with such instruction sequences to > build hardcoded addresses. One other thing: even though this address is in the instruction stream, it's not fixed: its target gets moved by garbage collection, and of course the collector has to rewrite these instructions as it goes along. Andrew.
Re: Large constants in RISC
Author: Michael S
Date: Sat, 21 Nov 2026 17:53
Date: Sat, 21 Nov 2026 17:53
50 lines
3621 bytes
3621 bytes
On Fri, 20 Nov 2026 15:23:23 GMT scott@slp53.sl.home (Scott Lurndal) wrote: > Michael S <already5chosen@yahoo.com> writes: > >On Thu, 19 Nov 2026 23:19:31 GMT > >scott@slp53.sl.home (Scott Lurndal) wrote: > > > >> Stefan Monnier <monnier@iro.umontreal.ca> writes: > >> >aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: > >> >> In a way, it does. Most large constants I see are addresses. > >> >> AArch64 (almost) always has a 48-bit user space, so except for > >> >> floating-point doubles and the very occasional large integer, > >> >> constant loads look like > >> >> > >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl > >> >> 32 > >> > > >> >Interesting. I would have expected that most AArch64 code is > >> >position independent (e.g. for address space randomization > >> >purposes), so I wonder in which circumstances we end up with such > >> >instruction sequences to build hardcoded addresses. > >> > >> Consider the case where one is creating the 64-bit address > >> (virtual or physical) of a hardware component, or a VDSO. > > > >The vast majority of hardware addresses are not absolute addresses, > >but offsets relative to dynamically assigned PCI BARs. Therefore, > >constants larger than 30 bits are rare. Even 24 bits are uncommon. > > I work today with systems that support > 32-bit PCI express BARs. > > Note that from the host perspective, the base address programmed in > the PCI-Express/PCI BARs is 64-bit. While the _offset_ to a register > may be smaller than 32-bits (I've worked with devices that have larger > BARs), the absoluate host address once the BAR has been mapped into > the physical address space is 64-bits. Of course, it's 64-bit. But host does not know upper bits at compile time. Except for deeply embedded hosts. But in 64-bit land those are rarity. I read that back in previous century, esp. before 1990, people regenerated their OSes after every change in HW configuration. We don't do it now, do we?
Re: Large constants in RISC
Author: Dan Cross
Date: Sat, 21 Nov 2026 19:49
Date: Sat, 21 Nov 2026 19:49
63 lines
3686 bytes
3686 bytes
In article <20261121195356.00001dd1@yahoo.com>, Michael S <already5chosen@yahoo.com> wrote: >On Fri, 20 Nov 2026 15:23:23 GMT >scott@slp53.sl.home (Scott Lurndal) wrote: > >> Michael S <already5chosen@yahoo.com> writes: >> >On Thu, 19 Nov 2026 23:19:31 GMT >> >scott@slp53.sl.home (Scott Lurndal) wrote: >> > >> >> Stefan Monnier <monnier@iro.umontreal.ca> writes: >> >> >aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >> >> >> In a way, it does. Most large constants I see are addresses. >> >> >> AArch64 (almost) always has a 48-bit user space, so except for >> >> >> floating-point doubles and the very occasional large integer, >> >> >> constant loads look like >> >> >> >> >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl >> >> >> 32 >> >> > >> >> >Interesting. I would have expected that most AArch64 code is >> >> >position independent (e.g. for address space randomization >> >> >purposes), so I wonder in which circumstances we end up with such >> >> >instruction sequences to build hardcoded addresses. >> >> >> >> Consider the case where one is creating the 64-bit address >> >> (virtual or physical) of a hardware component, or a VDSO. >> > >> >The vast majority of hardware addresses are not absolute addresses, >> >but offsets relative to dynamically assigned PCI BARs. Therefore, >> >constants larger than 30 bits are rare. Even 24 bits are uncommon. >> >> I work today with systems that support > 32-bit PCI express BARs. >> >> Note that from the host perspective, the base address programmed in >> the PCI-Express/PCI BARs is 64-bit. While the _offset_ to a register >> may be smaller than 32-bits (I've worked with devices that have larger >> BARs), the absoluate host address once the BAR has been mapped into >> the physical address space is 64-bits. > >Of course, it's 64-bit. Perhaps not. It's going to be whatever size the physical address space (or IOMMU-virtual address size) of the machine in question is. E.g., this might be 52 bits or something similar. >But host does not know upper bits at compile time. Except for deeply >embedded hosts. But in 64-bit land those are rarity. It depends on the device, not just the host. On a device that is known to only support 32-bit bars, for example, one _can_ know at compile time that the upper bits will all be 0. >I read that back in previous century, esp. before 1990, people >regenerated their OSes after every change in HW configuration. We don't >do it now, do we? Depends on the OS, but most do dynamic hardware discovery. However, most devices do not allocate physical address space to e.g. PCIe devices (well, outside of a hotplug path, anyway); in UEFI-based systems, that's usually at the PEI layer. - Dan C.
Re: Large constants in RISC
Author: Michael S
Date: Sat, 21 Nov 2026 20:10
Date: Sat, 21 Nov 2026 20:10
85 lines
4913 bytes
4913 bytes
On Sat, 21 Nov 2026 19:49:16 -0000 (UTC) cross@spitfire.i.gajendra.net (Dan Cross) wrote: > In article <20261121195356.00001dd1@yahoo.com>, > Michael S <already5chosen@yahoo.com> wrote: > >On Fri, 20 Nov 2026 15:23:23 GMT > >scott@slp53.sl.home (Scott Lurndal) wrote: > > > >> Michael S <already5chosen@yahoo.com> writes: > >> >On Thu, 19 Nov 2026 23:19:31 GMT > >> >scott@slp53.sl.home (Scott Lurndal) wrote: > >> > > >> >> Stefan Monnier <monnier@iro.umontreal.ca> writes: > >> >> >aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: > >> >> >> In a way, it does. Most large constants I see are addresses. > >> >> >> AArch64 (almost) always has a 48-bit user space, so except > >> >> >> for floating-point doubles and the very occasional large > >> >> >> integer, constant loads look like > >> >> >> > >> >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl > >> >> >> 32 > >> >> > > >> >> >Interesting. I would have expected that most AArch64 code is > >> >> >position independent (e.g. for address space randomization > >> >> >purposes), so I wonder in which circumstances we end up with > >> >> >such instruction sequences to build hardcoded addresses. > >> >> > >> >> Consider the case where one is creating the 64-bit address > >> >> (virtual or physical) of a hardware component, or a VDSO. > >> > > >> >The vast majority of hardware addresses are not absolute > >> >addresses, but offsets relative to dynamically assigned PCI BARs. > >> >Therefore, constants larger than 30 bits are rare. Even 24 bits > >> >are uncommon. > >> > >> I work today with systems that support > 32-bit PCI express BARs. > >> > >> Note that from the host perspective, the base address programmed in > >> the PCI-Express/PCI BARs is 64-bit. While the _offset_ to a > >> register may be smaller than 32-bits (I've worked with devices > >> that have larger BARs), the absoluate host address once the BAR > >> has been mapped into the physical address space is 64-bits. > > > >Of course, it's 64-bit. > > Perhaps not. It's going to be whatever size the physical > address space (or IOMMU-virtual address size) of the machine in > question is. E.g., this might be 52 bits or something similar. > > >But host does not know upper bits at compile time. Except for deeply > >embedded hosts. But in 64-bit land those are rarity. > > It depends on the device, not just the host. On a device that > is known to only support 32-bit bars, for example, one _can_ > know at compile time that the upper bits will all be 0. > The point is that not all address bits are known in compile-time. So, ability to efficiently assemble arbitrary 64-bit (or 48-bit, or 52-bit) constants in compile time does not help this particular application much. Or at all. OTHO, ability to efficiently assemble more narrow arbitrary constants, like 32-bit or 30-bit, but most importantly even smaller ones, in range from 17 to 24 bits, could be valuable. > >I read that back in previous century, esp. before 1990, people > >regenerated their OSes after every change in HW configuration. We > >don't do it now, do we? > > Depends on the OS, but most do dynamic hardware discovery. > However, most devices do not allocate physical address space to > e.g. PCIe devices (well, outside of a hotplug path, anyway); in > UEFI-based systems, that's usually at the PEI layer. > > - Dan C. > For the question at hand it does not matter which entity assigns BARs. The only thing that matters is that absolute HW addresses are not known at compile time.
Re: Large constants in RISC
Author: Dan Cross
Date: Sat, 21 Nov 2026 20:20
Date: Sat, 21 Nov 2026 20:20
101 lines
5213 bytes
5213 bytes
In article <20261121221023.000078cd@yahoo.com>, Michael S <already5chosen@yahoo.com> wrote: >On Sat, 21 Nov 2026 19:49:16 -0000 (UTC) >cross@spitfire.i.gajendra.net (Dan Cross) wrote: > >> In article <20261121195356.00001dd1@yahoo.com>, >> Michael S <already5chosen@yahoo.com> wrote: >> >On Fri, 20 Nov 2026 15:23:23 GMT >> >scott@slp53.sl.home (Scott Lurndal) wrote: >> > >> >> Michael S <already5chosen@yahoo.com> writes: >> >> >On Thu, 19 Nov 2026 23:19:31 GMT >> >> >scott@slp53.sl.home (Scott Lurndal) wrote: >> >> > >> >> >> Stefan Monnier <monnier@iro.umontreal.ca> writes: >> >> >> >aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >> >> >> >> In a way, it does. Most large constants I see are addresses. >> >> >> >> AArch64 (almost) always has a 48-bit user space, so except >> >> >> >> for floating-point doubles and the very occasional large >> >> >> >> integer, constant loads look like >> >> >> >> >> >> >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl >> >> >> >> 32 >> >> >> > >> >> >> >Interesting. I would have expected that most AArch64 code is >> >> >> >position independent (e.g. for address space randomization >> >> >> >purposes), so I wonder in which circumstances we end up with >> >> >> >such instruction sequences to build hardcoded addresses. >> >> >> >> >> >> Consider the case where one is creating the 64-bit address >> >> >> (virtual or physical) of a hardware component, or a VDSO. >> >> > >> >> >The vast majority of hardware addresses are not absolute >> >> >addresses, but offsets relative to dynamically assigned PCI BARs. >> >> >Therefore, constants larger than 30 bits are rare. Even 24 bits >> >> >are uncommon. >> >> >> >> I work today with systems that support > 32-bit PCI express BARs. >> >> >> >> Note that from the host perspective, the base address programmed in >> >> the PCI-Express/PCI BARs is 64-bit. While the _offset_ to a >> >> register may be smaller than 32-bits (I've worked with devices >> >> that have larger BARs), the absoluate host address once the BAR >> >> has been mapped into the physical address space is 64-bits. >> > >> >Of course, it's 64-bit. >> >> Perhaps not. It's going to be whatever size the physical >> address space (or IOMMU-virtual address size) of the machine in >> question is. E.g., this might be 52 bits or something similar. >> >> >But host does not know upper bits at compile time. Except for deeply >> >embedded hosts. But in 64-bit land those are rarity. >> >> It depends on the device, not just the host. On a device that >> is known to only support 32-bit bars, for example, one _can_ >> know at compile time that the upper bits will all be 0. > >The point is that not all address bits are known in compile-time. But again, this depends. Some addresses _are_ known at compile time. Consider the text-mode CGA buffer on x86_64 systems; this is known to be at a fixed physical address (0xb0000); on a system that maps that to some fixed virtual address, it is known at compile time. Similarly with things like the IOAPIC or MMIO-based xAPIC (though we do usually read those addresses from an MSR). >So, ability to efficiently assemble arbitrary 64-bit (or 48-bit, >or 52-bit) constants in compile time does not help this particular >application much. Or at all. I agree. >OTHO, ability to efficiently assemble more narrow arbitrary constants, >like 32-bit or 30-bit, but most importantly even smaller ones, in >range from 17 to 24 bits, could be valuable. I also agree. >> >I read that back in previous century, esp. before 1990, people >> >regenerated their OSes after every change in HW configuration. We >> >don't do it now, do we? >> >> Depends on the OS, but most do dynamic hardware discovery. >> However, most devices do not allocate physical address space to >> e.g. PCIe devices (well, outside of a hotplug path, anyway); in >> UEFI-based systems, that's usually at the PEI layer. > >For the question at hand it does not matter which entity assigns BARs. >The only thing that matters is that absolute HW addresses are not known >at compile time. See above. BARs aren't the only thing that matter, though I do agree that this isn't a particularly _common_ operation. - Dan C.
Re: Large constants in RISC
Author: Lawrence D’Oliveiro
Date: Sun, 22 Nov 2026 01:18
Date: Sun, 22 Nov 2026 01:18
10 lines
1602 bytes
1602 bytes
On Fri, 13 Nov 2026 14:30:52 -0000 (UTC), Waldek Hebisch wrote: > To solve problem of needing extra register I propose to have extra > register (call it C) dedicated to building constants, different from > normal architectural registers. When I first heard of RISC, I proposed, only half in jest, that the acronym should be “IRSC”, for “Increased Register Set Computer”. Because none of the real-world examples seemed to have particularly “Reduced Instruction Sets” at all...
Re: Large constants in RISC
Author: MitchAlsup
Date: Sun, 22 Nov 2026 01:21
Date: Sun, 22 Nov 2026 01:21
51 lines
3538 bytes
3538 bytes
Michael S <already5chosen@yahoo.com> posted: > On Fri, 20 Nov 2026 15:23:23 GMT > scott@slp53.sl.home (Scott Lurndal) wrote: > > > Michael S <already5chosen@yahoo.com> writes: > > >On Thu, 19 Nov 2026 23:19:31 GMT > > >scott@slp53.sl.home (Scott Lurndal) wrote: > > > > > >> Stefan Monnier <monnier@iro.umontreal.ca> writes: > > >> >aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: > > >> >> In a way, it does. Most large constants I see are addresses. > > >> >> AArch64 (almost) always has a 48-bit user space, so except for > > >> >> floating-point doubles and the very occasional large integer, > > >> >> constant loads look like > > >> >> > > >> >> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl > > >> >> 32 > > >> > > > >> >Interesting. I would have expected that most AArch64 code is > > >> >position independent (e.g. for address space randomization > > >> >purposes), so I wonder in which circumstances we end up with such > > >> >instruction sequences to build hardcoded addresses. > > >> > > >> Consider the case where one is creating the 64-bit address > > >> (virtual or physical) of a hardware component, or a VDSO. > > > > > >The vast majority of hardware addresses are not absolute addresses, > > >but offsets relative to dynamically assigned PCI BARs. Therefore, > > >constants larger than 30 bits are rare. Even 24 bits are uncommon. > > > > I work today with systems that support > 32-bit PCI express BARs. > > > > Note that from the host perspective, the base address programmed in > > the PCI-Express/PCI BARs is 64-bit. While the _offset_ to a register > > may be smaller than 32-bits (I've worked with devices that have larger > > BARs), the absoluate host address once the BAR has been mapped into > > the physical address space is 64-bits. > > Of course, it's 64-bit. > But host does not know upper bits at compile time. Except for deeply > embedded hosts. But in 64-bit land those are rarity. > > I read that back in previous century, esp. before 1990, people > regenerated their OSes after every change in HW configuration. We don't do it now, do we? We now have config space--that provides all we need to figure out what pieces of SW to load in order to use the device.
Re: Large constants in RISC
Author: Waldek Hebisch
Date: Sun, 22 Nov 2026 02:36
Date: Sun, 22 Nov 2026 02:36
29 lines
2591 bytes
2591 bytes
Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> On 2026-Nov-19 08:59, Stefan Monnier wrote:
>> aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote:
>>> In a way, it does. Most large constants I see are addresses.
>>> AArch64 (almost) always has a 48-bit user space, so except for
>>> floating-point doubles and the very occasional large integer, constant
>>> loads look like
>>> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32
>> Interesting. I would have expected that most AArch64 code is position
>> independent (e.g. for address space randomization purposes), so I wonder
>> in which circumstances we end up with such instruction sequences to
>> build hardcoded addresses.
>
> EricP [2026-11-20 15:47:57] wrote:
>> An application or OS can require that certain data structures
>> are mapped at predefined virtual addresses.
>
> I find it hard to believe that these thingies would account for a large
> portion of large constants.
In a Lisp-like system that I use 3 most frequently used addresses
are address of empty list, address of boolean false and
address of boolean true. There is bunch of other constat singleton
things whose addresses are needed, but the 3 above are most
frequent. OTOH they are all allocated within first 2GB of memory,
so 32-bit constants are enough.
--
Waldek Hebisch
Re: Large constants in RISC
Author: Terje Mathisen
Date: Sun, 22 Nov 2026 08:14
Date: Sun, 22 Nov 2026 08:14
65 lines
4456 bytes
4456 bytes
MitchAlsup wrote: > Michael S <already5chosen@yahoo.com> posted: > >> On Fri, 20 Nov 2026 15:23:23 GMT >> scott@slp53.sl.home (Scott Lurndal) wrote: >> >>> Michael S <already5chosen@yahoo.com> writes: >>>> On Thu, 19 Nov 2026 23:19:31 GMT >>>> scott@slp53.sl.home (Scott Lurndal) wrote: >>>> >>>>> Stefan Monnier <monnier@iro.umontreal.ca> writes: >>>>>> aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >>>>>>> In a way, it does. Most large constants I see are addresses. >>>>>>> AArch64 (almost) always has a 48-bit user space, so except for >>>>>>> floating-point doubles and the very occasional large integer, >>>>>>> constant loads look like >>>>>>> >>>>>>> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl >>>>>>> 32 >>>>>> >>>>>> Interesting. I would have expected that most AArch64 code is >>>>>> position independent (e.g. for address space randomization >>>>>> purposes), so I wonder in which circumstances we end up with such >>>>>> instruction sequences to build hardcoded addresses. >>>>> >>>>> Consider the case where one is creating the 64-bit address >>>>> (virtual or physical) of a hardware component, or a VDSO. >>>> >>>> The vast majority of hardware addresses are not absolute addresses, >>>> but offsets relative to dynamically assigned PCI BARs. Therefore, >>>> constants larger than 30 bits are rare. Even 24 bits are uncommon. >>> >>> I work today with systems that support > 32-bit PCI express BARs. >>> >>> Note that from the host perspective, the base address programmed in >>> the PCI-Express/PCI BARs is 64-bit. While the _offset_ to a register >>> may be smaller than 32-bits (I've worked with devices that have larger >>> BARs), the absoluate host address once the BAR has been mapped into >>> the physical address space is 64-bits. >> >> Of course, it's 64-bit. >> But host does not know upper bits at compile time. Except for deeply >> embedded hosts. But in 64-bit land those are rarity. >> >> I read that back in previous century, esp. before 1990, people >> regenerated their OSes after every change in HW configuration. I did so well into the 2000s, when I recompiled FreeBSD for the kernel pulse per second input into the core OS hardware clock. This was crucial for optimal performance with a local GPS timing receiver like the Oncore or (a few years later) the SURE evaluation board. This was before spread spectrum system clocks, getting sub-50 ns was absolutely doable. > > We don't do it now, do we? No, the PPS module is just that, a module, these days. Terje -- - <Terje.Mathisen at tmsw.no> "almost all programming can be viewed as an exercise in caching"
Re: Large constants in RISC
Author: Scott Lurndal
Date: Sun, 22 Nov 2026 16:22
Date: Sun, 22 Nov 2026 16:22
37 lines
2328 bytes
2328 bytes
Michael S <already5chosen@yahoo.com> writes: >On Sat, 21 Nov 2026 19:49:16 -0000 (UTC) >cross@spitfire.i.gajendra.net (Dan Cross) wrote: > >> >> >But host does not know upper bits at compile time. Except for deeply >> >embedded hosts. But in 64-bit land those are rarity. >> >> It depends on the device, not just the host. On a device that >> is known to only support 32-bit bars, for example, one _can_ >> know at compile time that the upper bits will all be 0. >> > >The point is that not all address bits are known in compile-time. >So, ability to efficiently assemble arbitrary 64-bit (or 48-bit, >or 52-bit) constants in compile time does not help this particular >application much. Or at all. I've worked with modern SoCs that support both a fixed physical address for the on-chip custom devices, as well as providing PCI compatable configuration spaces with dynamically programmed BARs for those same custom devices. The linux kernel and UEFI uses the standard programmed BARs, while the SoC specific firmware uses the fixed physical addresses (where bit 47 == 1). In both cases, software can also use an offset from a 48-bit base address. >For the question at hand it does not matter which entity assigns BARs. >The only thing that matters is that absolute HW addresses are not known >at compile time. > Usually, but not always.
Re: Large constants in RISC
Author: EricP
Date: Sun, 22 Nov 2026 17:23
Date: Sun, 22 Nov 2026 17:23
50 lines
3707 bytes
3707 bytes
On 2026-Nov-20 22:23, Stefan Monnier wrote: > On 2026-Nov-19 08:59, Stefan Monnier wrote: >> aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >>> In a way, it does. Most large constants I see are addresses. >>> AArch64 (almost) always has a 48-bit user space, so except for >>> floating-point doubles and the very occasional large integer, constant >>> loads look like >>> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 >> Interesting. I would have expected that most AArch64 code is position >> independent (e.g. for address space randomization purposes), so I wonder >> in which circumstances we end up with such instruction sequences to >> build hardcoded addresses. > > EricP [2026-11-20 15:47:57] wrote: >> An application or OS can require that certain data structures >> are mapped at predefined virtual addresses. > > I find it hard to believe that these thingies would account for a large > portion of large constants. In many ISAs code can be optimized to smaller a smaller memory model than the full 64b address space because most applications and libraries reside in the first 2GB and access small objects. As both absolute and PC-rel methods are possible on all cpu's, the comparison is a matter of how efficient each is in an ISA, in a compiled memory model. Compiled memory model matters because if one knows that only the lower and upper 2GB will be accessed then compiler can use sign extended 32b values as both relative offsets or absolute addresses. I'm interested is what happens when one tries to use an ISA in its full 64b address space as that is where many inefficiencies show up. Eg. RV requires 2 instructions to calculate a constant in a 32b memory model but 5 instructions in the full 64b memory model. Eg. RV's LD instruction contains a 12b immediate offset but if the offset is larger you must load the immediate, 2 or 5 instructions, add it to a base, then LD using it, 7 instructions. One needs to consider scenarios for: - each ISA, x64, A64, RV, - various memory models: - tiny memory model smaller than 32b, - small memory model using 32b subset inside a 64b address space, - full 64b address space, - absolute and PC-rel addressing, - various address sequences - first access vs subsequent access. - various usage scenarios - unshared vs shared exe or dll (and this brings different OS behaviour into consideration).
Re: Large constants in RISC
Author: EricP
Date: Sun, 22 Nov 2026 19:19
Date: Sun, 22 Nov 2026 19:19
107 lines
6513 bytes
6513 bytes
On 2026-Nov-22 12:23, EricP wrote: > On 2026-Nov-20 22:23, Stefan Monnier wrote: >> On 2026-Nov-19 08:59, Stefan Monnier wrote: >>> aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >>>> In a way, it does. Most large constants I see are addresses. >>>> AArch64 (almost) always has a 48-bit user space, so except for >>>> floating-point doubles and the very occasional large integer, constant >>>> loads look like >>>> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 >>> Interesting. I would have expected that most AArch64 code is position >>> independent (e.g. for address space randomization purposes), so I wonder >>> in which circumstances we end up with such instruction sequences to >>> build hardcoded addresses. >> >> EricP [2026-11-20 15:47:57] wrote: >>> An application or OS can require that certain data structures >>> are mapped at predefined virtual addresses. >> >> I find it hard to believe that these thingies would account for a large >> portion of large constants. > > In many ISAs code can be optimized to smaller a smaller memory model > than the full 64b address space because most applications and libraries > reside in the first 2GB and access small objects. > > As both absolute and PC-rel methods are possible on all cpu's, > the comparison is a matter of how efficient each is in an ISA, > in a compiled memory model. Compiled memory model matters > because if one knows that only the lower and upper 2GB will > be accessed then compiler can use sign extended 32b values > as both relative offsets or absolute addresses. > > I'm interested is what happens when one tries to use an ISA in its > full 64b address space as that is where many inefficiencies show up. > Eg. RV requires 2 instructions to calculate a constant in a 32b memory > model but 5 instructions in the full 64b memory model. > Eg. RV's LD instruction contains a 12b immediate offset but if the > offset is larger you must load the immediate, 2 or 5 instructions, > add it to a base, then LD using it, 7 instructions. > > One needs to consider scenarios for: > - each ISA, x64, A64, RV, > - various memory models: > - tiny memory model smaller than 32b, > - small memory model using 32b subset inside a 64b address space, > - full 64b address space, > - absolute and PC-rel addressing, > - various address sequences - first access vs subsequent access. > - various usage scenarios - unshared vs shared exe or dll > (and this brings different OS behaviour into consideration). > The above examination shows where the weaknesses are in each ISA. This is towards considering what modifications would improve each in different scenarios. Eg. as I noted earlier, defining rBase == R0 => RIP and adding LD & ST instructions with scaled-index address mode then it gives access to the full 64b address space, absolute or RIP-rel addressing, in two instructions. Does it need a similar optimization for branch, call, jump, or case/switches to ranges larger than an ISA supports? How big are the native relative branch/call offsets? If it is just a 12 or 13 bit offset, what is the sequence for a larger range? Does that need to be optimized too? I use the term "branch" and its variations like "branch and link" or "branch subroutine" to mean the destination is relative to the branching instruction, and "jump" and its variations to mean the destination is independent of the jumping instruction. In my terminology RV's Jump And Link JAL instruction would be BAL because it is relative, whereas its Jump and Link Register would remain JALR as it calculates its destination independent of the jumping instruction. RVs conditional branches remain Bcc as they are relative to the branching instruction. RV has a two instruction sequence to unconditional branch relative to any address within 32b, +-2GB, AUIPC and JALR plus a temp register. To branch relative to any address in 64b space requires AUIPC, a LD to load a 64b constant offset from the constant table, ADD to the PC base to get an absolute address, and JALR. If the destination is relocatable, as it would be with a destination in a shared library, then the 64b offset constant would need a relocation table entry so it is patched by the loader (aka an imported DLL library routine trampoline). However that relocation patch could make this code unsharable (depending on how the OS handles shared code and data in EXE and DLL's) so a longer code sequence might be necessary to extract a dynamic address from the GOT table, which is itself located at an absolute address. The above becomes simpler if a new JALR instruction incorporates the same rBase == R0 => RIP definition plus an offset register, which may be R0 the zero register. A general 64 branch relative is LD.L r1 = [RIP + table_offset] // load 64b offset JALR r0, [RIP + r1] // add offset to rip and jump and a general absolute jump is LD.L r1 = [RIP + table_offset] // load absolute address JALR r0, [r1 + r0] // jump there
Re: Large constants in RISC
Author: Thomas Koenig
Date: Mon, 23 Nov 2026 10:47
Date: Mon, 23 Nov 2026 10:47
17 lines
2269 bytes
2269 bytes
EricP <ThatWouldBeTelling@thevillage.com> schrieb: > As both absolute and PC-rel methods are possible on all cpu's, It is possible in principle for Power previous to version 3.1, but it was painful. The system has a TOC pointer in r2, which points to local data and the GOT, restricted to 64k. If you really wanted to do pc-relative, you had to get the PC via a branch and link instruction into the link register, then transfer it from there to a general register. Power version 3.0 added a add PC immediate + shifted instruction. V3.1 finally allows for pc-relative with 34-bit offset, in a 64-bit instruction. -- This USENET posting was made without artificial intelligence, artificial impertinence, artificial arrogance, artificial stupidity, artificial flavorings or artificial colorants.
Re: Large constants in RISC
Author: MitchAlsup
Date: Mon, 23 Nov 2026 18:40
Date: Mon, 23 Nov 2026 18:40
143 lines
7490 bytes
7490 bytes
EricP <ThatWouldBeTelling@thevillage.com> posted:
> On 2026-Nov-22 12:23, EricP wrote:
> > On 2026-Nov-20 22:23, Stefan Monnier wrote:
> >> On 2026-Nov-19 08:59, Stefan Monnier wrote:
> >>> aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote:
> >>>> In a way, it does. Most large constants I see are addresses.
> >>>> AArch64 (almost) always has a 48-bit user space, so except for
> >>>> floating-point doubles and the very occasional large integer, constant
> >>>> loads look like
> >>>> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32
> >>> Interesting. I would have expected that most AArch64 code is position
> >>> independent (e.g. for address space randomization purposes), so I wonder
> >>> in which circumstances we end up with such instruction sequences to
> >>> build hardcoded addresses.
> >>
> >> EricP [2026-11-20 15:47:57] wrote:
> >>> An application or OS can require that certain data structures
> >>> are mapped at predefined virtual addresses.
> >>
> >> I find it hard to believe that these thingies would account for a large
> >> portion of large constants.
> >
> > In many ISAs code can be optimized to smaller a smaller memory model
> > than the full 64b address space because most applications and libraries
> > reside in the first 2GB and access small objects.
> >
> > As both absolute and PC-rel methods are possible on all cpu's,
> > the comparison is a matter of how efficient each is in an ISA,
> > in a compiled memory model. Compiled memory model matters
> > because if one knows that only the lower and upper 2GB will
> > be accessed then compiler can use sign extended 32b values
> > as both relative offsets or absolute addresses.
> >
> > I'm interested is what happens when one tries to use an ISA in its
> > full 64b address space as that is where many inefficiencies show up.
> > Eg. RV requires 2 instructions to calculate a constant in a 32b memory
> > model but 5 instructions in the full 64b memory model.
> > Eg. RV's LD instruction contains a 12b immediate offset but if the
> > offset is larger you must load the immediate, 2 or 5 instructions,
> > add it to a base, then LD using it, 7 instructions.
> >
> > One needs to consider scenarios for:
> > - each ISA, x64, A64, RV,
> > - various memory models:
> > - tiny memory model smaller than 32b,
> > - small memory model using 32b subset inside a 64b address space,
> > - full 64b address space,
> > - absolute and PC-rel addressing,
> > - various address sequences - first access vs subsequent access.
> > - various usage scenarios - unshared vs shared exe or dll
> > (and this brings different OS behaviour into consideration).
> >
>
> The above examination shows where the weaknesses are in each ISA.
> This is towards considering what modifications would improve each in
> different scenarios.
>
> Eg. as I noted earlier, defining rBase == R0 => RIP and adding
> LD & ST instructions with scaled-index address mode then
> it gives access to the full 64b address space, absolute or RIP-rel
> addressing, in two instructions.
Errr...1 instruction:
MEM Rd,[Rbase,Rindex<<swcale,#Displacement64]
1 instruction 3-words
> Does it need a similar optimization for branch, call, jump, or
> case/switches to ranges larger than an ISA supports?
I found no reason to allow switches to use the whole 64-bit address
space. The rest, sure. I do support {10, 18, 34}-bit offset space for
a single switch.
But another optimization is a LD directly into IP with/without
saving return address in R0. I call these JMPX and CALX. If the
linker binds several modules into a final module and resolves
the addresses, I also have the JMPA and CALA versions which
deliver the AGENed address into IP.
> How big are the native relative branch/call offsets?
(effective) 18-bits, 32-bits and 64-bits.
> If it is just a 12 or 13 bit offset, what is the sequence for a
> larger range? Does that need to be optimized too?
If you have universal constants, that question does not need to be ask!
> I use the term "branch" and its variations like "branch and link"
> or "branch subroutine" to mean the destination is relative to the
> branching instruction, and "jump" and its variations to mean the
> destination is independent of the jumping instruction.
> In my terminology RV's Jump And Link JAL instruction would be
> BAL because it is relative, whereas its Jump and Link Register
> would remain JALR as it calculates its destination independent
> of the jumping instruction. RVs conditional branches remain Bcc
> as they are relative to the branching instruction.
>
> RV has a two instruction sequence to unconditional branch relative
> to any address within 32b, +-2GB, AUIPC and JALR plus a temp register.
> To branch relative to any address in 64b space requires AUIPC,
> a LD to load a 64b constant offset from the constant table,
> ADD to the PC base to get an absolute address, and JALR.
Sounds painful. I can do that in one 3-word instruction.
> If the destination is relocatable, as it would be with a destination
> in a shared library, then the 64b offset constant would need a
> relocation table entry so it is patched by the loader
> (aka an imported DLL library routine trampoline).
CALX [IP,,GOT[name]]
No trampoline needed.
> However that relocation patch could make this code unsharable
> (depending on how the OS handles shared code and data in EXE and DLL's)
> so a longer code sequence might be necessary to extract a
> dynamic address from the GOT table,
> which is itself located
> at an absolute address.
Not necessarily.
> The above becomes simpler if a new JALR instruction incorporates
> the same rBase == R0 => RIP definition plus an offset register,
> which may be R0 the zero register.
>
> A general 64 branch relative is
>
> LD.L r1 = [RIP + table_offset] // load 64b offset
> JALR r0, [RIP + r1] // add offset to rip and jump
>
> and a general absolute jump is
>
> LD.L r1 = [RIP + table_offset] // load absolute address
> JALR r0, [r1 + r0] // jump there
>
>
Re: Large constants in RISC
Author: EricP
Date: Mon, 23 Nov 2026 18:55
Date: Mon, 23 Nov 2026 18:55
36 lines
3212 bytes
3212 bytes
On 2026-Nov-23 05:47, Thomas Koenig wrote: > EricP <ThatWouldBeTelling@thevillage.com> schrieb: > >> As both absolute and PC-rel methods are possible on all cpu's, > > It is possible in principle for Power previous to version 3.1, but > it was painful. The system has a TOC pointer in r2, which points > to local data and the GOT, restricted to 64k. If you really wanted > to do pc-relative, you had to get the PC via a branch and link > instruction into the link register, then transfer it from there to > a general register. Power version 3.0 added a add PC immediate > + shifted instruction. V3.1 finally allows for pc-relative with > 34-bit offset, in a 64-bit instruction. > The 32b x86 also does not have native PC-rel addressing, just absolute, register relative, with optional scaled-index. To get the PC into a register one calls a two instruction routine, which pushes the incremented PC on the stack, loads the bottom of stack dword into a register and returns. This routine could be positioned after a basic block near by. To make that perform well the core needs store-to-load forwarding so it can pull the PC from the store queue and not wait for the value to hit memory, and a branch predictor so the call does not cause a stall. A "MOVPC Rd" instruction would have been simpler. An OS could support shared DLLs without PC-rel position independent code but the OS image loader's job is more complicated as it must decide whether to reuse an existing image already located at an absolute address, or make a new relocated version of that image file. It gets complicated because each loaded DLL can depend on many other DLLs loaded at their currently assigned absolute address. Doing that optimally sounds like a bin packing problem which are NP-hard, and since sharing isn't strictly required then probably a "best effort" at reuse would do.
Re: Large constants in RISC
Author: MitchAlsup
Date: Mon, 23 Nov 2026 20:35
Date: Mon, 23 Nov 2026 20:35
47 lines
3649 bytes
3649 bytes
EricP <ThatWouldBeTelling@thevillage.com> posted: > On 2026-Nov-23 05:47, Thomas Koenig wrote: > > EricP <ThatWouldBeTelling@thevillage.com> schrieb: > > > >> As both absolute and PC-rel methods are possible on all cpu's, > > > > It is possible in principle for Power previous to version 3.1, but > > it was painful. The system has a TOC pointer in r2, which points > > to local data and the GOT, restricted to 64k. If you really wanted > > to do pc-relative, you had to get the PC via a branch and link > > instruction into the link register, then transfer it from there to > > a general register. Power version 3.0 added a add PC immediate > > + shifted instruction. V3.1 finally allows for pc-relative with > > 34-bit offset, in a 64-bit instruction. > > > > The 32b x86 also does not have native PC-rel addressing, > just absolute, register relative, with optional scaled-index. > To get the PC into a register one calls a two instruction routine, > which pushes the incremented PC on the stack, loads the bottom > of stack dword into a register and returns. > This routine could be positioned after a basic block near by. > > To make that perform well the core needs store-to-load forwarding > so it can pull the PC from the store queue and not wait for the > value to hit memory, and a branch predictor so the call does not > cause a stall. A "MOVPC Rd" instruction would have been simpler. > > An OS could support shared DLLs without PC-rel position independent code While most ISAs naturally support IP-relative PIC, most ISAs do not support PIC switches with dense CASEs. These are generally done with tables of pointers, relocated at link time. My 66000 supports PIC switches directly. Our experiments have shown that the very vast majority (98%+) of these can use 16-bit tables (±17-bits of displacement) > but the OS image loader's job is more complicated as it must decide > whether to reuse an existing image already located at an absolute address, > or make a new relocated version of that image file. It gets complicated > because each loaded DLL can depend on many other DLLs loaded at their > currently assigned absolute address. Doing that optimally sounds like > a bin packing problem which are NP-hard, and since sharing isn't > strictly required then probably a "best effort" at reuse would do.
Re: Large constants in RISC
Author: aph
Date: Tue, 24 Nov 2026 13:46
Date: Tue, 24 Nov 2026 13:46
39 lines
3153 bytes
3153 bytes
Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>
> Hmm... didn't think of that. Indeed this could lead to actual
> addresses embedded in the code. Then again, I'd expect most such JIT
> setups to want to keep most of those addresses in data rather than in
> the code so the GC can more easily trace through them.
GCs have not the slightest difficulty tracing through embedded
addresses. We use whatever form of address constants is fastest.
> So, we now have 3 suggested circumstances, but it's not clear if these
> occur enough to explain Andrew's experience.
>
> Andrew, do you know what causes all those large constant addresses
> you're seeing?
Very common constants are classes used in expressions, such as
new File("filename")
or casts between types
File x = (File)anObject;
Casts are more common than is immediately apparent, because the
compiler inserts them when it can't prove the type of an object.
The other important case is metadata. We gather a lot of information
about running code: branch frequency, type frequency, and so on. Every
metadata address is a constant in the generated code. After a while,
code is recompiled and we use all the gathered informtion to drive
inlining and other optimizations.
[ We actually have two compilers. One is optimized to generate code as
fast as possible in order to gather metadata about the program, and
the other compiler optimizes everything, producing small code that
doesn't collect metadata. ]
Andrew.
Re: Large constants in RISC
Author: Stefan Monnier
Date: Wed, 25 Nov 2026 04:21
Date: Wed, 25 Nov 2026 04:21
50 lines
4016 bytes
4016 bytes
aph@littlepinkcloud.invalid [2026-11-24 13:46:24] wrote:
> Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>> Hmm... didn't think of that. Indeed this could lead to actual
>> addresses embedded in the code. Then again, I'd expect most such JIT
>> setups to want to keep most of those addresses in data rather than in
>> the code so the GC can more easily trace through them.
> GCs have not the slightest difficulty tracing through embedded
> addresses. We use whatever form of address constants is fastest.
Maybe it's not "hard" compared to the rest of what it takes to write
a high performance GC, but it's still a fair bit more work to have to
tell the GC where to find the few bitfields that need to be stitched
together to make up the actual pointer (it definitely makes tracing
through such references slower), and if you have a moving GC it also
makes moves harder, because of the need to both retrofit the new address
into the corresponding bitfields (which also means you can't use an
instruction sequence special-tailored to the original address) and take
care of updating/flushing the I-cache.
>> Andrew, do you know what causes all those large constant addresses
>> you're seeing?
>
> Very common constants are classes used in expressions, such as
>
> new File("filename")
>
> or casts between types
>
> File x = (File)anObject;
>
> Casts are more common than is immediately apparent, because the
> compiler inserts them when it can't prove the type of an object.
Interesting, thanks. So it is indeed the JIT case suggested by Anton!
> The other important case is metadata. We gather a lot of information
> about running code: branch frequency, type frequency, and so on. Every
> metadata address is a constant in the generated code. After a while,
> code is recompiled and we use all the gathered informtion to drive
> inlining and other optimizations.
Hmm... even more interesting: since the metadata-gathering is part of
the code that's generated cheaply, I'd have expected that code to use
a simple approach that always loads the large constants from some data
section rather than "use whatever form of address constants is fastest"
(which I would have expected is used only for the high-performance
version of the code).
=== Stefan
Re: Large constants in RISC
Author: Terje Mathisen
Date: Wed, 25 Nov 2026 11:35
Date: Wed, 25 Nov 2026 11:35
47 lines
3524 bytes
3524 bytes
EricP wrote: > On 2026-Nov-23 05:47, Thomas Koenig wrote: >> EricP <ThatWouldBeTelling@thevillage.com> schrieb: >> >>> As both absolute and PC-rel methods are possible on all cpu's, >> >> It is possible in principle for Power previous to version 3.1, but >> it was painful. The system has a TOC pointer in r2, which points >> to local data and the GOT, restricted to 64k. If you really wanted >> to do pc-relative, you had to get the PC via a branch and link >> instruction into the link register, then transfer it from there to >> a general register. Power version 3.0 added a add PC immediate >> + shifted instruction. V3.1 finally allows for pc-relative with >> 34-bit offset, in a 64-bit instruction. >> > > The 32b x86 also does not have native PC-rel addressing, > just absolute, register relative, with optional scaled-index. > To get the PC into a register one calls a two instruction routine, > which pushes the incremented PC on the stack, loads the bottom > of stack dword into a register and returns. > This routine could be positioned after a basic block near by. > > To make that perform well the core needs store-to-load forwarding > so it can pull the PC from the store queue and not wait for the > value to hit memory, and a branch predictor so the call does not > cause a stall. A "MOVPC Rd" instruction would have been simpler. We asm programmers didn't use that function call, we just did call next next: pop bx Two instructions, 4 bytes but when return predictors turned up having a CALL without the expected RET caused mispredicts. Terje -- - <Terje.Mathisen at tmsw.no> "almost all programming can be viewed as an exercise in caching"
Re: Large constants in RISC
Author: aph
Date: Wed, 25 Nov 2026 15:57
Date: Wed, 25 Nov 2026 15:57
67 lines
4846 bytes
4846 bytes
Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> aph@littlepinkcloud.invalid [2026-11-24 13:46:24] wrote:
>> Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>>> Hmm... didn't think of that. Indeed this could lead to actual
>>> addresses embedded in the code. Then again, I'd expect most such JIT
>>> setups to want to keep most of those addresses in data rather than in
>>> the code so the GC can more easily trace through them.
>> GCs have not the slightest difficulty tracing through embedded
>> addresses. We use whatever form of address constants is fastest.
>
> Maybe it's not "hard" compared to the rest of what it takes to write
> a high performance GC, but it's still a fair bit more work to have to
> tell the GC where to find the few bitfields that need to be stitched
> together to make up the actual pointer (it definitely makes tracing
> through such references slower), and if you have a moving GC it also
> makes moves harder, because of the need to both retrofit the new address
> into the corresponding bitfields (which also means you can't use an
> instruction sequence special-tailored to the original address) and take
> care of updating/flushing the I-cache.
Yes, we have to do all of that, but it's very-well-understood
technology that we've been using for decades. Object references in
code are treated as "old": the referenced objects are in regions that
rarely get moved. We have to be able to scan live stacks and code and
a variety of other places, and compared with that unpacking address
constants is a fairly easy case.
>>> Andrew, do you know what causes all those large constant addresses
>>> you're seeing?
>>
>> Very common constants are classes used in expressions, such as
>>
>> new File("filename")
>>
>> or casts between types
>>
>> File x = (File)anObject;
>>
>> Casts are more common than is immediately apparent, because the
>> compiler inserts them when it can't prove the type of an object.
>
> Interesting, thanks. So it is indeed the JIT case suggested by Anton!
Yes.
>> The other important case is metadata. We gather a lot of information
>> about running code: branch frequency, type frequency, and so on. Every
>> metadata address is a constant in the generated code. After a while,
>> code is recompiled and we use all the gathered informtion to drive
>> inlining and other optimizations.
>
> Hmm... even more interesting: since the metadata-gathering is part of
> the code that's generated cheaply,
Why is emitting three instructions harder than emitting a PC-relative
load and storing a pointer somewhere?
> I'd have expected that code to use a simple approach that always
> loads the large constants from some data section rather than "use
> whatever form of address constants is fastest" (which I would have
> expected is used only for the high-performance version of the code).
Metadata is very rarely moved, so there's no disadvantage to just
embedding addresses in the code. Sure, we could put address constants
in a local pool instead, but it wouldn't save any space.
Andrew.
Re: Large constants in RISC
Author: MitchAlsup
Date: Wed, 25 Nov 2026 18:57
Date: Wed, 25 Nov 2026 18:57
47 lines
3221 bytes
3221 bytes
Terje Mathisen <terje.mathisen@tmsw.no> posted:
> EricP wrote:
> > On 2026-Nov-23 05:47, Thomas Koenig wrote:
> >> EricP <ThatWouldBeTelling@thevillage.com> schrieb:
> >>
> >>> As both absolute and PC-rel methods are possible on all cpu's,
> >>
> >> It is possible in principle for Power previous to version 3.1, but
> >> it was painful. The system has a TOC pointer in r2, which points
> >> to local data and the GOT, restricted to 64k. If you really wanted
> >> to do pc-relative, you had to get the PC via a branch and link
> >> instruction into the link register, then transfer it from there to
> >> a general register. Power version 3.0 added a add PC immediate
> >> + shifted instruction. V3.1 finally allows for pc-relative with
> >> 34-bit offset, in a 64-bit instruction.
> >>
> >
> > The 32b x86 also does not have native PC-rel addressing,
> > just absolute, register relative, with optional scaled-index.
> > To get the PC into a register one calls a two instruction routine,
> > which pushes the incremented PC on the stack, loads the bottom
> > of stack dword into a register and returns.
> > This routine could be positioned after a basic block near by.
> >
> > To make that perform well the core needs store-to-load forwarding
> > so it can pull the PC from the store queue and not wait for the
> > value to hit memory, and a branch predictor so the call does not
> > cause a stall. A "MOVPC Rd" instruction would have been simpler.
>
> We asm programmers didn't use that function call, we just did
>
> call next
> next:
> pop bx
>
> Two instructions, 4 bytes
>
> but when return predictors turned up having a CALL without the expected
> RET caused mispredicts.
Which is why the proper thing to do _NOW_ is to make IP available as
a base register in memory reference instructions. {It is the predictor
friendly way forward}
> Terje
>
Page 2 of 2 • 100 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads