Skip to content

Add mask casting using From - #440

Merged
Shnatsel merged 2 commits into
linebender:mainfrom
RunDevelopment:mask-cast-from
Nov 12, 2026
Merged

Shnatsel merged 2 commits into
linebender:mainfrom
RunDevelopment:mask-cast-from

Conversation

@RunDevelopment

Copy link
Copy Markdown
Contributor

Related to #439.

I implemented casting between masks with an equal number of lanes. This makes compatible mask conversions very easy for users. Example:

let m32: mask32x16<_> = a.simd_gt(b);
let m8 = mask8x16::from(m32);

Under the hood, this is implemented using the existing Simd::{combine,narrow,split,widen}_maskNxM on all platforms except AVX512 where same-lane-count mask conversion is trivial.

The code in fearless_simd_gen is a bit out there, because I couldn't easily hook into existing systems .

@Shnatsel Shnatsel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The API seems reasonable. Things like enabling code generic over element type can be layered on top of this later.

The tests and the generated code are reasonable.

I didn't have the time to review the generator code in detail. Cleanup recommendations from an LLM are included below for your reference, feel free to disregard if you think they're not helpful.

LLM recommendations

  • generic_impl_mask_casts(is_trait_definition: bool, …) — the bool is opaque. Rename it (emit_docs) and/or have the helper emit #[inline(always)] itself, so mk_simd_trait.rs's awkward #( #[inline(always)] #mask_casts )* repetition goes away and both call sites look the same.
  • generic_mask_cast (body) vs generic_impl_mask_casts (list) are easy to confuse; mask_cast_body / mask_cast_methods would read better.
  • generic_mask_cast synthesizes intermediate VecTypes (scalar_bits / 2, * 2). That's valid today because the mask type ladder is gapless, but a debug_assert!(SIMD_TYPES.contains(&new_from_ty)) or a comment would document the assumption and fail loudly if the ladder ever changes.
  • Optionally, the Level hook could be modeled as fn mask_cast_is_noop(&self) -> bool (default false) so the trait generator owns body generation and mk_x86.rs doesn't have to emit whole method bodies. Not required.

Comment thread fearless_simd_gen/src/level.rs Resolved
@RunDevelopment

Copy link
Copy Markdown
Contributor Author

I implemented LLM suggestions 1 and 2, because they were quite good, but the rest didn't make much sense. I also changed one abstraction a little, so the code should be a bit cleaner now.

@Shnatsel
Shnatsel enabled auto-merge November 12, 2026 13:15
@Shnatsel
Shnatsel added this pull request to the merge queue Nov 12, 2026
Merged via the queue into linebender:main with commit 79d7d01 Nov 12, 2026
23 checks passed
@RunDevelopment
RunDevelopment deleted the mask-cast-from branch November 12, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants