Transform Dialect: how to use Transform-LoopUnrollOp and SCF-ForOp to implement Clang’s #pragma unroll

I want to use LoopUnrollOp and ForOp to implement Clang’s #pragma unroll, but I can’t understand the usage of LoopUnrollOp. Can anyone give me some examples or some suggestions

There are some examples in mlir/test/Dialect/SCF/transform-ops.mlir that look like this:

func.func @loop_unroll_op() {
  %c0 = arith.constant 0 : index
  %c42 = arith.constant 42 : index
  %c5 = arith.constant 5 : index
  scf.for %i = %c0 to %c42 step %c5 {
    arith.addi %i, %i : index
  }
  return
}

module attributes {transform.with_named_sequence} {
  transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
    %0 = transform.structured.match ops{["arith.addi"]} in %arg1 : (!transform.any_op) -> !transform.any_op
    %1 = transform.get_parent_op %0 {op_name = "scf.for"} : (!transform.any_op) -> !transform.op<"scf.for">
    transform.loop.unroll %1 { factor = 4 } : !transform.op<"scf.for">
    transform.yield
  }
}

and would be “run” like this: mlir-opt test.mlir -transform-interpreter (where test.mlir contains the above).

1 Like

Do you know how to implement it in C++?

Transform isn’t meant to be used/called from C++ - it’s actually the other way around: transform is a convenient way to avoid writing C++.

Ideally every transform op is pretty much a call to a C++ helper. So from C++ you’d just call that helper, which in this case you can see here llvm-project/mlir/lib/Dialect/SCF/TransformOps/SCFTransformOps.cpp at a25da1a92120eb5cb74f1a3d28a4849178cfbdff · llvm/llvm-project · GitHub .

2 Likes