Skip to content

Commit

Permalink
mlir printing and parsing fix (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-luecke committed Jan 9, 2023
1 parent 55261e4 commit e5d03ea
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
12 changes: 12 additions & 0 deletions tests/filecheck/mlir-conversion/builtin_attrs.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,16 @@

// CHECK: "value1" = opaque<"test", "contents">, "value2" = opaque<"test", "contents"> : tensor<2xf64>

"func.func"() ({}) {function_type = () -> (),
symbol = @some_symbol,
sym_name = "symbol_attr"} : () -> ()

// CHECK: "symbol" = @some_symbol

"func.func"() ({}) {function_type = () -> (),
value1 = tensor<?xi32>,
sym_name = "non_static_shaped_tensor"} : () -> ()

// CHECK: tensor<?xi32>

}) : () -> ()
10 changes: 10 additions & 0 deletions tests/filecheck/mlir-conversion/with-bindings/symbol_tests.xdsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: xdsl-opt -t mlir %s | mlir-opt --mlir-print-op-generic > %t-1 && xdsl-opt -t mlir %s | mlir-opt --mlir-print-op-generic > %t-2 && diff %t-1 %t-2

// Tests if the non generic form can be printed.

// CHECK: module {
builtin.module() {
func.func() ["function_type" = !fun<[], []>, "symbol" = @some_symbol, "sym_name" = "symbol_attr", "sym_visibility" = "private"] {}

func.func() ["function_type" = !fun<[], []>, "value1" = !tensor<[-1 : !index], !i32>, "sym_name" = "unranked_tensor_type", "sym_visibility" = "private"] {}
}
5 changes: 5 additions & 0 deletions xdsl/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,11 @@ def parse_optional_mlir_attribute(self,
self.parse_char("]")
return ArrayAttr.from_list(contents)

# FlatSymbolRefAttr
if self.parse_optional_char("@"):
symbol_name = self.parse_alpha_num(skip_white_space=False)
return FlatSymbolRefAttr.from_str(symbol_name)

# tensor type
if (tensor := self.parse_optional_mlir_tensor()) is not None:
return tensor
Expand Down
5 changes: 3 additions & 2 deletions xdsl/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,9 @@ def print_one_elem(val: Attribute):
attribute = cast(AnyVectorType, attribute)
self.print(
"vector<" if isinstance(attribute, VectorType) else "tensor<")
self.print_list(attribute.shape.data,
lambda x: self.print(x.value.data), "x")
self.print_list(
attribute.shape.data, lambda x: self.print(x.value.data)
if x.value.data != -1 else self.print("?"), "x")
if len(attribute.shape.data) != 0:
self.print("x")
self.print(attribute.element_type)
Expand Down

0 comments on commit e5d03ea

Please sign in to comment.