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

Add canonicalizer for airrt.alloc and dealloc #667

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions mlir/include/air/Dialect/AIRRt/AIRRtOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def AIRRt_AllocOp: AIRRt_Op<"alloc", []> {
let description = [{
AIRRt Allocation Op
}];
let hasCanonicalizer = 1;
}

def AIRRt_DeallocOp: AIRRt_Op<"dealloc", []> {
Expand All @@ -201,6 +202,7 @@ def AIRRt_DeallocOp: AIRRt_Op<"dealloc", []> {
let description = [{
AIRRt Deallocation Op
}];
let hasCanonicalizer = 1;
}

def AIRRt_WaitAllOp : AIRRt_Op<"wait_all", []> {
Expand Down
28 changes: 28 additions & 0 deletions mlir/lib/Dialect/AIRRt/IR/AIRRtDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,35 @@ static LogicalResult FoldWaitAll(WaitAllOp op, PatternRewriter &rewriter) {
return failure();
}

static LogicalResult FoldAlloc(AllocOp op, PatternRewriter &rewriter) {
auto memref = op.getResult();
if (!llvm::range_size(memref.getUsers())) {
rewriter.eraseOp(op);
return success();
}
return failure();
}

static LogicalResult FoldDealloc(DeallocOp op, PatternRewriter &rewriter) {
auto memref = op.getOperand();
if (llvm::range_size(memref.getUsers()) == 1) {
rewriter.eraseOp(op);
return success();
}
return failure();
}

void WaitAllOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add(FoldWaitAll);
}

void AllocOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add(FoldAlloc);
}

void DeallocOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
MLIRContext *context) {
patterns.add(FoldDealloc);
}
8 changes: 8 additions & 0 deletions mlir/test/Dialect/AIRRt/airrt_canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ func.func @wait_all_1(%e0 : !airrt.event, %e1 : !airrt.event, %e2 : !airrt.event
%7 = airrt.wait_all %6 : !airrt.event
return %7 : !airrt.event
}

// CHECK-LABEL: alloc_dealloc
// CHECK-NEXT: return
func.func @alloc_dealloc() {
%0 = airrt.alloc : memref<1x4x4x16xi32, 1>
airrt.dealloc %0 : memref<1x4x4x16xi32, 1>
return
}
Loading