LLVM Discussion Forums

Is there a tool helping translate a C code into MLIR?

Good afternoon!

I am new to MLIR, after playing with the toy dialect, I am interested in trying to use MLIR to optimize some of my old class projects written in C.
Is there any tool we can use to translate a C code(like printf(“Hello world\n”):wink: or Python code into .mlir form?

Thanks a lot!

There is nothing upstream, but several people mentioned working on C-related dialects. Here’s one example Playing around with a C dialect and wondering about how high-level it should be.

You will have to drive optimizations manually and, at this point, there’s nothing significantly interesting we can do at C level that existing tools don’t already do (i.e., loop transformations with the polyhedral model). Many existing transformations are better suited for a significantly higher levels of abstraction, with structured multi-dimensional arrays and semantically-loaded operations such as “convolution”, which are hard to extract form C abstractions.

We are interested in eventually exploring these kinds of optimizations though, so contributions in that direction are welcome.

1 Like

Hey @Arie, like @ftynse mentioned this is an area I’m exploring. I have a basic MLIR dialect for C that I’m improving, but this week I’ve been focusing on building out my C lexer and parser to be more fully-featured. For example, before this week my lexer did not support the string literal in your example, "Hello world\n", but now it does :slight_smile:

Depending on your specific interests, I could share some code with you:

  • If you wish to transform C source code into MLIR, either to MLIR’s LLVM dialect, or to a C dialect, that’s exactly what my project entails, so I could share the whole thing – but I’m not finished. I’m currently rewriting the lexing/parsing layer to properly handle all of C’s grammar elements. For example, yesterday I improved my lexer to handle integer constants like 0xdeadbeefUll. If you could wait a few weeks, I think my frontend will be able to translate most C source code into MLIR.
  • If you wish to use MLIR builders to create MLIR code that represents the equivalent of your hello world C source program, I could share my dialect with you. It can mostly handle your example: a c.call operation to printf, and a c.constant of type !c<"char*"> with a value of "Hello world\n".

However, I haven’t spent any time building out optimizations. My project would merely translate your old class projects into MLIR, I don’t have anything that would optimize them, certainly not beyond anything GCC or Clang do today.

(For those who may be curious: I’m writing my own C lexer/parser mostly for fun, and also because I didn’t want to try and shoehorn MLIR generation into an existing compiler like Clang. And if the C dialect I come up with is any good, I do want to talk about including it in the MLIR project itself, but I want to make sure it can handle any C code my frontend throws at it first.)

If you wait a few weeks, I’ll put what I have on GitHub, and you’ll at least be able to translate your class projects into MLIR. Beyond that, we can brainstorm then about what optimizations we could develop.


@modocache if you’re still willing, I’d love to take a look at what you’ve done

I’ve been working on something very similar and started utilizing MLIR but running into learning pains

For context, I’m building a structured editor for a hlsl-like language. From source, I generate UI (node graph editors/sequence editors/etc) which underneath directly operates on the ast
The user can also specify execution schedules (e.g. similar to Halide) and the resulting code is emitted back into C or Compute (for user-end debugging)

I’m attempting to migrate it to use MLIR to leverage some of its infrastructure (dag re-writing) and then possibly some of its optimization dialects (structured ops/affine)

1 Like