picceler
A compiled DSL for image processing, built on MLIR
Loading...
Searching...
No Matches
mlir_gen.h
Go to the documentation of this file.
1#pragma once
2
3#include <mlir/IR/MLIRContext.h>
4#include <unordered_map>
5#include <string>
6#include <vector>
7
8#include "mlir/IR/Builders.h"
9#include "mlir/IR/BuiltinOps.h"
10#include "mlir/IR/Dialect.h"
11#include "mlir/Pass/PassManager.h"
12
13#include "dialect.h"
14#include "types.h"
15#include "parser.h"
16
17namespace picceler {
18
23class MLIRGen {
24public:
25 using GeneratorFunction = std::function<mlir::Value(mlir::Location, const std::vector<mlir::Value> &)>;
26 using VariableTable = std::unordered_map<std::string, mlir::Value>;
27 using NamedVariableTable = std::pair<std::string, VariableTable>;
28
29public:
30 MLIRGen() = delete;
31 MLIRGen(mlir::MLIRContext *context);
32 MLIRGen(const MLIRGen &) = default;
33 MLIRGen &operator=(const MLIRGen &) = default;
34 MLIRGen(MLIRGen &&) = default;
35 MLIRGen &operator=(MLIRGen &&) = default;
36 ~MLIRGen() noexcept;
37
44 mlir::ModuleOp generate(ModuleNode *root, const std::string &sourceFile);
45
46private:
52 void declareUserFunctions(mlir::ModuleOp module, ModuleNode *root);
53
59 void defineUserFunctions(mlir::ModuleOp module, ModuleNode *root);
60
66 std::vector<mlir::Type> getFunctionArgTypes(FunctionNode *funcNode);
67
73 mlir::Type getMLIRType(const std::string &typeName);
74
80 Result<mlir::Value> lookupVariable(const std::string &name) const;
81
87 void declareVariable(const std::string &name, mlir::Value value);
88
93 void enterScope(const std::string &name);
94
98 void exitScope() noexcept;
99
104 void emitStatement(ASTNode *node);
105
110 mlir::Value emitKernel(KernelNode *node);
111 mlir::Value emitExpression(ASTNode *node);
112 mlir::Value emitAssignment(AssignmentNode *node);
113 mlir::Value emitCall(CallNode *node);
114 mlir::Value emitVariable(VariableNode *node);
115 mlir::Value emitString(StringNode *node);
116 mlir::Value emitNumber(NumberNode *node);
117 mlir::Value emitBinaryOp(BinaryOpNode *node);
118 void emitIf(IfNode *node);
119 void emitFor(ForNode *node);
120 void emitReturn(ReturnNode *node);
121 mlir::Value emitCallExpression(CallNode *node, const std::vector<mlir::Value> &args);
122
126
130 void registerBuiltinFunctions();
131
132private:
133 mlir::MLIRContext *_context;
134 mlir::OpBuilder _builder;
135 std::vector<NamedVariableTable> _scopedVariableTable;
136 std::unordered_map<std::string, GeneratorFunction> _functionTable;
137 std::string _sourceFile;
138};
139
140} // namespace picceler
Abstract Syntax Tree (AST) node base class.
Definition ast.h:20
AST node for assignment statements.
Definition ast.h:140
AST node for binary operations (arithmetic and relational).
Definition ast.h:194
AST node for function calls.
Definition ast.h:158
AST node for "for" statements.
Definition ast.h:235
AST node for function definitions.
Definition ast.h:63
AST node for if conditional statements.
Definition ast.h:214
AST node for kernel definitions.
Definition ast.h:178
std::pair< std::string, VariableTable > NamedVariableTable
Definition mlir_gen.h:27
MLIRGen & operator=(const MLIRGen &)=default
MLIRGen(const MLIRGen &)=default
~MLIRGen() noexcept
Definition mlir_gen.cpp:116
std::function< mlir::Value(mlir::Location, const std::vector< mlir::Value > &)> GeneratorFunction
Definition mlir_gen.h:25
mlir::ModuleOp generate(ModuleNode *root, const std::string &sourceFile)
Generates MLIR code from the given AST root node.
Definition mlir_gen.cpp:230
MLIRGen(MLIRGen &&)=default
MLIRGen & operator=(MLIRGen &&)=default
std::unordered_map< std::string, mlir::Value > VariableTable
Definition mlir_gen.h:26
AST node for the entire module.
Definition ast.h:43
AST node for numeric literals.
Definition ast.h:125
AST node for "return" statements.
Definition ast.h:261
AST node for string literals.
Definition ast.h:110
AST node for variable references.
Definition ast.h:92
Picceler MLIR dialect declarations.
Definition ast.h:11
std::expected< T, CompileError > Result
A type alias for the result of a compilation operation.
Definition error.h:59
Picceler MLIR type declarations, it includes piccelerTypes.h.inc, which is generated by MLIR TableGen...