-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}} | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) : () -> () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |