This is an archive of the discontinued LLVM Phabricator instance.

[mlir][openacc] Add reduction representation
ClosedPublic

Authored by clementval on May 17 2023, 2:35 PM.

Details

Summary

Similarly to D150622 for private clause, the reduction is currently not
modeled in a good way. This patch is inspired by the reduction representation
in the omp dialect (D105358) and make a new representation for the reduction in
the OpenACC dialect.

A new operation is introduced to model the sequences of operation needed to
initialize a local reduction value and how to combine two values during the
reduction. The operation requires two mandatory regions.

  1. The init region specifies how to initialize the local reduction value. The region has an argument that contains the value of the reduction accumulator at the start of the reduction. It is expected to acc.yield the new value.
  2. The reduction region contains a sequences of operations to combine two values of the reduction type into one. It has two arguments and it is expected to acc.yield the combined value.

Example:

mlir
acc.reduction.recipe @reduction_add_i64 : i64 init reduction_operator<add> {
^bb0(%0: i64):
  // init region contains a sequence of operations to initialize the local
  // reduction value as specified in 2.5.15
  %c0 = arith.constant 0 : i64
  acc.yield %c0 : i64
} reduction {
^bb0(%0: i64, %1: i64)
  // reduction region contains a sequence of operations to combine
  // two values into one.
  %2 = arith.addi %0, %1 : i64
  acc.yield %2 : i64
}

// The reduction symbol is then used in the corresponding operation.
acc.parallel reduction(@reduction_add_i64 -> %a : i64) {
}

Diff Detail

Event Timeline

clementval created this revision.May 17 2023, 2:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 17 2023, 2:35 PM
clementval requested review of this revision.May 17 2023, 2:35 PM

Just a few spelling errors. Some are also in the commit message.

mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
538

conatins -> contains

556

reductioon -> reduction

567

tbale -> table

razvanlupusoru added inline comments.May 18 2023, 8:07 AM
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
614–615

Did you intend to use llvm.ptr<i64>? I would guess yes. In that case, it doesn't seem to match the signature of combiner region (unless I am misunderstanding something).

razvanlupusoru added inline comments.May 18 2023, 8:23 AM
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
588

Can you please also attach the ReductionOperator to the recipe?

razvanlupusoru added inline comments.May 18 2023, 8:37 AM
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
591–592

I feel that we should name this "combinerRegion". I worry with too many things named "reduction" (acc.reduction.recipe, acc.reduction), it makes it somewhat ambiguous.

vzakhari added inline comments.May 18 2023, 8:49 AM
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
615

Could you please clarify where this operation is coming from? What is the flow for generating it?

From the language we only have a reduction clause, so I am not sure why the reduction operation appears. I suppose it may appear later in the OpenACC lowering flow, for example, when a "thread-private" accumulator value needs to be added to the "global" accumulator. In other words, I am not following it :)

623

Can you please clarify the connection between the reduction and the reduction.recipe?

Address review comments

clementval marked 5 inline comments as done.May 18 2023, 9:17 AM
clementval added inline comments.
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
615

The idea is that this operation replace the actual operation that has the reduction participating variable. With the reduction operator attached to the recipe we could omit this operation and be able to track back operation that participate in the reduction during codegen and place the combiner when needed.

vzakhari added inline comments.May 18 2023, 10:06 AM
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
615

I see. Is this an optimization for skipping the combining, if there are no actual reduction updates in the region?

I suppose we will not be able to place the reduction operation always, for example:

#pragma acc parallel reduction(+: r)
  func(&r); // r is updated inside func
clementval added inline comments.May 18 2023, 10:13 AM
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
615

Yes you are correct. I would suggest to not add this operation for the moment as it is not strictly necessary.

vzakhari added inline comments.May 18 2023, 11:56 AM
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
615

Yes, I think we'd better add it separately, when needed. Thank you!

  • Add reduction_operator
  • Remove acc.reduction
clementval marked 5 inline comments as done.May 18 2023, 1:59 PM
vzakhari added inline comments.May 18 2023, 2:47 PM
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
548

Please update the example with the reduction operator representation. Otherwise, it looks great! Thank you!

razvanlupusoru accepted this revision.May 18 2023, 2:51 PM

I agree with Slava about updating example. Otherwise looks great in this form. Thank you.

This revision is now accepted and ready to land.May 18 2023, 2:51 PM
clementval added inline comments.May 18 2023, 2:54 PM
mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
548

My bad, I missed it. I'll update it.

clementval marked an inline comment as done.

Update example

clementval edited the summary of this revision. (Show Details)May 18 2023, 2:58 PM

Updated. Thanks for the reviews!

clang-format

vzakhari accepted this revision.May 18 2023, 3:22 PM
This revision was automatically updated to reflect the committed changes.