abi: a type may name the form it takes in a register - #170
Merged
Conversation
`reg_form<T>` is the customization point: it defaults to T, and a type whose storage form is not its register form - a packed integer that is a byte array in memory and a plain word in a register - specializes it to name that word. `pass_in_reg` then carries the register form and converts at the boundary, once in each direction, so a comparator or a lookup receives the word and never re-assembles it per compare. A trivial wrapper inherits its value's register form when it names its `value_type`, can `rebind` to another value type, has its value's size and converts to and from the wrapper of the register form (`reg_form_wrapper`). A wrapper whose narrowing is deliberately explicit does not qualify and keeps travelling as itself. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pass_in_reg<T>decides by value versus by reference oncan_be_passed_in_reg<T>, and then carriesTitself. That is the right call for a type whose in-memory form is also its natural register form. It is not for a type that is a byte array in memory and a plain word in a register - a packed integer, say: passing it by value moves the bytes, and every comparator or lookup that receives it re-assembles the word per compare.This adds one customization point:
reg_form<T>names the form aTtakes in a register. It defaults toT, so nothing changes for any existing type. A type specializes it to name its register form; the contract is round-trip:Tmust convert to the form and be constructible from it.pass_in_reg<T>then storesreg_form_t<T>, decides by-value on that, converts on construction and offers the conversion back (operator value_type(), only when the two differ), so the boundary converts once in each direction and a consumer that wants the word gets the word.reg_form_wrapper: a trivial wrapper inherits its value's register form when it names itsvalue_type, canrebinditself to another value type, has its value's size and converts to and from the wrapper of the register form. A wrapper whose narrowing conversion is deliberately explicit does not qualify and keeps travelling as itself - opting in is one conversion function on the wrapper.The primary
pass_in_regconstructor is unchanged; brace-init of the stored form from the argument covers the conversion. The deduction guide is unchanged.Measured downstream on an LTO Release build with a 3-byte packed integer as the specialized type: the change is neutral to a few percent better on the workloads where the packed key is probed in a hot loop, and neutral elsewhere. On the SysV ABI a 3-byte trivially-copyable struct already travels in a register, so the gain is the removed per-compare re-assembly rather than the calling convention.
🤖 Generated with Claude Code