Skip to content

Commit

Permalink
core: Fix terminator conditions for unregistered operations (#1201)
Browse files Browse the repository at this point in the history
This PR refines terminator conditions for unregistered operations.
Namely, if we allow unregistered operations, then their terminator
conditions are implicitly fulfilled.
It also modifies `has_trait` to assume the existence of any trait for
unregistered operations.

This is modelled after the [`mayBeValidWithoutTerminator`][2] and
[`mightHaveTrait`][1] in MLIR.


Resolves #1167

[1]:
https://github.com/llvm/llvm-project/blob/268032f6f10d595bb723f6b4a21632a9f3b35be8/mlir/include/mlir/IR/OperationSupport.h#L288
[2]:
https://github.com/llvm/llvm-project/blob/268032f6f10d595bb723f6b4a21632a9f3b35be8/mlir/lib/IR/Verifier.cpp#L100
  • Loading branch information
compor committed Jun 28, 2023
1 parent 00608a1 commit e54676e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 0 additions & 2 deletions tests/filecheck/parser-printer/unregistered_dialect.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
%y = "op_with_res"() {otherattr = #unknown_attr<b2 ...2 [] <<>>>} : () -> (i32)
%z = "op_with_operands"(%y, %y) : (i32, i32) -> !unknown_type<{[<()>]}>
"op"() {ab = !unknown_singleton_type} : () -> ()
"test.termop"() : () -> ()
}) {testattr = "foo"} : () -> i32

// CHECK: %{{.*}} = "region_op"() ({
// CHECK-NEXT: %{{.*}} = "op_with_res"() {"otherattr" = #unknown_attr<b2 ...2 [] <<>>>} : () -> i32
// CHECK-NEXT: %{{.*}} = "op_with_operands"(%{{.*}}, %{{.*}}) : (i32, i32) -> !unknown_type<{[<()>]}>
// CHECK-NEXT: "op"() {"ab" = !unknown_singleton_type} : () -> ()
// CHECK-NEXT: "test.termop"() : () -> ()
// CHECK-NEXT: }) {"testattr" = "foo"} : () -> i32

}) : () -> ()
14 changes: 13 additions & 1 deletion xdsl/ir/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,10 +899,22 @@ def clone(
return op

@classmethod
def has_trait(cls, trait: type[OpTrait], parameters: Any = None) -> bool:
def has_trait(
cls,
trait: type[OpTrait],
parameters: Any = None,
value_if_unregistered: bool = True,
) -> bool:
"""
Check if the operation implements a trait with the given parameters.
If the operation is not registered, return value_if_unregisteed instead.
"""

from xdsl.dialects.builtin import UnregisteredOp

if issubclass(cls, UnregisteredOp):
return value_if_unregistered

return cls.get_trait(trait, parameters) is not None

@classmethod
Expand Down

0 comments on commit e54676e

Please sign in to comment.