Skip to content

preserve SIMD element type information#155005

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
folkertdev:simd-element-type-llvm
Apr 14, 2026
Merged

preserve SIMD element type information#155005
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
folkertdev:simd-element-type-llvm

Conversation

@folkertdev
Copy link
Copy Markdown
Contributor

@folkertdev folkertdev commented Apr 8, 2026

Preserve the SIMD element type and provide it to LLVM for better optimization.

This is relevant for AArch64 types like int16x4x2_t, see also llvm/llvm-project#181514. Such types are defined like so:

#[repr(simd)]
struct int16x4_t([i16; 4]);

#[repr(C)]
struct int16x4x2_t(pub int16x4_t, pub int16x4_t);

Previously this would be translated to the opaque [2 x <8 x i8>], with this PR it is instead [2 x <4 x i16>]. That change is not relevant for the ABI, but using the correct type prevents bitcasts that can (indeed, do) confuse the LLVM pattern matcher.

This change will make it possible to implement the deinterleaving loads on AArch64 in a portable way (without neon-specific intrinsics), which means that e.g. Miri or the cranelift backend can run them without additional support.

discussion at #t-compiler > loss of vector element type information

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 8, 2026
@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the simd-element-type-llvm branch from c1444a0 to 05b0368 Compare April 8, 2026 20:40
@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the simd-element-type-llvm branch 3 times, most recently from c9dd7e6 to 68ed562 Compare April 9, 2026 09:51
@folkertdev folkertdev marked this pull request as ready for review April 9, 2026 15:26
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 9, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 9, 2026

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 9, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 9, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 11 candidates

// CHECK: define [2 x <1 x ptr>] @pair_ptrx1_t([2 x <1 x ptr>] {{.*}} %0)
#[unsafe(no_mangle)] extern "C" fn pair_ptrx1_t(x: Pair<Simd<*const (), 1>>) -> Pair<Simd<*const (), 1>> { x }

// When it fits in a 128-bit register, it's passed directly.
Copy link
Copy Markdown
Contributor Author

@folkertdev folkertdev Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

the types above are actually used by neon intrinsics. Below are a couple that technically work but are unlikely to come up practically.

Then for any type is smaller than 128-bit padding is added which means the type information is lost (but I think that is needed for ABI reasons?). Larger types are passed indirectly, so the type information is not needed there (but we do still technically provide it, maybe it's useful elsewhere).

@folkertdev folkertdev changed the title preseve SIMD element type information preserve SIMD element type information Apr 9, 2026
Comment thread compiler/rustc_codegen_llvm/src/abi.rs Outdated
Comment thread tests/assembly-llvm/aarch64-vld2-s16.rs
@folkertdev folkertdev force-pushed the simd-element-type-llvm branch from 68ed562 to fbab8b2 Compare April 9, 2026 22:58
@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 13, 2026
Primitive::Pointer(_) => cx.type_ptr(),
};

let len = self.size.bytes() / hint_vector_elem.size(cx).bytes();
Copy link
Copy Markdown
Contributor

@nnethercote nnethercote Apr 13, 2026

Choose a reason for hiding this comment

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

View changes since the review

Is it worth asserting that this division is exact?

Comment thread compiler/rustc_abi/src/callconv/reg.rs Outdated
/// by always picking i8) by codegen backends.
///
/// The element kind is used to provide more accurate type information to the backend, which
/// helps with optimization (e.g. because it prevents extra bitcasts that obscure a pattern).
Copy link
Copy Markdown
Contributor

@nnethercote nnethercote Apr 13, 2026

Choose a reason for hiding this comment

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

View changes since the review

This comment is repetitive ("optimization" appears twice) and I found "The element kind" confusing -- I thought it was referring to something new, before I realized it's just referring back to hint_vector_elem.

A suggested alternative:

The hint_vector_elem is strictly for optimization purposes. E.g. it can be used by a codegen backend to prevent extra bitcasts that obscure a pattern. Alternatively, it can be safely ignored by always picking i8.

and provide it to LLVM for better optimization
@folkertdev folkertdev force-pushed the simd-element-type-llvm branch from fbab8b2 to 6f428df Compare April 13, 2026 11:27
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 13, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@folkertdev
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 13, 2026
Copy link
Copy Markdown
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

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

LGTM. I'm no expert on this stuff so I will cc @nikic to give them a chance to look if they want.

View changes since this review

@nnethercote
Copy link
Copy Markdown
Contributor

Eh, good enough. Any genuine problems can be fixed in a follow-up.

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 14, 2026

📌 Commit 6f428df has been approved by nnethercote

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 14, 2026
rust-bors bot pushed a commit that referenced this pull request Apr 14, 2026
Rollup of 3 pull requests

Successful merges:

 - #152530 (Use the term struct-like variant instead of anonymous structs for data of struct-like enum variants)
 - #155005 (preserve SIMD element type information)
 - #155230 (Avoid linting `doc_cfg` as unused in rustc)
@rust-bors rust-bors bot merged commit 803a722 into rust-lang:main Apr 14, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 14, 2026
rust-timer added a commit that referenced this pull request Apr 14, 2026
Rollup merge of #155005 - folkertdev:simd-element-type-llvm, r=nnethercote

preserve SIMD element type information

Preserve the SIMD element type and provide it to LLVM for better optimization.

This is relevant for AArch64 types like `int16x4x2_t`, see also llvm/llvm-project#181514. Such types are defined like so:

```rust
#[repr(simd)]
struct int16x4_t([i16; 4]);

#[repr(C)]
struct int16x4x2_t(pub int16x4_t, pub int16x4_t);
```

Previously this would be translated to the opaque `[2 x <8 x i8>]`, with this PR it is instead `[2 x <4 x i16>]`. That change is not relevant for the ABI, but using the correct type prevents bitcasts that can (indeed, do) confuse the LLVM pattern matcher.

This change will make it possible to implement the deinterleaving loads on AArch64 in a portable way (without neon-specific intrinsics), which means that e.g. Miri or the cranelift backend can run them without additional support.

discussion at [#t-compiler > loss of vector element type information](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/loss.20of.20vector.20element.20type.20information/with/584483611)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants