This is an archive of the discontinued LLVM Phabricator instance.

[clang][CodeGen][OpenMP] Fix if-clause for 'target teams loop'
AbandonedPublic

Authored by ddpagan on Aug 5 2023, 1:43 PM.

Details

Reviewers
ABataev
jdoerfert
Summary

When emitting parallel region for 'target teams distribute parallel for' expansion of 'target teams loop', ignore if-clause as anything specified
should apply to target region only.

Updated test results for OpenMP/target_teams_generic_loop_if_codegen.cpp

Diff Detail

Event Timeline

ddpagan created this revision.Aug 5 2023, 1:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 5 2023, 1:43 PM
ddpagan requested review of this revision.Aug 5 2023, 1:43 PM

Add the reference to openmp spec

ddpagan updated this revision to Diff 547515.Aug 5 2023, 5:13 PM

Added reference to relevant portion of the OpenMP 5.2 specification.

ABataev added inline comments.Aug 5 2023, 5:47 PM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1564

I don't see this statement in the standard

ddpagan added inline comments.Aug 5 2023, 10:13 PM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1564

Sorry for that confusion on my part, Alexey. You're correct. I'll change this to the proper spec phrasing and resubmit.

ddpagan updated this revision to Diff 547982.Aug 7 2023, 3:40 PM

As requested, added reference to OpenMP 5.2 specification that discusses handling of if clauses with combined/composite directives. Also, improved comment relating to what was being done and why.

ABataev added inline comments.Aug 8 2023, 6:52 AM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1570–1575

It does not match the spec.

For a combined or composite construct, if no directive-name-modifier is specified then the if clause applies to all constituent constructs to which an if clause can apply.

So, if(val) should be applied to both target and parallel regions, no?

ddpagan added inline comments.Aug 11 2023, 9:55 AM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1570–1575

It does not match the spec.

For a combined or composite construct, if no directive-name-modifier is specified then the if clause applies to all constituent constructs to which an if clause can apply.

So, if(val) should be applied to both target and parallel regions, no?

Hi Alexey - Question for you: does revising the comment above at lines 1570-1575 to the following text help explain in a better way what's being done, and why?

If we are handling a 'target teams distribute parallel for' explicitly written
in the source, and it has an 'if(val)' clause, the if condition is applied to
both 'target' and 'parallel' regions according to
OpenMP 5.2 [3.4, if Clause, Semantics, 15-18].

However, if we are mapping an explicit 'target teams loop if(val)' onto a
'target teams distribute parallel for if(val)', to preserve the 'if' semantics
as specified by the user with the 'target teams loop', we apply it just to
the 'target' region.
ABataev added inline comments.Aug 11 2023, 10:04 AM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1570–1575

It does not match the spec. Why we shall handle it this way?

ddpagan added inline comments.Aug 12 2023, 10:44 AM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1570–1575

You're right, Alexey ... it doesn't match the spec, but here's why we thought this would be an appropriate way to implement 'target teams loop if(val)'. When a user specifies 'if(val)' with a 'target teams loop', their expectation is that its effect will only apply to the 'target' region. Since a 'loop' construct can be implemented in a number of different ways with the freedom granted by the specs description of 'loop' (part of which describes it as being able to be run concurrently), using a 'target teams distribute parallel for' construct is a simple and effective default choice, which is what happens today.

target_teams_loop => target_teams_distribute_parallel_for

Applying the if clause to the parallel region for this case can potentially limit it to one thread, which would hinder performance gains otherwise possible, and presumably wouldn't be what the user wanted/expected.

The semantics of the spec (OpenMP 5.2 [3.4, if Clause, Semantics, 15-18]) is definitely what should be applied to an explicit instance of target_teams_distribute_parallel_for, but in this case (when mapping target_teams_loop => target_teams_distribute_parallel_for) it seems reasonable to make the choice described above.

ABataev added inline comments.Aug 12 2023, 12:25 PM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1570–1575

I'm not sure this is true users expectations.

ddpagan added inline comments.Aug 12 2023, 1:21 PM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1570–1575

You're right in the sense that there is an assumption being made there. So would you recommend just staying with the semantics as defined in the spec regardless?

ABataev added inline comments.Aug 12 2023, 1:24 PM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1570–1575

Yew, better to follow spec defaults.

ddpagan added inline comments.Aug 12 2023, 1:29 PM
clang/lib/CodeGen/CGStmtOpenMP.cpp
1570–1575

Ok. Sounds good. Thanks for review and suggestions, Alexey.

ddpagan abandoned this revision.Sep 2 2023, 7:59 AM

Change no longer needed per review/comments.