← Back to Pull requests

Size a primitive type to its alignment #62852

Merged
fingolfin merged 20 files (348 lines) on Oct 01, 2026 22:56 UTC
View on GitHub
fingolfin fingolfin commented on Aug 22, 2026 07:17 UTC
Author

First stab at addressing issue #61361. Would unblock item 2 of #62441.

Core.sizeof of a primitive type so far rounded its bit size up to the next multiple of 8. As a result, in many cases it was neither the allocation size nor the packed bit size. For example, primitive type T 17 end had sizeof 3 against Base.aligned_sizeof 4, where C23 gives sizeof(unsigned _BitInt(17)) == 4. Triage settled on following C.

Change the size of primitive types to LLT_ALIGN(nbytes, alignment), so Core.sizeof and Base.aligned_sizeof now agree. The trailing bytes are padding: we widen unused_bits to hold up to 8 * (MAX_ALIGN - 1) + 7. We only compare and hash the value bytes in primitive_bits_equal and immut_id_. Base.padding reports the new trailing padding, and the packed-copy path learns to handle a type with no fields.

On the upside, accessing an odd-bit integer (at least when used in isolation) now takes one power-of-two load or store instead of several sub-word ones. ABI-coerced register types keep their own width, since those may sit on a smaller object.

On the downside, Core.sizeof changes for some existing byte-multiple types. For example primitive type Int24 <: Signed 24 end (which has been valid since at least Julia 1.0) is affected; that one goes from sizeof 3 to 4. This could break packages? Not sure if anyone uses that, though, perhaps @PatrickHaecker ? Anyway such types now return true when passed to datatype_haspadding.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---

Note to reviewers: I have not yet de-sloppified comments, nor have I extensively reviewed this myself. I mainly post it here so people see I'm trying to tackle this; but of course if someone wants to give feedback, that's highly welcome.

Diff: 348 lines (+261 / -87) across 20 files Reviewer: topolarity Review duration: 58538m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.67) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.67 · 348 lines diff across 20 files
0.67
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.89 · 5 substantive comment(s)
0.89
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.350
Mass File Touch mass_file_touch
0.080
Real PR Conversation Timeline
39 items
nhz2 nhz2 commented on code on Aug 22, 2026 15:33 UTC
Code Review
base/reinterpretarray.jl:1

This function can be deleted and replaced with ispacked now that sizeof(T) is the same as aligned_sizeof(T).

fingolfin fingolfin commented on code on Aug 22, 2026 16:49 UTC
Code Review
base/reinterpretarray.jl:1

thanks, done

nhz2 nhz2 commented on Aug 22, 2026 18:55 UTC
Contributor

For example primitive type Int24 <: Signed 24 end (which has been valid since at least Julia 1.0) is affected; that one goes from sizeof 3 to 4.

I think this will likely fix more packages than it will break. For example https://github.com/JuliaSIMD/Polyester.jl/issues/167

@mkitti 's package https://github.com/JaneliaSciComp/UInt12Arrays.jl might need some updates (the package tests are failing on both nightly and this PR).

mkitti mkitti commented on Aug 22, 2026 19:13 UTC
Contributor

I'm a little confused about what changed and when.

I think there might be a need to specify if a primitive should be aligned or packed.

Packed data is of particular interest for data that has been serialized to disk and where alignment may not be congruent with the primary objective of minimizing space.

nhz2 nhz2 commented on Aug 23, 2026 00:50 UTC
Contributor

@mkitti here is a fix for UInt12Arrays.jl https://github.com/JaneliaSciComp/UInt12Arrays.jl/pull/14

https://github.com/JuliaLang/julia/pull/62771 changed ReinterpretArray to use aligned_sizeof instead of sizeof for element size and with this PR sizeof is the same as aligned_sizeof for primitive types.

I think right now you would need to use a struct with UInt8 fields to make a 24 bit integer type with alignment of one byte.

fingolfin fingolfin commented on Sep 05, 2026 13:20 UTC
Author

@topolarity thanks for merging PR #62492 ! If you are up to it, here is my follow-up ;-)

nhz2 nhz2 commented on code on Sep 07, 2026 16:16 UTC
Code Review
doc/src/manual/types.md:1
@@ -300,10 +300,13 @@ The number of bits gives the declared logical width of the primitive type, and t
 new type a name. A primitive type can optionally be declared to be a subtype of some supertype. If a
 supertype is omitted, then the type defaults to having `Any` as its immediate supertype. The
 declaration of [`Bool`](@ref) above therefore means that a boolean value has a logical width of eight
-bits, and has [`Integer`](@ref) as its immediate supertype. Primitive types continue to use
-byte-rounded storage, so `sizeof(T)` rounds their storage size up to a whole number of bytes even
-when their logical width is not a multiple of 8 bits. Use `Core.bitsizeof(T)` to query the declared
-logical width.
+bits, and has [`Integer`](@ref) as its immediate supertype. A primitive type of `N` bits keeps
+its value in the first `cld(N, 8)` bytes. Its alignment is the smallest power of two of bytes that
+holds them, capped at the platform's maximum alignment, and `sizeof(T)` rounds the value bytes up
+to a multiple of that alignment. This is the layout C23 gives `_BitInt(N)`, and it always equals
+`Base.aligned_sizeof(T)`. Bits past the declared width are padding and take no part in comparison

`Base.elsize(Array{T})`. Bits past the declared width are padding and take no part in comparison

Base.aligned_sizeof is currently an internal function.

nhz2 nhz2 commented on code on Sep 07, 2026 16:18 UTC
Code Review
NEWS.md:1
@@ -65,6 +65,12 @@ Language changes
   unaffected, and a method on `Type{Int}` remains more specific than one on `DataType`
   ([#33136], [#62141]).
 
+* `Core.sizeof` of a `primitive type` now rounds its value bytes up to a multiple of its
+  alignment rather than only up to a whole byte, so it always equals `Base.aligned_sizeof`,

  alignment rather than only up to a whole byte, so it always equals `Base.elsize(Array{T})`,

fingolfin fingolfin commented on Sep 11, 2026 09:04 UTC
Author

@nhz2 thanks, fixed

topolarity topolarity commented on Sep 24, 2026 15:15 UTC
Contributor

Thanks for this @fingolfin!

Changes largely look good to me. I think the intrinsics still need updating for the new padding bits IIUC (in particular, I think APINT_NBYTES is wrong now)

fingolfin fingolfin commented on Sep 24, 2026 23:40 UTC
Author

Got this together with Claude:

APINT_NBYTES itself is right: APInt reads and writes only the cld(N, 8) value bytes. The bugs were where a result becomes a sizeof-sized value, which now has padding:

  • FPtoInt copied sizeof bytes out of its word buffer, over-reading the stack (129 bits: 24-byte buffer, sizeof 32).

  • The runtime intrinsic wrappers boxed buffers whose padding APInt never wrote.

  • Codegen stored i24 into 4-byte boxes and stack slots, returned wide primitives through sret without their padding, and kept a C callee's sret padding as is.
  • db7c2d0715 and 2bb4429dd2 fix these, with tests that fail without the fixes.

    topolarity topolarity commented on code on Sep 25, 2026 14:14 UTC
    Code Review
    doc/src/manual/types.md:1
    @@ -300,10 +300,13 @@ The number of bits gives the declared logical width of the primitive type, and t
     new type a name. A primitive type can optionally be declared to be a subtype of some supertype. If a
     supertype is omitted, then the type defaults to having `Any` as its immediate supertype. The
     declaration of [`Bool`](@ref) above therefore means that a boolean value has a logical width of eight
    -bits, and has [`Integer`](@ref) as its immediate supertype. Primitive types continue to use
    -byte-rounded storage, so `sizeof(T)` rounds their storage size up to a whole number of bytes even
    -when their logical width is not a multiple of 8 bits. Use `Core.bitsizeof(T)` to query the declared
    -logical width.
    +bits, and has [`Integer`](@ref) as its immediate supertype. A primitive type of `N` bits keeps
    +its value in the first `cld(N, 8)` bytes. Its alignment is the smallest power of two of bytes that
    +holds them, capped at the platform's maximum alignment, and `sizeof(T)` rounds the value bytes up
    +to a multiple of that alignment. This is the layout C23 gives `_BitInt(N)`, and it always equals

    Unfortunately this is not quite true.. Crazy as it is, the System V C ABI defines _BitInt(128) and int128_t to have different alignment.

    Julia aligns Int128 to follow the int128_t convention, so its alignments diverge from BitInt for N > 64

    topolarity topolarity commented on Sep 25, 2026 14:17 UTC
    Reviewer

    Not really a bug, but APInt_sext should probably zero-pad rather than sign-pad since that is what we do in LLVM / everywhere else.

    Claude 🤖 points out that static_constant_instance in cgutils also needs to be careful not to overread the (LLVM) APInt buffer for primitives with padding. I think the @cfunction sret fix is also incomplete - there's another pathway that still does a too-large store.

    fingolfin fingolfin commented on Sep 27, 2026 08:46 UTC
    Author

    Thanks @topolarity, all four were real. An audit for the same class of bug found more, mostly older than this PR, so I split the work:

  • #63408, which this PR is now stacked on, has the fixes that also apply to master. Among them, APInt_sext no longer sign-pads, and flipsign_int and out-of-range fptoui/fptosi clear the bits above the width too.

  • Here, static_constant_instance copies only the APInt words, and a padded primitive no longer fuses the @cfunction sret with the callee's. The manual now says the _BitInt(N) match holds up to 64 bits, since wider types take Int128's alignment. Package images and compressed IR store the padding as zeros.
  • The size change also broke ccall's argument extension. It added signext/zeroext when sizeof < 4, so 17- to 24-bit types lost them once their sizeof became 4, although clang extends _BitInt(N) for N < 32 on most targets. It now decides by the width; RISC-V keeps sign-extending everything stored in 4 bytes, as clang does there.

    Padding stays zero rather than sign-filled: C leaves it unspecified on x86-64 and AArch64, and zero does not depend on signedness, so it survives reinterpret between signed and unsigned types.

    topolarity topolarity commented on code on Sep 30, 2026 14:20 UTC
    Code Review
    src/cgutils.cpp:1
    @@ -742,6 +742,19 @@ static Type *julia_primitive_storage_type(Type *register_type)
         return IntegerType::get(IT->getContext(), storage_bits);
     }
     
    +// The width `jt` occupies in memory. A primitive type's size is its value bytes
    +// rounded up to a multiple of its alignment, so an odd-bit integer is reached with
    +// one power-of-two access rather than several.
    +// Only the canonical register type widens this way; ABI coercions, which may sit on
    +// a smaller object, keep their own width.
    +static Type *julia_memory_access_type(Type *register_type, jl_value_t *jt)
    +{
    +    if (jl_is_primitivetype(jt) && register_type->isIntegerTy() &&
    +            cast<IntegerType>(register_type)->getBitWidth() == jl_datatype_nbits((jl_datatype_t*)jt))
    +        return Type::getIntNTy(register_type->getContext(), 8 * jl_datatype_size(jt));
    +    return zext_struct_type(register_type);

    Does zext_struct_type also need to widen these primitive types? IIUC it still widens e.g. i17 to i24 ATM

    topolarity topolarity commented on code on Sep 30, 2026 14:25 UTC
    Code Review
    src/serialize.h:1
    @@ -74,6 +74,19 @@ static inline uint32_t read_uint32(ios_t *s) JL_NOTSAFEPOINT
     #define read_uint(s) read_uint32(s)
     #endif
     
    +// Write a primitive value with the bits above its width zeroed, so the output
    +// does not depend on whatever padding the value was stored with.
    +static inline void write_primitive_bits(ios_t *s, const char *data, jl_datatype_t *dt) JL_NOTSAFEPOINT

    FWIW I think the rollout of this is incomplete. We already write structs with non-zero'd padding bytes to serialized IR / heap objects IIUC.

    We can try to clean that up of course (for determinism mostly?) but not sure the partial job is necessary until we do it completely

    fingolfin fingolfin commented on code on Sep 30, 2026 21:24 UTC
    Code Review
    src/serialize.h:1
    @@ -74,6 +74,19 @@ static inline uint32_t read_uint32(ios_t *s) JL_NOTSAFEPOINT
     #define read_uint(s) read_uint32(s)
     #endif
     
    +// Write a primitive value with the bits above its width zeroed, so the output
    +// does not depend on whatever padding the value was stored with.
    +static inline void write_primitive_bits(ios_t *s, const char *data, jl_datatype_t *dt) JL_NOTSAFEPOINT

    Yeah OK, agreed. I had Claude look into it and there are a bunch of places that would also need to be touched... but I think this is not the time and place to do it. So instead of doing ahead half-assed job, I'll instead drop it here, and then a follow-up PR can address this. Or, as Claude puts it: "canonicalizing all padding when serializing, nested fields and struct gaps included, belongs in its own PR"

    fingolfin fingolfin commented on code on Sep 30, 2026 21:51 UTC
    Code Review
    src/cgutils.cpp:1
    @@ -742,6 +742,19 @@ static Type *julia_primitive_storage_type(Type *register_type)
         return IntegerType::get(IT->getContext(), storage_bits);
     }
     
    +// The width `jt` occupies in memory. A primitive type's size is its value bytes
    +// rounded up to a multiple of its alignment, so an odd-bit integer is reached with
    +// one power-of-two access rather than several.
    +// Only the canonical register type widens this way; ABI coercions, which may sit on
    +// a smaller object, keep their own width.
    +static Type *julia_memory_access_type(Type *register_type, jl_value_t *jt)
    +{
    +    if (jl_is_primitivetype(jt) && register_type->isIntegerTy() &&
    +            cast<IntegerType>(register_type)->getBitWidth() == jl_datatype_nbits((jl_datatype_t*)jt))
    +        return Type::getIntNTy(register_type->getContext(), 8 * jl_datatype_size(jt));
    +    return zext_struct_type(register_type);

    Yes, in this fallback i17 still becomes i24. That happens when a tuple or struct is stored as one LLVM value, e.g. when it comes back from a non-inlined call: Claude checked that a Tuple{T17,UInt8} is then stored as store i24 plus store i8, so the 4th byte of the T17 field keeps whatever was there. Loads ignore that byte, so this is only about padding contents, same as the serialize question. Widening it here isn't straightforward: zext_struct_type only sees the LLVM type, and it is also used on memory coming from C, where e.g. a _BitInt(129) is smaller than our sizeof. So I'd leave that for a follow-up on padding.

    topolarity topolarity commented on code on Sep 30, 2026 21:54 UTC
    Code Review
    src/cgutils.cpp:1
    @@ -742,6 +742,19 @@ static Type *julia_primitive_storage_type(Type *register_type)
         return IntegerType::get(IT->getContext(), storage_bits);
     }
     
    +// The width `jt` occupies in memory. A primitive type's size is its value bytes
    +// rounded up to a multiple of its alignment, so an odd-bit integer is reached with
    +// one power-of-two access rather than several.
    +// Only the canonical register type widens this way; ABI coercions, which may sit on
    +// a smaller object, keep their own width.
    +static Type *julia_memory_access_type(Type *register_type, jl_value_t *jt)
    +{
    +    if (jl_is_primitivetype(jt) && register_type->isIntegerTy() &&
    +            cast<IntegerType>(register_type)->getBitWidth() == jl_datatype_nbits((jl_datatype_t*)jt))
    +        return Type::getIntNTy(register_type->getContext(), 8 * jl_datatype_size(jt));
    +    return zext_struct_type(register_type);

    I see - In that case, I think the only reason this exists is for Bool which is i1 but also !haspadding

    I'm OK to leave it in this awkward state though. Maybe we can clean it up when we clean up Bool.

    topolarity topolarity commented on code on Sep 30, 2026 21:58 UTC
    Code Review
    base/essentials.jl:1
    @@ -798,7 +798,8 @@ end
         sizeof(obj)
     
     Size, in bytes, of the canonical binary representation of the given `DataType` `T`, if any.
    -Or the size, in bytes, of object `obj` if it is not a `DataType`.
    +Or the size, in bytes, of object `obj` if it is not a `DataType`. The size of a primitive type
    +includes padding past its declared width, which `Core.bitsizeof` gives.

    includes padding, while `Core.bitsizeof` does not.
    

    topolarity topolarity commented on code on Sep 30, 2026 22:01 UTC
    Code Review
    base/reinterpretarray.jl:1
    @@ -753,7 +753,9 @@ function non_padding_bytes(T::DataType)::Memory{Bool}
     end
     function fill_nonpadding_bytes!(T::DataType, offset::Int, used::Memory{Bool})
         if isprimitivetype(T)
    -        for i in 1:sizeof(T)
    +        # sizeof rounds the value bytes up to a multiple of the alignment, so the
    +        # bytes past them are padding; a byte holding any value bits is not
    +        for i in 1:((Core.bitsizeof(T) + 7) >> 3)

            for i in 1:div(Core.bitsizeof(T), 8, RoundUp)
    

    or cld if you prefer - just a suggestion

    That said, I don't know if non_padding_bytes is well-defined for non-byte-multiple primitives. Might be good to error here for now.

    topolarity topolarity commented on code on Sep 30, 2026 22:05 UTC
    Code Review
    doc/src/manual/types.md:1
    @@ -300,14 +300,19 @@ The number of bits gives the declared logical width of the primitive type, and t
     new type a name. A primitive type can optionally be declared to be a subtype of some supertype. If a
     supertype is omitted, then the type defaults to having `Any` as its immediate supertype. The
     declaration of [`Bool`](@ref) above therefore means that a boolean value has a logical width of eight
    -bits, and has [`Integer`](@ref) as its immediate supertype. Primitive types continue to use
    -byte-rounded storage, so `sizeof(T)` rounds their storage size up to a whole number of bytes even
    -when their logical width is not a multiple of 8 bits. Use `Core.bitsizeof(T)` to query the declared
    -logical width.
    +bits, and has [`Integer`](@ref) as its immediate supertype. A primitive type of `N` bits keeps
    +its value in the first `cld(N, 8)` bytes. Its alignment is that of the smallest of `Int8`,
    +`Int16`, `Int32` and `Int64` that holds those bytes, or that of `Int128` if none does, and
    +`sizeof(T)` rounds the value bytes up to a multiple of that alignment; it always equals
    +`Base.elsize(Array{T})`. Up to 64 bits this is the layout the platform's C ABI gives
    +`_BitInt(N)`. Wider types can differ: x86-64 aligns `_BitInt(136)` to 8 bytes with size 24, while
    +`primitive type T 136 end` has alignment 16 and size 32. Bits past the declared width are padding
    +and take no part in comparison or hashing. Use `Core.bitsizeof(T)` to query the declared logical
    +width — for `primitive type T 24 end`, `Core.bitsizeof(T)` is 24 while `sizeof(T)` is 4.

    The _BitInt(128) vs. uint128_t example might be more illustrative here, but just a suggestion

    topolarity topolarity commented on code on Sep 30, 2026 22:07 UTC
    Code Review
    src/APInt.c:1

    unsigned onumbits = jl_datatype_nbits(oty);
    unsigned onumbytes = APINT_NBYTES(onumbits);
    unsigned onumbytes_padding = jl_datatype_size(oty) - onumbytes;
    

    topolarity topolarity commented on code on Sep 30, 2026 22:09 UTC
    Code Review
    src/builtins.c:1
    @@ -98,19 +98,25 @@ static int NOINLINE compare_svec(jl_svec_t *a, jl_svec_t *b) JL_NOTSAFEPOINT
         return 1;
     }
     
    +// Bytes of a primitive type that hold value bits, and the mask of the last of them.

    // Bytes of a primitive type that hold value bits
    

    too confusing to document one function by commenting on another IMO

    topolarity topolarity commented on code on Sep 30, 2026 22:11 UTC
    Code Review
    src/ccall.cpp:1
    @@ -2414,8 +2416,9 @@ jl_cgval_t function_sig_t::emit_a_ccall(
         else if (sret) {
             jlretboxed = sretboxed;
             if (!jlretboxed) {
    -            // something alloca'd above is SSA
    -            if (static_rt)
    +            // something alloca'd above is SSA. A primitive is reloaded instead:
    +            // the callee need not define its padding.

    "the callee need not define its padding" will probably be pretty cryptic when re-read outside of the context of this PR

    topolarity topolarity commented on code on Sep 30, 2026 22:12 UTC
    Code Review
    src/cgutils.cpp:1
    @@ -746,6 +746,19 @@ static Type *julia_primitive_storage_type(Type *register_type)
         return IntegerType::get(IT->getContext(), storage_bits);
     }
     
    +// The width `jt` occupies in memory. A primitive type's size is its value bytes
    +// rounded up to a multiple of its alignment, so an odd-bit integer is reached with
    +// one power-of-two access rather than several.
    +// Only the canonical register type widens this way; ABI coercions, which may sit on
    +// a smaller object, keep their own width.

    topolarity topolarity commented on code on Sep 30, 2026 22:14 UTC
    Code Review
    src/datatype.c:1
    @@ -1042,7 +1041,12 @@ JL_DLLEXPORT jl_datatype_t *jl_new_primitivetype(jl_value_t *name, jl_module_t *
         bt->ismutationfree = 1;
         bt->isidentityfree = 1;
         bt->isbitstype = (parameters == jl_emptysvec);
    -    bt->layout = jl_get_layout(nbytes, 0, 0, alignm, unused_bits != 0, 1, 0, unused_bits, NULL, NULL);
    +    // Round the value bytes up to a multiple of the alignment, as C23 lays out
    +    // `_BitInt(N)`: `Core.sizeof` is that allocation size, and the trailing bytes

    out-of-date, IIUC

    topolarity topolarity approved these changes on Sep 30, 2026 22:16 UTC
    Approved Reviewer

    Changes look pretty good to me! Appreciate you keeping up this initiative.

    fingolfin fingolfin commented on code on Oct 01, 2026 06:00 UTC
    Code Review
    base/essentials.jl:1
    @@ -798,7 +798,8 @@ end
         sizeof(obj)
     
     Size, in bytes, of the canonical binary representation of the given `DataType` `T`, if any.
    -Or the size, in bytes, of object `obj` if it is not a `DataType`.
    +Or the size, in bytes, of object `obj` if it is not a `DataType`. The size of a primitive type
    +includes padding past its declared width, which `Core.bitsizeof` gives.

    Applied in afb8473a0f.

    fingolfin fingolfin commented on code on Oct 01, 2026 06:00 UTC
    Code Review
    base/reinterpretarray.jl:1
    @@ -753,7 +753,9 @@ function non_padding_bytes(T::DataType)::Memory{Bool}
     end
     function fill_nonpadding_bytes!(T::DataType, offset::Int, used::Memory{Bool})
         if isprimitivetype(T)
    -        for i in 1:sizeof(T)
    +        # sizeof rounds the value bytes up to a multiple of the alignment, so the
    +        # bytes past them are padding; a byte holding any value bits is not
    +        for i in 1:((Core.bitsizeof(T) + 7) >> 3)

    Switched to cld, and non_padding_bytes now throws an ArgumentError for types with bit padding, as its callers already did (270ea9b89e).

    fingolfin fingolfin commented on code on Oct 01, 2026 06:00 UTC
    Code Review
    doc/src/manual/types.md:1
    @@ -300,14 +300,19 @@ The number of bits gives the declared logical width of the primitive type, and t
     new type a name. A primitive type can optionally be declared to be a subtype of some supertype. If a
     supertype is omitted, then the type defaults to having `Any` as its immediate supertype. The
     declaration of [`Bool`](@ref) above therefore means that a boolean value has a logical width of eight
    -bits, and has [`Integer`](@ref) as its immediate supertype. Primitive types continue to use
    -byte-rounded storage, so `sizeof(T)` rounds their storage size up to a whole number of bytes even
    -when their logical width is not a multiple of 8 bits. Use `Core.bitsizeof(T)` to query the declared
    -logical width.
    +bits, and has [`Integer`](@ref) as its immediate supertype. A primitive type of `N` bits keeps
    +its value in the first `cld(N, 8)` bytes. Its alignment is that of the smallest of `Int8`,
    +`Int16`, `Int32` and `Int64` that holds those bytes, or that of `Int128` if none does, and
    +`sizeof(T)` rounds the value bytes up to a multiple of that alignment; it always equals
    +`Base.elsize(Array{T})`. Up to 64 bits this is the layout the platform's C ABI gives
    +`_BitInt(N)`. Wider types can differ: x86-64 aligns `_BitInt(136)` to 8 bytes with size 24, while
    +`primitive type T 136 end` has alignment 16 and size 32. Bits past the declared width are padding
    +and take no part in comparison or hashing. Use `Core.bitsizeof(T)` to query the declared logical
    +width — for `primitive type T 24 end`, `Core.bitsizeof(T)` is 24 while `sizeof(T)` is 4.

    Good idea, done in ccad9f9b77; kept the 136-bit size example after it.

    fingolfin fingolfin commented on code on Oct 01, 2026 06:00 UTC
    Code Review
    src/APInt.c:1

    Done in bd71cfff66, naming the padding count opaddingbytes as in APInt_sext.

    fingolfin fingolfin commented on code on Oct 01, 2026 06:00 UTC
    Code Review
    src/builtins.c:1
    @@ -98,19 +98,25 @@ static int NOINLINE compare_svec(jl_svec_t *a, jl_svec_t *b) JL_NOTSAFEPOINT
         return 1;
     }
     
    +// Bytes of a primitive type that hold value bits, and the mask of the last of them.

    Done in d6715917a7.

    fingolfin fingolfin commented on code on Oct 01, 2026 06:00 UTC
    Code Review
    src/ccall.cpp:1
    @@ -2414,8 +2416,9 @@ jl_cgval_t function_sig_t::emit_a_ccall(
         else if (sret) {
             jlretboxed = sretboxed;
             if (!jlretboxed) {
    -            // something alloca'd above is SSA
    -            if (static_rt)
    +            // something alloca'd above is SSA. A primitive is reloaded instead:
    +            // the callee need not define its padding.

    Reworded in d6715917a7: the C function leaves the bits above N unspecified, so only the value bits are reloaded.

    fingolfin fingolfin commented on code on Oct 01, 2026 06:00 UTC
    Code Review
    src/cgutils.cpp:1
    @@ -746,6 +746,19 @@ static Type *julia_primitive_storage_type(Type *register_type)
         return IntegerType::get(IT->getContext(), storage_bits);
     }
     
    +// The width `jt` occupies in memory. A primitive type's size is its value bytes
    +// rounded up to a multiple of its alignment, so an odd-bit integer is reached with
    +// one power-of-two access rather than several.
    +// Only the canonical register type widens this way; ABI coercions, which may sit on
    +// a smaller object, keep their own width.

    Removed in d6715917a7.

    fingolfin fingolfin commented on code on Oct 01, 2026 06:00 UTC
    Code Review
    src/datatype.c:1
    @@ -1042,7 +1041,12 @@ JL_DLLEXPORT jl_datatype_t *jl_new_primitivetype(jl_value_t *name, jl_module_t *
         bt->ismutationfree = 1;
         bt->isidentityfree = 1;
         bt->isbitstype = (parameters == jl_emptysvec);
    -    bt->layout = jl_get_layout(nbytes, 0, 0, alignm, unused_bits != 0, 1, 0, unused_bits, NULL, NULL);
    +    // Round the value bytes up to a multiple of the alignment, as C23 lays out
    +    // `_BitInt(N)`: `Core.sizeof` is that allocation size, and the trailing bytes

    Right, fixed in d6715917a7: the _BitInt layout only matches up to 64 bits.

    fingolfin fingolfin commented on code on Oct 01, 2026 06:00 UTC
    Code Review
    src/cgutils.cpp:1
    @@ -742,6 +742,19 @@ static Type *julia_primitive_storage_type(Type *register_type)
         return IntegerType::get(IT->getContext(), storage_bits);
     }
     
    +// The width `jt` occupies in memory. A primitive type's size is its value bytes
    +// rounded up to a multiple of its alignment, so an odd-bit integer is reached with
    +// one power-of-two access rather than several.
    +// Only the canonical register type widens this way; ABI coercions, which may sit on
    +// a smaller object, keep their own width.
    +static Type *julia_memory_access_type(Type *register_type, jl_value_t *jt)
    +{
    +    if (jl_is_primitivetype(jt) && register_type->isIntegerTy() &&
    +            cast<IntegerType>(register_type)->getBitWidth() == jl_datatype_nbits((jl_datatype_t*)jt))
    +        return Type::getIntNTy(register_type->getContext(), 8 * jl_datatype_size(jt));
    +    return zext_struct_type(register_type);

    Agreed, let's do that together with Bool.

    fingolfin fingolfin commented on Oct 01, 2026 22:57 UTC
    Author

    Follow-ups that came up in review are collected in https://github.com/JuliaLang/julia/issues/62441#issuecomment-5942312999

    Reviewers
    Assignees
    fingolfin fingolfin
    Labels
    risk: low confidence: 90.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 66.5% (HIGH) · Higher = larger blast radius
    66.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 90.6% · Lower = rushed or shallow review
    90.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.3% · Safe to merge
    6.3%