Merge remote-tracking branch 'remote/main' into upgrade-llvm-zig

This commit is contained in:
Luke Boswell 2024-07-31 14:01:50 +10:00
commit 96db1bdce2
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
337 changed files with 16767 additions and 9022 deletions

View file

@ -0,0 +1,101 @@
const std = @import("std");
const sort = @import("sort.zig");
extern fn malloc(size: usize) callconv(.C) ?*anyopaque;
extern fn free(c_ptr: *anyopaque) callconv(.C) void;
fn cMain() callconv(.C) i32 {
fuzz_main() catch unreachable;
return 0;
}
comptime {
@export(cMain, .{ .name = "main", .linkage = .Strong });
}
const DEBUG = false;
var allocator: std.mem.Allocator = undefined;
pub fn fuzz_main() !void {
// Setup an allocator that will detect leaks/use-after-free/etc
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
// this will check for leaks and crash the program if it finds any
defer std.debug.assert(gpa.deinit() == .ok);
allocator = gpa.allocator();
// Read the data from stdin
const stdin = std.io.getStdIn();
const data = try stdin.readToEndAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(data);
const len = data.len / @sizeOf(i64);
const arr_ptr: [*]i64 = @alignCast(@ptrCast(data.ptr));
if (DEBUG) {
std.debug.print("Input: [{d}]{d}\n", .{ len, arr_ptr[0..len] });
}
var test_count: i64 = 0;
sort.fluxsort(@ptrCast(arr_ptr), len, &test_i64_compare_refcounted, @ptrCast(&test_count), true, &test_inc_n_data, @sizeOf(i64), @alignOf(i64), &test_i64_copy);
const sorted = std.sort.isSorted(i64, arr_ptr[0..len], {}, std.sort.asc(i64));
if (DEBUG) {
std.debug.print("Output: [{d}]{d}\nSorted: {}\nFinal RC: {}\n", .{ len, arr_ptr[0..len], sorted, test_count });
}
std.debug.assert(sorted);
std.debug.assert(test_count == 0);
}
const Opaque = ?[*]u8;
fn test_i64_compare_refcounted(count_ptr: Opaque, a_ptr: Opaque, b_ptr: Opaque) callconv(.C) u8 {
const a = @as(*i64, @alignCast(@ptrCast(a_ptr))).*;
const b = @as(*i64, @alignCast(@ptrCast(b_ptr))).*;
const gt = @as(u8, @intFromBool(a > b));
const lt = @as(u8, @intFromBool(a < b));
std.debug.assert(@as(*isize, @ptrCast(@alignCast(count_ptr))).* > 0);
@as(*isize, @ptrCast(@alignCast(count_ptr))).* -= 1;
// Eq = 0
// GT = 1
// LT = 2
return lt + lt + gt;
}
fn test_i64_copy(dst_ptr: Opaque, src_ptr: Opaque) callconv(.C) void {
@as(*i64, @alignCast(@ptrCast(dst_ptr))).* = @as(*i64, @alignCast(@ptrCast(src_ptr))).*;
}
fn test_inc_n_data(count_ptr: Opaque, n: usize) callconv(.C) void {
@as(*isize, @ptrCast(@alignCast(count_ptr))).* += @intCast(n);
}
comptime {
@export(testing_roc_alloc, .{ .name = "roc_alloc", .linkage = .Strong });
@export(testing_roc_dealloc, .{ .name = "roc_dealloc", .linkage = .Strong });
@export(testing_roc_panic, .{ .name = "roc_panic", .linkage = .Strong });
}
fn testing_roc_alloc(size: usize, _: u32) callconv(.C) ?*anyopaque {
// We store an extra usize which is the size of the full allocation.
const full_size = size + @sizeOf(usize);
var raw_ptr = (allocator.alloc(u8, full_size) catch unreachable).ptr;
@as([*]usize, @alignCast(@ptrCast(raw_ptr)))[0] = full_size;
raw_ptr += @sizeOf(usize);
return @as(?*anyopaque, @ptrCast(raw_ptr));
}
fn testing_roc_dealloc(c_ptr: *anyopaque, _: u32) callconv(.C) void {
const raw_ptr = @as([*]u8, @ptrCast(c_ptr)) - @sizeOf(usize);
const full_size = @as([*]usize, @alignCast(@ptrCast(raw_ptr)))[0];
const slice = raw_ptr[0..full_size];
allocator.free(slice);
}
fn testing_roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
_ = c_ptr;
_ = tag_id;
@panic("Roc panicked");
}

View file

@ -1,6 +1,7 @@
const std = @import("std");
const utils = @import("utils.zig");
const str = @import("str.zig");
const sort = @import("sort.zig");
const UpdateMode = utils.UpdateMode;
const mem = std.mem;
const math = std.math;
@ -690,60 +691,10 @@ pub fn listDropAt(
}
}
fn partition(
source_ptr: [*]u8,
transform: Opaque,
wrapper: CompareFn,
element_width: usize,
low: isize,
high: isize,
copy: CopyFn,
) isize {
const pivot = source_ptr + (@as(usize, @intCast(high)) * element_width);
var i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far
var j = low;
while (j <= high - 1) : (j += 1) {
const current_elem = source_ptr + (@as(usize, @intCast(j)) * element_width);
const ordering = wrapper(transform, current_elem, pivot);
const order = @as(utils.Ordering, @enumFromInt(ordering));
switch (order) {
utils.Ordering.LT => {
// the current element is smaller than the pivot; swap it
i += 1;
swapElements(source_ptr, element_width, @as(usize, @intCast(i)), @as(usize, @intCast(j)), copy);
},
utils.Ordering.EQ, utils.Ordering.GT => {},
}
}
swapElements(source_ptr, element_width, @as(usize, @intCast(i + 1)), @as(usize, @intCast(high)), copy);
return (i + 1);
}
fn quicksort(
source_ptr: [*]u8,
transform: Opaque,
wrapper: CompareFn,
element_width: usize,
low: isize,
high: isize,
copy: CopyFn,
) void {
if (low < high) {
// partition index
const pi = partition(source_ptr, transform, wrapper, element_width, low, high, copy);
_ = quicksort(source_ptr, transform, wrapper, element_width, low, pi - 1, copy); // before pi
_ = quicksort(source_ptr, transform, wrapper, element_width, pi + 1, high, copy); // after pi
}
}
pub fn listSortWith(
input: RocList,
caller: CompareFn,
data: Opaque,
cmp: CompareFn,
cmp_data: Opaque,
inc_n_data: IncN,
data_is_owned: bool,
alignment: u32,
@ -753,16 +704,13 @@ pub fn listSortWith(
dec: Dec,
copy: CopyFn,
) callconv(.C) RocList {
if (input.len() < 2) {
return input;
}
var list = input.makeUnique(alignment, element_width, elements_refcounted, inc, dec);
if (data_is_owned) {
inc_n_data(data, list.len());
}
if (list.bytes) |source_ptr| {
const low = 0;
const high: isize = @as(isize, @intCast(list.len())) - 1;
quicksort(source_ptr, data, caller, element_width, low, high, copy);
sort.fluxsort(source_ptr, list.len(), cmp, cmp_data, data_is_owned, inc_n_data, element_width, alignment, copy);
}
return list;

View file

@ -394,11 +394,13 @@ fn exportUtilsFn(comptime func: anytype, comptime func_name: []const u8) void {
// Custom panic function, as builtin Zig version errors during LLVM verification
pub fn panic(message: []const u8, stacktrace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
if (builtin.is_test) {
std.debug.print("{s}: {?}", .{ message, stacktrace });
if (builtin.target.cpu.arch != .wasm32) {
std.debug.print("\nSomehow in unreachable zig panic!\nThis is a roc standard libarry bug\n{s}: {?}", .{ message, stacktrace });
std.process.abort();
} else {
// Can't call abort or print from wasm. Just leave it as unreachable.
unreachable;
}
unreachable;
}
// Run all tests in imported modules

File diff suppressed because it is too large Load diff