<div dir="ltr">Where can we more details of FIR (or a dialect of MLIR)?<div><br></div><div>thanks,</div><div>-Prashanth</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 4, 2019 at 3:33 AM Kiran Chandramohan <<a href="mailto:Kiran.Chandramohan@arm.com">Kiran.Chandramohan@arm.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">




<div dir="ltr">
<div id="gmail-m_-1550589964373014788divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<div id="gmail-m_-1550589964373014788divtagdefaultwrapper" dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p style="margin-top:0px;margin-bottom:0px"></p>
<div>
<div><span style="font-size:12pt"><br>
A walkthrough for the collapse clause on an OpenMP loop construct is given below. This is an example where the transformation (collapse) is performed in the MLIR layer itself.<br>
<br>
</span></div>
<div><span style="font-size:12pt">1)Fortran OpenMP code with collapse <br>
!$omp parallel do private(j) collapse(2)</span><br>
</div>
<div>do i=lb1,ub1</div>
<div>  do j=lb2,ub2</div>
<div>    ...</div>
<div>    ...</div>
<div>  end do</div>
<div>end do</div>
<div><br>
</div>
2) The Fortran source with OpenMP will be converted to an AST by the F18 parser. Parse tree not shown here to keep it short.<br>
<br>
3) <br>
3.a)The Parse tree will be lowered to a mix of FIR and OpenMP dialects. There are omp.parallel and omp.do operations in the OpenMP dialect which represents parallel and OpenMP loop constructs. The omp.do operation has an attribute "collapse" which specifies
 the number of loops to be collapsed.<br>
omp.parallel {</div>
<div>  omp.do {collapse = 2} %i = %lb1 to %ub1 : !fir.integer {</div>
<div>    fir.do %j = %lb2 to %ub2 : !fir.integer {</div>
<div>    ...</div>
<div><span style="font-size:12pt">    }</span></div>
<div>  }</div>
<div><span style="font-size:12pt">}</span></div>
<div><br>
3.b) A transformation pass in MLIR will perform the collapsing. The collapse operation will cause the omp.do loop to be coalesced with the loop immediately following it. Note : There exists loop coalescing passes in MLIR transformation passes. We should try
 to make use of it.</div>
<div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
omp.parallel {</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
  %ub3 = </div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
  omp.do %i = 0 to %ub3 : !fir.integer {</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
    ...</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
  }</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
}</div>
<br>
4) Next conversion will be to a mix of LLVM and OpenMP dialect.</div>
<div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
omp.parallel {</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
  <span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">%ub3 = </span></div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
  omp.do %i = 0 to %ub3 : !llvm.integer {</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
    ...</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
  }</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
}</div>
</div>
<div><br>
5) Finally, LLVM IR will be generated for this code. The translation to LLVM IR can make use of the OpenMP IRBuilder. <span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">LLVM
 IR not shown here to keep it short.</span></div>
<div><br>
</div>
<div>Thanks,<br>
Kiran</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
</div>
<br>
<p></p>
</div>
<hr style="display:inline-block;width:98%">
<div id="gmail-m_-1550589964373014788divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Kiran Chandramohan <<a href="mailto:Kiran.Chandramohan@arm.com" target="_blank">Kiran.Chandramohan@arm.com</a>><br>
<b>Sent:</b> 21 August 2019 13:15:04<br>
<b>To:</b> Eric Schweitz (PGI) <<a href="mailto:eric.schweitz@pgroup.com" target="_blank">eric.schweitz@pgroup.com</a>>; <a href="mailto:flang-dev@lists.flang-compiler.org" target="_blank">flang-dev@lists.flang-compiler.org</a> <<a href="mailto:flang-dev@lists.flang-compiler.org" target="_blank">flang-dev@lists.flang-compiler.org</a>>; <a href="mailto:flang-dev@lists.llvm.org" target="_blank">flang-dev@lists.llvm.org</a> <<a href="mailto:flang-dev@lists.llvm.org" target="_blank">flang-dev@lists.llvm.org</a>><br>
<b>Cc:</b> nd <<a href="mailto:nd@arm.com" target="_blank">nd@arm.com</a>><br>
<b>Subject:</b> Re: Flang Technical Call : Summary of presentation on OpenMP for Flang</font>
<div> </div>
</div>
<div dir="ltr">
<div id="gmail-m_-1550589964373014788x_divtagdefaultwrapper" dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<div id="gmail-m_-1550589964373014788x_divtagdefaultwrapper" dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p style="margin-top:0px;margin-bottom:0px">Thanks, Eric for the clarification.<br>
<br>
Also, sharing this write up of the flow through the compiler for an OpenMP construct. The first one (Proposed Plan) is as per the presentation. The second one (Modified Plan) incorporates Eric's feedback to lower the F18 AST to a mix of OpenMP and FIR dialect.<br>
<br>
<br>
<b>I Proposed plan</b><br>
</p>
<div>1) Example OpenMP code
<br>
<Fortran code><br>

!$omp parallel</div>
<div>
c = a + b
<br>
!$omp end parallel
</div>
<div><More Fortran code>
 
<br>
<br>
2) Parse tree (Copied relevant section from -fdebug-dump-parse-tree)</div>
<div><Fortran parse tree>
| | ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPBlockConstruct</div>
<div>| | | OmpBlockDirective -> Directive = Parallel</div>
<div>| | | OmpClauseList -></div>
<div>| | | Block</div>
<div>| | | | ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> AssignmentStmt</div>
<div>| | | | | Variable -> Designator -> DataRef -> Name = 'c'</div>
<div>| | | | | Expr -> Add</div>
<div>| | | | | | Expr -> Designator -> DataRef -> Name = 'a'</div>
<div>| | | | | | Expr -> Designator -> DataRef -> Name = 'b'</div>
<div>| | | OmpEndBlockDirective -> OmpBlockDirective -> Directive = Parallel</div>
<div><More Fortran parse tree></div>
<div> </div>
<div>3) The first lowering will be to FIR dialect and the dialect has a pass-through operation for OpenMP. This operation has a nested region which contains the region of code influenced by the OpenMP directive. The contained region will have other FIR (or
 standard dialect) operations.
<br>
Mlir.region(…) {
  <br>
%1 = fir.x(…)
  …
  <br>
%20 = fir.omp attribute:parallel {</div>
<div>        %1 = addf %2, %3 : f32</div>
<div>        }</div>
<div> %21 = <more fir>
 </div>
<div>…
</div>
<div>}
</div>
<div><br>
</div>
<div>4) The next lowering will be to OpenMP and LLVM dialect. <span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">The OpenMP
 dialect has an operation called parallel with a nested </span><span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">region
 of code</span><span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">. The </span><span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">nested</span><span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px"> region
 will have</span><span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px"> llvm dialect operations.</span></div>
<div>
Mlir.region(…) {
  <br>
%1 = <a href="http://llvm.xyz" target="_blank">llvm.xyz</a>(...)
  …
  </div>
<div>%20 = omp.parallel {</div>
<div>        %1 = llvm.fadd %2, %3 : !llvm.float</div>
<div>        }</div>
<div> %21 = <more llvm dialect>
 </div>
<div>…
</div>
<div>}</div>
<div><br>
</div>
<div>
5) The next conversion will be to LLVM IR. Here the OpenMP dialect will be lowered using the OpenMP IRBuilder and the translation library of the LLVM dialect. The IR Builder will see that there is a region under the OpenMP construct omp.parallel. It will
 collect all the basic blocks inside that region and then generate outlined code using those basic blocks. Suitable calls will be inserted to the OpenMP API. 
 
</div>
<div><br>
</div>
<div>d<span style="font-size:12pt">efine @outlined_parallel_fn(...)</span></div>
<div>{</div>
<div>  ....</div>
<div>  %1 = fadd float %2, %3</div>
<div>  ...</div>
<div>}</div>
<div> 
define @xyz(…)</div>
<div>{   </div>
<div>  %1 = alloca float</div>
<div></div>
<div>  ....</div>
<div>  call kmpc_fork_call(...,outlined_parallel_fn,...)</div>
<div>}</div>
<br>
<b style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">II Modified plan</b><br style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
<div><span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px"><br>
The differences are only in steps 3 and 4. Other steps remain the same.<br>
<br>
</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
3) The first lowering will be to a mix of FIR dialect and OpenMP dialect. The OpenMP dialect has an operation called parallel with a nested region of code. The nested region will have FIR (and standard dialect) operations.
<br>
Mlir.region(…) {
  <br>
%1 = fir.x(…)
  …
  <br>
%20 = omp.parallel {</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
        %1 = addf %2, %3 : f32</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
        }</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
 %21 = <more fir>
 </div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
…
</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
}
</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
4) The next lowering will be to OpenMP and LLVM dialect</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">

Mlir.region(…) {
  <br>
%1 = <a href="http://llvm.xyz" target="_blank">llvm.xyz</a>(...)
  …
  </div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
%20 = omp.parallel {</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
        %1 = llvm.fadd %2, %3 : !llvm.float</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
        }</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
 %21 = <more llvm dialect>
 </div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
…
</div>
<div style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
}</div>
<div><br>
</div>
Thanks,<br>
Kiran<br>
<p></p>
<p></p>
</div>
<hr style="display:inline-block;width:98%">
<div id="gmail-m_-1550589964373014788x_divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Eric Schweitz (PGI) <<a href="mailto:eric.schweitz@pgroup.com" target="_blank">eric.schweitz@pgroup.com</a>><br>
<b>Sent:</b> 19 August 2019 17:35:04<br>
<b>To:</b> Kiran Chandramohan <<a href="mailto:Kiran.Chandramohan@arm.com" target="_blank">Kiran.Chandramohan@arm.com</a>>; <a href="mailto:flang-dev@lists.flang-compiler.org" target="_blank">flang-dev@lists.flang-compiler.org</a> <<a href="mailto:flang-dev@lists.flang-compiler.org" target="_blank">flang-dev@lists.flang-compiler.org</a>><br>
<b>Subject:</b> RE: Flang Technical Call : Summary of presentation on OpenMP for Flang</font>
<div> </div>
</div>
<div lang="EN-US">
<div class="gmail-m_-1550589964373014788x_x_WordSection1">
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Re: And would like the FIR to have nothing to do with OpenMP.</span></p>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">This seems stronger than what I meant, so I’ll clarify a bit.</span></p>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">FIR should have no dependence on OpenMP, since it is possible to write (vanilla) Fortran programs without OpenMP.  However FIR clearly must also co-exist with an OpenMP dialect.  Stated another
 way, we don’t want a circular dependence between “vanilla Fortran” and OpenMP.  Since OpenMP is a directive-based meta-language that impacts the code gen of a program (whether it is Fortran or some other language), it seems quite natural and necessary that
 OpenMP be able to operate upon the substrate language  (whether that substrate is FIR or LLVM IR or something else). Fortunately, MLIR allows one to mix dialects, and thus each dialect can focus on the problems it’s trying to solve.</span></p>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">--</span></p>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Eric</span></p>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"></span></p>
<div>
<div style="border-style:solid none none;border-top-width:1pt;border-top-color:rgb(225,225,225);padding:3pt 0in 0in">
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><b>From:</b> flang-dev <<a href="mailto:flang-dev-bounces@lists.flang-compiler.org" target="_blank">flang-dev-bounces@lists.flang-compiler.org</a>>
<b>On Behalf Of </b>Kiran Chandramohan<br>
<b>Sent:</b> Friday, August 16, 2019 9:13 AM<br>
<b>To:</b> <a href="mailto:flang-dev@lists.flang-compiler.org" target="_blank">flang-dev@lists.flang-compiler.org</a><br>
<b>Subject:</b> [Flang-dev] Flang Technical Call : Summary of presentation on OpenMP for Flang</p>
</div>
</div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"> </p>
<div id="gmail-m_-1550589964373014788x_x_divtagdefaultwrapper">
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Hi,</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">This mail is a summary of the presentation that I gave on 31st August about supporting OpenMP in Flang (F18). Link to presentation: <a href="https://drive.google.com/open?id=1Q2Y2fIavewq9oxRDruWoi8TaItlq1g9l" target="_blank">https://drive.google.com/open?id=1Q2Y2fIavewq9oxRDruWoi8TaItlq1g9l</a></span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">The mail follows the contents of the presentation and should be read in conjunction with the slides. It also includes feedback provided during and outside the presentation. It would be great
 to receive further feedback.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">This presentation discusses the design of how OpenMP can be represented in the IR and how it can be translated to LLVM IR. The design is intended to be modular so that other frontends (C/C++)
 can reuse it in the future. The design should also enable reuse of OpenMP code from Clang (LLVM IR generation, Outlining etc). It should also be acceptable to Flang, MLIR, Clang communities. And most importantly should allow for any OpenMP optimisation desired.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">The designs presented uses the following two components.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">a) MLIR [3]: Necessary since MLIR has already been chosen as the framework for building the Fortran IR (FIR) for Flang.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">b) OpenMP IRBuilder [2]: For sharing of OpenMP code with Clang.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">The current sequential code flow in Flang (Slide 5) can be summarised as follows,</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">[Fortran code] -> Parser -> [AST] -> Lowering -> [FIR MLIR] -> Conversion -> [LLVM MLIR] -> Translation -> [LLVM IR]</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Four design plans are presented. These plans add OpenMP support to the sequential code flow. All the plans assume that FIR is augmented with a pass-through operation which represents OpenMP
 constructs. </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Plan 1 (Slide 6)</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">The first plan extends LLVM MLIR with OpenMP operations. The pass-through OpenMP operation in FIR is converted to the OpenMP operation in LLVM MLIR. The OpenMP operation in LLVM MLIR can be
 converted to LLVM IR during translation. The translation process can call the OpenMP IR Builder to generated the LLVM IR code.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Pros: Easy to implement, Can reuse Clang OpenMP code.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Cons: Not acceptable to MLIR community [1]. By design LLVM MLIR should be similar to LLVM IR (OpenMP is a form of parallelism. Has concepts of nested regions etc which do not map directly
 to LLVM IR and hence to LLVM MLIR).</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Plan 2 (Slide 7)</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">In the second plan, LLVM MLIR is not extended. Here the pass-through OpenMP operation in FIR has to be converted to existing LLVM MLIR. This would involve outlining, privatisation etc during
 the conversion of FIR to LLVM MLIR.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Pros: Easy to implement.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Cons: Cannot be shared with other users (C/C++) and non-LLVM targets (accelerators). Cannot re-use Clang OpenMP code.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Plan 3 (Slide 8)</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">The third plan defines a separate MLIR dialect for OpenMP. The pass-through OpenMP operation in FIR can be converted to Operations in the OpenMP MLIR dialect. OpenMP specific optimisations
 can be performed in this dialect. This dialect will be lowered to the LLVM dialect and outlining etc will happen at this time.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Pros: MLIR dialect can be used by other users. Acceptable to MLIR community [1].</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Cons: Cannot reuse code from Clang for OpenMP</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Plan 4: The proposed design (Slide 9)</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">The proposed design also involves creating an OpenMP dialect in MLIR. The difference lies in the fact that the OpenMP dialect has some ties to the LLVM dialect like sharing the types and using
 the translation library of the LLVM dialect. The Fortran IR (FIR) will be lowered to a mix of OpenMP and LLVM dialect. The translation library of LLVM dialect will be extended to create a new translation library which can then lower the mix to LLVM IR. The
 new translation library can make use of the LLVM IRBuilder.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Pros: Other users (C/C++) can use it. Can reuse Clang OpenMP code. Acceptable to MLIR community [1].</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Cons: Has some ties to the LLVM dialect. </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">OpenMP dialect</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">The proposed plan involves writing an MLIR dialect for OpenMP. An MLIR dialect has types and operations. Types will be based on the LLVM dialect. Operations will be a mix of fine and coarse-grained.
 e.g. Coarse: omp.parallel, omp.target, Fine: omp.flush. The detailed design of the dialect is TBD and will be based on what should be optimized. The OpenMP dialect creation in the proposed plan is from FIR, this would require pass-through OpenMP operations
 in FIR. Another approach would be to lower F18 AST with OpenMP directly to a mix of OpenMP and FIR dialects. This would require that FIR co-exist with the OpenMP dialect.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"><br>
Next Steps</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">-> Address feedback provided after the presentation. And reach an agreement on a plan<br>
-> Design a minimum viable MLIR dialect and get community acceptance.<br>
-> Help with progressing the OpenMP IRBuilder.<br>
-> Implement the accepted plan. Implementation would follow a vertical plan (going construct by construct).</span></p>
</div>
<blockquote style="margin-left:30pt;margin-right:0in">
<div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">-> Represent construct in OpenMP MLIR</span></p>
</div>
</div>
<div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">-> Refactor the code for the construct in OpenMP IRBuilder</span></p>
</div>
</div>
<div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">-> Set up the translation library for OpenMP in MLIR to call the IRBuilder</span></p>
</div>
</div>
<div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">-> Set up the transformation from the frontend to OpenMP MLIR for this construct</span></p>
</div>
</div>
</blockquote>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"> </span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Feedback (during the call):</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">-> Johaness agrees with this proposed plan (Plan 4) and does not see the ties to LLVM as being a disadvantage.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">-> Hal Finkel informs that the OpenMP IRBuilder proposal does not have any opposition in the Clang community and hence that probably is not a major risk.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Feedback (other):</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">-> Eric Schweitz: "I like the idea of adding OpenMP as its own dialect.  I think the design should allow this dialect to coexist with FIR and other dialects.  The dialect should be informed
 by the specific optimizations you plan to perform on the OpenMP semantic level, of course." Would prefer to lower F18 AST to a mix of OpenMP and FIR dialects. And would like the FIR to have nothing to do with OpenMP.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">-> Steve Scalpone: "I think the design needs to be vetted against some of the scenarios that’ll be encountered when generating executable OpenMP code.  I have no reason to doubt what you’ve
 proposed will work but I do think it is important to consider how certain scenarios will be implemented.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">Some topics to consider:</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">- How to implement loop collapse?</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">- How to implement loop distribution?</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">- How to implement simd?</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">- Is an outliner needed?  In MLIR, before, after?  Model to reference shared variables?</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">- How will target offload be implemented?</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">- Where will the OpenMP runtime API be exposed?"<br>
<br>
References</span></p>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black">1. OpenMP MLIR dialect discussion.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"><a href="https://groups.google.com/a/tensorflow.org/forum/#!topic/mlir/4Aj_eawdHiw" target="_blank">https://groups.google.com/a/tensorflow.org/forum/#!topic/mlir/4Aj_eawdHiw</a><br>
2. Johaness’ OpenMP IRBuilder proposal.</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"><a href="http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/2019-May/000197.html" target="_blank">http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/2019-May/000197.html</a><br>
3. MLIR</span></p>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal"><span style="font-size:12pt;color:black"><a href="https://github.com/tensorflow/mlir" target="_blank">https://github.com/tensorflow/mlir</a><br>
<br>
Thanks,<br>
Kiran</span></p>
</div>
</div>
</div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal">IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other
 person, use it for any purpose, or store or copy the information in any medium. Thank you.
</p>
<div>
<div class="gmail-m_-1550589964373014788x_x_MsoNormal" align="center" style="text-align:center">
<hr size="2" width="100%" align="center">
</div>
</div>
<div>
<p class="gmail-m_-1550589964373014788x_x_MsoNormal">This email message is for the sole use of the intended recipient(s) and may contain confidential information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact
 the sender by reply email and destroy all copies of the original message. </p>
</div>
<div>
<div class="gmail-m_-1550589964373014788x_x_MsoNormal" align="center" style="text-align:center">
<hr size="2" width="100%" align="center">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

_______________________________________________<br>
flang-dev mailing list<br>
<a href="mailto:flang-dev@lists.flang-compiler.org" target="_blank">flang-dev@lists.flang-compiler.org</a><br>
<a href="http://lists.flang-compiler.org/mailman/listinfo/flang-dev_lists.flang-compiler.org" rel="noreferrer" target="_blank">http://lists.flang-compiler.org/mailman/listinfo/flang-dev_lists.flang-compiler.org</a><br>
</blockquote></div>