#Investigating assembly output on godbolt

1 messages · Page 1 of 1 (latest)

steady whale
#

If I wanted to see, for example, if this cache was stack deferred by the compiler in cases where the items length was less than 8, before the cache is actually used later on

https://github.com/ziglang/zig/blob/36499c251c592d10a8258b1562bee22e5fb7899a/lib/std/sort/block.zig#L116

How would I go about building a test case? I tried to make this example but its a little confusing using godbolt

https://godbolt.org/z/WMxhq5rob

const std = @import("std");

pub fn sorted(items: []u8) []u8 {
    std.sort.block(u8, items, {}, std.sort.asc(u8));
    return items[0..];
}

pub fn main() !void {
    // Get current time in milliseconds as runtime value
    const timestamp = std.time.milliTimestamp();

    // Convert timestamp to 4 bytes
    var items: [4]u8 = undefined;
    items[0] = @truncate(@as(u64, @bitCast(timestamp)) >> 0);
    items[1] = @truncate(@as(u64, @bitCast(timestamp)) >> 8);
    items[2] = @truncate(@as(u64, @bitCast(timestamp)) >> 16);
    items[3] = @truncate(@as(u64, @bitCast(timestamp)) >> 24);

    std.debug.print("Before sorting: ", .{});
    for (items) |item| {
        std.debug.print("{d} ", .{item});
    }
    std.debug.print("\n", .{});

    std.debug.print("After sorting: ", .{});
    for (sorted(items[0..])) |item| {
        std.debug.print("{d} ", .{item});
    }
    std.debug.print("\n", .{});
}
GitHub

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software. - ziglang/zig

uneven iron
steady whale
#

But that’s comptime known?

#

What if it’s a slice and length is runtime known

#

I guess I’m wondering if the compiler reorders that stack reservation until before where it’s used

#

Which is after the two early returns for length < 8

uneven iron
steady whale
#

Curiosity, both with how to reproduce the godbolt properly to know and investigate asm output of zig

#

I realize it’s not perf like heap alloc

uneven iron
steady whale
#

Ah

uneven iron
#

for some reason the call to std.sort.block wasnt inlined so I used @call( .always_inline, ...);

steady whale
#

I also wasn’t sure if I could answer this by looking at some kind of intermediate output from the compiler

uneven iron
# steady whale Ah

In general the stack allocations are done at the beginning of the function
here:

        sub     rsp, 952

looks like its allocating 952 bytes at the beginning of the function

steady whale
#

Amazing, thank you!!

uneven iron
#

but I'm not an expert so take this with a grain of salt

steady whale
#

perfect, I can take it from here then

uneven iron
#

np