-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Bugfix] Fix improper touched buffer assignment of Pass MergeSharedMemoryAllocations #17438
Open
LeiWang1999
wants to merge
2
commits into
apache:main
Choose a base branch
from
LeiWang1999:fix-liveness
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
2 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -47,6 +47,36 @@ namespace tir { | |
using runtime::StorageRank; | ||
using runtime::StorageScope; | ||
|
||
/*! | ||
* \brief collect the mapping from the buffer var to its allocate | ||
*/ | ||
class AllocateCollector : public StmtExprVisitor { | ||
private: | ||
bool IsDynamicSharedMemory(Var buffer_var) { | ||
StorageScope storage_scope = runtime::StorageScope::Create(GetPtrStorageScope(buffer_var)); | ||
return storage_scope.rank == runtime::StorageRank::kShared && storage_scope.tag == ".dyn"; | ||
} | ||
|
||
bool IsStaticSharedMemory(Var buffer_var) { | ||
StorageScope storage_scope = runtime::StorageScope::Create(GetPtrStorageScope(buffer_var)); | ||
return storage_scope.rank == runtime::StorageRank::kShared && storage_scope.tag == ""; | ||
} | ||
|
||
public: | ||
void VisitStmt_(const AllocateNode* op) final { | ||
if (IsDynamicSharedMemory(op->buffer_var)) { | ||
dyn_shmem_allocs_[op->buffer_var.get()] = op; | ||
} else if (IsStaticSharedMemory(op->buffer_var)) { | ||
static_shmem_allocs_[op->buffer_var.get()] = op; | ||
} | ||
StmtExprVisitor::VisitStmt_(op); | ||
} | ||
// The dynamic mapping from the original buffer var to its allocate | ||
std::unordered_map<const VarNode*, const AllocateNode*> dyn_shmem_allocs_; | ||
// The static mapping from the original buffer var to its allocate | ||
std::unordered_map<const VarNode*, const AllocateNode*> static_shmem_allocs_; | ||
}; | ||
|
||
// Find a linear pattern of storage access | ||
// Used for liveness analysis. | ||
// Composite scopes(loop/thread_launch/IfThen) is represented by two points: | ||
|
@@ -1733,7 +1763,15 @@ Pass StorageRewrite() { | |
bool enable_reuse = true; | ||
bool reuse_require_exact_matched_dtype = false; | ||
bool merge_static_smem = ctx->GetConfig<Bool>("tir.merge_static_smem", Bool(false)).value(); | ||
if (merge_static_smem) { | ||
|
||
AllocateCollector collector; | ||
collector(f->body); | ||
bool has_dynamic = collector.dyn_shmem_allocs_.size() > 1; | ||
if (has_dynamic || merge_static_smem) { | ||
// For IRModule utilizing dynamic shared memory, reuse is not enabled | ||
// Because dynamic doesn't require maintaining the readability and | ||
// it benefits from a more optimized allocation strategy through the | ||
// Pass `MergeSharedMemoryAllocations`. | ||
// When `merge_static_smem` is true, we will reuse and merge shared | ||
// memory in a dedicated pass `MergeSharedMemoryAllocations`. | ||
// And so we don't enable reuse in this pass. | ||
|
@@ -1755,7 +1793,7 @@ Pass StorageRewrite() { | |
// padded out to 32 bits) would require either rewriting | ||
// AllocateConst::data, or would require the code generators to | ||
// handle vectorized constants. | ||
return PointerValueTypeRewrite(std::move(f), true, false, false, true, true, true, false, | ||
return PointerValueTypeRewrite(std::move(f), true, false, false, false, true, true, false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fourth condition must be false, as the vectorized buffer merge (for example, merge half B_shared[1024] into halfx8 B_shared[128]) will occasional lead to a unhandled behavior during async copy lowering phase. |
||
false); | ||
}; | ||
return CreatePrimFuncPass(pass_func, 0, "tir.StorageRewrite", {}); | ||
|
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.
remove this visit function, as it only allow visit indices, which will lead to some buffer load statement not be traced.