Last active
October 19, 2021 02:55
-
-
Save zhanghb97/0e14a77c7f97370acdadab83d6888e0b to your computer and use it in GitHub Desktop.
MLIR RVV Dialect Example
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
#include <iostream> | |
using namespace std; | |
// Define Memref Descriptor. | |
template<class T> | |
struct MemRef_descriptor_ { | |
T *allocated; | |
T *aligned; | |
intptr_t offset; | |
intptr_t sizes[1]; | |
intptr_t strides[1]; | |
}; | |
// Constructor | |
template<class T> | |
struct MemRef_descriptor_<T> *MemRef_Descriptor(T *allocated, T *aligned, | |
intptr_t offset, intptr_t sizes[1], intptr_t strides[1]) { | |
struct MemRef_descriptor_<T> *n = (struct MemRef_descriptor_<T> *)malloc(sizeof(*n)); | |
n->allocated = allocated; | |
n->aligned = aligned; | |
n->offset = offset; | |
for (int i = 0; i < 1; i++) | |
n->sizes[i] = sizes[i]; | |
for (int j = 0; j < 1; j++) | |
n->strides[j] = strides[j]; | |
return n; | |
} | |
// Define Mask Type. | |
typedef struct Mask_ { | |
int m1:1, m2:1, m3:1, m4:1, m5:1, m6:1; | |
} mask; | |
typedef mask *Mask; | |
typedef struct MemRef_descriptor_<long long> *MemRef_descriptor_i64; | |
typedef struct MemRef_descriptor_<mask> *MemRef_descriptor_i1; | |
// Declare the interface. | |
extern "C" { | |
void _mlir_ciface_vadd(MemRef_descriptor_i64 input1, int input2, | |
MemRef_descriptor_i64 output, MemRef_descriptor_i64 maskedoff, | |
MemRef_descriptor_i1 mask); | |
} | |
int main(int argc, char *argv[]) { | |
long long input1[6] = {2, 4, 6, 8, 10, 12}; | |
long long output[6] = {0, 0, 0, 0, 0, 0}; | |
long long maskedoff[6] = {99, 99, 99, 99, 99, 99}; | |
Mask_ t = {1, 0, 0, 0, 0, 1}; | |
long long *allocated_i64 = (long long *)malloc(1 * sizeof(long long)); | |
Mask allocated_i1 = (Mask)malloc(1 * sizeof(Mask)); | |
intptr_t sizes[1] = {1}; | |
intptr_t strides[1] = {1}; | |
MemRef_descriptor_i64 in1 = MemRef_Descriptor<long long>(allocated_i64, input1, 0, sizes, strides); | |
MemRef_descriptor_i64 out = MemRef_Descriptor<long long>(allocated_i64, output, 0, sizes, strides); | |
MemRef_descriptor_i64 off = MemRef_Descriptor<long long>(allocated_i64, maskedoff, 0, sizes, strides); | |
MemRef_descriptor_i1 msk = MemRef_Descriptor<mask>(allocated_i1, &t, 0, sizes, strides); | |
int in2 = 5; | |
_mlir_ciface_vadd(in1, in2, out, off, msk); | |
printf("[ "); | |
for (int i = 0; i < 6; ++i) { | |
printf("%lld ", out->aligned[i]); | |
} | |
printf("]\n"); | |
free(in1); | |
free(out); | |
free(off); | |
free(msk); | |
free(allocated_i64); | |
free(allocated_i1); | |
return 0; | |
} |
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
func @vadd(%in1: memref<?xi64>, %in2: i32, %out: memref<?xi64>, %maskedoff: memref<?xi64>, %mask: memref<?xi1>) { | |
%c0 = arith.constant 0 : index | |
%vta = arith.constant 1 : i64 | |
%vl = arith.constant 6 : i64 | |
%input1 = riscvv.load %in1[%c0], %vl : memref<?xi64>, !riscvv.vector<!riscvv.m4,i64>, i64 | |
%off = riscvv.load %maskedoff[%c0], %vl : memref<?xi64>, !riscvv.vector<!riscvv.m4,i64>, i64 | |
%msk = riscvv.load %mask[%c0], %vl : memref<?xi1>, !riscvv.vector<!riscvv.mask16,i1>, i64 | |
%output = riscvv.masked.add %off, %input1, %in2, %msk, %vl, %vta : !riscvv.vector<!riscvv.m4,i64>, i32, !riscvv.vector<!riscvv.mask16,i1>, i64 | |
riscvv.store %output, %out[%c0], %vl : !riscvv.vector<!riscvv.m4,i64>, memref<?xi64>, i64 | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment