-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][memref] Use dedicated ops in AtomicRMWOpConverter
#66437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This patch refactors the `AtomicRMWOpConverter` class to use the dedicated operations from Arith dialect instead of using `cmpf` + `select` pattern. Also, a test for `minimumf` kind of `atomic_rmw` has been added.
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-core ChangesThis patch refactors the `AtomicRMWOpConverter` class to use the dedicated operations from Arith dialect instead of using `cmpf` + `select` pattern. Also, a test for `minimumf` kind of `atomic_rmw` has been added.-- 2 Files Affected:
diff --git a/mlir/lib/Dialect/MemRef/Transforms/ExpandOps.cpp b/mlir/lib/Dialect/MemRef/Transforms/ExpandOps.cpp index 7c3ca19b789c750..b3beaada2539dbc 100644 --- a/mlir/lib/Dialect/MemRef/Transforms/ExpandOps.cpp +++ b/mlir/lib/Dialect/MemRef/Transforms/ExpandOps.cpp @@ -33,18 +33,18 @@ using namespace mlir; namespace { /// Converts `atomic_rmw` that cannot be lowered to a simple atomic op with -/// AtomicRMWOpLowering pattern, e.g. with "minf" or "maxf" attributes, to -/// `memref.generic_atomic_rmw` with the expanded code. +/// AtomicRMWOpLowering pattern, such as minimum and maximum operations for +/// floating-point numbers, to `memref.generic_atomic_rmw` with the expanded +/// code. /// -/// %x = atomic_rmw "maximumf" %fval, %F[%i] : (f32, memref<10xf32>) -> f32 +/// %x = atomic_rmw maximumf %fval, %F[%i] : (f32, memref<10xf32>) -> f32 /// /// will be lowered to /// /// %x = memref.generic_atomic_rmw %F[%i] : memref<10xf32> { /// ^bb0(%current: f32): -/// %cmp = arith.cmpf "ogt", %current, %fval : f32 -/// %new_value = select %cmp, %current, %fval : f32 -/// memref.atomic_yield %new_value : f32 +/// %1 = arith.maximumf %current, %fval : f32 +/// memref.atomic_yield %1 : f32 /// } struct AtomicRMWOpConverter : public OpRewritePattern<memref::AtomicRMWOp> { public: @@ -52,18 +52,6 @@ struct AtomicRMWOpConverter : public OpRewritePattern<memref::AtomicRMWOp> { LogicalResult matchAndRewrite(memref::AtomicRMWOp op, PatternRewriter &rewriter) const final { - arith::CmpFPredicate predicate; - switch (op.getKind()) { - case arith::AtomicRMWKind::maximumf: - predicate = arith::CmpFPredicate::OGT; - break; - case arith::AtomicRMWKind::minimumf: - predicate = arith::CmpFPredicate::OLT; - break; - default: - return failure(); - } - auto loc = op.getLoc(); auto genericOp = rewriter.create<memref::GenericAtomicRMWOp>( loc, op.getMemref(), op.getIndices()); @@ -72,9 +60,10 @@ struct AtomicRMWOpConverter : public OpRewritePattern<memref::AtomicRMWOp> { Value lhs = genericOp.getCurrentValue(); Value rhs = op.getValue(); - Value cmp = bodyBuilder.create<arith::CmpFOp>(loc, predicate, lhs, rhs); - Value select = bodyBuilder.create<arith::SelectOp>(loc, cmp, lhs, rhs); - bodyBuilder.create<memref::AtomicYieldOp>(loc, select); + + Value arithOp = + mlir::arith::getReductionOp(op.getKind(), bodyBuilder, loc, lhs, rhs); + bodyBuilder.create<memref::AtomicYieldOp>(loc, arithOp); rewriter.replaceOp(op, genericOp.getResult()); return success(); diff --git a/mlir/test/Dialect/MemRef/expand-ops.mlir b/mlir/test/Dialect/MemRef/expand-ops.mlir index 3234b35e99dcdfe..6c98cf978505334 100644 --- a/mlir/test/Dialect/MemRef/expand-ops.mlir +++ b/mlir/test/Dialect/MemRef/expand-ops.mlir @@ -4,15 +4,20 @@ // CHECK-SAME: ([[F:%.*]]: memref<10xf32>, [[f:%.*]]: f32, [[i:%.*]]: index) func.func @atomic_rmw_to_generic(%F: memref<10xf32>, %f: f32, %i: index) -> f32 { %x = memref.atomic_rmw maximumf %f, %F[%i] : (f32, memref<10xf32>) -> f32 + %y = memref.atomic_rmw minimumf %f, %F[%i] : (f32, memref<10xf32>) -> f32 return %x : f32 } -// CHECK: %0 = memref.generic_atomic_rmw %arg0[%arg2] : memref<10xf32> { +// CHECK: [[RESULT:%.*]] = memref.generic_atomic_rmw %arg0[%arg2] : memref<10xf32> { // CHECK: ^bb0([[CUR_VAL:%.*]]: f32): -// CHECK: [[CMP:%.*]] = arith.cmpf ogt, [[CUR_VAL]], [[f]] : f32 -// CHECK: [[SELECT:%.*]] = arith.select [[CMP]], [[CUR_VAL]], [[f]] : f32 -// CHECK: memref.atomic_yield [[SELECT]] : f32 +// CHECK: [[MAXIMUM:%.*]] = arith.maximumf [[CUR_VAL]], [[f]] : f32 +// CHECK: memref.atomic_yield [[MAXIMUM]] : f32 // CHECK: } -// CHECK: return %0 : f32 +// CHECK: memref.generic_atomic_rmw %arg0[%arg2] : memref<10xf32> { +// CHECK: ^bb0([[CUR_VAL:%.*]]: f32): +// CHECK: [[MINIMUM:%.*]] = arith.minimumf [[CUR_VAL]], [[f]] : f32 +// CHECK: memref.atomic_yield [[MINIMUM]] : f32 +// CHECK: } +// CHECK: return [[RESULT]] : f32 // ----- |
dcaballe
approved these changes
Sep 14, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
ZijunZhaoCCK
pushed a commit
to ZijunZhaoCCK/llvm-project
that referenced
this pull request
Sep 19, 2023
This patch refactors the `AtomicRMWOpConverter` class to use the dedicated operations from Arith dialect instead of using `cmpf` + `select` pattern. Also, a test for `minimumf` kind of `atomic_rmw` has been added.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch refactors the
AtomicRMWOpConverter
class to usethe dedicated operations from Arith dialect instead of using
cmpf
+select
pattern.Also, a test for
minimumf
kind ofatomic_rmw
has been added.