mlir(google/heir)traits
https://mlir.llvm.org/docs/Traits/
MLIR allows for a truly open ecosystem, as any dialect may define attributes, operations, and types that suit a specific level of abstraction. Traits are a mechanism which abstracts implementation details and properties that are common across many different attributes/operations/types/etc.. Traits may be used to specify special properties and constraints of the object, including whether an operation has side effects or that its output has the same type as the input. Some examples of operation traits are Commutative, Terminator, etc. See the more comprehensive list of operation traits below for more examples of what is possible.
理解:MLIR 允许构建一个真正开放的生态系统,因为任何方言都可以定义适合特定抽象层次的属性、操作和类型。特性是一种机制,用于抽象出在许多不同属性/操作/类型等中常见的实现细节和属性。特性可以用来指定对象的特殊属性和约束,包括操作是否具有副作用,或者其输出是否与输入类型相同。一些操作特性的例子包括可交换性、终端等。请参见更全面的操作特性列表,以了解可能实现的更多示例。
Defining a Trait
Traits may be defined in C++ by inheriting from the TraitBase<ConcreteType, TraitType> class for the specific IR type. For attributes, this is AttributeTrait::TraitBase. For operations, this is OpTrait::TraitBase. For types, this is TypeTrait::TraitBase. This base class takes as template parameters:
- ConcreteType
The concrete class type that this trait was attached to.
- TraitType
The type of the trait class that is being defined, for use with the Curiously Recurring Template Pattern.
理解:在C++代码中定义AttributeTrait::TraitBase的继承类,可为类型为attributes的IR定义新的Traits;在C++代码中定义OpTrait::TraitBase的继承类,可为类型为operations的IR定义新的Traits;在C++代码中定义TypeTrait::TraitBase的继承类,可为类型为types的IR定义新的Traits;基类TraitBase的实例化需要两个参数:
- 参数1:ConcreteType,要应用此Traits的具体类;
- 参数2:TraitType,指新定义的Traits本身;
Operation traits may also provide a verifyTrait or verifyRegionTrait hook that is called when verifying the concrete operation. The difference between these two is that whether the verifier needs to access the regions, if so, the operations in the regions will be verified before the verification of this trait. The verification order determines when a verifier will be invoked.
理解:为类型Operation的IR定义的traits还可以提供钩子函数verifyTrait或verifyRegionTrait的定义。
比如:

1 template <typename ConcreteType> 2 class MyTrait : public OpTrait::TraitBase<ConcreteType, MyTrait> { 3 public: 4 /// Override the 'verifyTrait' hook to add additional verification on the 5 /// concrete operation. 6 static LogicalResult verifyTrait(Operation *op) { 7 // ... 8 } 9 };
又比如:

1 namespace mlir::heir::lwe { 2 3 // Trait that ensures that all operands and results ciphertext have the same set 4 // of rings. 5 template <typename ConcreteType> 6 class SameOperandsAndResultRings 7 : public OpTrait::TraitBase<ConcreteType, SameOperandsAndResultRings> { 8 public: 9 static LogicalResult verifyTrait(Operation *op) { 10 // ... 11 } 12 }; 13 14 // Trait that ensures that all operands and results ciphertext/plaintext have 15 // the same set of application space/plaintext spaces. 16 template <typename ConcreteType> 17 class SameOperandsAndResultPlaintextTypes 18 : public OpTrait::TraitBase<ConcreteType, 19 SameOperandsAndResultPlaintextTypes> { 20 public: 21 static LogicalResult verifyTrait(Operation *op) { 22 // ... 23 } 24 }; 25 26 } // namespace mlir::heir::lwe
Attaching Operation Traits in ODS
To use an operation trait in the ODS framework, we need to provide a definition of the trait class. This can be done using the NativeOpTrait and ParamNativeOpTrait classes. ParamNativeOpTrait provides a mechanism in which to specify arguments to a parametric trait class with an internal Impl.
理解:要在定义operation的td源码中使用为operation编写的trait类,需要在其他td文件中写好此trait类的声明(定义在另外的C++源码中),这个主要是在td中使用NativeOpTrait来实现的。
比如:

1 #ifndef LIB_DIALECT_LWE_IR_LWETRAITS_TD_ 2 #define LIB_DIALECT_LWE_IR_LWETRAITS_TD_ 3 4 include "mlir/IR/OpBase.td" 5 6 //SameOperandsAndResultRings是在lib/Dialect/LWE/IR/LWETraits.h定义的 7 def SameOperandsAndResultRings: NativeOpTrait<"SameOperandsAndResultRings"> { 8 let cppNamespace = "::mlir::heir::lwe"; 9 } 10 11 //SameOperandsAndResultPlaintextTypes是在lib/Dialect/LWE/IR/LWETraits.h定义的 12 def SameOperandsAndResultPlaintextTypes: NativeOpTrait<"SameOperandsAndResultPlaintextTypes"> { 13 let cppNamespace = "::mlir::heir::lwe"; 14 } 15 16 #endif // LIB_DIALECT_LWE_IR_LWETRAITS_TD_

1 #ifndef LIB_DIALECT_LWE_IR_LWEOPS_TD_ 2 #define LIB_DIALECT_LWE_IR_LWEOPS_TD_ 3 4 include "lib/Dialect/LWE/IR/LWETraits.td" 5 6 def LWE_RAddOp : LWE_BinOp<"radd", [SameOperandsAndResultRings, SameOperandsAndResultPlaintextTypes, InferTypeOpAdaptor, Commutative]> { 7 let summary = "Add two RLWE ciphertexts"; 8 let assemblyFormat = [{ 9 operands attr-dict `:` functional-type(operands, results) 10 }]; 11 } 12 13 #endif // LIB_DIALECT_LWE_IR_LWEOPS_TD_
posted on 2025-04-18 21:40 LiveWithACat 阅读(10) 评论(0) 收藏 举报