diff --git a/mlir/lib/IR/FunctionImplementation.cpp b/mlir/lib/IR/FunctionImplementation.cpp --- a/mlir/lib/IR/FunctionImplementation.cpp +++ b/mlir/lib/IR/FunctionImplementation.cpp @@ -180,7 +180,7 @@ return failure(); // Parse the function signature. - auto signatureLocation = parser.getCurrentLocation(); + llvm::SMLoc signatureLocation = parser.getCurrentLocation(); bool isVariadic = false; if (parseFunctionSignature(parser, allowVariadic, entryArgs, argTypes, argAttrs, isVariadic, resultTypes, resultAttrs)) @@ -196,9 +196,29 @@ << (errorMessage.empty() ? "" : ": ") << errorMessage; // If function attributes are present, parse them. - if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) + NamedAttrList parsedAttributes; + llvm::SMLoc attributeDictLocation = parser.getCurrentLocation(); + if (parser.parseOptionalAttrDictWithKeyword(parsedAttributes)) return failure(); + // Disallow attributes that are inferred from elsewhere in the attribute + // dictionary. + const std::array, 3> disallowedAttributes = { + {{SymbolTable::getVisibilityAttrName(), + "use public/private/nested keywords instead to specifiy visibility " + "before name of the function"}, + {SymbolTable::getSymbolAttrName(), + "it will be inferred from the name of the function"}, + {getTypeAttrName(), + "it will be inferred from the type of the function"}}}; + for (std::pair disallowed : disallowedAttributes) + if (parsedAttributes.get(disallowed.first)) + return parser.emitError(attributeDictLocation, "attribute '") + << disallowed.first + << "' should not be specified in the attribute dictionary, " + << disallowed.second; + result.attributes.append(parsedAttributes); + // Add the attributes to the function arguments. assert(argAttrs.size() == argTypes.size()); assert(resultAttrs.size() == resultTypes.size()); diff --git a/mlir/test/Dialect/Tosa/inlining.mlir b/mlir/test/Dialect/Tosa/inlining.mlir --- a/mlir/test/Dialect/Tosa/inlining.mlir +++ b/mlir/test/Dialect/Tosa/inlining.mlir @@ -19,11 +19,11 @@ }) : (tensor, tensor, tensor) -> tensor return %0 : tensor } -func @add(%arg0: tensor, %arg1: tensor) -> tensor attributes {sym_visibility = "private"} { +func private @add(%arg0: tensor, %arg1: tensor) -> tensor { %0 = "tosa.add"(%arg0, %arg1) : (tensor, tensor) -> tensor return %0 : tensor } -func @sub(%arg0: tensor, %arg1: tensor) -> tensor attributes {sym_visibility = "private"} { +func private @sub(%arg0: tensor, %arg1: tensor) -> tensor { %0 = "tosa.sub"(%arg0, %arg1) : (tensor, tensor) -> tensor return %0 : tensor } @@ -45,12 +45,12 @@ }) : (tensor, tensor, tensor, tensor<10xi32>) -> (tensor, tensor, tensor, tensor<10xi32>) return %1#3 : tensor<10xi32> } -func @while_body_50(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor<10xi32>) -> (tensor, tensor, tensor, tensor<10xi32>) attributes {sym_visibility = "private"} { +func private @while_body_50(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor<10xi32>) -> (tensor, tensor, tensor, tensor<10xi32>) { %1 = "tosa.add"(%arg0, %arg1) : (tensor, tensor) -> tensor %2 = "tosa.add"(%arg3, %1) : (tensor<10xi32>, tensor) -> tensor<10xi32> return %1, %arg1, %arg2, %2: tensor, tensor, tensor, tensor<10xi32> } -func @while_cond_40(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor<10xi32>) -> tensor attributes {sym_visibility = "private"} { +func private @while_cond_40(%arg0: tensor, %arg1: tensor, %arg2: tensor, %arg3: tensor<10xi32>) -> tensor { %0 = "tosa.greater_equal"(%arg0, %arg1) : (tensor, tensor) -> tensor %1 = "tosa.logical_not"(%0) : (tensor) -> tensor return %1 : tensor diff --git a/mlir/test/IR/core-ops.mlir b/mlir/test/IR/core-ops.mlir --- a/mlir/test/IR/core-ops.mlir +++ b/mlir/test/IR/core-ops.mlir @@ -942,6 +942,3 @@ return } - -// CHECK-LABEL: func private @legacy_visibility_syntax -func @legacy_visibility_syntax() attributes { sym_visibility = "private" } diff --git a/mlir/test/IR/invalid-func-op.mlir b/mlir/test/IR/invalid-func-op.mlir --- a/mlir/test/IR/invalid-func-op.mlir +++ b/mlir/test/IR/invalid-func-op.mlir @@ -78,3 +78,19 @@ // expected-error@+1 {{symbol declaration cannot have public visibility}} func @invalid_public_declaration() + +// ----- + +// expected-error@+1 {{attribute 'sym_visibility' should not be specified in the attribute dictionary, use public/private/nested keywords instead to specifiy visibility before name of the function}} +func @legacy_visibility_syntax() attributes { sym_visibility = "private" } + +// ----- + +// expected-error@+1 {{attribute 'sym_name' should not be specified in the attribute dictionary, it will be inferred from the name of the function}} +func private @invalid_symbol_name_attr() attributes { sym_name = "x" } + +// ----- + +// expected-error@+1 {{attribute 'type' should not be specified in the attribute dictionary, it will be inferred from the type of the function}} +func private @invalid_symbol_type_attr() attributes { type = "x" } +