-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
79 lines (55 loc) · 1.95 KB
/
main.cc
File metadata and controls
79 lines (55 loc) · 1.95 KB
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
#include "mlir/IR/Builders.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/Verifier.h"
// Conversions
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Pass/Pass.h"
using namespace mlir;
int main() {
// Create an MLIR context
MLIRContext context;
// Load the required dialects
context.getOrLoadDialect<arith::ArithDialect>();
context.getOrLoadDialect<func::FuncDialect>();
// Create a module
OwningOpRef<ModuleOp> module = ModuleOp::create(UnknownLoc::get(&context));
// Create a builder to help with constructing operations
OpBuilder builder(&context);
// Define the function type (takes no arguments, returns i32)
auto funcType = builder.getFunctionType({}, builder.getI32Type());
// Create a function named "main"
auto funcOp = builder.create<func::FuncOp>(
builder.getUnknownLoc(), "main", funcType);
// Add the function to the module
module->push_back(funcOp);
// Create a new block inside the function
Block *entryBlock = funcOp.addEntryBlock();
// Set the insertion point to the start of the block
builder.setInsertionPointToStart(entryBlock);
// Create a constant value of 42
auto constOp = builder.create<arith::ConstantOp>(
builder.getUnknownLoc(), builder.getI32Type(),
builder.getI32IntegerAttr(42));
// Return the constant value
builder.create<func::ReturnOp>(builder.getUnknownLoc(), constOp.getResult());
if(failed(verify(module.get())))
{
llvm::errs()<<"Error: Verification Failed\n";
return 0;
}
// Print the MLIR code to stdout
module->dump();
// All about Pass
PassManager pm(&context);
pm.addPass(createArithToLLVMConversionPass());
if (mlir::failed(pm.run(*module))) {
llvm::errs() << "Failed to convert to LLVM dialect\n";
return 1;
}
module->dump();
return 0;
}