Is there any library for emitting MLIR from core python syntax? (not like numba or similar)


I am searching for library using which I can generate MLIR from core python syntax. For example, I want to emit MLIR like following

func @matmul() {
  %A = memref.alloc() : memref<64x64xf32>
  %B = memref.alloc() : memref<64x64xf32>
  %C = memref.alloc() : memref<64x64xf32>

  affine.for %i = 0 to 64 {
    affine.for %j = 0 to 64 {
      affine.for %k = 0 to 64 {
        %0 = affine.load %A[%i, %k] : memref<64x64xf32>
        %1 = affine.load %B[%k, %j] : memref<64x64xf32>
        %2 = arith.mulf %0, %1 : f32
        %3 = affine.load %C[%i, %j] : memref<64x64xf32>
        %4 = arith.addf %2, %3 : f32 %4, %C[%i, %j] : memref<64x64xf32>



def matmul(A,B):
    for row in range(rows): 
        for col in range(cols):
            for elt in range(len(B)):
              C[row, col] += A[row, elt] * B[elt, col]
    return C

Is there any?

[PS: I am not searching for solutions like numba, or similar kind. I want to emit MLIR from plain simple python syntax. The example codes are explanation purpose]

Thanks in advance! :slightly_smiling_face:

Nelli might be what you are looking for. I am personally not very familiar with the project, but @makslevental could probably help :slight_smile:

@webmiche That’s an wonderful suggestion :innocent:
This project uses python for MLIR dev. I wonder if there are more projects like this… :thinking: Probably using C++/tablegen

xDSL may be also worth looking into:

1 Like

Yay I’m famous :slight_smile:

FWIW nelli dev has moved over to GitHub - makslevental/mlir-python-utils: The missing pieces (as far as boilerplate reduction goes) of the upstream MLIR python bindings. but really though most of it has already gotten upstreamed into the bindings themselves. I haven’t written docs (on my TODO list) but if you trawl through my recent commits you’ll get the idea.

In particular, the xDSL Frontend project might be worth checking out by @george and @AlexanderViand (@AlexanderViand-Intel nowadays I guess). This project has a similar aim as Nelli, but it tries to be a bit more extendable with new dialects, AFAIU.
Also, one could probably connect this easily to the new xDSL Interpreter to make code executable through xDSL, if that is interesting to you :slight_smile:

This is quite misleading IMO: while xDSL it is a convenient way to experiment in pure Python and is somehow a “Python clone of MLIR”. But it isn’t connect to MLIR (still?), does not use MLIR right now, and so isn’t a comparable answer to Nelli when it comes down “I want to emit MLIR from Python”.


I was talking about the xDSL Frontend project, a “sub-project” of xDSL. Sorry for being unclear on this. From the Frontend README:

The goal of the front-end framework is to allow users to:

1. Write non-SSA xDSL/MLIR programs in Python (or Pythonic DSL).
2. Mix-in real Python code (limited functionality is supported).
3. Compile programs to xDSL (and subsequently to MLIR).

I agree that xDSL itself is not actually an answer to the actual questions, that’s why I did not mention it in my first reply :slight_smile:

The xDSL Frontend project is unfortunately not under active development at the moment, but you might want to have a look at MLL which was presented at the LLVM Developer Meeting a few weeks ago:

imv1990/mll: A python like language for MLIR (

I wonder what’s the status of
There’s a mention of “the mirrored dialects found in xdsl/dialects (arith, builtin, cf, func, llvm, memref, and scf)” which suggests there’s a degree of interoperability, although from that document it’s not clear whether the “input.mlir” in the example was obtained directly from xDSL mapping to MLIR or not.

xDSL fundamentally interoperates in a textual way with MLIR, so essentially, you print the IR in xDSL and reparse it in MLIR (or vice-versa). The input.mlir file is just a hand-written example IR, but we have used this in actual compilation flows. Though, I think this is not the place to expand too far on xDSL. If you are curious, feel free to join the xDSL Zulip Chat.

1 Like