-
Notifications
You must be signed in to change notification settings - Fork 30
[A5][Sync] remove identity tmov before insert-sync #420
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
Open
TaoTao-real
wants to merge
16
commits into
hw-native-sys:main
Choose a base branch
from
TaoTao-real:codex/a5-identity-tmov-cleanup-zsu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1716c29
[A5][Sync] remove identity tmov before insert-sync
TaoTao-real 18d0db0
[A5][Sync] strengthen identity tmov elision criteria
TaoTao-real e09af6d
[A5][Sync][test] add if-else alias identity tmov guard
TaoTao-real 2c49361
[A5][Sync][test] add deadlock-signature guard for identity tmov
TaoTao-real 49515a6
[A5][Sync] harden identity tmov proof and add guards
TaoTao-real 50f5235
[A5][Sync][test] add direct issue828 pto regression guard
TaoTao-real ee59ce0
[A5][Sync][test] force issue828 board case into arg6==0 path
TaoTao-real 055b644
[A5][Sync] expose identity tmov cleanup switch for A/B validation
TaoTao-real 73fac29
[A5][Sync][test] run issue828 case through if/else branches
TaoTao-real 2deb942
[A5][Sync][test] split issue828 into if/else pto regressions
TaoTao-real b13c8a4
[A5][Test] add issue828 diagnostic split cases
TaoTao-real e8e7bc8
[A5][Test] add aligned issue828 if/else variants
TaoTao-real 913e2be
[A5][Diag] add else trace-print case for issue828
TaoTao-real 884f6c5
[A5][Diag] avoid tprint in else trace case for board compatibility
TaoTao-real 8ed1a05
[A5][Diag] force PIPE_ALL barriers around residual tmov triad
TaoTao-real 94917ed
[A5][Diag] add aligned TMOV A/B variants for issue828
TaoTao-real 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| // 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 "PTO/Transforms/InsertSync/MemoryDependentAnalyzer.h" | ||
| #include "PTO/Transforms/InsertSync/PTOIRTranslator.h" | ||
| #include "PTO/Transforms/InsertSync/SyncCommon.h" | ||
| #include "mlir/Dialect/Arith/IR/Arith.h" | ||
| #include "mlir/Dialect/Func/IR/FuncOps.h" | ||
| #include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
| #include "mlir/IR/BuiltinAttributes.h" | ||
| #include "mlir/IR/BuiltinOps.h" | ||
| #include "mlir/IR/Matchers.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include <optional> | ||
|
|
||
| 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().equals_insensitive("a5"); | ||
| } | ||
|
|
||
| static const BaseMemInfo * | ||
| getSingleMemInfo(const Buffer2MemInfoMap &buffer2MemInfoMap, Value value) { | ||
| auto it = buffer2MemInfoMap.find(value); | ||
| if (it == buffer2MemInfoMap.end()) | ||
| return nullptr; | ||
| if (it->second.size() != 1) | ||
| return nullptr; | ||
| return it->second.front().get(); | ||
| } | ||
|
|
||
| static std::optional<int64_t> tryEvalI64Constant(Value value) { | ||
| if (!value) | ||
| return std::nullopt; | ||
|
|
||
| APInt apInt; | ||
| if (matchPattern(value, m_ConstantInt(&apInt))) | ||
| return apInt.getSExtValue(); | ||
|
|
||
| Operation *defOp = value.getDefiningOp(); | ||
| if (!defOp) | ||
| return std::nullopt; | ||
|
|
||
| if (auto castOp = dyn_cast<arith::IndexCastOp>(defOp)) | ||
| return tryEvalI64Constant(castOp.getIn()); | ||
| if (auto castOp = dyn_cast<arith::ExtSIOp>(defOp)) | ||
| return tryEvalI64Constant(castOp.getIn()); | ||
| if (auto castOp = dyn_cast<arith::ExtUIOp>(defOp)) | ||
| return tryEvalI64Constant(castOp.getIn()); | ||
| if (auto castOp = dyn_cast<arith::TruncIOp>(defOp)) | ||
| return tryEvalI64Constant(castOp.getIn()); | ||
|
|
||
| return std::nullopt; | ||
| } | ||
|
|
||
| static std::optional<int64_t> | ||
| tryGetConcreteRootAddress(const BaseMemInfo *info) { | ||
| if (!info) | ||
| return std::nullopt; | ||
|
|
||
| if (auto direct = tryEvalI64Constant(info->rootBuffer)) | ||
| return direct; | ||
|
|
||
| Operation *defOp = info->rootBuffer.getDefiningOp(); | ||
| if (!defOp) | ||
| return std::nullopt; | ||
|
|
||
| if (auto alloc = dyn_cast<pto::AllocTileOp>(defOp)) | ||
| return tryEvalI64Constant(alloc.getAddr()); | ||
|
|
||
| if (auto cast = dyn_cast<pto::PointerCastOp>(defOp)) { | ||
| if (!cast.getAddrs().empty()) | ||
| return tryEvalI64Constant(cast.getAddrs().front()); | ||
| } | ||
|
|
||
| return std::nullopt; | ||
| } | ||
|
|
||
| static bool hasDynamicStaticList(ArrayRef<int64_t> values) { | ||
| return llvm::any_of(values, [](int64_t value) { | ||
| return value == ShapedType::kDynamic; | ||
| }); | ||
| } | ||
|
|
||
| static bool isStaticallyAddressableValue(Value value) { | ||
| int depth = 0; | ||
| constexpr int kMaxDepth = 32; | ||
| while (value && depth++ < kMaxDepth) { | ||
| Operation *defOp = value.getDefiningOp(); | ||
| if (!defOp) | ||
| return false; | ||
|
|
||
| if (auto subView = dyn_cast<memref::SubViewOp>(defOp)) { | ||
| if (hasDynamicStaticList(subView.getStaticOffsets()) || | ||
| hasDynamicStaticList(subView.getStaticSizes()) || | ||
| hasDynamicStaticList(subView.getStaticStrides())) { | ||
| return false; | ||
| } | ||
| value = subView.getSource(); | ||
| continue; | ||
| } | ||
|
|
||
| if (isa<memref::ReinterpretCastOp>(defOp)) | ||
| return false; | ||
|
|
||
| if (auto cast = dyn_cast<memref::CastOp>(defOp)) { | ||
| value = cast.getSource(); | ||
| continue; | ||
| } | ||
| if (auto collapse = dyn_cast<memref::CollapseShapeOp>(defOp)) { | ||
| value = collapse.getSrc(); | ||
| continue; | ||
| } | ||
| if (auto expand = dyn_cast<memref::ExpandShapeOp>(defOp)) { | ||
| value = expand.getSrc(); | ||
| continue; | ||
| } | ||
| if (auto view = dyn_cast<memref::ViewOp>(defOp)) { | ||
| if (view.getByteShift()) | ||
| return false; | ||
| value = view.getSource(); | ||
| continue; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| static bool hasExactSameAddressRange(const BaseMemInfo *srcInfo, | ||
| const BaseMemInfo *dstInfo) { | ||
| if (!srcInfo || !dstInfo) | ||
| return false; | ||
|
|
||
| if (srcInfo->scope != dstInfo->scope) | ||
| return false; | ||
| if (srcInfo->allocateSize == 0 || dstInfo->allocateSize == 0) | ||
| return false; | ||
| if (srcInfo->allocateSize != dstInfo->allocateSize) | ||
| return false; | ||
| if (srcInfo->baseAddresses.empty() || dstInfo->baseAddresses.empty()) | ||
| return false; | ||
| if (srcInfo->baseAddresses != dstInfo->baseAddresses) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static bool canEraseIdentityTMov( | ||
| TMovOp op, const Buffer2MemInfoMap &buffer2MemInfoMap) { | ||
| Value src = op.getSrc(); | ||
| Value dst = op.getDst(); | ||
|
|
||
| if (src == dst) | ||
| return true; | ||
|
|
||
| if (src.getType() != dst.getType()) | ||
| return false; | ||
| if (!isStaticallyAddressableValue(src) || !isStaticallyAddressableValue(dst)) | ||
| return false; | ||
|
|
||
| const BaseMemInfo *srcInfo = getSingleMemInfo(buffer2MemInfoMap, src); | ||
| const BaseMemInfo *dstInfo = getSingleMemInfo(buffer2MemInfoMap, dst); | ||
| if (!srcInfo || !dstInfo) | ||
| return false; | ||
|
|
||
| if (!hasExactSameAddressRange(srcInfo, dstInfo)) | ||
| return false; | ||
|
|
||
| if (srcInfo->rootBuffer == dstInfo->rootBuffer) | ||
| return true; | ||
|
|
||
| auto srcRootAddr = tryGetConcreteRootAddress(srcInfo); | ||
| auto dstRootAddr = tryGetConcreteRootAddress(dstInfo); | ||
| if (!srcRootAddr || !dstRootAddr) | ||
| return false; | ||
| return *srcRootAddr == *dstRootAddr; | ||
| } | ||
|
|
||
| struct PTORemoveIdentityTMovPass | ||
| : public mlir::pto::impl::PTORemoveIdentityTMovBase< | ||
| PTORemoveIdentityTMovPass> { | ||
| void runOnOperation() override { | ||
| func::FuncOp funcOp = getOperation(); | ||
| if (!isA5Target(funcOp)) | ||
| return; | ||
|
|
||
| bool hasTMov = false; | ||
| funcOp.walk([&](TMovOp) { | ||
| hasTMov = true; | ||
| return WalkResult::interrupt(); | ||
| }); | ||
| if (!hasTMov) | ||
| return; | ||
|
|
||
| MemoryDependentAnalyzer memAnalyzer; | ||
| SyncIRs syncIR; | ||
| Buffer2MemInfoMap buffer2MemInfoMap; | ||
| PTOIRTranslator translator(syncIR, memAnalyzer, buffer2MemInfoMap, funcOp, | ||
| SyncAnalysisMode::NORMALSYNC); | ||
| translator.Build(); | ||
|
|
||
| SmallVector<TMovOp> toErase; | ||
| funcOp.walk([&](TMovOp op) { | ||
| if (canEraseIdentityTMov(op, buffer2MemInfoMap)) | ||
| 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,40 @@ | ||
| // RUN: ptoas --pto-arch=a5 --enable-insert-sync %s | FileCheck %s --check-prefix=SAFE | ||
| // RUN: ptoas --pto-arch=a5 --enable-insert-sync --disable-identity-tmov-cleanup %s | FileCheck %s --check-prefix=UNSAFE | ||
|
|
||
| module attributes {"pto.device-spec" = "Ascend950"} { | ||
| func.func @identity_tmov_alias_deadlock_signature_a5( | ||
| %src: memref<1x64xf16, #pto.address_space<gm>>, | ||
| %dst: memref<1x64xf16, #pto.address_space<gm>>) { | ||
| %ub = memref.alloc() : memref<1x64xf16, #pto.address_space<vec>> | ||
| %src_alias = memref.subview %ub[0, 0] [1, 64] [1, 1] | ||
| : memref<1x64xf16, #pto.address_space<vec>> | ||
| to memref<1x64xf16, strided<[64, 1]>, #pto.address_space<vec>> | ||
| %dst_alias = memref.subview %ub[0, 0] [1, 64] [1, 1] | ||
| : memref<1x64xf16, #pto.address_space<vec>> | ||
| to memref<1x64xf16, strided<[64, 1]>, #pto.address_space<vec>> | ||
|
|
||
| pto.tload ins(%src : memref<1x64xf16, #pto.address_space<gm>>) | ||
| outs(%src_alias : memref<1x64xf16, strided<[64, 1]>, #pto.address_space<vec>>) | ||
| pto.tmov ins(%src_alias : memref<1x64xf16, strided<[64, 1]>, #pto.address_space<vec>>) | ||
| outs(%dst_alias : memref<1x64xf16, strided<[64, 1]>, #pto.address_space<vec>>) | ||
| pto.tstore ins(%dst_alias : memref<1x64xf16, strided<[64, 1]>, #pto.address_space<vec>>) | ||
| outs(%dst : memref<1x64xf16, #pto.address_space<gm>>) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // SAFE-LABEL: __global__ AICORE void identity_tmov_alias_deadlock_signature_a5( | ||
| // SAFE-NOT: TMOV( | ||
| // SAFE: set_flag(PIPE_MTE2, PIPE_MTE3, EVENT_ID0); | ||
| // SAFE: wait_flag(PIPE_MTE2, PIPE_MTE3, EVENT_ID0); | ||
| // SAFE-NOT: set_flag(PIPE_MTE2, PIPE_V | ||
| // SAFE-NOT: wait_flag(PIPE_MTE2, PIPE_V | ||
| // SAFE-NOT: set_flag(PIPE_V, PIPE_MTE3 | ||
| // SAFE-NOT: wait_flag(PIPE_V, PIPE_MTE3 | ||
|
|
||
| // UNSAFE-LABEL: __global__ AICORE void identity_tmov_alias_deadlock_signature_a5( | ||
| // UNSAFE: TMOV( | ||
| // UNSAFE: set_flag(PIPE_MTE2, PIPE_V, EVENT_ID0); | ||
| // UNSAFE: wait_flag(PIPE_MTE2, PIPE_V, EVENT_ID0); | ||
| // UNSAFE: set_flag(PIPE_V, PIPE_MTE3, EVENT_ID0); | ||
| // UNSAFE: wait_flag(PIPE_V, PIPE_MTE3, EVENT_ID0); |
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( |
Oops, something went wrong.
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 first collects all
TMovOps to be erased into aSmallVectorand then iterates over this vector to perform the erasure. This two-step process can be simplified and made more efficient. You can perform the erasure directly within thewalkcallback. SinceTMovOphas no regions, it's safe to erase it during the walk, which avoids the need for intermediate storage and a second loop.