-
Notifications
You must be signed in to change notification settings - Fork 38
[A5][Sync] remove identity tmov before insert-sync #419
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
Closed
TaoTao-real
wants to merge
1
commit into
hw-native-sys:main
from
TaoTao-real:codex/a5-identity-tmov-cleanup
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| // This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| // CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| // Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| // INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| // See LICENSE in the root of the software repository for the full text of the License. | ||
|
|
||
| #include "PTO/IR/PTO.h" | ||
| #include "PTO/Transforms/Passes.h" | ||
| #include "mlir/Dialect/Func/IR/FuncOps.h" | ||
| #include "mlir/IR/BuiltinAttributes.h" | ||
| #include "mlir/IR/BuiltinOps.h" | ||
| #include "mlir/Pass/Pass.h" | ||
|
|
||
| namespace mlir { | ||
| namespace pto { | ||
| namespace func = ::mlir::func; | ||
| #define GEN_PASS_DEF_PTOREMOVEIDENTITYTMOV | ||
| #include "PTO/Transforms/Passes.h.inc" | ||
| } // namespace pto | ||
| } // namespace mlir | ||
|
|
||
| using namespace mlir; | ||
| using namespace mlir::pto; | ||
|
|
||
| namespace { | ||
|
|
||
| static bool isA5Target(func::FuncOp funcOp) { | ||
| ModuleOp module = funcOp->getParentOfType<ModuleOp>(); | ||
| if (!module) | ||
| return false; | ||
| auto arch = module->getAttrOfType<StringAttr>("pto.target_arch"); | ||
| return arch && arch.getValue() == "a5"; | ||
| } | ||
|
|
||
| static bool canEraseIdentityTMov(TMovOp op) { | ||
| if (op.getSrc() != op.getDst()) | ||
| return false; | ||
|
|
||
| Value result = op.getResult(); | ||
| if (!result || result.use_empty()) | ||
| return true; | ||
|
|
||
| return result.getType() == op.getDst().getType(); | ||
| } | ||
|
|
||
| struct PTORemoveIdentityTMovPass | ||
| : public mlir::pto::impl::PTORemoveIdentityTMovBase< | ||
| PTORemoveIdentityTMovPass> { | ||
| void runOnOperation() override { | ||
| func::FuncOp funcOp = getOperation(); | ||
| if (!isA5Target(funcOp)) | ||
| return; | ||
|
|
||
| SmallVector<TMovOp> toErase; | ||
| funcOp.walk([&](TMovOp op) { | ||
| if (canEraseIdentityTMov(op)) | ||
| toErase.push_back(op); | ||
| }); | ||
|
|
||
| for (TMovOp op : toErase) { | ||
| Value result = op.getResult(); | ||
| if (result && !result.use_empty()) | ||
| result.replaceAllUsesWith(op.getDst()); | ||
| op.erase(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| std::unique_ptr<Pass> mlir::pto::createPTORemoveIdentityTMovPass() { | ||
| return std::make_unique<PTORemoveIdentityTMovPass>(); | ||
| } | ||
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,33 @@ | ||
| // RUN: ptoas --pto-arch=a5 --enable-insert-sync %s | FileCheck %s --check-prefix=A5 | ||
| // RUN: ptoas --pto-arch=a3 --enable-insert-sync %s | FileCheck %s --check-prefix=A3 | ||
|
|
||
| module attributes {"pto.device-spec" = "Ascend950"} { | ||
| func.func @identity_tmov_autosync_a5_only( | ||
| %src: memref<1x64xf16, #pto.address_space<gm>>, | ||
| %dst: memref<1x64xf16, #pto.address_space<gm>>) { | ||
| %ub = memref.alloc() : memref<1x64xf16, #pto.address_space<vec>> | ||
|
|
||
| pto.tload ins(%src : memref<1x64xf16, #pto.address_space<gm>>) | ||
| outs(%ub : memref<1x64xf16, #pto.address_space<vec>>) | ||
|
|
||
| // Identity move: should be removed on A5 before sync insertion. | ||
| pto.tmov ins(%ub : memref<1x64xf16, #pto.address_space<vec>>) | ||
| outs(%ub : memref<1x64xf16, #pto.address_space<vec>>) | ||
|
|
||
| pto.tstore ins(%ub : memref<1x64xf16, #pto.address_space<vec>>) | ||
| outs(%dst : memref<1x64xf16, #pto.address_space<gm>>) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // A5-LABEL: __global__ AICORE void identity_tmov_autosync_a5_only( | ||
| // A5: set_flag(PIPE_MTE2, PIPE_MTE3, EVENT_ID0); | ||
| // A5-NOT: set_flag(PIPE_MTE2, PIPE_V | ||
| // A5-NOT: wait_flag(PIPE_MTE2, PIPE_V | ||
| // A5-NOT: set_flag(PIPE_V, PIPE_MTE3 | ||
| // A5-NOT: wait_flag(PIPE_V, PIPE_MTE3 | ||
| // A5-NOT: TMOV( | ||
| // A5: wait_flag(PIPE_MTE2, PIPE_MTE3, EVENT_ID0); | ||
|
|
||
| // A3-LABEL: __global__ AICORE void identity_tmov_autosync_a5_only( | ||
| // A3: TMOV( |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation iterates over the function twice: once to collect the
TMovOps to erase, and a second time to perform the erasure. This can be simplified into a single walk. MLIR'swalkfunction is safe to use with in-place erasure of the visited operation, making the intermediatetoErasevector unnecessary. This improves both efficiency and readability.