Fix the fn slir generation proccess
This commit is contained in:
parent
00869e6c8d
commit
dc051797ee
|
@ -377,4 +377,28 @@ define i64 @main1(i64 %0, i64 %1, i64 %2) !dbg !9 {
|
|||
- https://mlir.llvm.org/docs/LangRef
|
||||
- https://en.wikipedia.org/wiki/Basic_block
|
||||
|
||||
* Episode 9 - Code Generation
|
||||
* Episode 9 - IR (SLIR) generation
|
||||
** Updates:
|
||||
- Source manager
|
||||
- Diagnostic Engine
|
||||
- JIT
|
||||
|
||||
There will be an episode dedicated to eache of these
|
||||
** How does IR generation works
|
||||
- Pass around MLIR context
|
||||
- Create Builder objects that creates operations in specific
|
||||
locations
|
||||
- ModuleOp
|
||||
- Namespace
|
||||
** How to define a new dialect
|
||||
- Pure C++
|
||||
- Tablegen
|
||||
** SLIR
|
||||
*** The SLIR goal
|
||||
- An IR that follows the AST
|
||||
- Rename?
|
||||
*** Steps
|
||||
- [X] Define the new dialect
|
||||
- [X] Setup the tablegen
|
||||
- [X] Define the operations
|
||||
- [X] Walk the AST and generate the operations
|
||||
|
|
|
@ -22,6 +22,21 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Commentary:
|
||||
* `Reader` is the base parser class and accepts a buffer like objenct (usually
|
||||
* `llvm::StringRef`) as the input and parsess it to create an AST (look at the
|
||||
* `serene::exprs::Expression` class).
|
||||
*
|
||||
* The parsing algorithm is quite simple and it is a LL(2). It means that, we
|
||||
* start parsing the input from the very first character and parse the input
|
||||
* one char at a time till we reach the end of the input. Please note that
|
||||
* when we call the `advance` function to move forward in the buffer, we
|
||||
* can't go back. In order to look ahead in the buffer without moving in the
|
||||
* buffer we use the `nextChar` method-
|
||||
|
||||
*/
|
||||
|
||||
#ifndef READER_H
|
||||
#define READER_H
|
||||
|
||||
|
|
|
@ -24,7 +24,6 @@
|
|||
|
||||
#include "serene/exprs/fn.h"
|
||||
|
||||
#include "mlir/IR/BuiltinAttributes.h"
|
||||
#include "serene/errors/error.h"
|
||||
#include "serene/exprs/expression.h"
|
||||
#include "serene/exprs/list.h"
|
||||
|
@ -37,6 +36,7 @@
|
|||
#include <llvm/Support/Casting.h>
|
||||
#include <llvm/Support/FormatVariadic.h>
|
||||
#include <mlir/IR/Block.h>
|
||||
#include <mlir/IR/BuiltinAttributes.h>
|
||||
|
||||
namespace serene {
|
||||
namespace exprs {
|
||||
|
@ -106,8 +106,8 @@ MaybeNode Fn::make(SereneContext &ctx, List *list) {
|
|||
};
|
||||
|
||||
void Fn::generateIR(serene::Namespace &ns, mlir::ModuleOp &m) {
|
||||
auto loc = slir::toMLIRLocation(ns, location.start);
|
||||
auto &ctx = ns.getContext();
|
||||
auto loc = slir::toMLIRLocation(ns, location.start);
|
||||
auto &ctx = ns.getContext();
|
||||
|
||||
mlir::OpBuilder builder(&ctx.mlirContext);
|
||||
|
||||
|
@ -127,7 +127,6 @@ void Fn::generateIR(serene::Namespace &ns, mlir::ModuleOp &m) {
|
|||
argSym->name, mlir::TypeAttr::get(builder.getI64Type())));
|
||||
}
|
||||
|
||||
// auto funcType = builder.getFunctionType(arg_types, builder.getI64Type());
|
||||
auto fn = builder.create<slir::FnOp>(
|
||||
loc, builder.getI64Type(), name,
|
||||
mlir::DictionaryAttr::get(builder.getContext(), arguments),
|
||||
|
@ -135,21 +134,26 @@ void Fn::generateIR(serene::Namespace &ns, mlir::ModuleOp &m) {
|
|||
|
||||
if (!fn) {
|
||||
m.emitError(llvm::formatv("Can't create the function '{0}'", name));
|
||||
return;
|
||||
}
|
||||
|
||||
auto *entryBlock = new mlir::Block();
|
||||
auto &body = fn.body();
|
||||
auto *entryBlock = new mlir::Block();
|
||||
|
||||
body.push_back(entryBlock);
|
||||
|
||||
builder.setInsertionPointToStart(entryBlock);
|
||||
auto retVal = builder.create<slir::ValueOp>(loc, 0).getResult();
|
||||
|
||||
mlir::ReturnOp returnOp = builder.create<mlir::ReturnOp>(loc, retVal);
|
||||
slir::ReturnOp returnOp = builder.create<slir::ReturnOp>(loc, retVal);
|
||||
|
||||
if (!returnOp) {
|
||||
m.emitError(
|
||||
llvm::formatv("Can't create the return value of function '{0}'", name));
|
||||
fn.erase();
|
||||
return;
|
||||
}
|
||||
|
||||
m.push_back(fn);
|
||||
};
|
||||
} // namespace exprs
|
||||
|
|
Loading…
Reference in New Issue