Skip to content

MLIR types are decided by parsing their printed form in 60 places, 51 of them in the typed AST backend #388

Open

Description

@hiraditya

Split out of the Phase 2 survey in #387, because the shape of it is worse than the
plan's finding 6 describes and the sequencing advice that follows from it is
different.

What is there

60 places decide something about an MLIR type by looking at its printed form.

Where Sites Backend
codegen/lower/expr.rs 22 AST
codegen/generator.rs 12 AST
codegen/lower/stmt.rs 12 AST
codegen/lower/mod.rs 5 AST
codegen/flat.rs 9 flat

What they test for, most common first: memref< (27), memref<memref< (6),
!llvm.ptr (6), vector< (4), tensor< (2), struct< (1).

Six named parsers exist to do this: slice_vec_len, is_slice_operand,
memref_elem, memref_lead_dims_and_elem, parse_llvm_struct_name,
vector_align_attr.

Why the AST path's version is the worse half

The flat emitter builds MLIR as text, so reading text back is at least consistent
with how it works. The AST path builds MLIR through melior's typed builder --
it has a real Type value -- and still round-trips through the printed form:

if ty_str.starts_with("memref<memref<") {
    let inner_ty_str = &ty_str[7..ty_str.len() - 1];
    let inner_ty = Type::parse(gen.context, inner_ty_str)...

A typed value is printed, matched by prefix, sliced at a hardcoded byte offset (7,
being "memref<".len()), and parsed back into a type. There are 127 Type::parse
calls in that path and eight of those hardcoded slices, in five files.

memref<memref< is a good illustration of the cost. It is a two-level memref, and
the way to ask is to inspect the element type. Asking by prefix means every place
that cares repeats the prefix, and is_scalar_memref at lower/expr.rs:155 has to
be written as "starts with memref<, does not contain x, and does not start with
memref<memref<" -- three string facts standing in for one structural question.

What to do, and when

A small MlirTy value type (scalar, memref, vector, llvm-ptr, llvm-struct) with a
Display, matched on instead of probed.

The sequencing is the part worth deciding deliberately. I had suggested scoping this
to the flat path's 9 sites on the grounds that Phase 4 retires the AST codegen, but
that is not the plan: the AST path stays until the flat path has survived a couple of
language evolutions, which makes its 51 sites live code that every new feature is
threaded through rather than code with a deletion date.

That argues for doing both halves, and for MlirTy living in src/mlir_ty.rs (added
in the Phase 2 commit, currently holding only the scalar spelling) so both backends
share one answer rather than two.

It does not argue for doing it before Phase 4. The AST path's sites are where the
work is, and Phase 4 rewrites how that path is reached. Worth revisiting once the
decline histogram (flat decline: lines, added in #387) shows what the flat path
still cannot carry -- that number is what decides how long "a couple of language
evolutions" actually is.

Not to be confused with

Expr::substitute mangling instantiated expression names at syntax/expr.rs:869
is a separate problem that finding 6 files under the same heading. The type-identity
round-trip it describes -- parse_enum_instance splitting "Option<i32>" back
apart -- is fixed for the flattener; that fix is in the Phase 2 commit on #387.

Activity

  1. transferred this issue fromon Sep 9, 2026
  2. added
    codegen
    refactor
    on Sep 12, 2026
  3. hiraditya commented on Dec 4, 2026

    @hiraditya
    CollaboratorAuthor

    This problem grew after August. On main 414bb29, src/codegen has 95 tests such as starts_with("memref<") on the text of an MLIR type. The issue counted 60 then, maybe with a different search. src/codegen also has 138 Type::parse calls, and the issue counted 127.

    #4565 added more of the same:

    • has_extents, a small parser that reads the rank of a memref from its text. Nine places used it.
    • A field load that found a vector field by the text vector<.
    • A fifth table for each register in the flat code generator, for one more kind of slot.

    PR #4589 removes these, and the older tests that they extended. After it, 79 of the 95 text tests remain. It does these things:

    • The AST code generator asks melior for the rank and the element of a memref (MemRefType, ShapedTypeLike), and uses Type::is_vector.
    • The flat code generator has one Slot enum in place of the five tables.
    • A table gives the type of a scalar or vector type ID. Before, the code hashed up to 242 types to find it.

    The remaining work, in smaller PRs, one or two files at a time:

    File Text tests
    codegen/lower/expr.rs 25
    codegen/lower/stmt.rs 18
    codegen/generator.rs 11
    codegen/lower/mod.rs 5
    codegen/flat/emit/aggregate.rs 4
    codegen/flat.rs 3
    other files 6

    In the flat code generator, 4 of the remaining tests read AggLayout::field_tys, which keeps each field type as text. The layout already has the typed FieldTy, so the fix there is to keep the FieldTy.

    Generated With Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    codegenrefactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions