Skip to content

Commit 67a339e

Browse files
committed
[MLIR] Disallow sym_visibility, sym_name and type attributes in the parsed attribute dictionary.
Differential Revision: https://reviews.llvm.org/D94200
1 parent 93b54b7 commit 67a339e

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

mlir/lib/IR/FunctionImplementation.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ mlir::impl::parseFunctionLikeOp(OpAsmParser &parser, OperationState &result,
180180
return failure();
181181

182182
// Parse the function signature.
183-
auto signatureLocation = parser.getCurrentLocation();
183+
llvm::SMLoc signatureLocation = parser.getCurrentLocation();
184184
bool isVariadic = false;
185185
if (parseFunctionSignature(parser, allowVariadic, entryArgs, argTypes,
186186
argAttrs, isVariadic, resultTypes, resultAttrs))
@@ -196,9 +196,24 @@ mlir::impl::parseFunctionLikeOp(OpAsmParser &parser, OperationState &result,
196196
<< (errorMessage.empty() ? "" : ": ") << errorMessage;
197197

198198
// If function attributes are present, parse them.
199-
if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
199+
NamedAttrList parsedAttributes;
200+
llvm::SMLoc attributeDictLocation = parser.getCurrentLocation();
201+
if (parser.parseOptionalAttrDictWithKeyword(parsedAttributes))
200202
return failure();
201203

204+
// Disallow attributes that are inferred from elsewhere in the attribute
205+
// dictionary.
206+
for (StringRef disallowed :
207+
{SymbolTable::getVisibilityAttrName(), SymbolTable::getSymbolAttrName(),
208+
getTypeAttrName()}) {
209+
if (parsedAttributes.get(disallowed))
210+
return parser.emitError(attributeDictLocation, "'")
211+
<< disallowed
212+
<< "' is an inferred attribute and should not be specified in the "
213+
"explicit attribute dictionary";
214+
}
215+
result.attributes.append(parsedAttributes);
216+
202217
// Add the attributes to the function arguments.
203218
assert(argAttrs.size() == argTypes.size());
204219
assert(resultAttrs.size() == resultTypes.size());

mlir/test/Dialect/Tosa/inlining.mlir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ func @inlined_if_fn(%arg0: tensor<f32>, %arg1: tensor<f32>, %arg2: tensor<i1>) -
1919
}) : (tensor<i1>, tensor<f32>, tensor<f32>) -> tensor<f32>
2020
return %0 : tensor<f32>
2121
}
22-
func @add(%arg0: tensor<f32>, %arg1: tensor<f32>) -> tensor<f32> attributes {sym_visibility = "private"} {
22+
func private @add(%arg0: tensor<f32>, %arg1: tensor<f32>) -> tensor<f32> {
2323
%0 = "tosa.add"(%arg0, %arg1) : (tensor<f32>, tensor<f32>) -> tensor<f32>
2424
return %0 : tensor<f32>
2525
}
26-
func @sub(%arg0: tensor<f32>, %arg1: tensor<f32>) -> tensor<f32> attributes {sym_visibility = "private"} {
26+
func private @sub(%arg0: tensor<f32>, %arg1: tensor<f32>) -> tensor<f32> {
2727
%0 = "tosa.sub"(%arg0, %arg1) : (tensor<f32>, tensor<f32>) -> tensor<f32>
2828
return %0 : tensor<f32>
2929
}
@@ -45,12 +45,12 @@ func @inlined_while_fn(%arg0: tensor<i32>, %arg1: tensor<i32>, %arg2: tensor<i32
4545
}) : (tensor<i32>, tensor<i32>, tensor<i32>, tensor<10xi32>) -> (tensor<i32>, tensor<i32>, tensor<i32>, tensor<10xi32>)
4646
return %1#3 : tensor<10xi32>
4747
}
48-
func @while_body_50(%arg0: tensor<i32>, %arg1: tensor<i32>, %arg2: tensor<i32>, %arg3: tensor<10xi32>) -> (tensor<i32>, tensor<i32>, tensor<i32>, tensor<10xi32>) attributes {sym_visibility = "private"} {
48+
func private @while_body_50(%arg0: tensor<i32>, %arg1: tensor<i32>, %arg2: tensor<i32>, %arg3: tensor<10xi32>) -> (tensor<i32>, tensor<i32>, tensor<i32>, tensor<10xi32>) {
4949
%1 = "tosa.add"(%arg0, %arg1) : (tensor<i32>, tensor<i32>) -> tensor<i32>
5050
%2 = "tosa.add"(%arg3, %1) : (tensor<10xi32>, tensor<i32>) -> tensor<10xi32>
5151
return %1, %arg1, %arg2, %2: tensor<i32>, tensor<i32>, tensor<i32>, tensor<10xi32>
5252
}
53-
func @while_cond_40(%arg0: tensor<i32>, %arg1: tensor<i32>, %arg2: tensor<i32>, %arg3: tensor<10xi32>) -> tensor<i1> attributes {sym_visibility = "private"} {
53+
func private @while_cond_40(%arg0: tensor<i32>, %arg1: tensor<i32>, %arg2: tensor<i32>, %arg3: tensor<10xi32>) -> tensor<i1> {
5454
%0 = "tosa.greater_equal"(%arg0, %arg1) : (tensor<i32>, tensor<i32>) -> tensor<i1>
5555
%1 = "tosa.logical_not"(%0) : (tensor<i1>) -> tensor<i1>
5656
return %1 : tensor<i1>

mlir/test/IR/core-ops.mlir

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,3 @@ func @subtensor_insert(%t: tensor<8x16x4xf32>, %t2: tensor<16x32x8xf32>, %idx :
942942

943943
return
944944
}
945-
946-
// CHECK-LABEL: func private @legacy_visibility_syntax
947-
func @legacy_visibility_syntax() attributes { sym_visibility = "private" }

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,19 @@ func @f(%arg0: i64) -> (i64 {test.invalid_attr}) {
7878

7979
// expected-error@+1 {{symbol declaration cannot have public visibility}}
8080
func @invalid_public_declaration()
81+
82+
// -----
83+
84+
// expected-error@+1 {{'sym_visibility' is an inferred attribute and should not be specified in the explicit attribute dictionary}}
85+
func @legacy_visibility_syntax() attributes { sym_visibility = "private" }
86+
87+
// -----
88+
89+
// expected-error@+1 {{'sym_name' is an inferred attribute and should not be specified in the explicit attribute dictionary}}
90+
func private @invalid_symbol_name_attr() attributes { sym_name = "x" }
91+
92+
// -----
93+
94+
// expected-error@+1 {{'type' is an inferred attribute and should not be specified in the explicit attribute dictionary}}
95+
func private @invalid_symbol_type_attr() attributes { type = "x" }
96+

0 commit comments

Comments
 (0)