From 551aa1067ba59631d867e3c31d87bb74ac695f07 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Wed, 11 Dec 2024 16:11:24 +0000 Subject: [PATCH] stdx/meta: introduced MergedTuple and mergeTuple This was required because we found an issue where tuple concatenation was producing an incorrect result (probably a compiler bug). --- stdx/meta.zig | 41 +++++++++++++++++++++++++++++++++++++++++ zml/exe.zig | 7 +++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/stdx/meta.zig b/stdx/meta.zig index 40b33bfb..3e6f8bff 100644 --- a/stdx/meta.zig +++ b/stdx/meta.zig @@ -178,3 +178,44 @@ pub fn Tail(Tuple: type) type { else => @compileError("Tail works on tuple type"), }; } + +fn MergedTuple(comptime T: type, comptime U: type) type { + const t_type_info = @typeInfo(T); + const u_type_info = @typeInfo(U); + if (t_type_info != .Struct or !t_type_info.Struct.is_tuple) @compileError("Expected T to be a tuple"); + if (u_type_info != .Struct or !u_type_info.Struct.is_tuple) @compileError("Expected U to be a tuple"); + + const field_count = t_type_info.Struct.fields.len + u_type_info.Struct.fields.len; + var fields: [field_count]std.builtin.Type.StructField = undefined; + inline for (t_type_info.Struct.fields, 0..) |f, i| { + fields[i] = f; + fields[i].name = std.fmt.comptimePrint("{d}", .{i}); + } + inline for (u_type_info.Struct.fields, 0..) |f, i| { + fields[t_type_info.Struct.fields.len + i] = f; + fields[t_type_info.Struct.fields.len + i].name = std.fmt.comptimePrint("{d}", .{t_type_info.Struct.fields.len + i}); + } + + const V = std.builtin.Type{ + .Struct = .{ + .decls = &.{}, + .fields = &fields, + .is_tuple = true, + .layout = .auto, + }, + }; + + return @Type(V); +} + +fn mergeTuple(t: anytype, u: anytype) MergedTuple(@TypeOf(t), @TypeOf(u)) { + var result: MergedTuple(@TypeOf(t), @TypeOf(u)) = undefined; + inline for (0..t.len) |i| { + result[i] = t[i]; + } + inline for (0..u.len) |i| { + result[t.len + i] = u[i]; + } + + return result; +} diff --git a/zml/exe.zig b/zml/exe.zig index 12a3fecc..117eb704 100644 --- a/zml/exe.zig +++ b/zml/exe.zig @@ -63,7 +63,10 @@ pub fn compileModel( var context = try CompilationContext.init(allocator, name, platform); defer context.deinit(); - return .{ .inner = try context.compileInternal(allocator, func, .{model} ++ args_shapes) }; + // NOTE(Corentin): We use `stdx.meta.mergeTuple` here because it happens that **SOMETIMES**, using + // .{model} ++ args_shapes result in non-null nullable field of args_shapes were null in the resulting tuple. + const merged_args = stdx.meta.mergeTuple(.{model}, args_shapes); + return .{ .inner = try context.compileInternal(allocator, func, merged_args) }; } /// Compiles a function with the given configuration and shapes, for the given platform. @@ -281,7 +284,7 @@ pub fn Exe(ArgsT: type, ReturnT: type) type { pub fn call(self: Self, args: Bufferized(ArgsT)) Bufferized(ReturnT) { const total_ready = fillBuffers(&args, self.inner.input_per_device, self.inner.ready_buffer_count); - std.debug.assert(total_ready == self.inner.input_buffer_count); + stdx.debug.assert(total_ready == self.inner.input_buffer_count, "Exe isn't ready to be called, expected {} buffer inputs got {}", .{ self.inner.input_buffer_count, total_ready }); self.inner._unsafeCall(); var result: Bufferized(ReturnT) = undefined; assignRawBuffers(&result, self.inner.platform, self.inner.output_per_device, self.inner.result_shapes);