Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move feature descriptions out of std.Target.Cpu.Feature #20977

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

squeek502
Copy link
Collaborator

@squeek502 squeek502 commented Aug 7, 2024

I happened to notice that there were a bunch of these description strings in the data section of compiled resinator binaries. It turns out they were coming from Aro because Aro uses std.Target. Aro does not actually use the description strings, though, so in both Aro and resinator, those strings are wasted space in the binary.

So, this PR allows users of std.Target to not include all the description strings in their program if they don't actually use them.

Counting all the bytes of the description strings after they were separated out using this code:

comptime {
    @setEvalBranchQuota(10000000);
    var total_byte_count: usize = 0;
    for (std.enums.values(Arch)) |arch| {
        const descs = allFeaturesDescList(arch);
        for (descs) |desc| {
            total_byte_count  = desc.len;
        }
    }
    @compileLog(total_byte_count);
}

gives a total size of 146898 bytes or ~143 KiB. However, I have not seen quite that much saved in compiled binaries in practice. A test program like this:

const std = @import("std");

pub fn main() !void {
    const target = try std.zig.system.resolveTargetQuery(.{});
    std.debug.print("{any}\n", .{target});
}

compiled in ReleaseSmall with -fstrip on Windows gives these results:

  • Before: 321024 bytes (313.5 KiB)
  • After: 219648 bytes (214.5 KiB)
  • Delta: 101376 bytes ( 99.0 KiB)

Side note: the use of std.Target still increases the size of the binary a lot; for comparison, a simple hello world compiled in ReleaseSmall is 5 KiB

Note: The spirv features were not intended to be changed, but I'm not sure what exact versions of the Headers/Registry were used to generate the current Target/spirv.zig. I used commits around the time of the last Target/spirv.zig update in May 2021 (98dc8eb); in particular, I used commits 60af2c93c46294a2bc9758889a90d935b6f9325f for SPIRV-Registry and ba29b3f59633836c6bb160b951007c8fc3842dee for SPIRV-Headers. Newer Registry/Headers versions were not used because (1) there are a lot more features, more than the current Feature.Set.needed_bit_count and (2) the current update_spirv_features.zig cannot handle some of the new files, e.g. https://raw.githubusercontent.com/KhronosGroup/SPIRV-Registry/main/extensions/EXT/SPV_EXT_relaxed_printf_string_address_space.asciidoc

cc @Snektron

@squeek502 squeek502 force-pushed the cpufeature-separate-desc branch 2 times, most recently from 892b13f to 67b1a21 Compare August 8, 2024 20:24
@Snektron
Copy link
Collaborator

Snektron commented Aug 8, 2024

Note: The spirv features were not intended to be changed, but I'm not sure what exact versions of the Headers/Registry were used to generate the current Target/spirv.zig

Oof, that is a good question. Currently its not terribly important, this part needs a little bit more thinking anyway. I'm fine with the current changes.

@andrewrk
Copy link
Member

andrewrk commented Aug 9, 2024

Sorry but I want to instead solve this with #21010 which is a low-priority enhancement.

@squeek502
Copy link
Collaborator Author

Would it suffice to add in some // TODO: https://github.com/ziglang/zig/issues/21010 comments to get this merged?

This allows users of std.Target to not include all the description strings in their program if they don't actually use them.

Counting all the bytes of the description strings after they were separated out using this code:

    comptime {
        @setEvalBranchQuota(10000000);
        var total_byte_count: usize = 0;
        for (std.enums.values(Arch)) |arch| {
            const descs = allFeaturesDescList(arch);
            for (descs) |desc| {
                total_byte_count  = desc.len;
            }
        }
        @compilelog(total_byte_count);
    }

gives a total size of 146898 bytes or ~143 KiB. However, I have not seen quite that much saved in compiled binaries in practice. A test program like this:

    const std = @import("std");

    pub fn main() !void {
        const target = try std.zig.system.resolveTargetQuery(.{});
        std.debug.print("{any}\n", .{target});
    }

compiled in ReleaseSmall on Windows gives these results:

- Before: 321024 bytes (313.5 KiB)
- After:  219648 bytes (214.5 KiB)
- Delta:  101376 bytes ( 99.0 KiB)

Note: The spirv features were not intended to be changed, but I'm not sure what exact versions of the Headers/Registry were used to generate the current `Target/spirv.zig`. I used commits around the time of the last `Target/spirv.zig` update in May 2021 (98dc8eb); in particular, I used commits 60af2c93c46294a2bc9758889a90d935b6f9325f for SPIRV-Registry and ba29b3f59633836c6bb160b951007c8fc3842dee for SPIRV-Headers. Newer Registry/Headers versions were not used because (1) there are a *lot* more features, more than the current Feature.Set.needed_bit_count and (2) the current update_spirv_features.zig cannot handle some of the new files, e.g. https://raw.githubusercontent.com/KhronosGroup/SPIRV-Registry/main/extensions/EXT/SPV_EXT_relaxed_printf_string_address_space.asciidoc
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.

3 participants