Skip to content

Commit

Permalink
Start adding end-to-end tests from arith
Browse files Browse the repository at this point in the history
  • Loading branch information
math-fehr committed May 22, 2024
1 parent 37746d1 commit 8acfe91
Show file tree
Hide file tree
Showing 8 changed files with 403 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: analyze-irdl-invariants %s %S/arith.irdl | filecheck %s

pdl.pattern @AddIAddConstant : benefit(0) {
%type = pdl.type
pdl.apply_native_constraint "is_integer_attr"(%type : !pdl.type)

%c0_attr = pdl.attribute : %type
%c1_attr = pdl.attribute : %type

%c0_op = pdl.operation "arith.constant" {"value" = %c0_attr} -> (%type : !pdl.type)
%c1_op = pdl.operation "arith.constant" {"value" = %c1_attr} -> (%type : !pdl.type)

%x = pdl.operand : %type
%c0 = pdl.result 0 of %c0_op
%c1 = pdl.result 0 of %c1_op

%add1_op = pdl.operation "arith.addi"(%x, %c0 : !pdl.value, !pdl.value) -> (%type : !pdl.type)
%add1 = pdl.result 0 of %add1_op

%add2 = pdl.operation "arith.addi"(%add1, %c1 : !pdl.value, !pdl.value) -> (%type : !pdl.type)

pdl.rewrite %add2 {
%res = pdl.apply_native_rewrite "addi"(%c0_attr, %c1_attr : !pdl.attribute, !pdl.attribute) : !pdl.attribute
%folded_op = pdl.operation "arith.constant" {"value" = %res} -> (%type : !pdl.type)
%folded = pdl.result 0 of %folded_op
%add = pdl.operation "arith.addi"(%x, %folded : !pdl.value, !pdl.value) -> (%type : !pdl.type)
pdl.replace %add2 with %add
}
}

// CHECK: PDL rewrite will not break IRDL invariants
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: analyze-irdl-invariants %s %S/arith.irdl | filecheck %s

// addi(muli(x, -1), y) -> subi(y, x)
pdl.pattern @AddIMulNegativeOneLhs : benefit(0) {
%type = pdl.type

%x = pdl.operand : %type
%y = pdl.operand : %type
%m1_attr = pdl.attribute : %type

pdl.apply_native_constraint "is_minus_one"(%m1_attr : !pdl.attribute)

%m1_op = pdl.operation "arith.constant" {"value" = %m1_attr} -> (%type : !pdl.type)
%m1 = pdl.result 0 of %m1_op

%mul_op = pdl.operation "arith.muli"(%x, %m1 : !pdl.value, !pdl.value) -> (%type : !pdl.type)
%mul = pdl.result 0 of %mul_op

%add = pdl.operation "arith.addi"(%mul, %y : !pdl.value, !pdl.value) -> (%type : !pdl.type)

pdl.rewrite %add {
%sub = pdl.operation "arith.subi"(%y, %x : !pdl.value, !pdl.value) -> (%type : !pdl.type)
pdl.replace %add with %sub
}
}

// CHECK: PDL rewrite will not break IRDL invariants
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: analyze-irdl-invariants %s %S/arith.irdl | filecheck %s

// addi(x, muli(y, -1)) -> subi(x, y)
pdl.pattern @AddIMulNegativeOneRhs : benefit(0) {
%type = pdl.type

%x = pdl.operand : %type
%y = pdl.operand : %type
%m1_attr = pdl.attribute : %type

pdl.apply_native_constraint "is_minus_one"(%m1_attr : !pdl.attribute)

%m1_op = pdl.operation "arith.constant" {"value" = %m1_attr} -> (%type : !pdl.type)
%m1 = pdl.result 0 of %m1_op

%mul_op = pdl.operation "arith.muli"(%y, %m1 : !pdl.value, !pdl.value) -> (%type : !pdl.type)
%mul = pdl.result 0 of %mul_op

%add = pdl.operation "arith.addi"(%x, %mul : !pdl.value, !pdl.value) -> (%type : !pdl.type)

pdl.rewrite %add {
%sub = pdl.operation "arith.subi"(%x, %y : !pdl.value, !pdl.value) -> (%type : !pdl.type)
pdl.replace %add with %sub
}
}

// CHECK: PDL rewrite will not break IRDL invariants
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: analyze-irdl-invariants %s %S/arith.irdl | filecheck %s

// addi(subi(c0, x), c1) -> addi(c0 + c1, x)
pdl.pattern @AddISubConstantLHS : benefit(0) {
%type = pdl.type

%c0_attr = pdl.attribute : %type
%c1_attr = pdl.attribute : %type

%c0_op = pdl.operation "arith.constant" {"value" = %c0_attr} -> (%type : !pdl.type)
%c1_op = pdl.operation "arith.constant" {"value" = %c1_attr} -> (%type : !pdl.type)

%x = pdl.operand : %type
%c0 = pdl.result 0 of %c0_op
%c1 = pdl.result 0 of %c1_op

%sub1_op = pdl.operation "arith.subi"(%c0, %x : !pdl.value, !pdl.value) -> (%type : !pdl.type)
%sub1 = pdl.result 0 of %sub1_op

%add2 = pdl.operation "arith.addi"(%sub1, %c1 : !pdl.value, !pdl.value) -> (%type : !pdl.type)

pdl.rewrite %add2 {
%res = pdl.apply_native_rewrite "addi"(%c0_attr, %c1_attr : !pdl.attribute, !pdl.attribute) : !pdl.attribute
%folded_op = pdl.operation "arith.constant" {"value" = %res} -> (%type : !pdl.type)
%folded = pdl.result 0 of %folded_op
%sub = pdl.operation "arith.subi"(%folded, %x : !pdl.value, !pdl.value) -> (%type : !pdl.type)
pdl.replace %add2 with %sub
}
}

// CHECK: PDL rewrite will not break IRDL invariants
34 changes: 34 additions & 0 deletions tests/filecheck/analyze_irdl_invariants/arith/and_of_extsi.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: analyze-irdl-invariants %s %S/arith.irdl | filecheck %s

// and extsi(x), extsi(y) -> extsi(and(x,y))
pdl.pattern @AndOfExtSI : benefit(0) {
%type = pdl.type
%new_type = pdl.type

pdl.apply_native_constraint "is_greater_integer_type"(%new_type, %type : !pdl.type, !pdl.type)
pdl.apply_native_constraint "is_tensor"(%type : !pdl.type)
pdl.apply_native_constraint "is_tensor"(%new_type : !pdl.type)

%i64 = pdl.type : i64

%x = pdl.operand : %type
%y = pdl.operand : %type

%extsi_x_op = pdl.operation "arith.extsi"(%x : !pdl.value) -> (%new_type : !pdl.type)
%extsi_x = pdl.result 0 of %extsi_x_op

%extsi_y_op = pdl.operation "arith.extsi"(%y : !pdl.value) -> (%new_type : !pdl.type)
%extsi_y = pdl.result 0 of %extsi_y_op

%and_op = pdl.operation "arith.andi"(%extsi_x, %extsi_y : !pdl.value, !pdl.value) -> (%new_type : !pdl.type)

pdl.rewrite %and_op {
%new_and = pdl.operation "arith.andi"(%x, %y : !pdl.value, !pdl.value) -> (%type : !pdl.type)
%and = pdl.result 0 of %new_and

%new_extsi = pdl.operation "arith.extsi"(%and : !pdl.value) -> (%new_type : !pdl.type)
pdl.replace %and_op with %new_extsi
}
}

// CHECK: PDL rewrite will not break IRDL invariants
34 changes: 34 additions & 0 deletions tests/filecheck/analyze_irdl_invariants/arith/and_of_extui.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: analyze-irdl-invariants %s %S/arith.irdl | filecheck %s

// and extui(x), extui(y) -> extui(and(x,y))
pdl.pattern @AndOfExtUI : benefit(0) {
%type = pdl.type
%new_type = pdl.type

pdl.apply_native_constraint "is_greater_integer_type"(%new_type, %type : !pdl.type, !pdl.type)
pdl.apply_native_constraint "is_tensor"(%type : !pdl.type)
pdl.apply_native_constraint "is_tensor"(%new_type : !pdl.type)

%i64 = pdl.type : i64

%x = pdl.operand : %type
%y = pdl.operand : %type

%extui_x_op = pdl.operation "arith.extui"(%x : !pdl.value) -> (%new_type : !pdl.type)
%extui_x = pdl.result 0 of %extui_x_op

%extui_y_op = pdl.operation "arith.extui"(%y : !pdl.value) -> (%new_type : !pdl.type)
%extui_y = pdl.result 0 of %extui_y_op

%and_op = pdl.operation "arith.andi"(%extui_x, %extui_y : !pdl.value, !pdl.value) -> (%new_type : !pdl.type)

pdl.rewrite %and_op {
%new_and = pdl.operation "arith.andi"(%x, %y : !pdl.value, !pdl.value) -> (%type : !pdl.type)
%and = pdl.result 0 of %new_and

%new_extui = pdl.operation "arith.extui"(%and : !pdl.value) -> (%new_type : !pdl.type)
pdl.replace %and_op with %new_extui
}
}

// CHECK: PDL rewrite will not break IRDL invariants
187 changes: 187 additions & 0 deletions tests/filecheck/analyze_irdl_invariants/arith/arith.irdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
irdl.dialect @builtin {
irdl.type @index

irdl.attribute @signedness {
%signless = irdl.is "signless"
%signed = irdl.is "signed"
%unsigned = irdl.is "unsigned"
%value = irdl.any_of(%signless, %signed, %unsigned)
irdl.parameters(%value)
}

irdl.type @integer_type {
%bitwidth = irdl.base "#int"
%signedness = irdl.base @signedness
irdl.parameters(%bitwidth, %signedness)
}

irdl.attribute @integer_attr {
%index = irdl.base @index
%integer = irdl.base @integer_type
%t = irdl.any_of(%index, %integer)
%value = irdl.base "#int"
irdl.parameters(%value, %t)
}

irdl.type @tensor {
%zero_shape = irdl.is "zero_shape"
%nonzero_shape = irdl.base "#int"
%shape = irdl.any_of (%zero_shape, %nonzero_shape)
%element_type = irdl.any
irdl.parameters(%shape, %element_type)
}

irdl.type @vector {
%zero_shape = irdl.is "zero_shape"
%nonzero_shape = irdl.base "#int"
%shape = irdl.any_of (%zero_shape, %nonzero_shape)
%element_type = irdl.any
irdl.parameters(%shape, %element_type)
}
}

irdl.dialect @arith {
irdl.operation @constant {
// %attr = irdl.any
// irdl.attributes(%attr)
%type = irdl.any
irdl.results(%type)
}

irdl.operation @addi {
%index = irdl.base @builtin::@index
%signless_opcode = irdl.is "signless"
%signless = irdl.parametric @builtin::@signedness<%signless_opcode>
%bitwidth = irdl.any
%integer = irdl.parametric @builtin::@integer_type<%bitwidth, %signless>
%signless_integer = irdl.any_of(%index, %integer)
%nonzero_shape = irdl.base "#int"
%shape = irdl.any
%vector = irdl.parametric @builtin::@vector<%nonzero_shape, %signless_integer>
%tensor = irdl.parametric @builtin::@tensor<%shape, %signless_integer>
%type = irdl.any_of(%signless_integer, %vector, %tensor)
irdl.operands(%type, %type)
irdl.results(%type)
}

irdl.operation @subi {
%index = irdl.base @builtin::@index
%signless_opcode = irdl.is "signless"
%signless = irdl.parametric @builtin::@signedness<%signless_opcode>
%bitwidth = irdl.any
%integer = irdl.parametric @builtin::@integer_type<%bitwidth, %signless>
%signless_integer = irdl.any_of(%index, %integer)
%nonzero_shape = irdl.base "#int"
%shape = irdl.any
%vector = irdl.parametric @builtin::@vector<%nonzero_shape, %signless_integer>
%tensor = irdl.parametric @builtin::@tensor<%shape, %signless_integer>
%type = irdl.any_of(%signless_integer, %vector, %tensor)
irdl.operands(%type, %type)
irdl.results(%type)
}

irdl.operation @muli {
%index = irdl.base @builtin::@index
%signless_opcode = irdl.is "signless"
%signless = irdl.parametric @builtin::@signedness<%signless_opcode>
%bitwidth = irdl.any
%integer = irdl.parametric @builtin::@integer_type<%bitwidth, %signless>
%signless_integer = irdl.any_of(%index, %integer)
%nonzero_shape = irdl.base "#int"
%shape = irdl.any
%vector = irdl.parametric @builtin::@vector<%nonzero_shape, %signless_integer>
%tensor = irdl.parametric @builtin::@tensor<%shape, %signless_integer>
%type = irdl.any_of(%signless_integer, %vector, %tensor)
irdl.operands(%type, %type)
irdl.results(%type)
}

irdl.operation @andi {
%index = irdl.base @builtin::@index
%signless_opcode = irdl.is "signless"
%signless = irdl.parametric @builtin::@signedness<%signless_opcode>
%bitwidth = irdl.any
%integer = irdl.parametric @builtin::@integer_type<%bitwidth, %signless>
%signless_integer = irdl.any_of(%index, %integer)
%nonzero_shape = irdl.base "#int"
%shape = irdl.any
%vector = irdl.parametric @builtin::@vector<%nonzero_shape, %signless_integer>
%tensor = irdl.parametric @builtin::@tensor<%shape, %signless_integer>
%type = irdl.any_of(%signless_integer, %vector, %tensor)
irdl.operands(%type, %type)
irdl.results(%type)
}

irdl.operation @cmpi {
%index = irdl.base @builtin::@index
%signless_opcode = irdl.is "signless"
%signless = irdl.parametric @builtin::@signedness<%signless_opcode>
%bitwidth = irdl.any
%integer = irdl.parametric @builtin::@integer_type<%bitwidth, %signless>
%signless_integer = irdl.any_of(%index, %integer)
%shape = irdl.any
%vector = irdl.parametric @builtin::@vector<%shape, %signless_integer>
%tensor = irdl.parametric @builtin::@tensor<%shape, %signless_integer>
%type = irdl.any_of(%signless_integer, %vector, %tensor)

%bool = irdl.is i1
%vector_bool = irdl.parametric @builtin::@vector<%shape, %bool>
%tensor_bool = irdl.parametric @builtin::@tensor<%shape, %bool>
%type_bool = irdl.any_of(%bool, %vector_bool, %tensor_bool)

irdl.operands(%type, %type)
irdl.results(%type_bool)
}

irdl.operation @extui {
%signless_opcode_in = irdl.is "signless"
%signless_in = irdl.parametric @builtin::@signedness<%signless_opcode_in>
%bitwidth_in = irdl.any
%signless_integer_in = irdl.parametric @builtin::@integer_type<%bitwidth_in, %signless_in>

%shape = irdl.any
%tensor_in = irdl.parametric @builtin::@tensor<%shape, %signless_integer_in>
%vector_in = irdl.parametric @builtin::@vector<%shape, %signless_integer_in>
%type_in = irdl.any_of(%signless_integer_in, %vector_in, %tensor_in)


%signless_opcode_out = irdl.is "signless"
%signless_out = irdl.parametric @builtin::@signedness<%signless_opcode_out>
%bitwidth_out = irdl.any
%signless_integer_out = irdl.parametric @builtin::@integer_type<%bitwidth_out, %signless_out>

%tensor_out = irdl.parametric @builtin::@tensor<%shape, %signless_integer_out>
%vector_out = irdl.parametric @builtin::@vector<%shape, %signless_integer_out>
%type_out = irdl.any_of(%signless_integer_out, %vector_out, %tensor_out)


irdl.operands(%type_in)
irdl.results(%type_out)
}

irdl.operation @extsi {
%signless_opcode_in = irdl.is "signless"
%signless_in = irdl.parametric @builtin::@signedness<%signless_opcode_in>
%bitwidth_in = irdl.any
%signless_integer_in = irdl.parametric @builtin::@integer_type<%bitwidth_in, %signless_in>

%shape = irdl.any
%tensor_in = irdl.parametric @builtin::@tensor<%shape, %signless_integer_in>
%vector_in = irdl.parametric @builtin::@vector<%shape, %signless_integer_in>
%type_in = irdl.any_of(%signless_integer_in, %vector_in, %tensor_in)


%signless_opcode_out = irdl.is "signless"
%signless_out = irdl.parametric @builtin::@signedness<%signless_opcode_out>
%bitwidth_out = irdl.any
%signless_integer_out = irdl.parametric @builtin::@integer_type<%bitwidth_out, %signless_out>

%tensor_out = irdl.parametric @builtin::@tensor<%shape, %signless_integer_out>
%vector_out = irdl.parametric @builtin::@vector<%shape, %signless_integer_out>
%type_out = irdl.any_of(%signless_integer_out, %vector_out, %tensor_out)


irdl.operands(%type_in)
irdl.results(%type_out)
}
}
Loading

0 comments on commit 8acfe91

Please sign in to comment.