forked from onnx/onnx-mlir
-
Notifications
You must be signed in to change notification settings - Fork 16
Result Names updation while running passes [AIESW-21158] #508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ilango100
merged 6 commits into
feature/onnx-to-tosa
from
irajagop.result_names_updation
Jan 5, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f175fd0
Add ResultNamesUpdater Pattern listener
ilango100 703f067
Use rnUpdater listener for the passes
ilango100 5a951d8
Add test cases for result names updation
ilango100 d7a945b
No need to fill if initialized properly
ilango100 a106b9f
Fix bug to init empty string for new ResultNames
ilango100 aceef1a
Propagate ResultNames of any attribute
ilango100 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright (C) 2022 - 2025 Advanced Micro Devices, Inc. All rights reserved. | ||
|
|
||
| #include <llvm/ADT/STLExtras.h> | ||
| #include <mlir/IR/BuiltinAttributes.h> | ||
| #include <mlir/IR/MLIRContext.h> | ||
| #include <mlir/IR/Value.h> | ||
| #include <mlir/Support/LLVM.h> | ||
|
|
||
| #include "src/Dialect/ONNX/Transforms/ResultNamesUpdater.hpp" | ||
|
|
||
| namespace onnx_mlir { | ||
|
|
||
| void ResultNamesUpdater::notifyOperationReplaced( | ||
| mlir::Operation *op, mlir::ValueRange replacement) { | ||
| if (!op->hasAttrOfType<mlir::ArrayAttr>("ResultNames")) | ||
| return; | ||
|
|
||
| auto resultNamesArray = op->getAttrOfType<mlir::ArrayAttr>("ResultNames"); | ||
|
|
||
| // If the op is replaced by a single op, simply copy the attribute | ||
| mlir::Operation *replSingleOp = replacement.front().getDefiningOp(); | ||
| if (replSingleOp && | ||
| llvm::all_of(replacement, [replSingleOp](mlir::Value val) -> bool { | ||
| return val.getDefiningOp() == replSingleOp; | ||
| })) { | ||
| replSingleOp->setAttr("ResultNames", resultNamesArray); | ||
| return; | ||
| } | ||
|
|
||
| mlir::MLIRContext *ctx = op->getContext(); | ||
| for (auto [name, value] : llvm::zip_equal(resultNamesArray, replacement)) { | ||
| if (mlir::OpResult replResult = mlir::dyn_cast<mlir::OpResult>(value)) { | ||
| mlir::Operation *replOp = replResult.getOwner(); | ||
|
|
||
| // Get new or existing ResultNames | ||
| mlir::SmallVector<mlir::Attribute> replResultNames( | ||
| replOp->getNumResults(), mlir::StringAttr::get(ctx)); | ||
| if (auto existing = replOp->getAttrOfType<mlir::ArrayAttr>("ResultNames")) | ||
| replResultNames = | ||
| mlir::SmallVector<mlir::Attribute>(existing.getValue()); | ||
|
|
||
| // Replace the ResultName of current result | ||
| replResultNames[replResult.getResultNumber()] = name; | ||
| replOp->setAttr( | ||
| "ResultNames", mlir::ArrayAttr::get(ctx, replResultNames)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace onnx_mlir | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright (C) 2022 - 2025 Advanced Micro Devices, Inc. All rights reserved. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <mlir/IR/PatternMatch.h> | ||
|
|
||
| namespace onnx_mlir { | ||
|
|
||
| class ResultNamesUpdater : public mlir::RewriterBase::Listener { | ||
| public: | ||
| void notifyOperationReplaced( | ||
| mlir::Operation *op, mlir::ValueRange replacement) override; | ||
| }; | ||
|
|
||
| } // namespace onnx_mlir |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // (c) Copyright 2022 - 2025 Advanced Micro Devices, Inc. All Rights reserved. | ||
|
|
||
| // RUN: onnx-mlir-opt %s --constprop-onnx --decompose-onnx=enable-split-to-slice --onnx-hybrid-transform --qdq-canonicalize=remove-qdq-around-ops | FileCheck %s | ||
|
|
||
| func.func @constprop() -> tensor<f32> { | ||
| %0 = onnx.Constant {ResultNames = ["const0"]} dense<1.000000e+00> : tensor<f32> | ||
| %1 = onnx.Constant {ResultNames = ["const0"]} dense<2.000000e+00> : tensor<f32> | ||
| %2 = "onnx.Add"(%0, %1) {ResultNames = ["add0"]} : (tensor<f32>, tensor<f32>) -> tensor<f32> | ||
| return %2 : tensor<f32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: @constprop() | ||
| // CHECK: onnx.Constant | ||
| // CHECK-SAME: ResultNames = ["add0"] | ||
| // CHECK-SAME: dense<3.000000e+00> | ||
|
|
||
| func.func @decompose(%arg0: tensor<8x4xf32>) -> (tensor<4x4xf32>, tensor<2x4xf32>, tensor<2x4xf32>) { | ||
| %0 = onnx.Constant dense<[4, 2, 2]> : tensor<3xi64> | ||
| %1:3 = "onnx.Split"(%arg0, %0) {axis = 0 : si64, ResultNames = ["split_out0", "split_out1", "split_out2"]} : (tensor<8x4xf32>, tensor<3xi64>) -> (tensor<4x4xf32>, tensor<2x4xf32>, tensor<2x4xf32>) | ||
| return %1#0, %1#1, %1#2 : tensor<4x4xf32>, tensor<2x4xf32>, tensor<2x4xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: @decompose | ||
| // CHECK: onnx.Slice | ||
| // CHECK-SAME: ResultNames = ["split_out0"] | ||
| // CHECK-NEXT: onnx.Slice | ||
| // CHECK-SAME: ResultNames = ["split_out1"] | ||
| // CHECK-NEXT: onnx.Slice | ||
| // CHECK-SAME: ResultNames = ["split_out2"] | ||
|
|
||
| func.func @canonicalize(%arg0: tensor<f32>) -> tensor<f32> { | ||
| %0 = onnx.Constant {ResultNames = ["const0"]} dense<2.000000e+00> : tensor<f32> | ||
| %1 = "onnx.Add"(%0, %arg0) {ResultNames = ["add0"]} : (tensor<f32>, tensor<f32>) -> tensor<f32> | ||
| return %1 : tensor<f32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: @canonicalize | ||
| // CHECK: "onnx.Add"(%arg0, %0) | ||
| // CHECK-SAME: ResultNames = ["add0"] | ||
|
|
||
| func.func @qdq_canonicalize(%arg0: tensor<1x128xf32>) -> tensor<1x1x128xf32> { | ||
| %0 = onnx.Constant {ResultNames = ["scale"]} dense<1.000000e+00> : tensor<f32> | ||
| %1 = onnx.Constant {ResultNames = ["zp"]} dense<128> : tensor<ui8> | ||
| %2 = onnx.Constant {ResultNames = ["shape"]} dense<[1, 1, 128]> : tensor<3xi64> | ||
| %3 = "onnx.QuantizeLinear"(%arg0, %0, %1) {ResultNames = ["q0"], axis = 1 : si64, block_size = 0 : si64, output_dtype = 0 : si64, saturate = 1 : si64} : (tensor<1x128xf32>, tensor<f32>, tensor<ui8>) -> tensor<1x128xui8> | ||
| %4 = "onnx.DequantizeLinear"(%3, %0, %1) {ResultNames = ["dq0"], axis = 1 : si64, block_size = 0 : si64} : (tensor<1x128xui8>, tensor<f32>, tensor<ui8>) -> tensor<1x128xf32> | ||
| %5 = "onnx.Reshape"(%4, %2) {ResultNames = ["reshape"], allowzero = 0 : si64} : (tensor<1x128xf32>, tensor<3xi64>) -> tensor<1x1x128xf32> | ||
| %6 = "onnx.QuantizeLinear"(%5, %0, %1) {ResultNames = ["q1"], axis = 1 : si64, block_size = 0 : si64, output_dtype = 0 : si64, saturate = 1 : si64} : (tensor<1x1x128xf32>, tensor<f32>, tensor<ui8>) -> tensor<1x1x128xui8> | ||
| %7 = "onnx.DequantizeLinear"(%6, %0, %1) {ResultNames = ["dq1"], axis = 1 : si64, block_size = 0 : si64} : (tensor<1x1x128xui8>, tensor<f32>, tensor<ui8>) -> tensor<1x1x128xf32> | ||
| return %7 : tensor<1x1x128xf32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: @qdq_canonicalize | ||
| // CHECK: onnx.QuantizeLinear | ||
| // CHECK-SAME: ResultNames = ["q0"] | ||
| // CHECK-NOT: onnx.DequantizeLinear | ||
| // CHECK: onnx.Reshape | ||
| // CHECK-SAME: ResultNames = ["q1"] | ||
| // CHECK-NOT: onnx.QuantizeLinear | ||
| // CHECK: onnx.DequantizeLinear | ||
|
|
||
| func.func @complex_names(%arg0: tensor<f32>) -> tensor<f32> { | ||
| %0 = onnx.Constant {ResultNames = ["const0"]} dense<2.000000e+00> : tensor<f32> | ||
| %1 = "onnx.Add"(%0, %arg0) {ResultNames = [["add0", "with", "array", [1, 2, 3, 4]]]} : (tensor<f32>, tensor<f32>) -> tensor<f32> | ||
| return %1 : tensor<f32> | ||
| } | ||
|
|
||
| // CHECK-LABEL: @complex_names | ||
| // CHECK: "onnx.Add"(%arg0, %0) | ||
| // CHECK-SAME: ResultNames = [ | ||
| // CHECK-SAME: ["add0", "with", "array", [1, 2, 3, 4]]] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.