diff --git a/BUILD.bazel b/BUILD.bazel index aa1656d4..f891cd2d 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -10,9 +10,13 @@ zls_completion( name = "completion", visibility = ["//visibility:public"], deps = [ - "//async", - "//examples/llama", - "//stdx", - "//zml", + # "//async", + # "//examples/llama", + # "//stdx", + # "//zml", + "//mlir:main", + # "//async", + # "//stdx", + # "//zml", ], ) diff --git a/mlir/BUILD.bazel b/mlir/BUILD.bazel index 87982a7e..e91a2c91 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", @@ -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", ], ) @@ -28,3 +27,15 @@ zig_test( name = "test", deps = [":mlir"], ) + +zig_binary( + name = "main", + main = "main.zig", + visibility = ["//visibility:public"], + deps = [ + ":mlir", + "//mlir/dialects", + # ":c", + # "//stdx", + ], +) diff --git a/mlir/context.zig b/mlir/context.zig new file mode 100644 index 00000000..e69de29b diff --git a/mlir/dialects/BUILD.bazel b/mlir/dialects/BUILD.bazel index 101f67ff..8b07057c 100644 --- a/mlir/dialects/BUILD.bazel +++ b/mlir/dialects/BUILD.bazel @@ -3,17 +3,17 @@ 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:mlir", "//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 47f74d8e..3649d443 100644 --- a/mlir/dialects/dialects.zig +++ b/mlir/dialects/dialects.zig @@ -1,12 +1,13 @@ 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 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"); test { std.testing.refAllDecls(@This()); } diff --git a/mlir/dialects/func.zig b/mlir/dialects/func.zig index 868ff3bc..c2af12a5 100644 --- a/mlir/dialects/func.zig +++ b/mlir/dialects/func.zig @@ -3,50 +3,91 @@ const std = @import("std"); const mlir = @import("mlir"); 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 { + pub const Visibility = enum { + public, + private, + }; - if (args.res_attrs.len > 0) { - attrs_tuple_buffer.appendAssumeCapacity(.{ "res_attrs", .array(ctx, args.res_attrs) }); + name: []const u8, + args: ?[]const *const mlir.Type = null, + args_attributes: ?[]const *const mlir.Attribute = null, + results: ?[]const *const mlir.Type = null, + results_attributes: ?[]const *const mlir.Attribute = null, + block: *mlir.Block, + location: *const mlir.Location, + no_inline: bool = false, + visibility: Visibility = .public, +}; + +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) = .{}; + 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( + 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(); + }, + ))), + }); + + if (args.args_attributes) |args_attributes| { + attr_tuples_buffer.appendAssumeCapacity(.named(ctx, "arg_attrs", mlir.arrayAttribute(ctx, args_attributes))); + } + if (args.results_attributes) |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: [: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, location: *const mlir.Location) *mlir.Operation { return mlir.Operation.make(ctx, "func.call", .{ - .variadic_operands = &.{values}, - .results = results, - .verify = true, - .attributes = &.{.{ "callee", .symbol(ctx, name) }}, - .location = loc, + .operands = .{ .variadic = &.{values} }, + .results = .{ .flat = results }, + .attributes = &.{ + .named(ctx, "callee", mlir.flatSymbolRefAttribute(ctx, name)), + }, + .location = location, }); } -pub fn return_(ctx: mlir.Context, values: []const mlir.Value, loc: 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 = values, + .operands = .{ .flat = &.{values} }, + .location = location, .verify = false, - .location = loc, + }); +} + +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..0596357a 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:mlir", "//stdx", "@stablehlo//:stablehlo_dialect_capi", ], diff --git a/mlir/dialects/stablehlo/stablehlo.zig b/mlir/dialects/stablehlo/stablehlo.zig index 0c60fd01..8789c7b0 100644 --- a/mlir/dialects/stablehlo/stablehlo.zig +++ b/mlir/dialects/stablehlo/stablehlo.zig @@ -2,112 +2,106 @@ const std = @import("std"); const c = @import("c"); const mlir = @import("mlir"); +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 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 = 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; +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; -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 const bitcast_convert = castOp("stablehlo.bitcast_convert").call; +pub const convert = castOp("stablehlo.convert").call; +pub const reshape = castOp("stablehlo.reshape").call; - 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, - }); +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, + }); + } + }; } -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, - }); +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 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 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: mlir.Value, lower: bool, location: mlir.Location) mlir.Operation { +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 = &.{value}, + .operands = .{ .flat = &.{value} }, .result_type_inference = true, .attributes = &.{ - .{ "lower", .i1FromBool(ctx, lower) }, + .named(ctx, "lower", mlir.integerAttribute(ctx, .i1, @intFromBool(lower))), }, .location = location, }); } -pub fn clamp(ctx: mlir.Context, min: mlir.Value, value: mlir.Value, max: mlir.Value, location: mlir.Location) mlir.Operation { +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 = &.{ min, value, max }, + .operands = .{ .flat = &.{ min, value, max } }, .result_type_inference = true, .location = location, }); @@ -119,18 +113,17 @@ pub const DotPrecision = union(enum) { highest, algorithm: DotAlgorithm, - pub fn precisionAttr(self: DotPrecision, ctx: mlir.Context) mlir.Attribute { - const precision = PrecisionAttribute.init(ctx, switch (self) { + 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, - }); - return precision.asAttr(); + }; } - pub fn algorithmAttr(self: DotPrecision, ctx: mlir.Context, operand_type: mlir.RankedTensorType) ?mlir.Attribute { + 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, @@ -149,159 +142,172 @@ pub const DotAlgorithm = struct { // 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, - )); - } }; +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); +} + /// 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, + ctx: *mlir.Context, + lhs: *const mlir.Value, + rhs: *const mlir.Value, + result_type: *const mlir.Type, opts: struct { lhs_batching_dimensions: []const i64, rhs_batching_dimensions: []const i64, lhs_contracting_dimensions: []const i64, rhs_contracting_dimensions: []const i64, - precision: DotPrecision, + dot_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, .{ + location: *const mlir.Location, +) *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, - }).asAttr(), - }, - .{ "precision_config", .array(ctx, &precisions) }, + }), + ), + .named(ctx, "precision_config", mlir.arrayAttribute(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 }, + .named(ctx, "algorithm", switch (opts.dot_precision) { + .algorithm => |v| dotAlgorithmAttribute(ctx, v, lhs.type_()), + else => undefined, + }), }; - const n_attributes = if (opts.precision == .algorithm) attributes.len else attributes.len - 1; + const n_attributes = if (opts.dot_precision == .algorithm) attributes.len else attributes.len - 1; return mlir.Operation.make(ctx, "stablehlo.dot_general", .{ - .operands = &.{ lhs, rhs }, - .results = &.{result_type}, + .operands = .{ .flat = &.{ lhs, rhs } }, + .results = .{ .flat = &.{result_type} }, .attributes = attributes[0..n_attributes], .location = location, }); } pub fn constant( - ctx: mlir.Context, + ctx: *mlir.Context, dims: []const i64, - elem_type: mlir.DenseElementsAttributeTypes, + elem_type: *const mlir.Type, raw_bytes: []const u8, - location: mlir.Location, -) mlir.Operation { + location: *const 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) }}, + .operands = .{ .flat = &.{} }, + .results = .{ .flat = &.{mlir.rankedTensorType(dims, elem_type)} }, + .attributes = &.{ + .named(ctx, "value", mlir.denseElementsAttribute(mlir.RankedTensorType.init(dims, elem_type).shaped(), 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 { +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 = &.{operand}, - .results = &.{result_type}, + .operands = .{ .flat = &.{operand} }, + .results = .{ .flat = &.{result_type} }, .attributes = &.{ - .{ "broadcast_dimensions", .dense(ctx, .i64, dims) }, + .named(ctx, "broadcast_dimensions", mlir.denseArrayAttribute(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 { +pub fn transpose( + ctx: *mlir.Context, + value: *const mlir.Value, + result_type: *const mlir.Type, + opts: struct { permutation: []const i64 }, + location: *const mlir.Location, +) *mlir.Operation { return mlir.Operation.make(ctx, "stablehlo.transpose", .{ - .operands = &.{value}, - .results = &.{result_type}, + .operands = .{ .flat = &.{value} }, + .results = .{ .flat = &.{result_type} }, .attributes = &.{ - .{ "permutation", .dense(ctx, .i64, opts.permutation) }, + .named(ctx, "permutation", mlir.denseArrayAttribute(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 { +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 = &.{operand}, - .results = &.{result_type}, + .operands = .{ .flat = &.{operand} }, + .results = .{ .flat = &.{result_type} }, .attributes = &.{ - .{ "start_indices", .dense(ctx, .i64, start_indices) }, - .{ "limit_indices", .dense(ctx, .i64, limit_indices) }, - .{ "strides", .dense(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, }); } -pub fn concatenate(ctx: mlir.Context, inputs: []const mlir.Value, dimension: i64, location: mlir.Location) mlir.Operation { +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, + .operands = .{ .flat = inputs }, .result_type_inference = true, .attributes = &.{ - .{ "dimension", .int(ctx, .i64, dimension) }, + .named(ctx, "dimension", mlir.integerAttribute(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 { +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 = &.{ condition, then, else_ }, - .results = &.{then.getType()}, + .operands = .{ .flat = &.{ condition, then, else_ } }, + .results = .{ .flat = &.{then.type_()} }, .location = location, }); } pub fn gather( - ctx: mlir.Context, - value: mlir.Value, - indices: mlir.Value, + ctx: *mlir.Context, + value: *const mlir.Value, + indices: *const mlir.Value, slice_sizes: []const i64, - location: mlir.Location, args: struct { offset_dims: []const i64, collapsed_slice_dims: []const i64, @@ -311,15 +317,16 @@ pub fn gather( index_vector_dim: i64, indices_are_sorted: bool = false, }, -) mlir.Operation { + location: *const mlir.Location, +) *mlir.Operation { return mlir.Operation.make( ctx, "stablehlo.gather", .{ - .operands = &.{ value, indices }, + .operands = .{ .flat = &.{ value, indices } }, .result_type_inference = true, .attributes = &.{ - .{ "dimension_numbers", GatherDimensionNumbersAttribute.init( + .named(ctx, "dimension_numbers", gatherDimensionNumbersAttribute( ctx, args.offset_dims, args.collapsed_slice_dims, @@ -327,20 +334,20 @@ pub fn gather( 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) }, + )), + .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: mlir.Type) mlir.Type { - return if (typ.as(mlir.ShapedType)) |shaped| { - return shaped.elementType(); - } else typ; -} +// 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, @@ -352,9 +359,9 @@ pub const ScatterArgs = struct { 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, + 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), @@ -366,29 +373,29 @@ pub const ScatterArgs = struct { @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 mlir.Value, - scatter_indices: []const mlir.Value, - updates: []const mlir.Value, - update_block: mlir.Block, + 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: mlir.Location, -) mlir.Operation { + location: *const mlir.Location, +) *mlir.Operation { return mlir.Operation.make( ctx, "stablehlo.scatter", .{ - .variadic_operands = &.{ inputs, scatter_indices, updates }, + .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) }, + .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, @@ -396,163 +403,159 @@ pub fn scatter( ); } -pub fn iota(ctx: mlir.Context, dimension: i64, result_type: mlir.Type, location: mlir.Location) mlir.Operation { +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 = &.{}, - .results = &.{result_type}, + .operands = .{ .flat = &.{} }, + .results = .{ .flat = &.{result_type} }, .attributes = &.{ - .{ "iota_dimension", .int(ctx, .i64, dimension) }, + .named(ctx, "iota_dimension", mlir.integerAttribute(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(); +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 = &.{operand}, - .results = &.{result_type}, + .operands = .{ .flat = &.{operand} }, + .results = .{ .flat = &.{operand.type_()} }, .attributes = &.{ - .{ "dimensions", .dense(ctx, .i64, dimensions) }, + .named(ctx, "dimensions", mlir.denseArrayAttribute(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 { +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 = &.{ lhs, rhs }, + .operands = .{ .flat = &.{ lhs, rhs } }, .result_type_inference = true, .attributes = &.{ - .{ "comparison_direction", comparison_direction.asAttr() }, - .{ "compare_type", compare_type.asAttr() }, + .named(ctx, "comparison_direction", comparisonDirection(ctx, comparison_direction)), + .named(ctx, "compare_type", compareType(ctx, compare_type)), }, .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; +// 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]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(); +// 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]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]); - } +// 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", .{ - .variadic_operands = &.{ inputs, init_values }, - .result_type_inference = true, - .block = block, - .attributes = &.{ - .{ "dimensions", .dense(ctx, .i64, dimensions) }, - }, - .location = location, - }); -} +// 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 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; +// 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]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; +// 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]mlir.Value = undefined; - for (0..inputs.len * 2) |i| { - block_inputs[i] = block.argument(i); - } - _ = compfn(blkctx, ctx, block_inputs[0 .. inputs.len * 2]); +// 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", .{ - .variadic_operands = &.{inputs}, - .result_type_inference = true, - .block = block, - .attributes = &.{ - .{ "dimension", .int(ctx, .i64, dimension) }, - .{ "is_stable", .boolean(ctx, is_stable) }, - }, - .location = location, - }); -} +// 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: mlir.Value, new_dims: []const i64, start_indices: []const mlir.Value, location: mlir.Location) mlir.Operation { +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", .{ - .variadic_operands = &.{ &.{operand}, start_indices }, + .operands = .{ .variadic = &.{ &.{operand}, start_indices } }, .result_type_inference = true, .attributes = &.{ - .{ "slice_sizes", .dense(ctx, .i64, new_dims) }, + .named(ctx, "slice_sizes", mlir.denseArrayAttribute(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 { +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 = &.{ value, padding_value }, + .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) }, + .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, }); @@ -562,18 +565,18 @@ pub const TriangularSolveOpts = struct { left_side: bool, lower: bool, unit_diagonal: bool, - transpose_a: Transpose.Type, + transpose_a: TransposeAttribute.Type, }; -pub fn triangular_solve(ctx: mlir.Context, value: mlir.Value, other: mlir.Value, location: 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 = &.{ value, other }, + .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() }, + .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, }); @@ -584,74 +587,132 @@ pub const FftOpts = struct { length: []const i64, }; -pub fn fft(ctx: mlir.Context, value: mlir.Value, location: 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 = &.{value}, + .operands = .{ .flat = &.{value} }, .result_type_inference = true, .attributes = &.{ - .{ "fft_type", FftType.init(ctx, opts.kind).asAttr() }, - .{ "fft_length", .dense(ctx, .i64, opts.length) }, + .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: mlir.Value, b: mlir.Value, shape: mlir.Value, rng_distribution: RngDistribution.Type, location: mlir.Location) mlir.Operation { +pub const RngDistribution = opaque { + const M = mlir.Methods(RngDistribution, c.MlirAttribute); + + 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 = &.{ a, b, shape }, + .operands = .{ .flat = &.{ a, b, shape } }, .result_type_inference = true, .attributes = &.{ - .{ "rng_distribution", RngDistribution.init(ctx, rng_distribution).asAttr() }, + .named(ctx, "rng_distribution", rngDistribution(ctx, rng_distribution)), }, .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 { +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 = &.{initial_state}, + .operands = .{ .flat = &.{initial_state} }, .results = &.{ res_state_type, res_type }, .attributes = &.{ - .{ "rng_algorithm", RngAlgorithm.init(ctx, rng_algorithm).asAttr() }, + .named(ctx, "rng_algorithm", rngAlgorithm(ctx, rng_algorithm)), }, .location = location, }); } -pub fn reduce_precision(ctx: mlir.Context, value: mlir.Value, exponent_bits: i32, mantissa_bits: i32, location: mlir.Location) mlir.Operation { +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 = &.{value}, + .operands = .{ .flat = &.{value} }, .result_type_inference = true, .attributes = &.{ - .{ "exponent_bits", .int(ctx, .i32, exponent_bits) }, - .{ "mantissa_bits", .int(ctx, .i32, mantissa_bits) }, + .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: mlir.Value, update: mlir.Value, start_indices: []const mlir.Value, location: mlir.Location) mlir.Operation { +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", .{ - .variadic_operands = &.{ &.{operand}, &.{update}, start_indices }, + .operands = .{ .variadic = &.{ &.{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 { +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 = &.{tuple_value}, + .operands = .{ .flat = &.{tuple_value} }, .result_type_inference = true, .attributes = &.{ - .{ "index", .int(ctx, .i32, index) }, + .named(ctx, "index", mlir.integerAttribute(ctx, .i32, index)), }, .location = location, }); @@ -679,46 +740,44 @@ pub const ConvolutionOpts = struct { }; pub fn convolution( - ctx: mlir.Context, - lhs: mlir.Value, - rhs: mlir.Value, + ctx: *mlir.Context, + lhs: *const mlir.Value, + rhs: *const mlir.Value, + res_type: *const mlir.Type, opts: ConvolutionOpts, - res_type: mlir.Type, - location: mlir.Location, -) mlir.Operation { - var max_precisions: [2]mlir.Attribute = undefined; + 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.init(ctx, p).asAttr(); + 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 = &.{ lhs, rhs }, - .results = &.{res_type}, + .operands = .{ .flat = &.{ lhs, rhs } }, + .results = .{ .flat = &.{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) }, + .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, }); @@ -732,261 +791,339 @@ pub const CustomCallOpts = struct { typed_ffi = 4, }; - call_target_name: [:0]const u8, - has_side_effect: bool, - backend_config: ?mlir.Attribute, + pub const BackendConfig = union(ApiVersion) { + 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 = null, + api_version: ?ApiVersion = null, + backend_config: ?BackendConfig = null, 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, + output_operand_aliases: ?[]const i64 = null, + additional_attributes: []const mlir.NamedAttribute = &.{}, }; -pub fn custom_call(ctx: mlir.Context, inputs: []const mlir.Value, opts: CustomCallOpts, res_types: []const mlir.Type, location: 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; 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); + 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)), + }); + attrs.appendSliceAssumeCapacity(opts.additional_attributes); + + 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| { + 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(ctx, .{ .operand_index = alias }), + ); + } + attrs.appendAssumeCapacity( + .named(ctx, "output_operand_aliases", mlir.arrayAttribute(ctx, buffer.constSlice())), + ); + } + + var layouts_buffer: stdx.BoundedArray(*const mlir.Attribute, MAX_OPERANDS) = .{}; 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)); + layouts_buffer.appendAssumeCapacity( + mlir.denseElementsAttribute( + mlir.RankedTensorType.get( + &.{@intCast(ol.len)}, + mlir.indexType(ctx), + ).shaped(), + 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()) }); + 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())), + ); + layouts_buffer.clear(); 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)); + layouts_buffer.appendAssumeCapacity( + mlir.denseElementsAttribute( + mlir.RankedTensorType.get( + &.{@intCast(rl.len)}, + mlir.indexType(ctx), + ).shaped(), + 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()) }); + 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.appendSlice(opts.additional_attributes) catch @panic("Too many additional_attributes"); + attrs.appendAssumeCapacity( + .named(ctx, "result_layouts", mlir.arrayAttribute(ctx, layouts_buffer.constSlice())), + ); return mlir.Operation.make(ctx, "stablehlo.custom_call", .{ - .operands = inputs, - .results = res_types, + .operands = .{ .flat = inputs }, + .results = .{ .flat = result_types }, .attributes = attrs.constSlice(), .location = location, }); } -pub const DotDimensionNumbersAttribute = struct { - _inner: c.MlirAttribute, +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, + }, location); +} - 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 pin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + return custom_call(ctx, &.{}, &.{@ptrCast(mlir.MemRefType.fromShaped(value.type_().isA(mlir.ShapedType).?))}, .{ + .call_target_name = "Pin", + .api_version = .typed_ffi, + }, location); +} - pub fn init(ctx: mlir.Context, args: struct { +pub fn unpin(ctx: *mlir.Context, value: *const mlir.Value, location: *const mlir.Location) *mlir.Operation { + return custom_call(ctx, &.{}, &.{@ptrCast(mlir.RankedTensorType.fromShaped(value.type_().isA(mlir.ShapedType).?))}, .{ + .call_target_name = "Unpin", + .api_version = .typed_ffi, + }, location); +} + +pub const DotDimensionNumbersAttribute = opaque { + const M = mlir.Methods(DotDimensionNumbersAttribute, mlir.Attribute); + + 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 const InitArgs = 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 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 getLhsBatchingDimensionsSize(self: Self) usize { - return @intCast(c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsSize(self._inner)); + pub fn getLhsBatchingDimensionsSize(self: *const DotDimensionNumbersAttribute) usize { + return @intCast(c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsSize(self.ptr())); } - pub fn getLhsBatchingDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsElem(self._inner, @intCast(pos)); + pub fn getLhsBatchingDimensionsElem(self: *const DotDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloDotDimensionNumbersGetLhsBatchingDimensionsElem(self.ptr(), @intCast(pos)); } - pub fn getRhsBatchingDimensionsSize(self: Self) usize { - return @intCast(c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsSize(self._inner)); + pub fn getRhsBatchingDimensionsSize(self: *const DotDimensionNumbersAttribute) usize { + return @intCast(c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsSize(self.ptr())); } - pub fn getRhsBatchingDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsElem(self._inner, @intCast(pos)); + pub fn getRhsBatchingDimensionsElem(self: *const DotDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloDotDimensionNumbersGetRhsBatchingDimensionsElem(self.ptr(), @intCast(pos)); } - pub fn getLhsContractingDimensionsSize(self: Self) usize { - return @intCast(c.stablehloDotDimensionNumbersGetLhsContractingDimensionsSize(self._inner)); + pub fn getLhsContractingDimensionsSize(self: *const DotDimensionNumbersAttribute) usize { + return @intCast(c.stablehloDotDimensionNumbersGetLhsContractingDimensionsSize(self.ptr())); } - pub fn getLhsContractingDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloDotDimensionNumbersGetLhsContractingDimensionsElem(self._inner, @intCast(pos)); + pub fn getLhsContractingDimensionsElem(self: *const DotDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloDotDimensionNumbersGetLhsContractingDimensionsElem(self.ptr(), @intCast(pos)); } - pub fn getRhsContractingDimensionsSize(self: Self) usize { - return @intCast(c.stablehloDotDimensionNumbersGetRhsContractingDimensionsSize(self._inner)); + pub fn getRhsContractingDimensionsSize(self: *const DotDimensionNumbersAttribute) usize { + return @intCast(c.stablehloDotDimensionNumbersGetRhsContractingDimensionsSize(self.ptr())); } - pub fn getRhsContractingDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloDotDimensionNumbersGetRhsContractingDimensionsElem(self._inner, @intCast(pos)); + pub fn getRhsContractingDimensionsElem(self: *const DotDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloDotDimensionNumbersGetRhsContractingDimensionsElem(self.ptr(), @intCast(pos)); } }; -pub const GatherDimensionNumbersAttribute = struct { - _inner: c.MlirAttribute, +pub fn dotDimensionNumbersAttribute(ctx: *mlir.Context, args: DotDimensionNumbersAttribute.InitArgs) *const mlir.Attribute { + return @ptrCast(DotDimensionNumbersAttribute.init(ctx, args)); +} - 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 const GatherDimensionNumbersAttribute = opaque { + const M = mlir.Methods(GatherDimensionNumbersAttribute, mlir.Attribute); + + 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, + 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, - ), - }; + ) *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 const ConvDimensionNumbersAttribute = struct { - _inner: c.MlirAttribute, +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 is_a_fn = c.stablehloAttributeIsAConvDimensionNumbers; - const Self = ConvDimensionNumbersAttribute; - pub const asAttr = mlir.Attribute.fromAny(Self); - pub const eql = mlir.Attribute.eqlAny(Self); +pub const ConvDimensionNumbersAttribute = opaque { + const M = mlir.Methods(ConvDimensionNumbersAttribute, mlir.Attribute); - pub fn init(ctx: mlir.Context, args: struct { + 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 const InitArgs = struct { input_batch_dimension: i64, input_feature_dimension: i64, input_spatial_dimensions: []const i64, @@ -996,106 +1133,116 @@ pub const ConvDimensionNumbersAttribute = struct { 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 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 getInputBatchDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetInputBatchDimension(self._inner); + pub fn getInputBatchDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetInputBatchDimension(self.ptr()); } - pub fn getInputFeatureDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetInputFeatureDimension(self._inner); + pub fn getInputFeatureDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetInputFeatureDimension(self.ptr()); } - pub fn getInputSpatialDimensionsSize(self: Self) usize { - return @intCast(c.stablehloConvDimensionNumbersGetInputSpatialDimensionsSize(self._inner)); + pub fn getInputSpatialDimensionsSize(self: *const ConvDimensionNumbersAttribute) usize { + return @intCast(c.stablehloConvDimensionNumbersGetInputSpatialDimensionsSize(self.ptr())); } - pub fn getInputSpatialDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloConvDimensionNumbersGetInputSpatialDimensionsElem(self._inner, @intCast(pos)); + pub fn getInputSpatialDimensionsElem(self: *const ConvDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloConvDimensionNumbersGetInputSpatialDimensionsElem(self.ptr(), @intCast(pos)); } - pub fn getKernelInputFeatureDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetKernelInputFeatureDimension(self._inner); + pub fn getKernelInputFeatureDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetKernelInputFeatureDimension(self.ptr()); } - pub fn getKernelOutputFeatureDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetKernelOutputFeatureDimension(self._inner); + pub fn getKernelOutputFeatureDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetKernelOutputFeatureDimension(self.ptr()); } - pub fn getKernelSpatialDimensionsSize(self: Self) usize { - return @intCast(c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsSize(self._inner)); + pub fn getKernelSpatialDimensionsSize(self: *const ConvDimensionNumbersAttribute) usize { + return @intCast(c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsSize(self.ptr())); } - pub fn getKernelSpatialDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsElem(self._inner, @intCast(pos)); + pub fn getKernelSpatialDimensionsElem(self: *const ConvDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloConvDimensionNumbersGetKernelSpatialDimensionsElem(self.ptr(), @intCast(pos)); } - pub fn getOutputBatchDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetOutputBatchDimension(self._inner); + pub fn getOutputBatchDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetOutputBatchDimension(self.ptr()); } - pub fn getOutputFeatureDimension(self: Self) i64 { - return c.stablehloConvDimensionNumbersGetOutputFeatureDimension(self._inner); + pub fn getOutputFeatureDimension(self: *const ConvDimensionNumbersAttribute) i64 { + return c.stablehloConvDimensionNumbersGetOutputFeatureDimension(self.ptr()); } - pub fn getOutputSpatialDimensionsSize(self: Self) usize { - return @intCast(c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsSize(self._inner)); + pub fn getOutputSpatialDimensionsSize(self: *const ConvDimensionNumbersAttribute) usize { + return @intCast(c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsSize(self.ptr())); } - pub fn getOutputSpatialDimensionsElem(self: Self, pos: usize) i64 { - return c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsElem(self._inner, @intCast(pos)); + pub fn getOutputSpatialDimensionsElem(self: *const ConvDimensionNumbersAttribute, pos: usize) i64 { + return c.stablehloConvDimensionNumbersGetOutputSpatialDimensionsElem(self.ptr(), @intCast(pos)); } }; -pub const OutputOperandAliasAttribute = struct { - _inner: c.MlirAttribute, +pub fn convDimensionNumbersAttribute(ctx: *mlir.Context, args: ConvDimensionNumbersAttribute.InitArgs) *const mlir.Attribute { + return @ptrCast(ConvDimensionNumbersAttribute.init(ctx, args)); +} - pub const is_a_fn = c.stablehloAttributeIsAOutputOperandAlias; - pub const asAttr = mlir.Attribute.fromAny(OutputOperandAliasAttribute); - pub const eql = mlir.Attribute.eqlAny(OutputOperandAliasAttribute); +pub const OutputOperandAliasAttribute = opaque { + const M = mlir.Methods(OutputOperandAliasAttribute, c.MlirAttribute); - pub fn init( - ctx: mlir.Context, - output_tuple_indices: []const i64, + 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 const InitArgs = struct { + 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, - ) }; + operand_tuple_indices: []const i64 = &.{}, + }; + + pub fn get(ctx: *mlir.Context, args: InitArgs) *const 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 const PrecisionAttribute = struct { - _inner: c.MlirAttribute, +pub fn outputOperandAliasAttribute(ctx: *mlir.Context, args: OutputOperandAliasAttribute.InitArgs) *const mlir.Attribute { + return @ptrCast(OutputOperandAliasAttribute.get(ctx, args)); +} - 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 PrecisionAttribute = opaque { + const M = mlir.Methods(PrecisionAttribute, mlir.Attribute); + + 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, @@ -1103,23 +1250,27 @@ pub const PrecisionAttribute = struct { HIGHEST, }; - pub fn init(ctx: mlir.Context, value: Precision) Self { - return .{ ._inner = c.stablehloPrecisionAttrGet(ctx._inner, mlir.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)); + pub fn getValue(self: *const PrecisionAttribute) Precision { + const value = mlir.fromStringRef(c.stablehloPrecisionAttrGetValue(self.ptr())); return std.meta.stringToEnum(Precision, value) orelse unreachable; } }; -pub const ComparisonDirection = struct { - _inner: c.MlirAttribute, +pub fn precisionAttribute(ctx: *mlir.Context, precision: PrecisionAttribute.Precision) *const mlir.Attribute { + return @ptrCast(PrecisionAttribute.init(ctx, precision)); +} - 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 ComparisonDirection = opaque { + const M = mlir.Methods(ComparisonDirection, c.MlirAttribute); + + 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, @@ -1130,23 +1281,27 @@ pub const ComparisonDirection = struct { LT, }; - pub fn init(ctx: mlir.Context, value: Direction) Self { - return .{ ._inner = c.stablehloComparisonDirectionAttrGet(ctx._inner, mlir.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)); + 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 is_a_fn = 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 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, @@ -1155,23 +1310,27 @@ pub const CompareType = struct { TOTALORDER, }; - pub fn init(ctx: mlir.Context, value: Type) Self { - return .{ ._inner = c.stablehloComparisonTypeAttrGet(ctx._inner, mlir.stringRef(@tagName(value))) }; + pub fn init(ctx: *mlir.Context, value: Type) *const CompareType { + return @ptrCast(c.stablehloComparisonTypeAttrGet(ctx.ptr(), stringRef(@tagName(value))).ptr); } - pub fn getValue(self: Self) Type { - const value = mlir.fromStringRef(c.stablehloComparisonTypeAttrGetValue(self._inner)); + pub fn getValue(self: *const CompareType) Type { + const value = mlir.fromStringRef(c.stablehloComparisonTypeAttrGetValue(self.ptr())); return std.meta.stringToEnum(Type, value) orelse unreachable; } }; -pub const Transpose = struct { - _inner: c.MlirAttribute, +pub fn compareType(ctx: *mlir.Context, value: CompareType.Type) *const mlir.Attribute { + return @ptrCast(CompareType.init(ctx, value)); +} - 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 TransposeAttribute = opaque { + const M = mlir.Methods(TransposeAttribute, mlir.Attribute); + + 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, @@ -1179,23 +1338,27 @@ pub const Transpose = struct { ADJOINT, }; - pub fn init(ctx: mlir.Context, value: Type) Self { - return .{ ._inner = c.stablehloTransposeAttrGet(ctx._inner, mlir.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)); + pub fn getValue(self: *const TransposeAttribute) Type { + const value = mlir.fromStringRef(c.stablehloTransposeAttrGetValue(self.ptr())); return std.meta.stringToEnum(Type, value) orelse unreachable; } }; -pub const FftType = struct { - _inner: c.MlirAttribute, +pub fn transposeAttribute(ctx: *mlir.Context, value: TransposeAttribute.Type) *const mlir.Attribute { + return @ptrCast(TransposeAttribute.init(ctx, value)); +} - 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 FftType = opaque { + const M = mlir.Methods(FftType, mlir.Attribute); + + 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, @@ -1204,122 +1367,45 @@ pub const FftType = struct { IRFFT, }; - pub fn init(ctx: mlir.Context, value: Type) Self { - return .{ ._inner = c.stablehloFftTypeAttrGet(ctx._inner, mlir.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)); + pub fn getValue(self: *const FftType) Type { + const value = mlir.fromStringRef(c.stablehloFftTypeAttrGetValue(self.ptr())); 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]; - } - }; - - return state.call(requirement); +pub fn fftType(ctx: *mlir.Context, value: FftType.Type) *const mlir.Attribute { + return @ptrCast(FftType.init(ctx, value)); } -pub fn stablehloGetSmallerVersion(version1: []const u8, version2: []const u8) []const u8 { +fn stringFromStream(buf: []u8, streamFn: anytype, args: anytype) []const u8 { + var writer = std.Io.Writer.fixed(buf); + var sctx: mlir.StringCallbackCtx = .{ .writer = &writer }; + @call(.auto, streamFn, args ++ .{ mlir.stringCallback, &sctx }); + 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 +1413,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 +1428,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 new file mode 100644 index 00000000..8eee2bbe --- /dev/null +++ b/mlir/main.zig @@ -0,0 +1,109 @@ +const std = @import("std"); + +const dialects = @import("mlir/dialects"); +const mlir = @import("mlir"); + +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 (.{ "memref", "func", "stablehlo" }) |d| { + registry.registerDialect(d); + } + + // mlir.registerPasses("AllStablehlo"); + + 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 + + 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 + + // 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 module = mlir.Module.init(.unknown(ctx)); + defer module.deinit(); + + const tensor_i32 = mlir.rankedTensorType(&.{ 4096, 4096 }, mlir.integerType(ctx, .i32)); + + _ = dialects.func.func(ctx, .{ + .name = "pouet", + .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), + }).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 })}); + + // 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)); + // _ = 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/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 diff --git a/mlir/mlir.zig b/mlir/mlir.zig old mode 100755 new mode 100644 index a222c40a..7f13df69 --- 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,1130 @@ 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; + }; +} + +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")(); } -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 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); + + 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(ctx: *Context, ft: FloatTypes) *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 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); + + 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 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, + 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 +1153,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 +1162,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 +1170,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/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.