Using C/C++ with MLIR

Maybe what I’m about to ask for is completely out of the scope of MLIR, I’m a bit lost about what MLIR should and should not do.

I’ve worked with passes in LLVM and I used to use clang to generate LLVM IR from C/C++ code. Then I used LLVM passes to work with that IR. However, in the case of MLIR, it looks like there is no way in which you can generate MLIR IR from C/C++ code. Maybe this is because MLIR is not focused on that kind of thing, as LLVM is. In the toy example, a new language (called toy) is defined, which is later transformed in the toy IR, so it starts working with MLIR from a custom language, not a standard one like C/C++.

The only way that I can think of to accomplish this is to generate LLVM IR from C/C++ code, and since MLIR has an LLVM dialect, one could work from this starting point to work on successive transformations and lowering. But to me, it looks like this is discouraged because the whole point is to get to LLVM IR by lowering the previous IR so this final LLVM IR can be finally translated into machine code.

Is it possible to use C/C++ as “source language”, or is it needed to define custom language and semantics to work with MLIR?

1 Like

From what I understand, MLIR was designed so that you could build a hypothetical C/C++ compiler on top of MLIR, and it integrated many learnings from clang development. That said, I am not aware of a C/C++ compiler utilizing MLIR for the pieces currently implemented in clang (the AST and related transforms being one of the big pieces), this would be cool though!
Depending on your use case, you may be interested in generating MLIR from a DSL embedded in C++, for which there are some facilities. Such an effort would share some design space with current binding efforts in Swift and Python, but a C++ variant would benefit from not having to go through the C bindings.

1 Like

Alright, thanks for your thoughts.

So another point of view in this LLVM/MLIR comparison could be:

  • LLVM: clang (compiler) → LLVM IR → executable
  • MLIR: MLIR IR → LLVM IR → executable

Where MLIR lacks of a C/C++ to IR compiler (which could be cool, as you said) and MLIR IR is designed to be multi-level (which is only one of many differences of those IR).

Hi @zEdQ,
@wsmoses, @kumasento, @ftynse, and I built up a tool to enter the MLIR lowering pipeline from C or C++ source code. At the moment, we walk the Clang AST and emit proper MLIR constructs; in the future, we will go for a more dialect approach where C or C++ constructs are modeled within MLIR as a dialect (contributions and ideas are welcome in this direction).

See GitHub - wsmoses/MLIR-GPU and MLIR-GPU/mlir/tools/mlir-clang at main · wsmoses/MLIR-GPU · GitHub on how to install.

The “core” MLIR living in the LLVM monorepo doesn’t have a C++ compiler indeed. Writing one is non-trivial and although there are many folks who expressed interest, nobody has had the time to invest in doing it in production mode so far.

There is an experimental project mentioned above that emits a combination of LLVM, Standard, SCF and Affine dialects.

There are two high-level points I want to make regarding this question:

  1. There is no “MLIR IR”, or at least not in the sense there is LLVM IR; MLIR lets you define arbitrary collections of attributes, types and operations and convert between them.
  2. MLIR was designed to cover the representation domains that are not already covered by LLVM IR. Both at a higher level (e.g., TOSA is at a significantly higher abstraction level than C) and at a lower level (I know of projects that use MLIR to emit target-specific ISA). Many languages afford a higher-level abstraction to perform language-specific transformations. Those could benefit from MLIR and that’s why there are DSLs targeting different dialects. Working exactly at the same level as LLVM IR sounds meaningless at this point, LLVM does it perfectly fine. So emitting only the LLVM dialect from Clang AST will only give you overhead, there are virtually no transformations MLIR does at that level.
1 Like

Thanks for your clarifications, ftynse. I find quite interesting both of your two points.

I probably have explained myself badly about the “MLIR IR” concept. It’s important to note that, as you said, there is not “MLIR IR” as such. Sorry for the confusion.

I have a much more clear vision about MLIR and now I have my question answered :slight_smile: