[Mlir-commits] [mlir] [tosa] Change VariableOp to align with spec (PR #142240)
Luke Hutton
llvmlistbot at llvm.org
Mon Jun 2 08:48:36 PDT 2025
================
@@ -177,42 +195,81 @@ Operation *TosaDialect::materializeConstant(OpBuilder &builder, Attribute value,
// Parsers and printers
//===----------------------------------------------------------------------===//
-ParseResult mlir::tosa::parseTypeOrAttr(OpAsmParser &parser, TypeAttr &typeAttr,
- Attribute &attr) {
+namespace {
+
+ParseResult getShapeAndElementType(OpAsmParser &parser, Type parsedType,
+ DenseElementsAttr &varShapeAttr,
+ TypeAttr &typeAttr) {
+ if (auto shapedType = dyn_cast<ShapedType>(parsedType)) {
+ if (!shapedType.hasRank())
+ return parser.emitError(parser.getCurrentLocation())
+ << "expected ranked type";
+
+ auto elementType = shapedType.getElementType();
+ typeAttr = TypeAttr::get(elementType);
+ ArrayRef<int64_t> shape = shapedType.getShape();
+ Builder builder(parser.getContext());
+ varShapeAttr = builder.getIndexTensorAttr(convertFromMlirShape(shape));
+ return success();
+ }
+ return parser.emitError(parser.getCurrentLocation())
+ << "expected shaped type";
+}
+
+} // namespace
+
+// parses the optional initial value or type for a tosa variable
+// with initial value:
+// tosa.variable @name = dense<0.0> : tensor<1x8xf32>
+//
+// without initial value:
+// tosa.variable @name : tensor<1x8xf32>
+ParseResult mlir::tosa::parseVariableOpTypeOrInitialValue(
+ OpAsmParser &parser, DenseElementsAttr &varShapeAttr, TypeAttr &typeAttr,
+ Attribute &initialValueAttr) {
if (succeeded(parser.parseOptionalEqual())) {
- if (failed(parser.parseAttribute(attr))) {
+ if (failed(parser.parseAttribute(initialValueAttr))) {
return parser.emitError(parser.getCurrentLocation())
<< "expected attribute";
}
- if (auto typedAttr = dyn_cast<TypedAttr>(attr)) {
- typeAttr = TypeAttr::get(typedAttr.getType());
+ if (auto typedAttr = dyn_cast<TypedAttr>(initialValueAttr)) {
+ return getShapeAndElementType(parser, typedAttr.getType(), varShapeAttr,
+ typeAttr);
}
- return success();
+ return parser.emitError(parser.getCurrentLocation())
+ << "expected Typed attr";
}
- Type type;
- if (failed(parser.parseColonType(type))) {
- return parser.emitError(parser.getCurrentLocation()) << "expected type";
+ initialValueAttr = nullptr;
+ Type parsedType;
+ if (failed(parser.parseColonType(parsedType))) {
+ return parser.emitError(parser.getCurrentLocation())
+ << "expected type after colon";
}
- typeAttr = TypeAttr::get(type);
-
- return success();
+ return getShapeAndElementType(parser, parsedType, varShapeAttr, typeAttr);
}
-void mlir::tosa::printTypeOrAttr(OpAsmPrinter &p, Operation *op, TypeAttr type,
- Attribute attr) {
+void mlir::tosa::printVariableOpTypeOrInitialValue(
+ OpAsmPrinter &p, Operation *op, DenseElementsAttr varShapeAttr,
+ TypeAttr typeAttr, Attribute initialValueAttr) {
bool needsSpace = false;
- auto typedAttr = dyn_cast_or_null<TypedAttr>(attr);
- if (!typedAttr || typedAttr.getType() != type.getValue()) {
+ auto typedAttr = dyn_cast_or_null<TypedAttr>(initialValueAttr);
+ if (!typedAttr) {
----------------
lhutton1 wrote:
nit: can the declaration of `typedAttr` be removed since it doesn't seem to be used below?
https://github.com/llvm/llvm-project/pull/142240
More information about the Mlir-commits
mailing list