- Static shapes only — all tensor dimensions are compile-time constants. MLIR types like
tensor<3x256x256xf32>. No?/ dynamic dims initially. - Graph-level auto-optimization — users describe the computation graph; MLIR built-in passes (Linalg, Affine, SCF) handle tiling, fusion, and vectorization. No manual compute/schedule separation.
- C/Rust-style infix syntax — similar to the original Colgm, but computation statements redesigned for tensor DSL.
- Lexer borrowed from Colgm bootstrap (C++) — token kinds trimmed and keywords adjusted for the tensor domain.
- Target backend — MLIR → LLVM IR (via the existing
-convert-scf-to-cf,-convert-to-llvmpipeline).
- Set up CMake build (MLIR dependency, LLVM find_package)
- Bring up minimal
main.cppthat links against MLIR and prints the MLIR version - Choose the LLVM/MLIR version to track (llvm-project revision)
- Port Colgm bootstrap lexer (
lexer.cpp,lexer.h) into the project - Slash the token enum down to what this DSL needs
- Keywords:
func,var,return,if,else,for,struct,pub,extern,true,false - Domain keywords:
tensor,f32,f64,i32,i64(dtype keywords) - Literals: integer, float, string, char
- Operators:
+,-,*,/,%,==,!=,<,>,<=,>=,=,->,(,),{,},[,],,,;,.,:
- Keywords:
- Write smoke tests for the lexer (tokenize a few example
.toy/.dslfiles)
- Define the AST node hierarchy (similar to Colgm's
ast_kinddiscriminated-union pattern, or a class hierarchy)- Declarations:
func_decl,struct_decl - Statements:
var_decl,assign,if_stmt,for_stmt,return_stmt,expr_stmt,block - Expressions:
binary,unary,call,tensor_literal,identifier,number_literal,float_literal,index_access
- Declarations:
- Each AST node carries a
spanfor error reporting - AST dumper / pretty-printer for debugging
- Recursive-descent parser (hand-written, following Colgm's parser / MLIR Toy parser pattern)
- Parse function declarations:
func name(params...) -> ret_type { body } - Parse variable declarations:
var name = expr;orvar name: type = expr; - Parse tensor type annotations:
f32[2, 3],i32[4],f64[2, 2, 2] - Parse expressions with precedence climbing (Pratt parser or layered recursive descent)
- Parse tensor literals:
[[1.0, 2.0], [3.0, 4.0]] - Parse control flow:
if,for - Parse tensor literal syntax:
[[1,2],[3,4]] - Error recovery strategy (at minimum: report the first error and exit)
- Write TableGen files for the
tensor_ops(ortoy2) dialect- ops:
AddOp,SubOp,MulOp,DivOp,NegOp,ReluOp,ReshapeOp - Each op has a custom assembly format and a verifier
- ops:
- Define the
ToyTensorTypewrapper (or reuse MLIR's built-inTensorType) - Write the
funcoperation for the dialect (or reuse the built-infunc.func) - Register the dialect with MLIR
- Write
MLIRGenvisitor class (analogous to Toy tutorial'sMLIRGen) - Expression lowering: each AST expression node → MLIR
Value(via op builder) - Statement lowering: var declarations →
colgm.constant+ SSA values - Control flow lowering:
if/for→colgm.if/colgm.for - Function lowering:
func_decl→func.funcwith body region - Shape propagation: infer result tensor shapes from input shapes
- Canonicalization patterns for each op (e.g.,
AddOp(x, 0) → x, constant folding) - Inline pass (inline small functions)
- Run the standard MLIR lowering pipeline:
-canonicalize,-cse-convert-tensor-to-linalg(or custom lowering)-convert-linalg-to-affine-loops,-lower-affine-convert-scf-to-cf,-convert-to-llvm
- Wire the
mlir-translate/ JIT pipeline - Emit a
.llfile or JIT-compile and run - Provide a CLI:
colgm-mlir <file.dsl> [-o output.ll] [--run] [--dump-mlir]
-
add— element-wise tensor addition (broadcasting not required yet) -
sub— element-wise tensor subtraction -
mul— element-wise tensor multiplication -
div— element-wise tensor division -
neg— element-wise negation -
relu—max(0, x)element-wise -
sigmoid—1 / (1 + exp(-x))element-wise -
tanh— element-wise hyperbolic tangent -
exp— element-wise exponential -
log— element-wise natural logarithm -
sqrt— element-wise square root -
abs— element-wise absolute value -
reshape— reshape tensor to new dimensions (total elements must match) -
transpose— permute tensor axes
-
sum— reduce along specified axis (or all axes) -
mean— reduce along specified axis -
max— reduce along specified axis -
min— reduce along specified axis -
matmul— matrix multiplication (2-D only initially) -
broadcast— broadcast a tensor to a larger shape -
reduce_sum— reduce along specified axis
-
conv2d— 2-D convolution -
max_pool2d/avg_pool2d— 2-D pooling -
gemm— general matrix multiply (with optional transpose flags)
- Better error messages (point to source location, show context)
- Test suite: run each test case through the full pipeline and compare output values
- CI (GitHub Actions) with a pinned LLVM build
- Editor syntax highlighting (VSCode extension)