Triton Linalg - WrapFuncBodyWithSingleBlockPass

功能

WrapFuncBodyWithSingleBlockPass用于将函数体中有多个Block的Function,用scf.execute_region封起来,让Function变为单Block的形式。

示例:

转换前:

 tt.func @foo(%arg0: i1, %arg1: i32, %arg2: i32) -> i32 {
     cf.cond_br %arg0, ^bb1, ^bb2
   ^bb1:
     tt.return %arg1: i32
   ^bb2:
     tt.return %arg2: i32
 }

转换后:

tt.func @foo(%arg0: i1, %arg1: i32, %arg2: i32) -> i32 {
   %0 = scf.execute_region -> i32 {
       cf.cond_br %arg0, ^bb1, ^bb2
     ^bb1:
       cb.br ^bb3(%arg1 : i32)
     ^bb2:
       cb.br ^bb3(%arg2 : i32)
     ^bb3(%1 : i32):
       scf.yield %1 : i32
     }
     tt.return %0 : i32
   }

WrapFuncBodyWithSingleBlockPass简化了Function的控制流,解决了在scf.for 循环中内联函数调用时由于多块(multi-block)问题导致的内联问题。

如对foo调用的代码:

scf.for %iv = %0 to %10 step %1 {
  %result = call @foo(%arg0, %arg1, %iv) : (i1, i32, i32) -> i32
}

假如inline优化需要把foo做内联,则原始的多Block的foo会导致生成的IR不正确或难以优化。

实现

Pass.td

WrapFuncBodyWithSingleBlock的td部分实现在triton-linalg/triton-linalg/include/triton-linalg/Dialect/Triton/Transforms/Passes.td:

  • WrapFuncBodyWithSingleBlock是一个ModuleOp范围的Pass。
  • constructor为createWrapFuncBodyWithSingleBlockPass。
def WrapFuncBodyWithSingleBlock : Pass<"wrap-func-body-with-single-block", "::mlir::ModuleOp"> {
  let summary = "Wrap function body with single block";
  let description = [{
    This pass wraps function body into a block by moving body to a `scf.execute_region`.
  }];

  let constructor = "mlir::triton::createWrapFuncBodyWithSingleBlockPass()";

  let dependentDialects = ["triton::TritonDialect", "scf::SCFDialect"];
}

Pass.cpp

WrapFuncBodyWithSingleBlock的cpp部分实现在:triton-linalg/triton-linalg/lib/Dialect/Triton/Transforms/WrapFuncBodyWithSingleBlock.cpp

runOnOperation

runOnOperation实现了对Modul

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值