<div dir="ltr"><div><font size="4">Please find the reply inline below:</font></div><div dir="ltr"><br></div><div dir="ltr">On Sat, Feb 15, 2020 at 12:51 AM Johannes Doerfert <<a href="mailto:jdoerfert@anl.gov">jdoerfert@anl.gov</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 02/14, Vinay Madhusudan wrote:<br>
> It sounds like LLVM IR is being considered for optimizations in OpenMP<br>
> constructs. There seems to be plans regarding improvement of LLVM IR<br>
> Framework for providing things required for OpenMP / flang(?)<br>
<br>
LLVM has the OpenMPOpt pass now [0] in which we can put OpenMP specific<br>
transformations. For now it is simple but we have some more downstream<br>
patches, e.g., parallel region expansion [Section 5, 1].  Other<br>
optimizations [Section 3 & 4, 1], will be performed by the Attributor<br>
(see [4] after [2,3]) after one missing piece (basically [5] with some<br>
more plumming) was put in place, see [2,3] for details on the idea.<br>
<br>
Please feel free to ask questions on any of this.<br>
<br>
[0] <a href="https://reviews.llvm.org/D69930" rel="noreferrer" target="_blank">https://reviews.llvm.org/D69930</a><br>
[1] <a href="http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf" rel="noreferrer" target="_blank">http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf</a><br>
[2] <a href="https://www.youtube.com/watch?v=zfiHaPaoQPc" rel="noreferrer" target="_blank">https://www.youtube.com/watch?v=zfiHaPaoQPc</a><br>
[3] <a href="http://compilers.cs.uni-saarland.de/people/doerfert/par_opt_lcpc18.pdf" rel="noreferrer" target="_blank">http://compilers.cs.uni-saarland.de/people/doerfert/par_opt_lcpc18.pdf</a><br>
[4] <a href="https://youtu.be/CzWkc_JcfS0" rel="noreferrer" target="_blank">https://youtu.be/CzWkc_JcfS0</a><br>
[5] <a href="https://reviews.llvm.org/D71505" rel="noreferrer" target="_blank">https://reviews.llvm.org/D71505</a><br>
<br>
It might also worth looking into [6,7] mentioned below.<br>
<br></blockquote><div><br></div><div><span id="gmail-docs-internal-guid-46a2ab74-7fff-8d10-2e2f-007d77180566"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">Thanks for sharing all the interesting work going on LLVM IR side of things for OpenMP. I will take a look at it.</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">As I have mentioned before, I am mostly concerned (in this thread) about the MLIR side of things for OpenMP and various optimizations/ transformations that are planned. I was surprised to see the plans for LLVM IR and not for MLIR.</span></p></span></div><div> <br></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>
> Are there any design considerations which contain pros and cons about using<br>
> the MLIR vs LLVM IR for various OpenMP related optimizations/<br>
> transformations?<br>
<br>
The biggest pro for LLVM-IR is that it works for C/C++ right now.</blockquote><div><br></div><div><span id="gmail-docs-internal-guid-c836aecf-7fff-2a84-3911-133776b0a2ab"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">> TBH, I don't even know what transformations people plan to do on OpenMP MLIR (and why).</span></span><br></div><div><br></div><div><span id="gmail-docs-internal-guid-fc904cd0-7fff-a899-64e7-de14272fe9cd"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">A good support for C/C++ in LLVM-IR shouldn’t be an impediment for considering OpenMP dialect transformations / optimizations for the ongoing *flang* effort. As we all agree, things like dependencies etc can be solved in an *easier way* in MLIR and C/C++ seems to be the “only caveat”.</span></p></span><br class="gmail-Apple-interchange-newline"></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"> In<br>
addition, as I mentioned before, LLVM-IR has mature analysis and<br>
transformation passes for real world programs and support things like<br>
LTO out of the box.<br>
<br></blockquote><div><span id="gmail-docs-internal-guid-324c4609-7fff-168f-b9c6-81a3dc89654d"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">I think LTO for flang is planned to be based on LLVM. I do not see any other option for now!</span></span><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">
> The latest RFC [ (3) in my original post ] mentions that:<br>
> <br>
> > So there exist some questions regarding where the optimisations should be<br>
> carried out.<br>
> <br>
> Could you please provide more details on this?<br>
> <br>
> I would like to quote Chris here:<br>
> <br>
> “if you ignore the engineering expense, it would clearly make sense to<br>
> reimplement the mid-level LLVM optimizers on top of MLIR and replace<br>
> include/llvm/IR with a dialect definition in MLIR instead.“ --<br>
> <a href="http://lists.llvm.org/pipermail/llvm-dev/2020-January/138341.html" rel="noreferrer" target="_blank">http://lists.llvm.org/pipermail/llvm-dev/2020-January/138341.html</a><br>
><br>
> *Rest of the comment are inlined.*<br>
> <br>
> On Thu, Feb 13, 2020 at 11:48 PM Johannes Doerfert <<a href="mailto:jdoerfert@anl.gov" target="_blank">jdoerfert@anl.gov</a>><br>
> wrote:<br>
> <br>
> > Hi Vinay,<br>
> ><br>
> > Thanks for taking an interest and the detailed discussion.<br>
> ><br>
> > To start by picking a few paragraph from your email to clarify a couple<br>
> > of things that lead to the current design or that might otherwise need<br>
> > clarification. We can talk about other points later as well.<br>
> ><br>
> > [<br>
> >   Site notes:<br>
> >     1) I'm not an MLIR person.<br>
> >     2) It seems unfortnuate that we do not have a mlir-dev list.<br>
> > ]<br>
> ><br>
> ><br>
> > > 1. With the current design, the number of transformations / optimizations<br>
> > > that one can write on OpenMP constructs would become limited as there can<br>
> > > be any custom loop structure with custom operations / types inside it.<br>
> ><br>
> > OpenMP, as an input language, does not make many assumptions about the<br>
> > code inside of constructs*.<br>
> <br>
> <br>
> This isn’t entirely correct because the current OpenMP API specification (<br>
> <a href="https://www.openmp.org/spec-html/5.0/openmpch1.html" rel="noreferrer" target="_blank">https://www.openmp.org/spec-html/5.0/openmpch1.html</a>) assumes that the code<br>
> inside the constructs belong to C, C++ and Fortran programs.<br>
<br>
(FWIW, my next sentence specified that I talk about the  base language<br>
       but anyway.)<br>
<br>
While technically true, I will recommend not to make that assumption. We<br>
do already allow non-base language constructs, e.g., CUDA intrinsics in<br>
target regions, and that will not go away because it is required to<br>
maximize performance.<br>
<br>
<br>
> > So, inside a parallel can be almost anything<br>
> > the base language has to offer, both lexically and dynamically.<br>
> ><br>
> <br>
> <br>
> I am mostly concerned with the MLIR side of things for OpenMP<br>
> representation.<br>
> <br>
>  MLIR can not only support operations for General Purpose languages like<br>
> C,C++, Fortran, etc but also various Domain Specific Language<br>
> representations as dialects (Example, ML, etc.). Note that there is also<br>
> SPIR V dialect which is again meant for “Parallel Compute”.<br>
> <br>
>  It becomes important to define the scope of the dialects / operations /<br>
> types supported inside OpenMP operations in MLIR.<br>
<br>
Arguably, the OpenMP dialect in MLIR should match the OpenMP directives<br>
and clauses as defined by the standard. Anything else is "not OpenMP".<br>
<br></blockquote><div> <br></div><div><span id="gmail-docs-internal-guid-7f60432f-7fff-0d4c-4629-b43d97d3c366"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">Yes! This is what needs to be “defined” for the various dialects...</span></span><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>
> > Assuming otherwise is not going to work. Analyzing a "generic" OpenMP<br>
> > representation in order to determine if can be represented as a more<br>
> > restricted "op" seems at least plausible. You will run into various<br>
> > issue, some mentioned explicitly below.<br>
> <br>
> Isn’t it the other way around? For example, it doesn’t make much sense to<br>
> wrap OpenMP operations for SPIR-V operations / types.<br>
<br>
I maybe misunderstanding but I thought you want to use something like<br>
the GPU / Affine dialect to represent an OpenMP target region / loop.<br>
That is plausible if you analyze the target region / loop and verify it<br>
fits into the more generic dialect semantics.<br>
<br>
<br>
> I think it is important to specify (in the design) which existing MLIR<br>
> dialects are supported in this effort and the various lowerings /<br>
> transformations / optimizations which are planned for them.<br>
<br>
That I cannot really help you with. TBH, I don't even know what<br>
transformations people plan to do on OpenMP MLIR (and why).<br>
<br>
<br>
> > For starters, you still have to<br>
> > generate proper OpenMP runtime calls, e.g., from your GPU dialect, even<br>
> > if it is "just" to make sure the OMPD/OMPT interfaces expose useful<br>
> > information.<br>
> ><br>
> ><br>
> You can have a well-defined call-like mlir::Operation which calls the GPU<br>
> kernel. Perform all cross-device transformations in an easier way.<br>
> Then, this operation can be lowered to OpenMP runtime calls during LLVM<br>
> dialect conversion.<br>
<br>
You missed my point I made in the other email. An OpenMP target region<br>
is statically not a GPU offload so you should not model it as such "for<br>
some time".<br>
<br></blockquote><div><span id="gmail-docs-internal-guid-f4fd27a4-7fff-1287-8c2a-177f89c578ed"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap"> </span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">I did not get your point here. </span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">Why is OpenMP program compiled with single target (say, nvptx) isn’t a GPU offload case? Are you saying that tgt_target might invoke some code other than GPU kernel even if the user intends to run it on GPU?</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">Even when there are multiple targets, all the code should get “statically” compiled and kept ready. </span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">In any case, unless it is statically proved that the GPU kernel is not executed, you need to optimize the code inside the GPU kernel and it would be better to do it in MLIR was the point.</span></p></span></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> I think this is much better than directly having calls<br>
> to the OpenMP runtime library based on a kernel name mentioned in<br>
> llvm::GlobalVariable.<br>
<br>
(Current) implementation is not semantics. There is no reason not to<br>
change the way we lower OpenMP, e.g., by getting rid of the global<br>
variables. They are present for a reason but not intrinsically required.<br>
See the TRegions for example [6,7], they totally change the GPU lowering,<br>
making it device agnostic and easy to analyze and optimize in the middle<br>
end. Arguing the current encoding of OpenMP in LLVM-IR is problematic is<br>
the same as arguing MLIR's LLVM dialect doesn't support atomic_rmw, it<br>
might be true but its changeable.<br>
<br>
[6] <a href="https://link.springer.com/chapter/10.1007/978-3-030-28596-8_11" rel="noreferrer" target="_blank">https://link.springer.com/chapter/10.1007/978-3-030-28596-8_11</a><br>
[7] <a href="http://parallel.auckland.ac.nz/iwomp2019/slides_TRegion.pdf" rel="noreferrer" target="_blank">http://parallel.auckland.ac.nz/iwomp2019/slides_TRegion.pdf</a><br>
<br>
<br>
> > * I preclude the `omp loop` construct here as it is not even implemented<br>
> >   anywhere as far as I know.<br>
> ><br>
> ><br>
> > > 2. It would also be easier to transform the Loop nests containing OpenMP<br>
> > > constructs if the body of the OpenMP operations is well defined (i.e.,<br>
> > does<br>
> > > not accept arbitrary loop structures). Having nested redundant<br>
> > "parallel" ,<br>
> > > "target" and "do" regions seems unnecessary.<br>
> ><br>
> > As mentioned above, you cannot start with the assumption OpenMP input is<br>
> > structured this this way. You have to analyze it first. This is the same<br>
> > reason we cannot simply transform C/C++ `for loops` into `affine.for`<br>
> > without proper analysis of the loop body.<br>
> ><br>
> > Now, more concrete. Nested parallel and target regions are not<br>
> > necessarily redundant, nor can/should we require the user not to have<br>
> > them. Nested parallelism can easily make sense, depending on the problem<br>
> > decomposition. Nested target will make a lot of sense with reverse<br>
> > offload, which is already in the standard, and it also should be allowed<br>
> > for the sake of a modular (user) code base.<br>
> ><br>
> <br>
> Just to be clear, having all three of “target”, “parallel” and “do” doesn’t<br>
> represent “Nested parallelism” at all in the proposed design! ( 2(d) ).<br>
> <br>
> omp.target {<br>
> <br>
>   omp.parallel {<br>
> <br>
>      omp.do {<br>
> <br>
>       …...<br>
> <br>
>       }<br>
> <br>
>    }<br>
> <br>
> }<br>
> <br>
> Above invokes a call to the tgt_target() for the code inside omp.do as<br>
> mentioned in the proposal.<br>
<br>
I do not follow. Just to make sure, the above should be roughly<br>
equivalent to the  code below, correct? There is no "nested"<br>
parallelism, sure, but I thought you were talking about the case where<br>
there is, e.g. add another `#pragma omp parallel` inside the one that </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
already is there. That is nested parallelism which can happen and make<br>
total sense for the application. <br></blockquote><div><br></div><div><span style="background-color:transparent;color:rgb(0,0,0);font-family:Arial;font-size:11pt;white-space:pre-wrap">No, actually. I was just saying that, all three could have been merged to one OpenMP operation “target_parallel_for” in MLIR rather than having three and analyzing them. </span><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>
#pragma omp target<br>
{<br>
  #pragma omp parallel<br>
  {<br>
    #pragma omp for<br>
    for (...)<br>
    {<br>
      ...<br>
    }<br>
  }<br>
}<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
> ><br>
> > > 3. There would also be new sets of loop structures in new dialects when<br>
> > > C/C++ is compiled to MLIR. It would complicate the number of possible<br>
> > > combinations inside the OpenMP region.<br>
> ><br>
> > Is anyone working on this? If so, what is the timeline? I personally was<br>
> > not expecting Clang to switch over to MLIR any time soon but I am happy<br>
> > if someone wants to correct me on this. I mention this only because it<br>
> > interacts with the arguments I will make below.<br>
> ><br>
> ><br>
> > > E. Lowering of target constructs mentioned in ( 2(d) ) specifies direct<br>
> > > lowering to LLVM IR ignoring all the advantages that MLIR provides. Being<br>
> > > able to compile the code for heterogeneous hardware is one of the biggest<br>
> > > advantages that MLIR brings to the table. That is being completely missed<br>
> > > here. This also requires solving the problem of handling target<br>
> > information<br>
> > > in MLIR. But that is a problem which needs to be solved anyway. Using GPU<br>
> > > dialect also gives us an opportunity to represent offloading semantics in<br>
> > > MLIR.<br>
> ><br>
> > I'm unsure what the problem with "handling target information in MLIR" is<br>
> > but<br>
> > whatever design we end up with, we need to know about the target<br>
> > (triple) in all stages of the pipeline, even if it is just to pass it<br>
> > down.<br>
> ><br>
> ><br>
> > > Given the ability to represent multiple ModuleOps and the existence of<br>
> > GPU<br>
> > > dialect, couldn't higher level optimizations on offloaded code be done at<br>
> > > MLIR level?. The proposed design would lead us to the same problems that<br>
> > we<br>
> > > are currently facing in LLVM IR.<br>
> > ><br>
> > > Also, OpenMP codegen will automatically benefit from the GPU dialect<br>
> > based<br>
> > > optimizations. For example, it would be way easier to hoist a memory<br>
> > > reference out of GPU kernel in MLIR than in LLVM IR.<br>
> ><br>
> > While I agree with the premise that you can potentially reuse MLIR<br>
> > transformations, it might not be as simple in practice.<br>
> ><br>
> > As mentioned above, you cannot assume much about OpenMP codes, almost<br>
> > nothing for a lot of application codes I have seen. Some examples:<br>
> ><br>
> > If you have a function call, or any synchronization event for that<br>
> > matter, located between two otherwise adjacent target regions (see<br>
> > below), you cannot assume the two target regions will be offloaded to<br>
> > the same device.<br>
> > ```<br>
> >   #omp target<br>
> >   {}<br>
> >   foo();<br>
> >   #omp target<br>
> >   {}<br>
> > ```<br>
> ><br>
> <br>
> These kinds of optimizations are much easier to write in MLIR:<br>
> <br>
> LLVM IR for the above code would contain a series of instructions of OpenMP<br>
> runtime call setup and foo() in the middle followed by another set of<br>
> OpenMP runtime related instructions. The body of the two target constructs<br>
> would be in two different outlined functions (if not modules).<br>
> <br>
> It takes quite a bit of code to do analysis / transformation to write any<br>
> optimization on the generated LLVM IR.<br>
<br>
You are right about the module's being a problem. As I mentioned in my<br>
last email, we are working on that by not having them in different ones<br>
during the optimization pipeline. If we make the `target` `parallel`<br>
instead we can simulate that right now. The bodies are in different<br>
functions, sure, but does it matter? Let's walk through parallel region<br>
expansion (see above [Section 5, 1]) so you can judge for yourself:<br>
<br>
```<br>
#omp parallel<br>
{ body0 }<br>
some_code<br>
#omp parallel<br>
{ body1 }<br>
```<br>
<br>
will become<br>
<br>
```<br>
__kmpc_fork_call(..., @body0_fn, ...)<br>
some_code<br>
__kmpc_fork_call(..., @body1_fn, ...)<br>
```<br>
<br>
in IR. Simplified, there are 3 cases here:<br>
1) some_code is harmless, meaning all of it can be executed redundantly.<br>
2) parts of some some_code need to be guarded to be sequential but<br>
    they can be executed in a parallel region otherwise, e.g., the code<br>
    will not observe the difference through runtime calls.<br>
3) parts of some some_code cannot be executed in a parallel region as<br>
   they might observe the difference through runtime calls.<br>
First note that you need to do the classification regardless of your<br>
encoding (=IR). In case of 3) we are done and nothing is happening.<br>
Let's consider case 2) as 1) is basically a special case of it. As shown<br>
in the paper [1], you need to broadcast values created by some_code<br>
across all threads and synchronize appropriately to preserve semantic.<br>
Other than that, the transformation is straight forward:<br>
<br>
A) Create a function "@body01_fn" that is basically the outlined region<br>
in which code is then guarded and __kmpc_fork_call are replaced by<br>
direct calls. It looks like this:<br>
```<br>
  call @body0_fn(...)<br>
  #omp master<br>
  some_code<br>
  #omp barrier<br>
  call @body1_fn(...)<br>
```<br>
<br>
B) Replace the region you put in the new function with a<br>
   __kmpc_fork_call to it:<br>
```<br>
  __kmpc_fork_call(..., @body01_fn, ...)<br>
```<br>
<br>
C) Done.<br>
<br>
If you are interested in the implementation I'll add you as a reviewer<br>
once I put it on Phab. I'm in the process of cleaning up my stand alone<br>
pass and moving it into the OpenMPOpt pass instead.<br>
<br>
<br>
> vs.<br>
> <br>
> MLIR provides a way to represent the operations closer to the source. It is<br>
> as simple as checking the next operation(s) in the mlir::Block. OpenMP<br>
> target operation contains an inlined region which can easily be fused/<br>
> split /  or any other valid transformation for that matter.<br>
> <br>
> Note that you can also perform various Control Structure Analysis /<br>
> Transformations much easier in MLIR. For example, you can decide to execute<br>
> foo() based on certain conditions, and you can merge the two target regions<br>
> in the else path.<br>
<br>
At the end, it's an encoding difference. Sure, the handling might be<br>
easier in certain situations but all the validity checks, hence code<br>
analyses, are still required. The actual "rewrite" is usually not the<br>
hard part.<br>
<br>
<br>
> > Similarly, you cannot assume a `omp parallel` is allowed to be executed<br>
> > with more than a single thread, or that a `omp [parallel] for` does not<br>
> > have loop carried data-dependences, ...<br>
> ><br>
> <br>
> With multi-dimensional index support for arrays, wouldn’t it be better to<br>
> do the data dependence analysis in MLIR?<br>
<br>
Yes, probably.<br>
<br>
<br>
> LLVM IR has linearized subscripts for multi-dimensional arrays.<br>
> llvm::DependenceAnalysis tries to “guess” the indices based on different<br>
> patterns in SCEV. It takes an intrinsic<br>
> <<a href="http://llvm.org/devmtg/2020-04/talks.html#LightningTalk_88" rel="noreferrer" target="_blank">http://llvm.org/devmtg/2020-04/talks.html#LightningTalk_88</a>> or metadata or<br>
> some other mechanism of communication from the front end (not the built-in<br>
> set of instructions) to solve this problem.<br>
<br>
Not disagreeing with you on this one ;)<br>
<br>
The only caveat is that we still live in a world in which C/C++ is a<br>
thing.<br>
<br></blockquote><div><br></div><div><span style="background-color:transparent;color:rgb(0,0,0);font-family:Arial;font-size:11pt;white-space:pre-wrap">Doesn’t mean that we should not be having optimizations in MLIR for Fortran :-)</span><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>
> > Data-sharing attributes are also something that has to be treated<br>
> > carefully:<br>
> > ```<br>
> > x = 5;<br>
> > #omp task<br>
> >   x = 3;<br>
> > print(x);<br>
> > ```<br>
> > Should print 5, not 3.<br>
> ><br>
> <br>
> You can have “x” as a locally defined variable inside the “task” contained<br>
> region in MLIR OR custom data-sharing attributes in OpenMP dialect.<br>
<br>
I'm not saying it is impossible or even hard, but maybe not as straight<br>
forward as one might think. Your encoding is for example very reasonable.<br>
<br>
In the example below you need to print 3, not 5, e.g., constant prop on<br>
the outer level should not happen.<br>
<br></blockquote><div><br></div><div><span id="gmail-docs-internal-guid-c574ae4d-7fff-160d-57d4-47ecbe42a025"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">Yes, that is why I am concerned about the design considerations of clauses like shared, map, firstprivate, etc. </span></span><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>
x = 5;<br>
#omp task shared(x)<br>
{<br>
  x = 3;<br>
  some_form_of_sync();<br>
  ...<br>
}<br>
some_form_of_sync();<br>
print(x);<br>
```<br>
<br>
<br>
> > I hope I convinced you that OpenMP is not trivially mappable to existing<br>
> > dialects without proper analysis. If not, please let me know why you<br>
> > expect it to be.<br>
> ><br>
> I do not see much reason why the issues you mentioned can’t trivially be<br>
> mapped to the MLIR infrastructure. There is an easy way to define custom<br>
> operations / types / attributes in OpenMP dialect and perform optimizations<br>
> based on the *IR that is created especially for OpenMP*. The analysis /<br>
> transformations required can be easily written on the custom operations<br>
> defined rather than having a lowered form in the LLVM IR.<br>
<br>
You can totally define your OpenMP dialect and map it to that. Mapping<br>
to other dialects is the problematic part.</blockquote><div><br></div><div><span id="gmail-docs-internal-guid-25831d37-7fff-7085-e299-94cf69679cf0"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap">Yes, this is why I have mentioned that RFC should talk about the following:</span></p><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-style:italic;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap"> "I think it is important to specify (in the design) which existing MLIR</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-style:italic;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap"> dialects are supported in this effort and the various lowerings /</span></p><span style="font-size:11pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-style:italic;font-variant-numeric:normal;font-variant-east-asian:normal;vertical-align:baseline;white-space:pre-wrap"> transformations / optimizations which are planned for them."</span></span></div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> As I mentioned, `omp<br>
parallel` does not mean "parallel" or "dependence-free".<br>
<br>
<br>
Since you mention it, why do you think it is conceptually or practically<br>
harder to write an analysis/transformations on IR? I mean, you teach<br>
your analysis what the op "omp.parallel" means, right? Why not teach an<br>
(interprocedural) analysis what __kmpc_fork_call() does (see [2,3] above)?<br>
<br>
FWIW, there are LLVM analyses and transformations that already know<br>
about the transitive call made by __kmpc_fork_call and pthread_create<br>
(see [4] above). It is done in a way that you can easily annotate your<br>
own C/C++ or IR to make use of it, e.g., for your own transitive<br>
callbacks:<br>
  <a href="https://clang.llvm.org/docs/AttributeReference.html#callback" rel="noreferrer" target="_blank">https://clang.llvm.org/docs/AttributeReference.html#callback</a><br>
  <a href="https://llvm.org/docs/LangRef.html#callback-metadata" rel="noreferrer" target="_blank">https://llvm.org/docs/LangRef.html#callback-metadata</a><br>
<br>
<br>
> The various dialects / transformations in MLIR are in development / early<br>
> phase (Example, GPU dialect) waiting to be improved with use cases such as<br>
> this!<br>
<br>
Great! I am eagerly looking forward to this.<br>
<br>
Cheers,<br>
  Johannes<br>
<br>
> ><br>
> > Now when it comes to code analyses, LLVM-IR offers a variety of<br>
> > interesting features, ranging from a mature set of passes to the<br>
> > cross-language LTO capabilities. We are working on the missing parts,<br>
> > e.g., heterogeneous llvm::Modules as we speak. Simple OpenMP<br>
> > optimizations are already present in LLVM and interesting ones are<br>
> > prototyped for a while now (let me know if you want to see more not-yet<br>
> > merged patches/optimizations). I also have papers, results, and<br>
> > talks that might be interesting here. Let me know if you need pointers<br>
> > to them.<br>
> ><br>
> ><br>
> > Cheers,<br>
> >   Johannes<br>
> ><br>
> ><br>
> ><br>
> > On 02/13, Vinay Madhusudan via llvm-dev wrote:<br>
> > > Hi,<br>
> > ><br>
> > > I have few questions / concerns regarding the design of OpenMP dialect in<br>
> > > MLIR that is currently being implemented, mainly for the f18 compiler.<br>
> > > Below, I summarize the current state of various efforts in clang / f18 /<br>
> > > MLIR / LLVM regarding this. Feel free to add to the list in case I have<br>
> > > missed something.<br>
> > ><br>
> > > 1. [May 2019] An OpenMPIRBuilder in LLVM was proposed for flang and clang<br>
> > > frontends. Note that this proposal was before considering MLIR for FIR.<br>
> > ><br>
> > > a. llvm-dev proposal :<br>
> > ><br>
> > <a href="http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/2019-May/000197.html" rel="noreferrer" target="_blank">http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/2019-May/000197.html</a><br>
> > ><br>
> > > b. Patches in review: <a href="https://reviews.llvm.org/D70290" rel="noreferrer" target="_blank">https://reviews.llvm.org/D70290</a>. This also<br>
> > includes<br>
> > > the clang codegen changes.<br>
> > ><br>
> > > 2.  [July - September 2019] OpenMP dialect for MLIR was discussed /<br>
> > > proposed with respect to the f18 compilation stack (keeping FIR in mind).<br>
> > ><br>
> > > a. flang-dev discussion link:<br>
> > > <a href="https://lists.llvm.org/pipermail/flang-dev/2019-September/000020.html" rel="noreferrer" target="_blank">https://lists.llvm.org/pipermail/flang-dev/2019-September/000020.html</a><br>
> > ><br>
> > > b. Design decisions captured in PPT:<br>
> > > <a href="https://drive.google.com/file/d/1vU6LsblsUYGA35B_3y9PmBvtKOTXj1Fu/view" rel="noreferrer" target="_blank">https://drive.google.com/file/d/1vU6LsblsUYGA35B_3y9PmBvtKOTXj1Fu/view</a><br>
> > ><br>
> > > c. MLIR google groups discussion:<br>
> > ><br>
> > <a href="https://groups.google.com/a/tensorflow.org/forum/#!topic/mlir/4Aj_eawdHiw" rel="noreferrer" target="_blank">https://groups.google.com/a/tensorflow.org/forum/#!topic/mlir/4Aj_eawdHiw</a><br>
> > ><br>
> > > d. Target constructs  design:<br>
> > ><br>
> > <a href="http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/2019-September/000285.html" rel="noreferrer" target="_blank">http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/2019-September/000285.html</a><br>
> > ><br>
> > > e. SIMD constructs design:<br>
> > ><br>
> > <a href="http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/2019-September/000278.html" rel="noreferrer" target="_blank">http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/2019-September/000278.html</a><br>
> > ><br>
> > > 3.  [Jan 2020] OpenMP dialect RFC in llvm discourse :<br>
> > > <a href="https://llvm.discourse.group/t/rfc-openmp-dialect-in-mlir/397" rel="noreferrer" target="_blank">https://llvm.discourse.group/t/rfc-openmp-dialect-in-mlir/397</a><br>
> > ><br>
> > > 4.  [Jan- Feb 2020] Implementation of OpenMP dialect in MLIR:<br>
> > ><br>
> > > a. The first patch which introduces the OpenMP dialect was pushed.<br>
> > ><br>
> > > b. Review of barrier construct is in progress:<br>
> > > <a href="https://reviews.llvm.org/D72962" rel="noreferrer" target="_blank">https://reviews.llvm.org/D72962</a><br>
> > ><br>
> > > I have tried to list below different topics of interest (to different<br>
> > > people) around this work. Most of these are in the design phase (or very<br>
> > > new) and multiple parties are interested with different sets of goals in<br>
> > > mind.<br>
> > ><br>
> > > I.  Flang frontend and its integration<br>
> > ><br>
> > > II. Fortran representation in MLIR / FIR development<br>
> > ><br>
> > > III. OpenMP development for flang,  OpenMP builder in LLVM.<br>
> > ><br>
> > > IV. Loop Transformations in MLIR / LLVM with respect to OpenMP.<br>
> > ><br>
> > > It looks like the design has evolved over time and there is no one place<br>
> > > which contains the latest design decisions that fits all the different<br>
> > > pieces of the puzzle. I will try to deduce it from the above mentioned<br>
> > > references. Please correct me If I am referring to anything which has<br>
> > > changed.<br>
> > ><br>
> > > A. For most OpenMP design discussions, FIR examples are used (as seen in<br>
> > > (2) and (3)). The MLIR examples mentioned in the design only talks about<br>
> > > FIR dialect and LLVM dialect.<br>
> > ><br>
> > > This completely ignores the likes of standard, affine (where most loop<br>
> > > transformations are supposed to happen) and loop dialects. I think it is<br>
> > > critical to decouple the OpenMP dialect development in MLIR from the<br>
> > > current flang / FIR effort. It would be useful if someone can mention<br>
> > these<br>
> > > examples using existing dialects in MLIR and also how the different<br>
> > > transformations / lowerings are planned.<br>
> > ><br>
> > > B. In latest RFC(3), it is mentioned that the initial OpenMP dialect<br>
> > > version will be as follows,<br>
> > ><br>
> > >   omp.parallel {<br>
> > ><br>
> > >     omp.do {<br>
> > ><br>
> > >        fir.do %i = 0 to %ub3 : !fir.integer {<br>
> > ><br>
> > >         ...<br>
> > ><br>
> > >        }<br>
> > ><br>
> > >     }<br>
> > ><br>
> > >   }<br>
> > ><br>
> > > and then after the "LLVM conversion" it is converted as follows:<br>
> > ><br>
> > >   omp.parallel {<br>
> > ><br>
> > >     %ub3 =<br>
> > ><br>
> > >     omp.do %i = 0 to %ub3 : !llvm.integer {<br>
> > ><br>
> > >     ...<br>
> > ><br>
> > >     }<br>
> > ><br>
> > >   }<br>
> > ><br>
> > ><br>
> > > a. Is it the same omp.do operation which now contains the bounds and<br>
> > > induction variables of the loop after the LLVM conversion? If so, will<br>
> > the<br>
> > > same operation have two different semantics during a single compilation?<br>
> > ><br>
> > > b. Will there be different lowerings for various loop operations from<br>
> > > different dialects? loop.for and affine.for under omp operations would<br>
> > need<br>
> > > different OpenMP / LLVM lowerings. Currently, both of them are lowered to<br>
> > > the CFG based loops during the LLVM dialect conversion (which is much<br>
> > > before the proposed OpenMP dialect lowering).<br>
> > ><br>
> > > There would be no standard way to represent OpenMP operations (especially<br>
> > > the ones which involve loops) in MLIR. This would drastically complicate<br>
> > > lowering.<br>
> > ><br>
> > > C. It is also not mentioned how clauses like firstprivate, shared,<br>
> > private,<br>
> > > reduce, map, etc are lowered to OpenMP dialect. The example in the RFC<br>
> > > contains FIR and LLVM types and nothing about std dialect types. Consider<br>
> > > the below example:<br>
> > ><br>
> > > #pragma omp parallel for reduction(+:x)<br>
> > ><br>
> > > for (int i = 0; i < N; ++i)<br>
> > ><br>
> > >   x += a[i];<br>
> > ><br>
> > > How would the above be represented in OpenMP dialect? and What type would<br>
> > > "x" be in MLIR?  It is not mentioned in the design as to how the various<br>
> > > SSA values for various OpenMP clauses are passed around in OpenMP<br>
> > > operations.<br>
> > ><br>
> > > D. Because of (A), (B) and (C), it would be beneficial to have an omp.<br>
> > > parallel_do operation which has semantics similar to other loop<br>
> > structures<br>
> > > (may not be LoopLikeInterface) in MLIR. To me, it looks like having<br>
> > OpenMP<br>
> > > operations based on standard MLIR types and operations (scalars and<br>
> > memrefs<br>
> > > mainly) is the right way to go.<br>
> > ><br>
> > > Why not have omp.parallel_do operation with AffineMap based bounds, so as<br>
> > > to decouple it from Value/Type similar to affine.for?<br>
> > ><br>
> > > 1. With the current design, the number of transformations / optimizations<br>
> > > that one can write on OpenMP constructs would become limited as there can<br>
> > > be any custom loop structure with custom operations / types inside it.<br>
> > ><br>
> > > 2. It would also be easier to transform the Loop nests containing OpenMP<br>
> > > constructs if the body of the OpenMP operations is well defined (i.e.,<br>
> > does<br>
> > > not accept arbitrary loop structures). Having nested redundant<br>
> > "parallel" ,<br>
> > > "target" and "do" regions seems unnecessary.<br>
> > ><br>
> > > 3. There would also be new sets of loop structures in new dialects when<br>
> > > C/C++ is compiled to MLIR. It would complicate the number of possible<br>
> > > combinations inside the OpenMP region.<br>
> > ><br>
> > > E. Lowering of target constructs mentioned in ( 2(d) ) specifies direct<br>
> > > lowering to LLVM IR ignoring all the advantages that MLIR provides. Being<br>
> > > able to compile the code for heterogeneous hardware is one of the biggest<br>
> > > advantages that MLIR brings to the table. That is being completely missed<br>
> > > here. This also requires solving the problem of handling target<br>
> > information<br>
> > > in MLIR. But that is a problem which needs to be solved anyway. Using GPU<br>
> > > dialect also gives us an opportunity to represent offloading semantics in<br>
> > > MLIR.<br>
> > ><br>
> > > Given the ability to represent multiple ModuleOps and the existence of<br>
> > GPU<br>
> > > dialect, couldn't higher level optimizations on offloaded code be done at<br>
> > > MLIR level?. The proposed design would lead us to the same problems that<br>
> > we<br>
> > > are currently facing in LLVM IRProposed design seems to allow such optimizations..<br>
> > ><br>
> > > Also, OpenMP codegen will automatically benefit from the GPU dialect<br>
> > based<br>
> > > optimizations. For example, it would be way easier to hoist a memory<br>
> > > reference out of GPU kernel in MLIR than in LLVM IR.<br>
> ><br>
<br>
-- <br>
<br>
Johannes Doerfert<br>
Researcher<br>
<br>
Argonne National Laboratory<br>
Lemont, IL 60439, USA<br>
<br>
<a href="mailto:jdoerfert@anl.gov" target="_blank">jdoerfert@anl.gov</a><br>
</blockquote></div></div>