kumasento / polymer

Bridging polyhedral analysis tools to the MLIR framework
MIT License
99 stars 21 forks source link

[FoldSCFIf] added -fold-scf-if pass #105

Closed kumasento closed 2 years ago

kumasento commented 2 years ago

Examples:

Without memref store

func @foo(%a: f32, %b: f32, %c: i1) -> f32 {
  %0 = scf.if %c -> f32 {
    %1 = arith.addf %a, %b : f32
    scf.yield %1 : f32
  } else {
    %1 = arith.mulf %a, %b : f32
    scf.yield %1 : f32
  }
  return %0 : f32
}

will be transformed to

func @foo(%arg0: f32, %arg1: f32, %arg2: i1) -> f32 {
  %0 = arith.addf %arg0, %arg1 : f32
  %1 = arith.mulf %arg0, %arg1 : f32
  %2 = select %arg2, %0, %1 : f32
  return %2 : f32
}

With memref store

func @foo(%A: memref<10xf32>, %a: f32, %cond: i1) {
  scf.if %cond {
    affine.store %a, %A[0] : memref<10xf32>
  }
  return
}

will be transformed to

func @foo(%arg0: memref<10xf32>, %arg1: f32, %arg2: i1) {
  %0 = affine.load %arg0[0] : memref<10xf32>
  %1 = select %arg2, %arg1, %0 : f32
  affine.store %1, %arg0[0] : memref<10xf32>
  return
}