This is an archive of the discontinued LLVM Phabricator instance.

Add implicit map for a list item appears in a reduction clause.
ClosedPublic

Authored by jyu2 on Aug 16 2021, 7:50 AM.

Details

Summary

A new rule is added in 5.0:
If a list item appears in a reduction, lastprivate or linear clause
on a combined target construct then it is treated as if it also appears
in a map clause with a map-type of tofrom.

Currently map clauses for all capture variables are added implicitly.
But missing for list item of expression for array elements or array
sections.

The change is to add implicit map clause for array of elements used in
reduction clause. Skip adding map clause if the expression is not
mappable.
Noted: For linear and lastprivate, since only variable name is
accepted, the map has been added though capture variables.

To do so:
During the mappable checking, if error, ignore diagnose and skip
adding implicit map clause.

The changes:
1> Add code to generate implicit map in ActOnOpenMPExecutableDirective,

for omp 5.0 and up.

2> Add extra default parameter NoDiagnose in ActOnOpenMPMapClause:
Use that to skip error as well as skip adding implicit map during the
mappable checking.

Note: there are only two places need to be check for NoDiagnose. Rest
of them either the check is for < omp 5.0 or the error already generated for
reduction clause.

Diff Detail

Event Timeline

jyu2 requested review of this revision.Aug 16 2021, 7:50 AM
jyu2 created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptAug 16 2021, 7:50 AM
Herald added a subscriber: sstefan1. · View Herald Transcript

Why it can not be performed in codegen?

Also, would be good to see a runtime test, which reveals the issue.

jyu2 added a comment.Aug 16 2021, 8:44 AM

Hi ABataev,
Thanks for reviedw.

Why it can not be performed in codegen?

I am not sure I can do that. Do you mean when generate map adding coding code to look though reduction clause and generate map for it?

Here is the runtime test, I am trying to find way on how to add runtime test in clang. But in my added test reduction_implicit_map.cpp, I did checked IR for this.

The command line: without may change:
cmplrllvm-25845>clang -fopenmp -fopenmp-targets=x86_64-pc-linux-gnu o.cpp -g
cmplrllvm-25845>./a.out
Segmentation fault (core dumped)
with may change:
cmplrllvm-25845> ./a.out
Result=5050

test

extern "C" int printf(const char *,...);
void sum(int* input, int size, int* output)
{

#pragma omp target teams distribute parallel for reduction(+:output[0]) map(to:input[0:size]) //map(output[0])
for(int i=0; i<size; i++)
    output[0] += input[i];

}
int main()
{

const int size = 100;
int *array = new int[size];
int result = 0;
for (int i = 0; i < size; i++)
    array[i] = i + 1;
sum(array, size, &result);
printf("Result=%d\n", result);
delete[] array;
return 0;

}

Hi ABataev,
Thanks for reviedw.

Why it can not be performed in codegen?

I am not sure I can do that. Do you mean when generate map adding coding code to look though reduction clause and generate map for it?

Yes, exactly.

Here is the runtime test, I am trying to find way on how to add runtime test in clang. But in my added test reduction_implicit_map.cpp, I did checked IR for this.

Add it to libomptarget.

The command line: without may change:
cmplrllvm-25845>clang -fopenmp -fopenmp-targets=x86_64-pc-linux-gnu o.cpp -g
cmplrllvm-25845>./a.out
Segmentation fault (core dumped)
with may change:
cmplrllvm-25845> ./a.out
Result=5050

test

extern "C" int printf(const char *,...);
void sum(int* input, int size, int* output)
{

#pragma omp target teams distribute parallel for reduction(+:output[0]) map(to:input[0:size]) //map(output[0])
for(int i=0; i<size; i++)
    output[0] += input[i];

}
int main()
{

const int size = 100;
int *array = new int[size];
int result = 0;
for (int i = 0; i < size; i++)
    array[i] = i + 1;
sum(array, size, &result);
printf("Result=%d\n", result);
delete[] array;
return 0;

}

jyu2 added a comment.Aug 16 2021, 8:56 AM

I am not sure I can do that. Do you mean when generate map adding coding code to look though reduction clause and generate map for it?

Yes, exactly.

We are missing mappable checking for example:

#pragma omp target parallel for reduction(task, +: b[0:2][2:4][1])

In this, we should not add map clause, since the section is not contiguous storage.

I am not sure I can do that. Do you mean when generate map adding coding code to look though reduction clause and generate map for it?

Yes, exactly.

We are missing mappable checking for example:

#pragma omp target parallel for reduction(task, +: b[0:2][2:4][1])

In this, we should not add map clause, since the section is not contiguous storage.

.
OK, and what's the problem to check for this in codegen? Also, we can map non-contiguous storage, at least in some cases.

cchen added a subscriber: cchen.Aug 16 2021, 9:46 AM
jyu2 updated this revision to Diff 366894.Aug 17 2021, 7:37 AM

Fix format and add new runtime test.

Herald added a project: Restricted Project. · View Herald TranscriptAug 17 2021, 7:37 AM
ABataev added inline comments.Aug 17 2021, 7:50 AM
clang/lib/Sema/SemaOpenMP.cpp
5825

isa. Also, what if this is a MemberExpr?

18475–18476

It still might be good to emit the warning if the reduction type is mappable. Also, need to extend this check in general and avoid emitting it if the user defined mapper for the type is provided (in a separate patch, not here).

19430–19432

You can remove /*NoDiagnose=*/ here.

19478–19479

Hmm, should not we still diagnose this case?

openmp/libomptarget/test/mapping/reduction_implicit_map.cpp
5

Can we make UNSUPPORTED instead of XFAIL?

jyu2 updated this revision to Diff 366945.Aug 17 2021, 10:31 AM
jyu2 edited the summary of this revision. (Show Details)

Address Alex's comments. Thanks Alex.

jyu2 marked 4 inline comments as not done.Aug 17 2021, 11:04 AM
clang/lib/Sema/SemaOpenMP.cpp
5825

Yes isa. Changed. Thanks.

For reduction MemeberExpr is not allowed, it is only allowed variable name, array element or array section.

18475–18476

Thanks. Make sense, in this case, implicit map will be add, so warning should be emit. Changed.

19430–19432

Changed. Thanks.

19478–19479

The rule is skip the error as well as skip adding implicit map clause. So that we don't get regression for old code.

openmp/libomptarget/test/mapping/reduction_implicit_map.cpp
5

changed. Thanks.

ABataev added inline comments.Aug 17 2021, 11:10 AM
clang/lib/Sema/SemaOpenMP.cpp
5825

I would also do this check if it is a not template parsing/analysis mode + also would do this for E->IgnoreParensImpCasts()

19478–19479

I think we already have the check for it for the reduction clause, so I think we can skip this check here.

jyu2 added inline comments.Aug 17 2021, 12:15 PM
clang/lib/Sema/SemaOpenMP.cpp
5825

The code already under non DependentContext(). I add IgnoreParenImpCasts(). Thanks.

if (AStmt && !CurContext->isDependentContext() && Kind != OMPD_atomic &&

Kind != OMPD_critical && Kind != OMPD_section && Kind != OMPD_master &&
Kind != OMPD_masked && !isOpenMPLoopTransformationDirective(Kind))
19478–19479

In general yes. But not for some special case, please look clang/test/OpenMP/reduction_implicit_map.cpp, in the first test, if I remove this change the error will be emit. In CUDA device code, the local variable is implicitly threat as thridprivate.

#if defined(CUDA)
// expected-no-diagnostics

int foo(int n) {

double *e;
//no error and no implicit map generated for e[:1]
#pragma omp target parallel reduction(+: e[:1])
  *e=10;
;
return 0;

}

ABataev added inline comments.Aug 17 2021, 12:18 PM
clang/lib/Sema/SemaOpenMP.cpp
19478–19479

I just was not quite clear, I agree with your previous answer ;)

jyu2 updated this revision to Diff 366991.Aug 17 2021, 12:57 PM

Address Alex's comment.

This revision is now accepted and ready to land.Aug 17 2021, 12:58 PM
jyu2 added a comment.Aug 19 2021, 1:04 PM

Thank you so much for Alex's review!!!