const std = @import("std"); const gpu = @import("mlir/dialects/gpu"); const mlir = @import("mlir"); const operation_names: []const []const u8 = &.{ "gpu.barrier", "gpu.block_dim", "gpu.block_id", "gpu.func", "gpu.grid_dim", "gpu.module", "gpu.return", "gpu.shuffle", "gpu.thread_id", }; fn expectPrints(expected: []const u8, value: anytype) !void { var buf: [128]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); try w.print("{f}", .{value}); try std.testing.expectEqualStrings(expected, w.buffered()); } test { @setEvalBranchQuota(100_000); std.testing.refAllDecls(gpu); inline for (comptime std.meta.declarations(gpu)) |decl| { const T = @field(gpu, decl); if (@TypeOf(T) == type and @typeInfo(T) == .@"opaque") std.testing.refAllDecls(T); } } test "gpu enum attributes are built through the C API" { const ctx = try mlir.Context.init(.{}); defer ctx.deinit(); inline for (@typeInfo(gpu.Dim).@"enum".field_names) |name| { const value: gpu.Dim = @field(gpu.Dim, name); const attr = try gpu.DimensionAttr.get(ctx, .{ .value = value }); try std.testing.expectEqual(value, attr.value()); try std.testing.expect(attr.attribute().isA(gpu.DimensionAttr) != null); try expectPrints("#gpu.dim<" ++ name ++ ">", attr); } inline for (@typeInfo(gpu.ShuffleMode).@"enum".field_names) |name| { const value: gpu.ShuffleMode = @field(gpu.ShuffleMode, name); const attr = try gpu.ShuffleModeAttr.get(ctx, .{ .value = value }); try std.testing.expectEqual(value, attr.value()); try expectPrints("#gpu.shuffle_mode<" ++ name ++ ">", attr); } } test "container_module and kernel are unit attributes of the gpu dialect" { const ctx = try mlir.Context.init(.{}); defer ctx.deinit(); var buf: [128]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); try w.print("{f}", .{mlir.Attribute.dict(ctx, &.{ gpu.containerModule(ctx), gpu.kernel(ctx) })}); try std.testing.expectEqualStrings("{gpu.container_module, gpu.kernel}", w.buffered()); } test "every bound operation is registered" { const registry = try mlir.DialectRegistry.init(); defer registry.deinit(); gpu.registerDialect(registry); const ctx = try mlir.Context.init(.{ .registry = registry, .threading = false }); defer ctx.deinit(); ctx.loadAllAvailableDialects(); for (operation_names) |name| try std.testing.expect(ctx.isRegisteredOperation(name)); try std.testing.expect(!ctx.allowUnregisteredDialects()); }