-
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 a pass to fold DMA waits #962
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ce810db
init commit
Yu-Zhewen d59184e
bugfix and resolve comments
Yu-Zhewen 00b8894
Merge branch 'main' into zhewen_remove_wait
newling 5f247c5
resolve comments
Yu-Zhewen 3160142
Merge branch 'zhewen_remove_wait' of github.com:nod-ai/iree-amd-aie i…
Yu-Zhewen ba2cc44
Merge branch 'main' into zhewen_remove_wait
Yu-Zhewen f820d08
fix merge
Yu-Zhewen 6fedb1d
resolve comments
Yu-Zhewen f9920b4
Merge branch 'main' into zhewen_remove_wait
Yu-Zhewen e056f3c
Merge branch 'main' into zhewen_remove_wait
Yu-Zhewen 1d46658
Merge branch 'main' into zhewen_remove_wait
Yu-Zhewen 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
167 changes: 167 additions & 0 deletions
167
compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEFoldDmaWaits.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,167 @@ | ||||||
// 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/Utils/AMDAIEDmaUtils.h" | ||||||
#include "iree-amd-aie/Transforms/Utils/AMDAIEUtils.h" | ||||||
#include "iree-amd-aie/aie_runtime/iree_aie_runtime.h" | ||||||
#include "mlir/IR/Iterators.h" | ||||||
#define DEBUG_TYPE "iree-amdaie-fold-dma-waits" | ||||||
|
||||||
namespace mlir::iree_compiler::AMDAIE { | ||||||
|
||||||
namespace { | ||||||
|
||||||
/// Traverses the control code in reverse, ensuring that for each connection, | ||||||
/// only one DMA wait op is retained for every maximum queue size. | ||||||
LogicalResult foldDmaWaits(const AMDAIE::AMDAIEDeviceModel &deviceModel, | ||||||
AMDAIE::ControlCodeOp controlCodeOp) { | ||||||
IRRewriter rewriter(controlCodeOp->getContext()); | ||||||
std::vector<AMDAIE::NpuDmaWaitOp> waitOpsToErase; | ||||||
DenseMap<std::pair<AMDAIE::TileOp, AMDAIE::ConnectionOp>, | ||||||
SmallVector<uint32_t>> | ||||||
tileConnectionToBdIdQueueMap; | ||||||
// Traverse the control code in reverse. | ||||||
WalkResult res = controlCodeOp->walk<WalkOrder::PostOrder, ReverseIterator>( | ||||||
[&](AMDAIE::NpuDmaWaitOp waitOp) { | ||||||
bool toErase = true; | ||||||
for (Value token : waitOp.getAsyncTokens()) { | ||||||
if (auto npuHalfDmaCpyNdOp = | ||||||
dyn_cast_if_present<AMDAIE::NpuHalfDmaCpyNdOp>( | ||||||
token.getDefiningOp())) { | ||||||
// Retrieve the connection op. | ||||||
std::optional<AMDAIE::ConnectionOp> maybeConnectionOp = | ||||||
npuHalfDmaCpyNdOp.getConnectionOp(); | ||||||
if (!maybeConnectionOp) { | ||||||
npuHalfDmaCpyNdOp.emitOpError() | ||||||
<< "expected to operate on an `amdaie.connection`"; | ||||||
return WalkResult::interrupt(); | ||||||
} | ||||||
AMDAIE::ConnectionOp connectionOp = maybeConnectionOp.value(); | ||||||
// Retrieve the flow op. | ||||||
std::optional<AMDAIE::FlowOp> maybeFlowOp = | ||||||
maybeConnectionOp->getFlowOp(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (!maybeFlowOp) { | ||||||
maybeConnectionOp->emitOpError() | ||||||
<< "expected to operate on an `amdaie.flow`"; | ||||||
return WalkResult::interrupt(); | ||||||
} | ||||||
bool isPacketFlow = maybeFlowOp->getIsPacketFlow(); | ||||||
// Retrieve the BD ID op. | ||||||
std::optional<AMDAIE::BdIdOp> maybeBdIdOp = | ||||||
npuHalfDmaCpyNdOp.getBdIdOp(); | ||||||
if (!maybeBdIdOp) { | ||||||
npuHalfDmaCpyNdOp.emitOpError() | ||||||
<< "must have a BD ID op to lower to " | ||||||
"`amdaie.npu.write_bd`"; | ||||||
return WalkResult::interrupt(); | ||||||
} | ||||||
AMDAIE::BdIdOp bdIdOp = maybeBdIdOp.value(); | ||||||
// Retrieve the tile op. | ||||||
AMDAIE::TileOp tileOp = dyn_cast_if_present<AMDAIE::TileOp>( | ||||||
bdIdOp.getTile().getDefiningOp()); | ||||||
if (!tileOp) { | ||||||
bdIdOp.emitOpError() << "must operate on an `amdaie.tile`"; | ||||||
return WalkResult::interrupt(); | ||||||
} | ||||||
// Get the maximum queue size. | ||||||
uint32_t col = getConstantIndexOrAssert(tileOp.getCol()); | ||||||
uint32_t row = getConstantIndexOrAssert(tileOp.getRow()); | ||||||
uint32_t maxQueueSize = deviceModel.getDmaMaxQueueSize(col, row); | ||||||
// Keep wait op if, either reaches the maximum queue size, or there | ||||||
// is a duplicate BD ID in the same tile. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
? |
||||||
uint32_t bdId = getConstantIndexOrAssert(bdIdOp.getValue()); | ||||||
bool isDuplicateBdId = llvm::any_of( | ||||||
tileConnectionToBdIdQueueMap, [&](const auto &entry) { | ||||||
return entry.first.first == tileOp && | ||||||
llvm::is_contained(entry.second, bdId); | ||||||
}); | ||||||
SmallVector<uint32_t> &bdIdQueue = | ||||||
tileConnectionToBdIdQueueMap[std::make_pair(tileOp, | ||||||
connectionOp)]; | ||||||
if (isDuplicateBdId || isPacketFlow || | ||||||
bdIdQueue.size() >= maxQueueSize) { | ||||||
bdIdQueue.clear(); | ||||||
} | ||||||
if (bdIdQueue.empty()) toErase = false; | ||||||
bdIdQueue.push_back(bdId); | ||||||
} | ||||||
} | ||||||
// Erase later to avoid invalidating the iterator. | ||||||
if (toErase) waitOpsToErase.push_back(waitOp); | ||||||
return WalkResult::advance(); | ||||||
}); | ||||||
if (res.wasInterrupted()) return failure(); | ||||||
|
||||||
for (AMDAIE::NpuDmaWaitOp waitOp : waitOpsToErase) { | ||||||
SmallVector<Value> asyncTokens(waitOp.getAsyncTokens()); | ||||||
// Erase the wait op. | ||||||
rewriter.eraseOp(waitOp); | ||||||
for (Value token : asyncTokens) { | ||||||
if (auto op = dyn_cast_if_present<AMDAIE::NpuHalfDmaCpyNdOp>( | ||||||
token.getDefiningOp())) { | ||||||
if (op.use_empty()) { | ||||||
rewriter.setInsertionPoint(op); | ||||||
TypeRange resultTypeRange = TypeRange{}; | ||||||
// Nullify the result to avoid issuing a token. | ||||||
rewriter.create<AMDAIE::NpuHalfDmaCpyNdOp>( | ||||||
op.getLoc(), resultTypeRange, op.getConnection(), op.getInput(), | ||||||
op.getMixedOffsets(), op.getMixedSizes(), op.getMixedStrides(), | ||||||
op.getBdId(), op.getChannel()); | ||||||
rewriter.eraseOp(op); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return success(); | ||||||
} | ||||||
|
||||||
class AMDAIEFoldDmaWaitsPass | ||||||
: public impl::AMDAIEFoldDmaWaitsBase<AMDAIEFoldDmaWaitsPass> { | ||||||
public: | ||||||
void getDependentDialects(DialectRegistry ®istry) const override { | ||||||
registry.insert<AMDAIEDialect>(); | ||||||
} | ||||||
|
||||||
AMDAIEFoldDmaWaitsPass() = default; | ||||||
AMDAIEFoldDmaWaitsPass(const AMDAIEFoldDmaWaitsPass &pass){}; | ||||||
void runOnOperation() override; | ||||||
}; | ||||||
|
||||||
void AMDAIEFoldDmaWaitsPass::runOnOperation() { | ||||||
Operation *parentOp = getOperation(); | ||||||
|
||||||
auto targetAttr = IREE::HAL::ExecutableTargetAttr::lookup(parentOp); | ||||||
std::optional<AMDAIEDevice> maybeDevice = getConfigAMDAIEDevice(targetAttr); | ||||||
if (!maybeDevice) { | ||||||
parentOp->emitOpError() | ||||||
<< "has no AMDAIEDevice in the target attribute configuration. This " | ||||||
"device-specific information is required to fold DMA wait " | ||||||
"ops."; | ||||||
return signalPassFailure(); | ||||||
} | ||||||
AMDAIE::AMDAIEDeviceModel deviceModel = | ||||||
AMDAIE::getDeviceModel(maybeDevice.value()); | ||||||
|
||||||
WalkResult res = parentOp->walk([&](AMDAIE::WorkgroupOp workgroupOp) { | ||||||
AMDAIE::ControlCodeOp controlCodeOp = workgroupOp.getControlCode(); | ||||||
if (failed(foldDmaWaits(deviceModel, controlCodeOp))) { | ||||||
return WalkResult::interrupt(); | ||||||
} | ||||||
return WalkResult::advance(); | ||||||
}); | ||||||
if (res.wasInterrupted()) return signalPassFailure(); | ||||||
} | ||||||
|
||||||
} // namespace | ||||||
|
||||||
std::unique_ptr<Pass> createAMDAIEFoldDmaWaitsPass() { | ||||||
return std::make_unique<AMDAIEFoldDmaWaitsPass>(); | ||||||
} | ||||||
|
||||||
} // 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
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.
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 still kinda think a function at the level
FailureOr canFoldBasedOnNpuHalfDmaCpyNdOp(...)
would make for slightly easier to read (less indented) code.
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.
Yes, made it a function now