-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewriting: Add convert-ml-program-to-memref (#2580)
I'm not 100% sure that this is the intended way that ml_program should be lowered but it seems to work for our ONNX kernels.
- Loading branch information
1 parent
14b4f9c
commit 8fbb3b2
Showing
5 changed files
with
111 additions
and
9 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
tests/filecheck/transforms/convert_ml_program_to_memref.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,11 @@ | ||
// RUN: xdsl-opt %s -p convert-ml-program-to-memref | filecheck %s | ||
|
||
ml_program.global private @global_same_type(dense<4> : tensor<4xi32>) : tensor<4xi32> | ||
|
||
%0 = ml_program.global_load_const @global_same_type : tensor<4xi32> | ||
|
||
// CHECK: builtin.module { | ||
// CHECK-NEXT: "memref.global"() <{"sym_name" = "global_same_type", "type" = memref<4xi32>, "initial_value" = dense<4> : tensor<4xi32>, "sym_visibility" = "private", "constant"}> : () -> () | ||
// CHECK-NEXT: %0 = memref.get_global @global_same_type : memref<4xi32> | ||
// CHECK-NEXT: %1 = "bufferization.to_tensor"(%0) : (memref<4xi32>) -> tensor<4xi32> | ||
// CHECK-NEXT: } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from typing import Any, cast | ||
|
||
from xdsl.dialects import bufferization, memref, ml_program | ||
from xdsl.dialects.builtin import ( | ||
ModuleOp, | ||
TensorType, | ||
UnitAttr, | ||
) | ||
from xdsl.ir import MLContext | ||
from xdsl.passes import ModulePass | ||
from xdsl.pattern_rewriter import ( | ||
GreedyRewritePatternApplier, | ||
PatternRewriter, | ||
PatternRewriteWalker, | ||
RewritePattern, | ||
op_type_rewrite_pattern, | ||
) | ||
|
||
|
||
class ConvertGlobalPattern(RewritePattern): | ||
@op_type_rewrite_pattern | ||
def match_and_rewrite( | ||
self, op: ml_program.Global, rewriter: PatternRewriter | ||
) -> None: | ||
if op.value is None: | ||
raise NotImplementedError( | ||
"Converting ml_program.global with no value not implemented" | ||
) | ||
assert isinstance(op_type := op.type, TensorType) | ||
op_type = cast(TensorType[Any], op_type) | ||
new_type = memref.MemRefType(op_type.element_type, op_type.shape) | ||
rewriter.replace_matched_op( | ||
( | ||
memref.Global.get( | ||
op.sym_name, | ||
new_type, | ||
op.value, | ||
op.sym_visibility, | ||
UnitAttr() if op.is_mutable is None else None, | ||
), | ||
) | ||
) | ||
|
||
|
||
class ConvertGlobalLoadConst(RewritePattern): | ||
@op_type_rewrite_pattern | ||
def match_and_rewrite( | ||
self, op: ml_program.GlobalLoadConstant, rewriter: PatternRewriter | ||
) -> None: | ||
assert isinstance(op_type := op.result.type, TensorType) | ||
op_type = cast(TensorType[Any], op_type) | ||
new_type = memref.MemRefType(op_type.element_type, op_type.shape) | ||
rewriter.replace_matched_op( | ||
( | ||
mem := memref.GetGlobal.get(op.global_attr, new_type), | ||
bufferization.ToTensorOp(mem.memref), | ||
) | ||
) | ||
|
||
|
||
class ConvertMlProgramToMemrefPass(ModulePass): | ||
""" | ||
Converts operations in the `ml_program` dialect to `memref`. | ||
`ml_program` operations are at the `tensor` level of abstraction, so some of the | ||
rewrites insert `bufferization` ops to bridge the gap to existing consumers of global | ||
`tensor`s. | ||
""" | ||
|
||
name = "convert-ml-program-to-memref" | ||
|
||
def apply(self, ctx: MLContext, op: ModuleOp) -> None: | ||
PatternRewriteWalker( | ||
GreedyRewritePatternApplier( | ||
[ | ||
ConvertGlobalPattern(), | ||
ConvertGlobalLoadConst(), | ||
] | ||
), | ||
apply_recursively=False, | ||
).rewrite_module(op) |