This is an archive of the discontinued LLVM Phabricator instance.

[OPENMP] Codegen for 'reduction' clause in 'parallel' directive.
ClosedPublic

Authored by ABataev on Apr 9 2015, 5:23 AM.

Details

Summary

Emit a code for reduction clause. Next code should be emitted for reductions:

static kmp_critical_name lock = { 0 };

void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
  *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]);
  ...
  *(Type<n>-1*)lhs[<n>-1] =
  ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1],
  *(Type<n>-1*)rhs[<n>-1]);
}

 ...
 void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
 switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), RedList, reduce_func, &<lock>)) {
 case 1:
  <LHSExprs>[0] = ReductionOperation0(*<LHSExprs>[0], *<RHSExprs>[0]);
  ...
  <LHSExprs>[<n>-1] = ReductionOperation<n>-1(*<LHSExprs>[<n>-1], *<RHSExprs>[<n>-1]);
 __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
 break;
 case 2:
  Atomic(<LHSExprs>[0] = ReductionOperation0(*<LHSExprs>[0], *<RHSExprs>[0]));
  ...
  Atomic(<LHSExprs>[<n>-1] = ReductionOperation<n>-1(*<LHSExprs>[<n>-1], *<RHSExprs>[<n>-1]));
 break;
 default:;
 }

Reduction variables are a kind of a private variables, they have private copies, but initial values are chosen in accordance with the reduction operation.

Diff Detail

Repository
rL LLVM

Event Timeline

ABataev updated this revision to Diff 23478.Apr 9 2015, 5:23 AM
ABataev retitled this revision from to [OPENMP] Codegen for 'reduction' clause in 'parallel' directive..
ABataev updated this object.
ABataev edited the test plan for this revision. (Show Details)
ABataev added a subscriber: Unknown Object (MLST).
rjmccall edited edge metadata.Apr 9 2015, 2:35 PM

Looks very nice, thanks. Only one serious question.

lib/CodeGen/CGOpenMPRuntime.cpp
1626 ↗(On Diff #23478)

Writing the second line actually makes this less clear. I would just write one line in terms of "i", like

*(Type<i>*)lhs[i] = ...
1782 ↗(On Diff #23478)

Pushing the cleanup immediately before the scope is left like this doesn't do anything except emit it immediately on the normal path. Did you mean to push it before doing any of the reduction operations?

John, thank you very much for the review!

lib/CodeGen/CGOpenMPRuntime.cpp
1626 ↗(On Diff #23478)

Ok, will be fixed.

1782 ↗(On Diff #23478)

Thanks, I'll fix this.

This revision was automatically updated to reflect the committed changes.