23#ifndef TVM_TIRX_LAYOUT_H_
24#define TVM_TIRX_LAYOUT_H_
26#include <tvm/ffi/container/array.h>
27#include <tvm/ffi/container/tuple.h>
28#include <tvm/ffi/enum.h>
29#include <tvm/ffi/function.h>
30#include <tvm/ffi/object.h>
57class Layout :
public ffi::ObjectRef {
82 const ffi::Array<PrimExpr>& shape,
90 const ffi::Array<PrimExpr>& inner_shape)
const;
103 static ffi::reflection::TypeAttrColumn state_column(ffi::type_attr::kEnumState);
104 static ffi::EnumState state =
105 ffi::Any(state_column[_GetOrAllocRuntimeTypeIndex()]).as_or_throw<ffi::EnumState>();
106 auto column = state->attrs.Get(
"thread");
107 auto thread = column ? column->Get(ffi::GetRef<ffi::ObjectRef>(
this)) : std::nullopt;
108 TVM_FFI_ICHECK(thread.has_value())
109 <<
"Axis '" << _str_index <<
"' has no thread classification";
110 return thread->cast<
bool>();
134 const ffi::String&
name()
const {
return get()->_str_index; }
158 namespace refl = tvm::ffi::reflection;
159 refl::ObjectDef<IterObj>()
169class Iter :
public ffi::ObjectRef {
172 auto n = ffi::make_object<IterObj>(std::move(extent), std::move(stride), std::move(axis));
173 data_ = std::move(n);
185 namespace refl = tvm::ffi::reflection;
186 refl::ObjectDef<TileLayoutObj>()
197 explicit TileLayout(ffi::Array<Iter> shard, ffi::Array<Iter> replica,
198 ffi::Map<Axis, PrimExpr> offset)
199 :
Layout(ffi::UnsafeInit{}) {
200 auto n = ffi::make_object<TileLayoutObj>();
201 n->shard = std::move(shard);
202 n->replica = std::move(replica);
203 n->offset = std::move(offset);
204 data_ = std::move(n);
209 return get()->shard.Map([](
const Iter& iter) {
return iter->extent; });
223 if (get()->shard.size() > 1)
return false;
224 if (get()->shard.size() == 1) {
225 if (!get()->shard[0]->axis->IsMemoryAxis() || !
prim::IsOne(get()->shard[0]->stride))
228 return get()->replica.size() == 0 && get()->offset.size() == 0;
233 return std::any_of(get()->shard.begin(), get()->shard.end(),
234 [](
const Iter& iter) { return iter->axis->IsThreadAxis(); });
264 namespace refl = tvm::ffi::reflection;
265 refl::ObjectDef<ComposeLayoutObj>()
281 TileLayout tile_layout,
bool swizzle_inner =
true);
Shared primitive construction and constant helpers.
#define TVM_TIRX_DLL
Definition base.h:55
Algebra expression simplifications.
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Coordinated symbolic analyses over an explicitly synchronized premise environment.
Definition analyzer.h:556
bool IsMemoryAxis() const
Check if the axis is a memory axis.
Definition layout.h:114
TVM_TIRX_DLL ffi::Optional< ffi::String > GetBackend() const
The backend owning this axis, if any.
TVM_TIRX_DLL ffi::Optional< FAxisFuser > GetFuser() const
Get the fuser of the (thread) axis.
TVM_TIRX_DLL ffi::Optional< ThreadScope > GetScope() const
Get the scope of the (thread) axis.
bool IsThreadAxis() const
Check if the axis is a thread axis.
Definition layout.h:102
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Axis", AxisObj, ffi::EnumObj)
TVM_TIRX_DLL ffi::Optional< ThreadScope > GetSubscope() const
Get the subscope of the (thread) axis.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Axis, ffi::Enum, AxisObj)
static TVM_TIRX_DLL Axis Get(const ffi::String &name)
Get or create the axis with the given name.
const ffi::String & name() const
Return the canonical axis name.
Definition layout.h:134
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.ComposeLayout", ComposeLayoutObj, LayoutObj)
int per_element
Definition layout.h:253
TileLayout tile_layout
Definition layout.h:257
int swizzle_len
Definition layout.h:254
ComposeLayoutObj(TileLayout tile_layout)
Definition layout.h:250
int atom_len
Definition layout.h:255
static void RegisterReflection()
Definition layout.h:263
int inner_mask
Definition layout.h:260
int outer_mask
Definition layout.h:261
bool swizzle_inner
Definition layout.h:256
ComposeLayoutObj(ffi::UnsafeInit)
Definition layout.h:251
TVM_DEFINE_OBJECT_REF_COW_METHOD(ComposeLayoutObj)
TVM_TIRX_DLL ComposeLayout(int per_element, int swizzle_len, int atom_len, TileLayout tile_layout, bool swizzle_inner=true)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ComposeLayout, Layout, ComposeLayoutObj)
static void RegisterReflection()
Definition layout.h:157
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition layout.h:165
PrimExpr extent
Definition layout.h:153
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Iter", IterObj, ffi::Object)
IterObj(PrimExpr extent, PrimExpr stride, Axis axis)
Definition layout.h:150
Axis axis
Definition layout.h:155
PrimExpr stride
Definition layout.h:154
IterObj(ffi::UnsafeInit tag)
Definition layout.h:148
Iter(PrimExpr extent, PrimExpr stride, Axis axis)
Definition layout.h:171
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Iter, ffi::ObjectRef, IterObj)
Base object for tensor-program coordinate layouts.
Definition layout.h:50
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition layout.h:52
TVM_FFI_DECLARE_OBJECT_INFO("tirx.Layout", LayoutObj, ffi::Object)
Managed reference to a tensor-program coordinate layout.
Definition layout.h:57
TVM_TIRX_DLL Layout Tile(const TileLayout &outer, const ffi::Array< PrimExpr > &outer_shape, const ffi::Array< PrimExpr > &inner_shape) const
Tile the current layout with a given layout.
TVM_TIRX_DLL ffi::Map< ffi::String, PrimExpr > Apply(ffi::Array< PrimExpr > coord, sym::AnalyzerObj *analyzer) const
Apply a layout using caller-owned analysis state.
TVM_TIRX_DLL ffi::Map< ffi::String, PrimExpr > Apply(const ffi::Array< PrimExpr > &coord, const ffi::Array< PrimExpr > &shape, sym::AnalyzerObj *analyzer) const
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Layout, ffi::ObjectRef, LayoutObj)
TVM_TIRX_DLL Layout Canonicalize() const
Turn the layout to canonical form.
TVM_TIRX_DLL PrimExpr GetSpan(ffi::Optional< ffi::String > axis_name=std::nullopt) const
Get the span of the layout (of some axis)
TVM_TIRX_DLL PrimExpr GetSize(ffi::Optional< ffi::String > axis_name=std::nullopt) const
Get the size of the layout (of some axis)
TVM_TIRX_DLL void CheckWellFormed() const
Verify if the layout is well-formed.
TVM_TIRX_DLL ffi::Map< ffi::String, PrimExpr > Apply(PrimExpr coord, sym::AnalyzerObj *analyzer) const
Canonical thread-scope identity, named as backend.scope.
Definition thread_scope.h:55
ffi::Array< Iter > replica
Definition layout.h:181
static void RegisterReflection()
Definition layout.h:184
ffi::Array< Iter > shard
Definition layout.h:180
ffi::Map< Axis, PrimExpr > offset
Definition layout.h:182
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.TileLayout", TileLayoutObj, LayoutObj)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TileLayout, Layout, TileLayoutObj)
static TVM_TIRX_DLL TileLayout DefaultLayout(ffi::Array< PrimExpr > shape)
Get the default layout for the shape.
bool HasThreadAxis() const
Has Thread Axis.
Definition layout.h:232
bool IsTrivial() const
Is the layout trivial (pure memory, identical mapping)
Definition layout.h:222
TileLayout(ffi::Array< Iter > shard, ffi::Array< Iter > replica, ffi::Map< Axis, PrimExpr > offset)
Definition layout.h:197
TVM_TIRX_DLL ffi::Optional< Tuple< ThreadScope, ThreadScope > > GetScope() const
Get the scope pair of the layout.
ffi::Array< PrimExpr > GetShardShape() const
Get the shape of the shard.
Definition layout.h:208
TVM_DEFINE_OBJECT_REF_COW_METHOD(TileLayoutObj)
TVM_TIRX_DLL bool IsContiguous(ffi::Optional< sym::Analyzer > analyzer=std::nullopt) const
Whether the tile describes contiguous row-major memory strides.
IRModule that holds the functions and type definitions.
bool IsOne(const PrimExpr &x)
Check whether x is a constant integer 1.
Definition op.h:1044
ffi::TypedFunction< ffi::Optional< Iter >(ThreadScope, ThreadScope, Iter)> FAxisFuser
Definition layout.h:96
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Enum identities for backend-defined thread hierarchies.