This is the minimal example to look into the way to implement the hello-world kind of program with MLIR. The basic code structure is borrowed from standalone and Toy language in LLVM project.
We need to build our own MLIR in the local machine in advance. Please follow the build instruction for MLIR here.
Please make sure to build LLVM project first according to the instruction.
mkdir build && cd build
cmake -G Ninja .. -DLLVM_DIR=/path/to/llvm-project/build/lib/cmake/llvm \
-DMLIR_DIR=/path/to/llvm-project/build/lib/cmake/mlir
cmake --build . --target hello-opt
To run the test, check-hello
target will be usable.
To build the documentation from the TableGen description of the dialect operations, run
cmake --build . --target mlir-doc
hello-opt
will lower the MLIR into the bytecode of LLVM.
# Lower MLIR to LLVM IR
$ ./build/bin/hello-opt ./test/Hello/print.mlir > /path/to/print.ll
# Execute the code with LLVM interpreter
$ lli /path/to/print.ll
1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
constant
Constant operation turns a literal into an SSA value. The data is attached to the operation as an attribute. For example:
%0 = "hello.constant"()
{ value = dense<[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]> : tensor<2x3xf64> }
: () -> tensor<2x3xf64>
Interfaces: NoSideEffect (MemoryEffectOpInterface)
Effects: MemoryEffects::Effect{}
Attribute | MLIR Type | Description |
---|---|---|
value |
::mlir::DenseElementsAttr | 64-bit float elements attribute |
Result | Description |
---|---|
«unnamed» | tensor of 64-bit float values |
print operation
Syntax:
operation ::= `hello.print` $input attr-dict `:` type($input)
The "print" builtin operation prints a given input tensor, and produces no results.
Interfaces: NoSideEffect (MemoryEffectOpInterface)
Effects: MemoryEffects::Effect{}
Operand | Description |
---|---|
input |
tensor of 64-bit float values or memref of 64-bit float values |