Skip to content

Commit

Permalink
[VectorExp] Add GetVL and SetVL operations to VectorExp dialect.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghb97 committed Jan 16, 2024
1 parent bd23291 commit cb98a55
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/VectorExpDialect/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,7 @@ vector-exp-add-predication-asm:
${LLC} ${OPT_FLAG} -mtriple riscv64 -target-abi lp64d \
-mattr=+m,+d,+v -riscv-v-vector-bits-min=128 \
--filetype=asm -o log.s

vector-exp-dynamic-vector-dump:
@${BUDDY_OPT} ./vector-exp-dynamic-vector.mlir \
-o log.mlir
51 changes: 51 additions & 0 deletions examples/VectorExpDialect/vector-exp-dynamic-vector.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#map = affine_map<(d0)[s0, s1] -> (s0, -d0 + s1)>

func.func private @printMemrefI32(memref<*xi32>)

func.func @alloc_mem_i32(%init: i32) -> memref<?xi32> {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c20 = arith.constant 20 : index
%mem = memref.alloc(%c20) : memref<?xi32>
scf.for %idx0 = %c0 to %c20 step %c1 {
memref.store %init, %mem[%idx0] : memref<?xi32>
}
return %mem : memref<?xi32>
}

func.func @vector_add(%input1: memref<?xi32>, %input2: memref<?xi32>, %output: memref<?xi32>) {
%c0 = arith.constant 0 : index
// Get the dimension of the workload.
%dim_size = memref.dim %input1, %c0 : memref<?xi32>
// Perform dynamic vector addition.
// Returns four times the physical vl for element type i32.
%vl = vector_exp.get_vl i32, 4 : index

scf.for %idx = %c0 to %dim_size step %vl { // Tiling
%it_vl = affine.min #map(%idx)[%vl, %dim_size]
vector_exp.set_vl %it_vl : index {
%vec_input1 = vector.load %input1[%idx] : memref<?xi32>, vector<[1]xi32> // vector<?xi32>
%vec_input2 = vector.load %input2[%idx] : memref<?xi32>, vector<[1]xi32> // vector<?xi32>
%vec_output = arith.addi %vec_input1, %vec_input2 : vector<[1]xi32> // vector<?xi32>
vector.store %vec_output, %output[%idx] : memref<?xi32>, vector<[1]xi32> // vector<?xi32>
vector.yield
}
}
return
}

func.func @main() -> i32 {
%c0_i32 = arith.constant 0 : i32
%c1_i32 = arith.constant 1 : i32

%input_mem = call @alloc_mem_i32(%c1_i32) : (i32) -> memref<?xi32>
%result_mem = call @alloc_mem_i32(%c0_i32) : (i32) -> memref<?xi32>

call @vector_add(%input_mem, %input_mem, %result_mem) : (memref<?xi32>, memref<?xi32>, memref<?xi32>) -> ()

%print_result_mem = memref.cast %result_mem : memref<?xi32> to memref<*xi32>
call @printMemrefI32(%print_result_mem) : (memref<*xi32>) -> ()

%ret = arith.constant 0 : i32
return %ret : i32
}
26 changes: 26 additions & 0 deletions midend/include/Dialect/VectorExp/VectorExpOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ include "VectorExpDialect.td"
include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td"

include "mlir/IR/AttrTypeBase.td"

//===----------------------------------------------------------------------===//
// Vector Predication Operation
//===----------------------------------------------------------------------===//
Expand All @@ -41,6 +43,30 @@ def VectorExp_PredicationOp : VectorExp_Op<"predication"> {
"$region `:` type($result)";
}

//===----------------------------------------------------------------------===//
// Vector GetVL Operation
//===----------------------------------------------------------------------===//

def VectorExp_GetVLOp : VectorExp_Op<"get_vl"> {
let summary = "Vector Experiment GetVL Operation.";
let arguments = (ins TypeAttr:$dtype, IndexAttr:$lmul);
let results = (outs Index:$result);
let assemblyFormat = "$dtype `,` $lmul attr-dict `:` type($result)";
}

//===----------------------------------------------------------------------===//
// Vector SetVL Operation
//===----------------------------------------------------------------------===//

def VectorExp_SetVLOp : VectorExp_Op<"set_vl"> {
let summary = "Vector Experiment SetVL Operation.";
let arguments = (ins Index:$vl);
// TODO: Add optional returns.
// let results = (outs AnyType:$result);
let regions = (region AnyRegion:$region);
let assemblyFormat = "$vl attr-dict `:` type($vl) $region";
}

//===----------------------------------------------------------------------===//
// Vector Load Operation with Dynamic Length
//===----------------------------------------------------------------------===//
Expand Down

0 comments on commit cb98a55

Please sign in to comment.