• Re: Large constants in RISC

    From EricP@ThatWouldBeTelling@thevillage.com to comp.arch on Sun Nov 22 14:19:01 2026
    From Newsgroup: comp.arch

    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


    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From Thomas Koenig@tkoenig@netcologne.de to comp.arch on Mon Nov 23 10:47:45 2026
    From Newsgroup: comp.arch

    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.
    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From MitchAlsup@user5857@newsgrouper.org.invalid to comp.arch on Mon Nov 23 18:40:54 2026
    From Newsgroup: comp.arch

    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


    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From EricP@ThatWouldBeTelling@thevillage.com to comp.arch on Mon Nov 23 13:55:02 2026
    From Newsgroup: comp.arch

    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.

    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From MitchAlsup@user5857@newsgrouper.org.invalid to comp.arch on Mon Nov 23 20:35:38 2026
    From Newsgroup: comp.arch

    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.

    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From aph@aph@littlepinkcloud.invalid to comp.arch on Tue Nov 24 13:46:24 2026
    From Newsgroup: comp.arch

    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.
    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From Stefan Monnier@monnier@iro.umontreal.ca to comp.arch on Tue Nov 24 23:21:48 2026
    From Newsgroup: comp.arch

    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
    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From Terje Mathisen@terje.mathisen@tmsw.no to comp.arch on Wed Nov 25 12:35:29 2026
    From Newsgroup: comp.arch

    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"
    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From aph@aph@littlepinkcloud.invalid to comp.arch on Wed Nov 25 15:57:57 2026
    From Newsgroup: comp.arch

    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.
    --- Synchronet 3.21e-Linux NewsLink 1.2
  • From MitchAlsup@user5857@newsgrouper.org.invalid to comp.arch on Wed Nov 25 18:57:30 2026
    From Newsgroup: comp.arch

    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

    --- Synchronet 3.21e-Linux NewsLink 1.2