Skip to content

Commit

Permalink
HLIR examples
Browse files Browse the repository at this point in the history
  • Loading branch information
drernie committed Nov 29, 2024
1 parent 9ee1a81 commit dbfc150
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
File renamed without changes.
54 changes: 54 additions & 0 deletions doc/HLIR/example2-vec.hc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
; Import required dialects
. <- "func"
. <- "linalg"
. <- "arith"
. <- "scf"
. <- "affine"
. <- "memref"

; Matrix multiplication function
.f (.A <tensor<2x3xf32>>, .B <tensor<3x2xf32>>) -> <tensor<2x2xf32>> ^{
; Initialize result tensor
.init <tensor<2x2xf32>> [0.0];

; Matrix multiplication using linalg
.init linalg.matmul (A, B);

; Custom reduction operation with a block
.reduced example.reduce init ^{
(.x <f32>, .y <f32>) ^{
x + y
}
};

; Control flow using scf
.c0 <index> 0;
.c1 <index> 1;
.c2 <index> 2;

; For loop using scf
c0 -> c2 & { (.i <index>) ^{
.slice tensor.extract_slice (
init,
[i, c0],
[c1, c2],
[c1, c1]
) -> <tensor<1x2xf32>>;
}};

; Return result
init
}

; Custom operation with affine maps
.map affine (d0, d1) -> d0 + d1;

; Custom dialect operation block
.custom ^{
; Nested affine loops
0 -> 10 & { (.i <index>) ^{
0 -> 10 & { (.j <index>) ^{
.idx map(i, j);
}}
}}
}
37 changes: 37 additions & 0 deletions doc/HLIR/example2-vec.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Define a function that computes matrix multiplication
func.func @matrix_multiply(%A: tensor<2x3xf32>, %B: tensor<3x2xf32>) -> tensor<2x2xf32> {
// Initialize the result tensor with zeros
%init = arith.constant dense<0.0> : tensor<2x2xf32>

// Use linalg operation for matrix multiplication
%result = linalg.matmul ins(%A, %B: tensor<2x3xf32>, tensor<3x2xf32>)
outs(%init: tensor<2x2xf32>) -> tensor<2x2xf32>

// Example of a custom operation with a region
"example.reduce"(%result) ({
^bb0(%arg0: f32, %arg1: f32):
%sum = arith.addf %arg0, %arg1 : f32
"example.yield"(%sum) : (f32) -> ()
}) : (tensor<2x2xf32>) -> tensor<2x2xf32>

// Control flow example using scf dialect
scf.for %i = %c0 to %c2 step %c1 {
%slice = tensor.extract_slice %result[%i, 0] [1, 2] [1, 1] :
tensor<2x2xf32> to tensor<1x2xf32>
}

// Return the result
return %result : tensor<2x2xf32>
}

// Example of a custom dialect operation
#map = affine_map<(d0, d1) -> (d0 + d1)>

"custom.operation"() ({
// Affine loop example
affine.for %i = 0 to 10 {
affine.for %j = 0 to 10 {
%idx = affine.apply #map(%i, %j)
}
}
}) : () -> ()
11 changes: 11 additions & 0 deletions doc/HLIR/example3-mem.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
; Allocate a 2x2 buffer
.buffer memref.alloc() -> <memref<2x2xf32>>;

; Store some computed values
memref.store(1.0, buffer[0, 0]);
memref.store(2.0, buffer[0, 1]);

; Load a value and use it
.val memref.load(buffer[0, 0]);
.doubled val * 2.0;
memref.store(doubled, buffer[1, 0]);

0 comments on commit dbfc150

Please sign in to comment.