-
Notifications
You must be signed in to change notification settings - Fork 30
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
Add logical objectFifo hoisting pass #679
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
62 changes: 62 additions & 0 deletions
62
compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEHoistLogicalObjFifo.cpp
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2024 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include "iree-amd-aie/IR/AMDAIEOps.h" | ||
#include "iree-amd-aie/Transforms/Passes.h" | ||
#include "iree-amd-aie/Transforms/Transforms.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
|
||
#define DEBUG_TYPE "iree-amdaie-hoist-logical-objectfifo" | ||
|
||
namespace mlir::iree_compiler::AMDAIE { | ||
|
||
/// Hoist logical objectFifo operations until one of the operands is located | ||
/// within the same scope. | ||
LogicalResult hoistLogicalObjFifoOp(RewriterBase &rewriter, | ||
AMDAIE::LogicalObjectFifoFromMemrefOp op) { | ||
Operation *ancestorOp = op; | ||
while (ancestorOp) { | ||
Operation *newAncestorOp = ancestorOp->getParentOp(); | ||
if (llvm::any_of(op->getOperands(), [&](Value operand) { | ||
return operand.getDefiningOp() && | ||
newAncestorOp->isProperAncestor(operand.getDefiningOp()); | ||
})) { | ||
break; | ||
} | ||
if (isa<AMDAIE::WorkgroupOp, AMDAIE::ControlCodeOp, func::FuncOp>( | ||
newAncestorOp)) { | ||
break; | ||
} | ||
ancestorOp = newAncestorOp; | ||
} | ||
if (ancestorOp && ancestorOp != op) rewriter.moveOpBefore(op, ancestorOp); | ||
return failure(); | ||
} | ||
|
||
namespace { | ||
struct AMDAIEHoistLogicalObjFifoPass | ||
: public impl::AMDAIEHoistLogicalObjFifoBase< | ||
AMDAIEHoistLogicalObjFifoPass> { | ||
void runOnOperation() override; | ||
}; | ||
|
||
void AMDAIEHoistLogicalObjFifoPass::runOnOperation() { | ||
Operation *parentOp = getOperation(); | ||
IRRewriter rewriter(parentOp->getContext()); | ||
|
||
SmallVector<AMDAIE::LogicalObjectFifoFromMemrefOp> logicalObjFifos; | ||
parentOp->walk([&](AMDAIE::LogicalObjectFifoFromMemrefOp op) { | ||
(void)hoistLogicalObjFifoOp(rewriter, op); | ||
}); | ||
} | ||
|
||
} // namespace | ||
|
||
std::unique_ptr<Pass> createAMDAIEHoistLogicalObjFifoPass() { | ||
return std::make_unique<AMDAIEHoistLogicalObjFifoPass>(); | ||
} | ||
|
||
} // namespace mlir::iree_compiler::AMDAIE |
This file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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
205 changes: 205 additions & 0 deletions
205
compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/test/hoist_logical_obj_fifo.mlir
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
// RUN: iree-opt --split-input-file --pass-pipeline="builtin.module(iree-amdaie-hoist-logical-objectfifo)" %s | FileCheck %s | ||
|
||
// CHECK-LABEL: @func_hoist | ||
// CHECK-SAME: %[[ARG0:.+]]: memref<32x64xi32> | ||
// CHECK: %[[C0:.+]] = arith.constant 0 : index | ||
// CHECK: %[[TILE_0_0:.+]] = amdaie.tile(%[[C0]], %[[C0]]) | ||
// CHECK: amdaie.logicalobjectfifo.from_memref %[[ARG0]], {%[[TILE_0_0]]} | ||
// CHECK: scf.forall | ||
// CHECK-NOT: amdaie.logicalobjectfifo.from_memref | ||
module { | ||
func.func @func_hoist(%arg0: memref<32x64xi32>) { | ||
%c0 = arith.constant 0 : index | ||
%tile_0_0 = amdaie.tile(%c0, %c0) | ||
scf.forall (%arg1, %arg2) in (1, 2) { | ||
%obj0 = amdaie.logicalobjectfifo.from_memref %arg0, {%tile_0_0} : memref<32x64xi32> -> !amdaie.logicalobjectfifo<memref<32x64xi32>> | ||
} | ||
return | ||
} | ||
} | ||
|
||
|
||
// ----- | ||
|
||
// CHECK-LABEL: @func_no_hoist | ||
// CHECK-SAME: %[[ARG0:.+]]: memref<32x64xi32> | ||
// CHECK: %[[C0:.+]] = arith.constant 0 : index | ||
// CHECK: scf.forall | ||
// CHECK: %[[TILE_0_0:.+]] = amdaie.tile(%[[C0]], %[[C0]]) | ||
// CHECK: amdaie.logicalobjectfifo.from_memref %[[ARG0]], {%[[TILE_0_0]]} | ||
module { | ||
func.func @func_no_hoist(%arg0: memref<32x64xi32>) { | ||
%c0 = arith.constant 0 : index | ||
scf.forall (%arg1, %arg2) in (1, 2) { | ||
%tile_0_0 = amdaie.tile(%c0, %c0) | ||
%obj0 = amdaie.logicalobjectfifo.from_memref %arg0, {%tile_0_0} : memref<32x64xi32> -> !amdaie.logicalobjectfifo<memref<32x64xi32>> | ||
} | ||
return | ||
} | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: @workgroup_hoist | ||
// CHECK-SAME: %[[ARG0:.+]]: memref<32x64xi32> | ||
// CHECK: amdaie.workgroup | ||
// CHECK: %[[C0:.+]] = arith.constant 0 : index | ||
// CHECK: %[[TILE_0_0:.+]] = amdaie.tile(%[[C0]], %[[C0]]) | ||
// CHECK: amdaie.logicalobjectfifo.from_memref %[[ARG0]], {%[[TILE_0_0]]} | ||
// CHECK: scf.forall | ||
// CHECK-NOT: amdaie.logicalobjectfifo.from_memref | ||
func.func @workgroup_hoist(%arg0: memref<32x64xi32>) { | ||
amdaie.workgroup { | ||
%c0 = arith.constant 0 : index | ||
%tile_0_0 = amdaie.tile(%c0, %c0) | ||
scf.forall (%arg1, %arg2) in (1, 2) { | ||
%obj0 = amdaie.logicalobjectfifo.from_memref %arg0, {%tile_0_0} : memref<32x64xi32> -> !amdaie.logicalobjectfifo<memref<32x64xi32>> | ||
} | ||
amdaie.controlcode { | ||
amdaie.end | ||
} | ||
} | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: @workgroup_no_hoist | ||
// CHECK-SAME: %[[ARG0:.+]]: memref<32x64xi32> | ||
// CHECK: amdaie.workgroup | ||
// CHECK: %[[C0:.+]] = arith.constant 0 : index | ||
// CHECK: scf.forall | ||
// CHECK: %[[TILE_0_0:.+]] = amdaie.tile(%[[C0]], %[[C0]]) | ||
// CHECK: amdaie.logicalobjectfifo.from_memref %[[ARG0]], {%[[TILE_0_0]]} | ||
func.func @workgroup_no_hoist(%arg0: memref<32x64xi32>) { | ||
amdaie.workgroup { | ||
%c0 = arith.constant 0 : index | ||
scf.forall (%arg1, %arg2) in (1, 2) { | ||
%tile_0_0 = amdaie.tile(%c0, %c0) | ||
%obj0 = amdaie.logicalobjectfifo.from_memref %arg0, {%tile_0_0} : memref<32x64xi32> -> !amdaie.logicalobjectfifo<memref<32x64xi32>> | ||
} | ||
amdaie.controlcode { | ||
amdaie.end | ||
} | ||
} | ||
return | ||
} | ||
// ----- | ||
|
||
// CHECK-LABEL: @workgroup_no_hoist_outside | ||
// CHECK-SAME: %[[ARG0:.+]]: memref<32x64xi32> | ||
// CHECK: %[[C0:.+]] = arith.constant 0 : index | ||
// CHECK: %[[TILE_0_0:.+]] = amdaie.tile(%[[C0]], %[[C0]]) | ||
// CHECK: amdaie.workgroup | ||
// CHECK: amdaie.logicalobjectfifo.from_memref %[[ARG0]], {%[[TILE_0_0]]} | ||
func.func @workgroup_no_hoist_outside(%arg0: memref<32x64xi32>) { | ||
%c0 = arith.constant 0 : index | ||
%tile_0_0 = amdaie.tile(%c0, %c0) | ||
amdaie.workgroup { | ||
%obj0 = amdaie.logicalobjectfifo.from_memref %arg0, {%tile_0_0} : memref<32x64xi32> -> !amdaie.logicalobjectfifo<memref<32x64xi32>> | ||
amdaie.controlcode { | ||
amdaie.end | ||
} | ||
} | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: @controlcode_hoist | ||
// CHECK-SAME: %[[ARG0:.+]]: memref<32x64xi32> | ||
// CHECK: amdaie.controlcode | ||
// CHECK: %[[C0:.+]] = arith.constant 0 : index | ||
// CHECK: %[[TILE_0_0:.+]] = amdaie.tile(%[[C0]], %[[C0]]) | ||
// CHECK: amdaie.logicalobjectfifo.from_memref %[[ARG0]], {%[[TILE_0_0]]} | ||
// CHECK: scf.forall | ||
// CHECK: scf.forall | ||
// CHECK-NOT: amdaie.logicalobjectfifo.from_memref | ||
func.func @controlcode_hoist(%arg0: memref<32x64xi32>) { | ||
amdaie.workgroup { | ||
amdaie.controlcode { | ||
%c0 = arith.constant 0 : index | ||
%tile_0_0 = amdaie.tile(%c0, %c0) | ||
scf.forall (%arg1, %arg2) in (1, 2) { | ||
scf.forall (%arg3, %arg4) in (1, 2) { | ||
%obj0 = amdaie.logicalobjectfifo.from_memref %arg0, {%tile_0_0} : memref<32x64xi32> -> !amdaie.logicalobjectfifo<memref<32x64xi32>> | ||
} | ||
} | ||
amdaie.end | ||
} | ||
} | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: @controlcode_partial_hoist | ||
// CHECK-SAME: %[[ARG0:.+]]: memref<32x64xi32> | ||
// CHECK: amdaie.controlcode | ||
// CHECK: %[[C0:.+]] = arith.constant 0 : index | ||
// CHECK: scf.forall | ||
// CHECK: %[[TILE_0_0:.+]] = amdaie.tile(%[[C0]], %[[C0]]) | ||
// CHECK: amdaie.logicalobjectfifo.from_memref %[[ARG0]], {%[[TILE_0_0]]} | ||
// CHECK: scf.forall | ||
// CHECK-NOT: amdaie.logicalobjectfifo.from_memref | ||
func.func @controlcode_partial_hoist(%arg0: memref<32x64xi32>) { | ||
amdaie.workgroup { | ||
amdaie.controlcode { | ||
%c0 = arith.constant 0 : index | ||
scf.forall (%arg1, %arg2) in (1, 2) { | ||
%tile_0_0 = amdaie.tile(%c0, %c0) | ||
scf.forall (%arg3, %arg4) in (1, 2) { | ||
%obj0 = amdaie.logicalobjectfifo.from_memref %arg0, {%tile_0_0} : memref<32x64xi32> -> !amdaie.logicalobjectfifo<memref<32x64xi32>> | ||
} | ||
} | ||
amdaie.end | ||
} | ||
} | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: @controlcode_no_hoist | ||
// CHECK-SAME: %[[ARG0:.+]]: memref<32x64xi32> | ||
// CHECK: amdaie.controlcode | ||
// CHECK: %[[C0:.+]] = arith.constant 0 : index | ||
// CHECK: scf.forall | ||
// CHECK: scf.forall | ||
// CHECK: %[[TILE_0_0:.+]] = amdaie.tile(%[[C0]], %[[C0]]) | ||
// CHECK: amdaie.logicalobjectfifo.from_memref %[[ARG0]], {%[[TILE_0_0]]} | ||
func.func @controlcode_no_hoist(%arg0: memref<32x64xi32>) { | ||
amdaie.workgroup { | ||
amdaie.controlcode { | ||
%c0 = arith.constant 0 : index | ||
scf.forall (%arg1, %arg2) in (1, 2) { | ||
scf.forall (%arg3, %arg4) in (1, 2) { | ||
%tile_0_0 = amdaie.tile(%c0, %c0) | ||
%obj0 = amdaie.logicalobjectfifo.from_memref %arg0, {%tile_0_0} : memref<32x64xi32> -> !amdaie.logicalobjectfifo<memref<32x64xi32>> | ||
} | ||
} | ||
amdaie.end | ||
} | ||
} | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: @controlcode_no_hoist_outside | ||
// CHECK-SAME: %[[ARG0:.+]]: memref<32x64xi32> | ||
// CHECK: %[[C0:.+]] = arith.constant 0 : index | ||
// CHECK: %[[TILE_0_0:.+]] = amdaie.tile(%[[C0]], %[[C0]]) | ||
// CHECK: amdaie.controlcode | ||
// CHECK: amdaie.logicalobjectfifo.from_memref %[[ARG0]], {%[[TILE_0_0]]} | ||
func.func @controlcode_no_hoist_outside(%arg0: memref<32x64xi32>) { | ||
amdaie.workgroup { | ||
%c0 = arith.constant 0 : index | ||
%tile_0_0 = amdaie.tile(%c0, %c0) | ||
amdaie.controlcode { | ||
%obj0 = amdaie.logicalobjectfifo.from_memref %arg0, {%tile_0_0} : memref<32x64xi32> -> !amdaie.logicalobjectfifo<memref<32x64xi32>> | ||
amdaie.end | ||
} | ||
} | ||
return | ||
} |
This file contains 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
Oops, something went wrong.
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.
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.
I'm not sure if
walk
enumerates all the ops it will visit before hand, I don't think so, so I think you actually visit all the hoisted ops twice (once after hoisting) here.FWIW I find code that
easier to grok.
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.
The hoisted ops will be inserted before the ancestor op, so should not be visited again?
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.
Oh yes, you're right.