fix zig hosts ensure they export main

This commit is contained in:
Luke Boswell 2024-08-04 21:12:09 +10:00
parent af7015f125
commit b4ec32126f
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
10 changed files with 36 additions and 21 deletions

View file

@ -111,12 +111,15 @@ comptime {
const Unit = extern struct {}; const Unit = extern struct {};
pub fn main() !u8 { pub export fn main() u8 {
const stderr = std.io.getStdErr().writer(); const stderr = std.io.getStdErr().writer();
// The size might be zero; if so, make it at least 8 so that we don't have a nullptr // The size might be zero; if so, make it at least 8 so that we don't have a nullptr
const size = @max(@as(usize, @intCast(roc__mainForHost_1_exposed_size())), 8); const size = @max(@as(usize, @intCast(roc__mainForHost_1_exposed_size())), 8);
const raw_output = roc_alloc(@as(usize, @intCast(size)), @alignOf(u64)).?; const raw_output = roc_alloc(@as(usize, @intCast(size)), @alignOf(u64)) orelse {
std.log.err("Memory allocation failed", .{});
return 1;
};
const output = @as([*]u8, @ptrCast(raw_output)); const output = @as([*]u8, @ptrCast(raw_output));
defer { defer {

View file

@ -105,7 +105,7 @@ extern fn roc__mainForHost_1_exposed_generic(*RocStr) void;
const Unit = extern struct {}; const Unit = extern struct {};
pub fn main() u8 { pub export fn main() u8 {
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer(); const stderr = std.io.getStdErr().writer();

View file

@ -2,8 +2,8 @@ const std = @import("std");
const time = std.time; const time = std.time;
const Timer = time.Timer; const Timer = time.Timer;
const RocStr = @import("../src/str.zig").RocStr; const RocStr = @import("./bitcode/src/str.zig").RocStr;
const RocDec = @import("../src/dec.zig").RocDec; const RocDec = @import("./bitcode/src/dec.zig").RocDec;
fn roc_alloc(_: usize, _: u32) callconv(.C) ?*anyopaque { fn roc_alloc(_: usize, _: u32) callconv(.C) ?*anyopaque {
@panic("Not needed for dec benchmark"); @panic("Not needed for dec benchmark");
@ -23,7 +23,15 @@ comptime {
var timer: Timer = undefined; var timer: Timer = undefined;
pub fn main() !void { pub export fn main() u8 {
run_tests() catch |err| {
std.debug.print("Error: {}\n", .{err});
return 1;
};
return 0;
}
fn run_tests() !void {
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
try stdout.print("Warning: Timer seems to step in units of 41ns\n\n", .{}); try stdout.print("Warning: Timer seems to step in units of 41ns\n\n", .{});
timer = try Timer.start(); timer = try Timer.start();
@ -35,11 +43,15 @@ pub fn main() !void {
// This number are very close to 1 to avoid over and underflow. // This number are very close to 1 to avoid over and underflow.
const f1 = 1.00123; const f1 = 1.00123;
const dec1 = RocDec.fromF64(f1).?; const dec1 = RocDec.fromF64(f1) orelse {
@panic("Failed to create RocDec from f64");
};
// `asin` and `acos` have a limited range, so they will use this value. // `asin` and `acos` have a limited range, so they will use this value.
const f2 = 0.00130000847; const f2 = 0.00130000847;
const dec2 = RocDec.fromF64(f2).?; const dec2 = RocDec.fromF64(f2) orelse {
@panic("Failed to create RocDec from f64");
};
try stdout.print("Dec:\n", .{}); try stdout.print("Dec:\n", .{});
try stdout.print("{} additions took ", .{add_sub_n}); try stdout.print("{} additions took ", .{add_sub_n});
@ -129,8 +141,8 @@ fn avg_runs(comptime T: type, comptime n: usize, comptime op: fn (T, T) T, v: T)
runs[i] = callWrapper(u64, .never_inline, run, .{ T, n, op, v }); runs[i] = callWrapper(u64, .never_inline, run, .{ T, n, op, v });
} }
var real_runs = runs[warmups..runs.len]; const real_runs = runs[warmups..runs.len];
std.sort.sort(u64, real_runs, {}, comptime std.sort.asc(u64)); std.sort.insertion(u64, real_runs, {}, comptime std.sort.asc(u64));
const median = real_runs[real_runs.len / 2]; const median = real_runs[real_runs.len / 2];
const highest = real_runs[real_runs.len - 1]; const highest = real_runs[real_runs.len - 1];
@ -146,8 +158,8 @@ fn run(comptime T: type, comptime n: usize, comptime op: fn (T, T) T, v: T) u64
// Split into outer and inner loop to avoid breaking comptime. // Split into outer and inner loop to avoid breaking comptime.
const max_inline = 100; const max_inline = 100;
comptime var outer = n / max_inline; const outer = n / max_inline;
comptime var inner = std.math.min(n, max_inline); const inner = @min(n, max_inline);
var i: usize = 0; var i: usize = 0;
while (i < outer) : (i += 1) { while (i < outer) : (i += 1) {
comptime var j = 0; comptime var j = 0;
@ -156,7 +168,7 @@ fn run(comptime T: type, comptime n: usize, comptime op: fn (T, T) T, v: T) u64
} }
} }
const rem = n % max_inline; const rem = n % max_inline;
comptime var j = 0; const j = 0;
inline while (j < rem) : (j += 1) { inline while (j < rem) : (j += 1) {
a = callWrapper(T, .always_inline, op, .{ a, v }); a = callWrapper(T, .always_inline, op, .{ a, v });
} }
@ -172,7 +184,7 @@ fn run(comptime T: type, comptime n: usize, comptime op: fn (T, T) T, v: T) u64
// This is needed to work around a bug with using `@call` in loops. // This is needed to work around a bug with using `@call` in loops.
inline fn callWrapper(comptime T: type, call_modifier: anytype, comptime func: anytype, params: anytype) T { inline fn callWrapper(comptime T: type, call_modifier: anytype, comptime func: anytype, params: anytype) T {
return @call(.{ .modifier = call_modifier }, func, params); return @call(call_modifier, func, params);
} }
fn addF64(x: f64, y: f64) f64 { fn addF64(x: f64, y: f64) f64 {

View file

@ -3,5 +3,5 @@
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/ # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -euxo pipefail set -euxo pipefail
zig build-exe benchmark/dec.zig -O ReleaseFast --main-pkg-path . zig build-exe benchmark-dec.zig -O ReleaseFast
./dec ./dec

View file

@ -33,7 +33,7 @@ export fn read_host_result() i32 {
return host_result; return host_result;
} }
pub fn main() !void { pub export fn main() u8 {
const host = host_called_directly_from_main(); const host = host_called_directly_from_main();
const js = js_called_directly_from_main(); const js = js_called_directly_from_main();
const app = roc__app_proc_1_exposed(); const app = roc__app_proc_1_exposed();

View file

@ -106,7 +106,7 @@ extern fn roc__mainForHost_1_exposed_generic(*RocStr) void;
const Unit = extern struct {}; const Unit = extern struct {};
pub fn main() u8 { pub export fn main() u8 {
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer(); const stderr = std.io.getStdErr().writer();

View file

@ -39,7 +39,7 @@ extern fn roc__mainForHost_1_exposed(*RocStr) void;
extern fn js_display_roc_string(str_bytes: ?[*]u8, str_len: usize) void; extern fn js_display_roc_string(str_bytes: ?[*]u8, str_len: usize) void;
pub fn main() u8 { pub export fn main() u8 {
// actually call roc to populate the callresult // actually call roc to populate the callresult
var callresult = RocStr.empty(); var callresult = RocStr.empty();
roc__mainForHost_1_exposed(&callresult); roc__mainForHost_1_exposed(&callresult);

View file

@ -39,7 +39,7 @@ extern fn roc__mainForHost_1_exposed(*RocStr) void;
extern fn js_display_roc_string(str_bytes: ?[*]u8, str_len: usize) void; extern fn js_display_roc_string(str_bytes: ?[*]u8, str_len: usize) void;
pub fn main() u8 { pub export fn main() u8 {
// actually call roc to populate the callresult // actually call roc to populate the callresult
var callresult = RocStr.empty(); var callresult = RocStr.empty();
roc__mainForHost_1_exposed(&callresult); roc__mainForHost_1_exposed(&callresult);

View file

@ -106,7 +106,7 @@ extern fn roc__mainForHost_1_exposed_generic(*RocStr) void;
const Unit = extern struct {}; const Unit = extern struct {};
pub fn main() u8 { pub export fn main() u8 {
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer(); const stderr = std.io.getStdErr().writer();

View file

@ -56,7 +56,7 @@ extern fn roc__main_1_exposed(RocStr, RocStr) callconv(.C) ResultStrStr;
const hostJavaScriptBytes = @embedFile("../client-side/host.js"); const hostJavaScriptBytes = @embedFile("../client-side/host.js");
pub fn main() u8 { pub export fn main() u8 {
const json = RocStr.fromSlice("42"); const json = RocStr.fromSlice("42");
defer json.decref(); defer json.decref();