<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 3 Feb 2020 at 12:06, Michael Kruse <<a href="mailto:llvmdev@meinersbur.de">llvmdev@meinersbur.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Am Do., 30. Jan. 2020 um 04:40 Uhr schrieb Uday Kumar Reddy Bondhugula<br>
<<a href="mailto:uday@polymagelabs.com" target="_blank">uday@polymagelabs.com</a>>:<br>
> There are multiple ways regions in MLIR can be viewed, but the more relevant point here is you do have a loop tree structure native in the IR with MLIR. Regions in MLIR didn't evolve from modeling inlined calls - the affine.for/affine.if were originally the only two operations in MLIR that could hold blocks (which in turn are a list of operations as you know) and there wasn't anything by the name region. Later, "a list of blocks" was renamed "region" in order to generalize and unify it with other concepts that could be captured with "ops with regions", one of which is isomorphic to a "just inlined" call as you view it. But that doesn't mean a loop tree doesn't exist as a first class thing in the IR when you have the relevant ops around -- there is a hierarchy.<br>
<br>
Thanks for the interesting insights into the development history of MLIR.<br>
<br>
<br>
>> Regarding the affine dialect, I see the same problem that Polly has when creating a schedule tree representation: A lot of work has to be done to make IR originating from Clang compatible. Everything becomes easy if the front-end can generate an affine dialect out-of-the box.<br>
><br>
> Right - but for the purposes of your proposal, this isn't really relevant - for that matter, one could just use the loop.for, loop.if ops if you don't want to leverage affine restrictions. Moreover, with affine.graybox ops, you can always use affine.for/if wherever you have structured loops (otherwise, you would fall back to a flat list of blocks inside the region of the graybox.) While directly generating the affine dialect maximally from the frontend / Clang is one option, the other is to just generate grayboxes with trivial affine.for/if (or just loop.for/if), and then eliminate the grayboxes maximally within MLIR. This way things are reusable across different frontends, and it would be similar to Polly's approach except that you would be dealing with loops/multi-dimensional arrays where possible instead of flat list of CFGs and GEPs.<br>
<br>
I think there is a relevant difference of whether we come from a<br>
high-level code generator and then lift restrictions or from a<br>
low-level IR which has to be raised. If starting with the high-level,<br>
we will have to bail out on representing things because we cannot<br>
ensure the expected semantics of the high-level idioms (while-,<br>
do-loops, coroutines, possibly infinite loops, non-returning elements<br>
in the loop body, ...) and have to work towards poking holes into the<br>
high-level representation that existing passes must me able to handle.<br>
When starting with a low-level approach, useful guarantees are added<br>
to the representation, but everything can be represented at the<br>
beginning.<br>
I am not saying that the two approaches cannot meet, but I am afraid<br>
that the high-level approach, like Polly, adds many bail-outs making<br>
it difficult to use in practice. For instance, we want to apply<br>
strip-mining to a loop. Since it does not change the execution order<br>
of any body code, it is always valid, yet we'd have to bail out if we<br>
cannot guarantee the representation's guarantees. I would like to<br>
avoid that.<br>
<br>
However, I agree that MLIR has the expressiveness requires for<br>
hierarchical loop structures. We don't think we need to argue about<br>
that.<br>
<br>
<br>
> That's actually not how I read it. Red/green trees was *one* of the nine items you mentioned in your list and this didn't come out as the central idea in your opening paras, but let's go with this now that it's clearer to me.<br>
<br>
Indeed, red/green trees (or DAGs) are one one of the ideas to improve<br>
loop optimizations, but does justify its use by itself. They happen to<br>
be effectively necessary for others in the list (e.g. versioning,<br>
profitiability heuristic by cost function, etc...) and the reason why<br>
I think the same cannot be done with MLIR. In hindsight, I could have<br>
pointed this out more in the original RFC. Note that a hierarchical<br>
representation was not an explicit feature in the list.<br>
<br>
To convince me that MLIR is the better IR for loop optimizations,<br>
might show that each of the features enabled by cheap subtree reuse<br>
can also be done sufficiently efficient and easily on MLIR (or that a<br>
feature is not what would actually would want).<br></blockquote><div><br></div><div>I suspect that you are looking at this as "MLIR vs your proposal", but the point I've been trying to make is to compare "your proposal on MLIR" vs "your proposal on LLVM", i.e., you may want to evaluate the cost/benefit of hosting your approach on MLIR vis-a-vis LLVM while accounting for the fact that you'll need the conversion for LLVM to MLIR and back for the relevant IR units with the former. On this note, in my opinion, MLIR doesn't have a unified/clear strategy for loop optimization yet (even op representation wise); there are different ops/representations that are being pedalled by different folks, and some I believe are redundant or are simply duplicating infrastructure in less powerful ways. I'm saying this because if you choose MLIR to host your stuff, you'll have choices to make on which forms to use.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
>> (which I firmly disagree with being close to MLIR's in-memory representation), not the textual format.<br>
><br>
> "close" isn't the right word here, "closer" is! Would you agree that the representation you are proposing is closer to MLIR's representation (both its in-memory and its textual representation) than to LLVM's or is this proximity not really relevant for the purposes of your proposal? I think it's important to know which among the two is the more important question.<br>
<br>
I think MLIR is an evolution of LLVM-IR and Swift-IR, built around<br>
similar principles such as SSA and Control-Flow Graphs (I understand<br>
that in addition to CFGs, MLIR also enables structured control flow<br>
idioms). SSA is a distinguishing feature: It allows to quickly<br>
traversing def/uses (where compilers without it need a data-flow<br>
analyses), but make subtree reuse hard.<br>
<br>
Does this answer your question?<br></blockquote><div><br></div><div>Not directly, but I think I have more information now. You are probably saying it's nearly as easy/difficult to host your approach on LLVM IR as it is on MLIR. </div><div><br></div><div>All the best!</div><div><br></div><div>~ Uday</div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
<br>
> Note that currently there is really very little difference between MLIR's textual format for 'for'/if's and the in-memory form its passes use. The passes don't create any in-memory higher order representations for these IR units; they directly update them. There is nothing like the kind of complementary abstractions you are proposing (for cost models, copy-wise, etc.).<br>
<br>
The point I was making is that the in-memory format has references to<br>
related items such as parents and use-def chains. These are implicit<br>
in the textual format, e.g. the parent of an operation is defined by<br>
its syntactical location. When reading into memory, it is not<br>
obligatory for the objects to have all the references to related<br>
objects.<br>
<br>
<br>
Michael<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr">Founder and Director, PolyMage Labs</div></div></div>