From 6036d23ebfea8cc904a00b7b4919df0e30f61dab Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Mon, 25 Aug 2025 15:54:43 +0200 Subject: [PATCH 01/37] WIP --- BUILD.bazel | 4 + mlir/BUILD.bazel | 28 +- mlir/context.zig | 0 mlir/main.zig | 73 +++ mlir/registry.zig | 1115 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1219 insertions(+), 1 deletion(-) create mode 100644 mlir/context.zig create mode 100644 mlir/main.zig create mode 100644 mlir/registry.zig diff --git a/BUILD.bazel b/BUILD.bazel index aa1656d4..4c17c21a 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -14,5 +14,9 @@ zls_completion( "//examples/llama", "//stdx", "//zml", + "//mlir:main", + # "//async", + # "//stdx", + # "//zml", ], ) diff --git a/mlir/BUILD.bazel b/mlir/BUILD.bazel index 87982a7e..0dccf25d 100644 --- a/mlir/BUILD.bazel +++ b/mlir/BUILD.bazel @@ -1,5 +1,5 @@ load("@rules_cc//cc:defs.bzl", "cc_library") -load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test") +load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_library", "zig_test") cc_library( name = "c", @@ -28,3 +28,29 @@ zig_test( name = "test", deps = [":mlir"], ) + +zig_library( + name = "mlir2", + main = "registry.zig", + deps = [":c"], +) + +zig_binary( + name = "main", + main = "main.zig", + visibility = ["//visibility:public"], + deps = [ + ":mlir2", + # ":c", + # "//stdx", + ], +) + +zig_test( + name = "test2", + deps = [ + ":mlir2", + # ":c", + # "//stdx", + ], +) diff --git a/mlir/context.zig b/mlir/context.zig new file mode 100644 index 00000000..e69de29b diff --git a/mlir/main.zig b/mlir/main.zig new file mode 100644 index 00000000..8095de85 --- /dev/null +++ b/mlir/main.zig @@ -0,0 +1,73 @@ +const std = @import("std"); + +const mlir = @import("mlir2"); + +pub fn main() !void { + const registry = try mlir.DialectRegistry.init(); + defer registry.deinit(); + + // const handle = mlir.DialectHandle.fromString("func"); + + inline for (.{ "arith", "func" }) |d| { + registry.registerDialect(d); + } + + const ctx = try mlir.Context.init(.{ + .registry = registry, + .threading = true, + }); + defer ctx.deinit(); + + std.debug.print(">>> {f}\n", .{ctx}); + ctx.loadAllAvailableDialects(); + std.debug.print(">>> {f}\n", .{ctx}); + + const unk = mlir.Location.unknown(ctx); + _ = unk; // autofix + + const attr = mlir.integerAttribute(ctx, @as(i32, 5)); + _ = attr; // autofix + + const one: i32 = 1; + const two: i32 = 2; + + const dict = mlir.DictionaryAttribute.init(ctx, &.{ + .named(ctx, "pouet", mlir.integerAttribute(ctx, one)), + .named(ctx, "pouet2", mlir.integerAttribute(ctx, two)), + }); + _ = dict; // autofix + + const op = try mlir.Operation.parse(ctx, "func.func private @pouet()", "pouet"); + + const mod = mlir.Module.init(mlir.Location.unknown(ctx)); + + const body = mod.body(); + _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); + _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); + _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); + + body.appendOwnedOperation(op); + + // op.deinit(); + // body.detach(); + // body.deinit(); + + // std.debug.print(">>>>>> {any}\n", .{op.block().?.parentOperation()}); + + // std.debug.print(">>>>>> {any}\n", .{op.block().?.parentOperation()}); + + std.debug.print(">>> {f}\n", .{body}); + // const attr = mlir.integerAttribute(ctx, @as(i32, 5)); + // const attr2: *const mlir.Attribute = @ptrCast(attr); + + // std.debug.print("coucou 2 {any} {f}\n", .{ + // ctx.isRegisteredOperation("func.func"), + // attr2, + // }); + + // var buffer: [256]u8 = undefined; + // const vv = "coucou\n"; + // var writer = std.fs.File.stdout().writer(&buffer); + // try writer.interface.writeAll(vv); + // try writer.interface.flush(); +} diff --git a/mlir/registry.zig b/mlir/registry.zig new file mode 100644 index 00000000..69e44975 --- /dev/null +++ b/mlir/registry.zig @@ -0,0 +1,1115 @@ +const std = @import("std"); + +const c = @import("c"); + +const log = std.log.scoped(.mlir); + +test { + std.testing.refAllDeclsRecursive(@This()); +} + +const Error = error{ + /// Invalid Mlir was created. + InvalidMlir, + /// Another Mlir error. Check the log for more context. + MlirUnexpected, + /// A resource/executor was not found. + NotFound, +} || std.mem.Allocator.Error; + +pub inline fn stringRef(str: []const u8) c.MlirStringRef { + return .{ .data = str.ptr, .length = str.len }; +} + +pub inline fn string(str: c.MlirStringRef) []const u8 { + // Note: mlir.StringRef need not to be null terminated. + return str.data[0..str.length]; +} + +pub const StringCallbackCtx = struct { + writer: *std.Io.Writer, + err: ?std.Io.Writer.Error = null, +}; + +pub fn stringCallback(str: c.MlirStringRef, ctx_: ?*anyopaque) callconv(.c) void { + const ctx: *StringCallbackCtx = @ptrCast(@alignCast(ctx_)); + ctx.writer.writeAll(string(str)) catch |err| { + ctx.err = err; + }; +} + +pub fn registerPasses(comptime passes: []const u8) void { + @field(c, "mlirRegister" ++ passes ++ "Passes")(); +} + +fn isPtrConst(comptime T: type) bool { + return @typeInfo(T).pointer.is_const; +} + +fn CastedPtr(comptime T1: type, comptime T2: type) type { + return if (@typeInfo(T1).pointer.is_const) *const T2 else *T2; +} + +pub fn StdMethods(comptime T: type, comptime NativeT: type) type { + return struct { + pub const IsPtrConst = @typeInfo(@typeInfo(@FieldType(NativeT, "ptr")).optional.child).pointer.is_const; + pub const Ptr = if (IsPtrConst) *const T else *T; + + pub fn ptr(self: anytype) NativeT { + return .{ .ptr = @ptrCast(@constCast(self)) }; + } + + fn format(f: anytype) fn (self: *const T, writer: *std.Io.Writer) std.Io.Writer.Error!void { + return struct { + fn format(self: *const T, writer: *std.Io.Writer) std.Io.Writer.Error!void { + var ctx: StringCallbackCtx = .{ .writer = writer }; + f(self.ptr(), stringCallback, &ctx); + if (ctx.err) |err| { + return err; + } + } + }.format; + } + + pub fn eql(f: anytype) fn (self: *const T, other: *const T) bool { + return struct { + fn eql(self: *const T, other: *const T) bool { + return f(self.ptr(), other.ptr()); + } + }.eql; + } + + pub fn isA(self: *const T, OtherType: type) ?CastedPtr(@TypeOf(self), OtherType) { + return if (OtherType.isAFn(self.ptr())) @ptrCast(self) else null; + } + }; +} + +pub const DialectRegistry = opaque { + const M = StdMethods(DialectRegistry, c.MlirDialectRegistry); + + pub const ptr = M.ptr; + + pub fn init() std.mem.Allocator.Error!*DialectRegistry { + return @ptrCast(c.mlirDialectRegistryCreate().ptr orelse return std.mem.Allocator.Error.OutOfMemory); + } + + pub fn deinit(self: *DialectRegistry) void { + c.mlirDialectRegistryDestroy(self.ptr()); + } + + pub fn registerDialect(self: *DialectRegistry, comptime name: []const u8) void { + DialectHandle.fromString(name).insertDialect(self); + } +}; + +pub const Context = opaque { + const M = StdMethods(Context, c.MlirContext); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirContextEqual); + + pub const InitOptions = struct { + registry: ?*const DialectRegistry = null, + threading: bool = false, + }; + + pub fn init(opts: InitOptions) std.mem.Allocator.Error!*Context { + const ctx = if (opts.registry) |r| c.mlirContextCreateWithRegistry( + r.ptr(), + opts.threading, + ) else c.mlirContextCreateWithThreading(opts.threading); + return @ptrCast(ctx.ptr orelse return std.mem.Allocator.Error.OutOfMemory); + } + + pub fn deinit(self: *Context) void { + c.mlirContextDestroy(self.ptr()); + } + + pub fn setMultiThreading(self: *Context, enabled: bool) void { + c.mlirContextEnableMultithreading(self.ptr(), enabled); + } + + pub fn appendDialectRegistry(self: *Context, registry: *const DialectRegistry) void { + c.mlirContextAppendDialectRegistry(self.ptr(), registry.ptr()); + } + + pub fn loadAllAvailableDialects(self: *Context) void { + c.mlirContextLoadAllAvailableDialects(self.ptr()); + } + + pub fn numRegisteredDialects(self: *const Context) usize { + return @intCast(c.mlirContextGetNumRegisteredDialects(self.ptr())); + } + + pub fn numLoadedDialects(self: *const Context) usize { + return @intCast(c.mlirContextGetNumLoadedDialects(self.ptr())); + } + + pub fn numThreads(self: *const Context) usize { + return @intCast(c.mlirContextGetNumThreads(self.ptr())); + } + + pub fn isRegisteredOperation(self: *const Context, op: []const u8) bool { + return c.mlirContextIsRegisteredOperation(self.ptr(), stringRef(op)); + } + + pub fn format(self: *const Context, writer: *std.Io.Writer) !void { + try writer.print("{s}{{ .numThreads = {d}, .numRegisteredDialects = {d}, .numLoadedDialects = {d} }}", .{ + @typeName(Context), + self.numThreads(), + self.numRegisteredDialects(), + self.numLoadedDialects(), + }); + } +}; + +pub const Location = opaque { + const M = StdMethods(Location, c.MlirLocation); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirLocationEqual); + pub const format = M.format(c.mlirLocationPrint); + + pub fn fromSrc(ctx: *Context, src: std.builtin.SourceLocation) *const Location { + return fileLineCol( + ctx, + src.file, + src.line, + src.column, + ); + } + + pub fn fileLineCol(ctx: *Context, file: []const u8, line: usize, column: usize) *const Location { + return @ptrCast(c.mlirLocationFileLineColGet( + ctx.ptr(), + stringRef(file), + @intCast(line), + @intCast(column), + ).ptr); + } + + pub fn callSite(callee: *const Location, caller: *const Location) *const Location { + return @ptrCast(c.mlirLocationCallSiteGet(callee.ptr(), caller.ptr()).ptr); + } + + pub fn fused(ctx: *Context, locs: []const *const Location, metadata: *const Attribute) *const Location { + return @ptrCast(c.mlirLocationFusedGet( + ctx.ptr(), + @intCast(locs.len), + @ptrCast(locs), + metadata.ptr(), + ).ptr); + } + + pub fn named(self: *const Location, ctx: *Context, loc_name: []const u8) *const Location { + return @ptrCast(c.mlirLocationNameGet(ctx.ptr(), stringRef(loc_name), self.ptr()).ptr); + } + + pub fn namedFmt(loc: *const Location, ctx: *Context, comptime fmt: []const u8, args: anytype) *const Location { + var buf: [256]u8 = undefined; + return loc.named(loc, ctx, std.fmt.bufPrint(&buf, fmt, args) catch blk: { + buf[buf.len - 3 ..].* = "...".*; + break :blk &buf; + }); + } + + pub fn fromAttribute(attribute: *const Attribute) *const Location { + return @ptrCast(c.mlirLocationFromAttribute(attribute.ptr()).ptr); + } + + pub fn unknown(ctx: *Context) *const Location { + return @ptrCast(c.mlirLocationUnknownGet(ctx.ptr()).ptr); + } +}; + +pub const Type = opaque { + const M = StdMethods(Type, c.MlirType); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + pub const isA = M.isA; + + pub fn parse(ctx: *Context, str: []const u8) Error!*const Type { + return @ptrCast(c.mlirTypeParseGet(ctx.ptr(), stringRef(str)).ptr orelse return Error.InvalidMlir); + } +}; + +pub const IndexType = opaque { + const M = StdMethods(IndexType, c.MlirType); + + pub const isAFn = c.mlirTypeIsAIndex; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + pub fn get(ctx: *Context) *const IndexType { + return @ptrCast(c.mlirIndexTypeGet(ctx.ptr()).ptr); + } +}; + +pub const Signedness = enum { + signless, + signed, + unsigned, +}; + +pub const IntegerType = opaque { + const M = StdMethods(IntegerType, c.MlirType); + + pub const isAFn = c.mlirTypeIsAInteger; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + fn get(ctx: *Context, bitwidth: usize, sign: Signedness) *const IntegerType { + return @ptrCast(switch (sign) { + .signless => c.mlirIntegerTypeGet(ctx.ptr(), @intCast(bitwidth)).ptr, + .signed => c.mlirIntegerTypeSignedGet(ctx.ptr(), @intCast(bitwidth)).ptr, + .unsigned => c.mlirIntegerTypeUnsignedGet(ctx.ptr(), @intCast(bitwidth)).ptr, + }); + } +}; + +pub fn integerType(ctx: *Context, bitwidth: usize, sign: Signedness) *const Type { + return @ptrCast(IntegerType.get(ctx, bitwidth, sign)); +} + +pub const FloatTypes = enum { + f4e2m1fn, + f6e2m3fn, + f6e3m2fn, + f8e3m4, + f8e4m3, + f8e4m3b11fnuz, + f8e4m3fn, + f8e4m3fnuz, + f8e5m2, + f8e5m2fnuz, + f8e8m0fnu, + bf16, + f16, + f32, + f64, + // f80, + // f128, + tf32, +}; + +pub fn FloatType(comptime ft: FloatTypes) type { + const Config = switch (ft) { + .f4e2m1fn => .{ c.mlirTypeIsAFloat4E2M1FN, c.mlirFloat4E2M1FNTypeGet }, + .f6e2m3fn => .{ c.mlirTypeIsAFloat6E2M3FN, c.mlirFloat6E2M3FNTypeGet }, + .f6e3m2fn => .{ c.mlirTypeIsAFloat6E3M2FN, c.mlirFloat6E3M2FNTypeGet }, + .f8e3m4 => .{ c.mlirTypeIsAFloat8E3M4, c.mlirFloat8E3M4TypeGet }, + .f8e4m3 => .{ c.mlirTypeIsAFloat8E4M3, c.mlirFloat8E4M3TypeGet }, + .f8e4m3b11fnuz => .{ c.mlirTypeIsAFloat8E4M3B11FNUZ, c.mlirFloat8E4M3B11FNUZTypeGet }, + .f8e4m3fn => .{ c.mlirTypeIsAFloat8E4M3FN, c.mlirFloat8E4M3FNTypeGet }, + .f8e4m3fnuz => .{ c.mlirTypeIsAFloat8E4M3FNUZ, c.mlirFloat8E4M3FNUZTypeGet }, + .f8e5m2 => .{ c.mlirTypeIsAFloat8E5M2, c.mlirFloat8E5M2TypeGet }, + .f8e5m2fnuz => .{ c.mlirTypeIsAFloat8E5M2FNUZ, c.mlirFloat8E5M2FNUZTypeGet }, + .f8e8m0fnu => .{ c.mlirTypeIsAFloat8E8M0FNU, c.mlirFloat8E8M0FNUTypeGet }, + .bf16 => .{ c.mlirTypeIsABF16, c.mlirBF16TypeGet }, + .f16 => .{ c.mlirTypeIsAF16, c.mlirF16TypeGet }, + .f32 => .{ c.mlirTypeIsAF32, c.mlirF32TypeGet }, + .f64 => .{ c.mlirTypeIsAF64, c.mlirF64TypeGet }, + .tf32 => .{ c.mlirTypeIsATF32, c.mlirTF32TypeGet }, + }; + + return opaque { + const Self = @This(); + const M = StdMethods(Self, c.MlirType); + + pub const isAFn = Config[0]; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + pub fn get(ctx: *Context) *const Self { + const getter = Config[1]; + return @ptrCast(getter(ctx.ptr()).ptr); + } + }; +} + +pub fn floatType(comptime ft: FloatTypes, ctx: *Context) *const Type { + return @ptrCast(FloatType(ft).get(ctx)); +} + +pub const Attribute = opaque { + const M = StdMethods(Attribute, c.MlirAttribute); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + pub const isA = M.isA; + + pub fn parse(ctx: *Context, attr: []const u8) Error!*const Attribute { + return @ptrCast(c.mlirAttributeParseGet(ctx.ptr(), stringRef(attr)).ptr orelse return Error.InvalidMlir); + } +}; + +pub const StringAttribute = opaque { + const M = StdMethods(StringAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAString; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, str: []const u8) *const StringAttribute { + return @ptrCast(c.mlirStringAttrGet(ctx.ptr(), stringRef(str)).ptr); + } + + pub fn value(self: *const StringAttribute) []const u8 { + return string(c.mlirStringAttrGetValue(self.ptr())); + } +}; + +pub fn stringAttribute(ctx: *Context, str: []const u8) *const Attribute { + return @ptrCast(StringAttribute.init(ctx, str)); +} + +pub const IntegerAttribute = opaque { + const M = StdMethods(IntegerAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAInteger; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, value_: anytype) *const IntegerAttribute { + const ti = @typeInfo(@TypeOf(value_)).int; + const sign: Signedness = switch (ti.signedness) { + .signed => .signless, + .unsigned => .unsigned, + }; + return @ptrCast(c.mlirIntegerAttrGet( + IntegerType.get(ctx, @intCast(ti.bits), sign).ptr(), + @intCast(value_), + ).ptr); + } + + pub fn value(self: *const IntegerAttribute, comptime T: type) T { + const getValue = switch (@typeInfo(T).int.signedness) { + .signed => c.mlirIntegerAttrGetValueInt, + .unsigned => c.mlirIntegerAttrGetValueUInt, + }; + return @intCast(getValue(self.ptr())); + } +}; + +pub fn integerAttribute(ctx: *Context, value_: anytype) *const Attribute { + return @ptrCast(IntegerAttribute.init(ctx, value_)); +} + +pub const BoolAttribute = opaque { + const M = StdMethods(BoolAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsABool; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, value_: bool) *const BoolAttribute { + return @ptrCast(c.mlirBoolAttrGet(ctx.ptr(), @intFromBool(value_)).ptr); + } + + pub fn value(self: *const BoolAttribute) bool { + return c.mlirBoolAttrGetValue(self.ptr()); + } +}; + +pub fn boolAttribute(ctx: *Context, value: bool) *const Attribute { + return @ptrCast(BoolAttribute.init(ctx, value)); +} + +pub const TypeAttribute = opaque { + const M = StdMethods(TypeAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAType; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(type_: *const Type) *const TypeAttribute { + return @ptrCast(c.mlirTypeAttrGet(type_.ptr()).ptr); + } + + pub fn value(self: *const TypeAttribute) *const Type { + return @ptrCast(c.mlirTypeAttrGetValue(self.ptr()).ptr); + } +}; + +pub fn typeAttribute(type_: *const Type) *const TypeAttribute { + return @ptrCast(TypeAttribute.init(type_)); +} + +pub const Dialect = struct { + const M = StdMethods(Dialect, c.MlirDialect); + + pub const ptr = M.ptr; + + pub fn context(self: *const Dialect) *Context { + return @ptrCast(c.mlirDialectGetContext(self.ptr()).ptr); + } + + pub fn namespace(self: *const Dialect) []const u8 { + return string(c.mlirDialectGetNamespace(self.ptr())); + } + + pub fn format(self: *const Dialect, writer: *std.Io.Writer) !void { + try writer.print("{s}{{ .namespace = \"{s}\" }}", .{ @typeName(Dialect), self.namespace() }); + } +}; + +pub const DialectHandle = opaque { + const M = StdMethods(DialectHandle, c.MlirDialectHandle); + + pub const ptr = M.ptr; + + pub fn format(self: *const DialectHandle, writer: *std.Io.Writer) !void { + try writer.print("{s}{{ .namespace = \"{s}\" }}", .{ @typeName(DialectHandle), self.namespace() }); + } + + pub fn namespace(self: *const DialectHandle) []const u8 { + return string(c.mlirDialectHandleGetNamespace(self.ptr())); + } + + pub fn insertDialect(self: *const DialectHandle, registry: *DialectRegistry) void { + c.mlirDialectHandleInsertDialect(self.ptr(), registry.ptr()); + } + + pub fn registerDialect(self: *const DialectHandle, ctx: *Context) void { + c.mlirDialectHandleRegisterDialect(self.ptr(), ctx.ptr()); + } + + pub fn loadDialect(self: *const DialectHandle, ctx: *Context) *Dialect { + return @ptrCast(c.mlirDialectHandleLoadDialect(self.ptr(), ctx.ptr()).ptr); + } + + pub fn fromString(comptime namespace_: []const u8) *const DialectHandle { + return @ptrCast(@field(c, "mlirGetDialectHandle__" ++ namespace_ ++ "__")().ptr); + } +}; + +pub const Identifier = opaque { + const M = StdMethods(Identifier, c.MlirIdentifier); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirIdentifierEqual); + + pub fn context(self: *const Identifier) *Context { + return @ptrCast(c.mlirIdentifierGetContext(self.ptr()).ptr); + } + + pub fn get(ctx: *Context, str_: []const u8) *const Identifier { + return @ptrCast(c.mlirIdentifierGet(ctx.ptr(), stringRef(str_)).ptr); + } + + pub fn str(self: *const Identifier) []const u8 { + return string(c.mlirIdentifierStr(self.ptr())); + } + + pub fn format(self: *const Identifier, writer: *std.Io.Writer) !void { + try writer.print("{s}{{ .str = \"{s}\" }}", .{ @typeName(Identifier), self.str() }); + } +}; + +pub const Module = opaque { + const M = StdMethods(Module, c.MlirModule); + + const ptr = M.ptr; + + pub fn init(loc: *const Location) *const Module { + return @ptrCast(c.mlirModuleCreateEmpty(loc.ptr()).ptr); + } + + pub fn deinit(self: *Module) void { + c.mlirModuleDestroy(self.ptr()); + } + + pub fn fromOperation(op: *Operation) *const Module { + return @ptrCast(c.mlirModuleFromOperation(op.ptr()).ptr); + } + + pub fn operation(self: *const Module) *Operation { + return @ptrCast(c.mlirModuleGetOperation(self.ptr()).ptr); + } + + pub fn parse(ctx: *Context, source: []const u8) Error!*const Module { + return @ptrCast(c.mlirModuleCreateParse(ctx.ptr(), stringRef(source)).ptr orelse return Error.InvalidMlir); + } + + pub fn context(self: *const Module) *Context { + return @ptrCast(c.mlirModuleGetContext(self.ptr()).ptr); + } + + pub fn body(self: *const Module) *Block { + return @ptrCast(c.mlirModuleGetBody(self.ptr()).ptr); + } +}; + +pub const Block = opaque { + const M = StdMethods(Block, c.MlirBlock); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirBlockEqual); + pub const format = M.format(c.mlirBlockPrint); + + pub fn init(args: []const *const Type, locs: []const *const Location) *Block { + return @ptrCast(c.mlirBlockCreate(@intCast(args.len), @ptrCast(args), @ptrCast(locs)).ptr); + } + + pub fn deinit(self: *Block) void { + std.debug.assert(self.parentOperation() == null); + c.mlirBlockDestroy(self.ptr()); + } + + pub fn argument(self: *const Block, index: usize) *const Value { + return @ptrCast(c.mlirBlockGetArgument(self.ptr(), @intCast(index)).ptr); + } + + pub fn numArguments(self: *const Block) usize { + return @intCast(c.mlirBlockGetNumArguments(self.ptr())); + } + + pub fn addArgument(self: *Block, type_: *const Type, loc: *const Location) *const Value { + return @ptrCast(c.mlirBlockAddArgument(self.ptr(), type_.ptr(), loc.ptr()).ptr); + } + + pub fn insertArgument(self: *Block, index: usize, type_: *const Type, loc: *const Location) *const Value { + return @ptrCast(c.mlirBlockInsertArgument(self.ptr(), @intCast(index), type_.ptr(), loc.ptr()).ptr); + } + + pub fn appendOwnedOperation(self: *Block, op: *Operation) void { + c.mlirBlockAppendOwnedOperation(self.ptr(), op.ptr()); + } + + pub fn appendOwnedOperations(self: *Block, ops: []const *Operation) void { + for (ops) |op| { + self.appendOwnedOperation(op); + } + } + + pub fn parentOperation(self: *const Block) ?*Operation { + return @ptrCast(c.mlirBlockGetParentOperation(self.ptr()).ptr); + } + + pub fn detach(self: *Block) void { + c.mlirBlockDetach(self.ptr()); + } +}; + +pub const NamedAttribute = struct { + inner: c.MlirNamedAttribute, + + pub fn ptr(self: NamedAttribute) c.MlirNamedAttribute { + return self.inner; + } + + pub fn name(self: NamedAttribute) *const Identifier { + return @ptrCast(self.inner.name.ptr); + } + + pub fn attribute(self: NamedAttribute) *const Attribute { + return @ptrCast(self.inner.attribute.ptr); + } + + pub fn named(ctx: *Context, name_: []const u8, attr: *const Attribute) NamedAttribute { + return .{ + .inner = .{ + .name = Identifier.get(ctx, name_).ptr(), + .attribute = attr.ptr(), + }, + }; + } + + pub fn init(name_: *const Identifier, attr: *const Attribute) NamedAttribute { + return .{ + .inner = .{ + .name = name_.ptr(), + .attribute = attr.ptr(), + }, + }; + } + + pub fn format(self: NamedAttribute, writer: *std.Io.Writer) std.Io.Writer.Error!void { + try writer.print("{s}{{ .{s} = \"{f}\" }}", .{ + @typeName(NamedAttribute), + self.name().str(), + self.attribute(), + }); + } +}; + +pub const DictionaryAttribute = opaque { + const M = StdMethods(DictionaryAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsADictionary; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, attributes: []const NamedAttribute) *const DictionaryAttribute { + return @ptrCast(c.mlirDictionaryAttrGet( + ctx.ptr(), + @intCast(attributes.len), + @ptrCast(attributes.ptr), + ).ptr); + } + + pub fn size(self: *const DictionaryAttribute) usize { + return @intCast(c.mlirDictionaryAttrGetNumElements(self.ptr())); + } + + pub fn get(self: *const DictionaryAttribute, pos: usize) NamedAttribute { + return .{ .inner = c.mlirDictionaryAttrGetElement(self.ptr(), @bitCast(pos)) }; + } + + pub fn getByName(self: *const DictionaryAttribute, name: []const u8) ?*const Attribute { + return @ptrCast(c.mlirDictionaryAttrGetElementByName(self.ptr(), stringRef(name)).ptr); + } +}; + +pub const Value = opaque { + const M = StdMethods(Value, c.MlirValue); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirValueEqual); + pub const format = M.format(c.mlirValuePrint); + pub const isA = M.isA; + + pub fn type_(val: *const Value) *const Type { + return @ptrCast(c.mlirValueGetType(val.ptr()).ptr); + } + + pub fn setType(self: *Value, type__: *const Type) void { + c.mlirValueSetType(self.ptr(), type__.ptr()); + } + + pub fn firstUse(self: *const Value) *const OpOperand { + return @ptrCast(c.mlirValueGetFirstUse(self.ptr()).ptr); + } + + pub fn replaceAllUsesWith(self: *const Value, with: *const Value) void { + c.mlirValueReplaceAllUsesOfWith(self.ptr(), with.ptr()); + } +}; + +pub const OpOperand = opaque { + const M = StdMethods(OpOperand, c.MlirOpOperand); + + pub const ptr = M.ptr; + + pub fn owner(self: *const OpOperand) *Operation { + return @ptrCast(c.mlirOpOperandGetOwner(self.ptr()).ptr); + } + + pub fn number(self: *const OpOperand) usize { + return @intCast(c.mlirOpOperandGetOperandNumber(self.ptr())); + } + + pub fn nextUse(self: *const OpOperand) ?*const OpOperand { + return @ptrCast(c.mlirOpOperandGetNextUse(self.ptr()).ptr); + } +}; + +pub const OpResult = opaque { + const M = StdMethods(OpResult, c.MlirValue); + + pub const isAFn = c.mlirValueIsAOpResult; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirValueEqual); + pub const format = M.format(c.mlirValuePrint); + + pub fn owner(self: *const OpResult) *Operation { + return @ptrCast(c.mlirOpResultGetOwner(self.ptr()).ptr); + } + + pub fn resultNumber(self: *const OpResult) usize { + return @intCast(c.mlirOpResultGetResultNumber(self.ptr())); + } +}; + +pub const BlockArgument = opaque { + const M = StdMethods(BlockArgument, c.MlirValue); + + pub const isAFn = c.mlirValueIsABlockArgument; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirValueEqual); + pub const format = M.format(c.mlirValuePrint); + + pub fn block(arg: *const BlockArgument) *Block { + return @ptrCast(c.mlirBlockArgumentGetOwner(arg.ptr()).ptr); + } + + pub fn number(self: *const BlockArgument) usize { + return @intCast(c.mlirBlockArgumentGetArgNumber(self.ptr())); + } + + pub fn setType(self: *BlockArgument, type_: *const Type) void { + c.mlirBlockArgumentSetType(self.ptr(), type_.ptr()); + } +}; + +pub const OperationState = struct { + const M = StdMethods(OperationState, c.MlirOperationState); + + name: []const u8, + location: *const Location, + results: []const *const Type = &.{}, + operands: []const *const Value = &.{}, + regions: []*const Region = &.{}, + successors: []*const Block = &.{}, + attributes: []NamedAttribute = &.{}, + result_type_inference: bool = false, + + pub const ptr = M.ptr; + + pub fn inner(self: OperationState) c.MlirOperationState { + return .{ + .name = stringRef(self.name), + .nResults = @intCast(self.results.len), + .results = @constCast(@ptrCast(self.results)), + .nOperands = @intCast(self.operands.len), + .operands = @constCast(@ptrCast(self.operands)), + .nRegions = @intCast(self.regions.len), + .regions = @constCast(@ptrCast(self.regions)), + .nSuccessors = @intCast(self.successors.len), + .successors = @constCast(@ptrCast(self.successors)), + .nAttributes = @intCast(self.attributes.len), + .attributes = @constCast(@ptrCast(self.attributes)), + .enableResultTypeInference = self.result_type_inference, + }; + } +}; + +pub const Operation = opaque { + const M = StdMethods(Operation, c.MlirOperation); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirOperationEqual); + pub const format = M.format(c.mlirOperationPrint); + + pub fn init(state: *OperationState) !*Operation { + var os = state.inner(); + return @ptrCast(c.mlirOperationCreate(&os).ptr orelse return Error.InvalidMlir); + } + + pub fn deinit(self: *Operation) void { + std.debug.assert(self.block() == null); + c.mlirOperationDestroy(self.ptr()); + } + + pub fn parse(ctx: *Context, source: []const u8, name: []const u8) Error!*Operation { + return @ptrCast(c.mlirOperationCreateParse(ctx.ptr(), stringRef(source), stringRef(name)).ptr orelse return Error.InvalidMlir); + } + + pub fn clone(self: *const Operation) *Operation { + return @ptrCast(c.mlirOperationClone(self.ptr()).ptr); + } + + pub fn block(self: *const Operation) ?*Block { + return @ptrCast(c.mlirOperationGetBlock(self.ptr()).ptr); + } + + pub fn parent(self: *const Operation) ?*Operation { + return @ptrCast(c.mlirOperationGetParentOperation(self.ptr()).ptr); + } + + pub const PrintFlags = struct { + elide_large_elements_attrs: ?usize = null, + debug_info: bool = false, + debug_info_pretty_form: bool = true, + print_generic_op_form: bool = false, + use_local_scope: bool = false, + assume_verified: bool = false, + + const FmtCtx = struct { + self: *const Operation, + flags: PrintFlags, + }; + }; + + fn formatWithFlags(fctx: PrintFlags.FmtCtx, writer: *std.io.Writer) std.Io.Writer.Error!void { + const flags = c.mlirOpPrintingFlagsCreate(); + defer c.mlirOpPrintingFlagsDestroy(flags); + + if (fctx.flags.elide_large_elements_attrs) |v| { + c.mlirOpPrintingFlagsElideLargeElementsAttrs(flags, @intCast(v)); + } + c.mlirOpPrintingFlagsEnableDebugInfo( + flags, + fctx.flags.debug_info, + fctx.flags.debug_info_pretty_form, + ); + if (fctx.flags.print_generic_op_form) { + c.mlirOpPrintingFlagsPrintGenericOpForm(flags); + } + if (fctx.flags.use_local_scope) { + c.mlirOpPrintingFlagsUseLocalScope(flags); + } + if (fctx.flags.assume_verified) { + c.mlirOpPrintingFlagsAssumeVerified(flags); + } + var sctx: StringCallbackCtx = .{ .writer = writer }; + c.mlirOperationPrintWithFlags(fctx.self.ptr(), flags, stringCallback, &sctx); + if (sctx.err) |err| { + return err; + } + } + + pub fn fmt(self: *const Operation, flags: PrintFlags) std.fmt.Alt(PrintFlags.FmtCtx, formatWithFlags) { + return .{ .data = .{ + .self = self, + .flags = flags, + } }; + } + + pub const MakeArgs = struct { + pub const AttrTuple = struct { []const u8, *const Attribute }; + + operands: []const *const Value, + operands_variadic: ?[]const usize = null, + results: []const *const Type, + results_variadic: ?[]const usize = null, + result_type_inference: bool = false, + n_regions: usize = 0, + attributes: ?[]const AttrTuple = null, + blocks: ?[]const *const Block = null, + verify: bool = true, + location: *const Location, + }; + + pub fn try_make(ctx: *Context, op_name: []const u8, args: MakeArgs) !*Operation { + _ = ctx; // autofix + var state: OperationState = .{ + .name = op_name, + .location = args.location, + .result_type_inference = args.result_type_inference, + }; + // std.debug.assert(!(args.operands != null and args.variadic_operands != null)); + state.addOperands(operands); + + // if (args.operands) |operands| { + // state.addOperands(operands); + // // } else if (args.variadic_operands) |operands_segments| { + // // const MAX_SEGMENTS = 32; + // // var segments: std.BoundedArray(i32, MAX_SEGMENTS) = .{}; + + // // for (operands_segments) |operands| { + // // state.addOperands(operands); + // // segments.appendAssumeCapacity(@intCast(operands.len)); + // // } + // // state.addAttribute(ctx, "operandSegmentSizes", .denseElements(ctx, &.{@intCast(segments.len)}, .i32, segments.constSlice())); + // // } else if (args.tt_variadic_operands) |operands_segments| { + // // // stablehlo and triton seems to disagree on the expected type of operandSegmentSizes, let's fix that. + // // const MAX_SEGMENTS = 32; + // // var segments: std.BoundedArray(i32, MAX_SEGMENTS) = .{}; + + // // for (operands_segments) |operands| { + // // state.addOperands(operands); + // // segments.appendAssumeCapacity(@intCast(operands.len)); + // // } + // // state.addAttribute(ctx, "operandSegmentSizes", .dense(ctx, .i32, segments.constSlice())); + // } + // std.debug.assert(!(args.results != null and args.variadic_results != null)); + // if (args.results) |results| { + // state.addResults(results); + // // } else if (args.variadic_results) |result_segments| { + // // for (result_segments) |results| { + // // state.addResults(results); + // // } + // } + // for (0..args.n_regions) |_| { + // var region_ = Region.init() catch { + // @panic("Failed to create MLIR region"); + // }; + // state.addRegion(®ion_); + // } + // if (args.attributes) |attrs| { + // for (attrs) |attr| { + // state.addAttributeRaw( + // Identifier.get(ctx, attr[0]), + // attr[1], + // ); + // } + // } + // if (args.blocks) |blocks_| { + // for (blocks_) |block_| { + // var region_ = Region.init() catch { + // @panic("Failed to create MLIR region"); + // }; + // region_.appendBlock(block_); + // state.addRegion(®ion_); + // } + // } + + const new_op = try Operation.init(&state); + if (args.verify and new_op.verify() == false) { + log.err("Failed to verify MLIR operation:\n{f}", .{ + new_op.fmt(.{ + .print_generic_op_form = true, + .debug_info = true, + }), + }); + return Error.InvalidMlir; + } + + return new_op; + } + + pub fn make(ctx: *Context, op_name: []const u8, args: MakeArgs) *Operation { + return try_make(ctx, op_name, args) catch { + @panic("Failed to create MLIR operation"); + }; + } + + pub fn verify(self: *const Operation) bool { + return c.mlirOperationVerify(self.ptr()); + } + + pub fn location(self: *const Operation) *const Location { + return @ptrCast(c.mlirOperationGetLocation(self.ptr()).ptr); + } + + pub const WalkOrder = enum(c.MlirWalkOrder) { + pre_order = c.MlirWalkPreOrder, + post_order = c.MlirWalkPostOrder, + }; + + pub const WalkResult = enum(c.MlirWalkResult) { + advance = c.MlirWalkResultAdvance, + interrupt = c.MlirWalkResultInterrupt, + skip = c.MlirWalkResultSkip, + }; + + pub fn walk(self: *Operation, order: WalkOrder, ctx: anytype, walkfn: fn (ctx_: @TypeOf(ctx), op: *Operation) WalkResult) void { + var inner_ctx = .{ .ctx = ctx }; + const ContextType = @TypeOf(inner_ctx); + + c.mlirOperationWalk( + self._inner, + (struct { + pub fn callback(op: c.MlirOperation, ctx_: ?*anyopaque) callconv(.c) c.MlirWalkResult { + const inner_ctx_: *ContextType = @ptrCast(@alignCast(ctx_)); + return @intFromEnum(walkfn(inner_ctx_.ctx, @ptrCast(op.ptr))); + } + }).callback, + &inner_ctx, + @intFromEnum(order), + ); + } +}; + +pub const Region = opaque { + const M = StdMethods(Region, c.MlirRegion); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirRegionEqual); + + pub fn init() Error!*Region { + return @ptrCast(c.mlirRegionCreate().ptr orelse return Error.InvalidMlir); + } + + pub fn deinit(self: *Region) void { + c.mlirRegionDestroy(self.ptr()); + } + + pub fn appendOwnedBlock(self: *Region, block: *Block) void { + c.mlirRegionAppendOwnedBlock(self.ptr(), block.ptr()); + } + + pub fn insertOwnedBlock(self: *Region, index: usize, block: *Block) void { + c.mlirRegionInsertOwnedBlock(self.ptr(), @intCast(index), block.ptr()); + } + + pub fn insertOwnedBlockBefore(self: *Region, reference: *const Block, block: *Block) void { + c.mlirRegionInsertOwnedBlockBefore(self.ptr(), reference.ptr(), block.ptr()); + } + + pub fn insertOwnedBlockAfter(self: *Region, reference: *const Block, block: *Block) void { + c.mlirRegionInsertOwnedBlockAfter(self.ptr(), reference.ptr(), block.ptr()); + } + + pub fn firstBlock(self: *const Region) ?*Block { + return @ptrCast(c.mlirRegionGetFirstBlock(self.ptr()).ptr); + } + + pub fn takeBody(self: *Region, from: *Region) void { + c.mlirRegionTakeBody(self.ptr(), from.ptr()); + } + + pub fn nextInOperation(self: *const Region) *Region { + return @ptrCast(c.mlirRegionGetNextInOperation(self.ptr()).ptr); + } +}; + +// pub const Block = opaque { +// const M = StdMethods(Block, c.MlirBlock); +// // pub const wrapOr = helpers.wrapOr(Block, c.mlirBlockIsNull); +// // pub const deinit = helpers.deinit(Block, c.mlirBlockDestroy); + +// // pub const eql = helpers.eql(Block, c.mlirBlockEqual); + +// pub fn init(args: []*const Type, locs: []const *const Location) !Block { +// return @ptrCast(c.mlirBlockCreate(@intCast(args.len), @ptrCast(args.ptr), @ptrCast(locs.ptr)) orelse return Error.InvalidMlir); +// } + +// pub fn argument(self: Block, index: usize) Value { +// return .{ ._inner = c.mlirBlockGetArgument(self._inner, @intCast(index)) }; +// } + +// pub fn numArguments(self: Block) usize { +// return @intCast(c.mlirBlockGetNumArguments(self._inner)); +// } + +// pub fn addArgument(self: *Block, typ: Type, loc: Location) Value { +// return .{ ._inner = c.mlirBlockAddArgument(self._inner, typ._inner, loc._inner) }; +// } + +// pub fn insertArgument(self: *Block, index: usize, typ: Type, loc: Location) Value { +// return .{ ._inner = c.mlirBlockInsertArgument(self._inner, @intCast(index), typ._inner, loc._inner) }; +// } + +// pub fn equals(self: Block, other: Block) bool { +// return c.mlirBlockEqual(self._inner, other._inner); +// } + +// pub fn appendOperation(self: Block, op: Operation) void { +// c.mlirBlockAppendOwnedOperation(self._inner, op._inner); +// } + +// pub fn appendOperations(self: *Block, ops: []const Operation) void { +// for (ops) |op| { +// c.mlirBlockAppendOwnedOperation(self._inner, op._inner); +// } +// } + +// pub const RecursiveOpts = enum { open, hermetic }; + +// pub fn appendValueRecursive(self: Block, value: Value, opt: RecursiveOpts) void { +// switch (value.kind()) { +// .op_result => |parent_op| self.appendOperationRecursive(parent_op, opt), +// .block_argument => |arg| { +// // Hermetic blocks are not allowed to use arguments from other blocks. +// stdx.debug.assert(opt == .open or self.eql(arg.block()), "Can't add {} from {?x} block to {?x} block", .{ arg, arg.block()._inner.ptr, self._inner.ptr }); +// }, +// .null => @panic("InvalidMlir"), +// } +// } + +// pub fn appendOperationRecursive(self: Block, op: Operation, opt: RecursiveOpts) void { +// if (op.block()) |prev_block| { +// // Hermetic blocks are not allowed to reference values from other blocks. +// stdx.debug.assert(opt == .open or self.equals(prev_block), "Can't add {} from {?x} block to {?x} block", .{ op, prev_block._inner.ptr, self._inner.ptr }); +// return; +// } +// for (0..op.numOperands()) |i| { +// self.appendValueRecursive(op.operand(i), opt); +// } +// self.appendOperation(op); +// } +// }; From 16ddc3d6a18a36272c9f11d8a75344845ed81804 Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Wed, 27 Aug 2025 19:21:18 +0200 Subject: [PATCH 02/37] WIP --- BUILD.bazel | 8 +- mlir/BUILD.bazel | 7 +- mlir/dialects/BUILD.bazel | 12 +- mlir/dialects/dialects.zig | 11 +- mlir/dialects/func.zig | 57 ++-- mlir/main.zig | 55 +++- mlir/registry.zig | 601 +++++++++++++++++++++++++------------ 7 files changed, 500 insertions(+), 251 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 4c17c21a..f891cd2d 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -10,10 +10,10 @@ zls_completion( name = "completion", visibility = ["//visibility:public"], deps = [ - "//async", - "//examples/llama", - "//stdx", - "//zml", + # "//async", + # "//examples/llama", + # "//stdx", + # "//zml", "//mlir:main", # "//async", # "//stdx", diff --git a/mlir/BUILD.bazel b/mlir/BUILD.bazel index 0dccf25d..f0ba476f 100644 --- a/mlir/BUILD.bazel +++ b/mlir/BUILD.bazel @@ -32,7 +32,11 @@ zig_test( zig_library( name = "mlir2", main = "registry.zig", - deps = [":c"], + visibility = ["//visibility:public"], + deps = [ + ":c", + "//stdx", + ], ) zig_binary( @@ -41,6 +45,7 @@ zig_binary( visibility = ["//visibility:public"], deps = [ ":mlir2", + "//mlir/dialects", # ":c", # "//stdx", ], diff --git a/mlir/dialects/BUILD.bazel b/mlir/dialects/BUILD.bazel index 101f67ff..4fbc61bd 100644 --- a/mlir/dialects/BUILD.bazel +++ b/mlir/dialects/BUILD.bazel @@ -3,18 +3,18 @@ load("@rules_zig//zig:defs.bzl", "zig_library", "zig_test") zig_library( name = "dialects", srcs = [ - "arith.zig", + # "arith.zig", "func.zig", - "math.zig", - "scf.zig", - "tensor.zig", + # "math.zig", + # "scf.zig", + # "tensor.zig", ], import_name = "mlir/dialects", main = "dialects.zig", visibility = ["//visibility:public"], deps = [ - "//mlir", - "//mlir/dialects/stablehlo", + "//mlir:mlir2", + # "//mlir/dialects/stablehlo", "//stdx", ], ) diff --git a/mlir/dialects/dialects.zig b/mlir/dialects/dialects.zig index 47f74d8e..b930086b 100644 --- a/mlir/dialects/dialects.zig +++ b/mlir/dialects/dialects.zig @@ -1,11 +1,12 @@ const std = @import("std"); -pub const arith = @import("arith.zig"); pub const func = @import("func.zig"); -pub const math = @import("math.zig"); -pub const scf = @import("scf.zig"); -pub const tensor = @import("tensor.zig"); -pub const stablehlo = @import("mlir/dialects/stablehlo"); + +// pub const arith = @import("arith.zig"); +// pub const math = @import("math.zig"); +// pub const scf = @import("scf.zig"); +// pub const tensor = @import("tensor.zig"); +// pub const stablehlo = @import("mlir/dialects/stablehlo"); test { std.testing.refAllDecls(@This()); diff --git a/mlir/dialects/func.zig b/mlir/dialects/func.zig index 868ff3bc..50eccf96 100644 --- a/mlir/dialects/func.zig +++ b/mlir/dialects/func.zig @@ -1,29 +1,27 @@ const std = @import("std"); -const mlir = @import("mlir"); +const mlir = @import("mlir2"); const stdx = @import("stdx"); -pub fn func( - ctx: mlir.Context, - args: struct { - sym_name: []const u8, - args: []const mlir.Type, - arg_attrs: []const mlir.Attribute = &.{}, - results: []const mlir.Type, - res_attrs: []const mlir.Attribute = &.{}, - block: mlir.Block, - location: mlir.Location, - }, -) mlir.Operation { - var attrs_tuple_buffer = stdx.BoundedArray(mlir.AttrTuple, 4){}; - attrs_tuple_buffer.appendAssumeCapacity(.{ "sym_name", .string(ctx, args.sym_name) }); - attrs_tuple_buffer.appendAssumeCapacity(.{ "function_type", .type_(.function(ctx, args.args, args.results)) }); - if (args.arg_attrs.len > 0) { - attrs_tuple_buffer.appendAssumeCapacity(.{ "arg_attrs", .array(ctx, args.arg_attrs) }); - } +const FuncOpArgs = struct { + name: []const u8, + args: []const *const mlir.Type, + args_attributes: ?[]const *const mlir.Attribute = null, + results: []const *const mlir.Type, + results_attributes: ?[]const *const mlir.Attribute = null, + block: *mlir.Block, + location: *const mlir.Location, +}; - if (args.res_attrs.len > 0) { - attrs_tuple_buffer.appendAssumeCapacity(.{ "res_attrs", .array(ctx, args.res_attrs) }); +pub fn func(ctx: *mlir.Context, args: FuncOpArgs) *mlir.Operation { + var attrs_tuple_buffer = stdx.BoundedArray(mlir.Operation.MakeArgs.AttrTuple, 4){}; + attrs_tuple_buffer.appendAssumeCapacity(.{ "sym_name", mlir.stringAttribute(ctx, args.name) }); + attrs_tuple_buffer.appendAssumeCapacity(.{ "function_type", mlir.typeAttribute(mlir.functionType(ctx, args.args, args.results)) }); + if (args.args_attributes) |args_attributes| { + attrs_tuple_buffer.appendAssumeCapacity(.{ "arg_attrs", mlir.arrayAttribute(ctx, args_attributes) }); + } + if (args.results_attributes) |results_attributes| { + attrs_tuple_buffer.appendAssumeCapacity(.{ "res_attrs", mlir.arrayAttribute(ctx, results_attributes) }); } return mlir.Operation.make(ctx, "func.func", .{ @@ -33,20 +31,21 @@ pub fn func( }); } -pub fn call(ctx: mlir.Context, name: [:0]const u8, values: []const mlir.Value, results: []const mlir.Type, loc: mlir.Location) mlir.Operation { +pub fn call(ctx: *mlir.Context, name: []const u8, values: []const *const mlir.Value, results: []const *const mlir.Type, loc: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "func.call", .{ - .variadic_operands = &.{values}, - .results = results, - .verify = true, - .attributes = &.{.{ "callee", .symbol(ctx, name) }}, + .operands = .{ .variadic = &.{values} }, + .results = .{ .flat = results }, + .attributes = &.{ + .{ "callee", mlir.flatSymbolRefAttribute(ctx, name) }, + }, .location = loc, }); } -pub fn return_(ctx: mlir.Context, values: []const mlir.Value, loc: mlir.Location) mlir.Operation { +pub fn return_(ctx: *mlir.Context, values: []const *const mlir.Value, loc: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "func.return", .{ - .operands = values, - .verify = false, + .operands = .{ .flat = values }, .location = loc, + .verify = false, }); } diff --git a/mlir/main.zig b/mlir/main.zig index 8095de85..177d2a5a 100644 --- a/mlir/main.zig +++ b/mlir/main.zig @@ -1,5 +1,6 @@ const std = @import("std"); +const dialects = @import("mlir/dialects"); const mlir = @import("mlir2"); pub fn main() !void { @@ -25,28 +26,50 @@ pub fn main() !void { const unk = mlir.Location.unknown(ctx); _ = unk; // autofix - const attr = mlir.integerAttribute(ctx, @as(i32, 5)); - _ = attr; // autofix + // const attr = mlir.integerAttribute(ctx, @as(i32, 5)); + // _ = attr; // autofix - const one: i32 = 1; - const two: i32 = 2; + // const one: i32 = 1; + // const two: i32 = 2; - const dict = mlir.DictionaryAttribute.init(ctx, &.{ - .named(ctx, "pouet", mlir.integerAttribute(ctx, one)), - .named(ctx, "pouet2", mlir.integerAttribute(ctx, two)), + // const dict = mlir.DictionaryAttribute.init(ctx, &.{ + // .named(ctx, "pouet", mlir.integerAttribute(ctx, one)), + // .named(ctx, "pouet2", mlir.integerAttribute(ctx, two)), + // }); + // _ = dict; // autofix + + const int64 = mlir.integerType(ctx, 64, .signless); + + const block = mlir.Block.init(&.{ int64, int64 }, &.{ .unknown(ctx), .unknown(ctx) }); + + block.appendOwnedOperation(dialects.func.return_(ctx, &.{block.argument(0)}, .unknown(ctx))); + + const op = dialects.func.func(ctx, .{ + .name = "pouet", + .args = &.{ int64, int64 }, + .results = &.{int64}, + .block = block, + .location = .unknown(ctx), }); - _ = dict; // autofix - const op = try mlir.Operation.parse(ctx, "func.func private @pouet()", "pouet"); + // const op = try mlir.Operation.parse(ctx, "func.func private @pouet()", "pouet"); - const mod = mlir.Module.init(mlir.Location.unknown(ctx)); + // // // const mod = mlir.Module.init(mlir.Location.unknown(ctx)); - const body = mod.body(); - _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); - _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); - _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); + // const op = mlir.Operation.make(ctx, "arith.addi", .{ + // .operands = .{ .flat = &.{ block.argument(0), block.argument(1) } }, + // .results = .{ .variadic = &.{ &.{ int64, int64 }, &.{int64} } }, + // }); + // defer op.deinit(); - body.appendOwnedOperation(op); + std.debug.print(">>>>>> {f}\n", .{op.fmt(.{ .print_generic_op_form = true })}); + + // const body = mod.body(); + // _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); + // _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); + // _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); + + // body.appendOwnedOperation(op); // op.deinit(); // body.detach(); @@ -56,7 +79,7 @@ pub fn main() !void { // std.debug.print(">>>>>> {any}\n", .{op.block().?.parentOperation()}); - std.debug.print(">>> {f}\n", .{body}); + // std.debug.print(">>> {f}\n", .{body}); // const attr = mlir.integerAttribute(ctx, @as(i32, 5)); // const attr2: *const mlir.Attribute = @ptrCast(attr); diff --git a/mlir/registry.zig b/mlir/registry.zig index 69e44975..b4b17d44 100644 --- a/mlir/registry.zig +++ b/mlir/registry.zig @@ -1,6 +1,7 @@ const std = @import("std"); const c = @import("c"); +const stdx = @import("stdx"); const log = std.log.scoped(.mlir); @@ -50,7 +51,7 @@ fn CastedPtr(comptime T1: type, comptime T2: type) type { return if (@typeInfo(T1).pointer.is_const) *const T2 else *T2; } -pub fn StdMethods(comptime T: type, comptime NativeT: type) type { +pub fn Methods(comptime T: type, comptime NativeT: type) type { return struct { pub const IsPtrConst = @typeInfo(@typeInfo(@FieldType(NativeT, "ptr")).optional.child).pointer.is_const; pub const Ptr = if (IsPtrConst) *const T else *T; @@ -86,7 +87,7 @@ pub fn StdMethods(comptime T: type, comptime NativeT: type) type { } pub const DialectRegistry = opaque { - const M = StdMethods(DialectRegistry, c.MlirDialectRegistry); + const M = Methods(DialectRegistry, c.MlirDialectRegistry); pub const ptr = M.ptr; @@ -104,7 +105,7 @@ pub const DialectRegistry = opaque { }; pub const Context = opaque { - const M = StdMethods(Context, c.MlirContext); + const M = Methods(Context, c.MlirContext); pub const ptr = M.ptr; pub const eql = M.eql(c.mlirContextEqual); @@ -165,7 +166,7 @@ pub const Context = opaque { }; pub const Location = opaque { - const M = StdMethods(Location, c.MlirLocation); + const M = Methods(Location, c.MlirLocation); pub const ptr = M.ptr; pub const eql = M.eql(c.mlirLocationEqual); @@ -224,7 +225,7 @@ pub const Location = opaque { }; pub const Type = opaque { - const M = StdMethods(Type, c.MlirType); + const M = Methods(Type, c.MlirType); pub const ptr = M.ptr; pub const eql = M.eql(c.mlirTypeEqual); @@ -237,7 +238,7 @@ pub const Type = opaque { }; pub const IndexType = opaque { - const M = StdMethods(IndexType, c.MlirType); + const M = Methods(IndexType, c.MlirType); pub const isAFn = c.mlirTypeIsAIndex; pub const ptr = M.ptr; @@ -256,7 +257,7 @@ pub const Signedness = enum { }; pub const IntegerType = opaque { - const M = StdMethods(IntegerType, c.MlirType); + const M = Methods(IntegerType, c.MlirType); pub const isAFn = c.mlirTypeIsAInteger; pub const ptr = M.ptr; @@ -298,7 +299,7 @@ pub const FloatTypes = enum { }; pub fn FloatType(comptime ft: FloatTypes) type { - const Config = switch (ft) { + const _isAFn, const getter = switch (ft) { .f4e2m1fn => .{ c.mlirTypeIsAFloat4E2M1FN, c.mlirFloat4E2M1FNTypeGet }, .f6e2m3fn => .{ c.mlirTypeIsAFloat6E2M3FN, c.mlirFloat6E2M3FNTypeGet }, .f6e3m2fn => .{ c.mlirTypeIsAFloat6E3M2FN, c.mlirFloat6E3M2FNTypeGet }, @@ -319,15 +320,14 @@ pub fn FloatType(comptime ft: FloatTypes) type { return opaque { const Self = @This(); - const M = StdMethods(Self, c.MlirType); + const M = Methods(Self, c.MlirType); - pub const isAFn = Config[0]; + pub const isAFn = _isAFn; pub const ptr = M.ptr; pub const eql = M.eql(c.mlirTypeEqual); pub const format = M.format(c.mlirTypePrint); pub fn get(ctx: *Context) *const Self { - const getter = Config[1]; return @ptrCast(getter(ctx.ptr()).ptr); } }; @@ -338,7 +338,7 @@ pub fn floatType(comptime ft: FloatTypes, ctx: *Context) *const Type { } pub const Attribute = opaque { - const M = StdMethods(Attribute, c.MlirAttribute); + const M = Methods(Attribute, c.MlirAttribute); pub const ptr = M.ptr; pub const eql = M.eql(c.mlirAttributeEqual); @@ -348,10 +348,18 @@ pub const Attribute = opaque { pub fn parse(ctx: *Context, attr: []const u8) Error!*const Attribute { return @ptrCast(c.mlirAttributeParseGet(ctx.ptr(), stringRef(attr)).ptr orelse return Error.InvalidMlir); } + + pub fn context(self: *const Context) *Context { + return @ptrCast(c.mlirAttributeGetContext(self.ptr()).ptr); + } + + pub fn dialect(self: *const Context) *const Dialect { + return @ptrCast(c.mlirAttributeGetDialect(self.ptr()).ptr); + } }; pub const StringAttribute = opaque { - const M = StdMethods(StringAttribute, c.MlirAttribute); + const M = Methods(StringAttribute, c.MlirAttribute); pub const isAFn = c.mlirAttributeIsAString; pub const ptr = M.ptr; @@ -372,7 +380,7 @@ pub fn stringAttribute(ctx: *Context, str: []const u8) *const Attribute { } pub const IntegerAttribute = opaque { - const M = StdMethods(IntegerAttribute, c.MlirAttribute); + const M = Methods(IntegerAttribute, c.MlirAttribute); pub const isAFn = c.mlirAttributeIsAInteger; pub const ptr = M.ptr; @@ -405,7 +413,7 @@ pub fn integerAttribute(ctx: *Context, value_: anytype) *const Attribute { } pub const BoolAttribute = opaque { - const M = StdMethods(BoolAttribute, c.MlirAttribute); + const M = Methods(BoolAttribute, c.MlirAttribute); pub const isAFn = c.mlirAttributeIsABool; pub const ptr = M.ptr; @@ -426,7 +434,7 @@ pub fn boolAttribute(ctx: *Context, value: bool) *const Attribute { } pub const TypeAttribute = opaque { - const M = StdMethods(TypeAttribute, c.MlirAttribute); + const M = Methods(TypeAttribute, c.MlirAttribute); pub const isAFn = c.mlirAttributeIsAType; pub const ptr = M.ptr; @@ -442,12 +450,12 @@ pub const TypeAttribute = opaque { } }; -pub fn typeAttribute(type_: *const Type) *const TypeAttribute { +pub fn typeAttribute(type_: *const Type) *const Attribute { return @ptrCast(TypeAttribute.init(type_)); } pub const Dialect = struct { - const M = StdMethods(Dialect, c.MlirDialect); + const M = Methods(Dialect, c.MlirDialect); pub const ptr = M.ptr; @@ -465,7 +473,7 @@ pub const Dialect = struct { }; pub const DialectHandle = opaque { - const M = StdMethods(DialectHandle, c.MlirDialectHandle); + const M = Methods(DialectHandle, c.MlirDialectHandle); pub const ptr = M.ptr; @@ -495,7 +503,7 @@ pub const DialectHandle = opaque { }; pub const Identifier = opaque { - const M = StdMethods(Identifier, c.MlirIdentifier); + const M = Methods(Identifier, c.MlirIdentifier); pub const ptr = M.ptr; pub const eql = M.eql(c.mlirIdentifierEqual); @@ -518,7 +526,7 @@ pub const Identifier = opaque { }; pub const Module = opaque { - const M = StdMethods(Module, c.MlirModule); + const M = Methods(Module, c.MlirModule); const ptr = M.ptr; @@ -552,7 +560,7 @@ pub const Module = opaque { }; pub const Block = opaque { - const M = StdMethods(Block, c.MlirBlock); + const M = Methods(Block, c.MlirBlock); pub const ptr = M.ptr; pub const eql = M.eql(c.mlirBlockEqual); @@ -645,7 +653,7 @@ pub const NamedAttribute = struct { }; pub const DictionaryAttribute = opaque { - const M = StdMethods(DictionaryAttribute, c.MlirAttribute); + const M = Methods(DictionaryAttribute, c.MlirAttribute); pub const isAFn = c.mlirAttributeIsADictionary; pub const ptr = M.ptr; @@ -656,7 +664,7 @@ pub const DictionaryAttribute = opaque { return @ptrCast(c.mlirDictionaryAttrGet( ctx.ptr(), @intCast(attributes.len), - @ptrCast(attributes.ptr), + @ptrCast(attributes), ).ptr); } @@ -665,7 +673,7 @@ pub const DictionaryAttribute = opaque { } pub fn get(self: *const DictionaryAttribute, pos: usize) NamedAttribute { - return .{ .inner = c.mlirDictionaryAttrGetElement(self.ptr(), @bitCast(pos)) }; + return .{ .inner = c.mlirDictionaryAttrGetElement(self.ptr(), @intCast(pos)) }; } pub fn getByName(self: *const DictionaryAttribute, name: []const u8) ?*const Attribute { @@ -674,7 +682,7 @@ pub const DictionaryAttribute = opaque { }; pub const Value = opaque { - const M = StdMethods(Value, c.MlirValue); + const M = Methods(Value, c.MlirValue); pub const ptr = M.ptr; pub const eql = M.eql(c.mlirValueEqual); @@ -699,7 +707,7 @@ pub const Value = opaque { }; pub const OpOperand = opaque { - const M = StdMethods(OpOperand, c.MlirOpOperand); + const M = Methods(OpOperand, c.MlirOpOperand); pub const ptr = M.ptr; @@ -717,7 +725,7 @@ pub const OpOperand = opaque { }; pub const OpResult = opaque { - const M = StdMethods(OpResult, c.MlirValue); + const M = Methods(OpResult, c.MlirValue); pub const isAFn = c.mlirValueIsAOpResult; pub const ptr = M.ptr; @@ -734,7 +742,7 @@ pub const OpResult = opaque { }; pub const BlockArgument = opaque { - const M = StdMethods(BlockArgument, c.MlirValue); + const M = Methods(BlockArgument, c.MlirValue); pub const isAFn = c.mlirValueIsABlockArgument; pub const ptr = M.ptr; @@ -755,47 +763,49 @@ pub const BlockArgument = opaque { }; pub const OperationState = struct { - const M = StdMethods(OperationState, c.MlirOperationState); + inner: c.MlirOperationState, - name: []const u8, - location: *const Location, - results: []const *const Type = &.{}, - operands: []const *const Value = &.{}, - regions: []*const Region = &.{}, - successors: []*const Block = &.{}, - attributes: []NamedAttribute = &.{}, - result_type_inference: bool = false, + pub fn init(name: []const u8, location: *const Location) OperationState { + return .{ .inner = c.mlirOperationStateGet(stringRef(name), location.ptr()) }; + } - pub const ptr = M.ptr; + pub fn addResults(self: *OperationState, results: []const *const Type) void { + c.mlirOperationStateAddResults(&self.inner, @intCast(results.len), @ptrCast(results)); + } - pub fn inner(self: OperationState) c.MlirOperationState { - return .{ - .name = stringRef(self.name), - .nResults = @intCast(self.results.len), - .results = @constCast(@ptrCast(self.results)), - .nOperands = @intCast(self.operands.len), - .operands = @constCast(@ptrCast(self.operands)), - .nRegions = @intCast(self.regions.len), - .regions = @constCast(@ptrCast(self.regions)), - .nSuccessors = @intCast(self.successors.len), - .successors = @constCast(@ptrCast(self.successors)), - .nAttributes = @intCast(self.attributes.len), - .attributes = @constCast(@ptrCast(self.attributes)), - .enableResultTypeInference = self.result_type_inference, - }; + pub fn addOperands(self: *OperationState, operands: []const *const Value) void { + c.mlirOperationStateAddOperands(&self.inner, @intCast(operands.len), @ptrCast(operands)); + } + + pub fn addOwnedRegions(self: *OperationState, regions: []const *Region) void { + c.mlirOperationStateAddOwnedRegions(&self.inner, @intCast(regions.len), @ptrCast(regions)); + } + + pub fn addSuccessors(self: *OperationState, successors: []*const Block) void { + c.mlirOperationStateAddSuccessors(&self.inner, @intCast(successors.len), @ptrCast(successors)); + } + + pub fn addAttributes(self: *OperationState, attributes: []const NamedAttribute) void { + c.mlirOperationStateAddAttributes(&self.inner, @intCast(attributes.len), @ptrCast(attributes.ptr)); + } + + pub fn enableResultTypeInference(self: *OperationState) void { + c.mlirOperationStateEnableResultTypeInference(&self.inner); } }; pub const Operation = opaque { - const M = StdMethods(Operation, c.MlirOperation); + const M = Methods(Operation, c.MlirOperation); + + const MAX_SEGMENTS = 32; pub const ptr = M.ptr; pub const eql = M.eql(c.mlirOperationEqual); pub const format = M.format(c.mlirOperationPrint); pub fn init(state: *OperationState) !*Operation { - var os = state.inner(); - return @ptrCast(c.mlirOperationCreate(&os).ptr orelse return Error.InvalidMlir); + defer state.* = undefined; + return @ptrCast(c.mlirOperationCreate(&state.inner).ptr orelse return Error.InvalidMlir); } pub fn deinit(self: *Operation) void { @@ -803,8 +813,12 @@ pub const Operation = opaque { c.mlirOperationDestroy(self.ptr()); } - pub fn parse(ctx: *Context, source: []const u8, name: []const u8) Error!*Operation { - return @ptrCast(c.mlirOperationCreateParse(ctx.ptr(), stringRef(source), stringRef(name)).ptr orelse return Error.InvalidMlir); + pub fn parse(ctx: *Context, source: []const u8, name_: []const u8) Error!*Operation { + return @ptrCast(c.mlirOperationCreateParse(ctx.ptr(), stringRef(source), stringRef(name_)).ptr orelse return Error.InvalidMlir); + } + + pub fn name(self: *const Operation) []const u8 { + return string(c.mlirOperationGetName(self.ptr())); } pub fn clone(self: *const Operation) *Operation { @@ -819,6 +833,38 @@ pub const Operation = opaque { return @ptrCast(c.mlirOperationGetParentOperation(self.ptr()).ptr); } + pub fn removeFromParent(self: *Operation) void { + c.mlirOperationRemoveFromParent(self.ptr()); + } + + pub fn numOperands(self: *const Operation) usize { + return @intCast(c.mlirOperationGetNumOperands(self.ptr())); + } + + pub fn operand(self: *const Operation, index: usize) *const Value { + return @ptrCast(c.mlirOperationGetOperand(self.ptr(), @intCast(index)).ptr); + } + + pub fn setOperand(self: *Operation, index: usize, value: *const Value) void { + c.mlirOperationSetOperand(self.ptr(), @intCast(index), value.ptr()); + } + + pub fn numResults(self: *const Operation) usize { + return @intCast(c.mlirOperationGetNumResults(self.ptr())); + } + + pub fn result(self: *const Operation, index: usize) Value { + return @ptrCast(c.mlirOperationGetResult(self.ptr(), @intCast(index)).ptr); + } + + pub fn nextInBlock(self: *const Operation) *const Operation { + return @ptrCast(c.mlirOperationGetNextInBlock(self.ptr()).ptr); + } + + pub fn context(self: *const Operation) *Context { + return @ptrCast(c.mlirOperationGetContext(self.ptr()).ptr); + } + pub const PrintFlags = struct { elide_large_elements_attrs: ?usize = null, debug_info: bool = false, @@ -827,13 +873,13 @@ pub const Operation = opaque { use_local_scope: bool = false, assume_verified: bool = false, - const FmtCtx = struct { + const Ctx = struct { self: *const Operation, flags: PrintFlags, }; }; - fn formatWithFlags(fctx: PrintFlags.FmtCtx, writer: *std.io.Writer) std.Io.Writer.Error!void { + fn formatWithFlags(fctx: PrintFlags.Ctx, writer: *std.io.Writer) std.Io.Writer.Error!void { const flags = c.mlirOpPrintingFlagsCreate(); defer c.mlirOpPrintingFlagsDestroy(flags); @@ -861,7 +907,7 @@ pub const Operation = opaque { } } - pub fn fmt(self: *const Operation, flags: PrintFlags) std.fmt.Alt(PrintFlags.FmtCtx, formatWithFlags) { + pub fn fmt(self: *const Operation, flags: PrintFlags) std.fmt.Alt(PrintFlags.Ctx, formatWithFlags) { return .{ .data = .{ .self = self, .flags = flags, @@ -871,82 +917,70 @@ pub const Operation = opaque { pub const MakeArgs = struct { pub const AttrTuple = struct { []const u8, *const Attribute }; - operands: []const *const Value, - operands_variadic: ?[]const usize = null, - results: []const *const Type, - results_variadic: ?[]const usize = null, + pub const Operands = union(enum) { + flat: []const *const Value, + variadic: []const []const *const Value, + }; + + pub const ResultTypes = union(enum) { + flat: []const *const Type, + variadic: []const []const *const Type, + }; + + operands: ?Operands = null, + results: ?ResultTypes = null, result_type_inference: bool = false, - n_regions: usize = 0, attributes: ?[]const AttrTuple = null, - blocks: ?[]const *const Block = null, + blocks: ?[]const *Block = null, verify: bool = true, - location: *const Location, + location: ?*const Location = null, }; - pub fn try_make(ctx: *Context, op_name: []const u8, args: MakeArgs) !*Operation { - _ = ctx; // autofix - var state: OperationState = .{ - .name = op_name, - .location = args.location, - .result_type_inference = args.result_type_inference, + pub fn try_make(ctx: *Context, name_: []const u8, args: MakeArgs) !*Operation { + var state: OperationState = .init(name_, args.location orelse .unknown(ctx)); + if (args.result_type_inference) { + state.enableResultTypeInference(); + } + if (args.operands) |operands| switch (operands) { + .flat => |v| state.addOperands(v), + .variadic => |segments| { + var sizes: stdx.BoundedArray(i32, MAX_SEGMENTS) = .{}; + for (segments) |segment_operands| { + state.addOperands(segment_operands); + sizes.appendAssumeCapacity(@intCast(segment_operands.len)); + } + state.addAttributes(&.{ + .named(ctx, "operandSegmentSizes", denseArrayAttribute(ctx, .i32, sizes.constSlice())), + }); + }, }; - // std.debug.assert(!(args.operands != null and args.variadic_operands != null)); - state.addOperands(operands); - - // if (args.operands) |operands| { - // state.addOperands(operands); - // // } else if (args.variadic_operands) |operands_segments| { - // // const MAX_SEGMENTS = 32; - // // var segments: std.BoundedArray(i32, MAX_SEGMENTS) = .{}; - - // // for (operands_segments) |operands| { - // // state.addOperands(operands); - // // segments.appendAssumeCapacity(@intCast(operands.len)); - // // } - // // state.addAttribute(ctx, "operandSegmentSizes", .denseElements(ctx, &.{@intCast(segments.len)}, .i32, segments.constSlice())); - // // } else if (args.tt_variadic_operands) |operands_segments| { - // // // stablehlo and triton seems to disagree on the expected type of operandSegmentSizes, let's fix that. - // // const MAX_SEGMENTS = 32; - // // var segments: std.BoundedArray(i32, MAX_SEGMENTS) = .{}; - - // // for (operands_segments) |operands| { - // // state.addOperands(operands); - // // segments.appendAssumeCapacity(@intCast(operands.len)); - // // } - // // state.addAttribute(ctx, "operandSegmentSizes", .dense(ctx, .i32, segments.constSlice())); - // } - // std.debug.assert(!(args.results != null and args.variadic_results != null)); - // if (args.results) |results| { - // state.addResults(results); - // // } else if (args.variadic_results) |result_segments| { - // // for (result_segments) |results| { - // // state.addResults(results); - // // } - // } - // for (0..args.n_regions) |_| { - // var region_ = Region.init() catch { - // @panic("Failed to create MLIR region"); - // }; - // state.addRegion(®ion_); - // } - // if (args.attributes) |attrs| { - // for (attrs) |attr| { - // state.addAttributeRaw( - // Identifier.get(ctx, attr[0]), - // attr[1], - // ); - // } - // } - // if (args.blocks) |blocks_| { - // for (blocks_) |block_| { - // var region_ = Region.init() catch { - // @panic("Failed to create MLIR region"); - // }; - // region_.appendBlock(block_); - // state.addRegion(®ion_); - // } - // } - + if (args.results) |results| switch (results) { + .flat => |v| state.addResults(v), + .variadic => |segments| { + var sizes: stdx.BoundedArray(i32, MAX_SEGMENTS) = .{}; + for (segments) |segment_results| { + state.addResults(segment_results); + sizes.appendAssumeCapacity(@intCast(segment_results.len)); + } + state.addAttributes(&.{ + .named(ctx, "resultSegmentSizes", denseArrayAttribute(ctx, .i32, sizes.constSlice())), + }); + }, + }; + if (args.attributes) |attrs| { + for (attrs) |attr_tuple| { + const attr_name, const attr = attr_tuple; + const named_attr = NamedAttribute.named(ctx, attr_name, attr); + state.addAttributes(&.{named_attr}); + } + } + if (args.blocks) |blocks| { + const region = Region.init(); + for (blocks) |block_| { + region.appendOwnedBlock(block_); + } + state.addOwnedRegions(&.{region}); + } const new_op = try Operation.init(&state); if (args.verify and new_op.verify() == false) { log.err("Failed to verify MLIR operation:\n{f}", .{ @@ -957,7 +991,6 @@ pub const Operation = opaque { }); return Error.InvalidMlir; } - return new_op; } @@ -1002,16 +1035,58 @@ pub const Operation = opaque { @intFromEnum(order), ); } + + pub const BytecodeWriterConfig = struct { + desired_emit_version: ?usize = null, + + const Ctx = struct { + self: *const Operation, + config: ?BytecodeWriterConfig, + }; + }; + + pub fn writeBytecode(self: *const Operation, config: ?BytecodeWriterConfig, writer: *std.Io.Writer) !void { + if (config) |config_| { + const bc_config = c.mlirBytecodeWriterConfigCreate(); + defer c.mlirBytecodeWriterConfigDestroy(bc_config); + if (config_.desired_emit_version) |version| { + c.mlirBytecodeWriterConfigDesiredEmitVersion(bc_config, @intCast(version)); + } + var sctx: StringCallbackCtx = .{ .writer = writer }; + c.mlirOperationWriteBytecodeWithConfig(self.ptr(), bc_config, stringCallback, &sctx); + if (sctx.err) |err| { + return err; + } + return; + } + + var sctx: StringCallbackCtx = .{ .writer = writer }; + c.mlirOperationWriteBytecode(self.ptr(), stringCallback, &sctx); + if (sctx.err) |err| { + return err; + } + } + + fn fmtBytecodeImpl(ctx: BytecodeWriterConfig.Ctx, writer: *std.io.Writer) std.Io.Writer.Error!void { + try ctx.self.writeBytecode(ctx.config, writer); + } + + pub fn fmtBytecode(self: *const Operation, config: ?BytecodeWriterConfig) std.fmt.Alt(BytecodeWriterConfig.Ctx, fmtBytecodeImpl) { + return .{ .data = .{ + .self = self, + .config = config, + } }; + } }; pub const Region = opaque { - const M = StdMethods(Region, c.MlirRegion); + const M = Methods(Region, c.MlirRegion); pub const ptr = M.ptr; pub const eql = M.eql(c.mlirRegionEqual); - pub fn init() Error!*Region { - return @ptrCast(c.mlirRegionCreate().ptr orelse return Error.InvalidMlir); + pub fn init() *Region { + return @ptrCast(c.mlirRegionCreate().ptr); } pub fn deinit(self: *Region) void { @@ -1047,69 +1122,215 @@ pub const Region = opaque { } }; -// pub const Block = opaque { -// const M = StdMethods(Block, c.MlirBlock); -// // pub const wrapOr = helpers.wrapOr(Block, c.mlirBlockIsNull); -// // pub const deinit = helpers.deinit(Block, c.mlirBlockDestroy); +pub const ShapedType = opaque { + const M = Methods(ShapedType, c.MlirType); -// // pub const eql = helpers.eql(Block, c.mlirBlockEqual); + pub const isAFn = c.mlirTypeIsAShaped; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + pub const isA = M.isA; -// pub fn init(args: []*const Type, locs: []const *const Location) !Block { -// return @ptrCast(c.mlirBlockCreate(@intCast(args.len), @ptrCast(args.ptr), @ptrCast(locs.ptr)) orelse return Error.InvalidMlir); -// } + pub fn elementType(self: *const ShapedType) *const Type { + return @ptrCast(c.mlirShapedTypeGetElementType(self.ptr()).ptr); + } -// pub fn argument(self: Block, index: usize) Value { -// return .{ ._inner = c.mlirBlockGetArgument(self._inner, @intCast(index)) }; -// } + pub fn rank(self: *const ShapedType) usize { + return @intCast(c.mlirShapedTypeGetRank(self.ptr())); + } -// pub fn numArguments(self: Block) usize { -// return @intCast(c.mlirBlockGetNumArguments(self._inner)); -// } + pub fn dimension(self: *const ShapedType, dim: usize) i64 { + return c.mlirShapedTypeGetDimSize(self.ptr(), @intCast(dim)); + } +}; -// pub fn addArgument(self: *Block, typ: Type, loc: Location) Value { -// return .{ ._inner = c.mlirBlockAddArgument(self._inner, typ._inner, loc._inner) }; -// } +pub const RankedTensorType = opaque { + const M = Methods(RankedTensorType, c.MlirType); -// pub fn insertArgument(self: *Block, index: usize, typ: Type, loc: Location) Value { -// return .{ ._inner = c.mlirBlockInsertArgument(self._inner, @intCast(index), typ._inner, loc._inner) }; -// } + pub const isAFn = c.mlirTypeIsARankedTensor; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); -// pub fn equals(self: Block, other: Block) bool { -// return c.mlirBlockEqual(self._inner, other._inner); -// } + pub fn init(dimensions: []const i64, elemType: *const Type) *const RankedTensorType { + return @ptrCast(c.mlirRankedTensorTypeGet( + @intCast(dimensions.len), + @ptrCast(dimensions), + elemType.ptr(), + c.mlirAttributeGetNull(), + ).ptr); + } -// pub fn appendOperation(self: Block, op: Operation) void { -// c.mlirBlockAppendOwnedOperation(self._inner, op._inner); -// } + pub fn elementType(self: *const RankedTensorType) *const Type { + return @ptrCast(c.mlirShapedTypeGetElementType(self.ptr()).ptr); + } -// pub fn appendOperations(self: *Block, ops: []const Operation) void { -// for (ops) |op| { -// c.mlirBlockAppendOwnedOperation(self._inner, op._inner); -// } -// } + pub fn rank(self: *const RankedTensorType) usize { + return @intCast(c.mlirShapedTypeGetRank(self.ptr())); + } -// pub const RecursiveOpts = enum { open, hermetic }; + pub fn dimension(self: *const RankedTensorType, dim: usize) i64 { + return c.mlirShapedTypeGetDimSize(self.ptr(), @intCast(dim)); + } -// pub fn appendValueRecursive(self: Block, value: Value, opt: RecursiveOpts) void { -// switch (value.kind()) { -// .op_result => |parent_op| self.appendOperationRecursive(parent_op, opt), -// .block_argument => |arg| { -// // Hermetic blocks are not allowed to use arguments from other blocks. -// stdx.debug.assert(opt == .open or self.eql(arg.block()), "Can't add {} from {?x} block to {?x} block", .{ arg, arg.block()._inner.ptr, self._inner.ptr }); -// }, -// .null => @panic("InvalidMlir"), -// } -// } + pub fn shaped(self: *const RankedTensorType) *const ShapedType { + return @ptrCast(self); + } +}; -// pub fn appendOperationRecursive(self: Block, op: Operation, opt: RecursiveOpts) void { -// if (op.block()) |prev_block| { -// // Hermetic blocks are not allowed to reference values from other blocks. -// stdx.debug.assert(opt == .open or self.equals(prev_block), "Can't add {} from {?x} block to {?x} block", .{ op, prev_block._inner.ptr, self._inner.ptr }); -// return; -// } -// for (0..op.numOperands()) |i| { -// self.appendValueRecursive(op.operand(i), opt); -// } -// self.appendOperation(op); -// } -// }; +pub fn rankedTensorType(dimensions: []const i64, elemType: *const Type) *const Type { + return @ptrCast(RankedTensorType.init(dimensions, elemType)); +} + +pub const DenseElementsAttribute = opaque { + const M = Methods(DenseElementsAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsADenseElements; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(shaped_type: *const ShapedType, values: []const u8) *const DenseElementsAttribute { + return @ptrCast(c.mlirDenseElementsAttrRawBufferGet( + shaped_type.ptr(), + @intCast(values.len), + @ptrCast(values), + ).ptr orelse unreachable); + } +}; + +pub fn denseElementsAttribute(shaped_type: *const ShapedType, values: []const u8) *const Attribute { + return @ptrCast(DenseElementsAttribute.init(shaped_type, values)); +} + +pub const DenseArrayTypes = enum { + bool, + i8, + i16, + i32, + i64, + f32, + f64, + + fn ZigType(comptime ElementType: @This()) type { + return switch (ElementType) { + .bool => i32, + .i8 => i8, + .i16 => i16, + .i32 => i32, + .i64 => i64, + .f32 => f32, + .f64 => f64, + }; + } +}; + +pub fn DenseArrayAttribute(comptime ElementType: DenseArrayTypes) type { + const ElementTypeZig = ElementType.ZigType(); + + const _isAFn, const getFn, const getElementFn = switch (ElementType) { + .bool => .{ c.mlirAttributeIsADenseBoolArray, c.mlirDenseBoolArrayGet, c.mlirDenseBoolArrayGetElement }, + .i8 => .{ c.mlirAttributeIsADenseI8Array, c.mlirDenseI8ArrayGet, c.mlirDenseI8ArrayGetElement }, + .i16 => .{ c.mlirAttributeIsADenseI16Array, c.mlirDenseI16ArrayGet, c.mlirDenseI16ArrayGetElement }, + .i32 => .{ c.mlirAttributeIsADenseI32Array, c.mlirDenseI32ArrayGet, c.mlirDenseI32ArrayGetElement }, + .i64 => .{ c.mlirAttributeIsADenseI64Array, c.mlirDenseI64ArrayGet, c.mlirDenseI64ArrayGetElement }, + .f32 => .{ c.mlirAttributeIsADenseF32Array, c.mlirDenseF32ArrayGet, c.mlirDenseF32ArrayGetElement }, + .f64 => .{ c.mlirAttributeIsADenseF64Array, c.mlirDenseF64ArrayGet, c.mlirDenseF64ArrayGetElement }, + }; + + return struct { + const Self = @This(); + + const M = Methods(@This(), c.MlirAttribute); + + pub const isAFn = _isAFn; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, values: []const ElementTypeZig) *const Self { + return @ptrCast(getFn(ctx.ptr(), @intCast(values.len), @ptrCast(values)).ptr); + } + + pub fn get(self: *const Self, pos: usize) ElementTypeZig { + return getElementFn(self.ptr(), @intCast(pos)); + } + + pub fn len(self: *const Self) usize { + return @intCast(c.mlirDenseArrayGetNumElements(self.ptr())); + } + }; +} + +pub fn denseArrayAttribute(ctx: *Context, comptime ElementType: DenseArrayTypes, values: []const ElementType.ZigType()) *const Attribute { + return @ptrCast(DenseArrayAttribute(ElementType).init(ctx, @ptrCast(values))); +} + +pub const ArrayAttribute = opaque { + const M = Methods(ArrayAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAArray; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, attrs: []const *const Attribute) *const ArrayAttribute { + return @ptrCast(c.mlirArrayAttrGet(ctx.ptr(), @intCast(attrs.len), @ptrCast(attrs)).ptr); + } + + pub fn size(self: *const ArrayAttribute) usize { + return @intCast(c.mlirArrayAttrGetNumElements(self.ptr())); + } + + pub fn get(self: *const ArrayAttribute, index: usize) Attribute { + return @ptrCast(c.mlirArrayAttrGetElement(self.ptr(), @intCast(index)).ptr); + } +}; + +pub fn arrayAttribute(ctx: *Context, attrs: []const *const Attribute) *const Attribute { + return @ptrCast(ArrayAttribute.init(ctx, attrs)); +} + +pub const FunctionType = opaque { + const M = Methods(FunctionType, c.MlirType); + pub const isAFn = c.mlirTypeIsAFunction; + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + pub fn init(ctx: *Context, args: []const *const Type, results: []const *const Type) *const FunctionType { + return @ptrCast(c.mlirFunctionTypeGet( + ctx.ptr(), + @intCast(args.len), + @ptrCast(args.ptr), + @intCast(results.len), + @ptrCast(results.ptr), + ).ptr); + } +}; + +pub fn functionType(ctx: *Context, args: []const *const Type, results: []const *const Type) *const Type { + return @ptrCast(FunctionType.init(ctx, args, results)); +} + +pub const FlatSymbolRefAttribute = opaque { + const M = Methods(FlatSymbolRefAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAFlatSymbolRef; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, symbol_: []const u8) *const FlatSymbolRefAttribute { + return @ptrCast(c.mlirFlatSymbolRefAttrGet(ctx.ptr(), stringRef(symbol_)).ptr); + } + + pub fn symbol(self: *const FlatSymbolRefAttribute) []const u8 { + return string(c.mlirFlatSymbolRefAttrGetValue(self.ptr())); + } +}; + +pub fn flatSymbolRefAttribute(ctx: *Context, symbol: []const u8) *const Attribute { + return @ptrCast(FlatSymbolRefAttribute.init(ctx, symbol)); +} From a49efee290c259a6ae79ab09b82abfd4f31b6bca Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Thu, 28 Aug 2025 01:25:22 +0200 Subject: [PATCH 03/37] WIP --- mlir/dialects/BUILD.bazel | 2 +- mlir/dialects/arith.zig | 78 +- mlir/dialects/dialects.zig | 4 +- mlir/dialects/func.zig | 69 +- mlir/dialects/math.zig | 36 - mlir/dialects/scf.zig | 61 - mlir/dialects/stablehlo/BUILD.bazel | 2 +- mlir/dialects/stablehlo/stablehlo.zig | 2585 ++++++++++++------------- mlir/main.zig | 53 +- mlir/registry.zig | 138 +- 10 files changed, 1493 insertions(+), 1535 deletions(-) delete mode 100644 mlir/dialects/math.zig delete mode 100644 mlir/dialects/scf.zig diff --git a/mlir/dialects/BUILD.bazel b/mlir/dialects/BUILD.bazel index 4fbc61bd..34400ca9 100644 --- a/mlir/dialects/BUILD.bazel +++ b/mlir/dialects/BUILD.bazel @@ -14,7 +14,7 @@ zig_library( visibility = ["//visibility:public"], deps = [ "//mlir:mlir2", - # "//mlir/dialects/stablehlo", + "//mlir/dialects/stablehlo", "//stdx", ], ) diff --git a/mlir/dialects/arith.zig b/mlir/dialects/arith.zig index 39cbc2a1..4e8e0ed9 100644 --- a/mlir/dialects/arith.zig +++ b/mlir/dialects/arith.zig @@ -1,59 +1,61 @@ const std = @import("std"); -const mlir = @import("mlir"); +const mlir = @import("mlir2"); -pub fn constant(ctx: mlir.Context, value: mlir.Attribute, location: mlir.Location) mlir.Operation { +pub fn constant(ctx: *mlir.Context, value: *const mlir.Attribute, location: *const mlir.Location) *const mlir.Operation { return mlir.Operation.make(ctx, "arith.constant", .{ - .attributes = &.{.{ "value", value }}, + .attributes = &.{ + .named(ctx, "value", value), + }, .result_type_inference = true, .location = location, }); } -fn binary_fn(comptime op_name: [:0]const u8) fn (mlir.Context, mlir.Value, mlir.Value, mlir.Location) mlir.Operation { +fn binaryOp(comptime op_name: []const u8) type { return struct { - pub fn call(ctx: mlir.Context, lhs: mlir.Value, rhs: mlir.Value, location: mlir.Location) mlir.Operation { + pub fn call(ctx: *mlir.Context, lhs: *const mlir.Value, rhs: *const mlir.Value, location: *const mlir.Location) *const mlir.Operation { return mlir.Operation.make(ctx, op_name, .{ - .operands = &.{ lhs, rhs }, + .operands = .{ .flat = &.{ lhs, rhs } }, .result_type_inference = true, .location = location, }); } - }.call; + }; } -fn cast_fn(comptime op_name: [:0]const u8) fn (mlir.Context, mlir.Value, mlir.Type, mlir.Location) mlir.Operation { +fn castOp(comptime op_name: []const u8) type { return struct { - pub fn call(ctx: mlir.Context, value: mlir.Value, new_type: mlir.Type, location: mlir.Location) mlir.Operation { + pub fn call(ctx: mlir.Context, value: *const mlir.Value, new_type: *const mlir.Type, location: *const mlir.Location) *const mlir.Operation { return mlir.Operation.make(ctx, op_name, .{ - .operands = &.{value}, - .results = &.{new_type}, + .operands = .{ .float = &.{value} }, + .results = .{ .flat = &.{new_type} }, .location = location, }); } - }.call; + }; } -pub const addi = binary_fn("arith.addi"); -pub const addf = binary_fn("arith.addf"); -pub const subi = binary_fn("arith.subi"); -pub const subf = binary_fn("arith.subf"); -pub const muli = binary_fn("arith.muli"); -pub const mulf = binary_fn("arith.mulf"); -pub const divsi = binary_fn("arith.divsi"); -pub const divui = binary_fn("arith.divui"); -pub const divf = binary_fn("arith.divf"); -pub const maxnumf = binary_fn("arith.maxnumf"); -pub const maxnumi = binary_fn("arith.maxnumi"); -pub const extsi = cast_fn("arith.extsi"); -pub const extui = cast_fn("arith.extui"); -pub const extf = cast_fn("arith.extf"); -pub const trunci = cast_fn("arith.trunci"); -pub const truncf = cast_fn("arith.truncf"); -pub const fptosi = cast_fn("arith.fptosi"); -pub const fptoui = cast_fn("arith.fptoui"); -pub const sitofp = cast_fn("arith.sitofp"); -pub const uitofp = cast_fn("arith.uitofp"); +pub const addi = binaryOp("arith.addi").call; +pub const addf = binaryOp("arith.addf").call; +pub const subi = binaryOp("arith.subi").call; +pub const subf = binaryOp("arith.subf").call; +pub const muli = binaryOp("arith.muli").call; +pub const mulf = binaryOp("arith.mulf").call; +pub const divsi = binaryOp("arith.divsi").call; +pub const divui = binaryOp("arith.divui").call; +pub const divf = binaryOp("arith.divf").call; +pub const maxnumf = binaryOp("arith.maxnumf").call; +pub const maxnumi = binaryOp("arith.maxnumi").call; +pub const extsi = castOp("arith.extsi").call; +pub const extui = castOp("arith.extui").call; +pub const extf = castOp("arith.extf").call; +pub const trunci = castOp("arith.trunci").call; +pub const truncf = castOp("arith.truncf").call; +pub const fptosi = castOp("arith.fptosi").call; +pub const fptoui = castOp("arith.fptoui").call; +pub const sitofp = castOp("arith.sitofp").call; +pub const uitofp = castOp("arith.uitofp").call; pub const CmpIPredicate = enum { eq, @@ -68,12 +70,12 @@ pub const CmpIPredicate = enum { uge, }; -pub fn cmpi(ctx: mlir.Context, predicate: CmpIPredicate, lhs: mlir.Value, rhs: mlir.Value, location: mlir.Location) mlir.Operation { +pub fn cmpi(ctx: mlir.Context, predicate: CmpIPredicate, lhs: *const mlir.Value, rhs: *const mlir.Value, location: mlir.Location) *const mlir.Operation { return mlir.Operation.make(ctx, "arith.cmpi", .{ - .operands = &.{ lhs, rhs }, + .operands = .{ .flat = &.{ lhs, rhs } }, .result_type_inference = true, .attributes = &.{ - .{ "predicate", .int(ctx, .i64, @intFromEnum(predicate)) }, + .{ "predicate", mlir.integerAttribute(ctx, .i64, @intFromEnum(predicate)) }, }, .location = location, }); @@ -98,12 +100,12 @@ pub const CmpFPredicate = enum { true, }; -pub fn cmpf(ctx: mlir.Context, predicate: CmpFPredicate, lhs: mlir.Value, rhs: mlir.Value, location: mlir.Location) mlir.Operation { +pub fn cmpf(ctx: mlir.Context, predicate: CmpFPredicate, lhs: *const mlir.Value, rhs: *const mlir.Value, location: *const mlir.Location) *const mlir.Operation { return mlir.Operation.make(ctx, "arith.cmpf", .{ - .operands = &.{ lhs, rhs }, + .operands = .{ .flat = &.{ lhs, rhs } }, .result_type_inference = true, .attributes = &.{ - .{ "predicate", .int(ctx, .i64, @intFromEnum(predicate)) }, + .named(ctx, "predicate", mlir.integerAttribute(ctx, .i64, @intFromEnum(predicate))), }, .location = location, }); diff --git a/mlir/dialects/dialects.zig b/mlir/dialects/dialects.zig index b930086b..3649d443 100644 --- a/mlir/dialects/dialects.zig +++ b/mlir/dialects/dialects.zig @@ -1,13 +1,13 @@ const std = @import("std"); +pub const stablehlo = @import("mlir/dialects/stablehlo"); + pub const func = @import("func.zig"); // pub const arith = @import("arith.zig"); // pub const math = @import("math.zig"); // pub const scf = @import("scf.zig"); // pub const tensor = @import("tensor.zig"); -// pub const stablehlo = @import("mlir/dialects/stablehlo"); - test { std.testing.refAllDecls(@This()); } diff --git a/mlir/dialects/func.zig b/mlir/dialects/func.zig index 50eccf96..178aab75 100644 --- a/mlir/dialects/func.zig +++ b/mlir/dialects/func.zig @@ -4,48 +4,89 @@ const mlir = @import("mlir2"); const stdx = @import("stdx"); const FuncOpArgs = struct { + pub const Visibility = enum { + public, + private, + }; + name: []const u8, - args: []const *const mlir.Type, + args: ?[]const *const mlir.Type = null, args_attributes: ?[]const *const mlir.Attribute = null, - results: []const *const mlir.Type, + results: ?[]const *const mlir.Type = null, results_attributes: ?[]const *const mlir.Attribute = null, block: *mlir.Block, location: *const mlir.Location, + no_inline: bool = true, + visibility: Visibility = .public, }; pub fn func(ctx: *mlir.Context, args: FuncOpArgs) *mlir.Operation { - var attrs_tuple_buffer = stdx.BoundedArray(mlir.Operation.MakeArgs.AttrTuple, 4){}; - attrs_tuple_buffer.appendAssumeCapacity(.{ "sym_name", mlir.stringAttribute(ctx, args.name) }); - attrs_tuple_buffer.appendAssumeCapacity(.{ "function_type", mlir.typeAttribute(mlir.functionType(ctx, args.args, args.results)) }); + var args_buffer: stdx.BoundedArray(*const mlir.Type, 1024) = .{}; + var results_buffer: stdx.BoundedArray(*const mlir.Type, 32) = .{}; + + var attr_tuples_buffer = stdx.BoundedArray(mlir.NamedAttribute, 16).fromSlice(&.{ + .named(ctx, "sym_name", mlir.stringAttribute(ctx, args.name)), + .named(ctx, "sym_visibility", mlir.stringAttribute(ctx, @tagName(args.visibility))), + .named(ctx, "function_type", mlir.typeAttribute(mlir.functionType( + ctx, + args.args orelse args: { + for (0..args.block.numArguments()) |i| { + args_buffer.appendAssumeCapacity(args.block.argument(i).type_()); + } + break :args args_buffer.constSlice(); + }, + args.results orelse results: { + const terminator = args.block.terminator() orelse { + @panic("block has no terminator"); + }; + for (0..terminator.numOperands()) |i| { + results_buffer.appendAssumeCapacity(terminator.operand(i).type_()); + } + break :results results_buffer.constSlice(); + }, + ))), + }) catch unreachable; + if (args.args_attributes) |args_attributes| { - attrs_tuple_buffer.appendAssumeCapacity(.{ "arg_attrs", mlir.arrayAttribute(ctx, args_attributes) }); + attr_tuples_buffer.appendAssumeCapacity(.named(ctx, "arg_attrs", mlir.arrayAttribute(ctx, args_attributes))); } if (args.results_attributes) |results_attributes| { - attrs_tuple_buffer.appendAssumeCapacity(.{ "res_attrs", mlir.arrayAttribute(ctx, results_attributes) }); + attr_tuples_buffer.appendAssumeCapacity(.named(ctx, "res_attrs", mlir.arrayAttribute(ctx, results_attributes))); + } + if (args.no_inline) { + attr_tuples_buffer.appendAssumeCapacity(.named(ctx, "no_inline", mlir.unitAttribute(ctx))); } return mlir.Operation.make(ctx, "func.func", .{ .blocks = &.{args.block}, - .attributes = attrs_tuple_buffer.constSlice(), + .attributes = attr_tuples_buffer.constSlice(), .location = args.location, }); } -pub fn call(ctx: *mlir.Context, name: []const u8, values: []const *const mlir.Value, results: []const *const mlir.Type, loc: *const mlir.Location) *mlir.Operation { +pub fn call(ctx: *mlir.Context, name: []const u8, values: []const *const mlir.Value, results: []const *const mlir.Type, location: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "func.call", .{ .operands = .{ .variadic = &.{values} }, .results = .{ .flat = results }, .attributes = &.{ - .{ "callee", mlir.flatSymbolRefAttribute(ctx, name) }, + .named(ctx, "callee", mlir.flatSymbolRefAttribute(ctx, name)), }, - .location = loc, + .location = location, }); } -pub fn return_(ctx: *mlir.Context, values: []const *const mlir.Value, loc: *const mlir.Location) *mlir.Operation { +pub fn return_(ctx: *mlir.Context, values: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "func.return", .{ - .operands = .{ .flat = values }, - .location = loc, + .operands = .{ .flat = &.{values} }, + .location = location, .verify = false, }); } + +pub fn returns(ctx: *mlir.Context, values: []const *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "func.return", .{ + .operands = .{ .variadic = &.{values} }, + .verify = false, + .location = location, + }); +} diff --git a/mlir/dialects/math.zig b/mlir/dialects/math.zig deleted file mode 100644 index 62ef622c..00000000 --- a/mlir/dialects/math.zig +++ /dev/null @@ -1,36 +0,0 @@ -const std = @import("std"); -const mlir = @import("mlir"); - -const namespace = "math"; - -fn unary_fn(comptime op_name: [:0]const u8) type { - return struct { - pub fn call(ctx: mlir.Context, value: mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, namespace ++ "." ++ op_name, .{ - .operands = &.{value}, - .results = &.{value.getType()}, - .location = location, - }); - } - }; -} - -fn binary_fn(comptime op_name: [:0]const u8) type { - return struct { - pub fn call(ctx: mlir.Context, lhs: mlir.Value, rhs: mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, namespace ++ "." ++ op_name, .{ - .operands = &.{ lhs, rhs }, - .results = &.{}, - .location = location, - }); - } - }; -} - -pub const ipowi = binary_fn("ipowi").call; -pub const fpowi = binary_fn("fpowi").call; -pub const tanh = unary_fn("tanh").call; -pub const sqrt = unary_fn("sqrt").call; -pub const exp = unary_fn("exp").call; -pub const exp2 = unary_fn("exp2").call; -pub const log = unary_fn("log").call; diff --git a/mlir/dialects/scf.zig b/mlir/dialects/scf.zig deleted file mode 100644 index 61fcb189..00000000 --- a/mlir/dialects/scf.zig +++ /dev/null @@ -1,61 +0,0 @@ -const std = @import("std"); - -const mlir = @import("mlir"); - -pub fn ForBody(ExtraArgs: type) type { - return fn (mlir.Context, mlir.Block, ExtraArgs) mlir.Operation; -} - -pub const ForRange = struct { - start: mlir.Value, - end: mlir.Value, - step: mlir.Value, -}; - -pub fn @"for"( - ExtraArgs: type, - ctx: mlir.Context, - range: ForRange, - init_values: []const mlir.Value, - body: ForBody(ExtraArgs), - extra_args: ExtraArgs, - loc: mlir.Location, -) mlir.Operation { - const n_args = init_values.len; - var init_types_buf: [32]mlir.Type = undefined; - var locs_buf: [32]mlir.Location = undefined; - - // The first block argument is the for loop induction variable, - // followed then by all the loop-carried variables. - const init_types = init_types_buf[0 .. n_args + 1]; - const locs = locs_buf[0 .. n_args + 1]; - init_types[0] = range.start.getType(); - locs[0] = loc; - for (1.., init_values) |i, val| { - init_types[i] = val.getType(); - locs[i] = loc; - } - - const block = mlir.Block.init(init_types, locs) catch unreachable; - const yield_op = @call(.auto, body, .{ ctx, block, extra_args }); - std.debug.assert(std.mem.eql(u8, "scf.yield", yield_op.name().str())); - block.appendOperationRecursive(yield_op, .open); - - const for_op = mlir.Operation.make(ctx, "scf.for", .{ - .variadic_operands = &.{ &.{ range.start, range.end, range.step }, init_values }, - .results = init_types[1..], - .blocks = &.{block}, - .location = loc, - .verify = false, - }); - return for_op; -} - -pub fn yield(ctx: mlir.Context, res: []const mlir.Value, loc: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "scf.yield", .{ - .variadic_operands = &.{res}, - .results = &.{}, - .location = loc, - .verify = false, - }); -} diff --git a/mlir/dialects/stablehlo/BUILD.bazel b/mlir/dialects/stablehlo/BUILD.bazel index 45e69239..442e8c7b 100644 --- a/mlir/dialects/stablehlo/BUILD.bazel +++ b/mlir/dialects/stablehlo/BUILD.bazel @@ -6,8 +6,8 @@ zig_library( main = "stablehlo.zig", visibility = ["//mlir/dialects:__subpackages__"], deps = [ - "//mlir", "//mlir:c", + "//mlir:mlir2", "//stdx", "@stablehlo//:stablehlo_dialect_capi", ], diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 0c60fd01..83ea5669 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -1,1325 +1,1248 @@ const std = @import("std"); const c = @import("c"); -const mlir = @import("mlir"); +const mlir = @import("mlir2"); +const stringRef = mlir.stringRef; const stdx = @import("stdx"); -pub const abs = functors.unary_fn("stablehlo.abs").call; -pub const cosine = functors.unary_fn("stablehlo.cosine").call; -pub const sine = functors.unary_fn("stablehlo.sine").call; -pub const exponential = functors.unary_fn("stablehlo.exponential").call; -pub const exponential_minus_one = functors.unary_fn("stablehlo.exponential_minus_one").call; -pub const floor = functors.unary_fn("stablehlo.floor").call; -pub const log = functors.unary_fn("stablehlo.log").call; -pub const log_plus_one = functors.unary_fn("stablehlo.log_plus_one").call; -pub const not = functors.unary_fn("stablehlo.not").call; -pub const negate = functors.unary_fn("stablehlo.negate").call; -pub const sqrt = functors.unary_fn("stablehlo.sqrt").call; -pub const tanh = functors.unary_fn("stablehlo.tanh").call; -pub const cbrt = functors.unary_fn("stablehlo.cbrt").call; -pub const ceil = functors.unary_fn("stablehlo.ceil").call; -pub const rsqrt = functors.unary_fn("stablehlo.rsqrt").call; -pub const count_leading_zeros = functors.unary_fn("stablehlo.count_leading_zeros").call; -pub const is_finite = functors.unary_fn("stablehlo.is_finite").call; -pub const logistic = functors.unary_fn("stablehlo.logistic").call; -pub const popcnt = functors.unary_fn("stablehlo.popcnt").call; -pub const sign = functors.unary_fn("stablehlo.sign").call; -pub const real = functors.unary_fn("stablehlo.real").call; -pub const imag = functors.unary_fn("stablehlo.imag").call; - -pub const add = functors.binary_fn("stablehlo.add").call; -pub const multiply = functors.binary_fn("stablehlo.multiply").call; -pub const divide = functors.binary_fn("stablehlo.divide").call; -pub const subtract = functors.binary_fn("stablehlo.subtract").call; -pub const or_ = functors.binary_fn("stablehlo.or").call; -pub const xor = functors.binary_fn("stablehlo.xor").call; -pub const and_ = functors.binary_fn("stablehlo.and").call; -pub const atan2 = functors.binary_fn("stablehlo.atan2").call; -pub const maximum = functors.binary_fn("stablehlo.maximum").call; -pub const minimum = functors.binary_fn("stablehlo.minimum").call; -pub const power = functors.binary_fn("stablehlo.power").call; -pub const remainder = functors.binary_fn("stablehlo.remainder").call; -pub const shift_left = functors.binary_fn("stablehlo.shift_left").call; -pub const shift_right_arithmetic = functors.binary_fn("stablehlo.shift_right_arithmetic").call; -pub const shift_right_logical = functors.binary_fn("stablehlo.shift_right_logical").call; -pub const complex = functors.binary_fn("stablehlo.complex").call; - -const functors = struct { - fn unary_fn(comptime op_name: [:0]const u8) type { - return struct { - pub fn call(ctx: mlir.Context, value: mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, op_name, .{ - .operands = &.{value}, - .result_type_inference = true, - .location = location, - }); - } - }; - } - - pub fn binary_fn(comptime op_name: [:0]const u8) type { - return struct { - pub fn call(ctx: mlir.Context, lhs: mlir.Value, rhs: mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, op_name, .{ - .operands = &.{ lhs, rhs }, - .result_type_inference = true, - .location = location, - }); - } - }; - } -}; - -pub fn return_(ctx: mlir.Context, value: mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.return", .{ - .variadic_operands = &.{&.{value}}, - .verify = false, - .location = location, - }); -} - -pub fn returns_(ctx: mlir.Context, values: []const mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.return", .{ - .variadic_operands = &.{values}, - .verify = false, - .location = location, - }); -} - -pub fn bitcast_convert(ctx: mlir.Context, value: mlir.Value, result_type: mlir.Type, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.bitcast_convert", .{ - .operands = &.{value}, - .results = &.{result_type}, - .location = location, - }); -} - -pub fn cholesky(ctx: mlir.Context, value: mlir.Value, lower: bool, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.cholesky", .{ - .operands = &.{value}, - .result_type_inference = true, - .attributes = &.{ - .{ "lower", .i1FromBool(ctx, lower) }, - }, - .location = location, - }); -} - -pub fn clamp(ctx: mlir.Context, min: mlir.Value, value: mlir.Value, max: mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.clamp", .{ - .operands = &.{ min, value, max }, - .result_type_inference = true, - .location = location, - }); -} - -pub const DotPrecision = union(enum) { - fast, - high, - highest, - algorithm: DotAlgorithm, - - pub fn precisionAttr(self: DotPrecision, ctx: mlir.Context) mlir.Attribute { - const precision = PrecisionAttribute.init(ctx, switch (self) { - .fast => .DEFAULT, - .high => .HIGH, - .highest => .HIGHEST, - // When we specify the dot algorithm, we should not specify the precision. - .algorithm => .DEFAULT, - }); - return precision.asAttr(); - } - - pub fn algorithmAttr(self: DotPrecision, ctx: mlir.Context, operand_type: mlir.RankedTensorType) ?mlir.Attribute { - return switch (self) { - .algorithm => |algo| algo.asAttr(ctx, operand_type), - else => null, - }; - } -}; - -pub const DotAlgorithm = struct { - accumulation: mlir.FloatTypes, - // Note stablehlo distinguish between left/right component_count - // but all the supported algorithm have the same component_count on both side. - component_count: u8 = 1, - num_primitive_operations: u8 = 1, - allow_imprecise_accumulation: bool = false, - - // bf16_6x: each input is decomposed to 3 bf16 components, then 6 dot operations are done on those components, and the result is accumulated in f32. - // not sure where this is available. - pub const bf16_6x: DotAlgorithm = .{ - .operand = .bf16, - .accumulation = .f32, - .component_count = 1, - .num_primitive_operations = 6, - .allow_imprecise_accumulation = false, - }; - - pub fn asAttr(self: DotAlgorithm, ctx: mlir.Context, tensor_type: mlir.RankedTensorType) mlir.Attribute { - const elem_type = tensor_type.getElementType(); - - return mlir.Attribute.wrap(c.stablehloDotAlgorithmGet( - ctx._inner, - elem_type._inner, - elem_type._inner, - self.accumulation.asType(ctx)._inner, - self.component_count, - self.component_count, - self.num_primitive_operations, - self.allow_imprecise_accumulation, - )); - } -}; - -/// General matrix multiplication "a la Einstein sum" -/// Note: stablehlo doesn't do type inference for dot_general -pub fn dot_general( - ctx: mlir.Context, - lhs: mlir.Value, - rhs: mlir.Value, - result_type: mlir.Type, - location: mlir.Location, - opts: struct { - lhs_batching_dimensions: []const i64, - rhs_batching_dimensions: []const i64, - lhs_contracting_dimensions: []const i64, - rhs_contracting_dimensions: []const i64, - precision: DotPrecision, - }, -) mlir.Operation { - const precisions: [2]mlir.Attribute = @splat(opts.precision.precisionAttr(ctx)); - const attributes = [3]mlir.AttrTuple{ - .{ - "dot_dimension_numbers", DotDimensionNumbersAttribute.init(ctx, .{ - .lhs_batching_dimensions = opts.lhs_batching_dimensions, - .rhs_batching_dimensions = opts.rhs_batching_dimensions, - .lhs_contracting_dimensions = opts.lhs_contracting_dimensions, - .rhs_contracting_dimensions = opts.rhs_contracting_dimensions, - }).asAttr(), - }, - .{ "precision_config", .array(ctx, &precisions) }, - // keep algorithm as the last attribute so we can omit it when it's not set. - .{ "algorithm", opts.precision.algorithmAttr(ctx, lhs.getType().as(mlir.RankedTensorType).?) orelse undefined }, - }; - const n_attributes = if (opts.precision == .algorithm) attributes.len else attributes.len - 1; - return mlir.Operation.make(ctx, "stablehlo.dot_general", .{ - .operands = &.{ lhs, rhs }, - .results = &.{result_type}, - .attributes = attributes[0..n_attributes], - .location = location, - }); -} - -pub fn constant( - ctx: mlir.Context, - dims: []const i64, - elem_type: mlir.DenseElementsAttributeTypes, - raw_bytes: []const u8, - location: mlir.Location, -) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.constant", .{ - .operands = &.{}, - .results = &.{.tensor(dims, elem_type.mlirType(ctx))}, - .attributes = &.{.{ "value", .denseElementsFromBytes(ctx, dims, elem_type, raw_bytes) }}, - .location = location, - }); -} - -pub fn convert(ctx: mlir.Context, value: mlir.Value, result_type: mlir.Type, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.convert", .{ - .operands = &.{value}, - .results = &.{result_type}, - .location = location, - }); -} - -pub fn broadcast_in_dim(ctx: mlir.Context, operand: mlir.Value, dims: []const i64, result_type: mlir.Type, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.broadcast_in_dim", .{ - .operands = &.{operand}, - .results = &.{result_type}, - .attributes = &.{ - .{ "broadcast_dimensions", .dense(ctx, .i64, dims) }, - }, - .location = location, - }); -} - -pub fn transpose(ctx: mlir.Context, value: mlir.Value, result_type: mlir.Type, location: mlir.Location, opts: struct { permutation: []const i64 }) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.transpose", .{ - .operands = &.{value}, - .results = &.{result_type}, - .attributes = &.{ - .{ "permutation", .dense(ctx, .i64, opts.permutation) }, - }, - .location = location, - }); -} - -pub fn slice(ctx: mlir.Context, operand: mlir.Value, start_indices: []const i64, limit_indices: []const i64, strides: []const i64, result_type: mlir.Type, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.slice", .{ - .operands = &.{operand}, - .results = &.{result_type}, - .attributes = &.{ - .{ "start_indices", .dense(ctx, .i64, start_indices) }, - .{ "limit_indices", .dense(ctx, .i64, limit_indices) }, - .{ "strides", .dense(ctx, .i64, strides) }, - }, - .location = location, - }); -} - -pub fn concatenate(ctx: mlir.Context, inputs: []const mlir.Value, dimension: i64, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.concatenate", .{ - .operands = inputs, - .result_type_inference = true, - .attributes = &.{ - .{ "dimension", .int(ctx, .i64, dimension) }, - }, - .location = location, - }); -} - -pub fn reshape(ctx: mlir.Context, value: mlir.Value, result_type: mlir.Type, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.reshape", .{ - .operands = &.{value}, - .results = &.{result_type}, - .location = location, - }); -} - -pub fn select(ctx: mlir.Context, condition: mlir.Value, then: mlir.Value, else_: mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.select", .{ - .operands = &.{ condition, then, else_ }, - .results = &.{then.getType()}, - .location = location, - }); -} - -pub fn gather( - ctx: mlir.Context, - value: mlir.Value, - indices: mlir.Value, - slice_sizes: []const i64, - location: mlir.Location, - args: struct { - offset_dims: []const i64, - collapsed_slice_dims: []const i64, - operand_batching_dims: []const i64, - start_indices_batching_dims: []const i64, - start_index_map: []const i64, - index_vector_dim: i64, - indices_are_sorted: bool = false, - }, -) mlir.Operation { - return mlir.Operation.make( - ctx, - "stablehlo.gather", - .{ - .operands = &.{ value, indices }, - .result_type_inference = true, - .attributes = &.{ - .{ "dimension_numbers", GatherDimensionNumbersAttribute.init( - ctx, - args.offset_dims, - args.collapsed_slice_dims, - args.operand_batching_dims, - args.start_indices_batching_dims, - args.start_index_map, - args.index_vector_dim, - ).asAttr() }, - .{ "slice_sizes", .dense(ctx, .i64, slice_sizes) }, - .{ "indices_are_sorted", .boolean(ctx, args.indices_are_sorted) }, - }, - .location = location, - }, - ); -} - -fn elementTypeOrSelf(typ: mlir.Type) mlir.Type { - return if (typ.as(mlir.ShapedType)) |shaped| { - return shaped.elementType(); - } else typ; -} - -pub const ScatterArgs = struct { - update_window_dims: []const i64, - inserted_window_dims: []const i64, - input_batching_dims: []const i64, - scatter_indices_batching_dims: []const i64, - scatter_dims_to_operand_dims: []const i64, - index_vector_dim: i64, - indices_are_sorted: bool = false, - unique_indices: bool = false, - - pub fn getScatterDimensionNumbers(self: ScatterArgs, ctx: mlir.Context) mlir.Attribute { - return .{ ._inner = c.stablehloScatterDimensionNumbersGet( - ctx._inner, - @intCast(self.update_window_dims.len), - self.update_window_dims.ptr, - @intCast(self.inserted_window_dims.len), - self.inserted_window_dims.ptr, - @intCast(self.input_batching_dims.len), - self.input_batching_dims.ptr, - @intCast(self.scatter_indices_batching_dims.len), - self.scatter_indices_batching_dims.ptr, - @intCast(self.scatter_dims_to_operand_dims.len), - self.scatter_dims_to_operand_dims.ptr, - self.index_vector_dim, - ) }; - } -}; - -pub fn scatter( - ctx: mlir.Context, - inputs: []const mlir.Value, - scatter_indices: []const mlir.Value, - updates: []const mlir.Value, - update_block: mlir.Block, - args: ScatterArgs, - location: mlir.Location, -) mlir.Operation { - return mlir.Operation.make( - ctx, - "stablehlo.scatter", - .{ - .variadic_operands = &.{ inputs, scatter_indices, updates }, - .blocks = &.{update_block}, - .attributes = &.{ - .{ "scatter_dimension_numbers", args.getScatterDimensionNumbers(ctx) }, - .{ "indices_are_sorted", .boolean(ctx, args.indices_are_sorted) }, - .{ "unique_indices", .boolean(ctx, args.unique_indices) }, - }, - .result_type_inference = true, - .location = location, - }, - ); -} - -pub fn iota(ctx: mlir.Context, dimension: i64, result_type: mlir.Type, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.iota", .{ - .operands = &.{}, - .results = &.{result_type}, - .attributes = &.{ - .{ "iota_dimension", .int(ctx, .i64, dimension) }, - }, - .location = location, - }); -} - -pub fn reverse(ctx: mlir.Context, operand: mlir.Value, dimensions: []const i64, location: mlir.Location) mlir.Operation { - const result_type = operand.getType(); - return mlir.Operation.make(ctx, "stablehlo.reverse", .{ - .operands = &.{operand}, - .results = &.{result_type}, - .attributes = &.{ - .{ "dimensions", .dense(ctx, .i64, dimensions) }, - }, - .location = location, - }); -} - -pub fn compare(ctx: mlir.Context, lhs: mlir.Value, rhs: mlir.Value, comparison_direction: ComparisonDirection, compare_type: CompareType, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.compare", .{ - .operands = &.{ lhs, rhs }, - .result_type_inference = true, - .attributes = &.{ - .{ "comparison_direction", comparison_direction.asAttr() }, - .{ "compare_type", compare_type.asAttr() }, - }, - .location = location, - }); -} - -pub fn reduce( - ctx: mlir.Context, - inputs: []const mlir.Value, - init_values: []const mlir.Value, - dimensions: []const i64, - blkctx: anytype, - blkfn: fn (anytype, mlir.Context, []const mlir.Value, []const mlir.Value) mlir.Operation, - location: mlir.Location, -) mlir.Operation { - const MaxBlockArguments = 32; - - const block_n_args = inputs.len + init_values.len; - const locations = ([_]mlir.Location{mlir.Location.unknown(ctx)} ** MaxBlockArguments)[0..block_n_args]; - var reduce_elem_types: [MaxBlockArguments]mlir.Type = undefined; - for (inputs, 0..) |input, i| { - const arg_type: mlir.Type = .tensor(&.{}, elementTypeOrSelf(input.getType())); - reduce_elem_types[i] = arg_type; - reduce_elem_types[inputs.len + i] = arg_type; - } - var block = mlir.Block.open(reduce_elem_types[0..block_n_args], locations) catch unreachable; - { - defer block.close(); - - var block_inputs: [MaxBlockArguments / 2]mlir.Value = undefined; - var block_accs: [MaxBlockArguments / 2]mlir.Value = undefined; - for (0..inputs.len) |i| { - block_inputs[i] = block.argument(i); - block_accs[i] = block.argument(inputs.len + i); - } - _ = blkfn(blkctx, ctx, block_inputs[0..inputs.len], block_accs[0..init_values.len]); - } - - return mlir.Operation.make(ctx, "stablehlo.reduce", .{ - .variadic_operands = &.{ inputs, init_values }, - .result_type_inference = true, - .block = block, - .attributes = &.{ - .{ "dimensions", .dense(ctx, .i64, dimensions) }, - }, - .location = location, - }); -} - -pub fn sort( - ctx: mlir.Context, - inputs: []const mlir.Value, - dimension: i64, - is_stable: bool, - blkctx: anytype, - compfn: fn (anytype, mlir.Context, []const mlir.Value) mlir.Operation, - location: mlir.Location, -) mlir.Operation { - const MaxBlockArguments = 32; - - const locations = ([_]mlir.Location{mlir.Location.unknown(ctx)} ** MaxBlockArguments)[0 .. inputs.len * 2]; - var sort_elem_types: [MaxBlockArguments]mlir.Type = undefined; - for (inputs, 0..) |input, i| { - const arg_type: mlir.Type = .tensor(&.{}, elementTypeOrSelf(input.getType())); - sort_elem_types[i * 2] = arg_type; - sort_elem_types[i * 2 + 1] = arg_type; - } - var block = mlir.Block.init(sort_elem_types[0 .. inputs.len * 2], locations) catch unreachable; - - var block_inputs: [MaxBlockArguments]mlir.Value = undefined; - for (0..inputs.len * 2) |i| { - block_inputs[i] = block.argument(i); - } - _ = compfn(blkctx, ctx, block_inputs[0 .. inputs.len * 2]); - - return mlir.Operation.make(ctx, "stablehlo.sort", .{ - .variadic_operands = &.{inputs}, - .result_type_inference = true, - .block = block, - .attributes = &.{ - .{ "dimension", .int(ctx, .i64, dimension) }, - .{ "is_stable", .boolean(ctx, is_stable) }, - }, - .location = location, - }); -} - -pub fn dynamic_slice(ctx: mlir.Context, operand: mlir.Value, new_dims: []const i64, start_indices: []const mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.dynamic_slice", .{ - .variadic_operands = &.{ &.{operand}, start_indices }, - .result_type_inference = true, - .attributes = &.{ - .{ "slice_sizes", .dense(ctx, .i64, new_dims) }, - }, - .location = location, - }); -} - -pub fn round_nearest_afz(ctx: mlir.Context, value: mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.round_nearest_afz", .{ - .operands = &.{value}, - .result_type_inference = true, - .location = location, - }); -} - -pub fn round_nearest_even(ctx: mlir.Context, value: mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.round_nearest_even", .{ - .operands = &.{value}, - .result_type_inference = true, - .location = location, - }); -} - -pub const PadOpts = struct { - low: []const i64, - high: []const i64, - interior: []const i64, -}; - -pub fn pad(ctx: mlir.Context, value: mlir.Value, padding_value: mlir.Value, opts: PadOpts, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.pad", .{ - .operands = &.{ value, padding_value }, - .result_type_inference = true, - .attributes = &.{ - .{ "edge_padding_low", .dense(ctx, .i64, opts.low) }, - .{ "edge_padding_high", .dense(ctx, .i64, opts.high) }, - .{ "interior_padding", .dense(ctx, .i64, opts.interior) }, - }, - .location = location, - }); -} - -pub const TriangularSolveOpts = struct { - left_side: bool, - lower: bool, - unit_diagonal: bool, - transpose_a: Transpose.Type, -}; - -pub fn triangular_solve(ctx: mlir.Context, value: mlir.Value, other: mlir.Value, location: mlir.Location, opts: TriangularSolveOpts) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.triangular_solve", .{ - .operands = &.{ value, other }, - .result_type_inference = true, - .attributes = &.{ - .{ "left_side", .i1FromBool(ctx, opts.left_side) }, - .{ "lower", .i1FromBool(ctx, opts.lower) }, - .{ "unit_diagonal", .i1FromBool(ctx, opts.unit_diagonal) }, - .{ "transpose_a", Transpose.init(ctx, opts.transpose_a).asAttr() }, - }, - .location = location, - }); -} - -pub const FftOpts = struct { - kind: FftType.Type, - length: []const i64, -}; - -pub fn fft(ctx: mlir.Context, value: mlir.Value, location: mlir.Location, opts: FftOpts) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.fft", .{ - .operands = &.{value}, - .result_type_inference = true, - .attributes = &.{ - .{ "fft_type", FftType.init(ctx, opts.kind).asAttr() }, - .{ "fft_length", .dense(ctx, .i64, opts.length) }, - }, - .location = location, - }); -} - -pub fn rng(ctx: mlir.Context, a: mlir.Value, b: mlir.Value, shape: mlir.Value, rng_distribution: RngDistribution.Type, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.rng", .{ - .operands = &.{ a, b, shape }, - .result_type_inference = true, - .attributes = &.{ - .{ "rng_distribution", RngDistribution.init(ctx, rng_distribution).asAttr() }, - }, - .location = location, - }); -} - -pub fn rng_bit_generator(ctx: mlir.Context, rng_algorithm: RngAlgorithm.Type, initial_state: mlir.Value, res_state_type: mlir.Type, res_type: mlir.Type, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.rng_bit_generator", .{ - .operands = &.{initial_state}, - .results = &.{ res_state_type, res_type }, - .attributes = &.{ - .{ "rng_algorithm", RngAlgorithm.init(ctx, rng_algorithm).asAttr() }, - }, - .location = location, - }); -} - -pub fn reduce_precision(ctx: mlir.Context, value: mlir.Value, exponent_bits: i32, mantissa_bits: i32, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.reduce_precision", .{ - .operands = &.{value}, - .result_type_inference = true, - .attributes = &.{ - .{ "exponent_bits", .int(ctx, .i32, exponent_bits) }, - .{ "mantissa_bits", .int(ctx, .i32, mantissa_bits) }, - }, - .location = location, - }); -} - -pub fn dynamic_update_slice(ctx: mlir.Context, operand: mlir.Value, update: mlir.Value, start_indices: []const mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.dynamic_update_slice", .{ - .variadic_operands = &.{ &.{operand}, &.{update}, start_indices }, - .result_type_inference = true, - .location = location, - }); -} - -pub fn tuple(ctx: mlir.Context, values: []const mlir.Value, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.tuple", .{ - .operands = values, - .result_type_inference = true, - .location = location, - }); -} - -pub fn get_tuple_element(ctx: mlir.Context, tuple_value: mlir.Value, index: i64, location: mlir.Location) mlir.Operation { - return mlir.Operation.make(ctx, "stablehlo.get_tuple_element", .{ - .operands = &.{tuple_value}, - .result_type_inference = true, - .attributes = &.{ - .{ "index", .int(ctx, .i32, index) }, - }, - .location = location, - }); -} - -pub const ConvolutionOpts = struct { - window_strides: []const i64, - pad_value: []const i64, - pad_shape: []const i64 = &.{}, - lhs_dilation: []const i64, - rhs_dilation: []const i64, - window_reversal: []const bool, - input_batch_dimension: i64, - input_feature_dimension: i64, - input_spatial_dimensions: []const i64, - kernel_input_feature_dimension: i64, - kernel_output_feature_dimension: i64, - kernel_spatial_dimensions: []const i64, - output_batch_dimension: i64, - output_feature_dimension: i64, - output_spatial_dimensions: []const i64, - feature_group_count: i64, - batch_group_count: i64, - precision_config: []const PrecisionAttribute.Precision = &.{}, -}; - -pub fn convolution( - ctx: mlir.Context, - lhs: mlir.Value, - rhs: mlir.Value, - opts: ConvolutionOpts, - res_type: mlir.Type, - location: mlir.Location, -) mlir.Operation { - var max_precisions: [2]mlir.Attribute = undefined; - for (opts.precision_config, 0..) |p, i| { - max_precisions[i] = PrecisionAttribute.init(ctx, p).asAttr(); - } - var window_reversal: [3]i32 = undefined; - for (opts.window_reversal, 0..) |w, i| { - window_reversal[i] = @intCast(@intFromBool(w)); - } - return mlir.Operation.make(ctx, "stablehlo.convolution", .{ - .operands = &.{ lhs, rhs }, - .results = &.{res_type}, - .attributes = &.{ - .{ "window_strides", .dense(ctx, .i64, opts.window_strides) }, - .{ "padding", .denseElements(ctx, opts.pad_shape, .i64, opts.pad_value) }, - .{ "lhs_dilation", .dense(ctx, .i64, opts.lhs_dilation) }, - .{ "rhs_dilation", .dense(ctx, .i64, opts.rhs_dilation) }, - .{ "window_reversal", .dense(ctx, .bool, window_reversal[0..opts.window_reversal.len]) }, - .{ - "dimension_numbers", ConvDimensionNumbersAttribute.init(ctx, .{ - .input_batch_dimension = opts.input_batch_dimension, - .input_feature_dimension = opts.input_feature_dimension, - .input_spatial_dimensions = opts.input_spatial_dimensions, - .kernel_input_feature_dimension = opts.kernel_input_feature_dimension, - .kernel_output_feature_dimension = opts.kernel_output_feature_dimension, - .kernel_spatial_dimensions = opts.kernel_spatial_dimensions, - .output_batch_dimension = opts.output_batch_dimension, - .output_feature_dimension = opts.output_feature_dimension, - .output_spatial_dimensions = opts.output_spatial_dimensions, - }).asAttr(), - }, - .{ "feature_group_count", .int(ctx, .i64, opts.feature_group_count) }, - .{ "batch_group_count", .int(ctx, .i64, opts.batch_group_count) }, - .{ "precision_config", .array(ctx, &max_precisions) }, - }, - .location = location, - }); -} - -pub const CustomCallOpts = struct { - pub const ApiVersion = enum(i32) { - original = 1, - status_returning = 2, - status_returning_unified = 3, - typed_ffi = 4, - }; - - call_target_name: [:0]const u8, - has_side_effect: bool, - backend_config: ?mlir.Attribute, - operand_layouts: ?[]const []const usize = null, - result_layouts: ?[]const []const usize = null, - output_operand_aliases: []const i64 = &.{}, - additional_attributes: []const mlir.AttrTuple = &.{}, - api_version: ApiVersion, -}; - -pub fn custom_call(ctx: mlir.Context, inputs: []const mlir.Value, opts: CustomCallOpts, res_types: []const mlir.Type, location: mlir.Location) mlir.Operation { - const MAX_OPERANDS = 64; - const MAX_RESULTS = 16; - - const backend_config = opts.backend_config orelse mlir.Attribute.string(ctx, ""); - if (@intFromEnum(opts.api_version) < @intFromEnum(CustomCallOpts.ApiVersion.typed_ffi)) { - stdx.debug.assert( - backend_config.isA(mlir.StringAttribute), - "API version < 4 requires a string as backend_config, got {}", - .{backend_config}, - ); - } else { - stdx.debug.assert( - backend_config.isA(mlir.DictionaryAttribute), - "API version >= 4 requires a dictionary as backend_config, got {}", - .{backend_config}, - ); - } - - var attrs: stdx.BoundedArray(mlir.AttrTuple, 32) = .{}; - attrs.appendSliceAssumeCapacity(&[_]mlir.AttrTuple{ - .{ "api_version", .int(ctx, .i32, @intFromEnum(opts.api_version)) }, - .{ "call_target_name", .string(ctx, opts.call_target_name) }, - .{ "has_side_effect", .boolean(ctx, opts.has_side_effect) }, - .{ "backend_config", backend_config }, - }); - - { - var output_operand_aliases: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; - for (opts.output_operand_aliases) |alias| { - output_operand_aliases.appendAssumeCapacity( - OutputOperandAliasAttribute.init(ctx, &.{}, alias, &.{}).asAttr(), - ); - } - attrs.appendAssumeCapacity(.{ "output_operand_aliases", .array(ctx, output_operand_aliases.constSlice()) }); - } - - const MINOR_TO_MAJOR = blk: { - const MAX_RANK = 8; - var ret: [MAX_RANK]usize = undefined; - for (0..MAX_RANK) |i| { - ret[i] = @intCast(MAX_RANK - i - 1); - } - break :blk ret; - }; - - if (opts.operand_layouts) |layouts| { - var operand_layouts: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; - for (layouts) |ol| { - operand_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); - } - attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); - } else { - const operand_layouts = blk: { - var ret: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; - for (inputs) |input| { - const ranked_type = input.getType().as(mlir.RankedTensorType).?; - const ol = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_type.getRank() ..]; - ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); - } - break :blk ret; - }; - attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); - } - - if (opts.result_layouts) |layouts| { - var result_layouts: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; - for (layouts) |rl| { - result_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); - } - attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); - } else { - const result_layouts = blk: { - var ret: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; - for (res_types) |t| { - const ranked_t = t.as(mlir.RankedTensorType).?; - const rl = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_t.getRank() ..]; - ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); - } - break :blk ret; - }; - attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); - } - - attrs.appendSlice(opts.additional_attributes) catch @panic("Too many additional_attributes"); - - return mlir.Operation.make(ctx, "stablehlo.custom_call", .{ - .operands = inputs, - .results = res_types, - .attributes = attrs.constSlice(), - .location = location, - }); -} - -pub const DotDimensionNumbersAttribute = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsADotDimensionNumbers; - const Self = DotDimensionNumbersAttribute; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub fn init(ctx: mlir.Context, args: struct { - lhs_batching_dimensions: []const i64, - rhs_batching_dimensions: []const i64, - lhs_contracting_dimensions: []const i64, - rhs_contracting_dimensions: []const i64, - }) Self { - return .{ - ._inner = c.stablehloDotDimensionNumbersGet( - ctx._inner, - @intCast(args.lhs_batching_dimensions.len), - args.lhs_batching_dimensions.ptr, - @intCast(args.rhs_batching_dimensions.len), - args.rhs_batching_dimensions.ptr, - @intCast(args.lhs_contracting_dimensions.len), - args.lhs_contracting_dimensions.ptr, - @intCast(args.rhs_contracting_dimensions.len), - args.rhs_contracting_dimensions.ptr, - ), - }; - } - - pub fn getLhsBatchingDimensionsSize(self: Self) usize { - return @intCast(c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsSize(self._inner)); - } - - pub fn getLhsBatchingDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsElem(self._inner, @intCast(pos)); - } - - pub fn getRhsBatchingDimensionsSize(self: Self) usize { - return @intCast(c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsSize(self._inner)); - } - - pub fn getRhsBatchingDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsElem(self._inner, @intCast(pos)); - } - - pub fn getLhsContractingDimensionsSize(self: Self) usize { - return @intCast(c.stablehloDotDimensionNumbersGetLhsContractingDimensionsSize(self._inner)); - } - - pub fn getLhsContractingDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloDotDimensionNumbersGetLhsContractingDimensionsElem(self._inner, @intCast(pos)); - } - - pub fn getRhsContractingDimensionsSize(self: Self) usize { - return @intCast(c.stablehloDotDimensionNumbersGetRhsContractingDimensionsSize(self._inner)); - } - - pub fn getRhsContractingDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloDotDimensionNumbersGetRhsContractingDimensionsElem(self._inner, @intCast(pos)); - } -}; - -pub const GatherDimensionNumbersAttribute = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsAGatherDimensionNumbers; - const Self = GatherDimensionNumbersAttribute; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub fn init( - ctx: mlir.Context, - offset_dims: []const i64, - collapsed_slice_dims: []const i64, - operand_batching_dims: []const i64, - start_indices_batching_dims: []const i64, - start_index_map: []const i64, - index_vector_dim: i64, - ) Self { - return .{ - ._inner = c.stablehloGatherDimensionNumbersGet( - ctx._inner, - @intCast(offset_dims.len), - offset_dims.ptr, - @intCast(collapsed_slice_dims.len), - collapsed_slice_dims.ptr, - @intCast(operand_batching_dims.len), - operand_batching_dims.ptr, - @intCast(start_indices_batching_dims.len), - start_indices_batching_dims.ptr, - @intCast(start_index_map.len), - start_index_map.ptr, - index_vector_dim, - ), - }; - } - - pub fn getOffsetDimsSize(self: Self) usize { - return @intCast(c.stablehloGatherDimensionNumbersGetOffsetDimsSize(self._inner)); - } - - pub fn getOffsetDimsElem(self: Self, pos: usize) i64 { - return c.stablehloGatherDimensionNumbersGetOffsetDimsElem(self._inner, @intCast(pos)); - } - - pub fn getCollapsedSliceDimsSize(self: Self) usize { - return @intCast(c.stablehloGatherDimensionNumbersGetCollapsedSliceDimsSize(self._inner)); - } - - pub fn getCollapsedSliceDimsElem(self: Self, pos: usize) i64 { - return c.stablehloGatherDimensionNumbersGetCollapsedSliceDimsElem(self._inner, @intCast(pos)); - } - - pub fn getStartIndexMapSize(self: Self) usize { - return @intCast(c.stablehloGatherDimensionNumbersGetStartIndexMapSize(self._inner)); - } - - pub fn getOperandBatchingDimsSize(self: Self) usize { - return @intCast(c.stablehloGatherDimensionNumbersGetOperandBatchingDimsSize(self._inner)); - } - - pub fn getOperandBatchingDimsElem(self: Self, pos: usize) i64 { - return c.stablehloGatherDimensionNumbersGetOperandBatchingDimsElem(self._inner, @intCast(pos)); - } - - pub fn getStartIndicesBatchingDimsSize(self: Self) usize { - return @intCast(c.stablehloGatherDimensionNumbersGetStartIndicesBatchingDimsSize(self._inner)); - } - - pub fn getStartIndicesBatchingDimsElem(self: Self, pos: usize) i64 { - return c.stablehloGatherDimensionNumbersGetStartIndicesBatchingDimsElem(self._inner, @intCast(pos)); - } - - pub fn getStartIndexMapElem(self: Self, pos: usize) i64 { - return c.stablehloGatherDimensionNumbersGetStartIndexMapElem(self._inner, @intCast(pos)); - } - - pub fn getIndexVectorDim(self: Self) usize { - return @intCast(c.stablehloGatherDimensionNumbersGetIndexVectorDim(self._inner)); - } -}; - -pub const ConvDimensionNumbersAttribute = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsAConvDimensionNumbers; - const Self = ConvDimensionNumbersAttribute; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub fn init(ctx: mlir.Context, args: struct { - input_batch_dimension: i64, - input_feature_dimension: i64, - input_spatial_dimensions: []const i64, - kernel_input_feature_dimension: i64, - kernel_output_feature_dimension: i64, - kernel_spatial_dimensions: []const i64, - output_batch_dimension: i64, - output_feature_dimension: i64, - output_spatial_dimensions: []const i64, - }) Self { - return .{ - ._inner = c.stablehloConvDimensionNumbersGet( - ctx._inner, - args.input_batch_dimension, - args.input_feature_dimension, - @intCast(args.input_spatial_dimensions.len), - args.input_spatial_dimensions.ptr, - args.kernel_input_feature_dimension, - args.kernel_output_feature_dimension, - @intCast(args.kernel_spatial_dimensions.len), - args.kernel_spatial_dimensions.ptr, - args.output_batch_dimension, - args.output_feature_dimension, - @intCast(args.output_spatial_dimensions.len), - args.output_spatial_dimensions.ptr, - ), - }; - } - - pub fn getInputBatchDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetInputBatchDimension(self._inner); - } - - pub fn getInputFeatureDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetInputFeatureDimension(self._inner); - } - - pub fn getInputSpatialDimensionsSize(self: Self) usize { - return @intCast(c.stablehloConvDimensionNumbersGetInputSpatialDimensionsSize(self._inner)); - } - - pub fn getInputSpatialDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloConvDimensionNumbersGetInputSpatialDimensionsElem(self._inner, @intCast(pos)); - } - - pub fn getKernelInputFeatureDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetKernelInputFeatureDimension(self._inner); - } - - pub fn getKernelOutputFeatureDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetKernelOutputFeatureDimension(self._inner); - } - - pub fn getKernelSpatialDimensionsSize(self: Self) usize { - return @intCast(c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsSize(self._inner)); - } - - pub fn getKernelSpatialDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsElem(self._inner, @intCast(pos)); - } - - pub fn getOutputBatchDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetOutputBatchDimension(self._inner); - } - - pub fn getOutputFeatureDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetOutputFeatureDimension(self._inner); - } - - pub fn getOutputSpatialDimensionsSize(self: Self) usize { - return @intCast(c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsSize(self._inner)); - } - - pub fn getOutputSpatialDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsElem(self._inner, @intCast(pos)); - } -}; - -pub const OutputOperandAliasAttribute = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsAOutputOperandAlias; - pub const asAttr = mlir.Attribute.fromAny(OutputOperandAliasAttribute); - pub const eql = mlir.Attribute.eqlAny(OutputOperandAliasAttribute); - - pub fn init( - ctx: mlir.Context, - output_tuple_indices: []const i64, - operand_index: i64, - operand_tuple_indices: []const i64, - ) OutputOperandAliasAttribute { - return .{ ._inner = c.stablehloOutputOperandAliasGet( - ctx._inner, - @intCast(output_tuple_indices.len), - output_tuple_indices.ptr, - @intCast(operand_index), - @intCast(operand_tuple_indices.len), - operand_tuple_indices.ptr, - ) }; - } -}; - -pub const PrecisionAttribute = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsAPrecisionAttr; - const Self = PrecisionAttribute; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub const Precision = enum { - DEFAULT, - HIGH, - HIGHEST, - }; - - pub fn init(ctx: mlir.Context, value: Precision) Self { - return .{ ._inner = c.stablehloPrecisionAttrGet(ctx._inner, mlir.stringRef(@tagName(value))) }; - } - - pub fn getValue(self: Self) Precision { - const value = mlir.fromStringRef(c.stablehloPrecisionAttrGetValue(self._inner)); - return std.meta.stringToEnum(Precision, value) orelse unreachable; - } -}; - -pub const ComparisonDirection = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsAComparisonDirectionAttr; - const Self = ComparisonDirection; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub const Direction = enum { - EQ, - NE, - GE, - GT, - LE, - LT, - }; - - pub fn init(ctx: mlir.Context, value: Direction) Self { - return .{ ._inner = c.stablehloComparisonDirectionAttrGet(ctx._inner, mlir.stringRef(@tagName(value))) }; - } - - pub fn getValue(self: Self) Direction { - const value = mlir.fromStringRef(c.stablehloComparisonDirectionAttrGetValue(self._inner)); - return std.meta.stringToEnum(Direction, value) orelse unreachable; - } -}; - -pub const CompareType = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsAComparisonTypeAttr; - const Self = CompareType; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub const Type = enum { - SIGNED, - UNSIGNED, - FLOAT, - TOTALORDER, - }; - - pub fn init(ctx: mlir.Context, value: Type) Self { - return .{ ._inner = c.stablehloComparisonTypeAttrGet(ctx._inner, mlir.stringRef(@tagName(value))) }; - } - - pub fn getValue(self: Self) Type { - const value = mlir.fromStringRef(c.stablehloComparisonTypeAttrGetValue(self._inner)); - return std.meta.stringToEnum(Type, value) orelse unreachable; - } -}; - -pub const Transpose = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsATransposeAttr; - const Self = Transpose; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub const Type = enum { - NO_TRANSPOSE, - TRANSPOSE, - ADJOINT, - }; - - pub fn init(ctx: mlir.Context, value: Type) Self { - return .{ ._inner = c.stablehloTransposeAttrGet(ctx._inner, mlir.stringRef(@tagName(value))) }; - } - - pub fn getValue(self: Self) Type { - const value = mlir.fromStringRef(c.stablehloTransposeAttrGetValue(self._inner)); - return std.meta.stringToEnum(Type, value) orelse unreachable; - } -}; - -pub const FftType = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsAFftTypeAttr; - const Self = FftType; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub const Type = enum { - FFT, - IFFT, - RFFT, - IRFFT, - }; - - pub fn init(ctx: mlir.Context, value: Type) Self { - return .{ ._inner = c.stablehloFftTypeAttrGet(ctx._inner, mlir.stringRef(@tagName(value))) }; - } - - pub fn getValue(self: Self) Type { - const value = mlir.fromStringRef(c.stablehloFftTypeAttrGetValue(self._inner)); - return std.meta.stringToEnum(Type, value) orelse unreachable; - } -}; - -pub const RngDistribution = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsARngDistributionAttr; - const Self = RngDistribution; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub const Type = enum { - UNIFORM, - NORMAL, - }; - - pub fn init(ctx: mlir.Context, value: Type) Self { - return .{ ._inner = c.stablehloRngDistributionAttrGet(ctx._inner, mlir.stringRef(@tagName(value))) }; - } - - pub fn getValue(self: Self) Type { - const value = mlir.fromStringRef(c.stablehloRngDistributionAttrGetValue(self._inner)); - return std.meta.stringToEnum(Type, value) orelse unreachable; - } -}; - -pub const RngAlgorithm = struct { - _inner: c.MlirAttribute, - - pub const is_a_fn = c.stablehloAttributeIsARngAlgorithmAttr; - const Self = RngAlgorithm; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); - - pub const Type = enum { - DEFAULT, - THREE_FRY, - PHILOX, - }; - - pub fn init(ctx: mlir.Context, value: Type) Self { - return .{ ._inner = c.stablehloRngAlgorithmAttrGet(ctx._inner, mlir.stringRef(@tagName(value))) }; - } - - pub fn getValue(self: Self) Type { - const value = mlir.fromStringRef(c.stablehloRngAlgorithmAttrGetValue(self._inner)); - return std.meta.stringToEnum(Type, value) orelse unreachable; - } -}; - -pub fn stablehloVersionFromCompatibilityRequirement(requirement: c.MlirStablehloCompatibilityRequirement) []const u8 { - const state = struct { - var buf: [32]u8 = undefined; - - fn call(req: c.MlirStablehloCompatibilityRequirement) []u8 { - var stream = std.io.fixedBufferStream(&buf); - var context = .{ .writer = stream.writer() }; - const WriterContext = @TypeOf(context); - - c.stablehloVersionFromCompatibilityRequirement(req, (struct { - pub fn callback(mlir_str: c.MlirStringRef, userdata: ?*anyopaque) callconv(.c) void { - const inner_ctx: *WriterContext = @ptrCast(@alignCast(userdata)); - _ = inner_ctx.writer.write(mlir.fromStringRef(mlir_str)) catch unreachable; - } - }).callback, &context); - - return buf[0..stream.pos]; +pub const abs = unaryOp("stablehlo.abs").call; +pub const cosine = unaryOp("stablehlo.cosine").call; +pub const sine = unaryOp("stablehlo.sine").call; +pub const exponential = unaryOp("stablehlo.exponential").call; +pub const exponential_minus_one = unaryOp("stablehlo.exponential_minus_one").call; +pub const floor = unaryOp("stablehlo.floor").call; +pub const log = unaryOp("stablehlo.log").call; +pub const log_plus_one = unaryOp("stablehlo.log_plus_one").call; +pub const not = unaryOp("stablehlo.not").call; +pub const negate = unaryOp("stablehlo.negate").call; +pub const sqrt = unaryOp("stablehlo.sqrt").call; +pub const tanh = unaryOp("stablehlo.tanh").call; +pub const cbrt = unaryOp("stablehlo.cbrt").call; +pub const ceil = unaryOp("stablehlo.ceil").call; +pub const rsqrt = unaryOp("stablehlo.rsqrt").call; +pub const count_leading_zeros = unaryOp("stablehlo.count_leading_zeros").call; +pub const is_finite = unaryOp("stablehlo.is_finite").call; +pub const logistic = unaryOp("stablehlo.logistic").call; +pub const popcnt = unaryOp("stablehlo.popcnt").call; +pub const sign = unaryOp("stablehlo.sign").call; +pub const real = unaryOp("stablehlo.real").call; +pub const imag = unaryOp("stablehlo.imag").call; +pub const round_nearest_afz = unaryOp("stablehlo.round_nearest_afz").call; +pub const round_nearest_even = unaryOp("stablehlo.round_nearest_even").call; +pub const tuple = unaryOp("stablehlo.tuple").call; + +pub const add = binaryOp("stablehlo.add").call; +pub const multiply = binaryOp("stablehlo.multiply").call; +pub const divide = binaryOp("stablehlo.divide").call; +pub const subtract = binaryOp("stablehlo.subtract").call; +pub const or_ = binaryOp("stablehlo.or").call; +pub const xor = binaryOp("stablehlo.xor").call; +pub const and_ = binaryOp("stablehlo.and").call; +pub const atan2 = binaryOp("stablehlo.atan2").call; +pub const maximum = binaryOp("stablehlo.maximum").call; +pub const minimum = binaryOp("stablehlo.minimum").call; +pub const power = binaryOp("stablehlo.power").call; +pub const remainder = binaryOp("stablehlo.remainder").call; +pub const shift_left = binaryOp("stablehlo.shift_left").call; +pub const shift_right_arithmetic = binaryOp("stablehlo.shift_right_arithmetic").call; +pub const shift_right_logical = binaryOp("stablehlo.shift_right_logical").call; +pub const complex = binaryOp("stablehlo.complex").call; + +pub const bitcast_convert = castOp("stablehlo.bitcast_convert").call; +pub const convert = castOp("stablehlo.convert").call; +pub const reshape = castOp("stablehlo.reshape").call; + +fn castOp(comptime op_name: []const u8) type { + return struct { + pub fn call(ctx: *mlir.Context, value: *const mlir.Value, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, op_name, .{ + .operands = .{ .flat = &.{value} }, + .results = .{ .flat = &.{result_type} }, + .location = location, + }); } }; - - return state.call(requirement); } -pub fn stablehloGetSmallerVersion(version1: []const u8, version2: []const u8) []const u8 { +fn unaryOp(comptime op_name: []const u8) type { + return struct { + pub fn call(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, op_name, .{ + .operands = .{ .flat = &.{value} }, + .result_type_inference = true, + .location = location, + }); + } + }; +} + +pub fn binaryOp(comptime op_name: []const u8) type { + return struct { + pub fn call(ctx: *mlir.Context, lhs: *const mlir.Value, rhs: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, op_name, .{ + .operands = .{ .flat = &.{ lhs, rhs } }, + .result_type_inference = true, + .location = location, + }); + } + }; +} + +// pub fn cholesky(ctx: *mlir.Context, value: *const mlir.Value, lower: bool, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.cholesky", .{ +// .operands = .{ .flat = &.{value} }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "lower", mlir.integerAttribute(ctx, .i1, @intFromBool(lower)) }, +// }, +// .location = location, +// }); +// } + +// pub fn clamp(ctx: *mlir.Context, min: *const mlir.Value, value: *const mlir.Value, max: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.clamp", .{ +// .operands = .{ .flat = &.{ min, value, max } }, +// .result_type_inference = true, +// .location = location, +// }); +// } + +// pub const DotPrecision = union(enum) { +// fast, +// high, +// highest, +// algorithm: DotAlgorithm, + +// pub fn precisionAttr(self: DotPrecision, ctx: *mlir.Context) *const mlir.Attribute { +// const precision = PrecisionAttribute.init(ctx, switch (self) { +// .fast => .DEFAULT, +// .high => .HIGH, +// .highest => .HIGHEST, +// // When we specify the dot algorithm, we should not specify the precision. +// .algorithm => .DEFAULT, +// }); +// return precision.asAttr(); +// } + +// pub fn algorithmAttr(self: DotPrecision, ctx: *mlir.Context, operand_type: mlir.RankedTensorType) ?mlir.Attribute { +// return switch (self) { +// .algorithm => |algo| algo.asAttr(ctx, operand_type), +// else => null, +// }; +// } +// }; + +// pub const DotAlgorithm = struct { +// accumulation: mlir.FloatTypes, +// // Note stablehlo distinguish between left/right component_count +// // but all the supported algorithm have the same component_count on both side. +// component_count: u8 = 1, +// num_primitive_operations: u8 = 1, +// allow_imprecise_accumulation: bool = false, + +// // bf16_6x: each input is decomposed to 3 bf16 components, then 6 dot operations are done on those components, and the result is accumulated in f32. +// // not sure where this is available. +// pub const bf16_6x: DotAlgorithm = .{ +// .operand = .bf16, +// .accumulation = .f32, +// .component_count = 1, +// .num_primitive_operations = 6, +// .allow_imprecise_accumulation = false, +// }; + +// pub fn asAttr(self: DotAlgorithm, ctx: *mlir.Context, tensor_type: mlir.RankedTensorType) *const mlir.Attribute { +// const elem_type = tensor_type.getElementType(); + +// return mlir.Attribute.wrap(c.stablehloDotAlgorithmGet( +// ctx._inner, +// elem_type._inner, +// elem_type._inner, +// self.accumulation.asType(ctx)._inner, +// self.component_count, +// self.component_count, +// self.num_primitive_operations, +// self.allow_imprecise_accumulation, +// )); +// } +// }; + +// /// General matrix multiplication "a la Einstein sum" +// /// Note: stablehlo doesn't do type inference for dot_general +// pub fn dot_general( +// ctx: *mlir.Context, +// lhs: *const mlir.Value, +// rhs: *const mlir.Value, +// result_type: *const mlir.Type, +// location: *const mlir.Location, +// opts: struct { +// lhs_batching_dimensions: []const i64, +// rhs_batching_dimensions: []const i64, +// lhs_contracting_dimensions: []const i64, +// rhs_contracting_dimensions: []const i64, +// precision: DotPrecision, +// }, +// ) *mlir.Operation { +// const precisions: [2]mlir.Attribute = @splat(opts.precision.precisionAttr(ctx)); +// const attributes = [3]mlir.AttrTuple{ +// .{ +// "dot_dimension_numbers", DotDimensionNumbersAttribute.init(ctx, .{ +// .lhs_batching_dimensions = opts.lhs_batching_dimensions, +// .rhs_batching_dimensions = opts.rhs_batching_dimensions, +// .lhs_contracting_dimensions = opts.lhs_contracting_dimensions, +// .rhs_contracting_dimensions = opts.rhs_contracting_dimensions, +// }).asAttr(), +// }, +// .{ "precision_config", .array(ctx, &precisions) }, +// // keep algorithm as the last attribute so we can omit it when it's not set. +// .{ "algorithm", opts.precision.algorithmAttr(ctx, lhs.getType().as(mlir.RankedTensorType).?) orelse undefined }, +// }; +// const n_attributes = if (opts.precision == .algorithm) attributes.len else attributes.len - 1; +// return mlir.Operation.make(ctx, "stablehlo.dot_general", .{ +// .operands = .{ .flat = &.{ lhs, rhs } }, +// .results = .{ .flat = &.{result_type} }, +// .attributes = attributes[0..n_attributes], +// .location = location, +// }); +// } + +// pub fn constant( +// ctx: *mlir.Context, +// dims: []const i64, +// elem_type: mlir.DenseElementsAttributeTypes, +// raw_bytes: []const u8, +// location: *const mlir.Location, +// ) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.constant", .{ +// .operands = .{ .flat = &.{} }, +// .results = &.{.tensor(dims, elem_type.mlirType(ctx))}, +// .attributes = &.{ +// .{ "value", .denseElementsFromBytes(ctx, dims, elem_type, raw_bytes) }, +// }, +// .location = location, +// }); +// } + +// pub fn broadcast_in_dim(ctx: *mlir.Context, operand: *const mlir.Value, dims: []const i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.broadcast_in_dim", .{ +// .operands = .{ .flat = &.{operand} }, +// .results = .{ .flat = &.{result_type} }, +// .attributes = &.{ +// .{ "broadcast_dimensions", mlir.denseArrayAttribute(ctx, .i64, dims) }, +// }, +// .location = location, +// }); +// } + +// pub fn transpose(ctx: *mlir.Context, value: *const mlir.Value, result_type: *const mlir.Type, location: *const mlir.Location, opts: struct { permutation: []const i64 }) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.transpose", .{ +// .operands = .{ .flat = &.{value} }, +// .results = .{ .flat = &.{result_type} }, +// .attributes = &.{ +// .{ "permutation", mlir.denseArrayAttribute(ctx, .i64, opts.permutation) }, +// }, +// .location = location, +// }); +// } + +// pub fn slice(ctx: *mlir.Context, operand: *const mlir.Value, start_indices: []const i64, limit_indices: []const i64, strides: []const i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.slice", .{ +// .operands = .{ .flat = &.{operand} }, +// .results = .{ .flat = &.{result_type} }, +// .attributes = &.{ +// .{ "start_indices", mlir.denseArrayAttribute(ctx, .i64, start_indices) }, +// .{ "limit_indices", mlir.denseArrayAttribute(ctx, .i64, limit_indices) }, +// .{ "strides", mlir.denseArrayAttribute(ctx, .i64, strides) }, +// }, +// .location = location, +// }); +// } + +// pub fn concatenate(ctx: *mlir.Context, inputs: []const *const mlir.Value, dimension: i64, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.concatenate", .{ +// .operands = inputs, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "dimension", mlir.integerAttribute(ctx, .i64, dimension) }, +// }, +// .location = location, +// }); +// } + +// pub fn select(ctx: *mlir.Context, condition: *const mlir.Value, then: *const mlir.Value, else_: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.select", .{ +// .operands = .{ .flat = &.{ condition, then, else_ } }, +// .results = &.{then.getType()}, +// .location = location, +// }); +// } + +// pub fn gather( +// ctx: *mlir.Context, +// value: *const mlir.Value, +// indices: *const mlir.Value, +// slice_sizes: []const i64, +// location: *const mlir.Location, +// args: struct { +// offset_dims: []const i64, +// collapsed_slice_dims: []const i64, +// operand_batching_dims: []const i64, +// start_indices_batching_dims: []const i64, +// start_index_map: []const i64, +// index_vector_dim: i64, +// indices_are_sorted: bool = false, +// }, +// ) *mlir.Operation { +// return mlir.Operation.make( +// ctx, +// "stablehlo.gather", +// .{ +// .operands = .{ .flat = &.{ value, indices } }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "dimension_numbers", GatherDimensionNumbersAttribute.init( +// ctx, +// args.offset_dims, +// args.collapsed_slice_dims, +// args.operand_batching_dims, +// args.start_indices_batching_dims, +// args.start_index_map, +// args.index_vector_dim, +// ).asAttr() }, +// .{ "slice_sizes", .dense(ctx, .i64, slice_sizes) }, +// .{ "indices_are_sorted", .boolean(ctx, args.indices_are_sorted) }, +// }, +// .location = location, +// }, +// ); +// } + +// fn elementTypeOrSelf(typ: *const mlir.Type) *const mlir.Type { +// return if (typ.as(mlir.ShapedType)) |shaped| { +// return shaped.elementType(); +// } else typ; +// } + +// pub const ScatterArgs = struct { +// update_window_dims: []const i64, +// inserted_window_dims: []const i64, +// input_batching_dims: []const i64, +// scatter_indices_batching_dims: []const i64, +// scatter_dims_to_operand_dims: []const i64, +// index_vector_dim: i64, +// indices_are_sorted: bool = false, +// unique_indices: bool = false, + +// pub fn getScatterDimensionNumbers(self: ScatterArgs, ctx: *mlir.Context) *const mlir.Attribute { +// return .{ ._inner = c.stablehloScatterDimensionNumbersGet( +// ctx._inner, +// @intCast(self.update_window_dims.len), +// self.update_window_dims.ptr, +// @intCast(self.inserted_window_dims.len), +// self.inserted_window_dims.ptr, +// @intCast(self.input_batching_dims.len), +// self.input_batching_dims.ptr, +// @intCast(self.scatter_indices_batching_dims.len), +// self.scatter_indices_batching_dims.ptr, +// @intCast(self.scatter_dims_to_operand_dims.len), +// self.scatter_dims_to_operand_dims.ptr, +// self.index_vector_dim, +// ) }; +// } +// }; + +// pub fn scatter( +// ctx: *mlir.Context, +// inputs: []const *const mlir.Value, +// scatter_indices: []const *const mlir.Value, +// updates: []const *const mlir.Value, +// update_block: mlir.Block, +// args: ScatterArgs, +// location: *const mlir.Location, +// ) *mlir.Operation { +// return mlir.Operation.make( +// ctx, +// "stablehlo.scatter", +// .{ +// .operands = .{ .variadic = &.{ inputs, scatter_indices, updates } }, +// .blocks = &.{update_block}, +// .attributes = &.{ +// .{ "scatter_dimension_numbers", args.getScatterDimensionNumbers(ctx) }, +// .{ "indices_are_sorted", .boolean(ctx, args.indices_are_sorted) }, +// .{ "unique_indices", .boolean(ctx, args.unique_indices) }, +// }, +// .result_type_inference = true, +// .location = location, +// }, +// ); +// } + +// pub fn iota(ctx: *mlir.Context, dimension: i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.iota", .{ +// .operands = .{ .flat = &.{} }, +// .results = .{ .flat = &.{result_type} }, +// .attributes = &.{ +// .{ "iota_dimension", mlir.integerType(ctx, .i64, dimension) }, +// }, +// .location = location, +// }); +// } + +// pub fn reverse(ctx: *mlir.Context, operand: *const mlir.Value, dimensions: []const i64, location: *const mlir.Location) *mlir.Operation { +// const result_type = operand.getType(); +// return mlir.Operation.make(ctx, "stablehlo.reverse", .{ +// .operands = .{ .flat = &.{operand} }, +// .results = .{ .flat = &.{result_type} }, +// .attributes = &.{ +// .{ "dimensions", mlir.denseArrayAttribute(ctx, .i64, dimensions) }, +// }, +// .location = location, +// }); +// } + +// pub fn compare(ctx: *mlir.Context, lhs: *const mlir.Value, rhs: *const mlir.Value, comparison_direction: ComparisonDirection, compare_type: CompareType, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.compare", .{ +// .operands = .{ .flat = &.{ lhs, rhs } }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "comparison_direction", comparison_direction.asAttr() }, +// .{ "compare_type", compare_type.asAttr() }, +// }, +// .location = location, +// }); +// } + +// pub fn reduce( +// ctx: *mlir.Context, +// inputs: []const *const mlir.Value, +// init_values: []const *const mlir.Value, +// dimensions: []const i64, +// blkctx: anytype, +// blkfn: fn (anytype, mlir.Context, []const *const mlir.Value, []const *const mlir.Value) mlir.Operation, +// location: *const mlir.Location, +// ) *mlir.Operation { +// const MaxBlockArguments = 32; + +// const block_n_args = inputs.len + init_values.len; +// const locations = ([_]mlir.Location{mlir.Location.unknown(ctx)} ** MaxBlockArguments)[0..block_n_args]; +// var reduce_elem_types: [MaxBlockArguments]*const mlir.Type = undefined; +// for (inputs, 0..) |input, i| { +// const arg_type: *const mlir.Type = .tensor(&.{}, elementTypeOrSelf(input.getType())); +// reduce_elem_types[i] = arg_type; +// reduce_elem_types[inputs.len + i] = arg_type; +// } +// var block = mlir.Block.open(reduce_elem_types[0..block_n_args], locations) catch unreachable; +// { +// defer block.close(); + +// var block_inputs: [MaxBlockArguments / 2]*const mlir.Value = undefined; +// var block_accs: [MaxBlockArguments / 2]*const mlir.Value = undefined; +// for (0..inputs.len) |i| { +// block_inputs[i] = block.argument(i); +// block_accs[i] = block.argument(inputs.len + i); +// } +// _ = blkfn(blkctx, ctx, block_inputs[0..inputs.len], block_accs[0..init_values.len]); +// } + +// return mlir.Operation.make(ctx, "stablehlo.reduce", .{ +// .operands = .{ .variadic = &.{ inputs, init_values } }, +// .result_type_inference = true, +// .block = block, +// .attributes = &.{ +// .{ "dimensions", .dense(ctx, .i64, dimensions) }, +// }, +// .location = location, +// }); +// } + +// pub fn sort( +// ctx: *mlir.Context, +// inputs: []const *const mlir.Value, +// dimension: i64, +// is_stable: bool, +// blkctx: anytype, +// compfn: fn (anytype, mlir.Context, []const *const mlir.Value) mlir.Operation, +// location: *const mlir.Location, +// ) *mlir.Operation { +// const MaxBlockArguments = 32; + +// const locations = ([_]mlir.Location{mlir.Location.unknown(ctx)} ** MaxBlockArguments)[0 .. inputs.len * 2]; +// var sort_elem_types: [MaxBlockArguments]*const mlir.Type = undefined; +// for (inputs, 0..) |input, i| { +// const arg_type: *const mlir.Type = .tensor(&.{}, elementTypeOrSelf(input.getType())); +// sort_elem_types[i * 2] = arg_type; +// sort_elem_types[i * 2 + 1] = arg_type; +// } +// var block = mlir.Block.init(sort_elem_types[0 .. inputs.len * 2], locations) catch unreachable; + +// var block_inputs: [MaxBlockArguments]*const mlir.Value = undefined; +// for (0..inputs.len * 2) |i| { +// block_inputs[i] = block.argument(i); +// } +// _ = compfn(blkctx, ctx, block_inputs[0 .. inputs.len * 2]); + +// return mlir.Operation.make(ctx, "stablehlo.sort", .{ +// .operands = .{ .variadic = &.{inputs} }, +// .result_type_inference = true, +// .block = block, +// .attributes = &.{ +// .{ "dimension", .int(ctx, .i64, dimension) }, +// .{ "is_stable", .boolean(ctx, is_stable) }, +// }, +// .location = location, +// }); +// } + +// pub fn dynamic_slice(ctx: *mlir.Context, operand: *const mlir.Value, new_dims: []const i64, start_indices: []const *const mlir.Value, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.dynamic_slice", .{ +// .operands = .{ .variadic = &.{ &.{operand}, start_indices } }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "slice_sizes", .dense(ctx, .i64, new_dims) }, +// }, +// .location = location, +// }); +// } + +// pub const PadOpts = struct { +// low: []const i64, +// high: []const i64, +// interior: []const i64, +// }; + +// pub fn pad(ctx: *mlir.Context, value: *const mlir.Value, padding_value: *const mlir.Value, opts: PadOpts, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.pad", .{ +// .operands = .{ .flat = &.{ value, padding_value } }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "edge_padding_low", .dense(ctx, .i64, opts.low) }, +// .{ "edge_padding_high", .dense(ctx, .i64, opts.high) }, +// .{ "interior_padding", .dense(ctx, .i64, opts.interior) }, +// }, +// .location = location, +// }); +// } + +// pub const TriangularSolveOpts = struct { +// left_side: bool, +// lower: bool, +// unit_diagonal: bool, +// transpose_a: Transpose.Type, +// }; + +// pub fn triangular_solve(ctx: *mlir.Context, value: *const mlir.Value, other: *const mlir.Value, location: *const mlir.Location, opts: TriangularSolveOpts) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.triangular_solve", .{ +// .operands = .{ .flat = &.{ value, other } }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "left_side", .i1FromBool(ctx, opts.left_side) }, +// .{ "lower", .i1FromBool(ctx, opts.lower) }, +// .{ "unit_diagonal", .i1FromBool(ctx, opts.unit_diagonal) }, +// .{ "transpose_a", Transpose.init(ctx, opts.transpose_a).asAttr() }, +// }, +// .location = location, +// }); +// } + +// pub const FftOpts = struct { +// kind: FftType.Type, +// length: []const i64, +// }; + +// pub fn fft(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location, opts: FftOpts) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.fft", .{ +// .operands = .{ .flat = &.{value} }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "fft_type", FftType.init(ctx, opts.kind).asAttr() }, +// .{ "fft_length", .dense(ctx, .i64, opts.length) }, +// }, +// .location = location, +// }); +// } + +// pub fn rng(ctx: *mlir.Context, a: *const mlir.Value, b: *const mlir.Value, shape: *const mlir.Value, rng_distribution: RngDistribution.Type, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.rng", .{ +// .operands = .{ .flat = &.{ a, b, shape } }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "rng_distribution", RngDistribution.init(ctx, rng_distribution).asAttr() }, +// }, +// .location = location, +// }); +// } + +// pub fn rng_bit_generator(ctx: *mlir.Context, rng_algorithm: RngAlgorithm.Type, initial_state: *const mlir.Value, res_state_type: *const mlir.Type, res_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.rng_bit_generator", .{ +// .operands = .{ .flat = &.{initial_state} }, +// .results = &.{ res_state_type, res_type }, +// .attributes = &.{ +// .{ "rng_algorithm", RngAlgorithm.init(ctx, rng_algorithm).asAttr() }, +// }, +// .location = location, +// }); +// } + +// pub fn reduce_precision(ctx: *mlir.Context, value: *const mlir.Value, exponent_bits: i32, mantissa_bits: i32, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.reduce_precision", .{ +// .operands = .{ .flat = &.{value} }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "exponent_bits", .int(ctx, .i32, exponent_bits) }, +// .{ "mantissa_bits", .int(ctx, .i32, mantissa_bits) }, +// }, +// .location = location, +// }); +// } + +// pub fn dynamic_update_slice(ctx: *mlir.Context, operand: *const mlir.Value, update: *const mlir.Value, start_indices: []const *const mlir.Value, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.dynamic_update_slice", .{ +// .operands = .{ .variadic = &.{ &.{operand}, &.{update}, start_indices } }, +// .result_type_inference = true, +// .location = location, +// }); +// } + +// pub fn get_tuple_element(ctx: *mlir.Context, tuple_value: *const mlir.Value, index: i64, location: *const mlir.Location) *mlir.Operation { +// return mlir.Operation.make(ctx, "stablehlo.get_tuple_element", .{ +// .operands = .{ .flat = &.{tuple_value} }, +// .result_type_inference = true, +// .attributes = &.{ +// .{ "index", .int(ctx, .i32, index) }, +// }, +// .location = location, +// }); +// } + +// pub const ConvolutionOpts = struct { +// window_strides: []const i64, +// pad_value: []const i64, +// pad_shape: []const i64 = &.{}, +// lhs_dilation: []const i64, +// rhs_dilation: []const i64, +// window_reversal: []const bool, +// input_batch_dimension: i64, +// input_feature_dimension: i64, +// input_spatial_dimensions: []const i64, +// kernel_input_feature_dimension: i64, +// kernel_output_feature_dimension: i64, +// kernel_spatial_dimensions: []const i64, +// output_batch_dimension: i64, +// output_feature_dimension: i64, +// output_spatial_dimensions: []const i64, +// feature_group_count: i64, +// batch_group_count: i64, +// precision_config: []const PrecisionAttribute.Precision = &.{}, +// }; + +// pub fn convolution( +// ctx: *mlir.Context, +// lhs: *const mlir.Value, +// rhs: *const mlir.Value, +// opts: ConvolutionOpts, +// res_type: *const mlir.Type, +// location: *const mlir.Location, +// ) *mlir.Operation { +// var max_precisions: [2]mlir.Attribute = undefined; +// for (opts.precision_config, 0..) |p, i| { +// max_precisions[i] = PrecisionAttribute.init(ctx, p).asAttr(); +// } +// var window_reversal: [3]i32 = undefined; +// for (opts.window_reversal, 0..) |w, i| { +// window_reversal[i] = @intCast(@intFromBool(w)); +// } +// return mlir.Operation.make(ctx, "stablehlo.convolution", .{ +// .operands = .{ .flat = &.{ lhs, rhs } }, +// .results = &.{res_type}, +// .attributes = &.{ +// .{ "window_strides", .dense(ctx, .i64, opts.window_strides) }, +// .{ "padding", .denseElements(ctx, opts.pad_shape, .i64, opts.pad_value) }, +// .{ "lhs_dilation", .dense(ctx, .i64, opts.lhs_dilation) }, +// .{ "rhs_dilation", .dense(ctx, .i64, opts.rhs_dilation) }, +// .{ "window_reversal", .dense(ctx, .bool, window_reversal[0..opts.window_reversal.len]) }, +// .{ +// "dimension_numbers", ConvDimensionNumbersAttribute.init(ctx, .{ +// .input_batch_dimension = opts.input_batch_dimension, +// .input_feature_dimension = opts.input_feature_dimension, +// .input_spatial_dimensions = opts.input_spatial_dimensions, +// .kernel_input_feature_dimension = opts.kernel_input_feature_dimension, +// .kernel_output_feature_dimension = opts.kernel_output_feature_dimension, +// .kernel_spatial_dimensions = opts.kernel_spatial_dimensions, +// .output_batch_dimension = opts.output_batch_dimension, +// .output_feature_dimension = opts.output_feature_dimension, +// .output_spatial_dimensions = opts.output_spatial_dimensions, +// }).asAttr(), +// }, +// .{ "feature_group_count", .int(ctx, .i64, opts.feature_group_count) }, +// .{ "batch_group_count", .int(ctx, .i64, opts.batch_group_count) }, +// .{ "precision_config", .array(ctx, &max_precisions) }, +// }, +// .location = location, +// }); +// } + +// pub const CustomCallOpts = struct { +// pub const ApiVersion = enum(i32) { +// original = 1, +// status_returning = 2, +// status_returning_unified = 3, +// typed_ffi = 4, +// }; + +// call_target_name: []const u8, +// has_side_effect: bool = false, +// backend_config: ?*const mlir.Attribute, +// operand_layouts: ?[]const []const usize = null, +// result_layouts: ?[]const []const usize = null, +// output_operand_aliases: ?[]const i64 = null, +// additional_attributes: ?[]const mlir.AttrTuple = &.{}, +// api_version: ApiVersion, +// }; + +// pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, opts: CustomCallOpts, result_types: []const *const mlir.Type, location: *const mlir.Location) *mlir.Operation { +// const MAX_OPERANDS = 64; +// const MAX_RESULTS = 16; + +// const backend_config = opts.backend_config orelse mlir.Attribute.string(ctx, ""); +// if (@intFromEnum(opts.api_version) < @intFromEnum(CustomCallOpts.ApiVersion.typed_ffi)) { +// stdx.debug.assert( +// backend_config.isA(mlir.StringAttribute), +// "API version < 4 requires a string as backend_config, got {}", +// .{backend_config}, +// ); +// } else { +// stdx.debug.assert( +// backend_config.isA(mlir.DictionaryAttribute), +// "API version >= 4 requires a dictionary as backend_config, got {}", +// .{backend_config}, +// ); +// } + +// var attrs: stdx.BoundedArray(mlir.AttrTuple, 32) = .{}; +// attrs.appendSliceAssumeCapacity(&[_]mlir.AttrTuple{ +// .{ "api_version", .int(ctx, .i32, @intFromEnum(opts.api_version)) }, +// .{ "call_target_name", .string(ctx, opts.call_target_name) }, +// .{ "has_side_effect", .boolean(ctx, opts.has_side_effect) }, +// .{ "backend_config", backend_config }, +// }); + +// { +// var output_operand_aliases: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; +// for (opts.output_operand_aliases) |alias| { +// output_operand_aliases.appendAssumeCapacity( +// OutputOperandAliasAttribute.init(ctx, &.{}, alias, &.{}).asAttr(), +// ); +// } +// attrs.appendAssumeCapacity(.{ "output_operand_aliases", .array(ctx, output_operand_aliases.constSlice()) }); +// } + +// const MINOR_TO_MAJOR = blk: { +// const MAX_RANK = 8; +// var ret: [MAX_RANK]usize = undefined; +// for (0..MAX_RANK) |i| { +// ret[i] = @intCast(MAX_RANK - i - 1); +// } +// break :blk ret; +// }; + +// if (opts.operand_layouts) |layouts| { +// var operand_layouts: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; +// for (layouts) |ol| { +// operand_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); +// } +// attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); +// } else { +// const operand_layouts = blk: { +// var ret: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; +// for (inputs) |input| { +// const ranked_type = input.getType().as(mlir.RankedTensorType).?; +// const ol = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_type.getRank() ..]; +// ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); +// } +// break :blk ret; +// }; +// attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); +// } + +// if (opts.result_layouts) |layouts| { +// var result_layouts: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; +// for (layouts) |rl| { +// result_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); +// } +// attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); +// } else { +// const result_layouts = blk: { +// var ret: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; +// for (res_types) |t| { +// const ranked_t = t.as(mlir.RankedTensorType).?; +// const rl = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_t.getRank() ..]; +// ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); +// } +// break :blk ret; +// }; +// attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); +// } + +// attrs.appendSlice(opts.additional_attributes) catch @panic("Too many additional_attributes"); + +// return mlir.Operation.make(ctx, "stablehlo.custom_call", .{ +// .operands = inputs, +// .results = res_types, +// .attributes = attrs.constSlice(), +// .location = location, +// }); +// } + +// pub const DotDimensionNumbersAttribute = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsADotDimensionNumbers; +// const Self = DotDimensionNumbersAttribute; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub fn init(ctx: *mlir.Context, args: struct { +// lhs_batching_dimensions: []const i64, +// rhs_batching_dimensions: []const i64, +// lhs_contracting_dimensions: []const i64, +// rhs_contracting_dimensions: []const i64, +// }) Self { +// return .{ +// ._inner = c.stablehloDotDimensionNumbersGet( +// ctx._inner, +// @intCast(args.lhs_batching_dimensions.len), +// args.lhs_batching_dimensions.ptr, +// @intCast(args.rhs_batching_dimensions.len), +// args.rhs_batching_dimensions.ptr, +// @intCast(args.lhs_contracting_dimensions.len), +// args.lhs_contracting_dimensions.ptr, +// @intCast(args.rhs_contracting_dimensions.len), +// args.rhs_contracting_dimensions.ptr, +// ), +// }; +// } + +// pub fn getLhsBatchingDimensionsSize(self: Self) usize { +// return @intCast(c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsSize(self._inner)); +// } + +// pub fn getLhsBatchingDimensionsElem(self: Self, pos: usize) i64 { +// return c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsElem(self._inner, @intCast(pos)); +// } + +// pub fn getRhsBatchingDimensionsSize(self: Self) usize { +// return @intCast(c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsSize(self._inner)); +// } + +// pub fn getRhsBatchingDimensionsElem(self: Self, pos: usize) i64 { +// return c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsElem(self._inner, @intCast(pos)); +// } + +// pub fn getLhsContractingDimensionsSize(self: Self) usize { +// return @intCast(c.stablehloDotDimensionNumbersGetLhsContractingDimensionsSize(self._inner)); +// } + +// pub fn getLhsContractingDimensionsElem(self: Self, pos: usize) i64 { +// return c.stablehloDotDimensionNumbersGetLhsContractingDimensionsElem(self._inner, @intCast(pos)); +// } + +// pub fn getRhsContractingDimensionsSize(self: Self) usize { +// return @intCast(c.stablehloDotDimensionNumbersGetRhsContractingDimensionsSize(self._inner)); +// } + +// pub fn getRhsContractingDimensionsElem(self: Self, pos: usize) i64 { +// return c.stablehloDotDimensionNumbersGetRhsContractingDimensionsElem(self._inner, @intCast(pos)); +// } +// }; + +// pub const GatherDimensionNumbersAttribute = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsAGatherDimensionNumbers; +// const Self = GatherDimensionNumbersAttribute; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub fn init( +// ctx: *mlir.Context, +// offset_dims: []const i64, +// collapsed_slice_dims: []const i64, +// operand_batching_dims: []const i64, +// start_indices_batching_dims: []const i64, +// start_index_map: []const i64, +// index_vector_dim: i64, +// ) Self { +// return .{ +// ._inner = c.stablehloGatherDimensionNumbersGet( +// ctx._inner, +// @intCast(offset_dims.len), +// offset_dims.ptr, +// @intCast(collapsed_slice_dims.len), +// collapsed_slice_dims.ptr, +// @intCast(operand_batching_dims.len), +// operand_batching_dims.ptr, +// @intCast(start_indices_batching_dims.len), +// start_indices_batching_dims.ptr, +// @intCast(start_index_map.len), +// start_index_map.ptr, +// index_vector_dim, +// ), +// }; +// } + +// pub fn getOffsetDimsSize(self: Self) usize { +// return @intCast(c.stablehloGatherDimensionNumbersGetOffsetDimsSize(self._inner)); +// } + +// pub fn getOffsetDimsElem(self: Self, pos: usize) i64 { +// return c.stablehloGatherDimensionNumbersGetOffsetDimsElem(self._inner, @intCast(pos)); +// } + +// pub fn getCollapsedSliceDimsSize(self: Self) usize { +// return @intCast(c.stablehloGatherDimensionNumbersGetCollapsedSliceDimsSize(self._inner)); +// } + +// pub fn getCollapsedSliceDimsElem(self: Self, pos: usize) i64 { +// return c.stablehloGatherDimensionNumbersGetCollapsedSliceDimsElem(self._inner, @intCast(pos)); +// } + +// pub fn getStartIndexMapSize(self: Self) usize { +// return @intCast(c.stablehloGatherDimensionNumbersGetStartIndexMapSize(self._inner)); +// } + +// pub fn getOperandBatchingDimsSize(self: Self) usize { +// return @intCast(c.stablehloGatherDimensionNumbersGetOperandBatchingDimsSize(self._inner)); +// } + +// pub fn getOperandBatchingDimsElem(self: Self, pos: usize) i64 { +// return c.stablehloGatherDimensionNumbersGetOperandBatchingDimsElem(self._inner, @intCast(pos)); +// } + +// pub fn getStartIndicesBatchingDimsSize(self: Self) usize { +// return @intCast(c.stablehloGatherDimensionNumbersGetStartIndicesBatchingDimsSize(self._inner)); +// } + +// pub fn getStartIndicesBatchingDimsElem(self: Self, pos: usize) i64 { +// return c.stablehloGatherDimensionNumbersGetStartIndicesBatchingDimsElem(self._inner, @intCast(pos)); +// } + +// pub fn getStartIndexMapElem(self: Self, pos: usize) i64 { +// return c.stablehloGatherDimensionNumbersGetStartIndexMapElem(self._inner, @intCast(pos)); +// } + +// pub fn getIndexVectorDim(self: Self) usize { +// return @intCast(c.stablehloGatherDimensionNumbersGetIndexVectorDim(self._inner)); +// } +// }; + +// pub const ConvDimensionNumbersAttribute = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsAConvDimensionNumbers; +// const Self = ConvDimensionNumbersAttribute; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub fn init(ctx: *mlir.Context, args: struct { +// input_batch_dimension: i64, +// input_feature_dimension: i64, +// input_spatial_dimensions: []const i64, +// kernel_input_feature_dimension: i64, +// kernel_output_feature_dimension: i64, +// kernel_spatial_dimensions: []const i64, +// output_batch_dimension: i64, +// output_feature_dimension: i64, +// output_spatial_dimensions: []const i64, +// }) Self { +// return .{ +// ._inner = c.stablehloConvDimensionNumbersGet( +// ctx._inner, +// args.input_batch_dimension, +// args.input_feature_dimension, +// @intCast(args.input_spatial_dimensions.len), +// args.input_spatial_dimensions.ptr, +// args.kernel_input_feature_dimension, +// args.kernel_output_feature_dimension, +// @intCast(args.kernel_spatial_dimensions.len), +// args.kernel_spatial_dimensions.ptr, +// args.output_batch_dimension, +// args.output_feature_dimension, +// @intCast(args.output_spatial_dimensions.len), +// args.output_spatial_dimensions.ptr, +// ), +// }; +// } + +// pub fn getInputBatchDimension(self: Self) i64 { +// return c.stablehloConvDimensionNumbersGetInputBatchDimension(self._inner); +// } + +// pub fn getInputFeatureDimension(self: Self) i64 { +// return c.stablehloConvDimensionNumbersGetInputFeatureDimension(self._inner); +// } + +// pub fn getInputSpatialDimensionsSize(self: Self) usize { +// return @intCast(c.stablehloConvDimensionNumbersGetInputSpatialDimensionsSize(self._inner)); +// } + +// pub fn getInputSpatialDimensionsElem(self: Self, pos: usize) i64 { +// return c.stablehloConvDimensionNumbersGetInputSpatialDimensionsElem(self._inner, @intCast(pos)); +// } + +// pub fn getKernelInputFeatureDimension(self: Self) i64 { +// return c.stablehloConvDimensionNumbersGetKernelInputFeatureDimension(self._inner); +// } + +// pub fn getKernelOutputFeatureDimension(self: Self) i64 { +// return c.stablehloConvDimensionNumbersGetKernelOutputFeatureDimension(self._inner); +// } + +// pub fn getKernelSpatialDimensionsSize(self: Self) usize { +// return @intCast(c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsSize(self._inner)); +// } + +// pub fn getKernelSpatialDimensionsElem(self: Self, pos: usize) i64 { +// return c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsElem(self._inner, @intCast(pos)); +// } + +// pub fn getOutputBatchDimension(self: Self) i64 { +// return c.stablehloConvDimensionNumbersGetOutputBatchDimension(self._inner); +// } + +// pub fn getOutputFeatureDimension(self: Self) i64 { +// return c.stablehloConvDimensionNumbersGetOutputFeatureDimension(self._inner); +// } + +// pub fn getOutputSpatialDimensionsSize(self: Self) usize { +// return @intCast(c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsSize(self._inner)); +// } + +// pub fn getOutputSpatialDimensionsElem(self: Self, pos: usize) i64 { +// return c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsElem(self._inner, @intCast(pos)); +// } +// }; + +// pub const OutputOperandAliasAttribute = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsAOutputOperandAlias; +// pub const asAttr = mlir.Attribute.fromAny(OutputOperandAliasAttribute); +// pub const eql = mlir.Attribute.eqlAny(OutputOperandAliasAttribute); + +// pub fn init( +// ctx: *mlir.Context, +// output_tuple_indices: []const i64, +// operand_index: i64, +// operand_tuple_indices: []const i64, +// ) OutputOperandAliasAttribute { +// return .{ ._inner = c.stablehloOutputOperandAliasGet( +// ctx._inner, +// @intCast(output_tuple_indices.len), +// output_tuple_indices.ptr, +// @intCast(operand_index), +// @intCast(operand_tuple_indices.len), +// operand_tuple_indices.ptr, +// ) }; +// } +// }; + +// pub const PrecisionAttribute = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsAPrecisionAttr; +// const Self = PrecisionAttribute; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub const Precision = enum { +// DEFAULT, +// HIGH, +// HIGHEST, +// }; + +// pub fn init(ctx: *mlir.Context, value: Precision) Self { +// return .{ ._inner = c.stablehloPrecisionAttrGet(ctx._inner, stringRef(@tagName(value))) }; +// } + +// pub fn getValue(self: Self) Precision { +// const value = mlir.fromStringRef(c.stablehloPrecisionAttrGetValue(self._inner)); +// return std.meta.stringToEnum(Precision, value) orelse unreachable; +// } +// }; + +// pub const ComparisonDirection = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsAComparisonDirectionAttr; +// const Self = ComparisonDirection; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub const Direction = enum { +// EQ, +// NE, +// GE, +// GT, +// LE, +// LT, +// }; + +// pub fn init(ctx: *mlir.Context, value: Direction) Self { +// return .{ ._inner = c.stablehloComparisonDirectionAttrGet(ctx._inner, stringRef(@tagName(value))) }; +// } + +// pub fn getValue(self: Self) Direction { +// const value = mlir.fromStringRef(c.stablehloComparisonDirectionAttrGetValue(self._inner)); +// return std.meta.stringToEnum(Direction, value) orelse unreachable; +// } +// }; + +// pub const CompareType = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsAComparisonTypeAttr; +// const Self = CompareType; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub const Type = enum { +// SIGNED, +// UNSIGNED, +// FLOAT, +// TOTALORDER, +// }; + +// pub fn init(ctx: *mlir.Context, value: Type) Self { +// return .{ ._inner = c.stablehloComparisonTypeAttrGet(ctx._inner, stringRef(@tagName(value))) }; +// } + +// pub fn getValue(self: Self) Type { +// const value = mlir.fromStringRef(c.stablehloComparisonTypeAttrGetValue(self._inner)); +// return std.meta.stringToEnum(Type, value) orelse unreachable; +// } +// }; + +// pub const Transpose = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsATransposeAttr; +// const Self = Transpose; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub const Type = enum { +// NO_TRANSPOSE, +// TRANSPOSE, +// ADJOINT, +// }; + +// pub fn init(ctx: *mlir.Context, value: Type) Self { +// return .{ ._inner = c.stablehloTransposeAttrGet(ctx._inner, stringRef(@tagName(value))) }; +// } + +// pub fn getValue(self: Self) Type { +// const value = mlir.fromStringRef(c.stablehloTransposeAttrGetValue(self._inner)); +// return std.meta.stringToEnum(Type, value) orelse unreachable; +// } +// }; + +// pub const FftType = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsAFftTypeAttr; +// const Self = FftType; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub const Type = enum { +// FFT, +// IFFT, +// RFFT, +// IRFFT, +// }; + +// pub fn init(ctx: *mlir.Context, value: Type) Self { +// return .{ ._inner = c.stablehloFftTypeAttrGet(ctx._inner, stringRef(@tagName(value))) }; +// } + +// pub fn getValue(self: Self) Type { +// const value = mlir.fromStringRef(c.stablehloFftTypeAttrGetValue(self._inner)); +// return std.meta.stringToEnum(Type, value) orelse unreachable; +// } +// }; + +// pub const RngDistribution = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsARngDistributionAttr; +// const Self = RngDistribution; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub const Type = enum { +// UNIFORM, +// NORMAL, +// }; + +// pub fn init(ctx: *mlir.Context, value: Type) Self { +// return .{ ._inner = c.stablehloRngDistributionAttrGet(ctx._inner, stringRef(@tagName(value))) }; +// } + +// pub fn getValue(self: Self) Type { +// const value = mlir.fromStringRef(c.stablehloRngDistributionAttrGetValue(self._inner)); +// return std.meta.stringToEnum(Type, value) orelse unreachable; +// } +// }; + +// pub const RngAlgorithm = struct { +// _inner: c.MlirAttribute, + +// pub const isAFn = c.stablehloAttributeIsARngAlgorithmAttr; +// const Self = RngAlgorithm; +// pub const asAttr = mlir.Attribute.fromAny(Self); +// pub const eql = mlir.Attribute.eqlAny(Self); + +// pub const Type = enum { +// DEFAULT, +// THREE_FRY, +// PHILOX, +// }; + +// pub fn init(ctx: *mlir.Context, value: Type) Self { +// return .{ ._inner = c.stablehloRngAlgorithmAttrGet(ctx._inner, stringRef(@tagName(value))) }; +// } + +// pub fn getValue(self: Self) Type { +// const value = mlir.fromStringRef(c.stablehloRngAlgorithmAttrGetValue(self._inner)); +// return std.meta.stringToEnum(Type, value) orelse unreachable; +// } +// }; + +fn stringFromStream(buf: []u8, streamFn: anytype, args: anytype) []const u8 { + var writer = std.Io.Writer.fixed(buf); + var sctx: mlir.StringCallbackCtx = .{ .writer = &writer }; + const new_args = args ++ .{ mlir.stringCallback, &sctx }; + @call(.auto, streamFn, new_args); + return writer.buffered(); +} + +pub fn versionFromCompatibilityRequirement(buffer: []u8, requirement: c.MlirStablehloCompatibilityRequirement) []const u8 { + return stringFromStream(buffer, c.stablehloVersionFromCompatibilityRequirement, .{requirement}); +} + +pub fn smallerVersion(version1: []const u8, version2: []const u8) []const u8 { var buf: [32]u8 = undefined; - - var stream = std.io.fixedBufferStream(&buf); - var context = .{ .writer = stream.writer() }; - const WriterContext = @TypeOf(context); - - _ = c.stablehloGetSmallerVersion(mlir.stringRef(version1), mlir.stringRef(version2), (struct { - pub fn callback(mlir_str: c.MlirStringRef, userdata: ?*anyopaque) callconv(.c) void { - const inner_ctx: *WriterContext = @ptrCast(@alignCast(userdata)); - _ = inner_ctx.writer.write(mlir.fromStringRef(mlir_str)) catch unreachable; - } - }).callback, &context); - - return if (std.mem.eql(u8, buf[0..stream.pos], version1)) version1 else version2; + const result = stringFromStream(&buf, c.stablehloGetSmallerVersion, .{ stringRef(version1), stringRef(version2) }); + return if (std.mem.eql(u8, result, version1)) version1 else version2; } -pub fn getCurrentVersion() []const u8 { +pub fn currentVersion() []const u8 { const state = struct { var buf: [32]u8 = undefined; var str: []const u8 = undefined; var once = std.once(call); fn call() void { - var stream = std.io.fixedBufferStream(&buf); - var writer_ = stream.writer(); - const ContextWriter = @TypeOf(writer_); - - c.stablehloGetCurrentVersion((struct { - pub fn callback(mlir_str: c.MlirStringRef, userdata: ?*anyopaque) callconv(.c) void { - const writer: *ContextWriter = @ptrCast(@alignCast(userdata)); - _ = writer.write(mlir.fromStringRef(mlir_str)) catch unreachable; - } - }).callback, &writer_); - - str = buf[0..stream.pos]; + str = stringFromStream(&buf, c.stablehloGetCurrentVersion, .{}); } }; @@ -1327,25 +1250,14 @@ pub fn getCurrentVersion() []const u8 { return state.str; } -pub fn getMinimumVersion() []const u8 { +pub fn minimumVersion() []const u8 { const state = struct { var buf: [32]u8 = undefined; var str: []const u8 = undefined; var once = std.once(call); fn call() void { - var stream = std.io.fixedBufferStream(&buf); - var context = .{ .writer = stream.writer() }; - const WriterContext = @TypeOf(context); - - c.stablehloGetMinimumVersion((struct { - pub fn callback(mlir_str: c.MlirStringRef, userdata: ?*anyopaque) callconv(.c) void { - const inner_ctx: *WriterContext = @ptrCast(@alignCast(userdata)); - _ = inner_ctx.writer.write(mlir.fromStringRef(mlir_str)) catch unreachable; - } - }).callback, &context); - - str = buf[0..stream.pos]; + str = stringFromStream(&buf, c.stablehloGetMinimumVersion, .{}); } }; @@ -1353,14 +1265,35 @@ pub fn getMinimumVersion() []const u8 { return state.str; } -pub fn serializePortableArtifact(bytecode: []const u8, target_version: []const u8, writer: anytype) !void { - var context = .{ .writer = writer }; - const WriterContext = @TypeOf(context); +pub fn serializePortableArtifact(bytecode: []const u8, target_version: []const u8, writer: *std.Io.Writer) !void { + var sctx: mlir.StringCallbackCtx = .{ .writer = writer }; + const result = c.stablehloSerializePortableArtifactFromStringRef( + stringRef(bytecode), + stringRef(target_version), + mlir.stringCallback, + &sctx, + ); + if (sctx.err) |err| { + return err; + } + if (result.ptr == null) { + return error.InvalidMlirBytecodeVersion; + } +} - try mlir.successOr(c.stablehloSerializePortableArtifactFromStringRef(mlir.stringRef(bytecode), mlir.stringRef(target_version), (struct { - pub fn callback(mlir_str: c.MlirStringRef, userdata: ?*anyopaque) callconv(.c) void { - const inner_ctx: *WriterContext = @ptrCast(@alignCast(userdata)); - _ = inner_ctx.writer.write(mlir.fromStringRef(mlir_str)) catch unreachable; - } - }).callback, &context), error.InvalidMlirBytecodeVersion); +pub fn serializePortableArtifact2(module: *mlir.Module, target_version: []const u8, writer: *std.Io.Writer) !void { + var sctx: mlir.StringCallbackCtx = .{ .writer = writer }; + const result = c.stablehloSerializePortableArtifactFromModule( + module.ptr(), + stringRef(target_version), + mlir.stringCallback, + &sctx, + true, + ); + if (sctx.err) |err| { + return err; + } + if (c.mlirLogicalResultIsFailure(result)) { + return error.InvalidMlirBytecodeVersion; + } } diff --git a/mlir/main.zig b/mlir/main.zig index 177d2a5a..3963a9ae 100644 --- a/mlir/main.zig +++ b/mlir/main.zig @@ -4,15 +4,18 @@ const dialects = @import("mlir/dialects"); const mlir = @import("mlir2"); pub fn main() !void { + const allocator = std.heap.c_allocator; const registry = try mlir.DialectRegistry.init(); defer registry.deinit(); // const handle = mlir.DialectHandle.fromString("func"); - inline for (.{ "arith", "func" }) |d| { + inline for (.{ "func", "stablehlo" }) |d| { registry.registerDialect(d); } + // mlir.registerPasses("AllStablehlo"); + const ctx = try mlir.Context.init(.{ .registry = registry, .threading = true, @@ -26,6 +29,16 @@ pub fn main() !void { const unk = mlir.Location.unknown(ctx); _ = unk; // autofix + var serialized: std.Io.Writer.Allocating = .init(allocator); + defer serialized.deinit(); + + // const stablehlo_version = blk: { + // if (dialects.stablehlo.currentVersion()) |requested_version| { + // break :blk dialects.stablehlo.stablehloGetSmallerVersion(requested_version, dialects.stablehlo.getCurrentVersion()); + // } + // break :blk dialects.stablehlo.minimumVersion(); + // }; + // const attr = mlir.integerAttribute(ctx, @as(i32, 5)); // _ = attr; // autofix @@ -38,31 +51,31 @@ pub fn main() !void { // }); // _ = dict; // autofix - const int64 = mlir.integerType(ctx, 64, .signless); + const module = mlir.Module.init(.unknown(ctx)); + defer module.deinit(); - const block = mlir.Block.init(&.{ int64, int64 }, &.{ .unknown(ctx), .unknown(ctx) }); + const tensor_i32 = mlir.rankedTensorType(&.{ 4096, 4096 }, mlir.integerType(ctx, .i32)); - block.appendOwnedOperation(dialects.func.return_(ctx, &.{block.argument(0)}, .unknown(ctx))); - - const op = dialects.func.func(ctx, .{ + module.body().appendOwnedOperation(dialects.func.func(ctx, .{ .name = "pouet", - .args = &.{ int64, int64 }, - .results = &.{int64}, - .block = block, + .visibility = .private, + .block = blk: { + const body = mlir.Block.init(&.{ tensor_i32, tensor_i32 }, &.{ .unknown(ctx), .unknown(ctx) }); + const add_op = dialects.stablehlo.add(ctx, body.argument(0), body.argument(1), .unknown(ctx)).appendTo(body); + const mul_op = dialects.stablehlo.multiply(ctx, add_op.result(0), body.argument(1), .unknown(ctx)).appendTo(body); + _ = dialects.func.return_(ctx, mul_op.result(0), .unknown(ctx)).appendTo(body); + break :blk body; + }, .location = .unknown(ctx), - }); + })); - // const op = try mlir.Operation.parse(ctx, "func.func private @pouet()", "pouet"); + std.debug.print(">>>>>>\n{f}\n", .{module.operation().fmt(.{ .print_generic_op_form = false })}); + std.debug.print(">>>>>>\n{f}\n", .{module.operation().fmt(.{ .print_generic_op_form = true })}); - // // // const mod = mlir.Module.init(mlir.Location.unknown(ctx)); - - // const op = mlir.Operation.make(ctx, "arith.addi", .{ - // .operands = .{ .flat = &.{ block.argument(0), block.argument(1) } }, - // .results = .{ .variadic = &.{ &.{ int64, int64 }, &.{int64} } }, - // }); - // defer op.deinit(); - - std.debug.print(">>>>>> {f}\n", .{op.fmt(.{ .print_generic_op_form = true })}); + // dialects.stablehlo.serializePortableArtifact2(module, dialects.stablehlo.minimumVersion(), &serialized.writer) catch |err| { + // std.debug.print("failed to serialize to portable artifact: {}\n", .{err}); + // return err; + // }; // const body = mod.body(); // _ = body.addArgument(mlir.integerType(ctx, 64, .signless), .unknown(ctx)); diff --git a/mlir/registry.zig b/mlir/registry.zig index b4b17d44..6c999b83 100644 --- a/mlir/registry.zig +++ b/mlir/registry.zig @@ -250,13 +250,57 @@ pub const IndexType = opaque { } }; -pub const Signedness = enum { - signless, - signed, - unsigned, +pub const IntegerTypes = enum { + i1, + i4, + i8, + i16, + i32, + i64, + i128, + u1, + u4, + u8, + u16, + u32, + u64, + u128, + si1, + si4, + si8, + si16, + si32, + si64, + si128, + + pub fn bitwidth(self: IntegerTypes) usize { + return switch (self) { + .i1, .u1, .si1 => 1, + .i4, .u4, .si4 => 4, + .i8, .u8, .si8 => 8, + .i16, .u16, .si16 => 16, + .i32, .u32, .si32 => 32, + .i64, .u64, .si64 => 64, + .i128, .u128, .si128 => 128, + }; + } + + pub fn signedness(self: IntegerTypes) IntegerType.Signedness { + return switch (self) { + .i1, .i4, .i8, .i16, .i32, .i64, .i128 => .signless, + .si1, .si4, .si8, .si16, .si32, .si64, .si128 => .signed, + .u1, .u4, .u8, .u16, .u32, .u64, .u128 => .unsigned, + }; + } }; pub const IntegerType = opaque { + pub const Signedness = enum { + signless, + signed, + unsigned, + }; + const M = Methods(IntegerType, c.MlirType); pub const isAFn = c.mlirTypeIsAInteger; @@ -264,7 +308,11 @@ pub const IntegerType = opaque { pub const eql = M.eql(c.mlirTypeEqual); pub const format = M.format(c.mlirTypePrint); - fn get(ctx: *Context, bitwidth: usize, sign: Signedness) *const IntegerType { + fn get(ctx: *Context, it: IntegerTypes) *const IntegerType { + return exact(ctx, it.bitwidth(), it.signedness()); + } + + fn exact(ctx: *Context, bitwidth: usize, sign: Signedness) *const IntegerType { return @ptrCast(switch (sign) { .signless => c.mlirIntegerTypeGet(ctx.ptr(), @intCast(bitwidth)).ptr, .signed => c.mlirIntegerTypeSignedGet(ctx.ptr(), @intCast(bitwidth)).ptr, @@ -273,8 +321,8 @@ pub const IntegerType = opaque { } }; -pub fn integerType(ctx: *Context, bitwidth: usize, sign: Signedness) *const Type { - return @ptrCast(IntegerType.get(ctx, bitwidth, sign)); +pub fn integerType(ctx: *Context, it: IntegerTypes) *const Type { + return @ptrCast(IntegerType.get(ctx, it)); } pub const FloatTypes = enum { @@ -387,14 +435,9 @@ pub const IntegerAttribute = opaque { pub const eql = M.eql(c.mlirAttributeEqual); pub const format = M.format(c.mlirAttributePrint); - pub fn init(ctx: *Context, value_: anytype) *const IntegerAttribute { - const ti = @typeInfo(@TypeOf(value_)).int; - const sign: Signedness = switch (ti.signedness) { - .signed => .signless, - .unsigned => .unsigned, - }; + pub fn init(ctx: *Context, it: IntegerTypes, value_: anytype) *const IntegerAttribute { return @ptrCast(c.mlirIntegerAttrGet( - IntegerType.get(ctx, @intCast(ti.bits), sign).ptr(), + IntegerType.get(ctx, it).ptr(), @intCast(value_), ).ptr); } @@ -408,8 +451,8 @@ pub const IntegerAttribute = opaque { } }; -pub fn integerAttribute(ctx: *Context, value_: anytype) *const Attribute { - return @ptrCast(IntegerAttribute.init(ctx, value_)); +pub fn integerAttribute(ctx: *Context, it: IntegerTypes, value_: anytype) *const Attribute { + return @ptrCast(IntegerAttribute.init(ctx, it, value_)); } pub const BoolAttribute = opaque { @@ -528,26 +571,26 @@ pub const Identifier = opaque { pub const Module = opaque { const M = Methods(Module, c.MlirModule); - const ptr = M.ptr; + pub const ptr = M.ptr; - pub fn init(loc: *const Location) *const Module { - return @ptrCast(c.mlirModuleCreateEmpty(loc.ptr()).ptr); + pub fn init(loc: *const Location) *Module { + return @constCast(@ptrCast(c.mlirModuleCreateEmpty(loc.ptr()).ptr)); } pub fn deinit(self: *Module) void { c.mlirModuleDestroy(self.ptr()); } - pub fn fromOperation(op: *Operation) *const Module { - return @ptrCast(c.mlirModuleFromOperation(op.ptr()).ptr); + pub fn fromOperation(op: *Operation) *Module { + return @constCast(@ptrCast(c.mlirModuleFromOperation(op.ptr()).ptr)); } pub fn operation(self: *const Module) *Operation { return @ptrCast(c.mlirModuleGetOperation(self.ptr()).ptr); } - pub fn parse(ctx: *Context, source: []const u8) Error!*const Module { - return @ptrCast(c.mlirModuleCreateParse(ctx.ptr(), stringRef(source)).ptr orelse return Error.InvalidMlir); + pub fn parse(ctx: *Context, source: []const u8) Error!*Module { + return @constCast(@ptrCast(c.mlirModuleCreateParse(ctx.ptr(), stringRef(source)).ptr orelse return Error.InvalidMlir)); } pub fn context(self: *const Module) *Context { @@ -608,6 +651,10 @@ pub const Block = opaque { pub fn detach(self: *Block) void { c.mlirBlockDetach(self.ptr()); } + + pub fn terminator(self: *const Block) ?*Operation { + return @ptrCast(c.mlirBlockGetTerminator(self.ptr()).ptr); + } }; pub const NamedAttribute = struct { @@ -829,6 +876,11 @@ pub const Operation = opaque { return @ptrCast(c.mlirOperationGetBlock(self.ptr()).ptr); } + pub fn appendTo(self: *Operation, block_: *Block) *Operation { + block_.appendOwnedOperation(self); + return self; + } + pub fn parent(self: *const Operation) ?*Operation { return @ptrCast(c.mlirOperationGetParentOperation(self.ptr()).ptr); } @@ -853,7 +905,7 @@ pub const Operation = opaque { return @intCast(c.mlirOperationGetNumResults(self.ptr())); } - pub fn result(self: *const Operation, index: usize) Value { + pub fn result(self: *const Operation, index: usize) *const Value { return @ptrCast(c.mlirOperationGetResult(self.ptr(), @intCast(index)).ptr); } @@ -930,7 +982,7 @@ pub const Operation = opaque { operands: ?Operands = null, results: ?ResultTypes = null, result_type_inference: bool = false, - attributes: ?[]const AttrTuple = null, + attributes: ?[]const NamedAttribute = null, blocks: ?[]const *Block = null, verify: bool = true, location: ?*const Location = null, @@ -968,11 +1020,7 @@ pub const Operation = opaque { }, }; if (args.attributes) |attrs| { - for (attrs) |attr_tuple| { - const attr_name, const attr = attr_tuple; - const named_attr = NamedAttribute.named(ctx, attr_name, attr); - state.addAttributes(&.{named_attr}); - } + state.addAttributes(attrs); } if (args.blocks) |blocks| { const region = Region.init(); @@ -982,6 +1030,7 @@ pub const Operation = opaque { state.addOwnedRegions(&.{region}); } const new_op = try Operation.init(&state); + errdefer new_op.deinit(); if (args.verify and new_op.verify() == false) { log.err("Failed to verify MLIR operation:\n{f}", .{ new_op.fmt(.{ @@ -1252,11 +1301,11 @@ pub fn DenseArrayAttribute(comptime ElementType: DenseArrayTypes) type { return @ptrCast(getFn(ctx.ptr(), @intCast(values.len), @ptrCast(values)).ptr); } - pub fn get(self: *const Self, pos: usize) ElementTypeZig { + pub fn element(self: *const Self, pos: usize) ElementTypeZig { return getElementFn(self.ptr(), @intCast(pos)); } - pub fn len(self: *const Self) usize { + pub fn numElements(self: *const Self) usize { return @intCast(c.mlirDenseArrayGetNumElements(self.ptr())); } }; @@ -1278,12 +1327,12 @@ pub const ArrayAttribute = opaque { return @ptrCast(c.mlirArrayAttrGet(ctx.ptr(), @intCast(attrs.len), @ptrCast(attrs)).ptr); } - pub fn size(self: *const ArrayAttribute) usize { - return @intCast(c.mlirArrayAttrGetNumElements(self.ptr())); + pub fn element(self: *const ArrayAttribute, index: usize) Attribute { + return @ptrCast(c.mlirArrayAttrGetElement(self.ptr(), @intCast(index)).ptr); } - pub fn get(self: *const ArrayAttribute, index: usize) Attribute { - return @ptrCast(c.mlirArrayAttrGetElement(self.ptr(), @intCast(index)).ptr); + pub fn numElements(self: *const ArrayAttribute) usize { + return @intCast(c.mlirArrayAttrGetNumElements(self.ptr())); } }; @@ -1334,3 +1383,20 @@ pub const FlatSymbolRefAttribute = opaque { pub fn flatSymbolRefAttribute(ctx: *Context, symbol: []const u8) *const Attribute { return @ptrCast(FlatSymbolRefAttribute.init(ctx, symbol)); } + +pub const UnitAttribute = opaque { + const M = Methods(UnitAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAUnit; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn get(ctx: *Context) *const UnitAttribute { + return @ptrCast(c.mlirUnitAttrGet(ctx.ptr()).ptr); + } +}; + +pub fn unitAttribute(ctx: *Context) *const Attribute { + return @ptrCast(UnitAttribute.get(ctx)); +} From 9b67afc4583bf283c1523bb1a308c3f1cc79b0a7 Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Thu, 28 Aug 2025 02:07:41 +0200 Subject: [PATCH 04/37] WIP --- mlir/BUILD.bazel | 3 +- mlir/dialects/BUILD.bazel | 1 + mlir/dialects/func.zig | 9 +- mlir/dialects/stablehlo/stablehlo.zig | 248 +++++++++++++++----------- mlir/main.zig | 6 +- mlir/mlir.h | 3 +- 6 files changed, 158 insertions(+), 112 deletions(-) diff --git a/mlir/BUILD.bazel b/mlir/BUILD.bazel index f0ba476f..73f1d12d 100644 --- a/mlir/BUILD.bazel +++ b/mlir/BUILD.bazel @@ -8,8 +8,7 @@ cc_library( deps = [ "@llvm-project//mlir:CAPIArith", "@llvm-project//mlir:CAPIIR", - "@llvm-project//mlir:CAPIMath", - "@llvm-project//mlir:CAPISCF", + "@llvm-project//mlir:CAPIMemRef", "@llvm-project//mlir:CAPITransforms", ], ) diff --git a/mlir/dialects/BUILD.bazel b/mlir/dialects/BUILD.bazel index 34400ca9..9c4bd6b1 100644 --- a/mlir/dialects/BUILD.bazel +++ b/mlir/dialects/BUILD.bazel @@ -5,6 +5,7 @@ zig_library( srcs = [ # "arith.zig", "func.zig", + "memref.zig", # "math.zig", # "scf.zig", # "tensor.zig", diff --git a/mlir/dialects/func.zig b/mlir/dialects/func.zig index 178aab75..173b8381 100644 --- a/mlir/dialects/func.zig +++ b/mlir/dialects/func.zig @@ -16,7 +16,7 @@ const FuncOpArgs = struct { results_attributes: ?[]const *const mlir.Attribute = null, block: *mlir.Block, location: *const mlir.Location, - no_inline: bool = true, + no_inline: bool = false, visibility: Visibility = .public, }; @@ -24,7 +24,8 @@ pub fn func(ctx: *mlir.Context, args: FuncOpArgs) *mlir.Operation { var args_buffer: stdx.BoundedArray(*const mlir.Type, 1024) = .{}; var results_buffer: stdx.BoundedArray(*const mlir.Type, 32) = .{}; - var attr_tuples_buffer = stdx.BoundedArray(mlir.NamedAttribute, 16).fromSlice(&.{ + var attr_tuples_buffer: stdx.BoundedArray(mlir.NamedAttribute, 16) = .{}; + attr_tuples_buffer.appendSliceAssumeCapacity(&.{ .named(ctx, "sym_name", mlir.stringAttribute(ctx, args.name)), .named(ctx, "sym_visibility", mlir.stringAttribute(ctx, @tagName(args.visibility))), .named(ctx, "function_type", mlir.typeAttribute(mlir.functionType( @@ -45,7 +46,7 @@ pub fn func(ctx: *mlir.Context, args: FuncOpArgs) *mlir.Operation { break :results results_buffer.constSlice(); }, ))), - }) catch unreachable; + }); if (args.args_attributes) |args_attributes| { attr_tuples_buffer.appendAssumeCapacity(.named(ctx, "arg_attrs", mlir.arrayAttribute(ctx, args_attributes))); @@ -57,7 +58,7 @@ pub fn func(ctx: *mlir.Context, args: FuncOpArgs) *mlir.Operation { attr_tuples_buffer.appendAssumeCapacity(.named(ctx, "no_inline", mlir.unitAttribute(ctx))); } - return mlir.Operation.make(ctx, "func.func", .{ + return .make(ctx, "func.func", .{ .blocks = &.{args.block}, .attributes = attr_tuples_buffer.constSlice(), .location = args.location, diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 83ea5669..feb9d49f 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -680,117 +680,163 @@ pub fn binaryOp(comptime op_name: []const u8) type { // }); // } -// pub const CustomCallOpts = struct { -// pub const ApiVersion = enum(i32) { -// original = 1, -// status_returning = 2, -// status_returning_unified = 3, -// typed_ffi = 4, -// }; +pub const CustomCallOpts = struct { + pub const ApiVersion = enum(i32) { + original = 1, + status_returning = 2, + status_returning_unified = 3, + typed_ffi = 4, + }; -// call_target_name: []const u8, -// has_side_effect: bool = false, -// backend_config: ?*const mlir.Attribute, -// operand_layouts: ?[]const []const usize = null, -// result_layouts: ?[]const []const usize = null, -// output_operand_aliases: ?[]const i64 = null, -// additional_attributes: ?[]const mlir.AttrTuple = &.{}, -// api_version: ApiVersion, -// }; + pub const BackendConfig = union(ApiVersion) { + original: *const mlir.StringAttribute, + status_returning: *const mlir.StringAttribute, + status_returning_unified: *const mlir.StringAttribute, + typed_ffi: *const mlir.DictionaryAttribute, + }; -// pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, opts: CustomCallOpts, result_types: []const *const mlir.Type, location: *const mlir.Location) *mlir.Operation { -// const MAX_OPERANDS = 64; -// const MAX_RESULTS = 16; + call_target_name: []const u8, + has_side_effect: bool = false, + api_version: ?ApiVersion, + backend_config: ?BackendConfig = null, + operand_layouts: ?[]const []const usize = null, + result_layouts: ?[]const []const usize = null, + output_operand_aliases: ?[]const i64 = null, + additional_attributes: []const mlir.NamedAttribute = &.{}, +}; -// const backend_config = opts.backend_config orelse mlir.Attribute.string(ctx, ""); -// if (@intFromEnum(opts.api_version) < @intFromEnum(CustomCallOpts.ApiVersion.typed_ffi)) { -// stdx.debug.assert( -// backend_config.isA(mlir.StringAttribute), -// "API version < 4 requires a string as backend_config, got {}", -// .{backend_config}, -// ); -// } else { -// stdx.debug.assert( -// backend_config.isA(mlir.DictionaryAttribute), -// "API version >= 4 requires a dictionary as backend_config, got {}", -// .{backend_config}, -// ); -// } +pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, opts: CustomCallOpts, result_types: []const *const mlir.Type, location: *const mlir.Location) *mlir.Operation { + const MAX_OPERANDS = 64; + _ = MAX_OPERANDS; // autofix + const MAX_RESULTS = 16; -// var attrs: stdx.BoundedArray(mlir.AttrTuple, 32) = .{}; -// attrs.appendSliceAssumeCapacity(&[_]mlir.AttrTuple{ -// .{ "api_version", .int(ctx, .i32, @intFromEnum(opts.api_version)) }, -// .{ "call_target_name", .string(ctx, opts.call_target_name) }, -// .{ "has_side_effect", .boolean(ctx, opts.has_side_effect) }, -// .{ "backend_config", backend_config }, -// }); + var attrs: stdx.BoundedArray(mlir.NamedAttribute, 32) = .{}; + attrs.appendSliceAssumeCapacity(&.{ + .named(ctx, "call_target_name", mlir.stringAttribute(ctx, opts.call_target_name)), + .named(ctx, "has_side_effect", mlir.boolAttribute(ctx, opts.has_side_effect)), + }); + attrs.appendSliceAssumeCapacity(opts.additional_attributes); -// { -// var output_operand_aliases: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; -// for (opts.output_operand_aliases) |alias| { -// output_operand_aliases.appendAssumeCapacity( -// OutputOperandAliasAttribute.init(ctx, &.{}, alias, &.{}).asAttr(), -// ); -// } -// attrs.appendAssumeCapacity(.{ "output_operand_aliases", .array(ctx, output_operand_aliases.constSlice()) }); -// } + if (opts.api_version orelse opts.backend_config) |v| { + attrs.appendAssumeCapacity(.named(ctx, "api_version", mlir.integerAttribute(ctx, .i32, @intFromEnum(v)))); + } + if (opts.backend_config) |bc| { + switch (bc) { + else => |v| attrs.appendAssumeCapacity(.named(ctx, "backend_config", @ptrCast(v))), + } + } -// const MINOR_TO_MAJOR = blk: { -// const MAX_RANK = 8; -// var ret: [MAX_RANK]usize = undefined; -// for (0..MAX_RANK) |i| { -// ret[i] = @intCast(MAX_RANK - i - 1); -// } -// break :blk ret; -// }; + { + var output_operand_aliases: stdx.BoundedArray(*const mlir.Attribute, MAX_RESULTS) = .{}; + for (opts.output_operand_aliases) |alias| { + _ = alias; // autofix + // output_operand_aliases.appendAssumeCapacity( + // OutputOperandAliasAttribute.init(ctx, &.{}, alias, &.{}).asAttr(), + // ); + } + attrs.appendAssumeCapacity(.named(ctx, "output_operand_aliases", mlir.arrayAttribute(ctx, output_operand_aliases.constSlice()))); + } -// if (opts.operand_layouts) |layouts| { -// var operand_layouts: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; -// for (layouts) |ol| { -// operand_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); -// } -// attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); -// } else { -// const operand_layouts = blk: { -// var ret: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; -// for (inputs) |input| { -// const ranked_type = input.getType().as(mlir.RankedTensorType).?; -// const ol = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_type.getRank() ..]; -// ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); -// } -// break :blk ret; -// }; -// attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); -// } + // var attrs: stdx.BoundedArray(mlir.AttrTuple, 32) = .{}; + // attrs.appendSliceAssumeCapacity(&[_]mlir.AttrTuple{ + // .{ "api_version", .int(ctx, .i32, @intFromEnum(opts.api_version)) }, + // .{ "call_target_name", .string(ctx, opts.call_target_name) }, + // .{ "has_side_effect", .boolean(ctx, opts.has_side_effect) }, + // .{ "backend_config", backend_config }, + // }); -// if (opts.result_layouts) |layouts| { -// var result_layouts: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; -// for (layouts) |rl| { -// result_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); -// } -// attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); -// } else { -// const result_layouts = blk: { -// var ret: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; -// for (res_types) |t| { -// const ranked_t = t.as(mlir.RankedTensorType).?; -// const rl = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_t.getRank() ..]; -// ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); -// } -// break :blk ret; -// }; -// attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); -// } + // { + // var output_operand_aliases: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; + // for (opts.output_operand_aliases) |alias| { + // output_operand_aliases.appendAssumeCapacity( + // OutputOperandAliasAttribute.init(ctx, &.{}, alias, &.{}).asAttr(), + // ); + // } + // attrs.appendAssumeCapacity(.{ "output_operand_aliases", .array(ctx, output_operand_aliases.constSlice()) }); + // } -// attrs.appendSlice(opts.additional_attributes) catch @panic("Too many additional_attributes"); + // const MINOR_TO_MAJOR = blk: { + // const MAX_RANK = 8; + // var ret: [MAX_RANK]usize = undefined; + // for (0..MAX_RANK) |i| { + // ret[i] = @intCast(MAX_RANK - i - 1); + // } + // break :blk ret; + // }; -// return mlir.Operation.make(ctx, "stablehlo.custom_call", .{ -// .operands = inputs, -// .results = res_types, -// .attributes = attrs.constSlice(), -// .location = location, -// }); -// } + // if (opts.operand_layouts) |layouts| { + // var operand_layouts: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; + // for (layouts) |ol| { + // operand_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); + // } + // attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); + // } else { + // const operand_layouts = blk: { + // var ret: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; + // for (inputs) |input| { + // const ranked_type = input.getType().as(mlir.RankedTensorType).?; + // const ol = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_type.getRank() ..]; + // ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); + // } + // break :blk ret; + // }; + // attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); + // } + + // if (opts.result_layouts) |layouts| { + // var result_layouts: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; + // for (layouts) |rl| { + // result_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); + // } + // attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); + // } else { + // const result_layouts = blk: { + // var ret: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; + // for (res_types) |t| { + // const ranked_t = t.as(mlir.RankedTensorType).?; + // const rl = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_t.getRank() ..]; + // ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); + // } + // break :blk ret; + // }; + // attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); + // } + + // attrs.appendSlice(opts.additional_attributes) catch @panic("Too many additional_attributes"); + + return mlir.Operation.make(ctx, "stablehlo.custom_call", .{ + .operands = .{ .flat = inputs }, + .results = .{ .flat = result_types }, + .attributes = attrs.constSlice(), + .location = location, + }); +} + +pub fn createBuffer(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + const result_type = mlir.integerType(ctx, .i64); + return custom_call(ctx, &.{value}, .{ + .call_target_name = "CreateBuffer", + .api_version = .typed_ffi, + }, &.{result_type}, location); +} + +pub fn pin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + const result_type = mlir.integerType(ctx, .i64); + const shape = value.type_().as(mlir.ShapedType).?; + _ = shape; // autofix + return custom_call(ctx, &.{value}, .{ + .call_target_name = "Pin", + .api_version = .typed_ffi, + }, &.{result_type}, location); +} + +pub fn unpin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + const result_type = mlir.integerType(ctx, .i64); + return custom_call(ctx, &.{value}, .{ + .call_target_name = "Unpin", + .api_version = .typed_ffi, + }, &.{result_type}, location); +} // pub const DotDimensionNumbersAttribute = struct { // _inner: c.MlirAttribute, diff --git a/mlir/main.zig b/mlir/main.zig index 3963a9ae..4bf94117 100644 --- a/mlir/main.zig +++ b/mlir/main.zig @@ -10,7 +10,7 @@ pub fn main() !void { // const handle = mlir.DialectHandle.fromString("func"); - inline for (.{ "func", "stablehlo" }) |d| { + inline for (.{ "memref", "func", "stablehlo" }) |d| { registry.registerDialect(d); } @@ -56,7 +56,7 @@ pub fn main() !void { const tensor_i32 = mlir.rankedTensorType(&.{ 4096, 4096 }, mlir.integerType(ctx, .i32)); - module.body().appendOwnedOperation(dialects.func.func(ctx, .{ + _ = dialects.func.func(ctx, .{ .name = "pouet", .visibility = .private, .block = blk: { @@ -67,7 +67,7 @@ pub fn main() !void { break :blk body; }, .location = .unknown(ctx), - })); + }).appendTo(module.body()); std.debug.print(">>>>>>\n{f}\n", .{module.operation().fmt(.{ .print_generic_op_form = false })}); std.debug.print(">>>>>>\n{f}\n", .{module.operation().fmt(.{ .print_generic_op_form = true })}); diff --git a/mlir/mlir.h b/mlir/mlir.h index 4ad1523e..633e11f6 100644 --- a/mlir/mlir.h +++ b/mlir/mlir.h @@ -2,8 +2,7 @@ #include #include #include -#include -#include +#include #include #include #include From 66bc168de2919c20a596417956726fcefb160894 Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Thu, 28 Aug 2025 02:22:01 +0200 Subject: [PATCH 05/37] WIP --- mlir/dialects/stablehlo/stablehlo.zig | 97 ++++++++++++--------------- mlir/registry.zig | 4 ++ 2 files changed, 48 insertions(+), 53 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index feb9d49f..41b99220 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -718,43 +718,30 @@ pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, opts: attrs.appendSliceAssumeCapacity(opts.additional_attributes); if (opts.api_version orelse opts.backend_config) |v| { - attrs.appendAssumeCapacity(.named(ctx, "api_version", mlir.integerAttribute(ctx, .i32, @intFromEnum(v)))); + attrs.appendAssumeCapacity( + .named(ctx, "api_version", mlir.integerAttribute(ctx, .i32, @intFromEnum(v))), + ); } - if (opts.backend_config) |bc| { - switch (bc) { - else => |v| attrs.appendAssumeCapacity(.named(ctx, "backend_config", @ptrCast(v))), + if (opts.backend_config) |backend_config| { + switch (backend_config) { + inline else => |v| attrs.appendAssumeCapacity( + .named(ctx, "backend_config", @ptrCast(v)), + ), } } - { - var output_operand_aliases: stdx.BoundedArray(*const mlir.Attribute, MAX_RESULTS) = .{}; - for (opts.output_operand_aliases) |alias| { - _ = alias; // autofix - // output_operand_aliases.appendAssumeCapacity( - // OutputOperandAliasAttribute.init(ctx, &.{}, alias, &.{}).asAttr(), - // ); + if (opts.output_operand_aliases) |output_operand_aliases| { + var buffer: stdx.BoundedArray(*const mlir.Attribute, MAX_RESULTS) = .{}; + for (output_operand_aliases) |alias| { + buffer.appendAssumeCapacity( + outputOperandAliasAttribute.init(ctx, .{ .operand_index = alias }), + ); } - attrs.appendAssumeCapacity(.named(ctx, "output_operand_aliases", mlir.arrayAttribute(ctx, output_operand_aliases.constSlice()))); + attrs.appendAssumeCapacity( + .named(ctx, "output_operand_aliases", mlir.arrayAttribute(ctx, buffer.constSlice())), + ); } - // var attrs: stdx.BoundedArray(mlir.AttrTuple, 32) = .{}; - // attrs.appendSliceAssumeCapacity(&[_]mlir.AttrTuple{ - // .{ "api_version", .int(ctx, .i32, @intFromEnum(opts.api_version)) }, - // .{ "call_target_name", .string(ctx, opts.call_target_name) }, - // .{ "has_side_effect", .boolean(ctx, opts.has_side_effect) }, - // .{ "backend_config", backend_config }, - // }); - - // { - // var output_operand_aliases: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; - // for (opts.output_operand_aliases) |alias| { - // output_operand_aliases.appendAssumeCapacity( - // OutputOperandAliasAttribute.init(ctx, &.{}, alias, &.{}).asAttr(), - // ); - // } - // attrs.appendAssumeCapacity(.{ "output_operand_aliases", .array(ctx, output_operand_aliases.constSlice()) }); - // } - // const MINOR_TO_MAJOR = blk: { // const MAX_RANK = 8; // var ret: [MAX_RANK]usize = undefined; @@ -802,8 +789,6 @@ pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, opts: // attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); // } - // attrs.appendSlice(opts.additional_attributes) catch @panic("Too many additional_attributes"); - return mlir.Operation.make(ctx, "stablehlo.custom_call", .{ .operands = .{ .flat = inputs }, .results = .{ .flat = result_types }, @@ -1067,29 +1052,35 @@ pub fn unpin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir // } // }; -// pub const OutputOperandAliasAttribute = struct { -// _inner: c.MlirAttribute, +pub const OutputOperandAliasAttribute = opaque { + const M = mlir.Methods(OutputOperandAliasAttribute, c.MlirAttribute); -// pub const isAFn = c.stablehloAttributeIsAOutputOperandAlias; -// pub const asAttr = mlir.Attribute.fromAny(OutputOperandAliasAttribute); -// pub const eql = mlir.Attribute.eqlAny(OutputOperandAliasAttribute); + pub const isAFn = c.stablehloAttributeIsAOutputOperandAlias; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); -// pub fn init( -// ctx: *mlir.Context, -// output_tuple_indices: []const i64, -// operand_index: i64, -// operand_tuple_indices: []const i64, -// ) OutputOperandAliasAttribute { -// return .{ ._inner = c.stablehloOutputOperandAliasGet( -// ctx._inner, -// @intCast(output_tuple_indices.len), -// output_tuple_indices.ptr, -// @intCast(operand_index), -// @intCast(operand_tuple_indices.len), -// operand_tuple_indices.ptr, -// ) }; -// } -// }; + pub const InitArgs = struct { + output_tuple_indices: []const i64 = &.{}, + operand_index: i64, + operand_tuple_indices: []const i64 = &.{}, + }; + + pub fn init(ctx: *mlir.Context, args: InitArgs) *OutputOperandAliasAttribute { + return @ptrCast(c.stablehloOutputOperandAliasGet( + ctx.ptr(), + @intCast(args.output_tuple_indices.len), + @ptrCast(args.output_tuple_indices), + @intCast(args.operand_index), + @intCast(args.operand_tuple_indices.len), + @ptrCast(args.operand_tuple_indices), + ).ptr); + } +}; + +pub fn outputOperandAliasAttribute(ctx: *mlir.Context, args: OutputOperandAliasAttribute.InitArgs) *mlir.Attribute { + return @ptrCast(OutputOperandAliasAttribute.init(ctx, args)); +} // pub const PrecisionAttribute = struct { // _inner: c.MlirAttribute, diff --git a/mlir/registry.zig b/mlir/registry.zig index 6c999b83..c08a2754 100644 --- a/mlir/registry.zig +++ b/mlir/registry.zig @@ -728,6 +728,10 @@ pub const DictionaryAttribute = opaque { } }; +pub fn dictionaryAttribute(ctx: *Context, attributes: []const NamedAttribute) *const Attribute { + return @ptrCast(DictionaryAttribute.init(ctx, attributes)); +} + pub const Value = opaque { const M = Methods(Value, c.MlirValue); From 96760c0bc3a78bac89a6638dd0108eaf0607c3ac Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Thu, 28 Aug 2025 09:29:37 +0200 Subject: [PATCH 06/37] WIP --- mlir/dialects/BUILD.bazel | 1 - mlir/dialects/func.zig | 2 +- mlir/dialects/stablehlo/stablehlo.zig | 7 ++-- mlir/registry.zig | 47 +++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/mlir/dialects/BUILD.bazel b/mlir/dialects/BUILD.bazel index 9c4bd6b1..34400ca9 100644 --- a/mlir/dialects/BUILD.bazel +++ b/mlir/dialects/BUILD.bazel @@ -5,7 +5,6 @@ zig_library( srcs = [ # "arith.zig", "func.zig", - "memref.zig", # "math.zig", # "scf.zig", # "tensor.zig", diff --git a/mlir/dialects/func.zig b/mlir/dialects/func.zig index 173b8381..d316f160 100644 --- a/mlir/dialects/func.zig +++ b/mlir/dialects/func.zig @@ -58,7 +58,7 @@ pub fn func(ctx: *mlir.Context, args: FuncOpArgs) *mlir.Operation { attr_tuples_buffer.appendAssumeCapacity(.named(ctx, "no_inline", mlir.unitAttribute(ctx))); } - return .make(ctx, "func.func", .{ + return mlir.Operation.make(ctx, "func.func", .{ .blocks = &.{args.block}, .attributes = attr_tuples_buffer.constSlice(), .location = args.location, diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 41b99220..11b209b2 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -797,9 +797,8 @@ pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, opts: }); } -pub fn createBuffer(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { - const result_type = mlir.integerType(ctx, .i64); - return custom_call(ctx, &.{value}, .{ +pub fn createBuffer(ctx: *mlir.Context, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { + return custom_call(ctx, &.{}, .{ .call_target_name = "CreateBuffer", .api_version = .typed_ffi, }, &.{result_type}, location); @@ -812,7 +811,7 @@ pub fn pin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.L return custom_call(ctx, &.{value}, .{ .call_target_name = "Pin", .api_version = .typed_ffi, - }, &.{result_type}, location); + }, &.{ result_type}, location); } pub fn unpin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { diff --git a/mlir/registry.zig b/mlir/registry.zig index c08a2754..cd12b6b0 100644 --- a/mlir/registry.zig +++ b/mlir/registry.zig @@ -1178,6 +1178,8 @@ pub const Region = opaque { pub const ShapedType = opaque { const M = Methods(ShapedType, c.MlirType); + pub const MAX_RANK = 16; + pub const isAFn = c.mlirTypeIsAShaped; pub const ptr = M.ptr; pub const eql = M.eql(c.mlirTypeEqual); @@ -1214,6 +1216,17 @@ pub const RankedTensorType = opaque { ).ptr); } + pub fn fromShaped(other: *const ShapedType) *const RankedTensorType { + var dims: stdx.BoundedArray(i64, ShapedType.MAX_RANK) = .{}; + for (other.rank()) |i| { + dims.appendAssumeCapacity(other.dimension(i)); + } + return init( + other.elementType(), + dims.constSlice(), + ); + } + pub fn elementType(self: *const RankedTensorType) *const Type { return @ptrCast(c.mlirShapedTypeGetElementType(self.ptr()).ptr); } @@ -1404,3 +1417,37 @@ pub const UnitAttribute = opaque { pub fn unitAttribute(ctx: *Context) *const Attribute { return @ptrCast(UnitAttribute.get(ctx)); } + +pub const MemRefType = opaque { + const M = Methods(MemRefType, c.MlirType); + + pub const isAFn = c.mlirTypeIsAInteger; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + pub fn init(element_type: *const Type, shape: []const i64, layout: ?*const Attribute, memory_space: ?*Attribute) *const MemRefType { + return @ptrCast(c.mlirMemRefTypeGet(element_type, shape.len, @ptrCast(shape), layout, memory_space).ptr); + } + + pub fn fromShaped(other: *const ShapedType) *const MemRefType { + var dims: stdx.BoundedArray(i64, ShapedType.MAX_RANK) = .{}; + for (other.rank()) |i| { + dims.appendAssumeCapacity(other.dimension(i)); + } + return init( + other.elementType(), + dims.constSlice(), + null, + null, + ); + } + + pub fn shaped(self: *const MemRefType) *const ShapedType { + return @ptrCast(self); + } +}; + +pub fn memRefType(element_type: *const Type, shape: []const i64, layout: ?*const Attribute, memory_space: ?*const Attribute) *const Type { + return @ptrCast(MemRefType.init(element_type, shape, layout, memory_space)); +} From 82ae988c665fddf1ebdc58977912b2be4c117219 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 09:54:08 +0200 Subject: [PATCH 07/37] stablehlo: bound cholesky --- mlir/dialects/stablehlo/stablehlo.zig | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 11b209b2..f4009c08 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -88,16 +88,16 @@ pub fn binaryOp(comptime op_name: []const u8) type { }; } -// pub fn cholesky(ctx: *mlir.Context, value: *const mlir.Value, lower: bool, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.cholesky", .{ -// .operands = .{ .flat = &.{value} }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "lower", mlir.integerAttribute(ctx, .i1, @intFromBool(lower)) }, -// }, -// .location = location, -// }); -// } +pub fn cholesky(ctx: *mlir.Context, value: *const mlir.Value, lower: bool, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.cholesky", .{ + .operands = .{ .flat = &.{value} }, + .result_type_inference = true, + .attributes = &.{ + mlir.NamedAttribute.named(ctx, "lower", mlir.integerAttribute(ctx, .i1, @intFromBool(lower))), + }, + .location = location, + }); +} // pub fn clamp(ctx: *mlir.Context, min: *const mlir.Value, value: *const mlir.Value, max: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { // return mlir.Operation.make(ctx, "stablehlo.clamp", .{ From e43d42c50d3b84c412b8d628e00d76cb404ebfff Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 09:56:14 +0200 Subject: [PATCH 08/37] stablehlo: bound clamp --- mlir/dialects/stablehlo/stablehlo.zig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index f4009c08..383c36ff 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -99,13 +99,13 @@ pub fn cholesky(ctx: *mlir.Context, value: *const mlir.Value, lower: bool, locat }); } -// pub fn clamp(ctx: *mlir.Context, min: *const mlir.Value, value: *const mlir.Value, max: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.clamp", .{ -// .operands = .{ .flat = &.{ min, value, max } }, -// .result_type_inference = true, -// .location = location, -// }); -// } +pub fn clamp(ctx: *mlir.Context, min: *const mlir.Value, value: *const mlir.Value, max: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.clamp", .{ + .operands = .{ .flat = &.{ min, value, max } }, + .result_type_inference = true, + .location = location, + }); +} // pub const DotPrecision = union(enum) { // fast, From d83e68a6cfb65bc802b1375b851b4d3999810e09 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 10:25:27 +0200 Subject: [PATCH 09/37] stablehlo: bound constant --- mlir/dialects/stablehlo/stablehlo.zig | 32 +++++++++++++-------------- mlir/registry.zig | 4 ++-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 383c36ff..35fc701f 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -205,22 +205,22 @@ pub fn clamp(ctx: *mlir.Context, min: *const mlir.Value, value: *const mlir.Valu // }); // } -// pub fn constant( -// ctx: *mlir.Context, -// dims: []const i64, -// elem_type: mlir.DenseElementsAttributeTypes, -// raw_bytes: []const u8, -// location: *const mlir.Location, -// ) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.constant", .{ -// .operands = .{ .flat = &.{} }, -// .results = &.{.tensor(dims, elem_type.mlirType(ctx))}, -// .attributes = &.{ -// .{ "value", .denseElementsFromBytes(ctx, dims, elem_type, raw_bytes) }, -// }, -// .location = location, -// }); -// } +pub fn constant( + ctx: *mlir.Context, + dims: []const i64, + elem_type: *const mlir.Type, + raw_bytes: []const u8, + location: *const mlir.Location, +) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.constant", .{ + .operands = .{ .flat = &.{} }, + .results = .{ .flat = &.{mlir.rankedTensorType(dims, elem_type)} }, + .attributes = &.{ + mlir.NamedAttribute.named(ctx, "value", mlir.denseElementsAttribute(mlir.RankedTensorType.init(dims, elem_type).shaped(), raw_bytes)), + }, + .location = location, + }); +} // pub fn broadcast_in_dim(ctx: *mlir.Context, operand: *const mlir.Value, dims: []const i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { // return mlir.Operation.make(ctx, "stablehlo.broadcast_in_dim", .{ diff --git a/mlir/registry.zig b/mlir/registry.zig index cd12b6b0..78ec4698 100644 --- a/mlir/registry.zig +++ b/mlir/registry.zig @@ -1244,8 +1244,8 @@ pub const RankedTensorType = opaque { } }; -pub fn rankedTensorType(dimensions: []const i64, elemType: *const Type) *const Type { - return @ptrCast(RankedTensorType.init(dimensions, elemType)); +pub fn rankedTensorType(dimensions: []const i64, elem_type: *const Type) *const Type { + return @ptrCast(RankedTensorType.init(dimensions, elem_type)); } pub const DenseElementsAttribute = opaque { From bcb37bec363f4e70fa8a0b2198815d9c3248e5bc Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 10:37:59 +0200 Subject: [PATCH 10/37] stablehlo: bound broadcast_in_dim --- mlir/dialects/stablehlo/stablehlo.zig | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 35fc701f..d74b06f5 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -222,16 +222,22 @@ pub fn constant( }); } -// pub fn broadcast_in_dim(ctx: *mlir.Context, operand: *const mlir.Value, dims: []const i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.broadcast_in_dim", .{ -// .operands = .{ .flat = &.{operand} }, -// .results = .{ .flat = &.{result_type} }, -// .attributes = &.{ -// .{ "broadcast_dimensions", mlir.denseArrayAttribute(ctx, .i64, dims) }, -// }, -// .location = location, -// }); -// } +pub fn broadcast_in_dim( + ctx: *mlir.Context, + operand: *const mlir.Value, + dims: []const i64, + result_type: *const mlir.Type, + location: *const mlir.Location, +) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.broadcast_in_dim", .{ + .operands = .{ .flat = &.{operand} }, + .results = .{ .flat = &.{result_type} }, + .attributes = &.{ + mlir.NamedAttribute.named(ctx, "broadcast_dimensions", mlir.denseArrayAttribute(ctx, .i64, dims)), + }, + .location = location, + }); +} // pub fn transpose(ctx: *mlir.Context, value: *const mlir.Value, result_type: *const mlir.Type, location: *const mlir.Location, opts: struct { permutation: []const i64 }) *mlir.Operation { // return mlir.Operation.make(ctx, "stablehlo.transpose", .{ From b01d43c411de743c0f8a562e55fad8ffaa6a76f0 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 10:40:49 +0200 Subject: [PATCH 11/37] stablehlo: bound transpose --- mlir/dialects/stablehlo/stablehlo.zig | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index d74b06f5..3e259b40 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -239,16 +239,22 @@ pub fn broadcast_in_dim( }); } -// pub fn transpose(ctx: *mlir.Context, value: *const mlir.Value, result_type: *const mlir.Type, location: *const mlir.Location, opts: struct { permutation: []const i64 }) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.transpose", .{ -// .operands = .{ .flat = &.{value} }, -// .results = .{ .flat = &.{result_type} }, -// .attributes = &.{ -// .{ "permutation", mlir.denseArrayAttribute(ctx, .i64, opts.permutation) }, -// }, -// .location = location, -// }); -// } +pub fn transpose( + ctx: *mlir.Context, + value: *const mlir.Value, + result_type: *const mlir.Type, + location: *const mlir.Location, + opts: struct { permutation: []const i64 }, +) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.transpose", .{ + .operands = .{ .flat = &.{value} }, + .results = .{ .flat = &.{result_type} }, + .attributes = &.{ + mlir.NamedAttribute.named(ctx, "permutation", mlir.denseArrayAttribute(ctx, .i64, opts.permutation)), + }, + .location = location, + }); +} // pub fn slice(ctx: *mlir.Context, operand: *const mlir.Value, start_indices: []const i64, limit_indices: []const i64, strides: []const i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { // return mlir.Operation.make(ctx, "stablehlo.slice", .{ From 0ea50b2880d5d9ff37cec125f8c6403e00497d63 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 10:44:49 +0200 Subject: [PATCH 12/37] stablehlo: bound slice --- mlir/dialects/stablehlo/stablehlo.zig | 32 +++++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 3e259b40..2f25edcf 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -256,18 +256,26 @@ pub fn transpose( }); } -// pub fn slice(ctx: *mlir.Context, operand: *const mlir.Value, start_indices: []const i64, limit_indices: []const i64, strides: []const i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.slice", .{ -// .operands = .{ .flat = &.{operand} }, -// .results = .{ .flat = &.{result_type} }, -// .attributes = &.{ -// .{ "start_indices", mlir.denseArrayAttribute(ctx, .i64, start_indices) }, -// .{ "limit_indices", mlir.denseArrayAttribute(ctx, .i64, limit_indices) }, -// .{ "strides", mlir.denseArrayAttribute(ctx, .i64, strides) }, -// }, -// .location = location, -// }); -// } +pub fn slice( + ctx: *mlir.Context, + operand: *const mlir.Value, + start_indices: []const i64, + limit_indices: []const i64, + strides: []const i64, + result_type: *const mlir.Type, + location: *const mlir.Location, +) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.slice", .{ + .operands = .{ .flat = &.{operand} }, + .results = .{ .flat = &.{result_type} }, + .attributes = &.{ + mlir.NamedAttribute.named(ctx, "start_indices", mlir.denseArrayAttribute(ctx, .i64, start_indices)), + mlir.NamedAttribute.named(ctx, "limit_indices", mlir.denseArrayAttribute(ctx, .i64, limit_indices)), + mlir.NamedAttribute.named(ctx, "strides", mlir.denseArrayAttribute(ctx, .i64, strides)), + }, + .location = location, + }); +} // pub fn concatenate(ctx: *mlir.Context, inputs: []const *const mlir.Value, dimension: i64, location: *const mlir.Location) *mlir.Operation { // return mlir.Operation.make(ctx, "stablehlo.concatenate", .{ From fbb7e94740364d054a31350671093381e1bceb31 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 10:50:13 +0200 Subject: [PATCH 13/37] stablehlo: bound concatenate --- mlir/dialects/stablehlo/stablehlo.zig | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 2f25edcf..3713faaa 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -277,16 +277,16 @@ pub fn slice( }); } -// pub fn concatenate(ctx: *mlir.Context, inputs: []const *const mlir.Value, dimension: i64, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.concatenate", .{ -// .operands = inputs, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "dimension", mlir.integerAttribute(ctx, .i64, dimension) }, -// }, -// .location = location, -// }); -// } +pub fn concatenate(ctx: *mlir.Context, inputs: []const *const mlir.Value, dimension: i64, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.concatenate", .{ + .operands = .{ .flat = inputs }, + .result_type_inference = true, + .attributes = &.{ + mlir.NamedAttribute.named(ctx, "dimension", mlir.integerAttribute(ctx, .i64, dimension)), + }, + .location = location, + }); +} // pub fn select(ctx: *mlir.Context, condition: *const mlir.Value, then: *const mlir.Value, else_: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { // return mlir.Operation.make(ctx, "stablehlo.select", .{ From 7dc6055e0583744461d8416a2b906f9cb96446dd Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 11:19:25 +0200 Subject: [PATCH 14/37] stablehlo: bound select --- mlir/dialects/stablehlo/stablehlo.zig | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 3713faaa..f26727b9 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -288,13 +288,19 @@ pub fn concatenate(ctx: *mlir.Context, inputs: []const *const mlir.Value, dimens }); } -// pub fn select(ctx: *mlir.Context, condition: *const mlir.Value, then: *const mlir.Value, else_: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.select", .{ -// .operands = .{ .flat = &.{ condition, then, else_ } }, -// .results = &.{then.getType()}, -// .location = location, -// }); -// } +pub fn select( + ctx: *mlir.Context, + condition: *const mlir.Value, + then: *const mlir.Value, + else_: *const mlir.Value, + location: *const mlir.Location, +) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.select", .{ + .operands = .{ .flat = &.{ condition, then, else_ } }, + .results = .{ .flat = &.{then.type_()} }, + .location = location, + }); +} // pub fn gather( // ctx: *mlir.Context, From 750a1639e269fe4fdf9a6024e9cd880ec71936e0 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 11:22:39 +0200 Subject: [PATCH 15/37] stablehlo: bound iota --- mlir/dialects/stablehlo/stablehlo.zig | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index f26727b9..79b8c2fa 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -402,16 +402,16 @@ pub fn select( // ); // } -// pub fn iota(ctx: *mlir.Context, dimension: i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.iota", .{ -// .operands = .{ .flat = &.{} }, -// .results = .{ .flat = &.{result_type} }, -// .attributes = &.{ -// .{ "iota_dimension", mlir.integerType(ctx, .i64, dimension) }, -// }, -// .location = location, -// }); -// } +pub fn iota(ctx: *mlir.Context, dimension: i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.iota", .{ + .operands = .{ .flat = &.{} }, + .results = .{ .flat = &.{result_type} }, + .attributes = &.{ + mlir.NamedAttribute.named(ctx, "iota_dimension", mlir.integerAttribute(ctx, .i64, dimension)), + }, + .location = location, + }); +} // pub fn reverse(ctx: *mlir.Context, operand: *const mlir.Value, dimensions: []const i64, location: *const mlir.Location) *mlir.Operation { // const result_type = operand.getType(); From 41678aee1fb1283207e614a8791ccf87ff231027 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 11:22:49 +0200 Subject: [PATCH 16/37] stablehlo: format typo --- mlir/dialects/stablehlo/stablehlo.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 79b8c2fa..e66d3a94 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -837,7 +837,7 @@ pub fn pin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.L return custom_call(ctx, &.{value}, .{ .call_target_name = "Pin", .api_version = .typed_ffi, - }, &.{ result_type}, location); + }, &.{result_type}, location); } pub fn unpin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { From 45f99a2a6d43d5cce2c044694b18d8f59f8e9bef Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 11:28:11 +0200 Subject: [PATCH 17/37] stablehlo: bound reverse --- mlir/dialects/stablehlo/stablehlo.zig | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index e66d3a94..15797beb 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -413,17 +413,16 @@ pub fn iota(ctx: *mlir.Context, dimension: i64, result_type: *const mlir.Type, l }); } -// pub fn reverse(ctx: *mlir.Context, operand: *const mlir.Value, dimensions: []const i64, location: *const mlir.Location) *mlir.Operation { -// const result_type = operand.getType(); -// return mlir.Operation.make(ctx, "stablehlo.reverse", .{ -// .operands = .{ .flat = &.{operand} }, -// .results = .{ .flat = &.{result_type} }, -// .attributes = &.{ -// .{ "dimensions", mlir.denseArrayAttribute(ctx, .i64, dimensions) }, -// }, -// .location = location, -// }); -// } +pub fn reverse(ctx: *mlir.Context, operand: *const mlir.Value, dimensions: []const i64, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.reverse", .{ + .operands = .{ .flat = &.{operand} }, + .results = .{ .flat = &.{operand.type_()} }, + .attributes = &.{ + mlir.NamedAttribute.named(ctx, "dimensions", mlir.denseArrayAttribute(ctx, .i64, dimensions)), + }, + .location = location, + }); +} // pub fn compare(ctx: *mlir.Context, lhs: *const mlir.Value, rhs: *const mlir.Value, comparison_direction: ComparisonDirection, compare_type: CompareType, location: *const mlir.Location) *mlir.Operation { // return mlir.Operation.make(ctx, "stablehlo.compare", .{ From 7bec82009f03efb9aa2f1ba72b350661afd175d8 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 11:42:58 +0200 Subject: [PATCH 18/37] stablehlo: simplify NamedAttribute usage --- mlir/dialects/stablehlo/stablehlo.zig | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 15797beb..63eb15c4 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -93,7 +93,7 @@ pub fn cholesky(ctx: *mlir.Context, value: *const mlir.Value, lower: bool, locat .operands = .{ .flat = &.{value} }, .result_type_inference = true, .attributes = &.{ - mlir.NamedAttribute.named(ctx, "lower", mlir.integerAttribute(ctx, .i1, @intFromBool(lower))), + .named(ctx, "lower", mlir.integerAttribute(ctx, .i1, @intFromBool(lower))), }, .location = location, }); @@ -216,7 +216,7 @@ pub fn constant( .operands = .{ .flat = &.{} }, .results = .{ .flat = &.{mlir.rankedTensorType(dims, elem_type)} }, .attributes = &.{ - mlir.NamedAttribute.named(ctx, "value", mlir.denseElementsAttribute(mlir.RankedTensorType.init(dims, elem_type).shaped(), raw_bytes)), + .named(ctx, "value", mlir.denseElementsAttribute(mlir.RankedTensorType.init(dims, elem_type).shaped(), raw_bytes)), }, .location = location, }); @@ -233,7 +233,7 @@ pub fn broadcast_in_dim( .operands = .{ .flat = &.{operand} }, .results = .{ .flat = &.{result_type} }, .attributes = &.{ - mlir.NamedAttribute.named(ctx, "broadcast_dimensions", mlir.denseArrayAttribute(ctx, .i64, dims)), + .named(ctx, "broadcast_dimensions", mlir.denseArrayAttribute(ctx, .i64, dims)), }, .location = location, }); @@ -250,7 +250,7 @@ pub fn transpose( .operands = .{ .flat = &.{value} }, .results = .{ .flat = &.{result_type} }, .attributes = &.{ - mlir.NamedAttribute.named(ctx, "permutation", mlir.denseArrayAttribute(ctx, .i64, opts.permutation)), + .named(ctx, "permutation", mlir.denseArrayAttribute(ctx, .i64, opts.permutation)), }, .location = location, }); @@ -269,9 +269,9 @@ pub fn slice( .operands = .{ .flat = &.{operand} }, .results = .{ .flat = &.{result_type} }, .attributes = &.{ - mlir.NamedAttribute.named(ctx, "start_indices", mlir.denseArrayAttribute(ctx, .i64, start_indices)), - mlir.NamedAttribute.named(ctx, "limit_indices", mlir.denseArrayAttribute(ctx, .i64, limit_indices)), - mlir.NamedAttribute.named(ctx, "strides", mlir.denseArrayAttribute(ctx, .i64, strides)), + .named(ctx, "start_indices", mlir.denseArrayAttribute(ctx, .i64, start_indices)), + .named(ctx, "limit_indices", mlir.denseArrayAttribute(ctx, .i64, limit_indices)), + .named(ctx, "strides", mlir.denseArrayAttribute(ctx, .i64, strides)), }, .location = location, }); @@ -282,7 +282,7 @@ pub fn concatenate(ctx: *mlir.Context, inputs: []const *const mlir.Value, dimens .operands = .{ .flat = inputs }, .result_type_inference = true, .attributes = &.{ - mlir.NamedAttribute.named(ctx, "dimension", mlir.integerAttribute(ctx, .i64, dimension)), + .named(ctx, "dimension", mlir.integerAttribute(ctx, .i64, dimension)), }, .location = location, }); @@ -407,7 +407,7 @@ pub fn iota(ctx: *mlir.Context, dimension: i64, result_type: *const mlir.Type, l .operands = .{ .flat = &.{} }, .results = .{ .flat = &.{result_type} }, .attributes = &.{ - mlir.NamedAttribute.named(ctx, "iota_dimension", mlir.integerAttribute(ctx, .i64, dimension)), + .named(ctx, "iota_dimension", mlir.integerAttribute(ctx, .i64, dimension)), }, .location = location, }); @@ -418,7 +418,7 @@ pub fn reverse(ctx: *mlir.Context, operand: *const mlir.Value, dimensions: []con .operands = .{ .flat = &.{operand} }, .results = .{ .flat = &.{operand.type_()} }, .attributes = &.{ - mlir.NamedAttribute.named(ctx, "dimensions", mlir.denseArrayAttribute(ctx, .i64, dimensions)), + .named(ctx, "dimensions", mlir.denseArrayAttribute(ctx, .i64, dimensions)), }, .location = location, }); From 7429a7e4c627538c650fdead7b233750dca25a7c Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 11:43:18 +0200 Subject: [PATCH 19/37] stablehlo: bound compare --- mlir/dialects/stablehlo/stablehlo.zig | 119 ++++++++++++++------------ 1 file changed, 66 insertions(+), 53 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 63eb15c4..46c40661 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -424,17 +424,24 @@ pub fn reverse(ctx: *mlir.Context, operand: *const mlir.Value, dimensions: []con }); } -// pub fn compare(ctx: *mlir.Context, lhs: *const mlir.Value, rhs: *const mlir.Value, comparison_direction: ComparisonDirection, compare_type: CompareType, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.compare", .{ -// .operands = .{ .flat = &.{ lhs, rhs } }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "comparison_direction", comparison_direction.asAttr() }, -// .{ "compare_type", compare_type.asAttr() }, -// }, -// .location = location, -// }); -// } +pub fn compare( + ctx: *mlir.Context, + lhs: *const mlir.Value, + rhs: *const mlir.Value, + comparison_direction: ComparisonDirection.Direction, + compare_type: CompareType.Type, + location: *const mlir.Location, +) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.compare", .{ + .operands = .{ .flat = &.{ lhs, rhs } }, + .result_type_inference = true, + .attributes = &.{ + .named(ctx, "comparison_direction", comparisonDirection(ctx, comparison_direction)), + .named(ctx, "compare_type", compareType(ctx, compare_type)), + }, + .location = location, + }); +} // pub fn reduce( // ctx: *mlir.Context, @@ -1130,57 +1137,63 @@ pub fn outputOperandAliasAttribute(ctx: *mlir.Context, args: OutputOperandAliasA // } // }; -// pub const ComparisonDirection = struct { -// _inner: c.MlirAttribute, +pub const ComparisonDirection = opaque { + const M = mlir.Methods(ComparisonDirection, c.MlirAttribute); -// pub const isAFn = c.stablehloAttributeIsAComparisonDirectionAttr; -// const Self = ComparisonDirection; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); + pub const isAFn = c.stablehloAttributeIsAComparisonDirectionAttr; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); -// pub const Direction = enum { -// EQ, -// NE, -// GE, -// GT, -// LE, -// LT, -// }; + pub const Direction = enum { + EQ, + NE, + GE, + GT, + LE, + LT, + }; -// pub fn init(ctx: *mlir.Context, value: Direction) Self { -// return .{ ._inner = c.stablehloComparisonDirectionAttrGet(ctx._inner, stringRef(@tagName(value))) }; -// } + pub fn init(ctx: *mlir.Context, value: Direction) *const ComparisonDirection { + return @ptrCast(c.stablehloComparisonDirectionAttrGet(ctx.ptr(), mlir.stringRef(@tagName(value))).ptr); + } -// pub fn getValue(self: Self) Direction { -// const value = mlir.fromStringRef(c.stablehloComparisonDirectionAttrGetValue(self._inner)); -// return std.meta.stringToEnum(Direction, value) orelse unreachable; -// } -// }; + pub fn getValue(self: *const ComparisonDirection) Direction { + const value = mlir.fromStringRef(c.stablehloComparisonDirectionAttrGetValue(self.ptr())); + return std.meta.stringToEnum(Direction, value) orelse unreachable; + } +}; -// pub const CompareType = struct { -// _inner: c.MlirAttribute, +pub fn comparisonDirection(ctx: *mlir.Context, value: ComparisonDirection.Direction) *const mlir.Attribute { + return @ptrCast(ComparisonDirection.init(ctx, value)); +} -// pub const isAFn = c.stablehloAttributeIsAComparisonTypeAttr; -// const Self = CompareType; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); +pub const CompareType = opaque { + const M = mlir.Methods(CompareType, c.MlirAttribute); -// pub const Type = enum { -// SIGNED, -// UNSIGNED, -// FLOAT, -// TOTALORDER, -// }; + pub const isAFn = c.stablehloAttributeIsAComparisonTypeAttr; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); -// pub fn init(ctx: *mlir.Context, value: Type) Self { -// return .{ ._inner = c.stablehloComparisonTypeAttrGet(ctx._inner, stringRef(@tagName(value))) }; -// } + pub const Type = enum { + SIGNED, + UNSIGNED, + FLOAT, + TOTALORDER, + }; -// pub fn getValue(self: Self) Type { -// const value = mlir.fromStringRef(c.stablehloComparisonTypeAttrGetValue(self._inner)); -// return std.meta.stringToEnum(Type, value) orelse unreachable; -// } -// }; + pub fn init(ctx: *mlir.Context, value: Type) *const CompareType { + return @ptrCast(c.stablehloComparisonTypeAttrGet(ctx.ptr(), stringRef(@tagName(value))).ptr); + } + + pub fn getValue(self: *const CompareType) Type { + const value = mlir.fromStringRef(c.stablehloComparisonTypeAttrGetValue(self.ptr())); + return std.meta.stringToEnum(Type, value) orelse unreachable; + } +}; + +pub fn compareType(ctx: *mlir.Context, value: CompareType.Type) *const mlir.Attribute { + return @ptrCast(CompareType.init(ctx, value)); +} // pub const Transpose = struct { // _inner: c.MlirAttribute, From 1a81df4a60400cf65f48cd3945f73cdc6785824e Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 11:50:21 +0200 Subject: [PATCH 20/37] stablehlo: bound dynamic_slice --- mlir/dialects/stablehlo/stablehlo.zig | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 46c40661..119ebf53 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -524,16 +524,22 @@ pub fn compare( // }); // } -// pub fn dynamic_slice(ctx: *mlir.Context, operand: *const mlir.Value, new_dims: []const i64, start_indices: []const *const mlir.Value, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.dynamic_slice", .{ -// .operands = .{ .variadic = &.{ &.{operand}, start_indices } }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "slice_sizes", .dense(ctx, .i64, new_dims) }, -// }, -// .location = location, -// }); -// } +pub fn dynamic_slice( + ctx: *mlir.Context, + operand: *const mlir.Value, + new_dims: []const i64, + start_indices: []const *const mlir.Value, + location: *const mlir.Location, +) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.dynamic_slice", .{ + .operands = .{ .variadic = &.{ &.{operand}, start_indices } }, + .result_type_inference = true, + .attributes = &.{ + .named(ctx, "slice_sizes", mlir.denseArrayAttribute(ctx, .i64, new_dims)), + }, + .location = location, + }); +} // pub const PadOpts = struct { // low: []const i64, From 86c502825f6f1007349852576c065d526be6d693 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 12:00:13 +0200 Subject: [PATCH 21/37] stablehlo: bound pad --- mlir/dialects/stablehlo/stablehlo.zig | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 119ebf53..3e9ce913 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -541,24 +541,24 @@ pub fn dynamic_slice( }); } -// pub const PadOpts = struct { -// low: []const i64, -// high: []const i64, -// interior: []const i64, -// }; +pub const PadOpts = struct { + low: []const i64, + high: []const i64, + interior: []const i64, +}; -// pub fn pad(ctx: *mlir.Context, value: *const mlir.Value, padding_value: *const mlir.Value, opts: PadOpts, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.pad", .{ -// .operands = .{ .flat = &.{ value, padding_value } }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "edge_padding_low", .dense(ctx, .i64, opts.low) }, -// .{ "edge_padding_high", .dense(ctx, .i64, opts.high) }, -// .{ "interior_padding", .dense(ctx, .i64, opts.interior) }, -// }, -// .location = location, -// }); -// } +pub fn pad(ctx: *mlir.Context, value: *const mlir.Value, padding_value: *const mlir.Value, opts: PadOpts, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.pad", .{ + .operands = .{ .flat = &.{ value, padding_value } }, + .result_type_inference = true, + .attributes = &.{ + .named(ctx, "edge_padding_low", mlir.denseArrayAttribute(ctx, .i64, opts.low)), + .named(ctx, "edge_padding_high", mlir.denseArrayAttribute(ctx, .i64, opts.high)), + .named(ctx, "interior_padding", mlir.denseArrayAttribute(ctx, .i64, opts.interior)), + }, + .location = location, + }); +} // pub const TriangularSolveOpts = struct { // left_side: bool, From cad23da7c087c0c0aed042c641a90740cbff2f03 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 12:07:32 +0200 Subject: [PATCH 22/37] stablehlo: bound triangular_solve --- mlir/dialects/stablehlo/stablehlo.zig | 79 ++++++++++++++------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 3e9ce913..0b5338b8 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -560,26 +560,26 @@ pub fn pad(ctx: *mlir.Context, value: *const mlir.Value, padding_value: *const m }); } -// pub const TriangularSolveOpts = struct { -// left_side: bool, -// lower: bool, -// unit_diagonal: bool, -// transpose_a: Transpose.Type, -// }; +pub const TriangularSolveOpts = struct { + left_side: bool, + lower: bool, + unit_diagonal: bool, + transpose_a: TransposeAttribute.Type, +}; -// pub fn triangular_solve(ctx: *mlir.Context, value: *const mlir.Value, other: *const mlir.Value, location: *const mlir.Location, opts: TriangularSolveOpts) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.triangular_solve", .{ -// .operands = .{ .flat = &.{ value, other } }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "left_side", .i1FromBool(ctx, opts.left_side) }, -// .{ "lower", .i1FromBool(ctx, opts.lower) }, -// .{ "unit_diagonal", .i1FromBool(ctx, opts.unit_diagonal) }, -// .{ "transpose_a", Transpose.init(ctx, opts.transpose_a).asAttr() }, -// }, -// .location = location, -// }); -// } +pub fn triangular_solve(ctx: *mlir.Context, value: *const mlir.Value, other: *const mlir.Value, location: *const mlir.Location, opts: TriangularSolveOpts) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.triangular_solve", .{ + .operands = .{ .flat = &.{ value, other } }, + .result_type_inference = true, + .attributes = &.{ + .named(ctx, "left_side", mlir.integerAttribute(ctx, .i1, @intFromBool(opts.left_side))), + .named(ctx, "lower", mlir.integerAttribute(ctx, .i1, @intFromBool(opts.lower))), + .named(ctx, "unit_diagonal", mlir.integerAttribute(ctx, .i1, @intFromBool(opts.unit_diagonal))), + .named(ctx, "transpose_a", transposeAttribute(ctx, opts.transpose_a)), + }, + .location = location, + }); +} // pub const FftOpts = struct { // kind: FftType.Type, @@ -1201,29 +1201,32 @@ pub fn compareType(ctx: *mlir.Context, value: CompareType.Type) *const mlir.Attr return @ptrCast(CompareType.init(ctx, value)); } -// pub const Transpose = struct { -// _inner: c.MlirAttribute, +pub const TransposeAttribute = opaque { + const M = mlir.Methods(TransposeAttribute, mlir.Attribute); -// pub const isAFn = c.stablehloAttributeIsATransposeAttr; -// const Self = Transpose; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); + pub const isAFn = c.stablehloAttributeIsATransposeAttr; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); -// pub const Type = enum { -// NO_TRANSPOSE, -// TRANSPOSE, -// ADJOINT, -// }; + pub const Type = enum { + NO_TRANSPOSE, + TRANSPOSE, + ADJOINT, + }; -// pub fn init(ctx: *mlir.Context, value: Type) Self { -// return .{ ._inner = c.stablehloTransposeAttrGet(ctx._inner, stringRef(@tagName(value))) }; -// } + pub fn init(ctx: *mlir.Context, value: Type) *const TransposeAttribute { + return @ptrCast(c.stablehloTransposeAttrGet(ctx.ptr(), mlir.stringRef(@tagName(value))).ptr); + } -// pub fn getValue(self: Self) Type { -// const value = mlir.fromStringRef(c.stablehloTransposeAttrGetValue(self._inner)); -// return std.meta.stringToEnum(Type, value) orelse unreachable; -// } -// }; + pub fn getValue(self: *const TransposeAttribute) Type { + const value = mlir.fromStringRef(c.stablehloTransposeAttrGetValue(self.ptr())); + return std.meta.stringToEnum(Type, value) orelse unreachable; + } +}; + +pub fn transposeAttribute(ctx: *mlir.Context, value: TransposeAttribute.Type) *const mlir.Attribute { + return @ptrCast(TransposeAttribute.init(ctx, value)); +} // pub const FftType = struct { // _inner: c.MlirAttribute, From a82bc3fe8dc29baf6913f86d2d7983bc6391cfcd Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 12:55:58 +0200 Subject: [PATCH 23/37] stablehlo: bound fft --- mlir/dialects/stablehlo/stablehlo.zig | 73 ++++++++++++++------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 0b5338b8..417c66a4 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -581,22 +581,22 @@ pub fn triangular_solve(ctx: *mlir.Context, value: *const mlir.Value, other: *co }); } -// pub const FftOpts = struct { -// kind: FftType.Type, -// length: []const i64, -// }; +pub const FftOpts = struct { + kind: FftType.Type, + length: []const i64, +}; -// pub fn fft(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location, opts: FftOpts) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.fft", .{ -// .operands = .{ .flat = &.{value} }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "fft_type", FftType.init(ctx, opts.kind).asAttr() }, -// .{ "fft_length", .dense(ctx, .i64, opts.length) }, -// }, -// .location = location, -// }); -// } +pub fn fft(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location, opts: FftOpts) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.fft", .{ + .operands = .{ .flat = &.{value} }, + .result_type_inference = true, + .attributes = &.{ + .named(ctx, "fft_type", fftType(ctx, opts.kind)), + .named(ctx, "fft_length", mlir.denseArrayAttribute(ctx, .i64, opts.length)), + }, + .location = location, + }); +} // pub fn rng(ctx: *mlir.Context, a: *const mlir.Value, b: *const mlir.Value, shape: *const mlir.Value, rng_distribution: RngDistribution.Type, location: *const mlir.Location) *mlir.Operation { // return mlir.Operation.make(ctx, "stablehlo.rng", .{ @@ -1228,30 +1228,33 @@ pub fn transposeAttribute(ctx: *mlir.Context, value: TransposeAttribute.Type) *c return @ptrCast(TransposeAttribute.init(ctx, value)); } -// pub const FftType = struct { -// _inner: c.MlirAttribute, +pub const FftType = opaque { + const M = mlir.Methods(FftType, mlir.Attribute); -// pub const isAFn = c.stablehloAttributeIsAFftTypeAttr; -// const Self = FftType; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); + pub const isAFn = c.stablehloAttributeIsAFftTypeAttr; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); -// pub const Type = enum { -// FFT, -// IFFT, -// RFFT, -// IRFFT, -// }; + pub const Type = enum { + FFT, + IFFT, + RFFT, + IRFFT, + }; -// pub fn init(ctx: *mlir.Context, value: Type) Self { -// return .{ ._inner = c.stablehloFftTypeAttrGet(ctx._inner, stringRef(@tagName(value))) }; -// } + pub fn init(ctx: *mlir.Context, value: Type) *const FftType { + return @ptrCast(c.stablehloFftTypeAttrGet(ctx.ptr(), mlir.stringRef(@tagName(value))).ptr); + } -// pub fn getValue(self: Self) Type { -// const value = mlir.fromStringRef(c.stablehloFftTypeAttrGetValue(self._inner)); -// return std.meta.stringToEnum(Type, value) orelse unreachable; -// } -// }; + pub fn getValue(self: *const FftType) Type { + const value = mlir.fromStringRef(c.stablehloFftTypeAttrGetValue(self.ptr())); + return std.meta.stringToEnum(Type, value) orelse unreachable; + } +}; + +pub fn fftType(ctx: *mlir.Context, value: FftType.Type) *const mlir.Attribute { + return @ptrCast(FftType.init(ctx, value)); +} // pub const RngDistribution = struct { // _inner: c.MlirAttribute, From 6433c8eb4ac799e31d3b87125250431f5ccf4b50 Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Thu, 28 Aug 2025 13:07:48 +0200 Subject: [PATCH 24/37] stablehlo: fix custom_call --- mlir/dialects/stablehlo/stablehlo.zig | 177 +++++++++++++++----------- mlir/registry.zig | 35 +++-- 2 files changed, 124 insertions(+), 88 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 417c66a4..0e241aff 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -727,15 +727,15 @@ pub const CustomCallOpts = struct { }; pub const BackendConfig = union(ApiVersion) { - original: *const mlir.StringAttribute, - status_returning: *const mlir.StringAttribute, - status_returning_unified: *const mlir.StringAttribute, + original: []const u8, + status_returning: []const u8, + status_returning_unified: []const u8, typed_ffi: *const mlir.DictionaryAttribute, }; call_target_name: []const u8, - has_side_effect: bool = false, - api_version: ?ApiVersion, + has_side_effect: ?bool = null, + api_version: ?ApiVersion = null, backend_config: ?BackendConfig = null, operand_layouts: ?[]const []const usize = null, result_layouts: ?[]const []const usize = null, @@ -743,36 +743,49 @@ pub const CustomCallOpts = struct { additional_attributes: []const mlir.NamedAttribute = &.{}, }; -pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, opts: CustomCallOpts, result_types: []const *const mlir.Type, location: *const mlir.Location) *mlir.Operation { +pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, result_types: []const *const mlir.Type, opts: CustomCallOpts, location: *const mlir.Location) *mlir.Operation { const MAX_OPERANDS = 64; - _ = MAX_OPERANDS; // autofix const MAX_RESULTS = 16; + const MINOR_TO_MAJOR = comptime blk: { + var ret: [mlir.ShapedType.MAX_RANK]usize = undefined; + for (0..ret.len) |i| { + ret[i] = @intCast(mlir.ShapedType.MAX_RANK - i - 1); + } + break :blk ret; + }; var attrs: stdx.BoundedArray(mlir.NamedAttribute, 32) = .{}; attrs.appendSliceAssumeCapacity(&.{ .named(ctx, "call_target_name", mlir.stringAttribute(ctx, opts.call_target_name)), - .named(ctx, "has_side_effect", mlir.boolAttribute(ctx, opts.has_side_effect)), }); attrs.appendSliceAssumeCapacity(opts.additional_attributes); - if (opts.api_version orelse opts.backend_config) |v| { + if (opts.has_side_effect) |has_side_effects| { + attrs.appendAssumeCapacity( + .named(ctx, "has_side_effect", mlir.boolAttribute(ctx, has_side_effects)), + ); + } + + const backend_config_api_version: ?CustomCallOpts.ApiVersion = if (opts.backend_config) |bc| bc else null; + if (opts.api_version orelse backend_config_api_version) |v| { attrs.appendAssumeCapacity( .named(ctx, "api_version", mlir.integerAttribute(ctx, .i32, @intFromEnum(v))), ); } if (opts.backend_config) |backend_config| { - switch (backend_config) { - inline else => |v| attrs.appendAssumeCapacity( - .named(ctx, "backend_config", @ptrCast(v)), - ), - } + attrs.appendAssumeCapacity( + .named(ctx, "backend_config", switch (backend_config) { + .typed_ffi => |v| @ptrCast(v), + inline else => |v| mlir.stringAttribute(ctx, v), + }), + ); } if (opts.output_operand_aliases) |output_operand_aliases| { var buffer: stdx.BoundedArray(*const mlir.Attribute, MAX_RESULTS) = .{}; for (output_operand_aliases) |alias| { buffer.appendAssumeCapacity( - outputOperandAliasAttribute.init(ctx, .{ .operand_index = alias }), + outputOperandAliasAttribute(ctx, .{ .operand_index = alias }), ); } attrs.appendAssumeCapacity( @@ -780,52 +793,69 @@ pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, opts: ); } - // const MINOR_TO_MAJOR = blk: { - // const MAX_RANK = 8; - // var ret: [MAX_RANK]usize = undefined; - // for (0..MAX_RANK) |i| { - // ret[i] = @intCast(MAX_RANK - i - 1); - // } - // break :blk ret; - // }; + var layouts_buffer: stdx.BoundedArray(*const mlir.Attribute, MAX_OPERANDS) = .{}; + if (opts.operand_layouts) |layouts| { + for (layouts) |ol| { + layouts_buffer.appendAssumeCapacity( + mlir.denseElementsAttribute( + mlir.RankedTensorType.get( + &.{@intCast(ol.len)}, + mlir.indexType(ctx), + ).shaped(), + ol, + ), + ); + } + } else { + for (inputs) |input| { + const shaped_type = input.type_().isA(mlir.ShapedType).?; + const ol = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - shaped_type.rank() ..]; + layouts_buffer.appendAssumeCapacity( + mlir.denseElementsAttribute( + mlir.RankedTensorType.get( + &.{@intCast(ol.len)}, + mlir.indexType(ctx), + ).shaped(), + ol, + ), + ); + } + } + attrs.appendAssumeCapacity( + .named(ctx, "operand_layouts", mlir.arrayAttribute(ctx, layouts_buffer.constSlice())), + ); - // if (opts.operand_layouts) |layouts| { - // var operand_layouts: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; - // for (layouts) |ol| { - // operand_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); - // } - // attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); - // } else { - // const operand_layouts = blk: { - // var ret: stdx.BoundedArray(mlir.Attribute, MAX_OPERANDS) = .{}; - // for (inputs) |input| { - // const ranked_type = input.getType().as(mlir.RankedTensorType).?; - // const ol = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_type.getRank() ..]; - // ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(ol.len)}, .index, ol)); - // } - // break :blk ret; - // }; - // attrs.appendAssumeCapacity(.{ "operand_layouts", .array(ctx, operand_layouts.constSlice()) }); - // } - - // if (opts.result_layouts) |layouts| { - // var result_layouts: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; - // for (layouts) |rl| { - // result_layouts.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); - // } - // attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); - // } else { - // const result_layouts = blk: { - // var ret: stdx.BoundedArray(mlir.Attribute, MAX_RESULTS) = .{}; - // for (res_types) |t| { - // const ranked_t = t.as(mlir.RankedTensorType).?; - // const rl = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - ranked_t.getRank() ..]; - // ret.appendAssumeCapacity(.denseElements(ctx, &.{@intCast(rl.len)}, .index, rl)); - // } - // break :blk ret; - // }; - // attrs.appendAssumeCapacity(.{ "result_layouts", .array(ctx, result_layouts.constSlice()) }); - // } + layouts_buffer.clear(); + if (opts.result_layouts) |layouts| { + for (layouts) |rl| { + layouts_buffer.appendAssumeCapacity( + mlir.denseElementsAttribute( + mlir.RankedTensorType.get( + &.{@intCast(rl.len)}, + mlir.indexType(ctx), + ).shaped(), + rl, + ), + ); + } + } else { + for (result_types) |result_type| { + const shaped_type = result_type.isA(mlir.ShapedType).?; + const rl = MINOR_TO_MAJOR[MINOR_TO_MAJOR.len - shaped_type.rank() ..]; + layouts_buffer.appendAssumeCapacity( + mlir.denseElementsAttribute( + mlir.RankedTensorType.get( + &.{@intCast(rl.len)}, + mlir.indexType(ctx), + ).shaped(), + rl, + ), + ); + } + } + attrs.appendAssumeCapacity( + .named(ctx, "result_layouts", mlir.arrayAttribute(ctx, layouts_buffer.constSlice())), + ); return mlir.Operation.make(ctx, "stablehlo.custom_call", .{ .operands = .{ .flat = inputs }, @@ -835,29 +865,25 @@ pub fn custom_call(ctx: *mlir.Context, inputs: []const *const mlir.Value, opts: }); } -pub fn createBuffer(ctx: *mlir.Context, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { - return custom_call(ctx, &.{}, .{ +pub fn createBuffer(ctx: *mlir.Context, value: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { + return custom_call(ctx, &.{}, &.{value}, .{ .call_target_name = "CreateBuffer", .api_version = .typed_ffi, - }, &.{result_type}, location); + }, location); } pub fn pin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { - const result_type = mlir.integerType(ctx, .i64); - const shape = value.type_().as(mlir.ShapedType).?; - _ = shape; // autofix - return custom_call(ctx, &.{value}, .{ + return custom_call(ctx, &.{}, &.{@ptrCast(mlir.MemRefType.fromShaped(value.type_().isA(mlir.ShapedType).?))}, .{ .call_target_name = "Pin", .api_version = .typed_ffi, - }, &.{result_type}, location); + }, location); } pub fn unpin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { - const result_type = mlir.integerType(ctx, .i64); - return custom_call(ctx, &.{value}, .{ + return custom_call(ctx, &.{}, &.{@ptrCast(mlir.RankedTensorType.fromShaped(value.type_().isA(mlir.ShapedType).?))}, .{ .call_target_name = "Unpin", .api_version = .typed_ffi, - }, &.{result_type}, location); + }, location); } // pub const DotDimensionNumbersAttribute = struct { @@ -1103,7 +1129,7 @@ pub const OutputOperandAliasAttribute = opaque { operand_tuple_indices: []const i64 = &.{}, }; - pub fn init(ctx: *mlir.Context, args: InitArgs) *OutputOperandAliasAttribute { + pub fn get(ctx: *mlir.Context, args: InitArgs) *const OutputOperandAliasAttribute { return @ptrCast(c.stablehloOutputOperandAliasGet( ctx.ptr(), @intCast(args.output_tuple_indices.len), @@ -1115,8 +1141,8 @@ pub const OutputOperandAliasAttribute = opaque { } }; -pub fn outputOperandAliasAttribute(ctx: *mlir.Context, args: OutputOperandAliasAttribute.InitArgs) *mlir.Attribute { - return @ptrCast(OutputOperandAliasAttribute.init(ctx, args)); +pub fn outputOperandAliasAttribute(ctx: *mlir.Context, args: OutputOperandAliasAttribute.InitArgs) *const mlir.Attribute { + return @ptrCast(OutputOperandAliasAttribute.get(ctx, args)); } // pub const PrecisionAttribute = struct { @@ -1306,8 +1332,7 @@ pub fn fftType(ctx: *mlir.Context, value: FftType.Type) *const mlir.Attribute { fn stringFromStream(buf: []u8, streamFn: anytype, args: anytype) []const u8 { var writer = std.Io.Writer.fixed(buf); var sctx: mlir.StringCallbackCtx = .{ .writer = &writer }; - const new_args = args ++ .{ mlir.stringCallback, &sctx }; - @call(.auto, streamFn, new_args); + @call(.auto, streamFn, args ++ .{ mlir.stringCallback, &sctx }); return writer.buffered(); } diff --git a/mlir/registry.zig b/mlir/registry.zig index 78ec4698..1367b660 100644 --- a/mlir/registry.zig +++ b/mlir/registry.zig @@ -250,6 +250,10 @@ pub const IndexType = opaque { } }; +pub fn indexType(ctx: *Context) *const Type { + return @ptrCast(IndexType.get(ctx)); +} + pub const IntegerTypes = enum { i1, i4, @@ -1207,7 +1211,7 @@ pub const RankedTensorType = opaque { pub const eql = M.eql(c.mlirTypeEqual); pub const format = M.format(c.mlirTypePrint); - pub fn init(dimensions: []const i64, elemType: *const Type) *const RankedTensorType { + pub fn get(dimensions: []const i64, elemType: *const Type) *const RankedTensorType { return @ptrCast(c.mlirRankedTensorTypeGet( @intCast(dimensions.len), @ptrCast(dimensions), @@ -1218,12 +1222,12 @@ pub const RankedTensorType = opaque { pub fn fromShaped(other: *const ShapedType) *const RankedTensorType { var dims: stdx.BoundedArray(i64, ShapedType.MAX_RANK) = .{}; - for (other.rank()) |i| { + for (0..other.rank()) |i| { dims.appendAssumeCapacity(other.dimension(i)); } - return init( - other.elementType(), + return get( dims.constSlice(), + other.elementType(), ); } @@ -1245,7 +1249,7 @@ pub const RankedTensorType = opaque { }; pub fn rankedTensorType(dimensions: []const i64, elem_type: *const Type) *const Type { - return @ptrCast(RankedTensorType.init(dimensions, elem_type)); + return @ptrCast(RankedTensorType.get(dimensions, elem_type)); } pub const DenseElementsAttribute = opaque { @@ -1256,16 +1260,17 @@ pub const DenseElementsAttribute = opaque { pub const eql = M.eql(c.mlirAttributeEqual); pub const format = M.format(c.mlirAttributePrint); - pub fn init(shaped_type: *const ShapedType, values: []const u8) *const DenseElementsAttribute { + pub fn init(shaped_type: *const ShapedType, values: anytype) *const DenseElementsAttribute { + const bytes = std.mem.sliceAsBytes(values); return @ptrCast(c.mlirDenseElementsAttrRawBufferGet( shaped_type.ptr(), - @intCast(values.len), - @ptrCast(values), + @intCast(bytes.len), + @ptrCast(bytes), ).ptr orelse unreachable); } }; -pub fn denseElementsAttribute(shaped_type: *const ShapedType, values: []const u8) *const Attribute { +pub fn denseElementsAttribute(shaped_type: *const ShapedType, values: anytype) *const Attribute { return @ptrCast(DenseElementsAttribute.init(shaped_type, values)); } @@ -1426,13 +1431,19 @@ pub const MemRefType = opaque { pub const eql = M.eql(c.mlirTypeEqual); pub const format = M.format(c.mlirTypePrint); - pub fn init(element_type: *const Type, shape: []const i64, layout: ?*const Attribute, memory_space: ?*Attribute) *const MemRefType { - return @ptrCast(c.mlirMemRefTypeGet(element_type, shape.len, @ptrCast(shape), layout, memory_space).ptr); + pub fn init(element_type: *const Type, shape: []const i64, layout: ?*const Attribute, memory_space: ?*const Attribute) *const MemRefType { + return @ptrCast(c.mlirMemRefTypeGet( + element_type.ptr(), + @intCast(shape.len), + @ptrCast(shape), + if (layout) |l| l.ptr() else .{}, + if (memory_space) |m| m.ptr() else .{}, + ).ptr); } pub fn fromShaped(other: *const ShapedType) *const MemRefType { var dims: stdx.BoundedArray(i64, ShapedType.MAX_RANK) = .{}; - for (other.rank()) |i| { + for (0..other.rank()) |i| { dims.appendAssumeCapacity(other.dimension(i)); } return init( From 277f5be00e2f1accb22d0599876705d35e8e8569 Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Thu, 28 Aug 2025 13:14:12 +0200 Subject: [PATCH 25/37] stablehlo: bound get_tuple_element --- mlir/dialects/stablehlo/stablehlo.zig | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 0e241aff..93612e38 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -640,16 +640,16 @@ pub fn fft(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.L // }); // } -// pub fn get_tuple_element(ctx: *mlir.Context, tuple_value: *const mlir.Value, index: i64, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.get_tuple_element", .{ -// .operands = .{ .flat = &.{tuple_value} }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "index", .int(ctx, .i32, index) }, -// }, -// .location = location, -// }); -// } +pub fn get_tuple_element(ctx: *mlir.Context, tuple_value: *const mlir.Value, index: i64, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.get_tuple_element", .{ + .operands = .{ .flat = &.{tuple_value} }, + .result_type_inference = true, + .attributes = &.{ + .named(ctx, "index", mlir.integerAttribute(ctx, .i32, index)), + }, + .location = location, + }); +} // pub const ConvolutionOpts = struct { // window_strides: []const i64, From 73d1f85a16d1ecc9bb506afa9e93a8c21ace93bb Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Thu, 28 Aug 2025 13:18:02 +0200 Subject: [PATCH 26/37] stablehlo: bound dynamic_update_slice --- mlir/dialects/stablehlo/stablehlo.zig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 93612e38..82e92cea 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -632,13 +632,13 @@ pub fn fft(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.L // }); // } -// pub fn dynamic_update_slice(ctx: *mlir.Context, operand: *const mlir.Value, update: *const mlir.Value, start_indices: []const *const mlir.Value, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.dynamic_update_slice", .{ -// .operands = .{ .variadic = &.{ &.{operand}, &.{update}, start_indices } }, -// .result_type_inference = true, -// .location = location, -// }); -// } +pub fn dynamic_update_slice(ctx: *mlir.Context, operand: *const mlir.Value, update: *const mlir.Value, start_indices: []const *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.dynamic_update_slice", .{ + .operands = .{ .variadic = &.{ &.{operand}, &.{update}, start_indices } }, + .result_type_inference = true, + .location = location, + }); +} pub fn get_tuple_element(ctx: *mlir.Context, tuple_value: *const mlir.Value, index: i64, location: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.get_tuple_element", .{ From a2df29defd4c12a6211dd841a4b6b351331ba832 Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Thu, 28 Aug 2025 13:18:54 +0200 Subject: [PATCH 27/37] stablehlo: bound reduce_precision --- mlir/dialects/stablehlo/stablehlo.zig | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 82e92cea..abf23fce 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -620,17 +620,17 @@ pub fn fft(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.L // }); // } -// pub fn reduce_precision(ctx: *mlir.Context, value: *const mlir.Value, exponent_bits: i32, mantissa_bits: i32, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.reduce_precision", .{ -// .operands = .{ .flat = &.{value} }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "exponent_bits", .int(ctx, .i32, exponent_bits) }, -// .{ "mantissa_bits", .int(ctx, .i32, mantissa_bits) }, -// }, -// .location = location, -// }); -// } +pub fn reduce_precision(ctx: *mlir.Context, value: *const mlir.Value, exponent_bits: i32, mantissa_bits: i32, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.reduce_precision", .{ + .operands = .{ .flat = &.{value} }, + .result_type_inference = true, + .attributes = &.{ + .named(ctx, "exponent_bits", mlir.integerAttribute(ctx, .i32, exponent_bits)), + .named(ctx, "mantissa_bits", mlir.integerAttribute(ctx, .i32, mantissa_bits)), + }, + .location = location, + }); +} pub fn dynamic_update_slice(ctx: *mlir.Context, operand: *const mlir.Value, update: *const mlir.Value, start_indices: []const *const mlir.Value, location: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.dynamic_update_slice", .{ From c62059baacd0fbf9ed2507f0b7f9da20f8614b84 Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Thu, 28 Aug 2025 13:25:13 +0200 Subject: [PATCH 28/37] stablehlo: bound rng and rng_bit_generator --- mlir/dialects/stablehlo/stablehlo.zig | 146 ++++++++++++++------------ 1 file changed, 79 insertions(+), 67 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index abf23fce..e0cfbdd9 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -598,27 +598,86 @@ pub fn fft(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.L }); } -// pub fn rng(ctx: *mlir.Context, a: *const mlir.Value, b: *const mlir.Value, shape: *const mlir.Value, rng_distribution: RngDistribution.Type, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.rng", .{ -// .operands = .{ .flat = &.{ a, b, shape } }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "rng_distribution", RngDistribution.init(ctx, rng_distribution).asAttr() }, -// }, -// .location = location, -// }); -// } +pub const RngDistribution = opaque { + const M = mlir.Methods(RngDistribution, c.MlirAttribute); -// pub fn rng_bit_generator(ctx: *mlir.Context, rng_algorithm: RngAlgorithm.Type, initial_state: *const mlir.Value, res_state_type: *const mlir.Type, res_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { -// return mlir.Operation.make(ctx, "stablehlo.rng_bit_generator", .{ -// .operands = .{ .flat = &.{initial_state} }, -// .results = &.{ res_state_type, res_type }, -// .attributes = &.{ -// .{ "rng_algorithm", RngAlgorithm.init(ctx, rng_algorithm).asAttr() }, -// }, -// .location = location, -// }); -// } + pub const isAFn = c.stablehloAttributeIsARngDistributionAttr; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub const Type = enum { + UNIFORM, + NORMAL, + }; + + pub fn get(ctx: *mlir.Context, value_: Type) *const RngDistribution { + return @ptrCast(c.stablehloRngDistributionAttrGet(ctx.ptr(), stringRef(@tagName(value_))).ptr); + } + + pub fn value(self: *const RngDistribution) Type { + return std.meta.stringToEnum( + Type, + mlir.string(c.stablehloRngDistributionAttrGetValue(self.ptr())), + ) orelse unreachable; + } +}; + +pub fn rngDistribution(ctx: *mlir.Context, value: RngDistribution.Type) *const mlir.Attribute { + return @ptrCast(RngDistribution.get(ctx, value)); +} + +pub const RngAlgorithm = opaque { + const M = mlir.Methods(RngAlgorithm, c.MlirAttribute); + + pub const isAFn = c.stablehloAttributeIsARngAlgorithmAttr; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub const Type = enum { + DEFAULT, + THREE_FRY, + PHILOX, + }; + + pub fn get(ctx: *mlir.Context, value_: Type) *const RngAlgorithm { + return @ptrCast(c.stablehloRngAlgorithmAttrGet(ctx.ptr(), stringRef(@tagName(value_))).ptr); + } + + pub fn value(self: *const RngAlgorithm) Type { + return std.meta.stringToEnum( + Type, + mlir.string(c.stablehloRngAlgorithmAttrGetValue(self.ptr())), + ) orelse unreachable; + } +}; + +pub fn rngAlgorithm(ctx: *mlir.Context, value: RngAlgorithm.Type) *const mlir.Attribute { + return @ptrCast(RngAlgorithm.get(ctx, value)); +} + +pub fn rng(ctx: *mlir.Context, a: *const mlir.Value, b: *const mlir.Value, shape: *const mlir.Value, rng_distribution: RngDistribution.Type, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.rng", .{ + .operands = .{ .flat = &.{ a, b, shape } }, + .result_type_inference = true, + .attributes = &.{ + .named(ctx, "rng_distribution", rngDistribution(ctx, rng_distribution)), + }, + .location = location, + }); +} + +pub fn rng_bit_generator(ctx: *mlir.Context, rng_algorithm: RngAlgorithm.Type, initial_state: *const mlir.Value, res_state_type: *const mlir.Type, res_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { + return mlir.Operation.make(ctx, "stablehlo.rng_bit_generator", .{ + .operands = .{ .flat = &.{initial_state} }, + .results = &.{ res_state_type, res_type }, + .attributes = &.{ + .named(ctx, "rng_algorithm", rngAlgorithm(ctx, rng_algorithm)), + }, + .location = location, + }); +} pub fn reduce_precision(ctx: *mlir.Context, value: *const mlir.Value, exponent_bits: i32, mantissa_bits: i32, location: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.reduce_precision", .{ @@ -1282,53 +1341,6 @@ pub fn fftType(ctx: *mlir.Context, value: FftType.Type) *const mlir.Attribute { return @ptrCast(FftType.init(ctx, value)); } -// pub const RngDistribution = struct { -// _inner: c.MlirAttribute, - -// pub const isAFn = c.stablehloAttributeIsARngDistributionAttr; -// const Self = RngDistribution; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); - -// pub const Type = enum { -// UNIFORM, -// NORMAL, -// }; - -// pub fn init(ctx: *mlir.Context, value: Type) Self { -// return .{ ._inner = c.stablehloRngDistributionAttrGet(ctx._inner, stringRef(@tagName(value))) }; -// } - -// pub fn getValue(self: Self) Type { -// const value = mlir.fromStringRef(c.stablehloRngDistributionAttrGetValue(self._inner)); -// return std.meta.stringToEnum(Type, value) orelse unreachable; -// } -// }; - -// pub const RngAlgorithm = struct { -// _inner: c.MlirAttribute, - -// pub const isAFn = c.stablehloAttributeIsARngAlgorithmAttr; -// const Self = RngAlgorithm; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); - -// pub const Type = enum { -// DEFAULT, -// THREE_FRY, -// PHILOX, -// }; - -// pub fn init(ctx: *mlir.Context, value: Type) Self { -// return .{ ._inner = c.stablehloRngAlgorithmAttrGet(ctx._inner, stringRef(@tagName(value))) }; -// } - -// pub fn getValue(self: Self) Type { -// const value = mlir.fromStringRef(c.stablehloRngAlgorithmAttrGetValue(self._inner)); -// return std.meta.stringToEnum(Type, value) orelse unreachable; -// } -// }; - fn stringFromStream(buf: []u8, streamFn: anytype, args: anytype) []const u8 { var writer = std.Io.Writer.fixed(buf); var sctx: mlir.StringCallbackCtx = .{ .writer = &writer }; From a0025304bee14cf816de0b6b49a280ac3427e94c Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 13:44:35 +0200 Subject: [PATCH 29/37] mlir: made mlir.floatType ft parameter allowed at runtime --- mlir/registry.zig | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mlir/registry.zig b/mlir/registry.zig index 1367b660..7536e701 100644 --- a/mlir/registry.zig +++ b/mlir/registry.zig @@ -385,8 +385,10 @@ pub fn FloatType(comptime ft: FloatTypes) type { }; } -pub fn floatType(comptime ft: FloatTypes, ctx: *Context) *const Type { - return @ptrCast(FloatType(ft).get(ctx)); +pub fn floatType(ft: FloatTypes, ctx: *Context) *const Type { + return switch (ft) { + inline else => |v| @ptrCast(FloatType(v).get(ctx)), + }; } pub const Attribute = opaque { From 333c352fc9abcbe0cfaf7be737053c0324ccdf81 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 13:45:22 +0200 Subject: [PATCH 30/37] stablehlo: bound dot_general --- mlir/dialects/stablehlo/stablehlo.zig | 331 +++++++++++++------------- 1 file changed, 170 insertions(+), 161 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index e0cfbdd9..33dcbc7b 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -107,103 +107,104 @@ pub fn clamp(ctx: *mlir.Context, min: *const mlir.Value, value: *const mlir.Valu }); } -// pub const DotPrecision = union(enum) { -// fast, -// high, -// highest, -// algorithm: DotAlgorithm, +pub const DotPrecision = union(enum) { + fast, + high, + highest, + algorithm: DotAlgorithm, -// pub fn precisionAttr(self: DotPrecision, ctx: *mlir.Context) *const mlir.Attribute { -// const precision = PrecisionAttribute.init(ctx, switch (self) { -// .fast => .DEFAULT, -// .high => .HIGH, -// .highest => .HIGHEST, -// // When we specify the dot algorithm, we should not specify the precision. -// .algorithm => .DEFAULT, -// }); -// return precision.asAttr(); -// } + pub fn precision(self: DotPrecision) PrecisionAttribute.Precision { + return switch (self) { + .fast => .DEFAULT, + .high => .HIGH, + .highest => .HIGHEST, + // When we specify the dot algorithm, we should not specify the precision. + .algorithm => .DEFAULT, + }; + } -// pub fn algorithmAttr(self: DotPrecision, ctx: *mlir.Context, operand_type: mlir.RankedTensorType) ?mlir.Attribute { -// return switch (self) { -// .algorithm => |algo| algo.asAttr(ctx, operand_type), -// else => null, -// }; -// } -// }; + pub fn algorithmAttr(self: DotPrecision, ctx: *mlir.Context, operand_type: mlir.RankedTensorType) ?mlir.Attribute { + return switch (self) { + .algorithm => |algo| algo.asAttr(ctx, operand_type), + else => null, + }; + } +}; -// pub const DotAlgorithm = struct { -// accumulation: mlir.FloatTypes, -// // Note stablehlo distinguish between left/right component_count -// // but all the supported algorithm have the same component_count on both side. -// component_count: u8 = 1, -// num_primitive_operations: u8 = 1, -// allow_imprecise_accumulation: bool = false, +pub const DotAlgorithm = struct { + accumulation: mlir.FloatTypes, + // Note stablehlo distinguish between left/right component_count + // but all the supported algorithm have the same component_count on both side. + component_count: u8 = 1, + num_primitive_operations: u8 = 1, + allow_imprecise_accumulation: bool = false, -// // bf16_6x: each input is decomposed to 3 bf16 components, then 6 dot operations are done on those components, and the result is accumulated in f32. -// // not sure where this is available. -// pub const bf16_6x: DotAlgorithm = .{ -// .operand = .bf16, -// .accumulation = .f32, -// .component_count = 1, -// .num_primitive_operations = 6, -// .allow_imprecise_accumulation = false, -// }; + // bf16_6x: each input is decomposed to 3 bf16 components, then 6 dot operations are done on those components, and the result is accumulated in f32. + // not sure where this is available. + pub const bf16_6x: DotAlgorithm = .{ + .accumulation = .f32, + .component_count = 1, + .num_primitive_operations = 6, + .allow_imprecise_accumulation = false, + }; +}; -// pub fn asAttr(self: DotAlgorithm, ctx: *mlir.Context, tensor_type: mlir.RankedTensorType) *const mlir.Attribute { -// const elem_type = tensor_type.getElementType(); +pub fn dotAlgorithmAttribute(ctx: *mlir.Context, dot_algorithm: DotAlgorithm, element_type: *const mlir.Type) *const mlir.Attribute { + return @ptrCast(c.stablehloDotAlgorithmGet( + ctx.ptr(), + element_type.ptr(), + element_type.ptr(), + mlir.floatType(dot_algorithm.accumulation, ctx).ptr(), + dot_algorithm.component_count, + dot_algorithm.component_count, + dot_algorithm.num_primitive_operations, + dot_algorithm.allow_imprecise_accumulation, + ).ptr); +} -// return mlir.Attribute.wrap(c.stablehloDotAlgorithmGet( -// ctx._inner, -// elem_type._inner, -// elem_type._inner, -// self.accumulation.asType(ctx)._inner, -// self.component_count, -// self.component_count, -// self.num_primitive_operations, -// self.allow_imprecise_accumulation, -// )); -// } -// }; - -// /// General matrix multiplication "a la Einstein sum" -// /// Note: stablehlo doesn't do type inference for dot_general -// pub fn dot_general( -// ctx: *mlir.Context, -// lhs: *const mlir.Value, -// rhs: *const mlir.Value, -// result_type: *const mlir.Type, -// location: *const mlir.Location, -// opts: struct { -// lhs_batching_dimensions: []const i64, -// rhs_batching_dimensions: []const i64, -// lhs_contracting_dimensions: []const i64, -// rhs_contracting_dimensions: []const i64, -// precision: DotPrecision, -// }, -// ) *mlir.Operation { -// const precisions: [2]mlir.Attribute = @splat(opts.precision.precisionAttr(ctx)); -// const attributes = [3]mlir.AttrTuple{ -// .{ -// "dot_dimension_numbers", DotDimensionNumbersAttribute.init(ctx, .{ -// .lhs_batching_dimensions = opts.lhs_batching_dimensions, -// .rhs_batching_dimensions = opts.rhs_batching_dimensions, -// .lhs_contracting_dimensions = opts.lhs_contracting_dimensions, -// .rhs_contracting_dimensions = opts.rhs_contracting_dimensions, -// }).asAttr(), -// }, -// .{ "precision_config", .array(ctx, &precisions) }, -// // keep algorithm as the last attribute so we can omit it when it's not set. -// .{ "algorithm", opts.precision.algorithmAttr(ctx, lhs.getType().as(mlir.RankedTensorType).?) orelse undefined }, -// }; -// const n_attributes = if (opts.precision == .algorithm) attributes.len else attributes.len - 1; -// return mlir.Operation.make(ctx, "stablehlo.dot_general", .{ -// .operands = .{ .flat = &.{ lhs, rhs } }, -// .results = .{ .flat = &.{result_type} }, -// .attributes = attributes[0..n_attributes], -// .location = location, -// }); -// } +/// General matrix multiplication "a la Einstein sum" +/// Note: stablehlo doesn't do type inference for dot_general +pub fn dot_general( + ctx: *mlir.Context, + lhs: *const mlir.Value, + rhs: *const mlir.Value, + result_type: *const mlir.Type, + location: *const mlir.Location, + opts: struct { + lhs_batching_dimensions: []const i64, + rhs_batching_dimensions: []const i64, + lhs_contracting_dimensions: []const i64, + rhs_contracting_dimensions: []const i64, + dot_precision: DotPrecision, + }, +) *mlir.Operation { + const precisions: [2]*const mlir.Attribute = @splat(precisionAttribute(ctx, opts.dot_precision.precision())); + const attributes = [3]mlir.NamedAttribute{ + .named( + ctx, + "dot_dimension_numbers", + dotDimensionNumbersAttribute(ctx, .{ + .lhs_batching_dimensions = opts.lhs_batching_dimensions, + .rhs_batching_dimensions = opts.rhs_batching_dimensions, + .lhs_contracting_dimensions = opts.lhs_contracting_dimensions, + .rhs_contracting_dimensions = opts.rhs_contracting_dimensions, + }), + ), + .named(ctx, "precision_config", mlir.arrayAttribute(ctx, &precisions)), + // keep algorithm as the last attribute so we can omit it when it's not set. + .named(ctx, "algorithm", switch (opts.dot_precision) { + .algorithm => |v| dotAlgorithmAttribute(ctx, v, lhs.type_()), + else => undefined, + }), + }; + const n_attributes = if (opts.dot_precision == .algorithm) attributes.len else attributes.len - 1; + return mlir.Operation.make(ctx, "stablehlo.dot_general", .{ + .operands = .{ .flat = &.{ lhs, rhs } }, + .results = .{ .flat = &.{result_type} }, + .attributes = attributes[0..n_attributes], + .location = location, + }); +} pub fn constant( ctx: *mlir.Context, @@ -945,67 +946,71 @@ pub fn unpin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir }, location); } -// pub const DotDimensionNumbersAttribute = struct { -// _inner: c.MlirAttribute, +pub const DotDimensionNumbersAttribute = opaque { + const M = mlir.Methods(DotDimensionNumbersAttribute, mlir.Attribute); -// pub const isAFn = c.stablehloAttributeIsADotDimensionNumbers; -// const Self = DotDimensionNumbersAttribute; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); + pub const isAFn = c.stablehloAttributeIsADotDimensionNumbers; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); -// pub fn init(ctx: *mlir.Context, args: struct { -// lhs_batching_dimensions: []const i64, -// rhs_batching_dimensions: []const i64, -// lhs_contracting_dimensions: []const i64, -// rhs_contracting_dimensions: []const i64, -// }) Self { -// return .{ -// ._inner = c.stablehloDotDimensionNumbersGet( -// ctx._inner, -// @intCast(args.lhs_batching_dimensions.len), -// args.lhs_batching_dimensions.ptr, -// @intCast(args.rhs_batching_dimensions.len), -// args.rhs_batching_dimensions.ptr, -// @intCast(args.lhs_contracting_dimensions.len), -// args.lhs_contracting_dimensions.ptr, -// @intCast(args.rhs_contracting_dimensions.len), -// args.rhs_contracting_dimensions.ptr, -// ), -// }; -// } + pub const InitArgs = struct { + lhs_batching_dimensions: []const i64, + rhs_batching_dimensions: []const i64, + lhs_contracting_dimensions: []const i64, + rhs_contracting_dimensions: []const i64, + }; -// pub fn getLhsBatchingDimensionsSize(self: Self) usize { -// return @intCast(c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsSize(self._inner)); -// } + pub fn init(ctx: *mlir.Context, args: InitArgs) *const DotDimensionNumbersAttribute { + return @ptrCast(c.stablehloDotDimensionNumbersGet( + ctx.ptr(), + @intCast(args.lhs_batching_dimensions.len), + args.lhs_batching_dimensions.ptr, + @intCast(args.rhs_batching_dimensions.len), + args.rhs_batching_dimensions.ptr, + @intCast(args.lhs_contracting_dimensions.len), + args.lhs_contracting_dimensions.ptr, + @intCast(args.rhs_contracting_dimensions.len), + args.rhs_contracting_dimensions.ptr, + ).ptr); + } -// pub fn getLhsBatchingDimensionsElem(self: Self, pos: usize) i64 { -// return c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsElem(self._inner, @intCast(pos)); -// } + pub fn getLhsBatchingDimensionsSize(self: *const DotDimensionNumbersAttribute) usize { + return @intCast(c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsSize(self.ptr())); + } -// pub fn getRhsBatchingDimensionsSize(self: Self) usize { -// return @intCast(c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsSize(self._inner)); -// } + pub fn getLhsBatchingDimensionsElem(self: *const DotDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsElem(self.ptr(), @intCast(pos)); + } -// pub fn getRhsBatchingDimensionsElem(self: Self, pos: usize) i64 { -// return c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsElem(self._inner, @intCast(pos)); -// } + pub fn getRhsBatchingDimensionsSize(self: *const DotDimensionNumbersAttribute) usize { + return @intCast(c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsSize(self.ptr())); + } -// pub fn getLhsContractingDimensionsSize(self: Self) usize { -// return @intCast(c.stablehloDotDimensionNumbersGetLhsContractingDimensionsSize(self._inner)); -// } + pub fn getRhsBatchingDimensionsElem(self: *const DotDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsElem(self.ptr(), @intCast(pos)); + } -// pub fn getLhsContractingDimensionsElem(self: Self, pos: usize) i64 { -// return c.stablehloDotDimensionNumbersGetLhsContractingDimensionsElem(self._inner, @intCast(pos)); -// } + pub fn getLhsContractingDimensionsSize(self: *const DotDimensionNumbersAttribute) usize { + return @intCast(c.stablehloDotDimensionNumbersGetLhsContractingDimensionsSize(self.ptr())); + } -// pub fn getRhsContractingDimensionsSize(self: Self) usize { -// return @intCast(c.stablehloDotDimensionNumbersGetRhsContractingDimensionsSize(self._inner)); -// } + pub fn getLhsContractingDimensionsElem(self: *const DotDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloDotDimensionNumbersGetLhsContractingDimensionsElem(self.ptr(), @intCast(pos)); + } -// pub fn getRhsContractingDimensionsElem(self: Self, pos: usize) i64 { -// return c.stablehloDotDimensionNumbersGetRhsContractingDimensionsElem(self._inner, @intCast(pos)); -// } -// }; + pub fn getRhsContractingDimensionsSize(self: *const DotDimensionNumbersAttribute) usize { + return @intCast(c.stablehloDotDimensionNumbersGetRhsContractingDimensionsSize(self.ptr())); + } + + pub fn getRhsContractingDimensionsElem(self: *const DotDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloDotDimensionNumbersGetRhsContractingDimensionsElem(self.ptr(), @intCast(pos)); + } +}; + +pub fn dotDimensionNumbersAttribute(ctx: *mlir.Context, args: DotDimensionNumbersAttribute.InitArgs) *const mlir.Attribute { + return @ptrCast(DotDimensionNumbersAttribute.init(ctx, args)); +} // pub const GatherDimensionNumbersAttribute = struct { // _inner: c.MlirAttribute, @@ -1204,29 +1209,33 @@ pub fn outputOperandAliasAttribute(ctx: *mlir.Context, args: OutputOperandAliasA return @ptrCast(OutputOperandAliasAttribute.get(ctx, args)); } -// pub const PrecisionAttribute = struct { -// _inner: c.MlirAttribute, +pub const PrecisionAttribute = opaque { + const M = mlir.Methods(PrecisionAttribute, mlir.Attribute); -// pub const isAFn = c.stablehloAttributeIsAPrecisionAttr; -// const Self = PrecisionAttribute; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); + pub const isAFn = c.stablehloAttributeIsAPrecisionAttr; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); -// pub const Precision = enum { -// DEFAULT, -// HIGH, -// HIGHEST, -// }; + pub const Precision = enum { + DEFAULT, + HIGH, + HIGHEST, + }; -// pub fn init(ctx: *mlir.Context, value: Precision) Self { -// return .{ ._inner = c.stablehloPrecisionAttrGet(ctx._inner, stringRef(@tagName(value))) }; -// } + pub fn init(ctx: *mlir.Context, value: Precision) *const PrecisionAttribute { + return @ptrCast(c.stablehloPrecisionAttrGet(ctx.ptr(), stringRef(@tagName(value))).ptr); + } -// pub fn getValue(self: Self) Precision { -// const value = mlir.fromStringRef(c.stablehloPrecisionAttrGetValue(self._inner)); -// return std.meta.stringToEnum(Precision, value) orelse unreachable; -// } -// }; + pub fn getValue(self: *const PrecisionAttribute) Precision { + const value = mlir.fromStringRef(c.stablehloPrecisionAttrGetValue(self.ptr())); + return std.meta.stringToEnum(Precision, value) orelse unreachable; + } +}; + +pub fn precisionAttribute(ctx: *mlir.Context, precision: PrecisionAttribute.Precision) *const mlir.Attribute { + return @ptrCast(PrecisionAttribute.init(ctx, precision)); +} pub const ComparisonDirection = opaque { const M = mlir.Methods(ComparisonDirection, c.MlirAttribute); From 00760331d324958197d3730846aa8a0ade115b37 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 14:25:54 +0200 Subject: [PATCH 31/37] stablehlo: bound gather --- mlir/dialects/stablehlo/stablehlo.zig | 228 ++++++++++++++------------ 1 file changed, 123 insertions(+), 105 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 33dcbc7b..a63bec55 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -303,45 +303,45 @@ pub fn select( }); } -// pub fn gather( -// ctx: *mlir.Context, -// value: *const mlir.Value, -// indices: *const mlir.Value, -// slice_sizes: []const i64, -// location: *const mlir.Location, -// args: struct { -// offset_dims: []const i64, -// collapsed_slice_dims: []const i64, -// operand_batching_dims: []const i64, -// start_indices_batching_dims: []const i64, -// start_index_map: []const i64, -// index_vector_dim: i64, -// indices_are_sorted: bool = false, -// }, -// ) *mlir.Operation { -// return mlir.Operation.make( -// ctx, -// "stablehlo.gather", -// .{ -// .operands = .{ .flat = &.{ value, indices } }, -// .result_type_inference = true, -// .attributes = &.{ -// .{ "dimension_numbers", GatherDimensionNumbersAttribute.init( -// ctx, -// args.offset_dims, -// args.collapsed_slice_dims, -// args.operand_batching_dims, -// args.start_indices_batching_dims, -// args.start_index_map, -// args.index_vector_dim, -// ).asAttr() }, -// .{ "slice_sizes", .dense(ctx, .i64, slice_sizes) }, -// .{ "indices_are_sorted", .boolean(ctx, args.indices_are_sorted) }, -// }, -// .location = location, -// }, -// ); -// } +pub fn gather( + ctx: *mlir.Context, + value: *const mlir.Value, + indices: *const mlir.Value, + slice_sizes: []const i64, + location: *const mlir.Location, + args: struct { + offset_dims: []const i64, + collapsed_slice_dims: []const i64, + operand_batching_dims: []const i64, + start_indices_batching_dims: []const i64, + start_index_map: []const i64, + index_vector_dim: i64, + indices_are_sorted: bool = false, + }, +) *mlir.Operation { + return mlir.Operation.make( + ctx, + "stablehlo.gather", + .{ + .operands = .{ .flat = &.{ value, indices } }, + .result_type_inference = true, + .attributes = &.{ + .named(ctx, "dimension_numbers", gatherDimensionNumbersAttribute( + ctx, + args.offset_dims, + args.collapsed_slice_dims, + args.operand_batching_dims, + args.start_indices_batching_dims, + args.start_index_map, + args.index_vector_dim, + )), + .named(ctx, "slice_sizes", mlir.denseArrayAttribute(ctx, .i64, slice_sizes)), + .named(ctx, "indices_are_sorted", mlir.boolAttribute(ctx, args.indices_are_sorted)), + }, + .location = location, + }, + ); +} // fn elementTypeOrSelf(typ: *const mlir.Type) *const mlir.Type { // return if (typ.as(mlir.ShapedType)) |shaped| { @@ -1012,85 +1012,103 @@ pub fn dotDimensionNumbersAttribute(ctx: *mlir.Context, args: DotDimensionNumber return @ptrCast(DotDimensionNumbersAttribute.init(ctx, args)); } -// pub const GatherDimensionNumbersAttribute = struct { -// _inner: c.MlirAttribute, +pub const GatherDimensionNumbersAttribute = opaque { + const M = mlir.Methods(GatherDimensionNumbersAttribute, mlir.Attribute); -// pub const isAFn = c.stablehloAttributeIsAGatherDimensionNumbers; -// const Self = GatherDimensionNumbersAttribute; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); + pub const isAFn = c.stablehloAttributeIsAGatherDimensionNumbers; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.eql(c.mlirAttributePrint); -// pub fn init( -// ctx: *mlir.Context, -// offset_dims: []const i64, -// collapsed_slice_dims: []const i64, -// operand_batching_dims: []const i64, -// start_indices_batching_dims: []const i64, -// start_index_map: []const i64, -// index_vector_dim: i64, -// ) Self { -// return .{ -// ._inner = c.stablehloGatherDimensionNumbersGet( -// ctx._inner, -// @intCast(offset_dims.len), -// offset_dims.ptr, -// @intCast(collapsed_slice_dims.len), -// collapsed_slice_dims.ptr, -// @intCast(operand_batching_dims.len), -// operand_batching_dims.ptr, -// @intCast(start_indices_batching_dims.len), -// start_indices_batching_dims.ptr, -// @intCast(start_index_map.len), -// start_index_map.ptr, -// index_vector_dim, -// ), -// }; -// } + pub fn init( + ctx: *mlir.Context, + offset_dims: []const i64, + collapsed_slice_dims: []const i64, + operand_batching_dims: []const i64, + start_indices_batching_dims: []const i64, + start_index_map: []const i64, + index_vector_dim: i64, + ) *const GatherDimensionNumbersAttribute { + return @ptrCast(c.stablehloGatherDimensionNumbersGet( + ctx.ptr(), + @intCast(offset_dims.len), + offset_dims.ptr, + @intCast(collapsed_slice_dims.len), + collapsed_slice_dims.ptr, + @intCast(operand_batching_dims.len), + operand_batching_dims.ptr, + @intCast(start_indices_batching_dims.len), + start_indices_batching_dims.ptr, + @intCast(start_index_map.len), + start_index_map.ptr, + index_vector_dim, + ).ptr); + } -// pub fn getOffsetDimsSize(self: Self) usize { -// return @intCast(c.stablehloGatherDimensionNumbersGetOffsetDimsSize(self._inner)); -// } + pub fn getOffsetDimsSize(self: *const GatherDimensionNumbersAttribute) usize { + return @intCast(c.stablehloGatherDimensionNumbersGetOffsetDimsSize(self.ptr())); + } -// pub fn getOffsetDimsElem(self: Self, pos: usize) i64 { -// return c.stablehloGatherDimensionNumbersGetOffsetDimsElem(self._inner, @intCast(pos)); -// } + pub fn getOffsetDimsElem(self: *const GatherDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloGatherDimensionNumbersGetOffsetDimsElem(self.ptr(), @intCast(pos)); + } -// pub fn getCollapsedSliceDimsSize(self: Self) usize { -// return @intCast(c.stablehloGatherDimensionNumbersGetCollapsedSliceDimsSize(self._inner)); -// } + pub fn getCollapsedSliceDimsSize(self: *const GatherDimensionNumbersAttribute) usize { + return @intCast(c.stablehloGatherDimensionNumbersGetCollapsedSliceDimsSize(self.ptr())); + } -// pub fn getCollapsedSliceDimsElem(self: Self, pos: usize) i64 { -// return c.stablehloGatherDimensionNumbersGetCollapsedSliceDimsElem(self._inner, @intCast(pos)); -// } + pub fn getCollapsedSliceDimsElem(self: *const GatherDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloGatherDimensionNumbersGetCollapsedSliceDimsElem(self.ptr(), @intCast(pos)); + } -// pub fn getStartIndexMapSize(self: Self) usize { -// return @intCast(c.stablehloGatherDimensionNumbersGetStartIndexMapSize(self._inner)); -// } + pub fn getStartIndexMapSize(self: *const GatherDimensionNumbersAttribute) usize { + return @intCast(c.stablehloGatherDimensionNumbersGetStartIndexMapSize(self.ptr())); + } -// pub fn getOperandBatchingDimsSize(self: Self) usize { -// return @intCast(c.stablehloGatherDimensionNumbersGetOperandBatchingDimsSize(self._inner)); -// } + pub fn getOperandBatchingDimsSize(self: *const GatherDimensionNumbersAttribute) usize { + return @intCast(c.stablehloGatherDimensionNumbersGetOperandBatchingDimsSize(self.ptr())); + } -// pub fn getOperandBatchingDimsElem(self: Self, pos: usize) i64 { -// return c.stablehloGatherDimensionNumbersGetOperandBatchingDimsElem(self._inner, @intCast(pos)); -// } + pub fn getOperandBatchingDimsElem(self: *const GatherDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloGatherDimensionNumbersGetOperandBatchingDimsElem(self.ptr(), @intCast(pos)); + } -// pub fn getStartIndicesBatchingDimsSize(self: Self) usize { -// return @intCast(c.stablehloGatherDimensionNumbersGetStartIndicesBatchingDimsSize(self._inner)); -// } + pub fn getStartIndicesBatchingDimsSize(self: *const GatherDimensionNumbersAttribute) usize { + return @intCast(c.stablehloGatherDimensionNumbersGetStartIndicesBatchingDimsSize(self.ptr())); + } -// pub fn getStartIndicesBatchingDimsElem(self: Self, pos: usize) i64 { -// return c.stablehloGatherDimensionNumbersGetStartIndicesBatchingDimsElem(self._inner, @intCast(pos)); -// } + pub fn getStartIndicesBatchingDimsElem(self: *const GatherDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloGatherDimensionNumbersGetStartIndicesBatchingDimsElem(self.ptr(), @intCast(pos)); + } -// pub fn getStartIndexMapElem(self: Self, pos: usize) i64 { -// return c.stablehloGatherDimensionNumbersGetStartIndexMapElem(self._inner, @intCast(pos)); -// } + pub fn getStartIndexMapElem(self: *const GatherDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloGatherDimensionNumbersGetStartIndexMapElem(self.ptr(), @intCast(pos)); + } -// pub fn getIndexVectorDim(self: Self) usize { -// return @intCast(c.stablehloGatherDimensionNumbersGetIndexVectorDim(self._inner)); -// } -// }; + pub fn getIndexVectorDim(self: *const GatherDimensionNumbersAttribute) usize { + return @intCast(c.stablehloGatherDimensionNumbersGetIndexVectorDim(self.ptr())); + } +}; + +pub fn gatherDimensionNumbersAttribute( + ctx: *mlir.Context, + offset_dims: []const i64, + collapsed_slice_dims: []const i64, + operand_batching_dims: []const i64, + start_indices_batching_dims: []const i64, + start_index_map: []const i64, + index_vector_dim: i64, +) *const mlir.Attribute { + return @ptrCast(GatherDimensionNumbersAttribute.init( + ctx, + offset_dims, + collapsed_slice_dims, + operand_batching_dims, + start_indices_batching_dims, + start_index_map, + index_vector_dim, + )); +} // pub const ConvDimensionNumbersAttribute = struct { // _inner: c.MlirAttribute, From 1caa45638c43a707df3eebd57933eb6894a4487b Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 14:31:10 +0200 Subject: [PATCH 32/37] stablehlo: bound scatter --- mlir/dialects/stablehlo/stablehlo.zig | 102 +++++++++++++------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index a63bec55..a702fc4f 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -349,59 +349,59 @@ pub fn gather( // } else typ; // } -// pub const ScatterArgs = struct { -// update_window_dims: []const i64, -// inserted_window_dims: []const i64, -// input_batching_dims: []const i64, -// scatter_indices_batching_dims: []const i64, -// scatter_dims_to_operand_dims: []const i64, -// index_vector_dim: i64, -// indices_are_sorted: bool = false, -// unique_indices: bool = false, +pub const ScatterArgs = struct { + update_window_dims: []const i64, + inserted_window_dims: []const i64, + input_batching_dims: []const i64, + scatter_indices_batching_dims: []const i64, + scatter_dims_to_operand_dims: []const i64, + index_vector_dim: i64, + indices_are_sorted: bool = false, + unique_indices: bool = false, -// pub fn getScatterDimensionNumbers(self: ScatterArgs, ctx: *mlir.Context) *const mlir.Attribute { -// return .{ ._inner = c.stablehloScatterDimensionNumbersGet( -// ctx._inner, -// @intCast(self.update_window_dims.len), -// self.update_window_dims.ptr, -// @intCast(self.inserted_window_dims.len), -// self.inserted_window_dims.ptr, -// @intCast(self.input_batching_dims.len), -// self.input_batching_dims.ptr, -// @intCast(self.scatter_indices_batching_dims.len), -// self.scatter_indices_batching_dims.ptr, -// @intCast(self.scatter_dims_to_operand_dims.len), -// self.scatter_dims_to_operand_dims.ptr, -// self.index_vector_dim, -// ) }; -// } -// }; + pub fn getScatterDimensionNumbers(self: ScatterArgs, ctx: *mlir.Context) *const mlir.Attribute { + return @ptrCast(c.stablehloScatterDimensionNumbersGet( + ctx.ptr(), + @intCast(self.update_window_dims.len), + self.update_window_dims.ptr, + @intCast(self.inserted_window_dims.len), + self.inserted_window_dims.ptr, + @intCast(self.input_batching_dims.len), + self.input_batching_dims.ptr, + @intCast(self.scatter_indices_batching_dims.len), + self.scatter_indices_batching_dims.ptr, + @intCast(self.scatter_dims_to_operand_dims.len), + self.scatter_dims_to_operand_dims.ptr, + self.index_vector_dim, + ).ptr); + } +}; -// pub fn scatter( -// ctx: *mlir.Context, -// inputs: []const *const mlir.Value, -// scatter_indices: []const *const mlir.Value, -// updates: []const *const mlir.Value, -// update_block: mlir.Block, -// args: ScatterArgs, -// location: *const mlir.Location, -// ) *mlir.Operation { -// return mlir.Operation.make( -// ctx, -// "stablehlo.scatter", -// .{ -// .operands = .{ .variadic = &.{ inputs, scatter_indices, updates } }, -// .blocks = &.{update_block}, -// .attributes = &.{ -// .{ "scatter_dimension_numbers", args.getScatterDimensionNumbers(ctx) }, -// .{ "indices_are_sorted", .boolean(ctx, args.indices_are_sorted) }, -// .{ "unique_indices", .boolean(ctx, args.unique_indices) }, -// }, -// .result_type_inference = true, -// .location = location, -// }, -// ); -// } +pub fn scatter( + ctx: *mlir.Context, + inputs: []const *const mlir.Value, + scatter_indices: []const *const mlir.Value, + updates: []const *const mlir.Value, + update_block: *mlir.Block, + args: ScatterArgs, + location: *const mlir.Location, +) *mlir.Operation { + return mlir.Operation.make( + ctx, + "stablehlo.scatter", + .{ + .operands = .{ .variadic = &.{ inputs, scatter_indices, updates } }, + .blocks = &.{update_block}, + .attributes = &.{ + .named(ctx, "scatter_dimension_numbers", args.getScatterDimensionNumbers(ctx)), + .named(ctx, "indices_are_sorted", mlir.boolAttribute(ctx, args.indices_are_sorted)), + .named(ctx, "unique_indices", mlir.boolAttribute(ctx, args.unique_indices)), + }, + .result_type_inference = true, + .location = location, + }, + ); +} pub fn iota(ctx: *mlir.Context, dimension: i64, result_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.iota", .{ From 1bbb2242030bf0f2a19987c2630dfc0aeccba2e2 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 14:55:21 +0200 Subject: [PATCH 33/37] stablehlo: bound convolution --- mlir/dialects/stablehlo/stablehlo.zig | 276 +++++++++++++------------- 1 file changed, 139 insertions(+), 137 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index a702fc4f..4bcfc1b7 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -711,72 +711,70 @@ pub fn get_tuple_element(ctx: *mlir.Context, tuple_value: *const mlir.Value, ind }); } -// pub const ConvolutionOpts = struct { -// window_strides: []const i64, -// pad_value: []const i64, -// pad_shape: []const i64 = &.{}, -// lhs_dilation: []const i64, -// rhs_dilation: []const i64, -// window_reversal: []const bool, -// input_batch_dimension: i64, -// input_feature_dimension: i64, -// input_spatial_dimensions: []const i64, -// kernel_input_feature_dimension: i64, -// kernel_output_feature_dimension: i64, -// kernel_spatial_dimensions: []const i64, -// output_batch_dimension: i64, -// output_feature_dimension: i64, -// output_spatial_dimensions: []const i64, -// feature_group_count: i64, -// batch_group_count: i64, -// precision_config: []const PrecisionAttribute.Precision = &.{}, -// }; +pub const ConvolutionOpts = struct { + window_strides: []const i64, + pad_value: []const i64, + pad_shape: []const i64 = &.{}, + lhs_dilation: []const i64, + rhs_dilation: []const i64, + window_reversal: []const bool, + input_batch_dimension: i64, + input_feature_dimension: i64, + input_spatial_dimensions: []const i64, + kernel_input_feature_dimension: i64, + kernel_output_feature_dimension: i64, + kernel_spatial_dimensions: []const i64, + output_batch_dimension: i64, + output_feature_dimension: i64, + output_spatial_dimensions: []const i64, + feature_group_count: i64, + batch_group_count: i64, + precision_config: []const PrecisionAttribute.Precision = &.{}, +}; -// pub fn convolution( -// ctx: *mlir.Context, -// lhs: *const mlir.Value, -// rhs: *const mlir.Value, -// opts: ConvolutionOpts, -// res_type: *const mlir.Type, -// location: *const mlir.Location, -// ) *mlir.Operation { -// var max_precisions: [2]mlir.Attribute = undefined; -// for (opts.precision_config, 0..) |p, i| { -// max_precisions[i] = PrecisionAttribute.init(ctx, p).asAttr(); -// } -// var window_reversal: [3]i32 = undefined; -// for (opts.window_reversal, 0..) |w, i| { -// window_reversal[i] = @intCast(@intFromBool(w)); -// } -// return mlir.Operation.make(ctx, "stablehlo.convolution", .{ -// .operands = .{ .flat = &.{ lhs, rhs } }, -// .results = &.{res_type}, -// .attributes = &.{ -// .{ "window_strides", .dense(ctx, .i64, opts.window_strides) }, -// .{ "padding", .denseElements(ctx, opts.pad_shape, .i64, opts.pad_value) }, -// .{ "lhs_dilation", .dense(ctx, .i64, opts.lhs_dilation) }, -// .{ "rhs_dilation", .dense(ctx, .i64, opts.rhs_dilation) }, -// .{ "window_reversal", .dense(ctx, .bool, window_reversal[0..opts.window_reversal.len]) }, -// .{ -// "dimension_numbers", ConvDimensionNumbersAttribute.init(ctx, .{ -// .input_batch_dimension = opts.input_batch_dimension, -// .input_feature_dimension = opts.input_feature_dimension, -// .input_spatial_dimensions = opts.input_spatial_dimensions, -// .kernel_input_feature_dimension = opts.kernel_input_feature_dimension, -// .kernel_output_feature_dimension = opts.kernel_output_feature_dimension, -// .kernel_spatial_dimensions = opts.kernel_spatial_dimensions, -// .output_batch_dimension = opts.output_batch_dimension, -// .output_feature_dimension = opts.output_feature_dimension, -// .output_spatial_dimensions = opts.output_spatial_dimensions, -// }).asAttr(), -// }, -// .{ "feature_group_count", .int(ctx, .i64, opts.feature_group_count) }, -// .{ "batch_group_count", .int(ctx, .i64, opts.batch_group_count) }, -// .{ "precision_config", .array(ctx, &max_precisions) }, -// }, -// .location = location, -// }); -// } +pub fn convolution( + ctx: *mlir.Context, + lhs: *const mlir.Value, + rhs: *const mlir.Value, + opts: ConvolutionOpts, + res_type: *const mlir.Type, + location: *const mlir.Location, +) *mlir.Operation { + var max_precisions: [2]*const mlir.Attribute = undefined; + for (opts.precision_config, 0..) |p, i| { + max_precisions[i] = precisionAttribute(ctx, p); + } + var window_reversal: [3]i32 = undefined; + for (opts.window_reversal, 0..) |w, i| { + window_reversal[i] = @intCast(@intFromBool(w)); + } + return mlir.Operation.make(ctx, "stablehlo.convolution", .{ + .operands = .{ .flat = &.{ lhs, rhs } }, + .results = .{ .flat = &.{res_type} }, + .attributes = &.{ + .named(ctx, "window_strides", mlir.denseArrayAttribute(ctx, .i64, opts.window_strides)), + .named(ctx, "padding", mlir.denseElementsAttribute(mlir.RankedTensorType.get(opts.pad_shape, mlir.integerType(ctx, .i64)).shaped(), opts.pad_value)), + .named(ctx, "lhs_dilation", mlir.denseArrayAttribute(ctx, .i64, opts.lhs_dilation)), + .named(ctx, "rhs_dilation", mlir.denseArrayAttribute(ctx, .i64, opts.rhs_dilation)), + .named(ctx, "window_reversal", mlir.denseArrayAttribute(ctx, .bool, window_reversal[0..opts.window_reversal.len])), + .named(ctx, "dimension_numbers", convDimensionNumbersAttribute(ctx, .{ + .input_batch_dimension = opts.input_batch_dimension, + .input_feature_dimension = opts.input_feature_dimension, + .input_spatial_dimensions = opts.input_spatial_dimensions, + .kernel_input_feature_dimension = opts.kernel_input_feature_dimension, + .kernel_output_feature_dimension = opts.kernel_output_feature_dimension, + .kernel_spatial_dimensions = opts.kernel_spatial_dimensions, + .output_batch_dimension = opts.output_batch_dimension, + .output_feature_dimension = opts.output_feature_dimension, + .output_spatial_dimensions = opts.output_spatial_dimensions, + })), + .named(ctx, "feature_group_count", mlir.integerAttribute(ctx, .i64, opts.feature_group_count)), + .named(ctx, "batch_group_count", mlir.integerAttribute(ctx, .i64, opts.batch_group_count)), + .named(ctx, "precision_config", mlir.arrayAttribute(ctx, &max_precisions)), + }, + .location = location, + }); +} pub const CustomCallOpts = struct { pub const ApiVersion = enum(i32) { @@ -1110,92 +1108,96 @@ pub fn gatherDimensionNumbersAttribute( )); } -// pub const ConvDimensionNumbersAttribute = struct { -// _inner: c.MlirAttribute, +pub const ConvDimensionNumbersAttribute = opaque { + const M = mlir.Methods(ConvDimensionNumbersAttribute, mlir.Attribute); -// pub const isAFn = c.stablehloAttributeIsAConvDimensionNumbers; -// const Self = ConvDimensionNumbersAttribute; -// pub const asAttr = mlir.Attribute.fromAny(Self); -// pub const eql = mlir.Attribute.eqlAny(Self); + pub const isAFn = c.stablehloAttributeIsAConvDimensionNumbers; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); -// pub fn init(ctx: *mlir.Context, args: struct { -// input_batch_dimension: i64, -// input_feature_dimension: i64, -// input_spatial_dimensions: []const i64, -// kernel_input_feature_dimension: i64, -// kernel_output_feature_dimension: i64, -// kernel_spatial_dimensions: []const i64, -// output_batch_dimension: i64, -// output_feature_dimension: i64, -// output_spatial_dimensions: []const i64, -// }) Self { -// return .{ -// ._inner = c.stablehloConvDimensionNumbersGet( -// ctx._inner, -// args.input_batch_dimension, -// args.input_feature_dimension, -// @intCast(args.input_spatial_dimensions.len), -// args.input_spatial_dimensions.ptr, -// args.kernel_input_feature_dimension, -// args.kernel_output_feature_dimension, -// @intCast(args.kernel_spatial_dimensions.len), -// args.kernel_spatial_dimensions.ptr, -// args.output_batch_dimension, -// args.output_feature_dimension, -// @intCast(args.output_spatial_dimensions.len), -// args.output_spatial_dimensions.ptr, -// ), -// }; -// } + pub const InitArgs = struct { + input_batch_dimension: i64, + input_feature_dimension: i64, + input_spatial_dimensions: []const i64, + kernel_input_feature_dimension: i64, + kernel_output_feature_dimension: i64, + kernel_spatial_dimensions: []const i64, + output_batch_dimension: i64, + output_feature_dimension: i64, + output_spatial_dimensions: []const i64, + }; -// pub fn getInputBatchDimension(self: Self) i64 { -// return c.stablehloConvDimensionNumbersGetInputBatchDimension(self._inner); -// } + pub fn init(ctx: *mlir.Context, args: InitArgs) *const ConvDimensionNumbersAttribute { + return @ptrCast(c.stablehloConvDimensionNumbersGet( + ctx.ptr(), + args.input_batch_dimension, + args.input_feature_dimension, + @intCast(args.input_spatial_dimensions.len), + args.input_spatial_dimensions.ptr, + args.kernel_input_feature_dimension, + args.kernel_output_feature_dimension, + @intCast(args.kernel_spatial_dimensions.len), + args.kernel_spatial_dimensions.ptr, + args.output_batch_dimension, + args.output_feature_dimension, + @intCast(args.output_spatial_dimensions.len), + args.output_spatial_dimensions.ptr, + ).ptr); + } -// pub fn getInputFeatureDimension(self: Self) i64 { -// return c.stablehloConvDimensionNumbersGetInputFeatureDimension(self._inner); -// } + pub fn getInputBatchDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetInputBatchDimension(self.ptr()); + } -// pub fn getInputSpatialDimensionsSize(self: Self) usize { -// return @intCast(c.stablehloConvDimensionNumbersGetInputSpatialDimensionsSize(self._inner)); -// } + pub fn getInputFeatureDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetInputFeatureDimension(self.ptr()); + } -// pub fn getInputSpatialDimensionsElem(self: Self, pos: usize) i64 { -// return c.stablehloConvDimensionNumbersGetInputSpatialDimensionsElem(self._inner, @intCast(pos)); -// } + pub fn getInputSpatialDimensionsSize(self: *const ConvDimensionNumbersAttribute) usize { + return @intCast(c.stablehloConvDimensionNumbersGetInputSpatialDimensionsSize(self.ptr())); + } -// pub fn getKernelInputFeatureDimension(self: Self) i64 { -// return c.stablehloConvDimensionNumbersGetKernelInputFeatureDimension(self._inner); -// } + pub fn getInputSpatialDimensionsElem(self: *const ConvDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloConvDimensionNumbersGetInputSpatialDimensionsElem(self.ptr(), @intCast(pos)); + } -// pub fn getKernelOutputFeatureDimension(self: Self) i64 { -// return c.stablehloConvDimensionNumbersGetKernelOutputFeatureDimension(self._inner); -// } + pub fn getKernelInputFeatureDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetKernelInputFeatureDimension(self.ptr()); + } -// pub fn getKernelSpatialDimensionsSize(self: Self) usize { -// return @intCast(c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsSize(self._inner)); -// } + pub fn getKernelOutputFeatureDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetKernelOutputFeatureDimension(self.ptr()); + } -// pub fn getKernelSpatialDimensionsElem(self: Self, pos: usize) i64 { -// return c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsElem(self._inner, @intCast(pos)); -// } + pub fn getKernelSpatialDimensionsSize(self: *const ConvDimensionNumbersAttribute) usize { + return @intCast(c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsSize(self.ptr())); + } -// pub fn getOutputBatchDimension(self: Self) i64 { -// return c.stablehloConvDimensionNumbersGetOutputBatchDimension(self._inner); -// } + pub fn getKernelSpatialDimensionsElem(self: *const ConvDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsElem(self.ptr(), @intCast(pos)); + } -// pub fn getOutputFeatureDimension(self: Self) i64 { -// return c.stablehloConvDimensionNumbersGetOutputFeatureDimension(self._inner); -// } + pub fn getOutputBatchDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetOutputBatchDimension(self.ptr()); + } -// pub fn getOutputSpatialDimensionsSize(self: Self) usize { -// return @intCast(c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsSize(self._inner)); -// } + pub fn getOutputFeatureDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetOutputFeatureDimension(self.ptr()); + } -// pub fn getOutputSpatialDimensionsElem(self: Self, pos: usize) i64 { -// return c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsElem(self._inner, @intCast(pos)); -// } -// }; + pub fn getOutputSpatialDimensionsSize(self: *const ConvDimensionNumbersAttribute) usize { + return @intCast(c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsSize(self.ptr())); + } + + pub fn getOutputSpatialDimensionsElem(self: *const ConvDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsElem(self.ptr(), @intCast(pos)); + } +}; + +pub fn convDimensionNumbersAttribute(ctx: *mlir.Context, args: ConvDimensionNumbersAttribute.InitArgs) *const mlir.Attribute { + return @ptrCast(ConvDimensionNumbersAttribute.init(ctx, args)); +} pub const OutputOperandAliasAttribute = opaque { const M = mlir.Methods(OutputOperandAliasAttribute, c.MlirAttribute); From c78c8402c49785126b87816bcea6ab8ca01b2120 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 15:03:12 +0200 Subject: [PATCH 34/37] stablehlo: added missing methods --- mlir/dialects/stablehlo/stablehlo.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 4bcfc1b7..75d7b08f 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -1263,6 +1263,7 @@ pub const ComparisonDirection = opaque { pub const isAFn = c.stablehloAttributeIsAComparisonDirectionAttr; pub const ptr = M.ptr; pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); pub const Direction = enum { EQ, @@ -1293,6 +1294,7 @@ pub const CompareType = opaque { pub const isAFn = c.stablehloAttributeIsAComparisonTypeAttr; pub const ptr = M.ptr; pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); pub const Type = enum { SIGNED, @@ -1321,6 +1323,7 @@ pub const TransposeAttribute = opaque { pub const isAFn = c.stablehloAttributeIsATransposeAttr; pub const ptr = M.ptr; pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); pub const Type = enum { NO_TRANSPOSE, @@ -1348,6 +1351,7 @@ pub const FftType = opaque { pub const isAFn = c.stablehloAttributeIsAFftTypeAttr; pub const ptr = M.ptr; pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); pub const Type = enum { FFT, From c79427e3f49faab3f3776757ed861ad9d3b260d7 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 16:22:11 +0200 Subject: [PATCH 35/37] stablehlo: reorder parameters for consistency --- mlir/dialects/stablehlo/stablehlo.zig | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 75d7b08f..cbf8d825 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -169,7 +169,6 @@ pub fn dot_general( lhs: *const mlir.Value, rhs: *const mlir.Value, result_type: *const mlir.Type, - location: *const mlir.Location, opts: struct { lhs_batching_dimensions: []const i64, rhs_batching_dimensions: []const i64, @@ -177,6 +176,7 @@ pub fn dot_general( rhs_contracting_dimensions: []const i64, dot_precision: DotPrecision, }, + location: *const mlir.Location, ) *mlir.Operation { const precisions: [2]*const mlir.Attribute = @splat(precisionAttribute(ctx, opts.dot_precision.precision())); const attributes = [3]mlir.NamedAttribute{ @@ -244,8 +244,8 @@ pub fn transpose( ctx: *mlir.Context, value: *const mlir.Value, result_type: *const mlir.Type, - location: *const mlir.Location, opts: struct { permutation: []const i64 }, + location: *const mlir.Location, ) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.transpose", .{ .operands = .{ .flat = &.{value} }, @@ -308,7 +308,6 @@ pub fn gather( value: *const mlir.Value, indices: *const mlir.Value, slice_sizes: []const i64, - location: *const mlir.Location, args: struct { offset_dims: []const i64, collapsed_slice_dims: []const i64, @@ -318,6 +317,7 @@ pub fn gather( index_vector_dim: i64, indices_are_sorted: bool = false, }, + location: *const mlir.Location, ) *mlir.Operation { return mlir.Operation.make( ctx, @@ -548,7 +548,7 @@ pub const PadOpts = struct { interior: []const i64, }; -pub fn pad(ctx: *mlir.Context, value: *const mlir.Value, padding_value: *const mlir.Value, opts: PadOpts, location: *const mlir.Location) *mlir.Operation { +pub fn pad(ctx: *mlir.Context, value: *const mlir.Value, padding_value: *const mlir.Value, location: *const mlir.Location, opts: PadOpts) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.pad", .{ .operands = .{ .flat = &.{ value, padding_value } }, .result_type_inference = true, @@ -568,7 +568,7 @@ pub const TriangularSolveOpts = struct { transpose_a: TransposeAttribute.Type, }; -pub fn triangular_solve(ctx: *mlir.Context, value: *const mlir.Value, other: *const mlir.Value, location: *const mlir.Location, opts: TriangularSolveOpts) *mlir.Operation { +pub fn triangular_solve(ctx: *mlir.Context, value: *const mlir.Value, other: *const mlir.Value, opts: TriangularSolveOpts, location: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.triangular_solve", .{ .operands = .{ .flat = &.{ value, other } }, .result_type_inference = true, @@ -587,7 +587,7 @@ pub const FftOpts = struct { length: []const i64, }; -pub fn fft(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location, opts: FftOpts) *mlir.Operation { +pub fn fft(ctx: *mlir.Context, value: *const mlir.Value, opts: FftOpts, location: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.fft", .{ .operands = .{ .flat = &.{value} }, .result_type_inference = true, @@ -669,7 +669,14 @@ pub fn rng(ctx: *mlir.Context, a: *const mlir.Value, b: *const mlir.Value, shape }); } -pub fn rng_bit_generator(ctx: *mlir.Context, rng_algorithm: RngAlgorithm.Type, initial_state: *const mlir.Value, res_state_type: *const mlir.Type, res_type: *const mlir.Type, location: *const mlir.Location) *mlir.Operation { +pub fn rng_bit_generator( + ctx: *mlir.Context, + rng_algorithm: RngAlgorithm.Type, + initial_state: *const mlir.Value, + res_state_type: *const mlir.Type, + res_type: *const mlir.Type, + location: *const mlir.Location, +) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.rng_bit_generator", .{ .operands = .{ .flat = &.{initial_state} }, .results = &.{ res_state_type, res_type }, @@ -736,8 +743,8 @@ pub fn convolution( ctx: *mlir.Context, lhs: *const mlir.Value, rhs: *const mlir.Value, - opts: ConvolutionOpts, res_type: *const mlir.Type, + opts: ConvolutionOpts, location: *const mlir.Location, ) *mlir.Operation { var max_precisions: [2]*const mlir.Attribute = undefined; From c936aec3476bfb41aa5974057d1ec094ae5ba3e4 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 16:31:28 +0200 Subject: [PATCH 36/37] mlir: swap new mlir module --- mlir/BUILD.bazel | 21 +- mlir/dialects/BUILD.bazel | 2 +- mlir/dialects/func.zig | 2 +- mlir/dialects/stablehlo/BUILD.bazel | 2 +- mlir/dialects/stablehlo/stablehlo.zig | 2 +- mlir/main.zig | 2 +- mlir/mlir.zig | 2795 +++++++++++-------------- mlir/registry.zig | 1466 ------------- 8 files changed, 1249 insertions(+), 3043 deletions(-) mode change 100755 => 100644 mlir/mlir.zig delete mode 100644 mlir/registry.zig diff --git a/mlir/BUILD.bazel b/mlir/BUILD.bazel index 73f1d12d..e91a2c91 100644 --- a/mlir/BUILD.bazel +++ b/mlir/BUILD.bazel @@ -28,33 +28,14 @@ zig_test( deps = [":mlir"], ) -zig_library( - name = "mlir2", - main = "registry.zig", - visibility = ["//visibility:public"], - deps = [ - ":c", - "//stdx", - ], -) - zig_binary( name = "main", main = "main.zig", visibility = ["//visibility:public"], deps = [ - ":mlir2", + ":mlir", "//mlir/dialects", # ":c", # "//stdx", ], ) - -zig_test( - name = "test2", - deps = [ - ":mlir2", - # ":c", - # "//stdx", - ], -) diff --git a/mlir/dialects/BUILD.bazel b/mlir/dialects/BUILD.bazel index 34400ca9..8b07057c 100644 --- a/mlir/dialects/BUILD.bazel +++ b/mlir/dialects/BUILD.bazel @@ -13,7 +13,7 @@ zig_library( main = "dialects.zig", visibility = ["//visibility:public"], deps = [ - "//mlir:mlir2", + "//mlir:mlir", "//mlir/dialects/stablehlo", "//stdx", ], diff --git a/mlir/dialects/func.zig b/mlir/dialects/func.zig index d316f160..c2af12a5 100644 --- a/mlir/dialects/func.zig +++ b/mlir/dialects/func.zig @@ -1,6 +1,6 @@ const std = @import("std"); -const mlir = @import("mlir2"); +const mlir = @import("mlir"); const stdx = @import("stdx"); const FuncOpArgs = struct { diff --git a/mlir/dialects/stablehlo/BUILD.bazel b/mlir/dialects/stablehlo/BUILD.bazel index 442e8c7b..0596357a 100644 --- a/mlir/dialects/stablehlo/BUILD.bazel +++ b/mlir/dialects/stablehlo/BUILD.bazel @@ -7,7 +7,7 @@ zig_library( visibility = ["//mlir/dialects:__subpackages__"], deps = [ "//mlir:c", - "//mlir:mlir2", + "//mlir:mlir", "//stdx", "@stablehlo//:stablehlo_dialect_capi", ], diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index cbf8d825..8789c7b0 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -1,7 +1,7 @@ const std = @import("std"); const c = @import("c"); -const mlir = @import("mlir2"); +const mlir = @import("mlir"); const stringRef = mlir.stringRef; const stdx = @import("stdx"); diff --git a/mlir/main.zig b/mlir/main.zig index 4bf94117..8eee2bbe 100644 --- a/mlir/main.zig +++ b/mlir/main.zig @@ -1,7 +1,7 @@ const std = @import("std"); const dialects = @import("mlir/dialects"); -const mlir = @import("mlir2"); +const mlir = @import("mlir"); pub fn main() !void { const allocator = std.heap.c_allocator; diff --git a/mlir/mlir.zig b/mlir/mlir.zig old mode 100755 new mode 100644 index a222c40a..7536e701 --- a/mlir/mlir.zig +++ b/mlir/mlir.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const builtin = @import("builtin"); const c = @import("c"); const stdx = @import("stdx"); @@ -7,9 +6,7 @@ const stdx = @import("stdx"); const log = std.log.scoped(.mlir); test { - std.testing.refAllDecls(@This()); - - _ = try Context.init(); + std.testing.refAllDeclsRecursive(@This()); } const Error = error{ @@ -19,954 +16,1055 @@ const Error = error{ MlirUnexpected, /// A resource/executor was not found. NotFound, - OutOfMemory, -}; +} || std.mem.Allocator.Error; pub inline fn stringRef(str: []const u8) c.MlirStringRef { return .{ .data = str.ptr, .length = str.len }; } -pub inline fn fromStringRef(str: c.MlirStringRef) []const u8 { +pub inline fn string(str: c.MlirStringRef) []const u8 { // Note: mlir.StringRef need not to be null terminated. return str.data[0..str.length]; } +pub const StringCallbackCtx = struct { + writer: *std.Io.Writer, + err: ?std.Io.Writer.Error = null, +}; + +pub fn stringCallback(str: c.MlirStringRef, ctx_: ?*anyopaque) callconv(.c) void { + const ctx: *StringCallbackCtx = @ptrCast(@alignCast(ctx_)); + ctx.writer.writeAll(string(str)) catch |err| { + ctx.err = err; + }; +} + pub fn registerPasses(comptime passes: []const u8) void { @field(c, "mlirRegister" ++ passes ++ "Passes")(); } -pub fn successOr(res: c.MlirLogicalResult, err: anytype) !void { - return if (res.value == 0) err; +fn isPtrConst(comptime T: type) bool { + return @typeInfo(T).pointer.is_const; } -/// Alternative to MlirWrapperType -pub const MlirStrCallback = fn (c.MlirStringRef, ?*anyopaque) callconv(.c) void; - -pub const Registry = struct { - _inner: c.MlirDialectRegistry, - - pub const deinit = helpers.deinit(Registry, c.mlirDialectRegistryDestroy); - - pub fn init() !Registry { - return helpers.init(Registry, c.mlirDialectRegistryCreate(), c.mlirDialectRegistryIsNull) orelse Error.MlirUnexpected; - } -}; - -pub const Context = struct { - _inner: c.MlirContext, - const Self = Context; - pub const deinit = helpers.deinit(Context, c.mlirContextDestroy); - pub const wrapOr = helpers.wrapOr(Context, c.mlirContextIsNull); - - pub fn init() !Self { - return Self.wrapOr(c.mlirContextCreate()) orelse Error.MlirUnexpected; - } - - pub fn initWithRegistry(registry: Registry, threadingEnabled: bool) !Self { - return Self.wrapOr( - c.mlirContextCreateWithRegistry(registry._inner, threadingEnabled), - ) orelse Error.InvalidMlir; - } - - pub fn setMultiThreading(self: *Self, enabled: bool) void { - c.mlirContextEnableMultithreading(self._inner, enabled); - } - - pub fn appendDialectRegistry(self: *Self, registry: Registry) void { - c.mlirContextAppendDialectRegistry(self._inner, registry._inner); - } - - pub fn loadAllAvailableDialects(self: *Self) void { - c.mlirContextLoadAllAvailableDialects(self._inner); - } - - pub fn numRegisteredDialects(self: Self) usize { - return @intCast(c.mlirContextGetNumRegisteredDialects(self._inner)); - } - - pub fn numLoadedDialects(self: Self) usize { - return @intCast(c.mlirContextGetNumLoadedDialects(self._inner)); - } - - pub fn isRegisteredOperation(self: Self, op: [:0]const u8) bool { - return c.mlirContextIsRegisteredOperation(self._inner, stringRef(op)); - } - - pub fn location(self: Self, src: std.builtin.SourceLocation) Location { - return Location.fromSrc(self, src); - } -}; - -pub const Module = struct { - _inner: c.MlirModule, - - pub const deinit = helpers.deinit(Module, c.mlirModuleDestroy); - pub const wrapOr = helpers.wrapOr(Module, c.mlirModuleIsNull); - - const Self = Module; - - pub fn init(loc: Location) Self { - return .{ ._inner = c.mlirModuleCreateEmpty(loc._inner) }; - } - - pub fn parse(ctx: Context, source: [:0]const u8) !Module { - return Module.wrapOr( - c.mlirModuleCreateParse(ctx._inner, stringRef(source)), - ) orelse Error.InvalidMlir; - } - - pub fn fromOperation(operation: Operation) Module { - return .{ ._inner = c.mlirModuleFromOperation(operation._inner) }; - } - - pub fn context(self: Module) Context { - return .{ ._inner = c.mlirModuleGetContext(self._inner) }; - } - - pub fn getBody(self: Module) Block { - return .{ ._inner = c.mlirModuleGetBody(self._inner) }; - } - - pub fn op(self: Module) Operation { - return .{ ._inner = c.mlirModuleGetOperation(self._inner) }; - } - - pub fn hash(self: Module, hasher: *std.hash.XxHash64) void { - return self.op().hash(hasher); - } -}; - -pub const PassManager = struct { - _inner: c.MlirPassManager, - - pub const deinit = helpers.deinit(PassManager, c.mlirPassManagerDestroy); - pub const wrapOr = helpers.wrapOr(PassManager, c.mlirPassManagerIsNull); - - const Self = PassManager; - - pub fn init(ctx: Context) !Self { - return Self.wrapOr( - c.mlirPassManagerCreate(ctx._inner), - ) orelse Error.MlirUnexpected; - } - - pub fn initOnOperation(ctx: Context, op: [:0]const u8) !Self { - return Self.wrapOr( - c.mlirPassManagerCreateOnOperation(ctx._inner, stringRef(op)), - ) orelse Error.MlirUnexpected; - } - - pub fn asOpPassManager(self: Self) OpPassManager { - return .{ ._inner = c.mlirPassManagerGetAsOpPassManager(self._inner) }; - } - - pub fn enableIRPrinting(self: *Self) void { - c.mlirPassManagerEnableIRPrinting(self._inner); - } - - pub fn runOnOp(self: *Self, op: Operation) error{InvalidMlir}!void { - if (c.mlirPassManagerRunOnOp(self._inner, op._inner).value == 0) { - return Error.InvalidMlir; - } - } -}; - -fn _mlir_passpipeline_error(err: c.MlirStringRef, ctx: ?*anyopaque) callconv(.c) void { - _ = ctx; - std.debug.print(">>ERROR: {s}\n", .{err.data}); +fn CastedPtr(comptime T1: type, comptime T2: type) type { + return if (@typeInfo(T1).pointer.is_const) *const T2 else *T2; } -pub const OpPassManager = struct { - _inner: c.MlirOpPassManager, - - pub fn addPipeline(self: *OpPassManager, pipeline: [:0]const u8) error{OutOfMemory}!void { - if (c.mlirOpPassManagerAddPipeline( - self._inner, - stringRef(pipeline), - &_mlir_passpipeline_error, - null, - ).value == 0) { - return Error.OutOfMemory; - } - } -}; - -pub const Identifier = struct { - _inner: c.MlirIdentifier, - const Self = Identifier; - - pub fn get(ctx: Context, str_: [:0]const u8) Self { - return .{ ._inner = c.mlirIdentifierGet(ctx._inner, stringRef(str_)) }; - } - - pub fn context(self: Self) Context { - return .{ ._inner = c.mlirIdentifierGetContext(self._inner) }; - } - - pub fn str(self: Self) []const u8 { - return fromStringRef(c.mlirIdentifierStr(self._inner)); - } - - pub fn equals(self: Self, other: Self) bool { - return c.mlirIdentifierEqual(self._inner, other._inner); - } -}; - -pub const AttrTuple = struct { [:0]const u8, Attribute }; - -pub const Attribute = struct { - _inner: c.MlirAttribute, - - pub const dump = helpers.dump(Attribute, c.mlirAttributeDump); - pub const eql = helpers.eql(Attribute, c.mlirAttributeEqual); - pub const format = helpers.format(Attribute, c.mlirAttributePrint); - pub const wrapOr = helpers.wrapOr(Attribute, c.mlirAttributeIsNull); - - pub fn wrap(c_attr: c.MlirAttribute) Attribute { - return .{ ._inner = c_attr }; - } - - pub fn parse(ctx: Context, attr: [:0]const u8) !Attribute { - return Attribute.wrapOr( - c.mlirAttributeParseGet(ctx._inner, stringRef(attr)), - ) orelse Error.InvalidMlir; - } - - pub fn fromAny(SpecificAttr: type) fn (x: SpecificAttr) Attribute { - return struct { - fn cast(x: SpecificAttr) Attribute { - return .{ ._inner = x._inner }; - } - }.cast; - } - - pub fn isA(self: Attribute, SpecificAttr: type) bool { - return SpecificAttr.is_a_fn(self._inner); - } - - // utilities function to built common attributes. - // All attributes are upcasted to the Attribute type, making it easier to chain construct, - // but losing type information. - - pub fn null_() Attribute { - return .wrap(c.mlirAttributeGetNull()); - } - - pub fn string(ctx: Context, str: []const u8) Attribute { - return StringAttribute.init(ctx, str).asAttr(); - } - - pub fn type_(t: Type) Attribute { - return TypeAttribute.init(t).asAttr(); - } - - pub fn unit(ctx: Context) Attribute { - return .wrap(c.mlirUnitAttrGet(ctx._inner)); - } - - pub fn boolean(ctx: Context, value: bool) Attribute { - return BoolAttribute.init(ctx, value).asAttr(); - } - - pub fn i1FromBool(ctx: Context, value: bool) Attribute { - return IntegerAttribute(.i1).init(ctx, @intFromBool(value)).asAttr(); - } - - pub fn int(ctx: Context, comptime int_type: IntegerTypes, value: i64) Attribute { - return IntegerAttribute(int_type).init(ctx, value).asAttr(); - } - - pub fn float(ctx: Context, comptime float_type: FloatTypes, value: f64) Attribute { - return FloatAttribute(float_type).init(ctx, value).asAttr(); - } - - pub fn array(ctx: Context, attrs: []const Attribute) Attribute { - return ArrayAttribute.init(ctx, attrs).asAttr(); - } - - pub fn dense(ctx: Context, comptime dt: DenseArrayTypes, values: []const dt.ZigType()) Attribute { - return DenseArrayAttribute(dt).init(ctx, values).asAttr(); - } - - /// Use a tensor as an attribute. - /// The tensor is specified by dims, dtype and a flat slice of values. - pub fn denseElements(ctx: Context, dims: []const i64, comptime dt: DenseElementsAttributeTypes, values: []const dt.ZigType()) Attribute { - return DenseElementsAttribute(dt).init(.tensor(dims, dt.mlirType(ctx)), values).asAttr(); - } - - pub fn denseElementsFromBytes(ctx: Context, dims: []const i64, dt: DenseElementsAttributeTypes, raw_bytes: []const u8) Attribute { - const shape: Type = .tensor(dims, dt.mlirType(ctx)); - return .{ ._inner = c.mlirDenseElementsAttrRawBufferGet( - shape._inner, - @intCast(raw_bytes.len), - raw_bytes.ptr, - ) }; - } - - pub fn symbol(ctx: Context, flat_name: [:0]const u8) Attribute { - return FlatSymbolRefAttribute.init(ctx, flat_name).asAttr(); - } - - pub fn named(attr: Attribute, ctx: Context, name: [:0]const u8) NamedAttribute { - return NamedAttribute.named(ctx, name, attr); - } - - pub fn dict(ctx: Context, named_attrs: []const AttrTuple) Attribute { - var attr_buf: [32]NamedAttribute = undefined; - stdx.debug.assert(named_attrs.len <= attr_buf.len, ".dict helper only support up to {} attribute, got {}. You will need to call mlir.DictionaryAttribute directly", .{ attr_buf.len, named_attrs.len }); - - const attrs = attr_buf[0..named_attrs.len]; - for (attrs, named_attrs) |*attr, tuple| { - attr.* = .named(ctx, tuple[0], tuple[1]); - } - - return DictionaryAttribute.init(ctx, attrs).asAttr(); - } -}; - -pub const NamedAttribute = extern struct { - _inner: c.MlirNamedAttribute, - - pub fn wrap(c_named_attribute: c.MlirNamedAttribute) NamedAttribute { - return @bitCast(c_named_attribute); - } - - pub fn named(ctx: Context, name: [:0]const u8, attr: Attribute) NamedAttribute { - return .{ ._inner = .{ - .name = c.mlirIdentifierGet(ctx._inner, stringRef(name)), - .attribute = attr._inner, - } }; - } - - pub fn init(name: Identifier, attr: Attribute) NamedAttribute { - return .{ ._inner = .{ - .name = name._inner, - .attribute = attr._inner, - } }; - } -}; - -pub const StringAttribute = struct { - _inner: c.MlirAttribute, - pub const is_a_fn = c.mlirAttributeIsAString; - const Self = StringAttribute; - pub const asAttr = Attribute.fromAny(Self); - pub const eql = Attribute.eqlAny(Self); - - pub fn init(ctx: Context, str: []const u8) Self { - return .{ ._inner = c.mlirStringAttrGet(ctx._inner, stringRef(str)) }; - } - - pub fn value(self: Self) []const u8 { - return fromStringRef(c.mlirStringAttrGetValue(self._inner)); - } -}; - -pub const BoolAttribute = struct { - _inner: c.MlirAttribute, - pub const is_a_fn = c.mlirAttributeIsABool; - const Self = BoolAttribute; - pub const asAttr = Attribute.fromAny(Self); - pub const eql = Attribute.eqlAny(Self); - - pub fn init(ctx: Context, value_: bool) Self { - return .{ ._inner = c.mlirBoolAttrGet(ctx._inner, if (value_) 1 else 0) }; - } - - pub fn value(self: Self) bool { - return c.mlirBoolAttrGetValue(self._inner); - } -}; - -pub const TypeAttribute = struct { - _inner: c.MlirAttribute, - pub const is_a_fn = c.mlirAttributeIsAType; - pub const eql = Attribute.eqlAny(TypeAttribute); - - pub fn init(type_: Type) TypeAttribute { - return .{ ._inner = c.mlirTypeAttrGet(type_._inner) }; - } - - pub fn typ(self: TypeAttribute) Type { - return .{ ._inner = c.mlirAttributeGetType(self._inner) }; - } - - pub const asAttr = Attribute.fromAny(TypeAttribute); -}; - -pub const ArrayAttribute = struct { - _inner: c.MlirAttribute, - pub const is_a_fn = c.mlirAttributeIsAArray; - const Self = ArrayAttribute; - pub const asAttr = Attribute.fromAny(Self); - pub const eql = Attribute.eqlAny(Self); - - pub fn init(ctx: Context, attrs: []const Attribute) Self { - return .{ ._inner = c.mlirArrayAttrGet(ctx._inner, @intCast(attrs.len), @ptrCast(attrs.ptr)) }; - } - - pub fn size(self: Self) usize { - return @intCast(c.mlirArrayAttrGetNumElements(self._inner)); - } - - pub fn get(self: Self, index: usize) Attribute { - return .{ ._inner = c.mlirArrayAttrGetElement(self._inner, @intCast(index)) }; - } -}; - -pub fn IntegerAttribute(comptime it: IntegerTypes) type { - const ZigType, const getter = comptime switch (it) { - .i1, .i4, .i8, .i16, .i32, .i64 => .{ i64, c.mlirIntegerAttrGetValueInt }, - .si4, .si8, .si16, .si32, .si64 => .{ i64, c.mlirIntegerAttrGetValueSInt }, - .u4, .u8, .u16, .u32, .u64 => .{ u64, c.mlirIntegerAttrGetValueUInt }, - .unknown => @compileError("IntegerAttribute(unknown)"), - }; - +pub fn Methods(comptime T: type, comptime NativeT: type) type { return struct { - _inner: c.MlirAttribute, - pub const is_a_fn = c.mlirAttributeIsAInteger; + pub const IsPtrConst = @typeInfo(@typeInfo(@FieldType(NativeT, "ptr")).optional.child).pointer.is_const; + pub const Ptr = if (IsPtrConst) *const T else *T; - pub const IntegerTypeType = IntegerType(it); - const IntAttr = @This(); - - pub const asAttr = Attribute.fromAny(IntAttr); - pub const eql = Attribute.eqlAny(IntAttr); - - pub fn init(ctx: Context, value: i64) IntAttr { - return .{ ._inner = c.mlirIntegerAttrGet( - IntegerType(it).init(ctx)._inner, - value, - ) }; + pub fn ptr(self: anytype) NativeT { + return .{ .ptr = @ptrCast(@constCast(self)) }; } - pub fn get(value: IntAttr) ZigType { - return @intCast(getter(value._inner)); + fn format(f: anytype) fn (self: *const T, writer: *std.Io.Writer) std.Io.Writer.Error!void { + return struct { + fn format(self: *const T, writer: *std.Io.Writer) std.Io.Writer.Error!void { + var ctx: StringCallbackCtx = .{ .writer = writer }; + f(self.ptr(), stringCallback, &ctx); + if (ctx.err) |err| { + return err; + } + } + }.format; + } + + pub fn eql(f: anytype) fn (self: *const T, other: *const T) bool { + return struct { + fn eql(self: *const T, other: *const T) bool { + return f(self.ptr(), other.ptr()); + } + }.eql; + } + + pub fn isA(self: *const T, OtherType: type) ?CastedPtr(@TypeOf(self), OtherType) { + return if (OtherType.isAFn(self.ptr())) @ptrCast(self) else null; } }; } -pub fn FloatAttribute(comptime ft: FloatTypes) type { - return struct { - _inner: c.MlirAttribute, - pub const is_a_fn = c.mlirAttributeIsAFloat; - const FloatAttr = @This(); - pub const asAttr = Attribute.fromAny(FloatAttr); +pub const DialectRegistry = opaque { + const M = Methods(DialectRegistry, c.MlirDialectRegistry); - pub fn init(ctx: Context, value: f64) FloatAttr { - return .{ ._inner = c.mlirFloatAttrDoubleGet( - ctx._inner, - FloatType(ft).init(ctx)._inner, - value, - ) }; - } + pub const ptr = M.ptr; - pub fn get(value: FloatAttr) f64 { - return c.mlirFloatAttrGetValueDouble(value._inner); - } + pub fn init() std.mem.Allocator.Error!*DialectRegistry { + return @ptrCast(c.mlirDialectRegistryCreate().ptr orelse return std.mem.Allocator.Error.OutOfMemory); + } + + pub fn deinit(self: *DialectRegistry) void { + c.mlirDialectRegistryDestroy(self.ptr()); + } + + pub fn registerDialect(self: *DialectRegistry, comptime name: []const u8) void { + DialectHandle.fromString(name).insertDialect(self); + } +}; + +pub const Context = opaque { + const M = Methods(Context, c.MlirContext); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirContextEqual); + + pub const InitOptions = struct { + registry: ?*const DialectRegistry = null, + threading: bool = false, }; + + pub fn init(opts: InitOptions) std.mem.Allocator.Error!*Context { + const ctx = if (opts.registry) |r| c.mlirContextCreateWithRegistry( + r.ptr(), + opts.threading, + ) else c.mlirContextCreateWithThreading(opts.threading); + return @ptrCast(ctx.ptr orelse return std.mem.Allocator.Error.OutOfMemory); + } + + pub fn deinit(self: *Context) void { + c.mlirContextDestroy(self.ptr()); + } + + pub fn setMultiThreading(self: *Context, enabled: bool) void { + c.mlirContextEnableMultithreading(self.ptr(), enabled); + } + + pub fn appendDialectRegistry(self: *Context, registry: *const DialectRegistry) void { + c.mlirContextAppendDialectRegistry(self.ptr(), registry.ptr()); + } + + pub fn loadAllAvailableDialects(self: *Context) void { + c.mlirContextLoadAllAvailableDialects(self.ptr()); + } + + pub fn numRegisteredDialects(self: *const Context) usize { + return @intCast(c.mlirContextGetNumRegisteredDialects(self.ptr())); + } + + pub fn numLoadedDialects(self: *const Context) usize { + return @intCast(c.mlirContextGetNumLoadedDialects(self.ptr())); + } + + pub fn numThreads(self: *const Context) usize { + return @intCast(c.mlirContextGetNumThreads(self.ptr())); + } + + pub fn isRegisteredOperation(self: *const Context, op: []const u8) bool { + return c.mlirContextIsRegisteredOperation(self.ptr(), stringRef(op)); + } + + pub fn format(self: *const Context, writer: *std.Io.Writer) !void { + try writer.print("{s}{{ .numThreads = {d}, .numRegisteredDialects = {d}, .numLoadedDialects = {d} }}", .{ + @typeName(Context), + self.numThreads(), + self.numRegisteredDialects(), + self.numLoadedDialects(), + }); + } +}; + +pub const Location = opaque { + const M = Methods(Location, c.MlirLocation); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirLocationEqual); + pub const format = M.format(c.mlirLocationPrint); + + pub fn fromSrc(ctx: *Context, src: std.builtin.SourceLocation) *const Location { + return fileLineCol( + ctx, + src.file, + src.line, + src.column, + ); + } + + pub fn fileLineCol(ctx: *Context, file: []const u8, line: usize, column: usize) *const Location { + return @ptrCast(c.mlirLocationFileLineColGet( + ctx.ptr(), + stringRef(file), + @intCast(line), + @intCast(column), + ).ptr); + } + + pub fn callSite(callee: *const Location, caller: *const Location) *const Location { + return @ptrCast(c.mlirLocationCallSiteGet(callee.ptr(), caller.ptr()).ptr); + } + + pub fn fused(ctx: *Context, locs: []const *const Location, metadata: *const Attribute) *const Location { + return @ptrCast(c.mlirLocationFusedGet( + ctx.ptr(), + @intCast(locs.len), + @ptrCast(locs), + metadata.ptr(), + ).ptr); + } + + pub fn named(self: *const Location, ctx: *Context, loc_name: []const u8) *const Location { + return @ptrCast(c.mlirLocationNameGet(ctx.ptr(), stringRef(loc_name), self.ptr()).ptr); + } + + pub fn namedFmt(loc: *const Location, ctx: *Context, comptime fmt: []const u8, args: anytype) *const Location { + var buf: [256]u8 = undefined; + return loc.named(loc, ctx, std.fmt.bufPrint(&buf, fmt, args) catch blk: { + buf[buf.len - 3 ..].* = "...".*; + break :blk &buf; + }); + } + + pub fn fromAttribute(attribute: *const Attribute) *const Location { + return @ptrCast(c.mlirLocationFromAttribute(attribute.ptr()).ptr); + } + + pub fn unknown(ctx: *Context) *const Location { + return @ptrCast(c.mlirLocationUnknownGet(ctx.ptr()).ptr); + } +}; + +pub const Type = opaque { + const M = Methods(Type, c.MlirType); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + pub const isA = M.isA; + + pub fn parse(ctx: *Context, str: []const u8) Error!*const Type { + return @ptrCast(c.mlirTypeParseGet(ctx.ptr(), stringRef(str)).ptr orelse return Error.InvalidMlir); + } +}; + +pub const IndexType = opaque { + const M = Methods(IndexType, c.MlirType); + + pub const isAFn = c.mlirTypeIsAIndex; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + pub fn get(ctx: *Context) *const IndexType { + return @ptrCast(c.mlirIndexTypeGet(ctx.ptr()).ptr); + } +}; + +pub fn indexType(ctx: *Context) *const Type { + return @ptrCast(IndexType.get(ctx)); } -pub const DenseArrayTypes = enum { - bool, - i8, - i16, - i32, - i64, - f32, - f64, - - pub fn ZigType(comptime dt: DenseArrayTypes) type { - return switch (dt) { - .bool => i32, - .i8 => i8, - .i16 => i16, - .i32 => i32, - .i64 => i64, - .f32 => f32, - .f64 => f64, - }; - } -}; - -pub fn DenseArrayAttribute(comptime dt: DenseArrayTypes) type { - const _is_a_fn, const get_fn, const get_element_fn = switch (dt) { - .bool => .{ c.mlirAttributeIsADenseBoolArray, c.mlirDenseBoolArrayGet, c.mlirDenseBoolArrayGetElement }, - .i8 => .{ c.mlirAttributeIsADenseI8Array, c.mlirDenseI8ArrayGet, c.mlirDenseI8ArrayGetElement }, - .i16 => .{ c.mlirAttributeIsADenseI16Array, c.mlirDenseI16ArrayGet, c.mlirDenseI16ArrayGetElement }, - .i32 => .{ c.mlirAttributeIsADenseI32Array, c.mlirDenseI32ArrayGet, c.mlirDenseI32ArrayGetElement }, - .i64 => .{ c.mlirAttributeIsADenseI64Array, c.mlirDenseI64ArrayGet, c.mlirDenseI64ArrayGetElement }, - .f32 => .{ c.mlirAttributeIsADenseF32Array, c.mlirDenseF32ArrayGet, c.mlirDenseF32ArrayGetElement }, - .f64 => .{ c.mlirAttributeIsADenseF64Array, c.mlirDenseF64ArrayGet, c.mlirDenseF64ArrayGetElement }, - }; - - return struct { - _inner: c.MlirAttribute, - const Attr = @This(); - const ElementType = dt; - const ElementTypeZig = dt.ZigType(); - - pub const asAttr = Attribute.fromAny(Attr); - pub const eql = Attribute.eqlAny(Attr); - pub const is_a_fn = _is_a_fn; - - pub fn init(ctx: Context, values: []const ElementTypeZig) Attr { - return .{ ._inner = get_fn(ctx._inner, @intCast(values.len), @ptrCast(values.ptr)) }; - } - - pub fn get(self: Attr, pos: usize) ElementTypeZig { - return get_element_fn(self._inner, @intCast(pos)); - } - - pub fn len(self: Attr) usize { - return @intCast(c.mlirDenseArrayGetNumElements(self._inner)); - } - }; -} - -pub const DenseElementsAttributeTypes = enum { - bool, +pub const IntegerTypes = enum { + i1, + i4, i8, i16, i32, i64, + i128, + u1, + u4, u8, u16, u32, u64, + u128, + si1, + si4, + si8, + si16, + si32, + si64, + si128, + + pub fn bitwidth(self: IntegerTypes) usize { + return switch (self) { + .i1, .u1, .si1 => 1, + .i4, .u4, .si4 => 4, + .i8, .u8, .si8 => 8, + .i16, .u16, .si16 => 16, + .i32, .u32, .si32 => 32, + .i64, .u64, .si64 => 64, + .i128, .u128, .si128 => 128, + }; + } + + pub fn signedness(self: IntegerTypes) IntegerType.Signedness { + return switch (self) { + .i1, .i4, .i8, .i16, .i32, .i64, .i128 => .signless, + .si1, .si4, .si8, .si16, .si32, .si64, .si128 => .signed, + .u1, .u4, .u8, .u16, .u32, .u64, .u128 => .unsigned, + }; + } +}; + +pub const IntegerType = opaque { + pub const Signedness = enum { + signless, + signed, + unsigned, + }; + + const M = Methods(IntegerType, c.MlirType); + + pub const isAFn = c.mlirTypeIsAInteger; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + fn get(ctx: *Context, it: IntegerTypes) *const IntegerType { + return exact(ctx, it.bitwidth(), it.signedness()); + } + + fn exact(ctx: *Context, bitwidth: usize, sign: Signedness) *const IntegerType { + return @ptrCast(switch (sign) { + .signless => c.mlirIntegerTypeGet(ctx.ptr(), @intCast(bitwidth)).ptr, + .signed => c.mlirIntegerTypeSignedGet(ctx.ptr(), @intCast(bitwidth)).ptr, + .unsigned => c.mlirIntegerTypeUnsignedGet(ctx.ptr(), @intCast(bitwidth)).ptr, + }); + } +}; + +pub fn integerType(ctx: *Context, it: IntegerTypes) *const Type { + return @ptrCast(IntegerType.get(ctx, it)); +} + +pub const FloatTypes = enum { + f4e2m1fn, + f6e2m3fn, + f6e3m2fn, + f8e3m4, + f8e4m3, + f8e4m3b11fnuz, + f8e4m3fn, + f8e4m3fnuz, + f8e5m2, + f8e5m2fnuz, + f8e8m0fnu, bf16, f16, f32, f64, - index, - - pub fn ZigType(comptime dt: DenseElementsAttributeTypes) type { - return switch (dt) { - .bool => bool, - .i8 => i8, - .i16 => i16, - .i32 => i32, - .i64 => i64, - .u8 => u8, - .u16 => u16, - .u32 => u32, - .u64 => u64, - .bf16 => u16, - .f16 => f16, - .f32 => f32, - .f64 => f64, - .index => usize, - }; - } - - pub fn mlirType(dt: DenseElementsAttributeTypes, ctx: Context) Type { - return switch (dt) { - .bool => .int(ctx, .i1), - .i8 => .int(ctx, .i8), - .i16 => .int(ctx, .i16), - .i32 => .int(ctx, .i32), - .i64 => .int(ctx, .i64), - .u8 => .int(ctx, .u8), - .u16 => .int(ctx, .u16), - .u32 => .int(ctx, .u32), - .u64 => .int(ctx, .u64), - .bf16 => .float(ctx, .bf16), - .f16 => .float(ctx, .f16), - .f32 => .float(ctx, .f32), - .f64 => .float(ctx, .f64), - .index => .index(ctx), - }; - } + // f80, + // f128, + tf32, }; -pub fn DenseElementsAttribute(comptime dt: DenseElementsAttributeTypes) type { - return struct { - _inner: c.MlirAttribute, +pub fn FloatType(comptime ft: FloatTypes) type { + const _isAFn, const getter = switch (ft) { + .f4e2m1fn => .{ c.mlirTypeIsAFloat4E2M1FN, c.mlirFloat4E2M1FNTypeGet }, + .f6e2m3fn => .{ c.mlirTypeIsAFloat6E2M3FN, c.mlirFloat6E2M3FNTypeGet }, + .f6e3m2fn => .{ c.mlirTypeIsAFloat6E3M2FN, c.mlirFloat6E3M2FNTypeGet }, + .f8e3m4 => .{ c.mlirTypeIsAFloat8E3M4, c.mlirFloat8E3M4TypeGet }, + .f8e4m3 => .{ c.mlirTypeIsAFloat8E4M3, c.mlirFloat8E4M3TypeGet }, + .f8e4m3b11fnuz => .{ c.mlirTypeIsAFloat8E4M3B11FNUZ, c.mlirFloat8E4M3B11FNUZTypeGet }, + .f8e4m3fn => .{ c.mlirTypeIsAFloat8E4M3FN, c.mlirFloat8E4M3FNTypeGet }, + .f8e4m3fnuz => .{ c.mlirTypeIsAFloat8E4M3FNUZ, c.mlirFloat8E4M3FNUZTypeGet }, + .f8e5m2 => .{ c.mlirTypeIsAFloat8E5M2, c.mlirFloat8E5M2TypeGet }, + .f8e5m2fnuz => .{ c.mlirTypeIsAFloat8E5M2FNUZ, c.mlirFloat8E5M2FNUZTypeGet }, + .f8e8m0fnu => .{ c.mlirTypeIsAFloat8E8M0FNU, c.mlirFloat8E8M0FNUTypeGet }, + .bf16 => .{ c.mlirTypeIsABF16, c.mlirBF16TypeGet }, + .f16 => .{ c.mlirTypeIsAF16, c.mlirF16TypeGet }, + .f32 => .{ c.mlirTypeIsAF32, c.mlirF32TypeGet }, + .f64 => .{ c.mlirTypeIsAF64, c.mlirF64TypeGet }, + .tf32 => .{ c.mlirTypeIsATF32, c.mlirTF32TypeGet }, + }; - const Attr = @This(); + return opaque { + const Self = @This(); + const M = Methods(Self, c.MlirType); - pub const is_a_fn = c.mlirAttributeIsADenseElements; - pub const asAttr = Attribute.fromAny(Attr); - pub const eql = Attribute.eqlAny(Attr); + pub const isAFn = _isAFn; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); - pub fn init(shaped_type: Type, slice: []const dt.ZigType()) Attr { - const raw_bytes = std.mem.sliceAsBytes(slice); - const res: Attr = .{ ._inner = c.mlirDenseElementsAttrRawBufferGet( - shaped_type._inner, - @intCast(raw_bytes.len), - @ptrCast(raw_bytes.ptr), - ) }; - std.debug.assert(Attribute.wrapOr(res._inner) != null); - return res; - } - - pub fn len(self: Attr) usize { - return @intCast(c.mlirElementsAttrGetNumElements(self._inner)); - } - - pub fn items(self: Attr) []const dt.ZigType() { - const raw_bytes: [*]const u8 = c.mlirDenseElementsAttrGetRawData(self._inner) orelse unreachable; - const ptr: [*]const dt.ZigType() = @alignCast(@ptrCast(raw_bytes)); - // Note the mlir API returns us the number of elements, not the number of bytes, - // that's why we track the element type at comptime to allow items to work. - return ptr[0..self.len()]; - } - - pub fn bytes(self: Attr) []const u8 { - return std.mem.sliceAsBytes(self.items()); + pub fn get(ctx: *Context) *const Self { + return @ptrCast(getter(ctx.ptr()).ptr); } }; } -pub const FlatSymbolRefAttribute = struct { - _inner: c.MlirAttribute, - pub const is_a_fn = c.mlirAttributeIsAFlatSymbolRef; - const Self = FlatSymbolRefAttribute; - pub const eql = Attribute.eqlAny(Self); +pub fn floatType(ft: FloatTypes, ctx: *Context) *const Type { + return switch (ft) { + inline else => |v| @ptrCast(FloatType(v).get(ctx)), + }; +} - pub fn init(ctx: Context, str: [:0]const u8) Self { - return .{ ._inner = c.mlirFlatSymbolRefAttrGet(ctx._inner, stringRef(str)) }; +pub const Attribute = opaque { + const M = Methods(Attribute, c.MlirAttribute); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + pub const isA = M.isA; + + pub fn parse(ctx: *Context, attr: []const u8) Error!*const Attribute { + return @ptrCast(c.mlirAttributeParseGet(ctx.ptr(), stringRef(attr)).ptr orelse return Error.InvalidMlir); } - pub fn value(self: Self) []const u8 { - return fromStringRef(c.mlirFlatSymbolRefAttrGetValue(self._inner)); + pub fn context(self: *const Context) *Context { + return @ptrCast(c.mlirAttributeGetContext(self.ptr()).ptr); } - pub const asAttr = Attribute.fromAny(Self); + pub fn dialect(self: *const Context) *const Dialect { + return @ptrCast(c.mlirAttributeGetDialect(self.ptr()).ptr); + } +}; + +pub const StringAttribute = opaque { + const M = Methods(StringAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAString; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, str: []const u8) *const StringAttribute { + return @ptrCast(c.mlirStringAttrGet(ctx.ptr(), stringRef(str)).ptr); + } + + pub fn value(self: *const StringAttribute) []const u8 { + return string(c.mlirStringAttrGetValue(self.ptr())); + } +}; + +pub fn stringAttribute(ctx: *Context, str: []const u8) *const Attribute { + return @ptrCast(StringAttribute.init(ctx, str)); +} + +pub const IntegerAttribute = opaque { + const M = Methods(IntegerAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAInteger; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, it: IntegerTypes, value_: anytype) *const IntegerAttribute { + return @ptrCast(c.mlirIntegerAttrGet( + IntegerType.get(ctx, it).ptr(), + @intCast(value_), + ).ptr); + } + + pub fn value(self: *const IntegerAttribute, comptime T: type) T { + const getValue = switch (@typeInfo(T).int.signedness) { + .signed => c.mlirIntegerAttrGetValueInt, + .unsigned => c.mlirIntegerAttrGetValueUInt, + }; + return @intCast(getValue(self.ptr())); + } +}; + +pub fn integerAttribute(ctx: *Context, it: IntegerTypes, value_: anytype) *const Attribute { + return @ptrCast(IntegerAttribute.init(ctx, it, value_)); +} + +pub const BoolAttribute = opaque { + const M = Methods(BoolAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsABool; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, value_: bool) *const BoolAttribute { + return @ptrCast(c.mlirBoolAttrGet(ctx.ptr(), @intFromBool(value_)).ptr); + } + + pub fn value(self: *const BoolAttribute) bool { + return c.mlirBoolAttrGetValue(self.ptr()); + } +}; + +pub fn boolAttribute(ctx: *Context, value: bool) *const Attribute { + return @ptrCast(BoolAttribute.init(ctx, value)); +} + +pub const TypeAttribute = opaque { + const M = Methods(TypeAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAType; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(type_: *const Type) *const TypeAttribute { + return @ptrCast(c.mlirTypeAttrGet(type_.ptr()).ptr); + } + + pub fn value(self: *const TypeAttribute) *const Type { + return @ptrCast(c.mlirTypeAttrGetValue(self.ptr()).ptr); + } +}; + +pub fn typeAttribute(type_: *const Type) *const Attribute { + return @ptrCast(TypeAttribute.init(type_)); +} + +pub const Dialect = struct { + const M = Methods(Dialect, c.MlirDialect); + + pub const ptr = M.ptr; + + pub fn context(self: *const Dialect) *Context { + return @ptrCast(c.mlirDialectGetContext(self.ptr()).ptr); + } + + pub fn namespace(self: *const Dialect) []const u8 { + return string(c.mlirDialectGetNamespace(self.ptr())); + } + + pub fn format(self: *const Dialect, writer: *std.Io.Writer) !void { + try writer.print("{s}{{ .namespace = \"{s}\" }}", .{ @typeName(Dialect), self.namespace() }); + } +}; + +pub const DialectHandle = opaque { + const M = Methods(DialectHandle, c.MlirDialectHandle); + + pub const ptr = M.ptr; + + pub fn format(self: *const DialectHandle, writer: *std.Io.Writer) !void { + try writer.print("{s}{{ .namespace = \"{s}\" }}", .{ @typeName(DialectHandle), self.namespace() }); + } + + pub fn namespace(self: *const DialectHandle) []const u8 { + return string(c.mlirDialectHandleGetNamespace(self.ptr())); + } + + pub fn insertDialect(self: *const DialectHandle, registry: *DialectRegistry) void { + c.mlirDialectHandleInsertDialect(self.ptr(), registry.ptr()); + } + + pub fn registerDialect(self: *const DialectHandle, ctx: *Context) void { + c.mlirDialectHandleRegisterDialect(self.ptr(), ctx.ptr()); + } + + pub fn loadDialect(self: *const DialectHandle, ctx: *Context) *Dialect { + return @ptrCast(c.mlirDialectHandleLoadDialect(self.ptr(), ctx.ptr()).ptr); + } + + pub fn fromString(comptime namespace_: []const u8) *const DialectHandle { + return @ptrCast(@field(c, "mlirGetDialectHandle__" ++ namespace_ ++ "__")().ptr); + } +}; + +pub const Identifier = opaque { + const M = Methods(Identifier, c.MlirIdentifier); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirIdentifierEqual); + + pub fn context(self: *const Identifier) *Context { + return @ptrCast(c.mlirIdentifierGetContext(self.ptr()).ptr); + } + + pub fn get(ctx: *Context, str_: []const u8) *const Identifier { + return @ptrCast(c.mlirIdentifierGet(ctx.ptr(), stringRef(str_)).ptr); + } + + pub fn str(self: *const Identifier) []const u8 { + return string(c.mlirIdentifierStr(self.ptr())); + } + + pub fn format(self: *const Identifier, writer: *std.Io.Writer) !void { + try writer.print("{s}{{ .str = \"{s}\" }}", .{ @typeName(Identifier), self.str() }); + } +}; + +pub const Module = opaque { + const M = Methods(Module, c.MlirModule); + + pub const ptr = M.ptr; + + pub fn init(loc: *const Location) *Module { + return @constCast(@ptrCast(c.mlirModuleCreateEmpty(loc.ptr()).ptr)); + } + + pub fn deinit(self: *Module) void { + c.mlirModuleDestroy(self.ptr()); + } + + pub fn fromOperation(op: *Operation) *Module { + return @constCast(@ptrCast(c.mlirModuleFromOperation(op.ptr()).ptr)); + } + + pub fn operation(self: *const Module) *Operation { + return @ptrCast(c.mlirModuleGetOperation(self.ptr()).ptr); + } + + pub fn parse(ctx: *Context, source: []const u8) Error!*Module { + return @constCast(@ptrCast(c.mlirModuleCreateParse(ctx.ptr(), stringRef(source)).ptr orelse return Error.InvalidMlir)); + } + + pub fn context(self: *const Module) *Context { + return @ptrCast(c.mlirModuleGetContext(self.ptr()).ptr); + } + + pub fn body(self: *const Module) *Block { + return @ptrCast(c.mlirModuleGetBody(self.ptr()).ptr); + } +}; + +pub const Block = opaque { + const M = Methods(Block, c.MlirBlock); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirBlockEqual); + pub const format = M.format(c.mlirBlockPrint); + + pub fn init(args: []const *const Type, locs: []const *const Location) *Block { + return @ptrCast(c.mlirBlockCreate(@intCast(args.len), @ptrCast(args), @ptrCast(locs)).ptr); + } + + pub fn deinit(self: *Block) void { + std.debug.assert(self.parentOperation() == null); + c.mlirBlockDestroy(self.ptr()); + } + + pub fn argument(self: *const Block, index: usize) *const Value { + return @ptrCast(c.mlirBlockGetArgument(self.ptr(), @intCast(index)).ptr); + } + + pub fn numArguments(self: *const Block) usize { + return @intCast(c.mlirBlockGetNumArguments(self.ptr())); + } + + pub fn addArgument(self: *Block, type_: *const Type, loc: *const Location) *const Value { + return @ptrCast(c.mlirBlockAddArgument(self.ptr(), type_.ptr(), loc.ptr()).ptr); + } + + pub fn insertArgument(self: *Block, index: usize, type_: *const Type, loc: *const Location) *const Value { + return @ptrCast(c.mlirBlockInsertArgument(self.ptr(), @intCast(index), type_.ptr(), loc.ptr()).ptr); + } + + pub fn appendOwnedOperation(self: *Block, op: *Operation) void { + c.mlirBlockAppendOwnedOperation(self.ptr(), op.ptr()); + } + + pub fn appendOwnedOperations(self: *Block, ops: []const *Operation) void { + for (ops) |op| { + self.appendOwnedOperation(op); + } + } + + pub fn parentOperation(self: *const Block) ?*Operation { + return @ptrCast(c.mlirBlockGetParentOperation(self.ptr()).ptr); + } + + pub fn detach(self: *Block) void { + c.mlirBlockDetach(self.ptr()); + } + + pub fn terminator(self: *const Block) ?*Operation { + return @ptrCast(c.mlirBlockGetTerminator(self.ptr()).ptr); + } +}; + +pub const NamedAttribute = struct { + inner: c.MlirNamedAttribute, + + pub fn ptr(self: NamedAttribute) c.MlirNamedAttribute { + return self.inner; + } + + pub fn name(self: NamedAttribute) *const Identifier { + return @ptrCast(self.inner.name.ptr); + } + + pub fn attribute(self: NamedAttribute) *const Attribute { + return @ptrCast(self.inner.attribute.ptr); + } + + pub fn named(ctx: *Context, name_: []const u8, attr: *const Attribute) NamedAttribute { + return .{ + .inner = .{ + .name = Identifier.get(ctx, name_).ptr(), + .attribute = attr.ptr(), + }, + }; + } + + pub fn init(name_: *const Identifier, attr: *const Attribute) NamedAttribute { + return .{ + .inner = .{ + .name = name_.ptr(), + .attribute = attr.ptr(), + }, + }; + } + + pub fn format(self: NamedAttribute, writer: *std.Io.Writer) std.Io.Writer.Error!void { + try writer.print("{s}{{ .{s} = \"{f}\" }}", .{ + @typeName(NamedAttribute), + self.name().str(), + self.attribute(), + }); + } +}; + +pub const DictionaryAttribute = opaque { + const M = Methods(DictionaryAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsADictionary; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, attributes: []const NamedAttribute) *const DictionaryAttribute { + return @ptrCast(c.mlirDictionaryAttrGet( + ctx.ptr(), + @intCast(attributes.len), + @ptrCast(attributes), + ).ptr); + } + + pub fn size(self: *const DictionaryAttribute) usize { + return @intCast(c.mlirDictionaryAttrGetNumElements(self.ptr())); + } + + pub fn get(self: *const DictionaryAttribute, pos: usize) NamedAttribute { + return .{ .inner = c.mlirDictionaryAttrGetElement(self.ptr(), @intCast(pos)) }; + } + + pub fn getByName(self: *const DictionaryAttribute, name: []const u8) ?*const Attribute { + return @ptrCast(c.mlirDictionaryAttrGetElementByName(self.ptr(), stringRef(name)).ptr); + } +}; + +pub fn dictionaryAttribute(ctx: *Context, attributes: []const NamedAttribute) *const Attribute { + return @ptrCast(DictionaryAttribute.init(ctx, attributes)); +} + +pub const Value = opaque { + const M = Methods(Value, c.MlirValue); + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirValueEqual); + pub const format = M.format(c.mlirValuePrint); + pub const isA = M.isA; + + pub fn type_(val: *const Value) *const Type { + return @ptrCast(c.mlirValueGetType(val.ptr()).ptr); + } + + pub fn setType(self: *Value, type__: *const Type) void { + c.mlirValueSetType(self.ptr(), type__.ptr()); + } + + pub fn firstUse(self: *const Value) *const OpOperand { + return @ptrCast(c.mlirValueGetFirstUse(self.ptr()).ptr); + } + + pub fn replaceAllUsesWith(self: *const Value, with: *const Value) void { + c.mlirValueReplaceAllUsesOfWith(self.ptr(), with.ptr()); + } +}; + +pub const OpOperand = opaque { + const M = Methods(OpOperand, c.MlirOpOperand); + + pub const ptr = M.ptr; + + pub fn owner(self: *const OpOperand) *Operation { + return @ptrCast(c.mlirOpOperandGetOwner(self.ptr()).ptr); + } + + pub fn number(self: *const OpOperand) usize { + return @intCast(c.mlirOpOperandGetOperandNumber(self.ptr())); + } + + pub fn nextUse(self: *const OpOperand) ?*const OpOperand { + return @ptrCast(c.mlirOpOperandGetNextUse(self.ptr()).ptr); + } +}; + +pub const OpResult = opaque { + const M = Methods(OpResult, c.MlirValue); + + pub const isAFn = c.mlirValueIsAOpResult; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirValueEqual); + pub const format = M.format(c.mlirValuePrint); + + pub fn owner(self: *const OpResult) *Operation { + return @ptrCast(c.mlirOpResultGetOwner(self.ptr()).ptr); + } + + pub fn resultNumber(self: *const OpResult) usize { + return @intCast(c.mlirOpResultGetResultNumber(self.ptr())); + } +}; + +pub const BlockArgument = opaque { + const M = Methods(BlockArgument, c.MlirValue); + + pub const isAFn = c.mlirValueIsABlockArgument; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirValueEqual); + pub const format = M.format(c.mlirValuePrint); + + pub fn block(arg: *const BlockArgument) *Block { + return @ptrCast(c.mlirBlockArgumentGetOwner(arg.ptr()).ptr); + } + + pub fn number(self: *const BlockArgument) usize { + return @intCast(c.mlirBlockArgumentGetArgNumber(self.ptr())); + } + + pub fn setType(self: *BlockArgument, type_: *const Type) void { + c.mlirBlockArgumentSetType(self.ptr(), type_.ptr()); + } }; pub const OperationState = struct { - _inner: c.MlirOperationState, + inner: c.MlirOperationState, - const Self = OperationState; - - pub fn init(name: [:0]const u8, loc: Location) Self { - return .{ ._inner = c.mlirOperationStateGet(stringRef(name), loc._inner) }; + pub fn init(name: []const u8, location: *const Location) OperationState { + return .{ .inner = c.mlirOperationStateGet(stringRef(name), location.ptr()) }; } - pub fn addResult(self: *Self, type_: Type) void { - c.mlirOperationStateAddResults(&self._inner, 1, &[_]c.MlirType{type_._inner}); + pub fn addResults(self: *OperationState, results: []const *const Type) void { + c.mlirOperationStateAddResults(&self.inner, @intCast(results.len), @ptrCast(results)); } - pub fn addResults(self: *Self, types: []const Type) void { - c.mlirOperationStateAddResults(&self._inner, @intCast(types.len), @ptrCast(types.ptr)); + pub fn addOperands(self: *OperationState, operands: []const *const Value) void { + c.mlirOperationStateAddOperands(&self.inner, @intCast(operands.len), @ptrCast(operands)); } - pub fn addOperand(self: *Self, value: Value) void { - c.mlirOperationStateAddOperands(&self._inner, 1, &[_]c.MlirValue{value._inner}); + pub fn addOwnedRegions(self: *OperationState, regions: []const *Region) void { + c.mlirOperationStateAddOwnedRegions(&self.inner, @intCast(regions.len), @ptrCast(regions)); } - pub fn addOperands(self: *Self, values: []const Value) void { - c.mlirOperationStateAddOperands(&self._inner, @intCast(values.len), @ptrCast(values.ptr)); + pub fn addSuccessors(self: *OperationState, successors: []*const Block) void { + c.mlirOperationStateAddSuccessors(&self.inner, @intCast(successors.len), @ptrCast(successors)); } - pub fn addRegion(self: *Self, region: *Region) void { - c.mlirOperationStateAddOwnedRegions(&self._inner, 1, &[_]c.MlirRegion{region._inner}); + pub fn addAttributes(self: *OperationState, attributes: []const NamedAttribute) void { + c.mlirOperationStateAddAttributes(&self.inner, @intCast(attributes.len), @ptrCast(attributes.ptr)); } - pub fn addRegions(self: *Self, regions: []const Region) void { - c.mlirOperationStateAddOwnedRegions(&self._inner, @intCast(regions.len), @ptrCast(regions.ptr)); - } - - pub fn addAttribute(self: *Self, ctx: Context, name: [:0]const u8, attr: Attribute) void { - c.mlirOperationStateAddAttributes(&self._inner, 1, @ptrCast(&.{ - .{ - .name = Identifier.get(ctx, name)._inner, - .attribute = attr._inner, - }, - })); - } - - pub fn addAttributeRaw(self: *Self, name: Identifier, attr: Attribute) void { - c.mlirOperationStateAddAttributes(&self._inner, 1, @ptrCast(&.{ - .{ - .name = name._inner, - .attribute = attr._inner, - }, - })); - } - - pub fn addAttributes(self: *Self, attributes: []const NamedAttribute) void { - c.mlirOperationStateAddAttributes(&self._inner, @intCast(attributes.len), @ptrCast(attributes.ptr)); - } - - pub fn resultTypeInference(self: *Self, enabled: bool) void { - self._inner.enableResultTypeInference = enabled; + pub fn enableResultTypeInference(self: *OperationState) void { + c.mlirOperationStateEnableResultTypeInference(&self.inner); } }; -pub const DictionaryAttribute = struct { - _inner: c.MlirAttribute, - pub const is_a_fn = c.mlirAttributeIsADictionary; - pub const asAttr = Attribute.fromAny(DictionaryAttribute); - pub const eql = Attribute.eqlAny(DictionaryAttribute); +pub const Operation = opaque { + const M = Methods(Operation, c.MlirOperation); - pub fn init(ctx: Context, attributes: []const NamedAttribute) DictionaryAttribute { - return .{ ._inner = c.mlirDictionaryAttrGet( - ctx._inner, - @intCast(attributes.len), - @ptrCast(attributes.ptr), - ) }; + const MAX_SEGMENTS = 32; + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirOperationEqual); + pub const format = M.format(c.mlirOperationPrint); + + pub fn init(state: *OperationState) !*Operation { + defer state.* = undefined; + return @ptrCast(c.mlirOperationCreate(&state.inner).ptr orelse return Error.InvalidMlir); } - pub fn size(self: DictionaryAttribute) usize { - return @intCast(c.mlirDictionaryAttrGetNumElements(self._inner)); + pub fn deinit(self: *Operation) void { + std.debug.assert(self.block() == null); + c.mlirOperationDestroy(self.ptr()); } - pub fn get(self: DictionaryAttribute, pos: usize) NamedAttribute { - return .wrap(c.mlirDictionaryAttrGetElement(self._inner, @bitCast(pos))); + pub fn parse(ctx: *Context, source: []const u8, name_: []const u8) Error!*Operation { + return @ptrCast(c.mlirOperationCreateParse(ctx.ptr(), stringRef(source), stringRef(name_)).ptr orelse return Error.InvalidMlir); } - pub fn getByName(self: DictionaryAttribute, name: [:0]const u8) ?Attribute { - return Attribute.wrapOr(c.mlirDictionaryAttrGetElementByName(self._inner, name)); - } -}; - -pub const Operation = struct { - const Self = Operation; - _inner: c.MlirOperation, - - pub const dump = helpers.dump(Operation, c.mlirOperationDestroy); - pub const deinit = helpers.deinit(Operation, c.mlirOperationDestroy); - pub const wrapOr = helpers.wrapOr(Operation, c.mlirOperationIsNull); - - pub const eql = Attribute.eqlAny(Self); - - pub fn init(state: *OperationState) !Self { - return Self.wrapOr(c.mlirOperationCreate(&state._inner)) orelse Error.InvalidMlir; + pub fn name(self: *const Operation) []const u8 { + return string(c.mlirOperationGetName(self.ptr())); } - pub fn make(ctx: Context, op_name: [:0]const u8, args: struct { - operands: ?[]const Value = null, - variadic_operands: ?[]const []const Value = null, - tt_variadic_operands: ?[]const []const Value = null, - results: ?[]const Type = null, - variadic_results: ?[]const []const Type = null, - result_type_inference: ?bool = null, - n_regions: usize = 0, - attributes: ?[]const AttrTuple = null, - blocks: ?[]const Block = null, - verify: bool = true, - location: Location, - }) Self { - var state = OperationState.init(op_name, args.location); - std.debug.assert(!(args.operands != null and args.variadic_operands != null)); - if (args.operands) |operands| { - state.addOperands(operands); - } else if (args.variadic_operands) |operands_segments| { - const MAX_SEGMENTS = 32; - var segments: stdx.BoundedArray(i32, MAX_SEGMENTS) = .{}; + pub fn clone(self: *const Operation) *Operation { + return @ptrCast(c.mlirOperationClone(self.ptr()).ptr); + } - for (operands_segments) |operands| { - state.addOperands(operands); - segments.appendAssumeCapacity(@intCast(operands.len)); - } - state.addAttribute(ctx, "operandSegmentSizes", .denseElements(ctx, &.{@intCast(segments.len)}, .i32, segments.constSlice())); - } else if (args.tt_variadic_operands) |operands_segments| { - // stablehlo and triton seems to disagree on the expected type of operandSegmentSizes, let's fix that. - const MAX_SEGMENTS = 32; - var segments: stdx.BoundedArray(i32, MAX_SEGMENTS) = .{}; + pub fn block(self: *const Operation) ?*Block { + return @ptrCast(c.mlirOperationGetBlock(self.ptr()).ptr); + } - for (operands_segments) |operands| { - state.addOperands(operands); - segments.appendAssumeCapacity(@intCast(operands.len)); - } - state.addAttribute(ctx, "operandSegmentSizes", .dense(ctx, .i32, segments.constSlice())); - } - if (args.result_type_inference) |enable| { - state.resultTypeInference(enable); - } - std.debug.assert(!(args.results != null and args.variadic_results != null)); - if (args.results) |results| { - state.addResults(results); - } else if (args.variadic_results) |result_segments| { - for (result_segments) |results| { - state.addResults(results); - } - } - for (0..args.n_regions) |_| { - var region_ = Region.init() catch { - @panic("Failed to create MLIR region"); - }; - state.addRegion(®ion_); - } - if (args.attributes) |attrs| { - for (attrs) |attr| { - state.addAttributeRaw( - Identifier.get(ctx, attr[0]), - attr[1], - ); - } - } - if (args.blocks) |blocks_| { - for (blocks_) |block_| { - var region_ = Region.init() catch { - @panic("Failed to create MLIR region"); - }; - region_.appendBlock(block_); - state.addRegion(®ion_); - } - } + pub fn appendTo(self: *Operation, block_: *Block) *Operation { + block_.appendOwnedOperation(self); + return self; + } - const new_op = Operation.init(&state) catch { - @panic("Failed to create MLIR operation"); + pub fn parent(self: *const Operation) ?*Operation { + return @ptrCast(c.mlirOperationGetParentOperation(self.ptr()).ptr); + } + + pub fn removeFromParent(self: *Operation) void { + c.mlirOperationRemoveFromParent(self.ptr()); + } + + pub fn numOperands(self: *const Operation) usize { + return @intCast(c.mlirOperationGetNumOperands(self.ptr())); + } + + pub fn operand(self: *const Operation, index: usize) *const Value { + return @ptrCast(c.mlirOperationGetOperand(self.ptr(), @intCast(index)).ptr); + } + + pub fn setOperand(self: *Operation, index: usize, value: *const Value) void { + c.mlirOperationSetOperand(self.ptr(), @intCast(index), value.ptr()); + } + + pub fn numResults(self: *const Operation) usize { + return @intCast(c.mlirOperationGetNumResults(self.ptr())); + } + + pub fn result(self: *const Operation, index: usize) *const Value { + return @ptrCast(c.mlirOperationGetResult(self.ptr(), @intCast(index)).ptr); + } + + pub fn nextInBlock(self: *const Operation) *const Operation { + return @ptrCast(c.mlirOperationGetNextInBlock(self.ptr()).ptr); + } + + pub fn context(self: *const Operation) *Context { + return @ptrCast(c.mlirOperationGetContext(self.ptr()).ptr); + } + + pub const PrintFlags = struct { + elide_large_elements_attrs: ?usize = null, + debug_info: bool = false, + debug_info_pretty_form: bool = true, + print_generic_op_form: bool = false, + use_local_scope: bool = false, + assume_verified: bool = false, + + const Ctx = struct { + self: *const Operation, + flags: PrintFlags, }; + }; + + fn formatWithFlags(fctx: PrintFlags.Ctx, writer: *std.io.Writer) std.Io.Writer.Error!void { + const flags = c.mlirOpPrintingFlagsCreate(); + defer c.mlirOpPrintingFlagsDestroy(flags); + + if (fctx.flags.elide_large_elements_attrs) |v| { + c.mlirOpPrintingFlagsElideLargeElementsAttrs(flags, @intCast(v)); + } + c.mlirOpPrintingFlagsEnableDebugInfo( + flags, + fctx.flags.debug_info, + fctx.flags.debug_info_pretty_form, + ); + if (fctx.flags.print_generic_op_form) { + c.mlirOpPrintingFlagsPrintGenericOpForm(flags); + } + if (fctx.flags.use_local_scope) { + c.mlirOpPrintingFlagsUseLocalScope(flags); + } + if (fctx.flags.assume_verified) { + c.mlirOpPrintingFlagsAssumeVerified(flags); + } + var sctx: StringCallbackCtx = .{ .writer = writer }; + c.mlirOperationPrintWithFlags(fctx.self.ptr(), flags, stringCallback, &sctx); + if (sctx.err) |err| { + return err; + } + } + + pub fn fmt(self: *const Operation, flags: PrintFlags) std.fmt.Alt(PrintFlags.Ctx, formatWithFlags) { + return .{ .data = .{ + .self = self, + .flags = flags, + } }; + } + + pub const MakeArgs = struct { + pub const AttrTuple = struct { []const u8, *const Attribute }; + + pub const Operands = union(enum) { + flat: []const *const Value, + variadic: []const []const *const Value, + }; + + pub const ResultTypes = union(enum) { + flat: []const *const Type, + variadic: []const []const *const Type, + }; + + operands: ?Operands = null, + results: ?ResultTypes = null, + result_type_inference: bool = false, + attributes: ?[]const NamedAttribute = null, + blocks: ?[]const *Block = null, + verify: bool = true, + location: ?*const Location = null, + }; + + pub fn try_make(ctx: *Context, name_: []const u8, args: MakeArgs) !*Operation { + var state: OperationState = .init(name_, args.location orelse .unknown(ctx)); + if (args.result_type_inference) { + state.enableResultTypeInference(); + } + if (args.operands) |operands| switch (operands) { + .flat => |v| state.addOperands(v), + .variadic => |segments| { + var sizes: stdx.BoundedArray(i32, MAX_SEGMENTS) = .{}; + for (segments) |segment_operands| { + state.addOperands(segment_operands); + sizes.appendAssumeCapacity(@intCast(segment_operands.len)); + } + state.addAttributes(&.{ + .named(ctx, "operandSegmentSizes", denseArrayAttribute(ctx, .i32, sizes.constSlice())), + }); + }, + }; + if (args.results) |results| switch (results) { + .flat => |v| state.addResults(v), + .variadic => |segments| { + var sizes: stdx.BoundedArray(i32, MAX_SEGMENTS) = .{}; + for (segments) |segment_results| { + state.addResults(segment_results); + sizes.appendAssumeCapacity(@intCast(segment_results.len)); + } + state.addAttributes(&.{ + .named(ctx, "resultSegmentSizes", denseArrayAttribute(ctx, .i32, sizes.constSlice())), + }); + }, + }; + if (args.attributes) |attrs| { + state.addAttributes(attrs); + } + if (args.blocks) |blocks| { + const region = Region.init(); + for (blocks) |block_| { + region.appendOwnedBlock(block_); + } + state.addOwnedRegions(&.{region}); + } + const new_op = try Operation.init(&state); + errdefer new_op.deinit(); if (args.verify and new_op.verify() == false) { - log.err("Failed to verify MLIR operation:\n{f}", .{new_op.mlirFormatter(.{ .debug_info = true })}); - @panic("Failed to verify MLIR operation"); + log.err("Failed to verify MLIR operation:\n{f}", .{ + new_op.fmt(.{ + .print_generic_op_form = true, + .debug_info = true, + }), + }); + return Error.InvalidMlir; } return new_op; } - pub fn initParse(ctx: Context, str: [:0]const u8) !Self { - return Self.wrapOr( - c.mlirOperationCreateParse(ctx._inner, stringRef(str), stringRef("pouet")), - ) orelse Error.InvalidMlir; - } - - pub fn clone(self: Self) !Self { - return Self.wrapOr( - c.mlirOperationClone(self._inner), - ) orelse Error.InvalidMlir; - } - - pub fn name(self: Self) Identifier { - return .{ ._inner = c.mlirOperationGetName(self._inner) }; - } - - pub fn removeFromParent(self: *Self) void { - c.mlirOperationRemoveFromParent(self._inner); - } - - pub fn numOperands(self: Self) usize { - return @intCast(c.mlirOperationGetNumOperands(self._inner)); - } - - pub fn operand(self: Self, index: usize) Value { - return .{ ._inner = c.mlirOperationGetOperand(self._inner, @intCast(index)) }; - } - - pub fn setOperand(self: *Self, index: usize, value: Value) void { - c.mlirOperationSetOperand(self._inner, @intCast(index), value._inner); - } - - pub fn numResults(self: Self) usize { - return @intCast(c.mlirOperationGetNumResults(self._inner)); - } - - pub fn result(self: Self, index: usize) Value { - return .{ ._inner = c.mlirOperationGetResult(self._inner, @intCast(index)) }; - } - - pub fn nextInBlock(self: Self) Self { - return .{ ._inner = c.mlirOperationGetNextInBlock(self._inner) }; - } - - // pub fn previousInBlock(self: Self) Self { - // return .{ ._inner = c.mlirOperationGetPrevInBlock(self._inner) }; - // } - - pub fn block(self: Self) ?Block { - return Block.wrapOr(c.mlirOperationGetBlock(self._inner)); - } - - pub fn parent(self: Self) ?Self { - return Self.wrapOr(c.mlirOperationGetParentOperation(self._inner)); - } - - pub fn region(self: Self, index: usize) Region { - return .{ ._inner = c.mlirOperationGetRegion(self._inner, @intCast(index)) }; - } - - pub fn context(self: Self) Context { - return .{ ._inner = c.mlirOperationGetContext(self._inner) }; - } - - pub fn writeBytecode(self: Self, writer: anytype) void { - var writer_context = .{ .writer = writer }; - const WriterContext = @TypeOf(writer_context); - - c.mlirOperationWriteBytecode( - self._inner, - (struct { - pub fn callback(str: c.MlirStringRef, ctx_: ?*anyopaque) callconv(.c) void { - const inner_writer_context: *WriterContext = @ptrCast(@alignCast(ctx_)); - _ = inner_writer_context.writer.write(str.data[0..str.length]) catch unreachable; - } - }).callback, - &writer_context, - ); - } - - pub fn writeBytecodeWithConfig(self: Self, writer: anytype, config: struct { - desiredEmitedVersion: ?i64 = null, - }) !void { - const cfg = c.mlirBytecodeWriterConfigCreate(); - defer c.mlirBytecodeWriterConfigDestroy(cfg); - if (config.desiredEmitedVersion) |v| { - c.mlirBytecodeWriterConfigDesiredEmitVersion(cfg, v); - } - - const WriterContext = struct { - writer: @TypeOf(writer), - write_error: ?@TypeOf(writer).Error = null, + pub fn make(ctx: *Context, op_name: []const u8, args: MakeArgs) *Operation { + return try_make(ctx, op_name, args) catch { + @panic("Failed to create MLIR operation"); }; - var writer_context: WriterContext = .{ .writer = writer }; - - try successOr(c.mlirOperationWriteBytecodeWithConfig( - self._inner, - cfg, - (struct { - pub fn callback(str: c.MlirStringRef, ctx_: ?*anyopaque) callconv(.c) void { - const inner_writer_context: *WriterContext = @ptrCast(@alignCast(ctx_)); - _ = inner_writer_context.writer.write(str.data[0..str.length]) catch |err| { - inner_writer_context.write_error = err; - }; - } - }).callback, - &writer_context, - ), error.InvalidMlirBytecodeVersion); - - if (writer_context.write_error) |err| return err; } - /// Enable a full dump of the IR. - /// Usage `std.log.debug("{}", .{ module.op().mlirFormatter(.{}) }); - pub fn mlirFormatter(self: Operation, flags: OpPrintingFlags) MlirFormatter { - return .{ .op = self, .flags = flags }; + pub fn verify(self: *const Operation) bool { + return c.mlirOperationVerify(self.ptr()); } - const MlirFormatter = struct { - op: Operation, - flags: OpPrintingFlags, - - pub fn format(self: @This(), writer: anytype) !void { - self.op.print(writer, self.flags); - } - }; - - pub fn print(self: Self, writer: *std.Io.Writer, flags: OpPrintingFlags) void { - const pflags = flags.create(); - defer c.mlirOpPrintingFlagsDestroy(pflags); - - c.mlirOperationPrintWithFlags( - self._inner, - pflags, - (struct { - pub fn callback(str: c.MlirStringRef, ctx_: ?*anyopaque) callconv(.c) void { - const _writer: *std.Io.Writer = @ptrCast(@alignCast(ctx_)); - _writer.writeAll(str.data[0..str.length]) catch @panic("Mlir print failed"); - } - }).callback, - writer, - ); - } - - pub fn verify(self: Self) bool { - return c.mlirOperationVerify(self._inner); - } - - pub fn getLocation(self: Self) Location { - return .{ ._inner = c.mlirOperationGetLocation(self._inner) }; + pub fn location(self: *const Operation) *const Location { + return @ptrCast(c.mlirOperationGetLocation(self.ptr()).ptr); } pub const WalkOrder = enum(c.MlirWalkOrder) { @@ -980,7 +1078,7 @@ pub const Operation = struct { skip = c.MlirWalkResultSkip, }; - pub fn walk(self: Self, order: WalkOrder, ctx: anytype, walkfn: fn (ctx: anytype, op: Operation) WalkResult) void { + pub fn walk(self: *Operation, order: WalkOrder, ctx: anytype, walkfn: fn (ctx_: @TypeOf(ctx), op: *Operation) WalkResult) void { var inner_ctx = .{ .ctx = ctx }; const ContextType = @TypeOf(inner_ctx); @@ -989,7 +1087,7 @@ pub const Operation = struct { (struct { pub fn callback(op: c.MlirOperation, ctx_: ?*anyopaque) callconv(.c) c.MlirWalkResult { const inner_ctx_: *ContextType = @ptrCast(@alignCast(ctx_)); - return @intFromEnum(walkfn(inner_ctx_.ctx, .{ ._inner = op })); + return @intFromEnum(walkfn(inner_ctx_.ctx, @ptrCast(op.ptr))); } }).callback, &inner_ctx, @@ -997,779 +1095,372 @@ pub const Operation = struct { ); } - pub fn getAttribute(self: Self, pos: usize) NamedAttribute { - return .{ ._inner = c.mlirOperationGetAttribute(self._inner, @intCast(pos)) }; - } + pub const BytecodeWriterConfig = struct { + desired_emit_version: ?usize = null, - pub fn getAttributeByName(self: Self, name_: [:0]const u8) ?Attribute { - return Attribute.wrapOr(c.mlirOperationGetAttributeByName(self._inner, stringRef(name_))); - } - - pub fn setAttributeByName(self: Self, name_: [:0]const u8, attr: Attribute) void { - c.mlirOperationSetAttributeByName(self._inner, stringRef(name_), attr._inner); - } - - pub fn removeAttributeByName(self: Self, name_: [:0]const u8) bool { - return c.mlirOperationRemoveAttributeByName(self._inner, stringRef(name_)); - } - - /// Hash the canonicalized IR, without debug information that can change across builds. - pub fn hash(op: Operation, hasher: *std.hash.XxHash64) void { - // Note: before we where using op.writeBytecode(writer), - // but it crashes on some inputs, notably for unused variables. - // So we use the text representation of the mlir. - // See https://github.com/zml/zml/issues/97. - const flags = OpPrintingFlags.create(.{ .debug_info = false }); - defer c.mlirOpPrintingFlagsDestroy(flags); - - c.mlirOperationPrintWithFlags( - op._inner, - flags, - (struct { - pub fn callback(str: c.MlirStringRef, ctx_: ?*anyopaque) callconv(.c) void { - const _hasher: *std.hash.XxHash64 = @ptrCast(@alignCast(ctx_)); - _hasher.update(str.data[0..str.length]); - } - }).callback, - hasher, - ); - } -}; - -pub const OpPrintingFlags = struct { - elide_large_elements_attrs: ?usize = null, - debug_info: bool = false, - debug_info_pretty_form: bool = true, - print_generic_op_form: bool = false, - use_local_scope: bool = false, - assume_verified: bool = false, - - pub fn create(self: OpPrintingFlags) c.MlirOpPrintingFlags { - const pflags = c.mlirOpPrintingFlagsCreate(); - if (self.elide_large_elements_attrs) |v| { - c.mlirOpPrintingFlagsElideLargeElementsAttrs(pflags, @intCast(v)); - } - c.mlirOpPrintingFlagsEnableDebugInfo(pflags, self.debug_info, self.debug_info_pretty_form); - if (self.print_generic_op_form) { - c.mlirOpPrintingFlagsPrintGenericOpForm(pflags); - } - if (self.use_local_scope) { - c.mlirOpPrintingFlagsUseLocalScope(pflags); - } - if (self.assume_verified) { - c.mlirOpPrintingFlagsAssumeVerified(pflags); - } - return pflags; - } -}; - -pub const OpOperand = struct { - _inner: c.MlirOpOperand, - const Self = OpOperand; - - pub fn owner(self: Self) Operation { - return .{ ._inner = c.mlirOpOperandGetOwner(self._inner) }; - } - - pub fn number(self: Self) usize { - return @intCast(c.mlirOpOperandGetOperandNumber(self._inner)); - } - - pub fn nextUse(self: Self) ?Self { - return Self.wrapOr( - c.mlirOpOperandGetNextUse(self._inner), - ); - } -}; - -pub const Region = struct { - _inner: c.MlirRegion, - - pub const eql = helpers.eql(Region, c.mlirBlockEqual); - pub const deinit = helpers.deinit(Region, c.mlirRegionDestroy); - pub const wrapOr = helpers.wrapOr(Region, c.mlirRegionIsNull); - - const Self = Region; - - pub fn init() !Self { - return Self.wrapOr(c.mlirRegionCreate()) orelse Error.InvalidMlir; - } - - pub fn appendBlock(self: *Self, block: Block) void { - c.mlirRegionAppendOwnedBlock(self._inner, block._inner); - } - - pub fn insertBlock(self: *Self, index: isize, block: Block) void { - c.mlirRegionInsertOwnedBlock(self._inner, index, block._inner); - } - - pub fn insertBlockBefore(self: *Self, reference: Block, block: Block) void { - c.mlirRegionInsertOwnedBlockBefore(self._inner, reference._inner, block._inner); - } - - pub fn insertBlockAfter(self: *Self, reference: Block, block: Block) void { - c.mlirRegionInsertOwnedBlockAfter(self._inner, reference._inner, block._inner); - } - - pub fn firstBlock(self: Self) Block { - return .{ ._inner = c.mlirRegionGetFirstBlock(self._inner) }; - } -}; - -pub const Value = struct { - _inner: c.MlirValue, - - pub const dump = helpers.dump(Value, c.mlirValueDump); - pub const eql = helpers.eql(Value, c.mlirValueEqual); - pub const format = helpers.format(Value, c.mlirValuePrint).format; - pub const wrapOr = helpers.wrapOr(Value, c.mlirValueIsNull); - - pub fn getType(val: Value) Type { - return .{ ._inner = c.mlirValueGetType(val._inner) }; - } - - pub fn setType(val: *Value, typ: Type) void { - c.mlirValueSetType(val._inner, typ._inner); - } - - pub fn firstUse(val: Value) OpOperand { - return .{ ._inner = c.mlirValueGetFirstUse(val._inner) }; - } - - pub fn replaceAllUsesWith(val: Value, with: Value) void { - c.mlirValueReplaceAllUsesOfWith(val._inner, with._inner); - } - - pub fn owner(val: Value) Operation { - return .{ ._inner = c.mlirOpResultGetOwner(val._inner) }; - } - - pub fn isABlockArgument(val: Value) bool { - return c.mlirValueIsABlockArgument(val._inner); - } - - pub fn isAOpResult(val: Value) bool { - return c.mlirValueIsAOpResult(val._inner); - } - - pub const Kind = union(enum) { - block_argument: BlockArgument, - op_result: Operation, - null, + const Ctx = struct { + self: *const Operation, + config: ?BytecodeWriterConfig, + }; }; - pub fn kind(val: Value) Kind { - if (val.isAOpResult()) { - return .{ .op_result = val.owner() }; - } - if (val.isABlockArgument()) { - return .{ .block_argument = .{ ._inner = val._inner } }; - } - // From MLIR docs: - // https://mlir.llvm.org/doxygen/classmlir_1_1Value.html#details - // > An SSA value is either a BlockArgument or the result of an operation. - return .null; - } -}; - -pub const BlockArgument = struct { - _inner: c.MlirValue, - - pub fn block(arg: BlockArgument) Block { - return .{ ._inner = c.mlirBlockArgumentGetOwner(arg._inner) }; - } - - pub fn number(arg: BlockArgument) usize { - return @bitCast(c.mlirBlockArgumentGetArgNumber(arg._inner)); - } - - pub fn format(self: BlockArgument, writer: anytype) !void { - const value = Value{ ._inner = self._inner }; - return value.format(writer); - } -}; - -pub const Type = struct { - _inner: c.MlirType, - - pub const dump = helpers.eql(Type, c.mlirTypeDump); - pub const eql = helpers.eql(Type, c.mlirTypeEqual); - pub const format = helpers.format(Type, c.mlirTypePrint); - pub const wrapOr = helpers.wrapOr(Type, c.mlirTypeIsNull); - - pub fn parse(ctx: Context, str: [:0]const u8) !Type { - return Type.wrapOr( - c.mlirTypeParseGet(ctx._inner, stringRef(str)), - ) orelse Error.InvalidMlir; - } - - pub fn as(generic: Type, SpecificType: type) ?SpecificType { - if (@hasDecl(SpecificType, "is_a_fn")) { - return if (SpecificType.is_a_fn(generic._inner)) - .{ ._inner = generic._inner } - else - null; - } - @compileError("Mlir subclass of type need `is_a_fn` attribute: " ++ @typeName(SpecificType)); - } - - pub fn fromAny(SpecificType: type) fn (x: SpecificType) Type { - stdx.debug.assertComptime(@hasDecl(SpecificType, "asType"), "Type.fromAny expects a type subclass, got: {}. Missing `asAttr` declaration.", .{SpecificType}); - return struct { - fn cast(x: SpecificType) Type { - return .{ ._inner = x._inner }; + pub fn writeBytecode(self: *const Operation, config: ?BytecodeWriterConfig, writer: *std.Io.Writer) !void { + if (config) |config_| { + const bc_config = c.mlirBytecodeWriterConfigCreate(); + defer c.mlirBytecodeWriterConfigDestroy(bc_config); + if (config_.desired_emit_version) |version| { + c.mlirBytecodeWriterConfigDesiredEmitVersion(bc_config, @intCast(version)); } - }.cast; - } - - pub fn eqlAny(SpecificType: type) fn (SpecificType, SpecificType) bool { - return struct { - fn eql(a: SpecificType, b: SpecificType) bool { - return a.asType().eql(b.asType()); + var sctx: StringCallbackCtx = .{ .writer = writer }; + c.mlirOperationWriteBytecodeWithConfig(self.ptr(), bc_config, stringCallback, &sctx); + if (sctx.err) |err| { + return err; } - }.eql; + return; + } + + var sctx: StringCallbackCtx = .{ .writer = writer }; + c.mlirOperationWriteBytecode(self.ptr(), stringCallback, &sctx); + if (sctx.err) |err| { + return err; + } } - pub fn formatAny(SpecificType: type) fn (SpecificType, SpecificType) type { - return struct { - pub fn format(self: SpecificType, writer: anytype) !void { - return try Type.format(self.asType(), writer); - } - }; + fn fmtBytecodeImpl(ctx: BytecodeWriterConfig.Ctx, writer: *std.io.Writer) std.Io.Writer.Error!void { + try ctx.self.writeBytecode(ctx.config, writer); } - pub fn index(ctx: Context) Type { - return IndexType.init(ctx).asType(); - } - - pub fn int(ctx: Context, int_type: IntegerTypes) Type { - return switch (int_type) { - .unknown => @panic("Unknown integer type"), - inline else => |t| IntegerType(t).init(ctx).asType(), - }; - } - - pub fn float(ctx: Context, float_type: FloatTypes) Type { - return switch (float_type) { - inline else => |t| FloatType(t).init(ctx).asType(), - }; - } - - pub fn complex(ctx: Context, complex_type: ComplexTypes) Type { - return switch (complex_type) { - .unknown => @panic("Unknown complex type can't be created like this"), // What's the point ? - inline else => |t| ComplexType(t).init(ctx).asType(), - }; - } - - pub fn tuple(ctx: Context, types: []const Type) Type { - return (TupleType.init(ctx, types) catch unreachable).asType(); - } - - pub fn function(ctx: Context, args: []const Type, results: []const Type) Type { - return (FunctionType.init(ctx, args, results) catch unreachable).asType(); - } - - pub fn tensor(dimensions: []const i64, elem_type: Type) Type { - return RankedTensorType.init(dimensions, elem_type).asType(); + pub fn fmtBytecode(self: *const Operation, config: ?BytecodeWriterConfig) std.fmt.Alt(BytecodeWriterConfig.Ctx, fmtBytecodeImpl) { + return .{ .data = .{ + .self = self, + .config = config, + } }; } }; -pub const IndexType = struct { - _inner: c.MlirType, +pub const Region = opaque { + const M = Methods(Region, c.MlirRegion); - pub const asType = Type.fromAny(IndexType); - pub const eql = Type.eqlAny(IndexType); - pub const format = Type.formatAny(IndexType).format; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirRegionEqual); - pub fn init(ctx: Context) IndexType { - return .{ ._inner = c.mlirIndexTypeGet(ctx._inner) }; + pub fn init() *Region { + return @ptrCast(c.mlirRegionCreate().ptr); + } + + pub fn deinit(self: *Region) void { + c.mlirRegionDestroy(self.ptr()); + } + + pub fn appendOwnedBlock(self: *Region, block: *Block) void { + c.mlirRegionAppendOwnedBlock(self.ptr(), block.ptr()); + } + + pub fn insertOwnedBlock(self: *Region, index: usize, block: *Block) void { + c.mlirRegionInsertOwnedBlock(self.ptr(), @intCast(index), block.ptr()); + } + + pub fn insertOwnedBlockBefore(self: *Region, reference: *const Block, block: *Block) void { + c.mlirRegionInsertOwnedBlockBefore(self.ptr(), reference.ptr(), block.ptr()); + } + + pub fn insertOwnedBlockAfter(self: *Region, reference: *const Block, block: *Block) void { + c.mlirRegionInsertOwnedBlockAfter(self.ptr(), reference.ptr(), block.ptr()); + } + + pub fn firstBlock(self: *const Region) ?*Block { + return @ptrCast(c.mlirRegionGetFirstBlock(self.ptr()).ptr); + } + + pub fn takeBody(self: *Region, from: *Region) void { + c.mlirRegionTakeBody(self.ptr(), from.ptr()); + } + + pub fn nextInOperation(self: *const Region) *Region { + return @ptrCast(c.mlirRegionGetNextInOperation(self.ptr()).ptr); } }; -pub const IntegerTypes = enum { - i1, - i4, +pub const ShapedType = opaque { + const M = Methods(ShapedType, c.MlirType); + + pub const MAX_RANK = 16; + + pub const isAFn = c.mlirTypeIsAShaped; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + pub const isA = M.isA; + + pub fn elementType(self: *const ShapedType) *const Type { + return @ptrCast(c.mlirShapedTypeGetElementType(self.ptr()).ptr); + } + + pub fn rank(self: *const ShapedType) usize { + return @intCast(c.mlirShapedTypeGetRank(self.ptr())); + } + + pub fn dimension(self: *const ShapedType, dim: usize) i64 { + return c.mlirShapedTypeGetDimSize(self.ptr(), @intCast(dim)); + } +}; + +pub const RankedTensorType = opaque { + const M = Methods(RankedTensorType, c.MlirType); + + pub const isAFn = c.mlirTypeIsARankedTensor; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + pub fn get(dimensions: []const i64, elemType: *const Type) *const RankedTensorType { + return @ptrCast(c.mlirRankedTensorTypeGet( + @intCast(dimensions.len), + @ptrCast(dimensions), + elemType.ptr(), + c.mlirAttributeGetNull(), + ).ptr); + } + + pub fn fromShaped(other: *const ShapedType) *const RankedTensorType { + var dims: stdx.BoundedArray(i64, ShapedType.MAX_RANK) = .{}; + for (0..other.rank()) |i| { + dims.appendAssumeCapacity(other.dimension(i)); + } + return get( + dims.constSlice(), + other.elementType(), + ); + } + + pub fn elementType(self: *const RankedTensorType) *const Type { + return @ptrCast(c.mlirShapedTypeGetElementType(self.ptr()).ptr); + } + + pub fn rank(self: *const RankedTensorType) usize { + return @intCast(c.mlirShapedTypeGetRank(self.ptr())); + } + + pub fn dimension(self: *const RankedTensorType, dim: usize) i64 { + return c.mlirShapedTypeGetDimSize(self.ptr(), @intCast(dim)); + } + + pub fn shaped(self: *const RankedTensorType) *const ShapedType { + return @ptrCast(self); + } +}; + +pub fn rankedTensorType(dimensions: []const i64, elem_type: *const Type) *const Type { + return @ptrCast(RankedTensorType.get(dimensions, elem_type)); +} + +pub const DenseElementsAttribute = opaque { + const M = Methods(DenseElementsAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsADenseElements; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(shaped_type: *const ShapedType, values: anytype) *const DenseElementsAttribute { + const bytes = std.mem.sliceAsBytes(values); + return @ptrCast(c.mlirDenseElementsAttrRawBufferGet( + shaped_type.ptr(), + @intCast(bytes.len), + @ptrCast(bytes), + ).ptr orelse unreachable); + } +}; + +pub fn denseElementsAttribute(shaped_type: *const ShapedType, values: anytype) *const Attribute { + return @ptrCast(DenseElementsAttribute.init(shaped_type, values)); +} + +pub const DenseArrayTypes = enum { + bool, i8, i16, i32, i64, - si4, - si8, - si16, - si32, - si64, - u4, - u8, - u16, - u32, - u64, - - unknown, -}; - -pub fn IntegerType(comptime it: IntegerTypes) type { - const Config = switch (it) { - .i1 => .{ 1, c.mlirIntegerTypeGet, c.mlirIntegerTypeIsSignless }, - .i4 => .{ 4, c.mlirIntegerTypeGet, c.mlirIntegerTypeIsSignless }, - .i8 => .{ 8, c.mlirIntegerTypeGet, c.mlirIntegerTypeIsSignless }, - .i16 => .{ 16, c.mlirIntegerTypeGet, c.mlirIntegerTypeIsSignless }, - .i32 => .{ 32, c.mlirIntegerTypeGet, c.mlirIntegerTypeIsSignless }, - .i64 => .{ 64, c.mlirIntegerTypeGet, c.mlirIntegerTypeIsSignless }, - .si4 => .{ 4, c.mlirIntegerTypeSignedGet, c.mlirIntegerTypeIsSigned }, - .si8 => .{ 8, c.mlirIntegerTypeSignedGet, c.mlirIntegerTypeIsSigned }, - .si16 => .{ 16, c.mlirIntegerTypeSignedGet, c.mlirIntegerTypeIsSigned }, - .si32 => .{ 32, c.mlirIntegerTypeSignedGet, c.mlirIntegerTypeIsSigned }, - .si64 => .{ 64, c.mlirIntegerTypeSignedGet, c.mlirIntegerTypeIsSigned }, - .u4 => .{ 4, c.mlirIntegerTypeUnsignedGet, c.mlirIntegerTypeIsUnsigned }, - .u8 => .{ 8, c.mlirIntegerTypeUnsignedGet, c.mlirIntegerTypeIsUnsigned }, - .u16 => .{ 16, c.mlirIntegerTypeUnsignedGet, c.mlirIntegerTypeIsUnsigned }, - .u32 => .{ 32, c.mlirIntegerTypeUnsignedGet, c.mlirIntegerTypeIsUnsigned }, - .u64 => .{ 64, c.mlirIntegerTypeUnsignedGet, c.mlirIntegerTypeIsUnsigned }, - .unknown => .{ 0, null, null }, - }; - - return struct { - _inner: c.MlirType, - - const Int = @This(); - pub const is_a_fn = switch (it) { - .unknown => c.mlirTypeIsAInteger, - else => typeIsAIntegerExact, - }; - - pub const asType = Type.fromAny(Int); - pub const eql = Type.eqlAny(Int); - pub const format = helpers.format(Int, c.mlirTypePrint); - - fn typeIsAIntegerExact(typ: c.MlirType) callconv(.c) bool { - const bit_width = Config[0]; - const is_sign = Config[2]; - return c.mlirTypeIsAInteger(typ) and (c.mlirIntegerTypeGetWidth(typ) == bit_width) and is_sign(typ); - } - - pub const BitWidth = Config[0]; - - pub const init = if (it != .unknown) struct { - pub fn init(ctx: Context) Int { - const type_get = Config[1]; - return .{ ._inner = type_get(ctx._inner, BitWidth) }; - } - }.init else {}; - }; -} - -pub const FloatTypes = enum { - f8e4m3b11fnuz, - f8e4m3fn, - f8e4m3fnuz, - f8e5m2, - f8e5m2fnuz, - bf16, - f16, f32, f64, - pub fn asType(self: FloatTypes, ctx: Context) Type { - return switch (self) { - inline else => |ft| FloatType(ft).init(ctx).asType(), + fn ZigType(comptime ElementType: @This()) type { + return switch (ElementType) { + .bool => i32, + .i8 => i8, + .i16 => i16, + .i32 => i32, + .i64 => i64, + .f32 => f32, + .f64 => f64, }; } }; -pub fn FloatType(comptime ft: FloatTypes) type { - const Config = switch (ft) { - .f8e4m3b11fnuz => .{ c.mlirTypeIsAFloat8E4M3B11FNUZ, c.mlirFloat8E4M3B11FNUZTypeGet }, - .f8e4m3fn => .{ c.mlirTypeIsAFloat8E4M3FN, c.mlirFloat8E4M3FNTypeGet }, - .f8e4m3fnuz => .{ c.mlirTypeIsAFloat8E4M3FNUZ, c.mlirFloat8E4M3FNUZTypeGet }, - .f8e5m2 => .{ c.mlirTypeIsAFloat8E5M2, c.mlirFloat8E5M2TypeGet }, - .f8e5m2fnuz => .{ c.mlirTypeIsAFloat8E5M2FNUZ, c.mlirFloat8E5M2FNUZTypeGet }, - .bf16 => .{ c.mlirTypeIsABF16, c.mlirBF16TypeGet }, - .f16 => .{ c.mlirTypeIsAF16, c.mlirF16TypeGet }, - .f32 => .{ c.mlirTypeIsAF32, c.mlirF32TypeGet }, - .f64 => .{ c.mlirTypeIsAF64, c.mlirF64TypeGet }, +pub fn DenseArrayAttribute(comptime ElementType: DenseArrayTypes) type { + const ElementTypeZig = ElementType.ZigType(); + + const _isAFn, const getFn, const getElementFn = switch (ElementType) { + .bool => .{ c.mlirAttributeIsADenseBoolArray, c.mlirDenseBoolArrayGet, c.mlirDenseBoolArrayGetElement }, + .i8 => .{ c.mlirAttributeIsADenseI8Array, c.mlirDenseI8ArrayGet, c.mlirDenseI8ArrayGetElement }, + .i16 => .{ c.mlirAttributeIsADenseI16Array, c.mlirDenseI16ArrayGet, c.mlirDenseI16ArrayGetElement }, + .i32 => .{ c.mlirAttributeIsADenseI32Array, c.mlirDenseI32ArrayGet, c.mlirDenseI32ArrayGetElement }, + .i64 => .{ c.mlirAttributeIsADenseI64Array, c.mlirDenseI64ArrayGet, c.mlirDenseI64ArrayGetElement }, + .f32 => .{ c.mlirAttributeIsADenseF32Array, c.mlirDenseF32ArrayGet, c.mlirDenseF32ArrayGetElement }, + .f64 => .{ c.mlirAttributeIsADenseF64Array, c.mlirDenseF64ArrayGet, c.mlirDenseF64ArrayGetElement }, }; return struct { - _inner: c.MlirType, - const Self = @This(); - pub const is_a_fn = Config[0]; + const M = Methods(@This(), c.MlirAttribute); - pub const asType = Type.fromAny(Self); - pub const eql = Type.eqlAny(Self); - pub const format = helpers.format(Self, c.mlirTypePrint); + pub const isAFn = _isAFn; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); - pub fn init(ctx: Context) Self { - const type_get = Config[1]; - return .{ ._inner = type_get(ctx._inner) }; + pub fn init(ctx: *Context, values: []const ElementTypeZig) *const Self { + return @ptrCast(getFn(ctx.ptr(), @intCast(values.len), @ptrCast(values)).ptr); + } + + pub fn element(self: *const Self, pos: usize) ElementTypeZig { + return getElementFn(self.ptr(), @intCast(pos)); + } + + pub fn numElements(self: *const Self) usize { + return @intCast(c.mlirDenseArrayGetNumElements(self.ptr())); } }; } -pub const ComplexTypes = enum { - c64, - c128, - - unknown, -}; - -pub fn ComplexType(comptime ct: ComplexTypes) type { - return struct { - _inner: c.MlirType, - const Complex = @This(); - - fn mlirC64TypeGet(ctx: c.MlirContext) callconv(.c) c.MlirType { - return c.mlirComplexTypeGet(c.mlirF32TypeGet(ctx)); - } - - fn mlirC128TypeGet(ctx: c.MlirContext) callconv(.c) c.MlirType { - return c.mlirComplexTypeGet(c.mlirF64TypeGet(ctx)); - } - - fn mlirTypeIsAC64(typ: c.MlirType) callconv(.c) bool { - const element_type: c.MlirType = c.mlirComplexTypeGetElementType(typ); - return c.mlirTypeIsAF32(element_type); - } - - fn mlirTypeIsAC128(typ: c.MlirType) callconv(.c) bool { - const element_type: c.MlirType = c.mlirComplexTypeGetElementType(typ); - return c.mlirTypeIsAF64(element_type); - } - - const Config = switch (ct) { - .c64 => .{ mlirTypeIsAC64, mlirC64TypeGet }, - .c128 => .{ mlirTypeIsAC128, mlirC128TypeGet }, - .unknown => .{ c.mlirTypeIsAComplex, null }, - }; - - fn typeIsAUnknownComplex(typ: c.MlirType) callconv(.c) bool { - return c.mlirTypeIsAComplex(typ); - } - - pub const is_a_fn = Config[0]; - - pub const asType = Type.fromAny(Complex); - pub const eql = Type.eqlAny(Complex); - pub const format = Type.formatAny(Complex).format; - pub const ComplexTypeType: ComplexTypes = ct; - - pub const init = if (ct != .unknown) struct { - pub fn init(ctx: Context) Complex { - const type_get = Config[1]; - return .{ ._inner = type_get(ctx._inner) }; - } - }.init else {}; - }; +pub fn denseArrayAttribute(ctx: *Context, comptime ElementType: DenseArrayTypes, values: []const ElementType.ZigType()) *const Attribute { + return @ptrCast(DenseArrayAttribute(ElementType).init(ctx, @ptrCast(values))); } -pub const TupleType = struct { - _inner: c.MlirType, - pub const is_a_fn = c.mlirTypeIsATuple; +pub const ArrayAttribute = opaque { + const M = Methods(ArrayAttribute, c.MlirAttribute); - const Self = TupleType; + pub const isAFn = c.mlirAttributeIsAArray; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); - pub fn init(ctx: Context, elements: []const Type) !Self { - return Self.wrapOr(c.mlirTupleTypeGet( - ctx._inner, - @intCast(elements.len), - @ptrCast(elements.ptr), - )) orelse Error.InvalidMlir; + pub fn init(ctx: *Context, attrs: []const *const Attribute) *const ArrayAttribute { + return @ptrCast(c.mlirArrayAttrGet(ctx.ptr(), @intCast(attrs.len), @ptrCast(attrs)).ptr); } - pub fn getNumTypes(self: Self) usize { - return @intCast(c.mlirTupleTypeGetNumTypes(self._inner)); + pub fn element(self: *const ArrayAttribute, index: usize) Attribute { + return @ptrCast(c.mlirArrayAttrGetElement(self.ptr(), @intCast(index)).ptr); } - pub fn getElementType(self: Self, index: usize) Type { - return .{ ._inner = c.mlirTupleTypeGetType(self._inner, @intCast(index)) }; + pub fn numElements(self: *const ArrayAttribute) usize { + return @intCast(c.mlirArrayAttrGetNumElements(self.ptr())); } - - pub const asType = Type.fromAny(Self); }; -pub const FunctionType = struct { - _inner: c.MlirType, - pub const is_a_fn = c.mlirTypeIsAFunction; - const Self = FunctionType; - pub const asType = Type.fromAny(Self); - pub const eql = Type.eqlAny(IndexType); +pub fn arrayAttribute(ctx: *Context, attrs: []const *const Attribute) *const Attribute { + return @ptrCast(ArrayAttribute.init(ctx, attrs)); +} - pub fn init(ctx: Context, args: []const Type, results: []const Type) !Self { - const func = Type.wrapOr(c.mlirFunctionTypeGet( - ctx._inner, +pub const FunctionType = opaque { + const M = Methods(FunctionType, c.MlirType); + pub const isAFn = c.mlirTypeIsAFunction; + + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + pub fn init(ctx: *Context, args: []const *const Type, results: []const *const Type) *const FunctionType { + return @ptrCast(c.mlirFunctionTypeGet( + ctx.ptr(), @intCast(args.len), @ptrCast(args.ptr), @intCast(results.len), @ptrCast(results.ptr), - )) orelse return Error.InvalidMlir; - return func.as(Self).?; + ).ptr); } }; -pub const RankedTensorType = struct { - _inner: c.MlirType, - pub const is_a_fn = c.mlirTypeIsARankedTensor; - pub const eql = Type.eqlAny(RankedTensorType); - pub const format = helpers.format(Type, c.mlirTypePrint); +pub fn functionType(ctx: *Context, args: []const *const Type, results: []const *const Type) *const Type { + return @ptrCast(FunctionType.init(ctx, args, results)); +} - pub fn init(dimensions: []const i64, elemType: Type) RankedTensorType { - return .{ ._inner = c.mlirRankedTensorTypeGet( - @intCast(dimensions.len), - @ptrCast(dimensions.ptr), - elemType._inner, - c.mlirAttributeGetNull(), - ) }; +pub const FlatSymbolRefAttribute = opaque { + const M = Methods(FlatSymbolRefAttribute, c.MlirAttribute); + + pub const isAFn = c.mlirAttributeIsAFlatSymbolRef; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); + + pub fn init(ctx: *Context, symbol_: []const u8) *const FlatSymbolRefAttribute { + return @ptrCast(c.mlirFlatSymbolRefAttrGet(ctx.ptr(), stringRef(symbol_)).ptr); } - pub fn getElementType(self: RankedTensorType) Type { - return .{ ._inner = c.mlirShapedTypeGetElementType(self._inner) }; - } - - pub fn getRank(self: RankedTensorType) usize { - return @intCast(c.mlirShapedTypeGetRank(self._inner)); - } - - pub fn getDimension(self: RankedTensorType, dim: usize) i64 { - return c.mlirShapedTypeGetDimSize(self._inner, @intCast(dim)); - } - - pub const asType = Type.fromAny(RankedTensorType); -}; - -pub const Dialect = struct { - _inner: c.MlirDialect, - - const Self = Dialect; - - pub fn getContext(self: Self) Context { - return .{ ._inner = c.mlirDialectGetContext(self._inner) }; - } - - pub fn getNamespace(self: Self) []const u8 { - return fromStringRef(c.mlirDialectGetNamespace(self._inner)); + pub fn symbol(self: *const FlatSymbolRefAttribute) []const u8 { + return string(c.mlirFlatSymbolRefAttrGetValue(self.ptr())); } }; -pub const DialectHandle = struct { - _inner: c.MlirDialectHandle, +pub fn flatSymbolRefAttribute(ctx: *Context, symbol: []const u8) *const Attribute { + return @ptrCast(FlatSymbolRefAttribute.init(ctx, symbol)); +} - pub fn getNamespace(self: DialectHandle) []const u8 { - return fromStringRef(c.mlirDialectHandleGetNamespace(self._inner)); - } +pub const UnitAttribute = opaque { + const M = Methods(UnitAttribute, c.MlirAttribute); - pub fn insertDialect(self: DialectHandle, registry: Registry) void { - c.mlirDialectHandleInsertDialect(self._inner, registry._inner); - } + pub const isAFn = c.mlirAttributeIsAUnit; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirAttributeEqual); + pub const format = M.format(c.mlirAttributePrint); - pub fn registerDialect(self: DialectHandle, ctx: Context) void { - c.mlirDialectHandleRegisterDialect(self._inner, ctx._inner); - } - - pub fn loadDialect(self: DialectHandle, ctx: Context) Dialect { - return .{ ._inner = c.mlirDialectHandleLoadDialect(self._inner, ctx._inner) }; - } - - pub fn fromString(comptime namespace: []const u8) DialectHandle { - return .{ ._inner = @field(c, "mlirGetDialectHandle__" ++ namespace ++ "__")() }; + pub fn get(ctx: *Context) *const UnitAttribute { + return @ptrCast(c.mlirUnitAttrGet(ctx.ptr()).ptr); } }; -pub const Location = struct { - _inner: c.MlirLocation, +pub fn unitAttribute(ctx: *Context) *const Attribute { + return @ptrCast(UnitAttribute.get(ctx)); +} - pub const eql = helpers.eql(Type, c.mlirLocationEqual); - pub const format = helpers.format(Location, c.mlirLocationPrint); +pub const MemRefType = opaque { + const M = Methods(MemRefType, c.MlirType); - pub fn fromSrc(ctx: Context, src: std.builtin.SourceLocation) Location { - return .{ ._inner = c.mlirLocationFileLineColGet( - ctx._inner, - stringRef(src.file), - @intCast(src.line), - @intCast(src.column), - ) }; + pub const isAFn = c.mlirTypeIsAInteger; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + pub fn init(element_type: *const Type, shape: []const i64, layout: ?*const Attribute, memory_space: ?*const Attribute) *const MemRefType { + return @ptrCast(c.mlirMemRefTypeGet( + element_type.ptr(), + @intCast(shape.len), + @ptrCast(shape), + if (layout) |l| l.ptr() else .{}, + if (memory_space) |m| m.ptr() else .{}, + ).ptr); } - pub fn fileLineCol(ctx: Context, file: []const u8, line: usize, column: usize) Location { - return .{ ._inner = c.mlirLocationFileLineColGet( - ctx._inner, - stringRef(file), - @intCast(line), - @intCast(column), - ) }; - } - - pub fn callSite(callee: Location, caller: Location) Location { - return .{ ._inner = c.mlirLocationCallSiteGet(callee._inner, caller._inner) }; - } - - pub fn fused(ctx: Context, locations: []const Location, metadata: Attribute) Location { - return .{ ._inner = c.mlirLocationFusedGet( - ctx._inner, - @intCast(locations.len), - @ptrCast(locations.ptr), - metadata._inner, - ) }; - } - - pub fn named(loc: Location, ctx: Context, loc_name: [:0]const u8) Location { - return .{ ._inner = c.mlirLocationNameGet(ctx._inner, stringRef(loc_name), loc._inner) }; - } - - pub fn namedFmt(loc: Location, ctx: Context, comptime fmt: [:0]const u8, args: anytype) Location { - var buf: [256]u8 = undefined; - var stream = std.io.fixedBufferStream(&buf); - std.fmt.format(stream.writer(), fmt, args) catch { - buf[256 - 3 ..].* = "...".*; - }; - return loc.named(ctx, @ptrCast(stream.getWritten())); - } - - pub fn unknown(ctx: Context) Location { - return .{ ._inner = c.mlirLocationUnknownGet(ctx._inner) }; - } -}; - -pub const Block = struct { - _inner: c.MlirBlock, - - pub const wrapOr = helpers.wrapOr(Block, c.mlirBlockIsNull); - pub const deinit = helpers.deinit(Block, c.mlirBlockDestroy); - - pub const eql = helpers.eql(Block, c.mlirBlockEqual); - - pub fn init(args: []const Type, locs: []const Location) !Block { - const block = Block.wrapOr( - c.mlirBlockCreate(@intCast(args.len), @ptrCast(args.ptr), @ptrCast(locs.ptr)), + pub fn fromShaped(other: *const ShapedType) *const MemRefType { + var dims: stdx.BoundedArray(i64, ShapedType.MAX_RANK) = .{}; + for (0..other.rank()) |i| { + dims.appendAssumeCapacity(other.dimension(i)); + } + return init( + other.elementType(), + dims.constSlice(), + null, + null, ); - return block orelse error.InvalidMlir; } - pub fn argument(self: Block, index: usize) Value { - return .{ ._inner = c.mlirBlockGetArgument(self._inner, @intCast(index)) }; - } - - pub fn numArguments(self: Block) usize { - return @intCast(c.mlirBlockGetNumArguments(self._inner)); - } - - pub fn addArgument(self: *Block, typ: Type, loc: Location) Value { - return .{ ._inner = c.mlirBlockAddArgument(self._inner, typ._inner, loc._inner) }; - } - - pub fn insertArgument(self: *Block, index: usize, typ: Type, loc: Location) Value { - return .{ ._inner = c.mlirBlockInsertArgument(self._inner, @intCast(index), typ._inner, loc._inner) }; - } - - pub fn equals(self: Block, other: Block) bool { - return c.mlirBlockEqual(self._inner, other._inner); - } - - pub fn appendOperation(self: Block, op: Operation) void { - c.mlirBlockAppendOwnedOperation(self._inner, op._inner); - } - - pub fn appendOperations(self: *Block, ops: []const Operation) void { - for (ops) |op| { - c.mlirBlockAppendOwnedOperation(self._inner, op._inner); - } - } - - pub const RecursiveOpts = enum { open, hermetic }; - - pub fn appendValueRecursive(self: Block, value: Value, opt: RecursiveOpts) void { - switch (value.kind()) { - .op_result => |parent_op| self.appendOperationRecursive(parent_op, opt), - .block_argument => |arg| { - // Hermetic blocks are not allowed to use arguments from other blocks. - stdx.debug.assert(opt == .open or self.eql(arg.block()), "Can't add {f} from {*} block to {*} block", .{ arg, arg.block()._inner.ptr, self._inner.ptr }); - }, - .null => @panic("InvalidMlir"), - } - } - - pub fn appendOperationRecursive(self: Block, op: Operation, opt: RecursiveOpts) void { - if (op.block()) |prev_block| { - // Hermetic blocks are not allowed to reference values from other blocks. - stdx.debug.assert(opt == .open or self.equals(prev_block), "Can't add {} from {*} block to {*} block", .{ op, prev_block._inner.ptr, self._inner.ptr }); - return; - } - for (0..op.numOperands()) |i| { - self.appendValueRecursive(op.operand(i), opt); - } - self.appendOperation(op); + pub fn shaped(self: *const MemRefType) *const ShapedType { + return @ptrCast(self); } }; -pub const helpers = struct { - pub fn eql(T: type, equal_fn: fn (@FieldType(T, "_inner"), @FieldType(T, "_inner")) callconv(.c) bool) fn (T, T) bool { - return struct { - fn eql(a: T, b: T) bool { - return equal_fn(a._inner, b._inner); - } - }.eql; - } - - pub fn deinit(T: type, deinit_fn: fn (@FieldType(T, "_inner")) callconv(.c) void) fn (*T) void { - return struct { - fn deinit(a: *T) void { - deinit_fn(a._inner); - a.* = undefined; - } - }.deinit; - } - - pub fn dump(T: type, dump_fn: fn (@FieldType(T, "_inner")) callconv(.c) void) fn (T) void { - return struct { - fn dump(a: T) void { - return dump_fn(a._inner); - } - }.dump; - } - - pub fn isNull(T: type, is_null_fn: fn (@FieldType(T, "_inner")) callconv(.c) bool) fn (T) bool { - return struct { - fn isNull(a: T) bool { - return is_null_fn(a._inner); - } - }.isNull; - } - - pub fn format(Any: type, print_fn: fn (@FieldType(Any, "_inner"), ?*const MlirStrCallback, ?*anyopaque) callconv(.c) void) type { - return struct { - pub fn format(self: Any, writer: *std.Io.Writer) !void { - const WriterWithErr = struct { - writer: *std.Io.Writer, - err: ?std.Io.Writer.Error = null, - fn printCallback(mlir_str: c.MlirStringRef, opaque_ctx: ?*anyopaque) callconv(.c) void { - var ctx: *@This() = @alignCast(@ptrCast(opaque_ctx)); - if (ctx.err) |_| return; - _ = ctx.writer.write(mlir_str.data[0..mlir_str.length]) catch |err| { - ctx.err = err; - return; - }; - } - }; - - var context: WriterWithErr = .{ .writer = writer }; - print_fn(self._inner, &WriterWithErr.printCallback, &context); - if (context.err) |err| return err; - } - }; - } - - pub fn wrapOr(T: type, is_null_fn: fn (@FieldType(T, "_inner")) callconv(.c) bool) fn (@FieldType(T, "_inner")) ?T { - return struct { - fn wrapOr(inner: @FieldType(T, "_inner")) ?T { - if (is_null_fn(inner)) return null; - return .{ ._inner = inner }; - } - }.wrapOr; - } - - pub fn init(T: type, inner: @FieldType(T, "_inner"), is_null_fn: fn (@FieldType(T, "_inner")) callconv(.c) bool) ?T { - if (is_null_fn(inner)) return null; - return .{ ._inner = inner }; - } -}; +pub fn memRefType(element_type: *const Type, shape: []const i64, layout: ?*const Attribute, memory_space: ?*const Attribute) *const Type { + return @ptrCast(MemRefType.init(element_type, shape, layout, memory_space)); +} diff --git a/mlir/registry.zig b/mlir/registry.zig deleted file mode 100644 index 7536e701..00000000 --- a/mlir/registry.zig +++ /dev/null @@ -1,1466 +0,0 @@ -const std = @import("std"); - -const c = @import("c"); -const stdx = @import("stdx"); - -const log = std.log.scoped(.mlir); - -test { - std.testing.refAllDeclsRecursive(@This()); -} - -const Error = error{ - /// Invalid Mlir was created. - InvalidMlir, - /// Another Mlir error. Check the log for more context. - MlirUnexpected, - /// A resource/executor was not found. - NotFound, -} || std.mem.Allocator.Error; - -pub inline fn stringRef(str: []const u8) c.MlirStringRef { - return .{ .data = str.ptr, .length = str.len }; -} - -pub inline fn string(str: c.MlirStringRef) []const u8 { - // Note: mlir.StringRef need not to be null terminated. - return str.data[0..str.length]; -} - -pub const StringCallbackCtx = struct { - writer: *std.Io.Writer, - err: ?std.Io.Writer.Error = null, -}; - -pub fn stringCallback(str: c.MlirStringRef, ctx_: ?*anyopaque) callconv(.c) void { - const ctx: *StringCallbackCtx = @ptrCast(@alignCast(ctx_)); - ctx.writer.writeAll(string(str)) catch |err| { - ctx.err = err; - }; -} - -pub fn registerPasses(comptime passes: []const u8) void { - @field(c, "mlirRegister" ++ passes ++ "Passes")(); -} - -fn isPtrConst(comptime T: type) bool { - return @typeInfo(T).pointer.is_const; -} - -fn CastedPtr(comptime T1: type, comptime T2: type) type { - return if (@typeInfo(T1).pointer.is_const) *const T2 else *T2; -} - -pub fn Methods(comptime T: type, comptime NativeT: type) type { - return struct { - pub const IsPtrConst = @typeInfo(@typeInfo(@FieldType(NativeT, "ptr")).optional.child).pointer.is_const; - pub const Ptr = if (IsPtrConst) *const T else *T; - - pub fn ptr(self: anytype) NativeT { - return .{ .ptr = @ptrCast(@constCast(self)) }; - } - - fn format(f: anytype) fn (self: *const T, writer: *std.Io.Writer) std.Io.Writer.Error!void { - return struct { - fn format(self: *const T, writer: *std.Io.Writer) std.Io.Writer.Error!void { - var ctx: StringCallbackCtx = .{ .writer = writer }; - f(self.ptr(), stringCallback, &ctx); - if (ctx.err) |err| { - return err; - } - } - }.format; - } - - pub fn eql(f: anytype) fn (self: *const T, other: *const T) bool { - return struct { - fn eql(self: *const T, other: *const T) bool { - return f(self.ptr(), other.ptr()); - } - }.eql; - } - - pub fn isA(self: *const T, OtherType: type) ?CastedPtr(@TypeOf(self), OtherType) { - return if (OtherType.isAFn(self.ptr())) @ptrCast(self) else null; - } - }; -} - -pub const DialectRegistry = opaque { - const M = Methods(DialectRegistry, c.MlirDialectRegistry); - - pub const ptr = M.ptr; - - pub fn init() std.mem.Allocator.Error!*DialectRegistry { - return @ptrCast(c.mlirDialectRegistryCreate().ptr orelse return std.mem.Allocator.Error.OutOfMemory); - } - - pub fn deinit(self: *DialectRegistry) void { - c.mlirDialectRegistryDestroy(self.ptr()); - } - - pub fn registerDialect(self: *DialectRegistry, comptime name: []const u8) void { - DialectHandle.fromString(name).insertDialect(self); - } -}; - -pub const Context = opaque { - const M = Methods(Context, c.MlirContext); - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirContextEqual); - - pub const InitOptions = struct { - registry: ?*const DialectRegistry = null, - threading: bool = false, - }; - - pub fn init(opts: InitOptions) std.mem.Allocator.Error!*Context { - const ctx = if (opts.registry) |r| c.mlirContextCreateWithRegistry( - r.ptr(), - opts.threading, - ) else c.mlirContextCreateWithThreading(opts.threading); - return @ptrCast(ctx.ptr orelse return std.mem.Allocator.Error.OutOfMemory); - } - - pub fn deinit(self: *Context) void { - c.mlirContextDestroy(self.ptr()); - } - - pub fn setMultiThreading(self: *Context, enabled: bool) void { - c.mlirContextEnableMultithreading(self.ptr(), enabled); - } - - pub fn appendDialectRegistry(self: *Context, registry: *const DialectRegistry) void { - c.mlirContextAppendDialectRegistry(self.ptr(), registry.ptr()); - } - - pub fn loadAllAvailableDialects(self: *Context) void { - c.mlirContextLoadAllAvailableDialects(self.ptr()); - } - - pub fn numRegisteredDialects(self: *const Context) usize { - return @intCast(c.mlirContextGetNumRegisteredDialects(self.ptr())); - } - - pub fn numLoadedDialects(self: *const Context) usize { - return @intCast(c.mlirContextGetNumLoadedDialects(self.ptr())); - } - - pub fn numThreads(self: *const Context) usize { - return @intCast(c.mlirContextGetNumThreads(self.ptr())); - } - - pub fn isRegisteredOperation(self: *const Context, op: []const u8) bool { - return c.mlirContextIsRegisteredOperation(self.ptr(), stringRef(op)); - } - - pub fn format(self: *const Context, writer: *std.Io.Writer) !void { - try writer.print("{s}{{ .numThreads = {d}, .numRegisteredDialects = {d}, .numLoadedDialects = {d} }}", .{ - @typeName(Context), - self.numThreads(), - self.numRegisteredDialects(), - self.numLoadedDialects(), - }); - } -}; - -pub const Location = opaque { - const M = Methods(Location, c.MlirLocation); - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirLocationEqual); - pub const format = M.format(c.mlirLocationPrint); - - pub fn fromSrc(ctx: *Context, src: std.builtin.SourceLocation) *const Location { - return fileLineCol( - ctx, - src.file, - src.line, - src.column, - ); - } - - pub fn fileLineCol(ctx: *Context, file: []const u8, line: usize, column: usize) *const Location { - return @ptrCast(c.mlirLocationFileLineColGet( - ctx.ptr(), - stringRef(file), - @intCast(line), - @intCast(column), - ).ptr); - } - - pub fn callSite(callee: *const Location, caller: *const Location) *const Location { - return @ptrCast(c.mlirLocationCallSiteGet(callee.ptr(), caller.ptr()).ptr); - } - - pub fn fused(ctx: *Context, locs: []const *const Location, metadata: *const Attribute) *const Location { - return @ptrCast(c.mlirLocationFusedGet( - ctx.ptr(), - @intCast(locs.len), - @ptrCast(locs), - metadata.ptr(), - ).ptr); - } - - pub fn named(self: *const Location, ctx: *Context, loc_name: []const u8) *const Location { - return @ptrCast(c.mlirLocationNameGet(ctx.ptr(), stringRef(loc_name), self.ptr()).ptr); - } - - pub fn namedFmt(loc: *const Location, ctx: *Context, comptime fmt: []const u8, args: anytype) *const Location { - var buf: [256]u8 = undefined; - return loc.named(loc, ctx, std.fmt.bufPrint(&buf, fmt, args) catch blk: { - buf[buf.len - 3 ..].* = "...".*; - break :blk &buf; - }); - } - - pub fn fromAttribute(attribute: *const Attribute) *const Location { - return @ptrCast(c.mlirLocationFromAttribute(attribute.ptr()).ptr); - } - - pub fn unknown(ctx: *Context) *const Location { - return @ptrCast(c.mlirLocationUnknownGet(ctx.ptr()).ptr); - } -}; - -pub const Type = opaque { - const M = Methods(Type, c.MlirType); - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirTypeEqual); - pub const format = M.format(c.mlirTypePrint); - pub const isA = M.isA; - - pub fn parse(ctx: *Context, str: []const u8) Error!*const Type { - return @ptrCast(c.mlirTypeParseGet(ctx.ptr(), stringRef(str)).ptr orelse return Error.InvalidMlir); - } -}; - -pub const IndexType = opaque { - const M = Methods(IndexType, c.MlirType); - - pub const isAFn = c.mlirTypeIsAIndex; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirTypeEqual); - pub const format = M.format(c.mlirTypePrint); - - pub fn get(ctx: *Context) *const IndexType { - return @ptrCast(c.mlirIndexTypeGet(ctx.ptr()).ptr); - } -}; - -pub fn indexType(ctx: *Context) *const Type { - return @ptrCast(IndexType.get(ctx)); -} - -pub const IntegerTypes = enum { - i1, - i4, - i8, - i16, - i32, - i64, - i128, - u1, - u4, - u8, - u16, - u32, - u64, - u128, - si1, - si4, - si8, - si16, - si32, - si64, - si128, - - pub fn bitwidth(self: IntegerTypes) usize { - return switch (self) { - .i1, .u1, .si1 => 1, - .i4, .u4, .si4 => 4, - .i8, .u8, .si8 => 8, - .i16, .u16, .si16 => 16, - .i32, .u32, .si32 => 32, - .i64, .u64, .si64 => 64, - .i128, .u128, .si128 => 128, - }; - } - - pub fn signedness(self: IntegerTypes) IntegerType.Signedness { - return switch (self) { - .i1, .i4, .i8, .i16, .i32, .i64, .i128 => .signless, - .si1, .si4, .si8, .si16, .si32, .si64, .si128 => .signed, - .u1, .u4, .u8, .u16, .u32, .u64, .u128 => .unsigned, - }; - } -}; - -pub const IntegerType = opaque { - pub const Signedness = enum { - signless, - signed, - unsigned, - }; - - const M = Methods(IntegerType, c.MlirType); - - pub const isAFn = c.mlirTypeIsAInteger; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirTypeEqual); - pub const format = M.format(c.mlirTypePrint); - - fn get(ctx: *Context, it: IntegerTypes) *const IntegerType { - return exact(ctx, it.bitwidth(), it.signedness()); - } - - fn exact(ctx: *Context, bitwidth: usize, sign: Signedness) *const IntegerType { - return @ptrCast(switch (sign) { - .signless => c.mlirIntegerTypeGet(ctx.ptr(), @intCast(bitwidth)).ptr, - .signed => c.mlirIntegerTypeSignedGet(ctx.ptr(), @intCast(bitwidth)).ptr, - .unsigned => c.mlirIntegerTypeUnsignedGet(ctx.ptr(), @intCast(bitwidth)).ptr, - }); - } -}; - -pub fn integerType(ctx: *Context, it: IntegerTypes) *const Type { - return @ptrCast(IntegerType.get(ctx, it)); -} - -pub const FloatTypes = enum { - f4e2m1fn, - f6e2m3fn, - f6e3m2fn, - f8e3m4, - f8e4m3, - f8e4m3b11fnuz, - f8e4m3fn, - f8e4m3fnuz, - f8e5m2, - f8e5m2fnuz, - f8e8m0fnu, - bf16, - f16, - f32, - f64, - // f80, - // f128, - tf32, -}; - -pub fn FloatType(comptime ft: FloatTypes) type { - const _isAFn, const getter = switch (ft) { - .f4e2m1fn => .{ c.mlirTypeIsAFloat4E2M1FN, c.mlirFloat4E2M1FNTypeGet }, - .f6e2m3fn => .{ c.mlirTypeIsAFloat6E2M3FN, c.mlirFloat6E2M3FNTypeGet }, - .f6e3m2fn => .{ c.mlirTypeIsAFloat6E3M2FN, c.mlirFloat6E3M2FNTypeGet }, - .f8e3m4 => .{ c.mlirTypeIsAFloat8E3M4, c.mlirFloat8E3M4TypeGet }, - .f8e4m3 => .{ c.mlirTypeIsAFloat8E4M3, c.mlirFloat8E4M3TypeGet }, - .f8e4m3b11fnuz => .{ c.mlirTypeIsAFloat8E4M3B11FNUZ, c.mlirFloat8E4M3B11FNUZTypeGet }, - .f8e4m3fn => .{ c.mlirTypeIsAFloat8E4M3FN, c.mlirFloat8E4M3FNTypeGet }, - .f8e4m3fnuz => .{ c.mlirTypeIsAFloat8E4M3FNUZ, c.mlirFloat8E4M3FNUZTypeGet }, - .f8e5m2 => .{ c.mlirTypeIsAFloat8E5M2, c.mlirFloat8E5M2TypeGet }, - .f8e5m2fnuz => .{ c.mlirTypeIsAFloat8E5M2FNUZ, c.mlirFloat8E5M2FNUZTypeGet }, - .f8e8m0fnu => .{ c.mlirTypeIsAFloat8E8M0FNU, c.mlirFloat8E8M0FNUTypeGet }, - .bf16 => .{ c.mlirTypeIsABF16, c.mlirBF16TypeGet }, - .f16 => .{ c.mlirTypeIsAF16, c.mlirF16TypeGet }, - .f32 => .{ c.mlirTypeIsAF32, c.mlirF32TypeGet }, - .f64 => .{ c.mlirTypeIsAF64, c.mlirF64TypeGet }, - .tf32 => .{ c.mlirTypeIsATF32, c.mlirTF32TypeGet }, - }; - - return opaque { - const Self = @This(); - const M = Methods(Self, c.MlirType); - - pub const isAFn = _isAFn; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirTypeEqual); - pub const format = M.format(c.mlirTypePrint); - - pub fn get(ctx: *Context) *const Self { - return @ptrCast(getter(ctx.ptr()).ptr); - } - }; -} - -pub fn floatType(ft: FloatTypes, ctx: *Context) *const Type { - return switch (ft) { - inline else => |v| @ptrCast(FloatType(v).get(ctx)), - }; -} - -pub const Attribute = opaque { - const M = Methods(Attribute, c.MlirAttribute); - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - pub const isA = M.isA; - - pub fn parse(ctx: *Context, attr: []const u8) Error!*const Attribute { - return @ptrCast(c.mlirAttributeParseGet(ctx.ptr(), stringRef(attr)).ptr orelse return Error.InvalidMlir); - } - - pub fn context(self: *const Context) *Context { - return @ptrCast(c.mlirAttributeGetContext(self.ptr()).ptr); - } - - pub fn dialect(self: *const Context) *const Dialect { - return @ptrCast(c.mlirAttributeGetDialect(self.ptr()).ptr); - } -}; - -pub const StringAttribute = opaque { - const M = Methods(StringAttribute, c.MlirAttribute); - - pub const isAFn = c.mlirAttributeIsAString; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn init(ctx: *Context, str: []const u8) *const StringAttribute { - return @ptrCast(c.mlirStringAttrGet(ctx.ptr(), stringRef(str)).ptr); - } - - pub fn value(self: *const StringAttribute) []const u8 { - return string(c.mlirStringAttrGetValue(self.ptr())); - } -}; - -pub fn stringAttribute(ctx: *Context, str: []const u8) *const Attribute { - return @ptrCast(StringAttribute.init(ctx, str)); -} - -pub const IntegerAttribute = opaque { - const M = Methods(IntegerAttribute, c.MlirAttribute); - - pub const isAFn = c.mlirAttributeIsAInteger; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn init(ctx: *Context, it: IntegerTypes, value_: anytype) *const IntegerAttribute { - return @ptrCast(c.mlirIntegerAttrGet( - IntegerType.get(ctx, it).ptr(), - @intCast(value_), - ).ptr); - } - - pub fn value(self: *const IntegerAttribute, comptime T: type) T { - const getValue = switch (@typeInfo(T).int.signedness) { - .signed => c.mlirIntegerAttrGetValueInt, - .unsigned => c.mlirIntegerAttrGetValueUInt, - }; - return @intCast(getValue(self.ptr())); - } -}; - -pub fn integerAttribute(ctx: *Context, it: IntegerTypes, value_: anytype) *const Attribute { - return @ptrCast(IntegerAttribute.init(ctx, it, value_)); -} - -pub const BoolAttribute = opaque { - const M = Methods(BoolAttribute, c.MlirAttribute); - - pub const isAFn = c.mlirAttributeIsABool; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn init(ctx: *Context, value_: bool) *const BoolAttribute { - return @ptrCast(c.mlirBoolAttrGet(ctx.ptr(), @intFromBool(value_)).ptr); - } - - pub fn value(self: *const BoolAttribute) bool { - return c.mlirBoolAttrGetValue(self.ptr()); - } -}; - -pub fn boolAttribute(ctx: *Context, value: bool) *const Attribute { - return @ptrCast(BoolAttribute.init(ctx, value)); -} - -pub const TypeAttribute = opaque { - const M = Methods(TypeAttribute, c.MlirAttribute); - - pub const isAFn = c.mlirAttributeIsAType; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn init(type_: *const Type) *const TypeAttribute { - return @ptrCast(c.mlirTypeAttrGet(type_.ptr()).ptr); - } - - pub fn value(self: *const TypeAttribute) *const Type { - return @ptrCast(c.mlirTypeAttrGetValue(self.ptr()).ptr); - } -}; - -pub fn typeAttribute(type_: *const Type) *const Attribute { - return @ptrCast(TypeAttribute.init(type_)); -} - -pub const Dialect = struct { - const M = Methods(Dialect, c.MlirDialect); - - pub const ptr = M.ptr; - - pub fn context(self: *const Dialect) *Context { - return @ptrCast(c.mlirDialectGetContext(self.ptr()).ptr); - } - - pub fn namespace(self: *const Dialect) []const u8 { - return string(c.mlirDialectGetNamespace(self.ptr())); - } - - pub fn format(self: *const Dialect, writer: *std.Io.Writer) !void { - try writer.print("{s}{{ .namespace = \"{s}\" }}", .{ @typeName(Dialect), self.namespace() }); - } -}; - -pub const DialectHandle = opaque { - const M = Methods(DialectHandle, c.MlirDialectHandle); - - pub const ptr = M.ptr; - - pub fn format(self: *const DialectHandle, writer: *std.Io.Writer) !void { - try writer.print("{s}{{ .namespace = \"{s}\" }}", .{ @typeName(DialectHandle), self.namespace() }); - } - - pub fn namespace(self: *const DialectHandle) []const u8 { - return string(c.mlirDialectHandleGetNamespace(self.ptr())); - } - - pub fn insertDialect(self: *const DialectHandle, registry: *DialectRegistry) void { - c.mlirDialectHandleInsertDialect(self.ptr(), registry.ptr()); - } - - pub fn registerDialect(self: *const DialectHandle, ctx: *Context) void { - c.mlirDialectHandleRegisterDialect(self.ptr(), ctx.ptr()); - } - - pub fn loadDialect(self: *const DialectHandle, ctx: *Context) *Dialect { - return @ptrCast(c.mlirDialectHandleLoadDialect(self.ptr(), ctx.ptr()).ptr); - } - - pub fn fromString(comptime namespace_: []const u8) *const DialectHandle { - return @ptrCast(@field(c, "mlirGetDialectHandle__" ++ namespace_ ++ "__")().ptr); - } -}; - -pub const Identifier = opaque { - const M = Methods(Identifier, c.MlirIdentifier); - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirIdentifierEqual); - - pub fn context(self: *const Identifier) *Context { - return @ptrCast(c.mlirIdentifierGetContext(self.ptr()).ptr); - } - - pub fn get(ctx: *Context, str_: []const u8) *const Identifier { - return @ptrCast(c.mlirIdentifierGet(ctx.ptr(), stringRef(str_)).ptr); - } - - pub fn str(self: *const Identifier) []const u8 { - return string(c.mlirIdentifierStr(self.ptr())); - } - - pub fn format(self: *const Identifier, writer: *std.Io.Writer) !void { - try writer.print("{s}{{ .str = \"{s}\" }}", .{ @typeName(Identifier), self.str() }); - } -}; - -pub const Module = opaque { - const M = Methods(Module, c.MlirModule); - - pub const ptr = M.ptr; - - pub fn init(loc: *const Location) *Module { - return @constCast(@ptrCast(c.mlirModuleCreateEmpty(loc.ptr()).ptr)); - } - - pub fn deinit(self: *Module) void { - c.mlirModuleDestroy(self.ptr()); - } - - pub fn fromOperation(op: *Operation) *Module { - return @constCast(@ptrCast(c.mlirModuleFromOperation(op.ptr()).ptr)); - } - - pub fn operation(self: *const Module) *Operation { - return @ptrCast(c.mlirModuleGetOperation(self.ptr()).ptr); - } - - pub fn parse(ctx: *Context, source: []const u8) Error!*Module { - return @constCast(@ptrCast(c.mlirModuleCreateParse(ctx.ptr(), stringRef(source)).ptr orelse return Error.InvalidMlir)); - } - - pub fn context(self: *const Module) *Context { - return @ptrCast(c.mlirModuleGetContext(self.ptr()).ptr); - } - - pub fn body(self: *const Module) *Block { - return @ptrCast(c.mlirModuleGetBody(self.ptr()).ptr); - } -}; - -pub const Block = opaque { - const M = Methods(Block, c.MlirBlock); - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirBlockEqual); - pub const format = M.format(c.mlirBlockPrint); - - pub fn init(args: []const *const Type, locs: []const *const Location) *Block { - return @ptrCast(c.mlirBlockCreate(@intCast(args.len), @ptrCast(args), @ptrCast(locs)).ptr); - } - - pub fn deinit(self: *Block) void { - std.debug.assert(self.parentOperation() == null); - c.mlirBlockDestroy(self.ptr()); - } - - pub fn argument(self: *const Block, index: usize) *const Value { - return @ptrCast(c.mlirBlockGetArgument(self.ptr(), @intCast(index)).ptr); - } - - pub fn numArguments(self: *const Block) usize { - return @intCast(c.mlirBlockGetNumArguments(self.ptr())); - } - - pub fn addArgument(self: *Block, type_: *const Type, loc: *const Location) *const Value { - return @ptrCast(c.mlirBlockAddArgument(self.ptr(), type_.ptr(), loc.ptr()).ptr); - } - - pub fn insertArgument(self: *Block, index: usize, type_: *const Type, loc: *const Location) *const Value { - return @ptrCast(c.mlirBlockInsertArgument(self.ptr(), @intCast(index), type_.ptr(), loc.ptr()).ptr); - } - - pub fn appendOwnedOperation(self: *Block, op: *Operation) void { - c.mlirBlockAppendOwnedOperation(self.ptr(), op.ptr()); - } - - pub fn appendOwnedOperations(self: *Block, ops: []const *Operation) void { - for (ops) |op| { - self.appendOwnedOperation(op); - } - } - - pub fn parentOperation(self: *const Block) ?*Operation { - return @ptrCast(c.mlirBlockGetParentOperation(self.ptr()).ptr); - } - - pub fn detach(self: *Block) void { - c.mlirBlockDetach(self.ptr()); - } - - pub fn terminator(self: *const Block) ?*Operation { - return @ptrCast(c.mlirBlockGetTerminator(self.ptr()).ptr); - } -}; - -pub const NamedAttribute = struct { - inner: c.MlirNamedAttribute, - - pub fn ptr(self: NamedAttribute) c.MlirNamedAttribute { - return self.inner; - } - - pub fn name(self: NamedAttribute) *const Identifier { - return @ptrCast(self.inner.name.ptr); - } - - pub fn attribute(self: NamedAttribute) *const Attribute { - return @ptrCast(self.inner.attribute.ptr); - } - - pub fn named(ctx: *Context, name_: []const u8, attr: *const Attribute) NamedAttribute { - return .{ - .inner = .{ - .name = Identifier.get(ctx, name_).ptr(), - .attribute = attr.ptr(), - }, - }; - } - - pub fn init(name_: *const Identifier, attr: *const Attribute) NamedAttribute { - return .{ - .inner = .{ - .name = name_.ptr(), - .attribute = attr.ptr(), - }, - }; - } - - pub fn format(self: NamedAttribute, writer: *std.Io.Writer) std.Io.Writer.Error!void { - try writer.print("{s}{{ .{s} = \"{f}\" }}", .{ - @typeName(NamedAttribute), - self.name().str(), - self.attribute(), - }); - } -}; - -pub const DictionaryAttribute = opaque { - const M = Methods(DictionaryAttribute, c.MlirAttribute); - - pub const isAFn = c.mlirAttributeIsADictionary; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn init(ctx: *Context, attributes: []const NamedAttribute) *const DictionaryAttribute { - return @ptrCast(c.mlirDictionaryAttrGet( - ctx.ptr(), - @intCast(attributes.len), - @ptrCast(attributes), - ).ptr); - } - - pub fn size(self: *const DictionaryAttribute) usize { - return @intCast(c.mlirDictionaryAttrGetNumElements(self.ptr())); - } - - pub fn get(self: *const DictionaryAttribute, pos: usize) NamedAttribute { - return .{ .inner = c.mlirDictionaryAttrGetElement(self.ptr(), @intCast(pos)) }; - } - - pub fn getByName(self: *const DictionaryAttribute, name: []const u8) ?*const Attribute { - return @ptrCast(c.mlirDictionaryAttrGetElementByName(self.ptr(), stringRef(name)).ptr); - } -}; - -pub fn dictionaryAttribute(ctx: *Context, attributes: []const NamedAttribute) *const Attribute { - return @ptrCast(DictionaryAttribute.init(ctx, attributes)); -} - -pub const Value = opaque { - const M = Methods(Value, c.MlirValue); - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirValueEqual); - pub const format = M.format(c.mlirValuePrint); - pub const isA = M.isA; - - pub fn type_(val: *const Value) *const Type { - return @ptrCast(c.mlirValueGetType(val.ptr()).ptr); - } - - pub fn setType(self: *Value, type__: *const Type) void { - c.mlirValueSetType(self.ptr(), type__.ptr()); - } - - pub fn firstUse(self: *const Value) *const OpOperand { - return @ptrCast(c.mlirValueGetFirstUse(self.ptr()).ptr); - } - - pub fn replaceAllUsesWith(self: *const Value, with: *const Value) void { - c.mlirValueReplaceAllUsesOfWith(self.ptr(), with.ptr()); - } -}; - -pub const OpOperand = opaque { - const M = Methods(OpOperand, c.MlirOpOperand); - - pub const ptr = M.ptr; - - pub fn owner(self: *const OpOperand) *Operation { - return @ptrCast(c.mlirOpOperandGetOwner(self.ptr()).ptr); - } - - pub fn number(self: *const OpOperand) usize { - return @intCast(c.mlirOpOperandGetOperandNumber(self.ptr())); - } - - pub fn nextUse(self: *const OpOperand) ?*const OpOperand { - return @ptrCast(c.mlirOpOperandGetNextUse(self.ptr()).ptr); - } -}; - -pub const OpResult = opaque { - const M = Methods(OpResult, c.MlirValue); - - pub const isAFn = c.mlirValueIsAOpResult; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirValueEqual); - pub const format = M.format(c.mlirValuePrint); - - pub fn owner(self: *const OpResult) *Operation { - return @ptrCast(c.mlirOpResultGetOwner(self.ptr()).ptr); - } - - pub fn resultNumber(self: *const OpResult) usize { - return @intCast(c.mlirOpResultGetResultNumber(self.ptr())); - } -}; - -pub const BlockArgument = opaque { - const M = Methods(BlockArgument, c.MlirValue); - - pub const isAFn = c.mlirValueIsABlockArgument; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirValueEqual); - pub const format = M.format(c.mlirValuePrint); - - pub fn block(arg: *const BlockArgument) *Block { - return @ptrCast(c.mlirBlockArgumentGetOwner(arg.ptr()).ptr); - } - - pub fn number(self: *const BlockArgument) usize { - return @intCast(c.mlirBlockArgumentGetArgNumber(self.ptr())); - } - - pub fn setType(self: *BlockArgument, type_: *const Type) void { - c.mlirBlockArgumentSetType(self.ptr(), type_.ptr()); - } -}; - -pub const OperationState = struct { - inner: c.MlirOperationState, - - pub fn init(name: []const u8, location: *const Location) OperationState { - return .{ .inner = c.mlirOperationStateGet(stringRef(name), location.ptr()) }; - } - - pub fn addResults(self: *OperationState, results: []const *const Type) void { - c.mlirOperationStateAddResults(&self.inner, @intCast(results.len), @ptrCast(results)); - } - - pub fn addOperands(self: *OperationState, operands: []const *const Value) void { - c.mlirOperationStateAddOperands(&self.inner, @intCast(operands.len), @ptrCast(operands)); - } - - pub fn addOwnedRegions(self: *OperationState, regions: []const *Region) void { - c.mlirOperationStateAddOwnedRegions(&self.inner, @intCast(regions.len), @ptrCast(regions)); - } - - pub fn addSuccessors(self: *OperationState, successors: []*const Block) void { - c.mlirOperationStateAddSuccessors(&self.inner, @intCast(successors.len), @ptrCast(successors)); - } - - pub fn addAttributes(self: *OperationState, attributes: []const NamedAttribute) void { - c.mlirOperationStateAddAttributes(&self.inner, @intCast(attributes.len), @ptrCast(attributes.ptr)); - } - - pub fn enableResultTypeInference(self: *OperationState) void { - c.mlirOperationStateEnableResultTypeInference(&self.inner); - } -}; - -pub const Operation = opaque { - const M = Methods(Operation, c.MlirOperation); - - const MAX_SEGMENTS = 32; - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirOperationEqual); - pub const format = M.format(c.mlirOperationPrint); - - pub fn init(state: *OperationState) !*Operation { - defer state.* = undefined; - return @ptrCast(c.mlirOperationCreate(&state.inner).ptr orelse return Error.InvalidMlir); - } - - pub fn deinit(self: *Operation) void { - std.debug.assert(self.block() == null); - c.mlirOperationDestroy(self.ptr()); - } - - pub fn parse(ctx: *Context, source: []const u8, name_: []const u8) Error!*Operation { - return @ptrCast(c.mlirOperationCreateParse(ctx.ptr(), stringRef(source), stringRef(name_)).ptr orelse return Error.InvalidMlir); - } - - pub fn name(self: *const Operation) []const u8 { - return string(c.mlirOperationGetName(self.ptr())); - } - - pub fn clone(self: *const Operation) *Operation { - return @ptrCast(c.mlirOperationClone(self.ptr()).ptr); - } - - pub fn block(self: *const Operation) ?*Block { - return @ptrCast(c.mlirOperationGetBlock(self.ptr()).ptr); - } - - pub fn appendTo(self: *Operation, block_: *Block) *Operation { - block_.appendOwnedOperation(self); - return self; - } - - pub fn parent(self: *const Operation) ?*Operation { - return @ptrCast(c.mlirOperationGetParentOperation(self.ptr()).ptr); - } - - pub fn removeFromParent(self: *Operation) void { - c.mlirOperationRemoveFromParent(self.ptr()); - } - - pub fn numOperands(self: *const Operation) usize { - return @intCast(c.mlirOperationGetNumOperands(self.ptr())); - } - - pub fn operand(self: *const Operation, index: usize) *const Value { - return @ptrCast(c.mlirOperationGetOperand(self.ptr(), @intCast(index)).ptr); - } - - pub fn setOperand(self: *Operation, index: usize, value: *const Value) void { - c.mlirOperationSetOperand(self.ptr(), @intCast(index), value.ptr()); - } - - pub fn numResults(self: *const Operation) usize { - return @intCast(c.mlirOperationGetNumResults(self.ptr())); - } - - pub fn result(self: *const Operation, index: usize) *const Value { - return @ptrCast(c.mlirOperationGetResult(self.ptr(), @intCast(index)).ptr); - } - - pub fn nextInBlock(self: *const Operation) *const Operation { - return @ptrCast(c.mlirOperationGetNextInBlock(self.ptr()).ptr); - } - - pub fn context(self: *const Operation) *Context { - return @ptrCast(c.mlirOperationGetContext(self.ptr()).ptr); - } - - pub const PrintFlags = struct { - elide_large_elements_attrs: ?usize = null, - debug_info: bool = false, - debug_info_pretty_form: bool = true, - print_generic_op_form: bool = false, - use_local_scope: bool = false, - assume_verified: bool = false, - - const Ctx = struct { - self: *const Operation, - flags: PrintFlags, - }; - }; - - fn formatWithFlags(fctx: PrintFlags.Ctx, writer: *std.io.Writer) std.Io.Writer.Error!void { - const flags = c.mlirOpPrintingFlagsCreate(); - defer c.mlirOpPrintingFlagsDestroy(flags); - - if (fctx.flags.elide_large_elements_attrs) |v| { - c.mlirOpPrintingFlagsElideLargeElementsAttrs(flags, @intCast(v)); - } - c.mlirOpPrintingFlagsEnableDebugInfo( - flags, - fctx.flags.debug_info, - fctx.flags.debug_info_pretty_form, - ); - if (fctx.flags.print_generic_op_form) { - c.mlirOpPrintingFlagsPrintGenericOpForm(flags); - } - if (fctx.flags.use_local_scope) { - c.mlirOpPrintingFlagsUseLocalScope(flags); - } - if (fctx.flags.assume_verified) { - c.mlirOpPrintingFlagsAssumeVerified(flags); - } - var sctx: StringCallbackCtx = .{ .writer = writer }; - c.mlirOperationPrintWithFlags(fctx.self.ptr(), flags, stringCallback, &sctx); - if (sctx.err) |err| { - return err; - } - } - - pub fn fmt(self: *const Operation, flags: PrintFlags) std.fmt.Alt(PrintFlags.Ctx, formatWithFlags) { - return .{ .data = .{ - .self = self, - .flags = flags, - } }; - } - - pub const MakeArgs = struct { - pub const AttrTuple = struct { []const u8, *const Attribute }; - - pub const Operands = union(enum) { - flat: []const *const Value, - variadic: []const []const *const Value, - }; - - pub const ResultTypes = union(enum) { - flat: []const *const Type, - variadic: []const []const *const Type, - }; - - operands: ?Operands = null, - results: ?ResultTypes = null, - result_type_inference: bool = false, - attributes: ?[]const NamedAttribute = null, - blocks: ?[]const *Block = null, - verify: bool = true, - location: ?*const Location = null, - }; - - pub fn try_make(ctx: *Context, name_: []const u8, args: MakeArgs) !*Operation { - var state: OperationState = .init(name_, args.location orelse .unknown(ctx)); - if (args.result_type_inference) { - state.enableResultTypeInference(); - } - if (args.operands) |operands| switch (operands) { - .flat => |v| state.addOperands(v), - .variadic => |segments| { - var sizes: stdx.BoundedArray(i32, MAX_SEGMENTS) = .{}; - for (segments) |segment_operands| { - state.addOperands(segment_operands); - sizes.appendAssumeCapacity(@intCast(segment_operands.len)); - } - state.addAttributes(&.{ - .named(ctx, "operandSegmentSizes", denseArrayAttribute(ctx, .i32, sizes.constSlice())), - }); - }, - }; - if (args.results) |results| switch (results) { - .flat => |v| state.addResults(v), - .variadic => |segments| { - var sizes: stdx.BoundedArray(i32, MAX_SEGMENTS) = .{}; - for (segments) |segment_results| { - state.addResults(segment_results); - sizes.appendAssumeCapacity(@intCast(segment_results.len)); - } - state.addAttributes(&.{ - .named(ctx, "resultSegmentSizes", denseArrayAttribute(ctx, .i32, sizes.constSlice())), - }); - }, - }; - if (args.attributes) |attrs| { - state.addAttributes(attrs); - } - if (args.blocks) |blocks| { - const region = Region.init(); - for (blocks) |block_| { - region.appendOwnedBlock(block_); - } - state.addOwnedRegions(&.{region}); - } - const new_op = try Operation.init(&state); - errdefer new_op.deinit(); - if (args.verify and new_op.verify() == false) { - log.err("Failed to verify MLIR operation:\n{f}", .{ - new_op.fmt(.{ - .print_generic_op_form = true, - .debug_info = true, - }), - }); - return Error.InvalidMlir; - } - return new_op; - } - - pub fn make(ctx: *Context, op_name: []const u8, args: MakeArgs) *Operation { - return try_make(ctx, op_name, args) catch { - @panic("Failed to create MLIR operation"); - }; - } - - pub fn verify(self: *const Operation) bool { - return c.mlirOperationVerify(self.ptr()); - } - - pub fn location(self: *const Operation) *const Location { - return @ptrCast(c.mlirOperationGetLocation(self.ptr()).ptr); - } - - pub const WalkOrder = enum(c.MlirWalkOrder) { - pre_order = c.MlirWalkPreOrder, - post_order = c.MlirWalkPostOrder, - }; - - pub const WalkResult = enum(c.MlirWalkResult) { - advance = c.MlirWalkResultAdvance, - interrupt = c.MlirWalkResultInterrupt, - skip = c.MlirWalkResultSkip, - }; - - pub fn walk(self: *Operation, order: WalkOrder, ctx: anytype, walkfn: fn (ctx_: @TypeOf(ctx), op: *Operation) WalkResult) void { - var inner_ctx = .{ .ctx = ctx }; - const ContextType = @TypeOf(inner_ctx); - - c.mlirOperationWalk( - self._inner, - (struct { - pub fn callback(op: c.MlirOperation, ctx_: ?*anyopaque) callconv(.c) c.MlirWalkResult { - const inner_ctx_: *ContextType = @ptrCast(@alignCast(ctx_)); - return @intFromEnum(walkfn(inner_ctx_.ctx, @ptrCast(op.ptr))); - } - }).callback, - &inner_ctx, - @intFromEnum(order), - ); - } - - pub const BytecodeWriterConfig = struct { - desired_emit_version: ?usize = null, - - const Ctx = struct { - self: *const Operation, - config: ?BytecodeWriterConfig, - }; - }; - - pub fn writeBytecode(self: *const Operation, config: ?BytecodeWriterConfig, writer: *std.Io.Writer) !void { - if (config) |config_| { - const bc_config = c.mlirBytecodeWriterConfigCreate(); - defer c.mlirBytecodeWriterConfigDestroy(bc_config); - if (config_.desired_emit_version) |version| { - c.mlirBytecodeWriterConfigDesiredEmitVersion(bc_config, @intCast(version)); - } - var sctx: StringCallbackCtx = .{ .writer = writer }; - c.mlirOperationWriteBytecodeWithConfig(self.ptr(), bc_config, stringCallback, &sctx); - if (sctx.err) |err| { - return err; - } - return; - } - - var sctx: StringCallbackCtx = .{ .writer = writer }; - c.mlirOperationWriteBytecode(self.ptr(), stringCallback, &sctx); - if (sctx.err) |err| { - return err; - } - } - - fn fmtBytecodeImpl(ctx: BytecodeWriterConfig.Ctx, writer: *std.io.Writer) std.Io.Writer.Error!void { - try ctx.self.writeBytecode(ctx.config, writer); - } - - pub fn fmtBytecode(self: *const Operation, config: ?BytecodeWriterConfig) std.fmt.Alt(BytecodeWriterConfig.Ctx, fmtBytecodeImpl) { - return .{ .data = .{ - .self = self, - .config = config, - } }; - } -}; - -pub const Region = opaque { - const M = Methods(Region, c.MlirRegion); - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirRegionEqual); - - pub fn init() *Region { - return @ptrCast(c.mlirRegionCreate().ptr); - } - - pub fn deinit(self: *Region) void { - c.mlirRegionDestroy(self.ptr()); - } - - pub fn appendOwnedBlock(self: *Region, block: *Block) void { - c.mlirRegionAppendOwnedBlock(self.ptr(), block.ptr()); - } - - pub fn insertOwnedBlock(self: *Region, index: usize, block: *Block) void { - c.mlirRegionInsertOwnedBlock(self.ptr(), @intCast(index), block.ptr()); - } - - pub fn insertOwnedBlockBefore(self: *Region, reference: *const Block, block: *Block) void { - c.mlirRegionInsertOwnedBlockBefore(self.ptr(), reference.ptr(), block.ptr()); - } - - pub fn insertOwnedBlockAfter(self: *Region, reference: *const Block, block: *Block) void { - c.mlirRegionInsertOwnedBlockAfter(self.ptr(), reference.ptr(), block.ptr()); - } - - pub fn firstBlock(self: *const Region) ?*Block { - return @ptrCast(c.mlirRegionGetFirstBlock(self.ptr()).ptr); - } - - pub fn takeBody(self: *Region, from: *Region) void { - c.mlirRegionTakeBody(self.ptr(), from.ptr()); - } - - pub fn nextInOperation(self: *const Region) *Region { - return @ptrCast(c.mlirRegionGetNextInOperation(self.ptr()).ptr); - } -}; - -pub const ShapedType = opaque { - const M = Methods(ShapedType, c.MlirType); - - pub const MAX_RANK = 16; - - pub const isAFn = c.mlirTypeIsAShaped; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirTypeEqual); - pub const format = M.format(c.mlirTypePrint); - pub const isA = M.isA; - - pub fn elementType(self: *const ShapedType) *const Type { - return @ptrCast(c.mlirShapedTypeGetElementType(self.ptr()).ptr); - } - - pub fn rank(self: *const ShapedType) usize { - return @intCast(c.mlirShapedTypeGetRank(self.ptr())); - } - - pub fn dimension(self: *const ShapedType, dim: usize) i64 { - return c.mlirShapedTypeGetDimSize(self.ptr(), @intCast(dim)); - } -}; - -pub const RankedTensorType = opaque { - const M = Methods(RankedTensorType, c.MlirType); - - pub const isAFn = c.mlirTypeIsARankedTensor; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirTypeEqual); - pub const format = M.format(c.mlirTypePrint); - - pub fn get(dimensions: []const i64, elemType: *const Type) *const RankedTensorType { - return @ptrCast(c.mlirRankedTensorTypeGet( - @intCast(dimensions.len), - @ptrCast(dimensions), - elemType.ptr(), - c.mlirAttributeGetNull(), - ).ptr); - } - - pub fn fromShaped(other: *const ShapedType) *const RankedTensorType { - var dims: stdx.BoundedArray(i64, ShapedType.MAX_RANK) = .{}; - for (0..other.rank()) |i| { - dims.appendAssumeCapacity(other.dimension(i)); - } - return get( - dims.constSlice(), - other.elementType(), - ); - } - - pub fn elementType(self: *const RankedTensorType) *const Type { - return @ptrCast(c.mlirShapedTypeGetElementType(self.ptr()).ptr); - } - - pub fn rank(self: *const RankedTensorType) usize { - return @intCast(c.mlirShapedTypeGetRank(self.ptr())); - } - - pub fn dimension(self: *const RankedTensorType, dim: usize) i64 { - return c.mlirShapedTypeGetDimSize(self.ptr(), @intCast(dim)); - } - - pub fn shaped(self: *const RankedTensorType) *const ShapedType { - return @ptrCast(self); - } -}; - -pub fn rankedTensorType(dimensions: []const i64, elem_type: *const Type) *const Type { - return @ptrCast(RankedTensorType.get(dimensions, elem_type)); -} - -pub const DenseElementsAttribute = opaque { - const M = Methods(DenseElementsAttribute, c.MlirAttribute); - - pub const isAFn = c.mlirAttributeIsADenseElements; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn init(shaped_type: *const ShapedType, values: anytype) *const DenseElementsAttribute { - const bytes = std.mem.sliceAsBytes(values); - return @ptrCast(c.mlirDenseElementsAttrRawBufferGet( - shaped_type.ptr(), - @intCast(bytes.len), - @ptrCast(bytes), - ).ptr orelse unreachable); - } -}; - -pub fn denseElementsAttribute(shaped_type: *const ShapedType, values: anytype) *const Attribute { - return @ptrCast(DenseElementsAttribute.init(shaped_type, values)); -} - -pub const DenseArrayTypes = enum { - bool, - i8, - i16, - i32, - i64, - f32, - f64, - - fn ZigType(comptime ElementType: @This()) type { - return switch (ElementType) { - .bool => i32, - .i8 => i8, - .i16 => i16, - .i32 => i32, - .i64 => i64, - .f32 => f32, - .f64 => f64, - }; - } -}; - -pub fn DenseArrayAttribute(comptime ElementType: DenseArrayTypes) type { - const ElementTypeZig = ElementType.ZigType(); - - const _isAFn, const getFn, const getElementFn = switch (ElementType) { - .bool => .{ c.mlirAttributeIsADenseBoolArray, c.mlirDenseBoolArrayGet, c.mlirDenseBoolArrayGetElement }, - .i8 => .{ c.mlirAttributeIsADenseI8Array, c.mlirDenseI8ArrayGet, c.mlirDenseI8ArrayGetElement }, - .i16 => .{ c.mlirAttributeIsADenseI16Array, c.mlirDenseI16ArrayGet, c.mlirDenseI16ArrayGetElement }, - .i32 => .{ c.mlirAttributeIsADenseI32Array, c.mlirDenseI32ArrayGet, c.mlirDenseI32ArrayGetElement }, - .i64 => .{ c.mlirAttributeIsADenseI64Array, c.mlirDenseI64ArrayGet, c.mlirDenseI64ArrayGetElement }, - .f32 => .{ c.mlirAttributeIsADenseF32Array, c.mlirDenseF32ArrayGet, c.mlirDenseF32ArrayGetElement }, - .f64 => .{ c.mlirAttributeIsADenseF64Array, c.mlirDenseF64ArrayGet, c.mlirDenseF64ArrayGetElement }, - }; - - return struct { - const Self = @This(); - - const M = Methods(@This(), c.MlirAttribute); - - pub const isAFn = _isAFn; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn init(ctx: *Context, values: []const ElementTypeZig) *const Self { - return @ptrCast(getFn(ctx.ptr(), @intCast(values.len), @ptrCast(values)).ptr); - } - - pub fn element(self: *const Self, pos: usize) ElementTypeZig { - return getElementFn(self.ptr(), @intCast(pos)); - } - - pub fn numElements(self: *const Self) usize { - return @intCast(c.mlirDenseArrayGetNumElements(self.ptr())); - } - }; -} - -pub fn denseArrayAttribute(ctx: *Context, comptime ElementType: DenseArrayTypes, values: []const ElementType.ZigType()) *const Attribute { - return @ptrCast(DenseArrayAttribute(ElementType).init(ctx, @ptrCast(values))); -} - -pub const ArrayAttribute = opaque { - const M = Methods(ArrayAttribute, c.MlirAttribute); - - pub const isAFn = c.mlirAttributeIsAArray; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn init(ctx: *Context, attrs: []const *const Attribute) *const ArrayAttribute { - return @ptrCast(c.mlirArrayAttrGet(ctx.ptr(), @intCast(attrs.len), @ptrCast(attrs)).ptr); - } - - pub fn element(self: *const ArrayAttribute, index: usize) Attribute { - return @ptrCast(c.mlirArrayAttrGetElement(self.ptr(), @intCast(index)).ptr); - } - - pub fn numElements(self: *const ArrayAttribute) usize { - return @intCast(c.mlirArrayAttrGetNumElements(self.ptr())); - } -}; - -pub fn arrayAttribute(ctx: *Context, attrs: []const *const Attribute) *const Attribute { - return @ptrCast(ArrayAttribute.init(ctx, attrs)); -} - -pub const FunctionType = opaque { - const M = Methods(FunctionType, c.MlirType); - pub const isAFn = c.mlirTypeIsAFunction; - - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirTypeEqual); - pub const format = M.format(c.mlirTypePrint); - - pub fn init(ctx: *Context, args: []const *const Type, results: []const *const Type) *const FunctionType { - return @ptrCast(c.mlirFunctionTypeGet( - ctx.ptr(), - @intCast(args.len), - @ptrCast(args.ptr), - @intCast(results.len), - @ptrCast(results.ptr), - ).ptr); - } -}; - -pub fn functionType(ctx: *Context, args: []const *const Type, results: []const *const Type) *const Type { - return @ptrCast(FunctionType.init(ctx, args, results)); -} - -pub const FlatSymbolRefAttribute = opaque { - const M = Methods(FlatSymbolRefAttribute, c.MlirAttribute); - - pub const isAFn = c.mlirAttributeIsAFlatSymbolRef; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn init(ctx: *Context, symbol_: []const u8) *const FlatSymbolRefAttribute { - return @ptrCast(c.mlirFlatSymbolRefAttrGet(ctx.ptr(), stringRef(symbol_)).ptr); - } - - pub fn symbol(self: *const FlatSymbolRefAttribute) []const u8 { - return string(c.mlirFlatSymbolRefAttrGetValue(self.ptr())); - } -}; - -pub fn flatSymbolRefAttribute(ctx: *Context, symbol: []const u8) *const Attribute { - return @ptrCast(FlatSymbolRefAttribute.init(ctx, symbol)); -} - -pub const UnitAttribute = opaque { - const M = Methods(UnitAttribute, c.MlirAttribute); - - pub const isAFn = c.mlirAttributeIsAUnit; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirAttributeEqual); - pub const format = M.format(c.mlirAttributePrint); - - pub fn get(ctx: *Context) *const UnitAttribute { - return @ptrCast(c.mlirUnitAttrGet(ctx.ptr()).ptr); - } -}; - -pub fn unitAttribute(ctx: *Context) *const Attribute { - return @ptrCast(UnitAttribute.get(ctx)); -} - -pub const MemRefType = opaque { - const M = Methods(MemRefType, c.MlirType); - - pub const isAFn = c.mlirTypeIsAInteger; - pub const ptr = M.ptr; - pub const eql = M.eql(c.mlirTypeEqual); - pub const format = M.format(c.mlirTypePrint); - - pub fn init(element_type: *const Type, shape: []const i64, layout: ?*const Attribute, memory_space: ?*const Attribute) *const MemRefType { - return @ptrCast(c.mlirMemRefTypeGet( - element_type.ptr(), - @intCast(shape.len), - @ptrCast(shape), - if (layout) |l| l.ptr() else .{}, - if (memory_space) |m| m.ptr() else .{}, - ).ptr); - } - - pub fn fromShaped(other: *const ShapedType) *const MemRefType { - var dims: stdx.BoundedArray(i64, ShapedType.MAX_RANK) = .{}; - for (0..other.rank()) |i| { - dims.appendAssumeCapacity(other.dimension(i)); - } - return init( - other.elementType(), - dims.constSlice(), - null, - null, - ); - } - - pub fn shaped(self: *const MemRefType) *const ShapedType { - return @ptrCast(self); - } -}; - -pub fn memRefType(element_type: *const Type, shape: []const i64, layout: ?*const Attribute, memory_space: ?*const Attribute) *const Type { - return @ptrCast(MemRefType.init(element_type, shape, layout, memory_space)); -} From fc3cd785fb5b380d82da21353dcacb0274250765 Mon Sep 17 00:00:00 2001 From: Corentin Godeau Date: Thu, 28 Aug 2025 17:36:20 +0200 Subject: [PATCH 37/37] wip --- mlir/mlir.zig | 77 +++++++++++++++++++++++++++++++++++++++++++++++++- zml/mlirx.zig | 55 ++++++++++++++++-------------------- zml/module.zig | 70 +++++++++++++++++++++++---------------------- zml/tensor.zig | 2 +- 4 files changed, 139 insertions(+), 65 deletions(-) diff --git a/mlir/mlir.zig b/mlir/mlir.zig index 7536e701..7f13df69 100644 --- a/mlir/mlir.zig +++ b/mlir/mlir.zig @@ -39,6 +39,13 @@ pub fn stringCallback(str: c.MlirStringRef, ctx_: ?*anyopaque) callconv(.c) void }; } +fn stringFromStream(buf: []u8, streamFn: anytype, args: anytype) []const u8 { + var writer = std.Io.Writer.fixed(buf); + var sctx: StringCallbackCtx = .{ .writer = &writer }; + _ = @call(.auto, streamFn, args ++ .{ stringCallback, &sctx }); + return writer.buffered(); +} + pub fn registerPasses(comptime passes: []const u8) void { @field(c, "mlirRegister" ++ passes ++ "Passes")(); } @@ -104,6 +111,45 @@ pub const DialectRegistry = opaque { } }; +pub const PassManager = opaque { + const M = Methods(PassManager, c.MlirPassManager); + + pub const ptr = M.ptr; + + pub fn init(ctx: *Context) *PassManager { + return @ptrCast(c.mlirPassManagerCreate(ctx.ptr()).ptr); + } + + pub fn deinit(self: *const PassManager) void { + c.mlirPassManagerDestroy(self.ptr()); + } + + pub fn asOpPassManager(self: *PassManager) *OpPassManager { + return @ptrCast(c.mlirPassManagerGetAsOpPassManager(self.ptr()).ptr); + } +}; + +pub const OpPassManager = opaque { + const M = Methods(OpPassManager, c.MlirOpPassManager); + + pub const ptr = M.ptr; + + pub fn addPipeline(self: *OpPassManager, pipeline_name: []const u8) !void { + var buffer: [1024]u8 = undefined; + const result = stringFromStream(&buffer, c.mlirOpPassManagerAddPipeline, .{ self.ptr(), stringRef(pipeline_name) }); + if (result.len != 0) { + log.err("mlir error: {s}", .{result}); + return error.MlirUnexpected; + } + } +}; + +pub const Pass = opaque { + const M = Methods(Pass, c.MlirPass); + + pub const ptr = M.ptr; +}; + pub const Context = opaque { const M = Methods(Context, c.MlirContext); @@ -385,12 +431,37 @@ pub fn FloatType(comptime ft: FloatTypes) type { }; } -pub fn floatType(ft: FloatTypes, ctx: *Context) *const Type { +pub fn floatType(ctx: *Context, ft: FloatTypes) *const Type { return switch (ft) { inline else => |v| @ptrCast(FloatType(v).get(ctx)), }; } +pub const ComplexTypes = enum { + c64, + c128, +}; + +pub const ComplexType = opaque { + const M = Methods(ComplexType, c.MlirType); + + pub const isAFn = c.mlirTypeIsAComplex; + pub const ptr = M.ptr; + pub const eql = M.eql(c.mlirTypeEqual); + pub const format = M.format(c.mlirTypePrint); + + pub fn get(ctx: *Context, complex_type: ComplexTypes) *const ComplexType { + return @ptrCast(c.mlirComplexTypeGet(switch (complex_type) { + .c64 => floatType(ctx, .f32).ptr(), + .c128 => floatType(ctx, .f64).ptr(), + }).ptr); + } +}; + +pub fn complexType(ctx: *Context, complex_type: ComplexTypes) *const Type { + return @ptrCast(ComplexType.get(ctx, complex_type)); +} + pub const Attribute = opaque { const M = Methods(Attribute, c.MlirAttribute); @@ -927,6 +998,10 @@ pub const Operation = opaque { return @ptrCast(c.mlirOperationGetContext(self.ptr()).ptr); } + pub fn setAttributeByName(self: *Operation, name_: []const u8, attribute: *const Attribute) void { + c.mlirOperationSetAttributeByName(self.ptr(), stringRef(name_), attribute.ptr()); + } + pub const PrintFlags = struct { elide_large_elements_attrs: ?usize = null, debug_info: bool = false, diff --git a/zml/mlirx.zig b/zml/mlirx.zig index 28f40383..46c85fa4 100644 --- a/zml/mlirx.zig +++ b/zml/mlirx.zig @@ -7,11 +7,6 @@ const Shape = @import("shape.zig").Shape; const mlirx = @This(); -/// Returns the mlir.Type corresponding to a given zml.Shape. -pub fn tensorType(ctx: mlir.Context, sh: Shape) mlir.Type { - return .tensor(sh.dims(), mlirx.Type.fromDType(ctx, sh.dtype())); -} - pub fn denseElementAttrType(dt: dtype.DataType) ?mlir.DenseElementsAttributeTypes { return switch (dt) { .bool => .bool, @@ -32,34 +27,34 @@ pub fn denseElementAttrType(dt: dtype.DataType) ?mlir.DenseElementsAttributeType } pub const Type = struct { - pub fn fromDType(ctx: mlir.Context, dt: dtype.DataType) mlir.Type { + pub fn fromDType(ctx: *mlir.Context, dt: dtype.DataType) *const mlir.Type { return switch (dt) { - .bool => .int(ctx, .i1), - .f8e4m3b11fnuz => .float(ctx, .f8e4m3b11fnuz), - .f8e4m3fn => .float(ctx, .f8e4m3fn), - .f8e4m3fnuz => .float(ctx, .f8e4m3fnuz), - .f8e5m2 => .float(ctx, .f8e5m2), - .f8e5m2fnuz => .float(ctx, .f8e5m2fnuz), - .bf16 => .float(ctx, .bf16), - .f16 => .float(ctx, .f16), - .f32 => .float(ctx, .f32), - .f64 => .float(ctx, .f64), - .i4 => .int(ctx, .i4), - .i8 => .int(ctx, .i8), - .i16 => .int(ctx, .i16), - .i32 => .int(ctx, .i32), - .i64 => .int(ctx, .i64), - .u4 => .int(ctx, .u4), - .u8 => .int(ctx, .u8), - .u16 => .int(ctx, .u16), - .u32 => .int(ctx, .u32), - .u64 => .int(ctx, .u64), - .c64 => .complex(ctx, .c64), - .c128 => .complex(ctx, .c128), + .bool => mlir.integerType(ctx, .i1), + .f8e4m3b11fnuz => mlir.floatType(ctx, .f8e4m3b11fnuz), + .f8e4m3fn => mlir.floatType(ctx, .f8e4m3fn), + .f8e4m3fnuz => mlir.floatType(ctx, .f8e4m3fnuz), + .f8e5m2 => mlir.floatType(ctx, .f8e5m2), + .f8e5m2fnuz => mlir.floatType(ctx, .f8e5m2fnuz), + .bf16 => mlir.floatType(ctx, .bf16), + .f16 => mlir.floatType(ctx, .f16), + .f32 => mlir.floatType(ctx, .f32), + .f64 => mlir.floatType(ctx, .f64), + .i4 => mlir.integerType(ctx, .i4), + .i8 => mlir.integerType(ctx, .i8), + .i16 => mlir.integerType(ctx, .i16), + .i32 => mlir.integerType(ctx, .i32), + .i64 => mlir.integerType(ctx, .i64), + .u4 => mlir.integerType(ctx, .u4), + .u8 => mlir.integerType(ctx, .u8), + .u16 => mlir.integerType(ctx, .u16), + .u32 => mlir.integerType(ctx, .u32), + .u64 => mlir.integerType(ctx, .u64), + .c64 => mlir.complexType(ctx, .c64), + .c128 => mlir.complexType(ctx, .c128), }; } - pub fn toDType(mlir_type: mlir.Type) dtype.DataType { + pub fn toDType(mlir_type: *const mlir.Type) dtype.DataType { const mapping = .{ .{ .bool, mlir.IntegerType(.i1) }, @@ -91,7 +86,7 @@ pub const Type = struct { inline for (mapping) |entry| { const dt, const mlirT = entry; - if (mlirT.is_a_fn(mlir_type._inner)) { + if (mlirT.isAFn(mlir_type._inner)) { return dt; } } diff --git a/zml/module.zig b/zml/module.zig index 4e40882e..9f3858d8 100644 --- a/zml/module.zig +++ b/zml/module.zig @@ -29,10 +29,10 @@ pub const MlirFn = struct { name: []const u8, args_shapes: []Shape, res_tensors: *const anyopaque, - res_types: []mlir.Type, + res_types: []*const mlir.Type, res_shapes: []Shape, res_donations: []Tensor._Donation, - mlir_fn: mlir.Operation, + mlir_fn: *mlir.Operation, pub const Kind = enum { main, @@ -40,16 +40,18 @@ pub const MlirFn = struct { }; }; +const RecursiveOpts = enum { open, hermetic }; + pub const CompilationContext = struct { _platform: Platform, _name: []const u8, _arena: std.heap.ArenaAllocator, - _mlir_ctx: mlir.Context, - _mlir_registry: mlir.Registry, - _mlir_canonicalizer: mlir.PassManager, + _mlir_ctx: *mlir.Context, + _mlir_registry: *mlir.DialectRegistry, + _mlir_canonicalizer: *mlir.PassManager, - _module: mlir.Module, + _module: *mlir.Module, _blocks: stdx.BoundedArray(TaggedBlock, 64) = .{}, _fn_cache: FnCache = .{}, @@ -61,16 +63,16 @@ pub const CompilationContext = struct { _previous: ?*CompilationContext = null, threadlocal var _current: ?*CompilationContext = null; - const TaggedBlock = struct { mlir.Block, mlir.Block.RecursiveOpts }; - const TensorToBlockArg = std.AutoHashMapUnmanaged(Tensor._Id, struct { mlir.Value, Tensor._Donation }); + const TaggedBlock = struct { *mlir.Block, RecursiveOpts }; + const TensorToBlockArg = std.AutoHashMapUnmanaged(Tensor._Id, struct { *const mlir.Value, Tensor._Donation }); const AttributeList = stdx.BoundedArray(mlir.NamedAttribute, 3); pub fn init(allocator_: std.mem.Allocator, full_name: []const u8, platform: Platform) !CompilationContext { - const mlir_registry = mlir.Registry.init() catch unreachable; + const mlir_registry = mlir.DialectRegistry.init() catch unreachable; inline for (.{ "func", "stablehlo" }) |d| { mlir.DialectHandle.fromString(d).insertDialect(mlir_registry); } - var mlir_ctx = mlir.Context.initWithRegistry(mlir_registry, false) catch unreachable; + var mlir_ctx = mlir.Context.init(.{ .registry = mlir_registry, .threading = false }) catch unreachable; mlir_ctx.loadAllAvailableDialects(); // Too long module names create too long file paths and files failed to create. @@ -79,11 +81,11 @@ pub const CompilationContext = struct { const max_name_len = @divFloor(std.fs.max_path_bytes, 2) - 17; const name = full_name[0..@min(max_name_len, full_name.len)]; - const loc = mlir_ctx.location(@src()).named(mlir_ctx, "main"); + const loc = mlir.Location.fromSrc(mlir_ctx, @src()).named(mlir_ctx, "main"); const module = mlir.Module.init(loc); - module.op().setAttributeByName("sym_name", .string(mlir_ctx, "zml")); + module.operation().setAttributeByName("sym_name", mlir.stringAttribute(mlir_ctx, "zml")); - var canonicalizer = try mlir.PassManager.init(mlir_ctx); + var canonicalizer = mlir.PassManager.init(mlir_ctx); { var opm = canonicalizer.asOpPassManager(); try opm.addPipeline("canonicalize"); @@ -138,7 +140,7 @@ pub const CompilationContext = struct { return self._platform.target; } - pub fn mlirCtx(self: *const CompilationContext) mlir.Context { + pub fn mlirCtx(self: *const CompilationContext) *mlir.Context { return self._mlir_ctx; } @@ -164,12 +166,12 @@ pub const CompilationContext = struct { // Run in a dedicated thread because compilation relies on `threadlocal`. const f = try asynk.callBlocking(CompilationContext.emitMlir, .{ self, func, &tensor_args, CompilationContext.EmitMlirOpts{ .name = "main", .kind = .main } }); const module = self._module; - module.getBody().appendOperation(f.mlir_fn); + _ = f.mlir_fn.appendTo(module.body()); const sharding = self._platform.sharding(); const mlir_ctx = self._mlir_ctx; - module.op().setAttributeByName("mhlo.num_replicas", .int(mlir_ctx, .i32, sharding.num_replicas)); - module.op().setAttributeByName("mhlo.num_partitions", .int(mlir_ctx, .i32, sharding.num_partitions)); + module.operation().setAttributeByName("mhlo.num_replicas", mlir.integerAttribute(mlir_ctx, .i32, sharding.num_replicas)); + module.operation().setAttributeByName("mhlo.num_partitions", mlir.integerAttribute(mlir_ctx, .i32, sharding.num_partitions)); const module_hash = computeModuleHash(self._platform, module); var module_dir: ?[]const u8 = null; @@ -244,8 +246,8 @@ pub const CompilationContext = struct { return if (self._blocks.len > 0) self._blocks.get(self._blocks.len - 1) else null; } - pub fn openBlock(self: *CompilationContext, kind: mlir.Block.RecursiveOpts, args: []const mlir.Type, locs: []const mlir.Location) !TaggedBlock { - const block: TaggedBlock = .{ try mlir.Block.init(args, locs), kind }; + pub fn openBlock(self: *CompilationContext, kind: RecursiveOpts, args: []const *const mlir.Type, locs: []const *const mlir.Location) !TaggedBlock { + const block: TaggedBlock = .{ mlir.Block.init(args, locs), kind }; self.pushBlock(block); return block; } @@ -336,17 +338,19 @@ pub const CompilationContext = struct { const tensor_count = meta.count(Tensor, args); const mlir_ctx = self.mlirCtx(); - const loc = mlir_ctx.location(@src()); + const loc: *const mlir.Location = .fromSrc(mlir_ctx, @src()); - const locations = try arena.alloc(mlir.Location, tensor_count); - @memset(locations, mlir.Location.unknown(mlir_ctx)); + const locations = try arena.alloc(*const mlir.Location, tensor_count); + @memset(locations, .unknown(mlir_ctx)); var input_shapes: std.array_list.Managed(Shape) = try .initCapacity(res_allocator, tensor_count); meta.collect(Tensor.shape, {}, &input_shapes, args) catch unreachable; stdx.debug.internalAssert(input_shapes.items.len == tensor_count, "args have changed ?", .{}); - const input_types = try arena.alloc(mlir.Type, tensor_count); - for (input_types, input_shapes.items) |*t, sh| t.* = mlirx.tensorType(mlir_ctx, sh); + const input_types = try arena.alloc(*const mlir.Type, tensor_count); + for (input_types, input_shapes.items) |*t, sh| { + t.* = mlir.rankedTensorType(sh.dims(), mlirx.Type.fromDType(mlir_ctx, sh.dtype())); + } const og_block_args = self._block_args; defer { @@ -364,7 +368,7 @@ pub const CompilationContext = struct { // Those are returned to caller so we don't put them in the arena, but in the module allocator. const fn_res = try res_allocator.create(ReturnT); - const fn_res_types = try res_allocator.alloc(mlir.Type, out_tensor_count); + const fn_res_types = try res_allocator.alloc(*const mlir.Type, out_tensor_count); const fn_res_shapes = try res_allocator.alloc(Shape, out_tensor_count); const fn_res_donations = try res_allocator.alloc(Tensor._Donation, out_tensor_count); const fn_res_output_memory_kind = try res_allocator.alloc(Buffer.Memory, out_tensor_count); @@ -382,10 +386,10 @@ pub const CompilationContext = struct { break :forward @call(.auto, func, args.*); }; - var fn_res_values: [out_tensor_count]mlir.Value = undefined; + var fn_res_values: [out_tensor_count]*const mlir.Value = undefined; self.extractValuesAndTypes(fn_res, &fn_res_values, fn_res_types, fn_res_shapes, fn_res_donations, fn_res_output_memory_kind); - const fn_ret = dialect.func.return_(mlir_ctx, &fn_res_values, loc); + const fn_ret = dialect.func.returns(mlir_ctx, &fn_res_values, loc); fn_body[0].appendOperationRecursive(fn_ret, fn_body[1]); } @@ -690,10 +694,10 @@ pub const CompilationContext = struct { /// This is done so that we have a mapping between the arguments of the kernel associated with a module and the actual Tensors /// stored in the Module. /// Caller need to allocate required memory in self._block_args. - pub fn mapBlockArguments(self: *CompilationContext, v: anytype, block: mlir.Block, start: usize) usize { + pub fn mapBlockArguments(self: *CompilationContext, v: anytype, block: *mlir.Block, start: usize) usize { const LocalContext = struct { index: usize, - block: mlir.Block, + block: *mlir.Block, self: *CompilationContext, }; var context = LocalContext{ .self = self, .block = block, .index = start }; @@ -736,8 +740,8 @@ pub const CompilationContext = struct { pub fn extractValuesAndTypes( self: *const CompilationContext, v: anytype, - values: []mlir.Value, - types: []mlir.Type, + values: []*const mlir.Value, + types: []*const mlir.Type, shapes: []Shape, donations: []Tensor._Donation, output_memory_kind: []Buffer.Memory, @@ -746,8 +750,8 @@ pub const CompilationContext = struct { const LocalContext = struct { self: *const CompilationContext, index: usize = 0, - values: []mlir.Value, - types: []mlir.Type, + values: []*const mlir.Value, + types: []*const mlir.Type, shapes: []Shape, donations: []Tensor._Donation, output_memory_kind: []Buffer.Memory, diff --git a/zml/tensor.zig b/zml/tensor.zig index 26f670d7..2dbfe9a2 100644 --- a/zml/tensor.zig +++ b/zml/tensor.zig @@ -42,7 +42,7 @@ pub const Tensor = struct { _output_memory_kind: Memory = .device, pub const _Donation = union(enum) { no_buffer, input_buffer, arg: u16 }; - pub const _Id = union(enum) { mlir: mlir.Value, buffer_id: u64, arg_id: u64 }; + pub const _Id = union(enum) { mlir: *const mlir.Value, buffer_id: u64, arg_id: u64 }; pub const MAX_RANK = Shape.MAX_RANK; /// Returns the current compilation context.