forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TIR] Add pass to replace global to shared memory load with cp async
- Loading branch information
Showing
7 changed files
with
194 additions
and
20 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* \brief Replace copy from global to shared with async copy | ||
* \file inject_async_copy.cc | ||
*/ | ||
#include <tvm/tir/analysis.h> | ||
#include <tvm/tir/builtin.h> | ||
#include <tvm/tir/expr.h> | ||
#include <tvm/tir/stmt_functor.h> | ||
#include <tvm/tir/transform.h> | ||
|
||
#include "storage_access.h" | ||
|
||
namespace tvm { | ||
namespace tir { | ||
|
||
class AsyncCopyOnjector : public StmtMutator { | ||
public: | ||
std::unordered_set<const VarNode*> pending_async_copy_; | ||
|
||
Stmt VisitStmt_(const BufferStoreNode* store) { | ||
if (store->buffer.scope() == "shared") { | ||
if (auto* load = store->value.as<BufferLoadNode>()) { | ||
if (load->buffer.scope() == "global") { | ||
ICHECK(load->indices.size() == 1); | ||
const int bytes = load->indices[0]->dtype.lanes() * load->buffer->dtype.bytes(); | ||
if (bytes == 4 || bytes == 8 || bytes == 16) { | ||
auto dst_offset = store->indices[0].as<RampNode>()->base; | ||
auto src_offset = load->indices[0].as<RampNode>()->base; | ||
pending_async_copy_.insert(store->buffer->data.get()); | ||
return Evaluate(Call(store->buffer->dtype, tvm::tir::builtin::ptx_cp_async(), | ||
{store->buffer->data, dst_offset, load->buffer->data, src_offset, | ||
PrimExpr(bytes)})); | ||
} | ||
} | ||
} | ||
} | ||
return StmtMutator::VisitStmt_(store); | ||
} | ||
|
||
Stmt VisitStmt_(const SeqStmtNode* store) { return StmtMutator::VisitStmt_(store); } | ||
}; | ||
|
||
class InsertWaitGroupPlanner : public StorageAccessVisitor { | ||
public: | ||
explicit InsertWaitGroupPlanner(const std::unordered_set<const VarNode*>& pending_async_copy) | ||
: pending_async_copy_{pending_async_copy} {} | ||
|
||
std::unordered_set<const Object*> insert_wait_before_; | ||
|
||
protected: | ||
bool Enabled(const VarNode* buf, const StorageScope& scope) const final { | ||
return scope == StorageScope::Create("shared") && pending_async_copy_.count(buf) != 0; | ||
} | ||
|
||
std::vector<AccessEntry> Summarize(std::vector<StmtEntry> seq, const StmtNode* parent) final { | ||
std::vector<AccessEntry> flattened; | ||
std::vector<AccessEntry> pending_writes; | ||
|
||
for (const StmtEntry& s : seq) { | ||
bool wait_before_stmt = false; | ||
for (const AccessEntry& acc : s.access) { | ||
ICHECK(pending_async_copy_.count(acc.buffer.get()) != 0); | ||
if (acc.type == kRead) { | ||
if (FindConflict(pending_writes, acc)) { | ||
wait_before_stmt = true; | ||
break; | ||
} | ||
} else if (acc.type == kWrite) { | ||
pending_writes.push_back(acc); | ||
} | ||
flattened.push_back(acc); | ||
} | ||
if (wait_before_stmt) { | ||
ICHECK_EQ(condition_counter(), 0) << "Cannot insert syncs inside condition"; | ||
insert_wait_before_.insert(s.stmt); | ||
} | ||
} | ||
return flattened; | ||
} | ||
|
||
private: | ||
bool FindConflict(const std::vector<AccessEntry>& pending_writes, const AccessEntry& read) { | ||
for (const AccessEntry& pending_write : pending_writes) { | ||
if (pending_write.buffer == read.buffer) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
const std::unordered_set<const VarNode*> pending_async_copy_; | ||
}; | ||
|
||
class InsertWaitGroup : public StmtMutator { | ||
public: | ||
explicit InsertWaitGroup(std::unordered_set<const Object*> insert_wait_before) | ||
: insert_wait_before_(insert_wait_before) {} | ||
|
||
Stmt VisitStmt(const Stmt& stmt) final { | ||
if (insert_wait_before_.count(stmt.get())) { | ||
auto commit_group = | ||
Evaluate(Call(DataType::Void(), tvm::tir::builtin::ptx_commit_group(), {})); | ||
auto wait_group = | ||
Evaluate(Call(DataType::Void(), tvm::tir::builtin::ptx_wait_group(), {PrimExpr(0)})); | ||
auto ret = StmtMutator::VisitStmt(stmt); | ||
return SeqStmt({commit_group, wait_group, ret}); | ||
} else { | ||
return StmtMutator::VisitStmt(stmt); | ||
} | ||
} | ||
|
||
std::unordered_set<const Object*> insert_wait_before_; | ||
}; | ||
|
||
namespace transform { | ||
|
||
Pass InjectAsyncCopy() { | ||
auto pass_func = [=](PrimFunc f, IRModule m, PassContext ctx) { | ||
auto* n = f.CopyOnWrite(); | ||
AsyncCopyOnjector copy_inject; | ||
auto cp_async_injected = copy_inject(n->body); | ||
n->body = cp_async_injected; | ||
LOG(INFO) << f; | ||
return f; | ||
}; | ||
return CreatePrimFuncPass(pass_func, 0, "tir.InjectAsyncCopy", {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("tir.transform.InjectAsyncCopy").set_body_typed(InjectAsyncCopy); | ||
|
||
} // namespace transform | ||
|
||
} // namespace tir | ||
} // namespace tvm |
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
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