TSEL

指令示意图

TSEL tile operation

简介

使用掩码 Tile 在两个 Tile 之间进行选择(逐元素选择)。

数学语义

对每个元素 (i, j) 在有效区域内:

\[ \mathrm{dst}_{i,j} = \begin{cases} \mathrm{src0}_{i,j} & \text{if } \mathrm{mask}_{i,j}\ \text{is true} \\ \mathrm{src1}_{i,j} & \text{otherwise} \end{cases} \]

汇编语法

PTO-AS 形式:参见 PTO-AS 规范

同步形式:

%dst = tsel %mask, %src0, %src1 : !pto.tile<...>

AS Level 1(SSA)

%dst = pto.tsel %mask, %src0, %src1 : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

AS Level 2(DPS)

pto.tsel ins(%mask, %src0, %src1 : !pto.tile_buf<...>, !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%dst : !pto.tile_buf<...>)

C++ 内建接口

声明于 include/pto/common/pto_instr.hpp

template <typename TileData, typename MaskTile, typename TmpTile, typename... WaitEvents>
PTO_INST RecordEvent TSEL(TileData &dst, MaskTile &selMask, TileData &src0, TileData &src1, TmpTile &tmp, WaitEvents &... events);

约束

  • 实现检查 (A2A3):
    • sizeof(TileData::DType) 必须是 24 字节。
    • TileData::DType 必须是 int16_tuint16_tint32_tuint32_thalfbfloat16_tfloat
    • 掩码 tile 类型/形状不强制执行显式断言;掩码编码由目标定义。
    • 实现使用 dst.GetValidRow() / dst.GetValidCol() 作为选择域。
  • 实现检查 (A5):
    • sizeof(TileData::DType) 必须是 24 字节。
    • TileData::DType 必须是 int16_tuint16_tint32_tuint32_thalfbfloat16_tfloat
    • TSEL_IMPL 不强制执行显式的 static_assert/PTO_ASSERT 检查。
    • 实现使用 dst.GetValidRow() / dst.GetValidCol() 作为选择域。
  • 掩码编码:
    • 掩码 tile 被解释为目标定义布局中的打包谓词位。

示例

自动(Auto)

#include <pto/pto-inst.hpp>

using namespace pto;

void example_auto() {
  using TileT = Tile<TileType::Vec, float, 16, 16>;
  using MaskT = Tile<TileType::Vec, uint8_t, 16, 32, BLayout::RowMajor, -1, -1>;
  TileT src0, src1, dst;
  MaskT mask(16, 2);
  TSEL(dst, mask, src0, src1);
}

手动(Manual)

#include <pto/pto-inst.hpp>

using namespace pto;

void example_manual() {
  using TileT = Tile<TileType::Vec, float, 16, 16>;
  using MaskT = Tile<TileType::Vec, uint8_t, 16, 32, BLayout::RowMajor, -1, -1>;
  TileT src0, src1, dst;
  MaskT mask(16, 2);
  TASSIGN(src0, 0x1000);
  TASSIGN(src1, 0x2000);
  TASSIGN(dst,  0x3000);
  TASSIGN(mask, 0x4000);
  TSEL(dst, mask, src0, src1);
}

汇编示例(ASM)

自动模式

# 自动模式:由编译器/运行时负责资源放置与调度。
%dst = pto.tsel %mask, %src0, %src1 : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

手动模式

# 手动模式:先显式绑定资源,再发射指令。
# 可选(当该指令包含 tile 操作数时):
# pto.tassign %arg0, @tile(0x1000)
# pto.tassign %arg1, @tile(0x2000)
%dst = pto.tsel %mask, %src0, %src1 : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

PTO 汇编形式

%dst = tsel %mask, %src0, %src1 : !pto.tile<...>
# AS Level 2 (DPS)
pto.tsel ins(%mask, %src0, %src1 : !pto.tile_buf<...>, !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%dst : !pto.tile_buf<...>)