Skip to content

Commit 7d055af

Browse files
caojoshuajoker-eph
andauthored
[mlir][Symbol] Add verification that symbol's parent is a SymbolTable (#80590)
Following the discussion in https://discourse.llvm.org/t/symboltable-and-symbol-parent-child-relationship/75446, we should enforce that a symbol's immediate parent is a symbol table. I changed some tests to pass the verification. In most cases, we can wrap the func with a module, change the func to another op with regions i.e. scf.if, or change the expected error message. --------- Co-authored-by: Mehdi Amini <[email protected]>
1 parent d53043f commit 7d055af

File tree

13 files changed

+68
-85
lines changed

13 files changed

+68
-85
lines changed

mlir/include/mlir/IR/SymbolInterfaces.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ def Symbol : OpInterface<"SymbolOpInterface"> {
171171
if (concreteOp.isDeclaration() && concreteOp.isPublic())
172172
return concreteOp.emitOpError("symbol declaration cannot have public "
173173
"visibility");
174+
auto parent = $_op->getParentOp();
175+
if (parent && !parent->hasTrait<OpTrait::SymbolTable>() && parent->isRegistered()) {
176+
return concreteOp.emitOpError("symbol's parent must have the SymbolTable "
177+
"trait");
178+
}
174179
return success();
175180
}];
176181

mlir/test/Dialect/LLVMIR/global.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ llvm.mlir.global internal constant @constant(37.0) : !llvm.label
132132
// -----
133133

134134
func.func @foo() {
135-
// expected-error @+1 {{must appear at the module level}}
135+
// expected-error @+1 {{op symbol's parent must have the SymbolTable trait}}
136136
llvm.mlir.global internal @bar(42) : i32
137137

138138
return

mlir/test/Dialect/Linalg/transform-op-replace.mlir

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ module attributes {transform.with_named_sequence} {
1212
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
1313
%0 = transform.structured.match ops{["func.func"]} in %arg1 : (!transform.any_op) -> !transform.any_op
1414
transform.structured.replace %0 {
15-
func.func @foo() {
16-
"dummy_op"() : () -> ()
15+
builtin.module {
16+
func.func @foo() {
17+
"dummy_op"() : () -> ()
18+
}
1719
}
1820
} : (!transform.any_op) -> !transform.any_op
1921
transform.yield

mlir/test/Dialect/Transform/ops-invalid.mlir

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,9 @@ module {
433433
// -----
434434

435435
module attributes { transform.with_named_sequence} {
436-
// expected-note @below {{ancestor transform op}}
437436
transform.sequence failures(suppress) {
438437
^bb0(%arg0: !transform.any_op):
439-
// expected-error @below {{cannot be defined inside another transform op}}
438+
// expected-error @below {{op symbol's parent must have the SymbolTable trai}}
440439
transform.named_sequence @nested() {
441440
transform.yield
442441
}

mlir/test/IR/invalid-func-op.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func.func @func_op() {
3131
// -----
3232

3333
func.func @func_op() {
34-
// expected-error@+1 {{entry block must have 1 arguments to match function signature}}
34+
// expected-error@+1 {{op symbol's parent must have the SymbolTable trait}}
3535
func.func @mixed_named_arguments(f32) {
3636
^entry:
3737
return
@@ -42,7 +42,7 @@ func.func @func_op() {
4242
// -----
4343

4444
func.func @func_op() {
45-
// expected-error@+1 {{type of entry block argument #0('i32') must match the type of the corresponding argument in function signature('f32')}}
45+
// expected-error@+1 {{op symbol's parent must have the SymbolTable trait}}
4646
func.func @mixed_named_arguments(f32) {
4747
^entry(%arg : i32):
4848
return

mlir/test/IR/region.mlir

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,17 @@ func.func @named_region_has_wrong_number_of_blocks() {
8787
// CHECK: test.single_no_terminator_op
8888
"test.single_no_terminator_op"() (
8989
{
90-
func.func @foo1() { return }
91-
func.func @foo2() { return }
90+
%foo = arith.constant 1 : i32
9291
}
9392
) : () -> ()
9493

9594
// CHECK: test.variadic_no_terminator_op
9695
"test.variadic_no_terminator_op"() (
9796
{
98-
func.func @foo1() { return }
97+
%foo = arith.constant 1 : i32
9998
},
10099
{
101-
func.func @foo2() { return }
100+
%bar = arith.constant 1 : i32
102101
}
103102
) : () -> ()
104103

mlir/test/IR/traits.mlir

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,13 @@ func.func @failedHasDominanceScopeOutsideDominanceFreeScope() -> () {
572572

573573
// Ensure that SSACFG regions of operations in GRAPH regions are
574574
// checked for dominance
575-
func.func @illegalInsideDominanceFreeScope() -> () {
575+
func.func @illegalInsideDominanceFreeScope(%cond: i1) -> () {
576576
test.graph_region {
577-
func.func @test() -> i1 {
578-
^bb1:
577+
scf.if %cond {
579578
// expected-error @+1 {{operand #0 does not dominate this use}}
580579
%2:3 = "bar"(%1) : (i64) -> (i1,i1,i1)
581580
// expected-note @+1 {{operand defined here}}
582-
%1 = "baz"(%2#0) : (i1) -> (i64)
583-
return %2#1 : i1
581+
%1 = "baz"(%2#0) : (i1) -> (i64)
584582
}
585583
"terminator"() : () -> ()
586584
}
@@ -591,20 +589,21 @@ func.func @illegalInsideDominanceFreeScope() -> () {
591589

592590
// Ensure that SSACFG regions of operations in GRAPH regions are
593591
// checked for dominance
594-
func.func @illegalCDFGInsideDominanceFreeScope() -> () {
592+
func.func @illegalCFGInsideDominanceFreeScope(%cond: i1) -> () {
595593
test.graph_region {
596-
func.func @test() -> i1 {
597-
^bb1:
598-
// expected-error @+1 {{operand #0 does not dominate this use}}
599-
%2:3 = "bar"(%1) : (i64) -> (i1,i1,i1)
600-
cf.br ^bb4
601-
^bb2:
602-
cf.br ^bb2
603-
^bb4:
604-
%1 = "foo"() : ()->i64 // expected-note {{operand defined here}}
605-
return %2#1 : i1
594+
scf.if %cond {
595+
"test.ssacfg_region"() ({
596+
^bb1:
597+
// expected-error @+1 {{operand #0 does not dominate this use}}
598+
%2:3 = "bar"(%1) : (i64) -> (i1,i1,i1)
599+
cf.br ^bb4
600+
^bb2:
601+
cf.br ^bb2
602+
^bb4:
603+
%1 = "foo"() : ()->i64 // expected-note {{operand defined here}}
604+
}) : () -> ()
606605
}
607-
"terminator"() : () -> ()
606+
"terminator"() : () -> ()
608607
}
609608
return
610609
}

mlir/test/Transforms/canonicalize-dce.mlir

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ func.func @f(%arg0: f32, %pred: i1) {
7777

7878
// Test case: Recursively DCE into enclosed regions.
7979

80-
// CHECK: func @f(%arg0: f32)
81-
// CHECK-NEXT: func @g(%arg1: f32)
82-
// CHECK-NEXT: return
80+
// CHECK: func.func @f(%arg0: f32)
81+
// CHECK-NOT: arith.addf
8382

8483
func.func @f(%arg0: f32) {
85-
func.func @g(%arg1: f32) {
86-
%0 = "arith.addf"(%arg1, %arg1) : (f32, f32) -> f32
87-
return
88-
}
84+
"test.region"() (
85+
{
86+
%0 = "arith.addf"(%arg0, %arg0) : (f32, f32) -> f32
87+
}
88+
) : () -> ()
8989
return
9090
}
9191

mlir/test/Transforms/canonicalize.mlir

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,16 +424,15 @@ func.func @write_only_alloca_fold(%v: f32) {
424424
// CHECK-LABEL: func @dead_block_elim
425425
func.func @dead_block_elim() {
426426
// CHECK-NOT: ^bb
427-
func.func @nested() {
428-
return
427+
builtin.module {
428+
func.func @nested() {
429+
return
429430

430-
^bb1:
431-
return
431+
^bb1:
432+
return
433+
}
432434
}
433435
return
434-
435-
^bb1:
436-
return
437436
}
438437

439438
// CHECK-LABEL: func @dyn_shape_fold(%arg0: index, %arg1: index)

mlir/test/Transforms/constant-fold.mlir

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,12 +756,15 @@ func.func @cmpf_inf() -> (i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1
756756

757757
// CHECK-LABEL: func @nested_isolated_region
758758
func.func @nested_isolated_region() {
759+
// CHECK-NEXT: builtin.module {
759760
// CHECK-NEXT: func @isolated_op
760761
// CHECK-NEXT: arith.constant 2
761-
func.func @isolated_op() {
762-
%0 = arith.constant 1 : i32
763-
%2 = arith.addi %0, %0 : i32
764-
"foo.yield"(%2) : (i32) -> ()
762+
builtin.module {
763+
func.func @isolated_op() {
764+
%0 = arith.constant 1 : i32
765+
%2 = arith.addi %0, %0 : i32
766+
"foo.yield"(%2) : (i32) -> ()
767+
}
765768
}
766769

767770
// CHECK: "foo.unknown_region"

0 commit comments

Comments
 (0)