[Mlir-commits] [mlir] [mlir][tosa] Align Variable ops to match with TOSA v1.0 spec (PR #130680)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Mar 11 13:29:33 PDT 2025


https://github.com/Jerry-Ge updated https://github.com/llvm/llvm-project/pull/130680

>From ea44b6addc193d7e21c73eb7ef52d3a0969e3c18 Mon Sep 17 00:00:00 2001
From: Jerry Ge <jerry.ge at arm.com>
Date: Wed, 4 Dec 2024 00:29:10 +0000
Subject: [PATCH] [mlir][tosa] Align Variable ops to match with TOSA v1.0 spec

* updated $name to $uid
* updated AnyType:$value to Tosa_Tensor:$input1 and Tosa_Tensor:$output1 for VariableWrite and VriableRead Operators
* updated description discrepancies
* note: in the TOSA spec, we had var_shape attr, but it's already included
  in the TypeAttr:$type in MLIR

Signed-off-by: Jerry Ge <jerry.ge at arm.com>
Change-Id: I4cd0348cd4e306dbc2e0e53a89a9404d91fb44d4
---
 .../mlir/Dialect/Tosa/IR/TosaUtilOps.td       | 23 ++++++++++---------
 .../TosaToMLProgram/TosaToMLProgram.cpp       |  8 +++----
 .../Tosa/Transforms/TosaValidation.cpp        | 13 +++++------
 3 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaUtilOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaUtilOps.td
index 8a27e5ba39331..c3a8f8d2af97c 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaUtilOps.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaUtilOps.td
@@ -86,12 +86,13 @@ def Tosa_VariableOp : Tosa_Op<"variable", []> {
   let summary = "Defines a variable";
 
   let description = [{
-    Defines a new TOSA variable. This is a mutable value.
+    Defines a new TOSA variable.
+    This is a persistent mutable value across multiple TOSA graph invocations.
     Modifications are expressed using read/write semantics.
   }];
 
   let arguments = (ins
-    SymbolNameAttr:$name,
+    SymbolNameAttr:$uid,
     TypeAttr:$type,
     OptionalAttr<AnyAttr>:$initial_value
   );
@@ -102,7 +103,7 @@ def Tosa_VariableOp : Tosa_Op<"variable", []> {
   ];
 
   let assemblyFormat = [{
-    $name
+    $uid
     attr-dict
     custom<TypeOrAttr>($type, $initial_value)
   }];
@@ -115,12 +116,12 @@ def Tosa_VariableWriteOp : Tosa_Op<"variable.write", []> {
   let summary = "write_buffer operator";
 
   let description = [{
-    Assigns a value to pseudo-buffer resource holding a mutable tensor.
+    Assigns a value to the pseudo-buffer resource holding a persistent mutable tensor.
   }];
 
   let arguments = (ins
-    SymbolNameAttr:$name,
-    AnyType:$value
+    SymbolNameAttr:$uid,
+    Tosa_Tensor:$input1
   );
 
   list<Availability> availability = [
@@ -129,7 +130,7 @@ def Tosa_VariableWriteOp : Tosa_Op<"variable.write", []> {
   ];
 
   let assemblyFormat = [{
-    $name attr-dict `,` $value `:` type($value)
+    $uid attr-dict `,` $input1 `:` type($input1)
   }];
 }
 
@@ -140,15 +141,15 @@ def Tosa_VariableReadOp : Tosa_Op<"variable.read", []> {
   let summary = "read_buffer operator";
 
   let description = [{
-    Reads the value from a pseudo-buffer resource holding a mutable tensor.
+    Reads the value from a pseudo-buffer resource holding a persistent mutable tensor.
   }];
 
   let arguments = (ins
-    SymbolNameAttr:$name
+    SymbolNameAttr:$uid
   );
 
   let results = (outs
-    AnyType:$value
+    Tosa_Tensor:$output1
   );
 
   list<Availability> availability = [
@@ -157,7 +158,7 @@ def Tosa_VariableReadOp : Tosa_Op<"variable.read", []> {
   ];
 
   let assemblyFormat = [{
-    $name attr-dict `:` type($value)
+    $uid attr-dict `:` type($output1)
   }];
 }
 
diff --git a/mlir/lib/Conversion/TosaToMLProgram/TosaToMLProgram.cpp b/mlir/lib/Conversion/TosaToMLProgram/TosaToMLProgram.cpp
index d134d8cdf485e..fb3981a6547e6 100644
--- a/mlir/lib/Conversion/TosaToMLProgram/TosaToMLProgram.cpp
+++ b/mlir/lib/Conversion/TosaToMLProgram/TosaToMLProgram.cpp
@@ -27,7 +27,7 @@ class VariableOpConverter : public OpRewritePattern<tosa::VariableOp> {
   LogicalResult matchAndRewrite(tosa::VariableOp op,
                                 PatternRewriter &rewriter) const final {
     auto newVariable = rewriter.create<mlir::ml_program::GlobalOp>(
-        op.getLoc(), op.getName(), op.getType(), /*is_mutable=*/true,
+        op.getLoc(), op.getUid(), op.getType(), /*is_mutable=*/true,
         op.getInitialValueAttr(), /*sym_visibility=*/nullptr);
     newVariable.setPrivate();
     rewriter.replaceOp(op, newVariable);
@@ -43,9 +43,9 @@ class VariableWriteOpConverter
   LogicalResult matchAndRewrite(tosa::VariableWriteOp op,
                                 PatternRewriter &rewriter) const final {
     auto globalSymbolRef =
-        SymbolRefAttr::get(rewriter.getContext(), op.getName());
+        SymbolRefAttr::get(rewriter.getContext(), op.getUid());
     auto newVariableWrite = rewriter.create<ml_program::GlobalStoreOp>(
-        op.getLoc(), globalSymbolRef, op.getValue());
+        op.getLoc(), globalSymbolRef, op.getInput1());
     rewriter.replaceOp(op, newVariableWrite);
     return success();
   }
@@ -58,7 +58,7 @@ class VariableReadOpConverter : public OpRewritePattern<tosa::VariableReadOp> {
   LogicalResult matchAndRewrite(tosa::VariableReadOp op,
                                 PatternRewriter &rewriter) const final {
     auto globalSymbolRef =
-        SymbolRefAttr::get(rewriter.getContext(), op.getName());
+        SymbolRefAttr::get(rewriter.getContext(), op.getUid());
     auto newVariableRead = rewriter.create<ml_program::GlobalLoadOp>(
         op.getLoc(), op.getType(), globalSymbolRef);
     rewriter.replaceOp(op, newVariableRead);
diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
index 6b1b651a90cfb..02d4642f929e0 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
@@ -677,9 +677,9 @@ inline bool CompatibleTypes(const mlir::Type &type,
 
 bool TosaValidation::CheckVariable(Operation *op) {
   if (isa<mlir::tosa::VariableOp>(op)) {
-    auto nameAttr = cast<mlir::StringAttr>(op->getAttr("name"));
+    mlir::StringAttr uidAttr = cast<mlir::StringAttr>(op->getAttr("uid"));
 
-    if (variablesMap.count(nameAttr)) {
+    if (variablesMap.count(uidAttr)) {
       op->emitOpError() << "name has already been declared";
       return false;
     }
@@ -687,7 +687,7 @@ bool TosaValidation::CheckVariable(Operation *op) {
     auto typeAttr = cast<mlir::TypeAttr>(op->getAttr("type"));
     mlir::Type type = typeAttr.getValue();
 
-    variablesMap[nameAttr] = type;
+    variablesMap[uidAttr] = type;
   }
 
   return true;
@@ -696,14 +696,13 @@ bool TosaValidation::CheckVariable(Operation *op) {
 bool TosaValidation::CheckVariableReadOrWrite(Operation *op) {
   if (isa<mlir::tosa::VariableReadOp>(op) ||
       isa<mlir::tosa::VariableWriteOp>(op)) {
-    auto nameAttr = cast<mlir::StringAttr>(op->getAttr("name"));
-
-    if (!variablesMap.count(nameAttr)) {
+    mlir::StringAttr uidAttr = cast<mlir::StringAttr>(op->getAttr("uid"));
+    if (!variablesMap.count(uidAttr)) {
       op->emitOpError() << "name has not been declared";
       return false;
     }
 
-    auto varType = variablesMap[nameAttr];
+    auto varType = variablesMap[uidAttr];
 
     for (auto v : op->getOperands()) {
       auto type = v.getType();



More information about the Mlir-commits mailing list