第 11 课:设计第一个自定义 Dialect
文章目录
好,继续第 11 课。
这节我们真正开始“自己造一个 Dialect”。
第 11 课:设计第一个自定义 Dialect
我们先做一个非常小的:
ToyMath Dialect
名字就叫:
toy
先设计 4 个 Operation:
toy.const
toy.add
toy.mul
toy.print
目标是最终能写出:
%0 = toy.const 10 : i32
%1 = toy.const 20 : i32
%2 = toy.add %0, %1 : i32
%3 = toy.mul %2, %1 : i32
toy.print %3 : i32
这段 IR 表示:
10
20
↓
10 + 20 = 30
↓
30 * 20 = 600
↓
打印 600
1. 先不要写代码,先设计 IR
这是做 Dialect 很重要的一步。
不要一上来就写 C++。
先问:
我的 IR 想表达什么?
我们现在只表达:
整数常量
整数加法
整数乘法
打印
所以:
ToyMath
├── const
├── add
├── mul
└── print
2. 设计 toy.const
我们希望:
%0 = toy.const 10 : i32
拆一下。
Operation:
toy.const
Operand:
无
Attribute:
10
Result:
%0
Type:
i32
所以结构:
toy.const
│
├── attribute: value = 10
└── result: i32
注意:
10
不是 Operand。
它是:
Attribute
因为编译期就知道。
3. toy.const 的 ODS 思路
概念上可以定义:
def Toy_ConstOp : Toy_Op<"const"> {
let arguments = (ins
I32Attr:$value
);
let results = (outs
I32:$result
);
}
意思就是:
输入信息:
value : i32 attribute
结果:
result : i32
然后:
%0 = toy.const 10 : i32
4. 设计 toy.add
希望:
%2 = toy.add %0, %1 : i32
结构:
toy.add
│
├── operand 0: %0
├── operand 1: %1
└── result: %2
也就是:
两个输入
一个输出
ODS:
def Toy_AddOp : Toy_Op<"add"> {
let arguments = (ins
I32:$lhs,
I32:$rhs
);
let results = (outs
I32:$result
);
}
于是 C++ 里以后可以很自然地:
op.getLhs();
op.getRhs();
op.getResult();
5. 设计 toy.mul
这个和 add 非常像:
%3 = toy.mul %2, %1 : i32
ODS:
def Toy_MulOp : Toy_Op<"mul"> {
let arguments = (ins
I32:$lhs,
I32:$rhs
);
let results = (outs
I32:$result
);
}
所以:
toy.add
toy.mul
结构完全类似。
区别只是语义不同。
6. 设计 toy.print
希望:
toy.print %3 : i32
它:
有一个 Operand
没有 Result
所以:
def Toy_PrintOp : Toy_Op<"print"> {
let arguments = (ins
I32:$input
);
let results = (outs);
}
结构:
toy.print
│
└── operand: %3
没有:
result
7. 把 4 个 Operation 放一起
现在我们的 Dialect 长这样:
toy Dialect
│
├── toy.const
│ ├── attr: value
│ └── result: i32
│
├── toy.add
│ ├── lhs: i32
│ ├── rhs: i32
│ └── result: i32
│
├── toy.mul
│ ├── lhs: i32
│ ├── rhs: i32
│ └── result: i32
│
└── toy.print
└── input: i32
这已经是一个完整的小 IR 设计了。
8. Dialect 本身也要定义
比如:
def Toy_Dialect : Dialect {
let name = "toy";
let cppNamespace = "::toy";
}
表示:
MLIR 文本名字:
toy
C++ namespace:
toy
所以:
toy.add
C++ 对应大概:
toy::AddOp
9. 定义一个公共 Toy_Op
通常不会每个 Operation 都直接从最底层 Op 写起。
可以先定义一个公共基类:
class Toy_Op<string mnemonic, list<Trait> traits = []>
: Op<Toy_Dialect, mnemonic, traits>;
然后:
def Toy_AddOp : Toy_Op<"add"> {
...
}
这样所有 Toy Operation 都自动属于:
Toy_Dialect
10. 整体 .td 大概长什么样?
概念版:
def Toy_Dialect : Dialect {
let name = "toy";
let cppNamespace = "::toy";
}
class Toy_Op<string mnemonic, list<Trait> traits = []>
: Op<Toy_Dialect, mnemonic, traits>;
def Toy_ConstOp : Toy_Op<"const"> {
let arguments = (ins
I32Attr:$value
);
let results = (outs
I32:$result
);
}
def Toy_AddOp : Toy_Op<"add"> {
let arguments = (ins
I32:$lhs,
I32:$rhs
);
let results = (outs
I32:$result
);
}
def Toy_MulOp : Toy_Op<"mul"> {
let arguments = (ins
I32:$lhs,
I32:$rhs
);
let results = (outs
I32:$result
);
}
def Toy_PrintOp : Toy_Op<"print"> {
let arguments = (ins
I32:$input
);
}
先把它当“设计图”看。
11. 现在来理解生成的 C++ 类型
经过:
mlir-tblgen
后,大概会得到:
toy::ConstOp
toy::AddOp
toy::MulOp
toy::PrintOp
于是以后写 Pass:
func.walk([&](toy::AddOp op) {
...
});
或者 Rewrite:
struct LowerAdd
: OpRewritePattern<toy::AddOp> {
...
};
就很自然了。
12. 给 toy.add 加语义约束
现在有个问题。
如果有人写:
%0 = toy.add %a, %b : i32
但:
%a = i32
%b = i64
怎么办?
如果 ODS 明确写:
I32:$lhs
I32:$rhs
那 verifier 会直接认为不合法。
所以:
ODS 本身就是 IR 合法性规范的一部分
这很重要。
13. 但我们现在的设计有点“太死”
目前:
toy.add
只能算:
i32
但以后可能希望支持:
i64
f32
f64
比如:
%0 = toy.add %a, %b : f32
那应该把:
I32
改成更通用的约束。
例如概念上:
AnyType:$lhs
AnyType:$rhs
AnyType:$result
但这样又太宽松:
lhs
rhs
result
类型可能互相不一致。
所以通常需要更精细的:
same type constraints
这个以后我们讲 Trait / Constraint 时会展开。
14. toy.print 有副作用
这里有个非常关键的问题。
比如:
%0 = toy.add %a, %b : i32
如果 %0 没人用:
可能可以删
因为加法通常没有副作用。
但:
toy.print %0 : i32
即使没有 Result:
也不能随便删
因为它有:
IO side effect
也就是打印行为。
这说明 Operation 除了:
Operand
Result
Type
Attribute
之外,还有:
Side Effects
这个概念。
15. 为什么副作用信息这么重要?
假设:
%0 = toy.add %a, %b : i32
toy.print %0 : i32
编译器不能说:
toy.print 没有 result
↓
那就删掉吧
因为删掉以后:
程序行为变了
所以:
Dead Code Elimination
需要知道:
这个 Operation 是否有副作用
这类信息可以通过 Trait / Interface 等机制表达。
后面我们会专门讲。
16. 我们下一步想 Lower 它
这套 Toy Dialect 的最终目标不是永远留着。
我们希望:
toy.const
↓
arith.constant
toy.add
↓
arith.addi
toy.mul
↓
arith.muli
toy.print
↓
func.call @printf
也就是:
Toy Dialect
↓
标准 Dialect
↓
LLVM Dialect
↓
LLVM IR
这就把我们前面学的:
ODS
Pattern
Pass
Dialect Conversion
全串起来了。
17. 先看 toy.add 的 Lowering
原始:
%0 = toy.add %a, %b : i32
Lower 后:
%0 = arith.addi %a, %b : i32
Pattern 概念上:
struct LowerAdd
: OpConversionPattern<toy::AddOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
toy::AddOp op,
OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<arith::AddIOp>(
op,
adaptor.getLhs(),
adaptor.getRhs());
return success();
}
};
你现在应该已经能看懂大半了。
18. 把前面的知识串起来
看到:
OpConversionPattern<toy::AddOp>
你知道:
这是 Dialect Conversion Pattern
看到:
adaptor.getLhs()
你知道:
这是转换后的 operand
看到:
replaceOpWithNewOp
你知道:
创建 arith.addi
+
替换 toy.add
看到:
return success();
你知道:
Pattern 转换成功
这说明前面几课已经开始连成体系了。
19. 一个小型完整 pipeline
以后我们最终希望跑:
输入 Toy IR
例如:
%0 = toy.const 10 : i32
%1 = toy.const 20 : i32
%2 = toy.add %0, %1 : i32
%3 = toy.mul %2, %1 : i32
toy.print %3 : i32
然后:
Toy Lowering Pass
变:
%0 = arith.constant 10 : i32
%1 = arith.constant 20 : i32
%2 = arith.addi %0, %1 : i32
%3 = arith.muli %2, %1 : i32
func.call @print_i32(%3) : (i32) -> ()
再继续:
arith / func
↓
LLVM Dialect
↓
LLVM IR
最终真正运行。
第 11 课总结
今天最重要的是:
我们第一次完整设计了一个自己的 Dialect:
toy
里面:
toy.const
toy.add
toy.mul
toy.print
并且每个 Operation 都要明确设计:
Operands
Results
Attributes
Types
Side Effects
ODS 的作用就是把这些结构声明下来。
然后:
ODS
↓
mlir-tblgen
↓
C++ Op 类
↓
Pass / Pattern / Lowering
你可以把整个 MLIR 开发流程开始记成:
1. 设计 IR
2. 用 ODS 定义 Dialect / Op
3. TableGen 生成代码
4. 写 Pass / Pattern
5. Lower 到标准 Dialect
6. Lower 到 LLVM
这一课最重要的一句话是:
写一个 Dialect,第一步不是写代码,而是设计“我要让 IR 表达什么”。
下一课我们继续 第 12 课:真正把 Toy Dialect 的文件结构、CMake、.td、生成代码串起来。
到那时会第一次看到一个比较接近真实 MLIR 工程的目录:
Toy/
├── CMakeLists.txt
├── ToyDialect.h
├── ToyDialect.cpp
├── ToyDialect.td
├── ToyOps.td
└── ...
然后我会带你理解每个文件是干什么的。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论