Skip to content

Commit

Permalink
add pass
Browse files Browse the repository at this point in the history
  • Loading branch information
effrey-liu committed Oct 31, 2024
1 parent c452afe commit 06a9b78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 63 deletions.
36 changes: 4 additions & 32 deletions examples/BuddyNext/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -298,38 +298,10 @@ next-eliminate-identity-run:
${MLIR_CPU_RUNNER} ${OPT_FLAG} -e main -entry-point-result=void \
-shared-libs=${MLIR_RUNNER_UTILS} -shared-libs=${MLIR_C_RUNNER_UTILS}

next-const-add-simplify-run:
@${MLIR_OPT} ./next-const-add-simplify.mlir \
-pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | \
${MLIR_OPT} \
-arith-expand \
-eliminate-empty-tensors \
-empty-tensor-to-alloc-tensor \
-one-shot-bufferize \
-convert-linalg-to-affine-loops \
-affine-loop-fusion \
-lower-affine \
-func-bufferize \
-arith-bufferize \
-tensor-bufferize \
-buffer-deallocation \
-finalizing-bufferize \
-convert-vector-to-scf \
-expand-strided-metadata \
-convert-vector-to-llvm \
-memref-expand \
-arith-expand \
-convert-arith-to-llvm \
-finalize-memref-to-llvm \
-convert-scf-to-cf \
-convert-openmp-to-llvm \
-convert-arith-to-llvm \
-convert-math-to-llvm \
-convert-math-to-libm \
-convert-func-to-llvm \
-reconcile-unrealized-casts | \
${MLIR_CPU_RUNNER} ${OPT_FLAG} -e main -entry-point-result=void \
-shared-libs=${MLIR_RUNNER_UTILS} -shared-libs=${MLIR_C_RUNNER_UTILS}
next-const-add-simplify-lower:
@${BUDDY_OPT} next-const-add-simplify.mlir \
-simplify-tosa-add-reshape \
-o log.mlir



43 changes: 12 additions & 31 deletions examples/BuddyNext/next-const-add-simplify.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: buddy-opt %s \
// RUN: --simplify-tosa-add-reshape \
// RUN: | buddy-opt \
// RUN: -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" \
// RUN: | buddy-opt \
// RUN: -arith-expand \
Expand Down Expand Up @@ -36,16 +38,21 @@ module {
func.func private @printMemrefF32(tensor<*xf32>)
func.func private @rtclock() -> f64

func.func @const_add_original() {
%t0_original = call @rtclock() : () -> f64

func.func @const_add_reshape() -> tensor<32x40x128xf32> {
%0 = "tosa.const"() <{value = dense<3.5> : tensor<1x32x40x128xf32>}> : () -> tensor<1x32x40x128xf32>
%1 = "tosa.const"() <{value = dense<3.5> : tensor<1x32x40x128xf32>}> : () -> tensor<1x32x40x128xf32>
%2 = tosa.add %0, %1 : (tensor<1x32x40x128xf32>, tensor<1x32x40x128xf32>) -> tensor<1x32x40x128xf32>
%3 = tosa.reshape %2 {new_shape = array<i64: 32, 40, 128>} : (tensor<1x32x40x128xf32>) -> tensor<32x40x128xf32>


return %3 : tensor<32x40x128xf32>
}

func.func @main() {
%t0_original = call @rtclock() : () -> f64

%res = call @const_add_reshape() : () -> tensor<32x40x128xf32>
%t1_original = call @rtclock() : () -> f64
%tensor_unranked = tensor.cast %3 : tensor<32x40x128xf32> to tensor<*xf32>
%tensor_unranked = tensor.cast %res : tensor<32x40x128xf32> to tensor<*xf32>

// All the elements of the MemRef are the same,
// only check the first line to verify the correctness.
Expand All @@ -59,33 +66,7 @@ module {
// Print timings.

%t_original = arith.subf %t1_original, %t0_original : f64
vector.print str "original operation time: "
vector.print %t_original : f64
return
}

func.func @const_add_optimized() {
%t0_optimized = call @rtclock() : () -> f64

%0 = "tosa.const"() <{value = dense<7.000000e+00> : tensor<32x40x128xf32>}> : () -> tensor<32x40x128xf32>
%t1_optimized = call @rtclock() : () -> f64

%tensor_unranked = tensor.cast %0 : tensor<32x40x128xf32> to tensor<*xf32>

// Print results.
call @printMemrefF32(%tensor_unranked) : (tensor<*xf32>) -> ()
// Print timings.

%t_optimized = arith.subf %t1_optimized, %t0_optimized : f64
vector.print str "optimized operation time: "
vector.print %t_optimized : f64
return
}


func.func @main() {
call @const_add_original() : () -> ()
call @const_add_optimized() : () -> ()

return
}
Expand Down

0 comments on commit 06a9b78

Please sign in to comment.