tvm
exec_scope.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  */
24 #ifndef TVM_TIRX_EXEC_SCOPE_H_
25 #define TVM_TIRX_EXEC_SCOPE_H_
26 
27 #include <tvm/ffi/container/variant.h>
28 #include <tvm/ir/module.h>
29 #include <tvm/tirx/var.h>
30 
31 #include <string>
32 #include <utility>
33 
34 namespace tvm {
35 namespace tirx {
36 
45 enum class ScopeKind : int {
46  kCluster = 2,
47  kCta = 3,
48  kWarpgroup = 4,
49  kWarp = 5,
50  kThread = 6,
51 };
52 
54 TVM_DLL std::string ScopeKindToString(ScopeKind kind);
55 
57 TVM_DLL ScopeKind StringToScopeKind(const ffi::String& name);
58 
79 enum class ScopeBinding : int {
80  kKernelCluster = 0,
81  kKernelCta = 1,
82  kClusterCta = 2,
83  kCtaWarpgroup = 3,
84  kCtaWarp = 4,
85  kWarpgroupWarp = 5,
86  kWarpThread = 6,
87  kCtaThread = 7,
88  kWarpgroupThread = 8,
89  kClusterCtaPair = 9,
90 };
91 
93 TVM_DLL std::pair<ffi::String, ffi::String> ScopeBindingToStringPair(ScopeBinding binding);
94 
96 TVM_DLL ScopeBinding StringPairToScopeBinding(const ffi::String& parent, const ffi::String& cur);
97 
98 /******** Definition of ScopeId ********/
99 class ScopeIdDefNode : public ffi::Object {
100  public:
102  ffi::Array<PrimVar> def_ids;
115  ffi::Optional<ffi::Array<PrimExpr>> extents;
122  ffi::Optional<ffi::Array<PrimExpr>> preferred_extents;
123 
124  static void RegisterReflection() {
125  namespace refl = tvm::ffi::reflection;
126  refl::ObjectDef<ScopeIdDefNode>()
127  .def_ro("def_ids", &ScopeIdDefNode::def_ids, refl::AttachFieldFlag::SEqHashDefRecursive())
128  .def_ro("extents", &ScopeIdDefNode::extents)
129  .def_ro("scope", &ScopeIdDefNode::scope)
130  .def_ro("preferred_extents", &ScopeIdDefNode::preferred_extents);
131  }
132 
133  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
134  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.ScopeIdDef", ScopeIdDefNode, ffi::Object);
135 };
136 
137 class ScopeIdDef : public ffi::ObjectRef {
138  public:
139  TVM_DLL explicit ScopeIdDef(ffi::Array<PrimVar> def_ids,
140  ffi::Optional<ffi::Array<PrimExpr>> extents, ScopeBinding scope,
141  ffi::Optional<ffi::Array<PrimExpr>> preferred_extents =
142  ffi::Optional<ffi::Array<PrimExpr>>(std::nullopt));
143 
145  bool is_deferred() const { return !get()->extents.has_value(); }
146 
149 
152 };
153 
155  public:
156  using ScopeIdSet = std::unordered_map<ScopeBinding, ScopeIdDef>;
157 
167  enum class Mode { kRelaxed, kStrict };
168 
170  bool Verify(const ffi::Array<ScopeIdDef>& defs, Mode mode = Mode::kStrict);
171 
177 };
178 
184  public:
185  using LaunchParams = std::unordered_map<ffi::String, IterVar>;
186 
188  TVM_DLL static ffi::Array<PrimExpr> Resolve(ScopeBinding binding,
189  const ffi::Optional<ffi::Array<PrimExpr>>& extents,
190  int out_dim, const ffi::String& target_kind,
191  const LaunchParams& params);
192 
194  TVM_DLL static PrimExpr ComputeWarpIdInCta(const LaunchParams& params);
195 };
196 
204  return static_cast<int>(a) < static_cast<int>(b);
205 }
206 
208 TVM_DLL bool ScopeNameHigher(const ffi::String& a, const ffi::String& b);
209 
210 /******** Definition of Execution Scope ********/
211 class ExecScopeNode : public ffi::Object {
212  public:
215 
217  ffi::String name() const { return ScopeKindToString(kind); }
218 
219  static void RegisterReflection() {
220  namespace refl = tvm::ffi::reflection;
221  refl::ObjectDef<ExecScopeNode>().def_ro("kind", &ExecScopeNode::kind);
222  }
223 
224  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
225  TVM_FFI_DECLARE_OBJECT_INFO("tirx.ExecScope", ExecScopeNode, ffi::Object);
226 };
227 
228 class ExecScope : public ffi::ObjectRef {
229  public:
231  TVM_DLL explicit ExecScope(ScopeKind kind);
233  TVM_DLL explicit ExecScope(const ffi::String& name) : ExecScope(StringToScopeKind(name)) {}
234 
236 };
237 
238 } // namespace tirx
239 } // namespace tvm
240 
241 #endif // TVM_TIRX_EXEC_SCOPE_H_
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition: base_expr.h:354
Definition: exec_scope.h:211
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: exec_scope.h:224
ScopeKind kind
scope identity; one of the closed ScopeKind values.
Definition: exec_scope.h:214
static void RegisterReflection()
Definition: exec_scope.h:219
TVM_FFI_DECLARE_OBJECT_INFO("tirx.ExecScope", ExecScopeNode, ffi::Object)
ffi::String name() const
Human-readable name derived from kind (for printing / errors).
Definition: exec_scope.h:217
Definition: exec_scope.h:228
ExecScope(ScopeKind kind)
Construct from a ScopeKind (canonical).
ExecScope(const ffi::String &name)
Construct from a name string (FATALs on unknown name).
Definition: exec_scope.h:233
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ExecScope, ffi::ObjectRef, ExecScopeNode)
Definition: exec_scope.h:99
ffi::Array< PrimVar > def_ids
The ScopeId defined.
Definition: exec_scope.h:102
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.ScopeIdDef", ScopeIdDefNode, ffi::Object)
ScopeBinding scope
The (parent, cur) binding of this scope id as a closed enum.
Definition: exec_scope.h:117
static void RegisterReflection()
Definition: exec_scope.h:124
ffi::Optional< ffi::Array< PrimExpr > > extents
The extents of the ScopeId.
Definition: exec_scope.h:115
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: exec_scope.h:133
ffi::Optional< ffi::Array< PrimExpr > > preferred_extents
Optional preferred extents (cluster→cta only). Maps to cudaLaunchAttributePreferredClusterDimension (...
Definition: exec_scope.h:122
Definition: exec_scope.h:154
std::unordered_map< ScopeBinding, ScopeIdDef > ScopeIdSet
Definition: exec_scope.h:156
ScopeIdSet id_set
The resolved scope id set; id_set[binding] is the best-known def for that binding (extents filled in ...
Definition: exec_scope.h:176
Mode
Verification mode.
Definition: exec_scope.h:167
bool Verify(const ffi::Array< ScopeIdDef > &defs, Mode mode=Mode::kStrict)
Verify the scope id definitions are well formed.
Definition: exec_scope.h:137
TVM_DEFINE_OBJECT_REF_COW_METHOD(ScopeIdDefNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ScopeIdDef, ffi::ObjectRef, ScopeIdDefNode)
bool is_deferred() const
Whether this def has a deferred (unknown) extent.
Definition: exec_scope.h:145
ScopeIdDef(ffi::Array< PrimVar > def_ids, ffi::Optional< ffi::Array< PrimExpr >> extents, ScopeBinding scope, ffi::Optional< ffi::Array< PrimExpr >> preferred_extents=ffi::Optional< ffi::Array< PrimExpr >>(std::nullopt))
PrimExpr fused_extent() const
Product of all extent dimensions. PRECONDITION: !is_deferred().
Static resolver for ScopeIdDef values. Replaces the former ScopeIdResolveTable runtime registry with ...
Definition: exec_scope.h:183
static ffi::Array< PrimExpr > Resolve(ScopeBinding binding, const ffi::Optional< ffi::Array< PrimExpr >> &extents, int out_dim, const ffi::String &target_kind, const LaunchParams &params)
Resolve a ScopeIdDef for a given canonical binding + target.
static PrimExpr ComputeWarpIdInCta(const LaunchParams &params)
Compute the warp_id_in_cta shuffle expression from threadIdx in launch params.
std::unordered_map< ffi::String, IterVar > LaunchParams
Definition: exec_scope.h:185
IRModule that holds the functions and type definitions.
std::pair< ffi::String, ffi::String > ScopeBindingToStringPair(ScopeBinding binding)
Convert a ScopeBinding to its (parent, cur) string pair.
bool ScopeKindHigher(ScopeKind a, ScopeKind b)
Strict-weak "a is wider than b" on scope kinds: world > kernel > cluster > cta > warpgroup > warp > t...
Definition: exec_scope.h:203
ScopeKind StringToScopeKind(const ffi::String &name)
Parse a string name to a ScopeKind. FATAL if unknown.
ScopeKind
The target execution scope kind of a tile primitive call.
Definition: exec_scope.h:45
bool ScopeNameHigher(const ffi::String &a, const ffi::String &b)
String-keyed convenience over ScopeKindHigher. FATALs on bad name.
ScopeBinding
The binding between a parent scope and a child scope as used by a ScopeIdDef. The closed enum of vali...
Definition: exec_scope.h:79
ScopeBinding StringPairToScopeBinding(const ffi::String &parent, const ffi::String &cur)
Parse a (parent, cur) string pair to a ScopeBinding. FATAL if unknown.
std::string ScopeKindToString(ScopeKind kind)
Convert a ScopeKind to its string name (e.g. kThread -> "thread").
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
Variables in the TIR.