tvm
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19#ifndef TVM_S_TIR_UTILS_H_
20#define TVM_S_TIR_UTILS_H_
21
23#include <tvm/s_tir/stmt.h>
24#include <tvm/tirx/stmt.h>
25
26#include <unordered_map>
27
28namespace tvm {
29namespace tirx {
30
38#define TVM_SREF_AS_OR_ERR(Result, SRef, Type) \
39 SRef->StmtAs<Type>(); \
40 TVM_FFI_CHECK(Result, TypeError)
41
50#define TVM_SREF_TO_SBLOCK(SRef) \
51 [&]() { \
52 auto result = TVM_SREF_AS_OR_ERR(result, (SRef), ::tvm::s_tir::SBlockNode) \
53 << "Expects StmtSRef `" << #SRef << "` points to `Block`, but gets: " \
54 << ((SRef)->stmt ? (SRef)->stmt->GetTypeKey() : "None"); \
55 return result; \
56 }()
57
66#define TVM_SREF_TO_FOR(SRef) \
67 [&]() { \
68 auto result = TVM_SREF_AS_OR_ERR(result, (SRef), ::tvm::ForNode) \
69 << "Expects StmtSRef `" << #SRef << "` points to `Loop`, but gets: " \
70 << ((SRef)->stmt ? (SRef)->stmt->GetTypeKey() : "None"); \
71 return result; \
72 }()
73
81#define TVM_TYPE_AS_OR_ERR(Result, From, Type) \
82 From.as<Type>(); \
83 TVM_FFI_CHECK(Result, TypeError)
84
91#define TVM_TYPE_AS(From, Type) \
92 [&]() { \
93 auto result = TVM_TYPE_AS_OR_ERR(result, (From), Type) \
94 << "Expects `" << #From << "` to have type `" << Type::_type_key \
95 << "`, but gets: " << ((From).GetTypeKey()); \
96 return result; \
97 }()
98
107inline void SetSeqIndex(std::unordered_map<const StmtNode*, StmtSRef>& stmt2ref, // NOLINT(*)
108 const Stmt& stmt, int seq_index, bool include_loops = true) {
109 if (const auto* realize = stmt.as<s_tir::SBlockRealizeNode>()) {
110 const s_tir::SBlockNode* block = realize->block.get();
111 TVM_FFI_ICHECK(stmt2ref.count(block));
112 stmt2ref.at(block)->seq_index = seq_index;
113 } else if (const auto* block = stmt.as<s_tir::SBlockNode>()) {
114 TVM_FFI_ICHECK(stmt2ref.count(block));
115 stmt2ref.at(block)->seq_index = seq_index;
116 } else if (const auto* loop = stmt.as<ForNode>()) {
117 if (!include_loops) return;
118 TVM_FFI_ICHECK(stmt2ref.count(loop));
119 stmt2ref.at(loop)->seq_index = seq_index;
120 }
121}
122
130 std::unordered_map<const StmtNode*, StmtSRef>& stmt2ref, // NOLINT(*)
131 const SeqStmtNode* seq_stmt, bool include_loops = true) {
132 int i = 0;
133 for (const Stmt& stmt : seq_stmt->seq) {
134 SetSeqIndex(stmt2ref, stmt, i, include_loops);
135 ++i;
136 }
137}
138
139} // namespace tirx
140} // namespace tvm
141
142#endif // TVM_S_TIR_UTILS_H_
A for loop, with possible type annotations.
Definition stmt.h:457
The container of seq statement. Represent a sequence of statements.
Definition stmt.h:75
ffi::Array< Stmt > seq
internal sequence content.
Definition stmt.h:78
Container of all statements.
Definition stmt.h:62
A block is a basic schedule unit in TIR.
Definition stmt.h:112
A block realization node represents execution of the block at the binding values.
Definition stmt.h:188
void SetSeqIndex(std::unordered_map< const StmtNode *, StmtSRef > &stmt2ref, const Stmt &stmt, int seq_index, bool include_loops=true)
Set the StmtSRefObj::seq_index field for stmt.
Definition utils.h:107
void SetSeqIndexInChildren(std::unordered_map< const StmtNode *, StmtSRef > &stmt2ref, const SeqStmtNode *seq_stmt, bool include_loops=true)
Update seq_index of the children of a SeqStmt.
Definition utils.h:129
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
S-TIR (Schedulable TIR) statements and attributes.
Definition of two pillar data structure for TensorIR scheduling: StmtSRef, SBlockScope.
TIRx statements and shared statement entry header.