Skip to content

all() causing LLVM bloat #491

Closed
Closed

Description

@bolshoytoster

Currently, bitflags::all() outputs 6 + 20n lines of LLVM IR in debug builds, and 8 + 14n on release builds, where n is the number of flags. I first noticed this with wgpu's FeaturesWGPU, which has 58 flags (1166 lines debug, 820 release).

The implementation is here:

bitflags/src/public.rs

Lines 150 to 168 in 9253889

fn all() {
let mut truncated = <$T as $crate::Bits>::EMPTY;
let mut i = 0;
$(
$crate::__bitflags_expr_safe_attrs!(
$(#[$inner $($args)*])*
{{
let flag = <$PublicBitFlags as $crate::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}}
);
)*
let _ = i;
Self(truncated)
}

If I wrap the function in const {} then the function emits only 1 line of IR in debug, and seems to be fully optimized away in release.

This bloat slows down builds (sllightly) and probably makes it less likely for some optimizations to occur (don't quote me on that, I'm not a compiler developer).

There are probably some other generated functions that suffer from this. I can investigate and open a PR if that's wanted.

Activity

  1. bolshoytoster commented on Jun 30, 2026

    @bolshoytoster
    ContributorAuthor

    from_name is also fairly big 13 + 7n debug, 12 + 14n release (not a typo, it's way bigger in release). I can get it down to 13 + 6n and 13 + 12n release by changing $PublicBitFlags::$Flag.bits() to $value (only affected debug), and changing the list of if statements to a match (only affected release).

  2. KodrAus commented on Jun 30, 2026

    @KodrAus
    Member

    Thanks for investigating this @bolshoytoster! Would you be happy to open a PR with your improvements and we can take a look? We should be aiming for as small a footprint as possible.

  3. bolshoytoster commented on Jul 1, 2026

    @bolshoytoster
    ContributorAuthor

    I've opened a PR (#492)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions