跳转到正文
Retep's
返回

[TIL] MLIR Toy 详解:第 7 章

编辑文章

本文由英文原文翻译。代码、公式与技术名称保留原文。

教程链接

这篇文章讨论如何在方言中创建复合类型。

复合类型

此前我们只处理 tensor 这一基础类型。现在为 Toy 添加复合类型,例如:

struct MyStruct {
  # Inside of the struct is a list of variable declarations without initializers
  # or shapes, which may also be other previously defined structs.
  var a;
  var b;
}

结构体应能用作函数参数和返回值,并支持通过 . 访问成员。

类型类

需要为复合类型定义自己的类型类。我们把结构体表示为一组元素类型构成的匿名容器,因为结构体名和成员名只对 Toy 编译器的 AST 有意义。

注意,这里创建的不是编程语言层面的具体类型。语言中的 S1 和 S2 可能是不同类型,但它们由同一个类型类,也就是 struct,来表示。

struct S1{
  var a;
  var b;
}

struct S2{
  var c;
}

教程指出,创建类型类时首先需要理解:

MLIR 中的类型对象具有值语义,实际数据由内部存储对象持有。Type 类只是内部 TypeStorage 的轻量封装,而存储对象会在同一个 MLIRContext 内进行唯一化。

我们同样在 Dialect.cpp 中加入类型类,与操作定义放在一起。StructTypeStorage 保存元素类型列表,它的键就是所有成员的类型列表。

/// This class represents the internal storage of the Toy `StructType`.
struct StructTypeStorage : public mlir::TypeStorage {
  /// The `KeyTy` is a required type that provides an interface for the storage
  /// instance. This type will be used when uniquing an instance of the type
  /// storage. For our struct type, we will unique each instance structurally on
  /// the elements that it contains.
  using KeyTy = llvm::ArrayRef<mlir::Type>;

  /// A constructor for the type storage instance.
  StructTypeStorage(llvm::ArrayRef<mlir::Type> elementTypes)
      : elementTypes(elementTypes) {}

  /// Define the comparison function for the key type with the current storage
  /// instance. This is used when constructing a new instance to ensure that we
  /// haven't already uniqued an instance of the given key.
  bool operator==(const KeyTy &key) const { return key == elementTypes; }

  /// Define a hash function for the key type. This is used when uniquing
  /// instances of the storage.
  /// Note: This method isn't necessary as both llvm::ArrayRef and mlir::Type
  /// have hash functions available, so we could just omit this entirely.
  static llvm::hash_code hashKey(const KeyTy &key) {
    return llvm::hash_value(key);
  }

  /// Define a construction function for the key type from a set of parameters.
  /// These parameters will be provided when constructing the storage instance
  /// itself, see the `StructType::get` method further below.
  /// Note: This method isn't necessary because KeyTy can be directly
  /// constructed with the given parameters.
  static KeyTy getKey(llvm::ArrayRef<mlir::Type> elementTypes) {
    return KeyTy(elementTypes);
  }

  /// Define a construction method for creating a new instance of this storage.
  /// This method takes an instance of a storage allocator, and an instance of a
  /// `KeyTy`. The given allocator must be used for *all* necessary dynamic
  /// allocations used to create the type storage and its internal.
  static StructTypeStorage *construct(mlir::TypeStorageAllocator &allocator,
                                      const KeyTy &key) {
    // Copy the elements from the provided `KeyTy` into the allocator.
    llvm::ArrayRef<mlir::Type> elementTypes = allocator.copyInto(key);

    // Allocate the storage instance and construct it.
    return new (allocator.allocate<StructTypeStorage>())
        StructTypeStorage(elementTypes);
  }

  /// The following field contains the element types of the struct.
  llvm::ArrayRef<mlir::Type> elementTypes;
};

类型中不必保存 member_a 等成员名,因为 AST 阶段可以将它们转换为成员索引。进一步说,在编译器眼中,下面这些是相同的类型:

struct A {
  var a;
  var b;
}

struct B {
  var c;
  var d;
}

接下来基于存储类型定义结构体类型,代码很精简。

/// This class defines the Toy struct type. It represents a collection of
/// element types. All derived types in MLIR must inherit from the CRTP class
/// 'Type::TypeBase'. It takes as template parameters the concrete type
/// (StructType), the base class to use (Type), and the storage class
/// (StructTypeStorage).
class StructType : public mlir::Type::TypeBase<StructType, mlir::Type,
                                               StructTypeStorage> {
public:
  /// Inherit some necessary constructors from 'TypeBase'.
  using Base::Base;


  /// factory method/constructor for the type
  /// Create an instance of a `StructType` with the given element types. There
  /// *must* be at least one element type.
  static StructType get(llvm::ArrayRef<mlir::Type> elementTypes) {
    assert(!elementTypes.empty() && "expected at least 1 element type");

    // Call into a helper 'get' method in 'TypeBase' to get a uniqued instance
    // of this type. The first parameter is the context to unique in. The
    // parameters after are forwarded to the storage instance.
    mlir::MLIRContext *ctx = elementTypes.front().getContext();
    return Base::get(ctx, elementTypes);
  }

  /// Returns the element types of this struct type.
  llvm::ArrayRef<mlir::Type> getElementTypes() {
    // 'getImpl' returns a pointer to the internal storage instance.
    return getImpl()->elementTypes;
  }

  /// Returns the number of element type held by this struct.
  size_t getNumElementTypes() { return getElementTypes().size(); }
};

添加类型的方式与添加操作、接口相同:

void ToyDialect::initialize() {
  addOperations<
#define GET_OP_LIST
#include "toy/Ops.cpp.inc"
      >();
  addInterfaces<ToyInlinerInterface>();
  addTypes<StructType>(); // add type here
}

在操作中使用类型(ODS)

目前只完成了类型定义,还需要让操作使用它。

首先修改 Ops.td,让相应操作能够接收和返回 struct。

def Toy_StructType :
    DialectType<Toy_Dialect, CPred<"::llvm::isa<StructType>($_self)">,
                "Toy struct type">;

// Provide a definition of the types that are used within the Toy dialect.
def Toy_Type : AnyTypeOf<[F64Tensor, Toy_StructType]>;

只需更新 return 和 call,因为张量运算不适用于结构体。另外定义新的 struct_access 操作,表示 . 成员访问。

def StructAccessOp : Toy_Op<"struct_access", [Pure]> {
  let summary = "struct access";
  let description = [{
    Access the Nth element of a value returning a struct type.
  }];

  let arguments = (ins Toy_StructType:$input, I64Attr:$index);
  let results = (outs Toy_Type:$output);

  let assemblyFormat = [{
    $input `[` $index `]` attr-dict `:` type($input) `->` type($output)
  }];

  // Allow building a StructAccessOp with just a struct value and an index.
  let builders = [
    OpBuilder<(ins "Value":$input, "size_t":$index)>
  ];

  // Indicate that additional verification for this operation is necessary.
  let hasVerifier = 1;

  // Set the folder bit so that we can fold constant accesses.
  let hasFolder = 1;
}

还可以实现常量折叠等优化,不过写到这里有些累了,就先告一段落。


编辑文章
分享文章:

上一篇
[TIL] 线程块内的数据交换
下一篇
[TIL] MLIR Toy 详解:第 6 章