-
Notifications
You must be signed in to change notification settings - Fork 3
/
mlir_generator.cpp
245 lines (210 loc) · 8.91 KB
/
mlir_generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "mlir_generator.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
#include "mlir/Dialect/Affine/Passes.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/ExecutionEngine/ExecutionEngine.h"
#include "mlir/ExecutionEngine/OptUtils.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Verifier.h"
#include "mlir/InitAllDialects.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Export.h"
#include "mlir/Transforms/Passes.h"
namespace luac {
MLIRGenerator::MLIRGenerator(mlir::MLIRContext* context, AstContext* ast) : builder(context), ast(ast) {
module = builder.create<mlir::ModuleOp>(loc(0, 0));
}
void MLIRGenerator::gen() {
createLuaVarSturct();
createLuaStringSturct();
createMainFunction();
for (int i = 0; i < ast->stats.size(); i++){
Stat* stat = ast->stats[i].get();
if (typeid(*stat) == typeid(FunctionCall)){
//
std::cout << "function call" << std::endl;
genFunctioncall(dynamic_cast<FunctionCall*>(stat));
}
}
endMainFunction();
}
void MLIRGenerator::createLuaVarSturct() {
auto context = module->getContext();
context->getOrLoadDialect<mlir::LLVM::LLVMDialect>();
// struct LuaVar {}
auto varStruct =
mlir::LLVM::LLVMStructType::getIdentified(context, "struct.LuaVar");
llvm::SmallVector<mlir::Type, 4> fields;
fields.push_back(mlir::IntegerType::get(context, 64));
fields.push_back(mlir::IntegerType::get(context, 64));
varStruct.setBody(fields, false);
}
void MLIRGenerator::createLuaStringSturct() {
auto context = module->getContext();
context->getOrLoadDialect<mlir::LLVM::LLVMDialect>();
// struct LuaString {}
auto varStruct =
mlir::LLVM::LLVMStructType::getIdentified(context, "struct.LuaString");
llvm::SmallVector<mlir::Type, 4> fields;
fields.push_back(
mlir::LLVM::LLVMPointerType::get(mlir::IntegerType::get(context, 8)));
fields.push_back(mlir::IntegerType::get(context, 32));
varStruct.setBody(fields, false);
}
mlir::LLVM::LLVMStructType MLIRGenerator::getLuaStringSturct() {
auto context = module->getContext();
context->getOrLoadDialect<mlir::LLVM::LLVMDialect>();
// struct LuaString {}
return mlir::LLVM::LLVMStructType::getIdentified(context, "struct.LuaString");
}
mlir::Value MLIRGenerator::getOrCreateGlobalString(llvm::StringRef name, llvm::StringRef value) {
// Create the global at the entry of the module.
mlir::LLVM::GlobalOp global;
if (!(global = module.lookupSymbol<mlir::LLVM::GlobalOp>(name))) {
mlir::OpBuilder::InsertionGuard insertGuard(builder);
builder.setInsertionPointToStart(module.getBody());
auto type = mlir::LLVM::LLVMArrayType::get(
mlir::IntegerType::get(builder.getContext(), 8), value.size());
global = builder.create<mlir::LLVM::GlobalOp>(loc(0,0), type, /*isConstant=*/true,
mlir::LLVM::Linkage::Internal, name,
builder.getStringAttr(value),
/*alignment=*/0);
}
// Get the pointer to the first character in the global string.
mlir::Value globalPtr = builder.create<mlir::LLVM::AddressOfOp>(loc(0,0), global);
mlir::Value cst0 = builder.create<mlir::LLVM::ConstantOp>(
loc(0,0), mlir::IntegerType::get(builder.getContext(), 64),
builder.getIntegerAttr(builder.getIndexType(), 0));
return builder.create<mlir::LLVM::GEPOp>(
loc(0,0),
mlir::LLVM::LLVMPointerType::get(mlir::IntegerType::get(builder.getContext(), 8)),
globalPtr, llvm::ArrayRef<mlir::Value>({cst0, cst0}));
}
void MLIRGenerator::createMainFunction() {
llvm::SmallVector<mlir::Type, 4> args;
auto retType = mlir::IntegerType::get(module->getContext(), 32);
auto func = mlir::LLVM::lookupOrCreateFn(module, "main", args, retType);
auto entryBlock = func.addEntryBlock();
builder.setInsertionPointToStart(entryBlock);
}
void MLIRGenerator::endMainFunction() {
auto res = builder.create<mlir::arith::ConstantIntOp>(loc(0, 0), 0,
builder.getI32Type());
builder.create<mlir::LLVM::ReturnOp>(loc(0, 0), res.getResult());
}
mlir::LLVM::LLVMFuncOp MLIRGenerator::getFunction(llvm::StringRef name) {
auto item = functionMap.find(name);
if(item == functionMap.end()) {
if(name == "print") {
auto strStruct = getLuaStringSturct();
llvm::SmallVector<mlir::Type> params;
params.push_back( strStruct);
return mlir::LLVM::lookupOrCreateFn(
module, "print", params, mlir::LLVM::LLVMVoidType::get(module->getContext()));
}
}
return nullptr;
}
std::any MLIRGenerator::genFunctioncall(FunctionCall* funcCall) {
std::cout << "enter function call" << std::endl;
llvm::SmallVector<mlir::Type> params;
auto funcOp = getFunction(funcCall->name);
if(funcOp == nullptr) { // function not exist
return 0;
}
// visit Args
auto helloStr = getOrCreateGlobalString("hello", "hello");
auto zero = builder.create<mlir::LLVM::ConstantOp>(loc(0,0), builder.getI32Type(), builder.getI32IntegerAttr(0));
auto one = builder.create<mlir::LLVM::ConstantOp>(loc(0,0), builder.getI32Type(), builder.getI32IntegerAttr(1));
auto strStruct = getLuaStringSturct();
mlir::Value strInstance = builder.create<mlir::LLVM::UndefOp>(loc(0,0), strStruct);
auto ptrToNs = helloStr;//builder.create<mlir::LLVM::AddressOfOp>(loc(0,0), helloStr);
strInstance = builder.create<mlir::LLVM::InsertValueOp>(
loc(0,0), strInstance.getType(), strInstance, ptrToNs,
builder.getI64ArrayAttr(0));
auto len = builder.create<mlir::LLVM::ConstantOp>(loc(0,0), builder.getI32Type(), builder.getI32IntegerAttr(5));
strInstance = builder.create<mlir::LLVM::InsertValueOp>(
loc(0,0), strInstance.getType(), strInstance, len,
builder.getI64ArrayAttr(1));
//auto s = builder.create<mlir::LLVM::AddressOfOp>(loc(0,0), mlir::ValueRange{strInstance});
builder.create<mlir::LLVM::CallOp>(loc(0,0), funcOp, mlir::ValueRange{strInstance});
return 0;
}
std::any MLIRGenerator::visitNameAndArgs(LuaParser::NameAndArgsContext* ctx) {
}
/*
void MLIRGenerator::visitFuncname(LuaParser::FuncnameContext* ctx) {
std::cout << "enter into a function declear" << ctx->getStart()->getText()
<< std::endl;
llvm::ScopedHashTableScope<llvm::StringRef, mlir::Value> varScope(
symbolTable);
builder.setInsertionPointToEnd(module.getBody());
llvm::SmallVector<mlir::Type> argTypes;
llvm::SmallVector<mlir::Type> results;
argTypes.push_back(builder.getI64Type());
argTypes.push_back(builder.getI64Type());
auto varStruct = mlir::LLVM::LLVMStructType::getIdentified(
module.getContext(), "struct.LuaVar");
argTypes.push_back(varStruct);
// argTypes.push_back()
results.push_back(builder.getI64Type());
auto funcType = builder.getFunctionType(argTypes, results);
// ctx->getToken()->getText();
auto funcOp = builder.create<mlir::func::FuncOp>(
loc(0, 0), ctx->getStart()->getText(), funcType);
funcOp.addEntryBlock();
mlir::Block& entryBlock = funcOp.front();
builder.setInsertionPointToStart(&entryBlock);
}
void MLIRGenerator::exitFuncname(LuaParser::FuncnameContext* ctx) {
auto res = builder.create<mlir::arith::ConstantIntOp>(loc(0, 0), 123,
builder.getI64Type());
builder.create<mlir::func::ReturnOp>(loc(0, 0), res.getResult());
}
*/
void MLIRGenerator::dumpMLIR() {
if (mlir::failed(mlir::verify(module))) {
module.emitError("module verification error");
// return nullptr;
}
module.dump();
}
void MLIRGenerator::dumpIR() {
mlir::PassManager pm(module->getContext());
pm.addPass(mlir::createConvertFuncToLLVMPass());
pm.run(module);
llvm::LLVMContext llvmContext;
mlir::registerLLVMDialectTranslation(*module.getContext());
auto llvmModule = mlir::translateModuleToLLVMIR(module, llvmContext);
if (!llvmModule) {
llvm::errs() << "Failed to emit LLVM IR\n";
return;
}
auto optPipeline = mlir::makeOptimizingTransformer(
/*optLevel=*/0, /*sizeLevel=*/0,
/*targetMachine=*/nullptr);
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
mlir::ExecutionEngine::setupTargetTriple(llvmModule.get());
if (auto err = optPipeline(llvmModule.get())) {
llvm::errs() << "Failed to optimize LLVM IR " << err << "\n";
return;
}
llvm::errs() << *llvmModule << "\n";
}
} // namespace luac