<div dir="ltr">+1, tokens are the current True Way to create single-entry multi-exit regions. Your example for an annotated loop would look like:<div><br></div><div><span style="font-size:12.8px">%region = call token @llvm.openmp.regionstart(metadata ...) ; whatever parameters you need here</span><br style="font-size:12.8px"><span style="font-size:12.8px">  loop</span><br style="font-size:12.8px"><span style="font-size:12.8px">call void @llvm.openmp.regionend(token %region)</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">If you use tokens, I would recommend proposal (c), where you introduce new intrinsics for every new kind of region, instead of adding one overly generic set of region intrinsics.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">We already have a way to form regions with real barriers, and it's tokens.</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 11, 2017 at 2:17 PM, David Majnemer via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">FWIW, we needed to maintain single entry-multiple exit regions for WinEH and we accomplished it via a different mechanism.<div><br></div><div>We had an instruction which produces a value of type Token (<a href="http://llvm.org/docs/LangRef.html#token-type" target="_blank">http://llvm.org/docs/LangRef.<wbr>html#token-type</a>) which let us establish the region and another instruction to exit the region by consuming it. The dominance rules allowed us to avoid situations where the compiler might trash the regions in weird ways and made sure that regions would be left unharmed.</div><div><br></div><div>AFAIK, a similar approach using Token could work here. I think it would reduce the amount of stuff you'd need LLVM to maintain.</div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 11, 2017 at 2:02 PM, Hal Finkel via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">A Proposal for adding an experimental IR-level region-annotation infrastructure<br>
==============================<wbr>==============================<wbr>================= <br>
Hal Finkel (ANL) and Xinmin Tian (Intel)<br>
<br>
This is a proposal for adding an experimental infrastructure to support<br>
annotating regions in LLVM IR, making use of intrinsics and metadata, and<br>
a generic analysis to allow transformations to easily make use of these<br>
annotated regions. This infrastructure is flexible enough to support<br>
representation of directives for parallelization, vectorization, and<br>
offloading of both loops and more-general code regions. Under this scheme,<br>
the conceptual distance between source-level directives and the region<br>
annotations need not be significant, making the incremental cost of<br>
supporting new directives and modifiers often small. It is not, however,<br>
specific to those use cases.<br>
<br>
Problem Statement<br>
=================<br>
There are a series of discussions on LLVM IR extensions for representing region<br>
and loop annotations for parallelism, and other user-guided transformations,<br>
among both industrial and academic members of the LLVM community. Increasing<br>
the quality of our OpenMP implementation is an important motivating use case,<br>
but certainly not the only one. For OpenMP in particular, we've discussed<br>
having an IR representation for years. Presently, all OpenMP pragmas are<br>
transformed directly into runtime-library calls in Clang, and outlining (i.e.<br>
extracting parallel regions into their own functions to be invoked by the<br>
runtime library) is done in Clang as well. Our implementation does not further<br>
optimize OpenMP constructs, and a lot of thought has been put into how we might<br>
improve this. For some optimizations, such as redundant barrier removal, we<br>
could use a TargetLibraryInfo-like mechanism to recognize frontend-generated<br>
runtime calls and proceed from there. Dealing with cases where we lose<br>
pointer-aliasing information, information on loop bounds, etc. we could improve<br>
by improving our inter-procedural-analysis capabilities. We should do that<br>
regardless. However, there are important cases where the underlying scheme we<br>
want to use to lower the various parallelism constructs, especially when<br>
targeting accelerators, changes depending on what is in the parallel region.<br>
In important cases where we can see everything (i.e. there aren't arbitrary<br>
external calls), code generation should proceed in a way that is very different<br>
from the general case. To have a sensible implementation, this must be done<br>
after inlining. When using LTO, this should be done during the link-time phase.<br>
As a result, we must move away from our purely-front-end based lowering scheme.<br>
The question is what to do instead, and how to do it in a way that is generally<br>
useful to the entire community.<br>
<br>
Designs previously discussed can be classified into four categories:<br>
<br>
(a) Add a large number of new kinds of LLVM metadata, and use them to annotate<br>
    each necessary instruction for parallelism, data attributes, etc.<br>
(b) Add several new LLVM instructions such as, for parallelism, fork, spawn,<br>
    join, barrier, etc.<br>
(c) Add a large number of LLVM intrinsics for directives and clauses, each<br>
    intrinsic representing a directive or a clause.<br>
(d) Add a small number of LLVM intrinsics for region or loop annotations,<br>
    represent the directive/clause names using metadata and the remaining<br>
    information using arguments.<br>
<br>
Here we're proposing (d), and below is a brief pros and cons analysis based on<br>
these discussions and our own experiences of supporting region/loop annotations<br>
in LLVM-based compilers. The table below shows a short summary of our analysis.<br>
<br>
Various commercial compilers (e.g. from Intel, IBM, Cray, PGI), and GCC [1,2],<br>
have IR-level representations for parallelism constructs. Based on experience<br>
from these previous developments, we'd like a solution for LLVM that maximizes<br>
optimization enablement while minimizing the maintenance costs and complexity<br>
increase experienced by the community as a whole.<br>
<br>
Representing the desired information in the LLVM IR is just the first step. The<br>
challenge is to maintain the desired semantics without blocking useful<br>
optimizations. With options (c) and (d), dependencies can be preserved mainly<br>
based on the use/def chain of the arguments of each intrinsic, and a manageable<br>
set LLVM analysis and transformations can be made aware of certain kinds of<br>
annotations in order to enable specific optimizations. In this regard,<br>
options (c) and (d) are close with respect to maintenance efforts. However,<br>
based on our experiences, option (d) is preferable because it is easier to<br>
extend to support new directives and clauses in the future without the need to<br>
add new intrinsics as required by option (c).<br>
<br>
Table 1. Pros/cons summary of LLVM IR experimental extension options<br>
<br>
--------+---------------------<wbr>-+----------------------------<wbr>------------------- <br>
Options |         Pros         | Cons<br>
--------+---------------------<wbr>-+----------------------------<wbr>------------------- <br>
(a)     | No need to add new   | LLVM passes do not always maintain metadata.<br>
        | instructions or      | Need to educate many passes (if not all) to<br>
        | new intrinsics       | understand and handle them.<br>
--------+---------------------<wbr>-+----------------------------<wbr>------------------- <br>
(b)     | Parallelism becomes  | Huge effort for extending all LLVM passes and<br>
        | first class citizen  | code generation to support new instructions.<br>
        |                      | A large set of information still needs to be<br>
        |                      | represented using other means.<br>
--------+---------------------<wbr>-+----------------------------<wbr>------------------- <br>
(c)     | Less impact on the   | A large number of intrinsics must be added.<br>
        | exist LLVM passes.   | Some of the optimizations need to be<br>
        | Fewer requirements   | educated to understand them.<br>
        | for passes to        |<br>
        | maintain metadata.   |<br>
--------+---------------------<wbr>-+----------------------------<wbr>------------------- <br>
(d)     | Minimal impact on    | Some of the optimizations need to be<br>
        | existing LLVM        | educated to understand them.<br>
        | optimizations passes.| No requirements for all passes to maintain<br>
        | directive and clause | large set of metadata with values.<br>
        | names use metadata   |<br>
        | strings.             |<br>
--------+---------------------<wbr>-+----------------------------<wbr>------------------- <br>
<br>
Regarding (a), LLVM already uses metadata for certain loop information (e.g.<br>
annotations directing loop transformations and assertions about loop-carried<br>
dependencies), but there is no natural or consistent way to extend this scheme<br>
to represent necessary data-movement or region information.<br>
<br>
<br>
New Intrinsics for Region and Value Annotations<br>
==============================<wbr>================<br>
The following new (experimental) intrinsics are proposed which allow:<br>
<br>
a) Annotating a code region marked with directives / pragmas,<br>
b) Annotating values associated with the region (or loops), that is, those<br>
   values associated with directives / pragmas.<br>
c) Providing information on LLVM IR transformations needed for the annotated<br>
   code regions (or loops).<br>
<br>
These can be used both by frontends and also by transformation passes (e.g.<br>
automated parallelization). The names used here are similar to those used by<br>
our internal prototype, but obviously we expect a community bikeshed<br>
discussion.<br>
<br>
def int_experimental_directive : Intrinsic<[], [llvm_metadata_ty],<br>
                                   [IntrArgMemOnly],<br>
"llvm.experimental.directive"><wbr>;<br>
<br>
def int_experimental_dir_qual : Intrinsic<[], [llvm_metadata_ty],<br>
[IntrArgMemOnly],<br>
"llvm.experimental.dir.qual">;<br>
<br>
def int_experimental_dir_qual_opnd : Intrinsic<[],<br>
[llvm_metadata_ty, llvm_any_ty],<br>
[IntrArgMemOnly],<br>
"llvm.experimental.dir.qual.op<wbr>nd">;<br>
<br>
def int_experimental_dir_qual_opnd<wbr>list : Intrinsic<<br>
                                        [],<br>
[llvm_metadata_ty, llvm_vararg_ty],<br>
[IntrArgMemOnly],<br>
"llvm.experimental.dir.qual.op<wbr>ndlist">;<br>
<br>
Note that calls to these intrinsics might need to be annotated with the<br>
convergent attribute when they represent fork/join operations, barriers, and<br>
similar.<br>
<br>
Usage Examples<br>
==============<br>
<br>
This section shows a few examples using these experimental intrinsics.<br>
LLVM developers who will use these intrinsics can defined their own MDstring.<br>
All details of using these intrinsics on representing OpenMP 4.5 constructs are described in [1][3].<br>
<br>
<br>
Example I: An OpenMP combined construct<br>
<br>
#pragma omp target teams distribute parallel for simd<br>
  loop<br>
<br>
LLVM IR<br>
-------<br>
call void @llvm.experimental.directive(m<wbr>etadata !0)<br>
call void @llvm.experimental.directive(m<wbr>etadata !1)<br>
call void @llvm.experimental.directive(m<wbr>etadata !2)<br>
call void @llvm.experimental.directive(m<wbr>etadata !3)<br>
  loop<br>
call void @llvm.experimental.directive(m<wbr>etadata !6)<br>
call void @llvm.experimental.directive(m<wbr>etadata !5)<br>
call void @llvm.experimental.directive(m<wbr>etadata !4)<br>
<br>
!0 = metadata !{metadata !DIR.OMP.TARGET}<br>
!1 = metadata !{metadata !DIR.OMP.TEAMS}<br>
!2 = metadata !{metadata !<a href="http://DIR.OMP.DISTRIBUTE.PARLOOP.SI" target="_blank">DIR.OMP.DISTRIBUTE.PARLOOP.SI</a><wbr>MD}<br>
<br>
!6 = metadata !{metadata !DIR.OMP.END.DISTRIBUTE.PARLOO<wbr>P.SIMD}<br>
!5 = metadata !{metadata !DIR.OMP.END.TEAMS}<br>
!4 = metadata !{metadata !DIR.OMP.END.TARGET}<br>
<br>
Example II: Assume x,y,z are int variables, and s is a non-POD variable.<br>
            Then, lastprivate(x,y,s,z) is represented as:<br>
<br>
LLVM IR<br>
-------<br>
call void @llvm.experimental.dir.qual.op<wbr>ndlist(<br>
                metadata !1, %x, %y, metadata !2, %a, %ctor, %dtor, %z)<br>
<br>
!1 = metadata !{metadata !QUAL.OMP.PRIVATE}<br>
!2 = metadata !{metadata !QUAL.OPND.NONPOD}<br>
<br>
Example III: A prefetch pragma example<br>
<br>
// issue vprefetch1 for xp with a distance of 20 vectorized iterations ahead<br>
// issue vprefetch0 for yp with a distance of 10 vectorized iterations ahead<br>
#pragma prefetch x:1:20 y:0:10<br>
for (i=0; i<2*N; i++) { xp[i*m + j] = -1; yp[i*n +j] = -2; }<br>
<br>
LLVM IR<br>
-------<br>
call void @llvm.experimental.directive(m<wbr>etadata !0)<br>
call void @llvm.experimental.dir.qual.op<wbr>nslist(metadata !1, %xp, 1, 20,<br>
                                               metadata !1, %yp, 0, 10)<br>
  loop<br>
call void @llvm.experimental.directive(m<wbr>etadata !3)<br>
<br>
References<br>
==========<br>
<br>
[1] LLVM Framework and IR extensions for Parallelization, SIMD Vectorization<br>
    and Offloading Support. SC'2016 LLVM-HPC3 Workshop. (Xinmin Tian <a href="http://et.al" rel="noreferrer" target="_blank">et.al</a>.)<br>
    Saltlake City, Utah.<br>
<br>
[2] Extending LoopVectorizer towards supporting OpenMP4.5 SIMD and outer loop<br>
    auto-vectorization. (Hideki Saito, <a href="http://et.al" rel="noreferrer" target="_blank">et.al</a>.) LLVM Developers' Meeting 2016,<br>
    San Jose.<br>
<br>
[3] Intrinsics, Metadata, and Attributes: The Story continues! (Hal Finkel)<br>
    LLVM Developers' Meeting, 2016. San Jose<br>
<br>
[4] LLVM Intrinsic Function and Metadata String Interface for Directive (or<br>
    Pragmas) Representation. Specification Draft v0.9, Intel Corporation, 2016.<br>
<br>
<br>
Acknowledgements<br>
================<br>
We would like to thank Chandler Carruth (Google), Johannes Doerfert (Saarland<br>
Univ.), Yaoqing Gao (HuaWei), Michael Wong (Codeplay), Ettore Tiotto,<br>
Carlo Bertolli, Bardia Mahjour (IBM), and all other LLVM-HPC IR Extensions WG<br>
members for their constructive feedback on the LLVM framework and IR extension<br>
proposal.<br>
<br>
Proposed Implementation<br>
=======================<br>
<br>
Two sets of patches of supporting these experimental intrinsics and demonstrate<br>
the usage are ready for community review.<br>
<br>
a) Clang patches that support core OpenMP pragmas using this approach.<br>
b) W-Region framework patches: CFG restructuring to form single-entry-<br>
   single-exit work region (W-Region) based on annotations, Demand-driven<br>
   intrinsic parsing, and WRegionInfo collection and analysis passes,<br>
   Dump functions of WRegionInfo.<br>
<br>
On top of this functionality, we will provide the transformation patches for<br>
core OpenMP constructs (e.g. start with "#pragma omp parallel for" loop for<br>
lowering and outlining, and "#pragma omp simd" to hook it up with<br>
LoopVectorize.cpp). We have internal implementations for many constructs now.<br>
We will break this functionality up to create a series of patches for<br>
community review.<span class="m_8178307607774293309HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Hal Finkel<br>
Lead, Compiler Technology and Programming Languages<br>
Leadership Computing Facility<br>
Argonne National Laboratory<br>
<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</font></span></blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>