0
Fork 0
mirror of https://github.com/zml/zml.git synced 2026-11-16 19:11:09 +00:00
zml/mlir/dialects/rocdl
Raphael Nahoum 3f60e626e4
mlir/dialects: split into one package per dialect and unify them (#982)
Splits the monolithic mlir/dialects library into one Bazel package per
dialect, then makes every package follow the same layout, C API and Zig
API. Each package owns its Zig bindings, its C API when upstream doesn't
provide one, and its tests. Dialect registration moves out of the core
mlir package, so a target only pulls in the dialects it actually uses.
The conventions are documented in `mlir/dialects/README.md`, with a
porting checklist and conformance checks.

Along the way:

- cute_ir is flattened, and its Cuda, CuteNVGPU and NVVM sub-dialects
become their own packages
- fly's ROCDL extension moves to fly_rocdl
- new packages for ROCDL and UB
- kernels/ is renamed to dsl/, since it holds the DSLs
- every package registers through its own registerDialect, and each DSL
through registerDialects, instead of dialect name strings
- the C and C++ follow the LLVM and MLIR C API conventions, formatted
with a new mlir/.clang-format
- BUILD files share one template
- accessors are named as nouns like in the rest of ZML
- each package tests itself in its own test.zig, including that every
bound operation is registered and that every wrapper method compiles

Fixes:

- memref atomic_rmw emitted the wrong kind, because the AtomicRMWKind
values didn't match the pinned LLVM
- the pinned Triton and Mosaic no longer have tt.cat and tpu.repeat, so
the DSL's cat and repeat now lower the way tl.cat and pltpu.repeat do
- the ROCDL bindings are completed: buffer address spaces, vector types,
waves_per_eu range

Breaking changes:

- register dialects through their package instead of
DialectHandle.fromString, and mlir.registerFuncExtensions becomes
func.registerExtensions
- accessors drop the get prefix, so getValue() becomes value()
- ttir.cat and mosaic_tpu.repeat are removed, and stablehlo pad and
mosaic_tpu sort take their options before the location
- zml.kernel.fly.rocdl becomes fly_rocdl
- Mesh.sdyMeshAttr takes a context and returns an attribute instead of
text
- func.func no longer prints public, its default visibility

---------

Co-authored-by: Steeve Morin <steeve@zml.ai>
History 2026-10-26 11:41:05 +01:00
..
annotations.zig mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
attributes.zig mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
BUILD.bazel mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
capi.cc mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
capi.h mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
enums.zig mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
ops.zig mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
README.md mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
REGENERATING.md mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
rocdl.zig mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
target.zig mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
test.zig mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00
types.zig mlir/dialects: split into one package per dialect and unify them (#982) 2026-10-26 11:41:05 +01:00

ROCDL

Depend on //mlir/dialects/rocdl and import mlir/dialects/rocdl for LLVM's AMDGPU intrinsic dialect. The package links LLVM's ROCDLDialect and CAPIROCDL. fly_rocdl is a separate dialect and package.

The pinned LLVM revision supplies 374 operations, 15 attributes, and 12 enums.

  • ops provides every operation builder. Names replace dots with underscores, such as ops.raw_buffer_load and ops.mfma_f32_16x16x4f32. Arguments are operands, explicit result types, required attributes, an opts struct when there are optional attributes, and the location, preceded by the MLIR context. Attribute names match ODS. Optional and default-valued attributes are fields of opts with null defaults; pass .{} to omit them and let MLIR supply its defaults. Builders verify the resulting operation.
  • Enum types and attribute wrappers are available at the module root and in enums/attributes. Attribute constructors return error.InvalidMlir for invalid enum values or flag bits. Bit enums support combine and contains.
  • TargetAttr constructs and reads #rocdl.target, including optimization level, triple, chip, features, ABI, flags, and link files. Defaults match LLVM. Invalid fields and attributes from another context are rejected.
  • annotations supplies function, module, and memory annotations. Apply function annotations to llvm.func, and xnack/sramecc to LLVM-compatible modules. A missing module option preserves the target default; false explicitly disables the option.
  • types supplies every builtin vector type ROCDL's TableGen records use, named or inline, and LLVM pointers in ROCDL address spaces. ROCDL defines no independent MLIR types.
  • registerDialect registers ROCDL; loading it also loads its LLVM dependency. The existing cvtPkF32Fp8 convenience builder remains available.
const rocdl = @import("mlir/dialects/rocdl");

const policy = try rocdl.Gfx942CachePolicyAttr.get(ctx, .{ .value = .combine(&.{ .sc0, .nt }) });
const load = rocdl.ops.raw_buffer_load(
    ctx, resource, offset, scalar_offset,
    mlir.Type.float(ctx, .f32), .{ .aux = policy.attribute() }, location,
);

The :capi target exposes attribute constructors, predicates, and getters through capi.h; dialect registration comes from upstream mlir-c/Dialect/ROCDL.h. Declarations and implementations are grouped by introspection (a test helper), attributes, and target configuration. Getters require a handle accepted by the matching predicate. Contexts own all returned attributes and strings. There are no additional C operation wrappers: Zig builders use MLIR's standard operation API.

Tests compare the generated operation list with the loaded dialect's registered operation count, check every operation name, round-trip every named enum value, reject invalid enum/target inputs, and verify and reparse representative barrier, scheduling, buffer, and matrix operations in an LLVM function. The tests require no AMD GPU or ROCm runtime.