public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Frederik Harwath <frederik.harwath@siemens.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Frederik Harwath <frederik@codesourcery.com>,
	gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org,
	tobias@codesourcery.com, joseph@codesourcery.com,
	jason@redhat.com
Subject: Re: [PATCH 0/7] openmp: OpenMP 5.1 loop transformation directives
Date: Wed, 17 May 2023 13:55:00 +0200	[thread overview]
Message-ID: <8b53269a-c385-52d1-b862-8a4b904ce66d@siemens.com> (raw)
In-Reply-To: <ZGNiUS9h+sk7SjOe@tucnak>

Hi Jakub,

On 16.05.23 13:00, Jakub Jelinek wrote:
> On Tue, May 16, 2023 at 11:45:16AM +0200, Frederik Harwath wrote:
>> The place where different compilers implement the loop transformations
>> was discussed in an OpenMP loop transformation meeting last year. Two
>> compilers (another one and GCC with this patch series) transformed 
>> the loops
>> in the middle end after the handling of data sharing, one planned to 
>> do so.
>> Yet another vendor had not yet decided where it will be implemented. 
>> Clang
>> currently does everything in the front end, but it was mentioned that 
>> this
>> might change in the future e.g. for code sharing with Flang. Implementing
>> the loop transformations late could potentially
>> complicate the implementation of transformations which require 
>> adjustments
>> of the data sharing clauses, but this is known and consequentially, 
>> no such
> When already in the FE we determine how many canonical loops a particular
> loop transformation creates, I think the primary changes I'd like to 
> see is
> really have OMP_UNROLL/OMP_TILE GENERIC statements (see below) and 
> consider
> where is the best spot to lower it. I believe for data sharing it is best
> done during gimplification before the containing loops are handled, it is
> already shared code among all the FEs, I think will make it easier to 
> handle
> data sharing right and gimplification is also where doacross processing is
> done. While there is restriction that ordered clause is incompatible with
> generated loops from tile construct, there isn't one for unroll (unless
> "The ordered clause must not appear on a worksharing-loop directive if 
> the associated loops
> include the generated loops of a tile directive."
> means unroll partial implicitly because partial unroll tiles the loop, but
> it doesn't say it acts as if it was a tile construct), so we'd have to 
> handle
> #pragma omp for ordered(2)
> for (int i = 0; i < 64; i++)
> #pragma omp unroll partial(4)
> for (int j = 0; j < 64; j++)
> {
> #pragma omp ordered depend (sink: i - 1, j - 2)
> #pragma omp ordered depend (source)
> }
> and I think handling it after gimplification is going to be increasingly
> harder. Of course another possibility is ask lang committee to clarify
> unless it has been clarified already in 6.0 (but in TR11 it is not).

I do not really expect that we will have to handle this. Questions 
concerning
the correctness of code after applying loop transformations came up several
times since I have been following the design meetings and the result was
always either that nothing will be changed, because the loop transformations
are not expected to ensure the correctness of enclosing directives, or that
the use of the problematic construct in conjunction with loop 
transformations
will be forbidden. Concerning the use of "ordered" on transformed loops, the
latter approach was suggested for all transformations, cf. issue #3494 
in the
private OpenMP spec repository. I see that you have already asked for 
clarification
on unroll. I suppose this could also be fixed after gimplification with
reasonable effort. But let's just wait for the result of that discussion 
before we
continue worrying about this.

> Also, I think creating temporaries is easier to be done during
> gimplification than later.

This has not caused problems with the current approach.

> Another option is as you implemented a separate pre-omp-lowering pass,
> and another one would be do it in the omplower pass, which has actually
> several subpasses internally, do it in the scan phase. Disadvantage of
> a completely separate pass is that we have to walk the whole IL again,
> while doing it in the scan phase means we avoid that cost. We already
> do there similar transformations, scan_omp_simd transforms simd constructs
> into if (...) simd else simt and then we process it with normal 
> scan_omp_for
> on what we've created. So, if you insist doing it after gimplification
> perhaps for compatibility with other non-LLVM compilers, I'd prefer to
> do it there rather than in a completely separate pass.

I see. This would be possible. My current approach is indeed rather
wasteful because the pass is not restricted to functions that actually
use loop transformations. I could add an attribute to such functions
that could be used to avoid the execution of the pass and hence
the gimple walk on functions that do not use transformations.

>> This is necessary to represent the loop nest that is affected by the
>> loop transformations by a single OMP_FOR to meet the expectations
>> of all later OpenMP code transformations. This is also the major
>> reason why the loop transformations are represented by clauses
>> instead of representing them as  "OMP_UNROLL/OMP_TILE as
>> GENERIC constructs like OMP_FOR" as you suggest below. Since the
> I really don't see why. We try to represent what we see in the source
> as OpenMP constructs as those constructs. We already have a precedent
> with composite loop constructs, where for the combined constructs which
> aren't innermost we temporarily use NULL 
> OMP_FOR_{INIT,COND,INCR,ORIG_DECLS}
> vectors to stand for this will be some loop, but the details for it aren't
> known yet, to be filled up later. So, why can't we similarly represent
> #pragma omp for collapse(3)
> #pragma omp tile sizes (4, 2, 2)
> #pragma omp tile sizes (4, 8, 16)
> for (int i = 0; i < 64; ++i)
> for (int j = 0; j < 64; ++j)
> for (int k = 0; k < 64; ++k)
> body;
> as OMP_FOR with NULL OMP_FOR_{INIT,COND,INCR,ORIG_DECLS}
> with the appropriate clauses on it, with
> OMP_TILE (again, right clauses, NULL OMP_FOR_{INIT,COND,INCR,ORIG_DECLS})
> and another OMP_TILE, this time with all the vectors filled in in GENERIC?
>
> #pragma omp for collapse(2)
> for (int i = 0; i < 64; ++i)
> #pragma omp tile sizes (4)
> for (int j = 0; j < 64; ++j)
> would be represented by non-NULL vectors which would have all the inner
> entries NULL (the outer loop is not generated loop, the inner one is
> generated) with OMP_TILE inside of it.
>
> Then depending on where the loop transformation is actually performed,
> we'd either need to preserve such shape from gimplification until the
> loop transformations are applied, or would be solely on GENERIC and
> GIMPLE would already have the transformed loops.

Thanks for the explanation! I think now I understand how you would do this.

> Clauses e.g. have the disadvantage that generally they aren't ordered.
> If it is separate statements, it is e.g. easier to print it right in
> original dump, so that people can compare the loops before the
> transformation and after it.
>
You mean, the clauses are not ordered at the level of the specification?
In the implementation they are of course ordered and the order has
proved to be sufficiently stable. But perhaps you mean that you would
like to avoid introducing code that relies on the ordering of the clauses?
In this case, I could move the transformations to a separate chain which
could be accessed e.g. by OMP_FOR_TRANSFORMS and by
gimple_omp_for_transforms per level. This would also allow to print the
transformations in the pretty printing functions on the corresponding levels
of the loop nest. This would also be possible somehow with the present
representation. But right now the transformations are just printed together
with the other clauses on the directive. I considerd this to be acceptable
because I suppose the dumps will be mostly read by GCC developers.
There are also other clauses that are only used internally.
>> You suggest to implement the loop transformations during gimplification.
>> I am not sure if gimplification is actually well-suited to implement the
>> depth-first evaluation of the loop transformations. I also believe that
> Why not? The loop transformation constructs can't be deeply nested in the
> bodies, they need to be close.
> gimplify_omp_for already searches the body for the case of composite
> constructs - if (OMP_FOR_INIT (for_stmt) == NULL_TREE) early in it.
> So, this would just mean doing it if that condition is true also looking
> for loop transformation constructs (if they are found, pass in the
> containing OMP_{FOR,SIMD,LOOP,DISTRIBUTE,TASKLOOP} if any to a routine
> that handles the transformation, such that it can update the containing
> looping construct if any during the transformation.
> That alone would handle the case where the looping construct should work
> solely with the generated loop. It would need to do the same thing
> also if OMP_FOR_INIT (for_stmt) is non-NULL but
> TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), TREE_VEC_LENGTH (OMP_FOR_INIT 
> (for_stmt)))
> is NULL to handle the case where generated loops are just some of the 
> inner
> ones.
> And then when gimplify_omp_for would encounter an OMP_TILE/OMP_UNROLL
> loop on its own (i.e. not nested inside some other loop), it would 
> similarly
> find further transform constructs in it like the above but then would just
> normally do the loop transformation, with NULL_TREE for the containing 
> loop,
> meaning it is a sequential stuff.

Thanks for the explanation. But actually doing this would require a
complete rewrite which would almost certainly imply that mainline GCC
would not support the loop transformations for a long time.


Best regards,

Frederik


  reply	other threads:[~2023-05-17 11:55 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-24 15:30 Frederik Harwath
2023-03-24 15:30 ` [PATCH 1/7] openmp: Add Fortran support for "omp unroll" directive Frederik Harwath
2023-04-01  8:42   ` Thomas Schwinge
2023-04-06 13:07     ` Frederik Harwath
2023-03-24 15:30 ` [PATCH 2/7] openmp: Add C/C++ " Frederik Harwath
2023-03-24 15:30 ` [PATCH 3/7] openacc: Rename OMP_CLAUSE_TILE to OMP_CLAUSE_OACC_TILE Frederik Harwath
2023-03-24 15:30 ` [PATCH 4/7] openmp: Add Fortran support for "omp tile" Frederik Harwath
2023-03-24 15:30 ` [PATCH 5/7] openmp: Add C/C++ " Frederik Harwath
2023-03-24 15:30 ` [PATCH 6/7] openmp: Add Fortran support for loop transformations on inner loops Frederik Harwath
2023-03-24 15:30 ` [PATCH 7/7] openmp: Add C/C++ " Frederik Harwath
2023-05-15 10:19 ` [PATCH 0/7] openmp: OpenMP 5.1 loop transformation directives Jakub Jelinek
2023-05-15 11:03   ` Jakub Jelinek
2023-05-16  9:45   ` Frederik Harwath
2023-05-16 11:00     ` Jakub Jelinek
2023-05-17 11:55       ` Frederik Harwath [this message]
2023-05-22 14:20         ` Jakub Jelinek
2023-07-28 13:04 ` [PATCH 0/4] openmp: loop transformation fixes Frederik Harwath
2023-07-28 13:04   ` [PATCH 1/4] openmp: Fix loop transformation tests Frederik Harwath
2023-07-28 13:04   ` [PATCH 2/4] openmp: Fix initialization for 'unroll full' Frederik Harwath
2023-07-28 13:04   ` [PATCH 3/4] openmp: Fix diagnostic message for "omp unroll" Frederik Harwath
2023-07-28 13:04   ` [PATCH 4/4] openmp: Fix number of iterations computation for "omp unroll full" Frederik Harwath

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8b53269a-c385-52d1-b862-8a4b904ce66d@siemens.com \
    --to=frederik.harwath@siemens.com \
    --cc=fortran@gcc.gnu.org \
    --cc=frederik@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jason@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=tobias@codesourcery.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).