第 2 课:Operation、Value、Operand、Result、Type、Attribute

好,继续第 2 课。

这节我们专门把一段最简单的 MLIR 拆开,认识几个最核心的组成部分:

func.func @add(%a: i32, %b: i32) -> i32 {
  %0 = arith.addi %a, %b : i32
  return %0 : i32
}

第 2 课:Operation、Value、Operand、Result、Type、Attribute

先别急着记定义,我们从这一行开始:

%0 = arith.addi %a, %b : i32

它大概对应:

int result = a + b;

但在 MLIR 里,它可以拆成:

%0 = arith.addi %a, %b : i32
│        │       │  │     │
│        │       │  │     └── Type
│        │       │  └──────── Operand
│        │       └─────────── Operand
│        └─────────────────── Operation
└──────────────────────────── Result

先记住一个总结构:

Operation
├── Operands
├── Results
├── Attributes
├── Regions
└── Type information

以后绝大多数 MLIR,都可以这么拆。


1. Operation

MLIR 最核心的概念就是:

Operation

比如:

arith.addi

这是一个 Operation。

再比如:

arith.muli
func.call
memref.load
scf.for

全部都是 Operation。

所以:

%0 = arith.addi %a, %b : i32

真正执行的动作是:

arith.addi

你可以暂时把 Operation 理解为:

MLIR 里的“动作节点”。

比如:

加法
乘法
函数调用
加载内存
循环
条件判断

都可以是 Operation。


2. Operand

看这里:

arith.addi %a, %b

arith.addi 需要两个输入:

%a
%b

这两个输入就叫:

Operands

也就是:

Operation 的输入

因此:

arith.addi
   │
   ├── operand 0 = %a
   └── operand 1 = %b

可以画成:

%a ─────┐
        │
        ▼
     arith.addi
        ▲
        │
%b ─────┘

3. Result

Operation 执行之后可能产生结果。

例如:

%0 = arith.addi %a, %b : i32

这里:

%0

就是 Operation 的:

Result

因此:

             arith.addi
             /        \
           %a          %b
               │
               ▼
              %0

或者从数据流角度看:

%a ───┐
      ├──> arith.addi ───> %0
%b ───┘

4. Value

现在有个特别重要的问题:

%a
%b
%0

它们到底是什么?

它们统称:

Value

MLIR 中:

Operand 是 Value
Result 也是 Value
Block Argument 也是 Value

所以你可以理解成:

Value = MLIR 中流动的数据

例如:

%0 = arith.addi %a, %b : i32
%1 = arith.muli %0, %b : i32

数据流就是:

        %a
         \
          \
        arith.addi
          /
        %b
         |
         v
        %0
         \
          \
        arith.muli
          /
        %b
         |
         v
        %1

因此:

%a
%b
%0
%1

都是 Value。


一个特别重要的关系

你可以这样记:

Operation 消费 Value
Operation 产生 Value

例如:

%a ──┐
     ├── arith.addi ──> %0
%b ──┘

也就是说:

%a、%b

是输入 Value,

而:

%0

是输出 Value。

这就是 MLIR 的数据流模型。


5. SSA

看到这里,你可能已经发现:

%0
%1
%2

这种写法跟 LLVM IR 很像。

这是因为 MLIR 也采用:

SSA

即:

Static Single Assignment
静态单赋值

意思是:

一个 Value 只被定义一次。

比如:

%0 = arith.addi %a, %b : i32

之后不能再写:

%0 = arith.muli %a, %b : i32

因为 %0 已经被定义了。

正确方式是:

%0 = arith.addi %a, %b : i32
%1 = arith.muli %a, %b : i32

这个理念和 LLVM SSA 是一致的。

所以你之前学过 LLVM 的话,这部分会比较容易。


6. Type

继续看:

%0 = arith.addi %a, %b : i32

最后:

i32

表示:

Type

也就是类型。

比如:

i1
i8
i32
i64
f32
f64

MLIR 里还会出现一些 LLVM IR 里不太常见的高级类型,比如:

tensor<2x3xf32>

表示:

2 × 3 的 f32 tensor

例如:

tensor<
  2 x 3 x f32
>

又比如:

memref<10xi32>

表示:

包含 10 个 i32 的内存引用

所以 MLIR 的 Type 系统比 LLVM IR 更灵活。

这也是因为 MLIR 要支持高层 IR。


7. Attribute

Attribute 很容易和 Value 搞混。

来看一个例子:

%0 = arith.constant 42 : i32

这里:

42

不是 Operand。

它属于:

Attribute

为什么?

因为:

42

不是运行时产生的数据,而是:

编译期就知道的固定信息

这就是 Attribute。

我们可以先这样区分:

Value
= 运行时数据

Attribute
= 编译期常量 / 元数据

比如:

arith.constant 42

42 是 Attribute。

再例如:

arith.cmpi slt, %a, %b : i32

这里:

slt

也是某种 Attribute。

它表达:

signed less than
有符号小于

Value 和 Attribute 的区别

这是今天非常重要的一个点。

比如:

%0 = arith.constant 42 : i32
%1 = arith.addi %0, %a : i32

这里:

42

是 Attribute。

而:

%0

是 Value。

虽然它们都表示“42”,但角色不同。

可以理解成:

42
 │
 │ attribute
 ▼
arith.constant
 │
 ▼
%0
 │
 │ value
 ▼
arith.addi

也就是:

Attribute
    ↓
Operation
    ↓
Value

再看完整例子

现在重新看:

func.func @add(%a: i32, %b: i32) -> i32 {
  %0 = arith.addi %a, %b : i32
  return %0 : i32
}

我们已经可以拆出很多东西了。

函数参数:

%a: i32
%b: i32

这里:

%a、%b = Value
i32     = Type

加法:

%0 = arith.addi %a, %b : i32

这里:

arith.addi = Operation

%a
%b         = Operands

%0         = Result

%a
%b
%0         = Values

i32        = Type

最后:

return %0 : i32

其实 return 自己也是一个:

Operation

只是它:

有 Operand
没有 Result

也就是:

return
 └── operand = %0

Operation 不一定有 Result

比如:

return %0 : i32

没有输出。

又比如:

memref.store %value, %mem[%index]

它做的是写内存。

它也不一定返回 Value。

所以:

Operation 可以有:
0 个 Result
1 个 Result
多个 Result

这是 MLIR 很重要的一点。

甚至一个 Operation 可以返回多个值。

例如概念上:

%x, %y = mydialect.foo ...

所以 MLIR 的 Operation 模型比普通“CPU 指令”更一般。


用 C 代码对照一下

假设:

int add(int a, int b) {
    int c = a + b;
    return c;
}

MLIR:

func.func @add(%a: i32, %b: i32) -> i32 {
  %c = arith.addi %a, %b : i32
  return %c : i32
}

对应关系:

C               MLIR

a               %a
b               %b

a + b           arith.addi

c               %c

int             i32

return c        return %c

但有一个关键区别:

C 里的:

c

通常理解为变量。

而 MLIR 的:

%c

最好不要理解成普通变量。

它应该理解为:

某个 Operation 产生出来的一个 SSA Value。

这个区别后面非常重要。


今天建立一个核心模型

以后看到:

%result = some.operation %input1, %input2

你脑子里应该自动出现:

            Operation
          some.operation
          /            \
     operand          operand
       %input1         %input2
            \          /
             \        /
              result
             %result

因此整个 MLIR 程序可以理解成一张:

数据流图

例如:

%0 = arith.addi %a, %b : i32
%1 = arith.muli %0, %c : i32
%2 = arith.subi %1, %d : i32

其实就是:

%a ──┐
     ├─> addi ──> %0 ──┐
%b ──┘                  │
                        ├─> muli ──> %1 ──┐
%c ─────────────────────┘                 │
                                          ├─> subi ──> %2
%d ───────────────────────────────────────┘

如果能看懂这张图,你已经开始真正理解 MLIR 了。


第 2 课总结

今天先牢牢记住这 6 个词:

Operation
    动作

Operand
    Operation 的输入

Result
    Operation 的输出

Value
    在 Operation 之间流动的数据

Type
    Value 的类型

Attribute
    编译期已知的固定信息

以及最重要的一句话:

Operation 消费 Value,并产生新的 Value。

下一课我们进入一个 MLIR 中非常关键、也比 LLVM 更有特色的概念:

Block 和 Region。

到时候你会真正理解:

func.func @add(...) {
    ...
}

为什么函数体的大括号不是简单的“代码块”,以及 scf.if、scf.for 为什么能直接在 Operation 内部嵌套代码。

评论

暂无评论