Skip to content

intrinsic: Add min_align_of#4582

Merged
P-E-P merged 1 commit into
Rust-GCC:masterfrom
nsvke:add-min-align-of
Jun 17, 2026
Merged

intrinsic: Add min_align_of#4582
P-E-P merged 1 commit into
Rust-GCC:masterfrom
nsvke:add-min-align-of

Conversation

@nsvke

@nsvke nsvke commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

This patch implements the 'min_align_of' compiler intrinsic. It resolves the minimum alignment of a given compile-time sized type and returns it as a size_type_node integer constant expression.

gcc/rust/ChangeLog:

* backend/rust-compile-intrinsic.cc (generic_intrinsics): Add min_align_of to map.
* backend/rust-intrinsic-handlers.cc (min_align_of_handler): New function.
* backend/rust-intrinsic-handlers.h (min_align_of_handler): New declaration.

gcc/testsuite/ChangeLog:

* rust/compile/min_align_of.rs: New test.

@CohenArthur CohenArthur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, @philberty if you have the time can you take a look? Note that we have https://forge.sourceware.org/gcc/gcc-TEST/pulls/161 going on at the same time so you might need to be careful with merge conflicts

Comment on lines +25 to +26
// { dg-final { scan-tree-dump {D\.[0-9]+ = 2;} "gimple" } }
// { dg-final { scan-tree-dump {D\.[0-9]+ = 4;} "gimple" } }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you can do here instead is make it an execution test, add the two alignments and then make the main function return the substraction of the sum of the alignments and the expected sum:

fn main() -> i32 {
    let align_u16 = get_u16_align();
    let align_u32 = get_u32_align();

    align_u16 + align_u32 - 6
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the good tip!

@nsvke nsvke force-pushed the add-min-align-of branch 2 times, most recently from 330263a to 8c2b270 Compare June 8, 2026 16:12
min_align_of_handler (Context *ctx, TyTy::FnType *fntype)
{
// min_align_of has _zero_ parameters its parameter is the generic one
rust_assert (fntype->get_params ().size () == 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably have a step earlier that throws an error as well as a test to highlight that behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the existing pattern from size_of. I'm not yet familiar with where intrinsic signatures are verified during type checking, but I will investigate it.

Comment thread gcc/testsuite/rust/execute/min_align_of.rs Outdated
Comment thread gcc/testsuite/rust/execute/min_align_of.rs Outdated
@CohenArthur CohenArthur added the alloc Issue related to the compilation of the `alloc` crate label Jun 11, 2026
This patch implements the 'min_align_of' compiler intrinsic. It
resolves the minimum alignment of a given compile-time sized type
and returns it as a size_type_node integer constant expression.

gcc/rust/ChangeLog:

	* backend/rust-compile-intrinsic.cc (generic_intrinsics): Add
	min_align_of to map.
	* backend/rust-intrinsic-handlers.cc (min_align_of_handler):
	New function.
	* backend/rust-intrinsic-handlers.h (min_align_of_handler):
	New declaration.

gcc/testsuite/ChangeLog:

	* rust/execute/min_align_of.rs: New test.

Signed-off-by: Enes Cevik <enes@nsvke.com>
@nsvke nsvke force-pushed the add-min-align-of branch from 8c2b270 to 88c62f1 Compare June 16, 2026 08:35

@P-E-P P-E-P left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@P-E-P P-E-P added this pull request to the merge queue Jun 17, 2026
Merged via the queue into Rust-GCC:master with commit 6dae3d0 Jun 17, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

alloc Issue related to the compilation of the `alloc` crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants