#"hello world" program isn't portable, results in illegal instruction

1 messages · Page 1 of 1 (latest)

sick flax
#

The following Zig program builds and runs on my laptop, as expected. (Per the first screenshot.)

const std    = @import("std");

pub fn main() void {
    var x : u32 = 1;
    x += 1;
    std.debug.print("x is: {}\n", .{x});
}

However, when I copy the .exe to another laptop (also running Windows 10 on a x64 CPU) the program fails to run because of an illegal instruction: vpbroadcastb xmm0, edx (see the second screenshot).

Why is printx.exe not portable? How can I build print.exe so that it will work when I transfer it to the other laptop?

For reference, I built the program on a Framework with a Intel 11th Gen i5-1135G7 CPU. The other laptop is a HP Pavillion with a Intel i5-7200U.

Here's my build script.

const std = @import("std");

pub fn build(b : *std.Build) void {
    const target   = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "printx",
        .root_source_file = b.path("main.zig"),
        .target = target,
        .optimize = optimize,
    });

    const install_artifact = b.addInstallArtifact(exe, .{
        .dest_dir = .{ .override = .prefix },
    });

    b.getInstallStep().dependOn(&install_artifact.step);
}
tulip cradle
#

Zig builds with the native target by default. This means it emits instructions available to cpu features for the host's cpu. vpbroadcastb in particular is part of the AVX2 feature (came out around 2014) and isnt on the 7200U? You can start with passing -Dcpu=baseline which should support everything since the pentium4 IIRC

sick flax
tulip cradle
#

Note that it may limit the performance as newer/faster instructions arent used on cpus which may support it. Given your lowest common denominator is a 7200U, maybe you can get away with -Dcpu=x86-64-v2 which is a set of features available on a generation of cpus. See here for more info: https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels

uneven holly
sick flax
tulip cradle
sick flax
#

Also thanks for your talk about Zig at TB. Really enjoyed it.