ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.62k stars 2.46k forks source link

std.time.milliTimestamp doesn't work with x86 backend #19807

Open rupakhetibinit opened 4 months ago

rupakhetibinit commented 4 months ago

Zig Version

0.13.0-dev.46+3648d7df1

Steps to Reproduce and Observed Behavior

Given a simple program where you try to print the current milliseconds unix timestamp.

//src/timestamp.zig
const std = @import("std");
pub fn main() !void {
    const time = std.time.milliTimestamp();
    std.debug.print("{}", .{time});
}

When running with x86 backend zig run src/timestamp.zig -fno-llvm -fno-lld gives error

❯ zig run .\src\timestamp.zig -fno-llvm -fno-lld
error(x86_64_encoder): no encoding found for: none or xmm0 rax none none
user_path\zig\0.13.0-dev.46+3648d7df1\files\lib\std\time.zig:74:5: error: CodeGen failed to find a viable instruction. This is a bug in the Zig compiler.
pub fn milliTimestamp() i64 {
~~~~^~~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

Expected to print timestamp, similar to the llvm backend.

❯ zig run .\src\timestamp.zig
1714405874063
rupakhetibinit commented 4 months ago

So I did some more digging. And I found that std.time.timestamp() , std.time.microTimestamp() and std.time.milliTimestamp() directly or indirectly call std.time.nanoTimestamp() when calculating timestamp and when trying to directly call std.time.nanoTimestamp() it silently fails without an error message. Reproduction steps below

//src/timestamp.zig
const std = @import("std");
pub fn main() !void {
    const time = std.time.nanoTimestamp();
    std.debug.print("{}", .{time});
}

when ran with zig run src/timestamp.zig -fno-llvm -fno-lld silently fails

❯ zig run .\src\timestamp.zig -fno-llvm -fno-lld

which gives no output, without any error. Hope this helps to narrow down the problem.

10aded commented 1 month ago

As of 0.14.0-dev.564+75cf7fca9 the compiler now describes an encoding error when the .nanoTimestamp() version above is called with just -fno-llvm:

> zig run .\timestamp.zig -fno-llvm
error(x86_64_encoder): no encoding found for: none cmp m64 xmm0 none none
C:\Users\10aded\Zig-Curr-Dev\lib\std\fmt.zig:1168:5: error: CodeGen failed to find a viable instruction. This is a bug in the Zig compiler.
pub fn formatInt(
~~~~^~
PS C:\Users\10aded\Documents\Temp\Zig-x86-Timestamp-Bug>

The .nanoTimestamp(); version now no longer silently fails either when complied with just -fno-lld:

> zig run .\timestamp.zig -fno-lld
error: the following command failed with 'Unexpected':
C:\Users\10aded\AppData\Local\zig\o\7663f6d27a22940b13ecad349050f1a7\bug.exe --seed=0xa5466038