Skip to content

codegen: the remaining places that decide an MLIR type from its printed text #4617

Backlog

Description

@hiraditya

This issue lists the remaining work on #388, with one item for each place in the code. In these places a code generator decides what an MLIR type is from its printed text, for example ty.to_string().starts_with("memref<"). Each item changes the place to ask the type itself.

Merged so far: #4589, #4604, #4606, #4609, #4610, #4611, #4613.

Line numbers are from f8bd285, the branch of #4613.

1. Open PRs

2. Small places in the AST code generator

Each of these places has the type value already. #4621 (open) changes all of them except lower/tensors.rs:119, which #4595 removed.

  • lower/expr.rs:511: the rank of a row's slot. It counts the x characters after memref<memref<.
  • lower/expr.rs:3600 and erase_memref_extents (generator.rs:206): a cast between two slots cuts the text and removes the extents from it.
  • lower/expr.rs:3720: the shape of a strided view.
  • lower/expr.rs:6535 (tensor_sizes) and lower/expr.rs:6743 (clone): the sizes and the element come from the text.
  • lower/expr.rs:5438: the placeholder value after a match with no matching arm. It uses strip_prefix('i') and is_float_text.
  • lower/control_flow.rs:83 and :91: the placeholder value of a block compares the text with "f32" and "i1".
  • lower/control_flow.rs:746: a for loop asks starts_with("tensor<").
  • lower/mod.rs:1377 and :1515: the struct name of a closure. llvm_struct_name does this already.
  • lower/mod.rs:1678 and strip_memref_space (generator.rs:230).
  • lower/tensors.rs:430: takes the text after byte 7.
  • lower/tensors.rs:858: a gradient asks contains("f32") and three more. is_float_arith does this already.
  • lower/seam_cert.rs:74: asks contains("i1"). The type i16 and the type i128 also match it.
  • lower/tensors.rs:119: asked starts_with("memref<memref<"). codegen: load the tensor references of a GPU region in parameter order #4595 removed it.
  • Four places that this list missed, also in codegen: ask the MLIR type in the AST generator's tensor methods and loops #4621: matmul_into took the element from the text after the last x. reshape and transpose read the extents with trim_start_matches("memref<"). A for loop over a tensor read its element, and a loop over an iterator read its struct name, from the text. Printing a tensor in another memory space built its type from text.

3. The type lowering of the AST code generator

lower_type and lower_type_str build the text of a type, and lower_type parses that text. The places below read that text. The change makes lower_type build the type with melior, and keeps the text only for printing. It may need two PRs: one for the type lowering and one for the enum layout.

  • lower_type (generator.rs:2918) and lower_type_str (generator.rs:3155). lower_type_str has 13 callers: 6 in generator.rs, 6 in lower/expr.rs and 1 in lower/mod.rs.
  • The enum layout (generator.rs:2669 to :2708) asks starts_with("memref<") and compares text with "none". It records the payload types as text in enum_payloads and enum_tensor_payloads, and the match parses them again.
  • The text helpers of the enum layout: scalar_type_bits, is_float_text, enum_byte_slot, and llvm_type_size_align with split_top_level.
  • field_type_str (generator.rs:2727).
  • A borrowed tensor in lower_type (generator.rs:2976).
  • returns_through_slot (generator.rs:338). The flat code generator has its own copy, and the two copies must agree.
  • tensor_descriptor_str, with 6 callers. tensor_descriptor_ty is its typed copy.

4. The flat code generator

The flat code generator writes MLIR as text, and it keeps each type as a string. The fix is the MlirTy value that #388 proposes: a type value that the generator matches on and prints with Display.

  • MlirTy in src/mlir_ty.rs. Register types, argument types and field types (AggLayout::field_tys) become MlirTy.
  • flat.rs:2060 and :3128: the C ABI check asks starts_with("!llvm.struct").
  • memref_lead_dims_and_elem (flat.rs:2195) and memref_lead_dim (flat.rs:2209).
  • flat/emit/aggregate.rs: is_scalar_field, the two !llvm.struct tests at :239 and :270, memref_rank and descriptor_rank. memref_rank gives 2 for memref<?xindex>.
  • flat/emit/call.rs:111 and :274: a cast compares two types as text without their spaces.
  • flat/emit/parallel.rs:202.
  • flat/emit/tensor.rs: memref_element (:13) gives f32 for memref<4xvector<4xf32>>. The zero values at :540 and :939 ask starts_with('f').
  • The helpers that both code generators use: memref_space and memref_in_space_of (lower/mod.rs:2262), and fits_on_the_stack (codegen/mod.rs:41).

How to check each PR

  • The vxc from main and the vxc from the branch emit MLIR for all 1,820 programs under tests/*/pass/, with each code generator. The outputs must be the same.
  • cargo test --workspace --no-fail-fast passes.

Done when

  • No code generator decides what an MLIR type is from its text.
  • No Type::parse call parses text that the generator built from another type. Today src/codegen has 84 Type::parse calls.

Generated With Claude Code

Activity

  1. added
    codegen
    refactor
    on Dec 4, 2026
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