Skip to content
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

[Refactor] Remove AttrStmt with storage_scope key #8516

Merged
merged 7 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/tvm/tir/stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,6 @@ constexpr const char* extern_scope = "extern_scope";
* This can hint some code generator to create a new function for compute.
*/
constexpr const char* compute_scope = "compute_scope";
/*! \brief Mark storage scope of buffers */
constexpr const char* storage_scope = "storage_scope";
/*! \brief Mark storage alignement requirement of buffers */
constexpr const char* storage_alignment = "storage_alignment";
/*! \brief Mark storage scope of realization */
Expand Down
15 changes: 9 additions & 6 deletions python/tvm/contrib/hexagon.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,26 @@ def buf_align(var):
def visit(stmt):
"""Collect information about VTCM buffers and their alignments."""
if isinstance(stmt, tvm.tir.AttrStmt):
if stmt.attr_key == "storage_scope" and stmt.value == "local.vtcm":
vtcm_buffers.append(stmt.node)
elif stmt.attr_key == "storage_alignment":
if stmt.attr_key == "storage_alignment":
if not stmt.node in alignments:
alignments[stmt.node] = []
alignments[stmt.node].append(stmt.value)
elif isinstance(stmt, tvm.tir.Allocate):
scope = stmt.buffer_var.type_annotation.storage_scope
if scope == "local.vtcm":
vtcm_buffers.append(stmt.buffer_var)

def mutate(stmt):
"""Insert calls to VTCM allocation and deallocation routines."""
if isinstance(stmt, tvm.tir.AttrStmt):
if stmt.attr_key == "storage_scope" and stmt.value == "local.vtcm":
vtcm_buffers.pop()
elif stmt.attr_key == "storage_alignment":
if stmt.attr_key == "storage_alignment":
alignments[stmt.node].pop()
return stmt
if isinstance(stmt, tvm.tir.Allocate):
var = stmt.buffer_var
scope = var.type_annotation.storage_scope
if scope == "local.vtcm":
vtcm_buffers.pop()
if var in vtcm_buffers:
masahi marked this conversation as resolved.
Show resolved Hide resolved
is_null = tvm.tir.call_intrin("bool", tvm.ir.Op.get("tir.isnullptr"), var)
throw_error = tvm.tir.call_intrin(
Expand Down
3 changes: 1 addition & 2 deletions python/tvm/script/scope_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ def __init__(self):
def allocate(extents, dtype, scope, condition=True, span=None):
condition = tvm.runtime.convert(condition)
scope = tvm.runtime.convert(scope)
body = tvm.tir.Allocate(
return tvm.tir.Allocate(
self.buffer_var, dtype, extents, condition, self.body, span=span
)
return tvm.tir.AttrStmt(self.buffer_var, "storage_scope", scope, body, span=span)

super().__init__(allocate, concise_scope=True, def_symbol=True)
self.buffer_var = None
Expand Down
2 changes: 0 additions & 2 deletions python/tvm/tir/ir_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,6 @@ def allocate(self, dtype, shape, name="buf", scope=""):
buffer_var = _expr.Var(name, PointerType(PrimType(dtype), scope))
if not isinstance(shape, (list, tuple, _container.Array)):
shape = [shape]
if scope:
self.scope_attr(buffer_var, "storage_scope", scope)
self.emit(lambda x: _stmt.Allocate(buffer_var, dtype, shape, const(1, dtype="uint1"), x))
return BufferVar(self, buffer_var, shape, dtype)

Expand Down
4 changes: 3 additions & 1 deletion src/printer/tir_text_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <algorithm>
#include <string>

#include "../tir/transforms/ir_utils.h"
#include "doc.h"
#include "meta_data.h"
#include "text_printer.h"
Expand Down Expand Up @@ -447,8 +448,9 @@ Doc TIRTextPrinter::VisitStmt_(const BufferRealizeNode* op) {

Doc TIRTextPrinter::VisitStmt_(const AllocateNode* op) {
Doc doc;
auto scope = GetPtrStorageScope(op->buffer_var);
doc << "allocate(" << Print(op->buffer_var) << ", " << PrintDType(op->dtype) << ", "
<< Print(op->extents) << ")";
<< Print(op->extents) << "), storage_scope = " << scope;
if (!is_one(op->condition)) {
doc << " if " << Print(op->condition);
}
Expand Down
48 changes: 21 additions & 27 deletions src/printer/tvmscript_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <algorithm>
#include <utility>

#include "../tir/transforms/ir_utils.h"
#include "doc.h"
#include "meta_data.h"
#include "text_printer.h"
Expand Down Expand Up @@ -579,31 +580,6 @@ Doc TVMScriptPrinter::VisitStmt_(const LetStmtNode* op) {

Doc TVMScriptPrinter::VisitStmt_(const AttrStmtNode* op) {
Doc doc;
// merge attr with allocate when possible
if (op->node->IsInstance<VarNode>() && op->attr_key == "storage_scope" &&
op->body->IsInstance<AllocateNode>()) {
const auto* alloc = Downcast<Allocate>(op->body).get();
if (alloc->buffer_var.same_as(op->node)) {
var_not_in_headers.insert(alloc->buffer_var.get());
if (current_num_ != num_child_ - 1) {
doc << "with tir.allocate(" << Print(alloc->extents) << ", " << PrintDType(alloc->dtype)
<< ", " << Print(op->value);
if (!is_one(alloc->condition)) {
doc << ", " << Print(alloc->condition);
}
doc << ") as " << Print(op->node) << ":";
doc << Doc::Indent(4, Doc::NewLine() << PrintBody(alloc->body));
} else {
doc << Print(op->node) << " = tir.allocate(" << Print(alloc->extents) << ", "
<< PrintDType(alloc->dtype) << ", " << Print(op->value);
if (!is_one(alloc->condition)) {
doc << ", " << Print(alloc->condition);
}
doc << ")" << Doc::NewLine() << PrintBody(alloc->body);
}
return doc;
}
}
// merge attr with realize when possible
if (op->node->IsInstance<BufferNode>() && op->attr_key == "realize_scope" &&
op->body->IsInstance<BufferRealizeNode>()) {
Expand Down Expand Up @@ -681,8 +657,26 @@ Doc TVMScriptPrinter::VisitStmt_(const BufferRealizeNode* op) {
}

Doc TVMScriptPrinter::VisitStmt_(const AllocateNode* op) {
LOG(FATAL) << "TVM Script Printer Internal Error: All the Allocate should be folded with Attr";
return Doc();
var_not_in_headers.insert(op->buffer_var.get());
Doc doc;
auto storage_scope = GetPtrStorageScope(op->buffer_var);
if (current_num_ != num_child_ - 1) {
doc << "with tir.allocate(" << Print(op->extents) << ", " << PrintDType(op->dtype) << ", "
<< Print(storage_scope);
if (!is_one(op->condition)) {
doc << ", " << Print(op->condition);
}
doc << ") as " << Print(op->buffer_var) << ":";
doc << Doc::Indent(4, Doc::NewLine() << PrintBody(op->body));
} else {
doc << Print(op->buffer_var) << " = tir.allocate(" << Print(op->extents) << ", "
<< PrintDType(op->dtype) << ", " << Print(storage_scope);
if (!is_one(op->condition)) {
doc << ", " << Print(op->condition);
}
doc << ")" << Doc::NewLine() << PrintBody(op->body);
}
return doc;
}

Doc TVMScriptPrinter::VisitStmt_(const IfThenElseNode* op) {
Expand Down
2 changes: 0 additions & 2 deletions src/relay/backend/aot_executor_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,6 @@ class AOTExecutorCodegen : public ExprVisitor {
// so we don't pay the price of allocation for every inference
if (!allocated[sid]) {
body = tir::Allocate(sids_table_[sid], DataType::Int(8), {size}, tir::const_true(), body);
body = tir::AttrStmt(sids_table_[sid], tir::attr::storage_scope, tir::StringImm("global"),
body);
}
allocated[sid] = true;
}
Expand Down
15 changes: 5 additions & 10 deletions src/target/source/codegen_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,11 @@ void CodeGenC::VisitStmt_(const AllocateNode* op) {
this->PrintIndent();
int32_t constant_size = op->constant_allocation_size();
ICHECK_GT(constant_size, 0) << "Can only handle constant size stack allocation for now";
const VarNode* buffer = op->buffer_var.as<VarNode>();
auto it = alloc_storage_scope_.find(buffer);
if (it != alloc_storage_scope_.end()) {
std::string scope = alloc_storage_scope_.at(buffer);
PrintStorageScope(scope, stream);
}

auto scope = GetPtrStorageScope(op->buffer_var);
alloc_storage_scope_[op->buffer_var.get()] = scope;
PrintStorageScope(scope, stream);

PrintType(op->dtype, stream);
stream << ' ' << vid << '[' << constant_size << "];\n";

Expand All @@ -882,10 +881,6 @@ void CodeGenC::VisitStmt_(const AttrStmtNode* op) {
BindThreadIndex(iv);
}
}
} else if (op->attr_key == tir::attr::storage_scope) {
const VarNode* v = op->node.as<VarNode>();
ICHECK(v);
alloc_storage_scope_[v] = op->value.as<StringImmNode>()->value;
} else if (op->attr_key == tir::attr::volatile_scope) {
const VarNode* v = op->node.as<VarNode>();
ICHECK(v);
Expand Down
3 changes: 0 additions & 3 deletions src/te/operation/cross_thread_reduction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,9 @@ Stmt MakeCrossThreadReduction(const ComputeOpNode* self, const Stage& stage,
Stmt body = SeqStmt::Flatten(reduce_body, assign_body);
for (size_t idx = size; idx != 0; --idx) {
body = Allocate(res_handles[idx - 1], reduces[idx - 1]->dtype, {1}, const_true(), body);
body = AttrStmt(res_handles[idx - 1], tir::attr::storage_scope, StringImm("local"), body);
if (!normal_red.empty()) {
body =
Allocate(normal_res_handles[idx - 1], reduces[idx - 1]->dtype, {1}, const_true(), body);
body =
AttrStmt(normal_res_handles[idx - 1], tir::attr::storage_scope, StringImm("local"), body);
}
}
body = Substitute(body, value_map);
Expand Down
21 changes: 6 additions & 15 deletions src/tir/analysis/verify_gpu_code.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <tvm/tir/stmt.h>
#include <tvm/tir/stmt_functor.h>

#include "../transforms/ir_utils.h"

namespace tvm {
namespace tir {

Expand Down Expand Up @@ -58,11 +60,12 @@ class GPUCodeVerifier : public StmtExprVisitor {

void VisitStmt_(const AllocateNode* op) final {
StmtVisitor::VisitStmt_(op);
auto scope = GetPtrStorageScope(op->buffer_var);
// visit an allocation of a buffer in shared memory, record its size
if (visited_local_buffers_.count(op->buffer_var.get()) != 0) {
if (scope == "local") {
size_t size = static_cast<size_t>(op->constant_allocation_size());
local_memory_per_block_ += size * op->dtype.bytes() * op->dtype.lanes();
} else if (visited_shared_buffers_.count(op->buffer_var.get()) != 0) {
} else if (scope == "shared") {
size_t size = static_cast<size_t>(op->constant_allocation_size());
shared_memory_per_block_ += size * op->dtype.bytes() * op->dtype.lanes();
}
Expand All @@ -78,15 +81,7 @@ class GPUCodeVerifier : public StmtExprVisitor {
}

void VisitStmt_(const AttrStmtNode* op) final {
if (op->attr_key == attr::storage_scope) {
std::string op_value = op->value.as<StringImmNode>()->value;
if (op_value == "local") {
visited_local_buffers_.insert(op->node.as<VarNode>());
} else if (op_value == "shared") {
visited_shared_buffers_.insert(op->node.as<VarNode>());
}
StmtVisitor::VisitStmt_(op);
} else if (op->attr_key == attr::thread_extent || op->attr_key == attr::virtual_thread) {
if (op->attr_key == attr::thread_extent || op->attr_key == attr::virtual_thread) {
if (nest_level_ == 0) {
// enter a new kernel, reset statistics
Reset_();
Expand Down Expand Up @@ -211,8 +206,6 @@ class GPUCodeVerifier : public StmtExprVisitor {
private:
int nest_level_{0};

std::unordered_set<const VarNode*> visited_local_buffers_;
std::unordered_set<const VarNode*> visited_shared_buffers_;
std::unordered_set<std::string> visited_threads_;

size_t thread_x_extent_, thread_y_extent_, thread_z_extent_;
Expand All @@ -230,8 +223,6 @@ class GPUCodeVerifier : public StmtExprVisitor {
std::vector<String> errors_;

void Reset_() {
visited_local_buffers_.clear();
visited_shared_buffers_.clear();
local_memory_per_block_ = 0;
shared_memory_per_block_ = 0;

Expand Down
14 changes: 3 additions & 11 deletions src/tir/ir/stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)

// AttrStmt
AttrStmt::AttrStmt(ObjectRef node, String attr_key, PrimExpr value, Stmt body, Span span) {
if (attr_key == attr::storage_scope) {
const VarNode* buf = node.as<VarNode>();
ICHECK(buf);
const auto* ptr_type = buf->type_annotation.as<PointerTypeNode>();
ICHECK(ptr_type) << "The provided variable is not of pointer type";
auto attr_scope = value.as<StringImmNode>()->value;
ICHECK_EQ(attr_scope, ptr_type->storage_scope)
<< "Storage scopes attached to AttrStmt and buffer var are different. " << attr_scope
<< ", " << ptr_type->storage_scope;
}
auto n = make_object<AttrStmtNode>();
n->node = node;
n->attr_key = std::move(attr_key);
Expand Down Expand Up @@ -370,13 +360,15 @@ TVM_REGISTER_NODE_TYPE(AllocateNode);
TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
.set_dispatch<AllocateNode>([](const ObjectRef& node, ReprPrinter* p) {
auto* op = static_cast<const AllocateNode*>(node.get());
const auto* ptr_type = op->buffer_var->type_annotation.as<PointerTypeNode>();
ICHECK(ptr_type) << "The provided variable is not of pointer type";
p->PrintIndent();
p->stream << "allocate " << op->buffer_var << "[" << op->dtype;
for (size_t i = 0; i < op->extents.size(); ++i) {
p->stream << " * ";
p->Print(op->extents[i]);
}
p->stream << "]";
p->stream << "], storage_scope = " << ptr_type->storage_scope;
if (!is_one(op->condition)) {
p->stream << " if ";
p->Print(op->condition);
Expand Down
1 change: 0 additions & 1 deletion src/tir/transforms/flatten_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ class BufferFlattener : public StmtExprMutator {
String storage_scope = buffer.scope();
PrimExpr area = BufferArea(buffer);
body = Allocate(buffer->data, buffer->dtype, {area}, const_true(), std::move(body));
body = AttrStmt(buffer->data, attr::storage_scope, StringImm(storage_scope), std::move(body));
return body;
}

Expand Down
18 changes: 3 additions & 15 deletions src/tir/transforms/inject_copy_intrin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <tvm/tir/transform.h>

#include "../../arith/pattern_match.h"
#include "ir_utils.h"

namespace tvm {
namespace tir {
Expand All @@ -42,10 +43,7 @@ class CopyIntrinInjector : public StmtMutator {
flower_copy_fromto_(flower_copy_fromto) {}

Stmt VisitStmt_(const AttrStmtNode* op) final {
if (op->attr_key == attr::storage_scope) {
const VarNode* buf = op->node.as<VarNode>();
storage_scope_[buf] = op->value.as<StringImmNode>()->value;
} else if (op->attr_key == pragma_key_) {
if (op->attr_key == pragma_key_) {
Stmt ret;
ICHECK(MatchCopyPattern(op->body, &ret)) << "Cannot match copy pattern of " << op->body;
return ret;
Expand Down Expand Up @@ -155,21 +153,11 @@ class CopyIntrinInjector : public StmtMutator {
ICHECK(out->defined()) << "flower function did not return correct stmt";
return true;
}
// Get storage scope
std::string GetStorageScope(const VarNode* var) const {
auto it = storage_scope_.find(var);
if (it != storage_scope_.end()) {
return it->second;
} else {
return "";
}
}

// pragma key
std::string pragma_key_;
// function to lower copy intrinsics.
const PackedFunc& flower_copy_fromto_;
// Storage scope
std::unordered_map<const VarNode*, std::string> storage_scope_;
// arith analyzer
arith::Analyzer analyzer_;
};
Expand Down
17 changes: 4 additions & 13 deletions src/tir/transforms/inject_double_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,18 @@ class DoubleBufferInjector : public StmtExprMutator {
}

Stmt VisitStmt_(const AttrStmtNode* op) final {
if (op->attr_key == attr::storage_scope) {
const VarNode* buf = op->node.as<VarNode>();
auto it = dbuffer_info_.find(buf);
if (it != dbuffer_info_.end()) {
it->second.scope = op->value.as<StringImmNode>()->value;
return this->VisitStmt(op->body);
} else {
return StmtExprMutator::VisitStmt_(op);
}
} else if (op->attr_key == attr::double_buffer_scope) {
if (op->attr_key == attr::double_buffer_scope) {
return MakeProducer(op);
} else {
return StmtExprMutator::VisitStmt_(op);
}
}

Stmt VisitStmt_(const AllocateNode* op) final {
auto it = dbuffer_info_.find(op->buffer_var.get());
const VarNode* buf = op->buffer_var.as<VarNode>();
auto it = dbuffer_info_.find(buf);
if (it != dbuffer_info_.end()) {
it->second.scope = GetPtrStorageScope(op->buffer_var);
it->second.stride = foldl([](PrimExpr a, PrimExpr b, Span span) { return mul(a, b, span); },
make_const(DataType::Int(32), 1), op->extents) *
op->dtype.lanes();
Expand All @@ -125,8 +118,6 @@ class DoubleBufferInjector : public StmtExprMutator {
}
ICHECK(it->second.loop != nullptr);
auto& alloc_nest = loop_allocs_[it->second.loop];
alloc_nest.emplace_back(
AttrStmt(op->buffer_var, attr::storage_scope, StringImm(it->second.scope), Evaluate(0)));
alloc_nest.emplace_back(
Allocate(op->buffer_var, op->dtype, new_extents, op->condition, Evaluate(0)));
return op->body;
Expand Down
Loading