Skip to content
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

[LowerToAIE] Fix for non-deterministic error (observed with convolution) #694

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "iree-amd-aie/IR/AMDAIEOps.h"
#include "iree-amd-aie/Transforms/AMDAIEUtils.h"
#include "iree-amd-aie/Transforms/Passes.h"
#include "iree-amd-aie/Transforms/Transforms.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/Iterators.h"
Expand All @@ -43,12 +42,26 @@ void remapOperands(Operation *op, IRMapping &mapper) {
}
}

/// It is dangerous to erase ops with `rewriter` without erasing them from
/// `mapper` too, as addresses of Operations/Values can be reused, resulting in
/// unexpected key-value pairs in `mapper`. Use this utility if `mapper` might
/// be used after `op` is erased.
void eraseOp(IRRewriter &rewriter, IRMapping &mapper, Operation *op) {
for (Value result : op->getResults()) {
mapper.erase(result);
}
mapper.erase(op);
op->dropAllUses();
rewriter.eraseOp(op);
}

//===----------------------------------------------------------------------===//
// Convert amdaie.core operation to aie.core
//===----------------------------------------------------------------------===//

namespace {


/// Utility to convert vectors of `size` and `stride` into an
/// `AIE::BDDimLayoutArrayAttr`.
AIE::BDDimLayoutArrayAttr convertSizeStrideToBDDimLayoutArrayAttr(
Expand Down Expand Up @@ -259,7 +272,7 @@ LogicalResult coreLinalgOpToAIE(IRRewriter &rewriter, linalg::LinalgOp linalgOp,
OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPoint(linalgOp);
rewriter.clone(*(linalgOp.getOperation()), mapper);
rewriter.eraseOp(linalgOp);
eraseOp(rewriter, mapper, linalgOp);
return success();
}

Expand Down Expand Up @@ -422,8 +435,7 @@ LogicalResult coreToAIE(IRRewriter &rewriter, AMDAIE::CoreOp coreOp,
return failure();
}
for (auto *op : toBeErased) {
op->dropAllUses();
rewriter.eraseOp(op);
eraseOp(rewriter, mapper, op);
}

mapper.map(coreOp.getResult(), aieCoreOp.getResult());
Expand Down Expand Up @@ -687,7 +699,7 @@ LogicalResult controlCodeToAie(IRRewriter &rewriter,
bindingsMapper);
})
.Case<AMDAIE::EndOp>([&](auto endOp) {
rewriter.eraseOp(endOp);
eraseOp(rewriter, mapper, endOp);
return success();
})
.Default([&](Operation *op) {
Expand All @@ -701,8 +713,7 @@ LogicalResult controlCodeToAie(IRRewriter &rewriter,
});
if (res.wasInterrupted()) return failure();
for (auto *op : toBeErased) {
op->dropAllUses();
rewriter.eraseOp(op);
eraseOp(rewriter, mapper, op);
}
return success();
}
Expand Down Expand Up @@ -917,7 +928,8 @@ LogicalResult lowerToAIE(ModuleOp moduleOp) {
rewriter.moveOpBefore(ipuFuncOp, deviceBlock, deviceBlock->end());
// After walking the FuncOp, it has been converted into a DeviceOp and can
// safely be erased.
rewriter.eraseOp(funcOp);
eraseOp(rewriter, mapper, funcOp);

return WalkResult::advance();
});
if (funcRes.wasInterrupted()) return failure();
Expand Down
Loading