I’m forking comments from the patch D85481 by @zhanghb97 to this thread to have a higher level discussion about next steps.
@ftynse and I were discussing over chat this morning/evening and were thinking that we could reduce ambiguity on the next step by focusing it a bit more tightly. The patch covers a number of core IR constructs that have a fair bit of subtlety to them, and we would like to take a more incremental/design driven approach to them.
We were wondering if it might make sense to scope the first patch down to a few things:
- Binding for
MlirContext
- Binding for
MlirModule
without a constructor (avoids having to bind location) -
MlirContext.parse()
method that returns an MlirModule -
MlirModule.dump()
method
That would be enough for a first patch, and it defers a lot of the more complex issues. A good second and third patch could be (note that there may be some other opinions on naming and details):
- Add a C-API for
mlirModulePrint(...)
that allows to get the printed asm form. - Add a python binding for
MlirModule.print(file)
which prints ASM to an open file handle and anMlirModule.print_to_string()
that is a shortcut for returning a string directly. As a very first step, you could just provide theprint_to_string()
method.
I would recommend to have a simple mlirModulePrint()
C-API that does not take a mapping of OpPrintingFlags
as a first step, and then provide a second mlirModulePrintWithOptions(MlirOpPrintingOptions)
as a followup. Leaving out a C-API mapping of the printing flags will simplify things, and there is ambiguity in getting the OpPrintingOptions
mapped well in a way that maximizes version compatibility.
Further, I would recommend modeling the mlirModulePrint()
function to take a callback and a cookie for appending to the string. Example:
// Accumulates string contents represented by data and len with a caller supplied cookie.
// Returns 0 (false) on IO failure and non-zero on success.
typedef void (*MlirAppendStringCallback)(void *cookie, const char *data, size_t len));
// Prints the ASM form of the module to the given callback with a user-supplied cookie.
void mlirModulePrint(MlirAppendStringCallback callback, void *cookie);
Note that there are multiple ways to model a print function like this, and this is the form that I would choose/advocate for as the most suitable for this kind of API.
I think that getting the python bindings bootstrapped with the ability to create a context and parse/dump/print the ASM form could be a nice thing to build further on. From a work sequencing perspective and given the state we have on it, either Alex or I would like to drive some of the subtler issues of mapping the Operation hierarchy ourselves within the next week or so and were hoping that scoping down your patch a bit to just the basics would open us up to be able to work better in parallel (i.e. after you submit the scaled down first patch above).