-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini_nn.cpp
More file actions
213 lines (175 loc) · 6.42 KB
/
Copy pathmini_nn.cpp
File metadata and controls
213 lines (175 loc) · 6.42 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
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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "dialect/MLIRGen.h"
#include "dialect/NNDialect.h"
#include "mlir/IR/OwningOpRef.h"
#include "parser/parser.h"
#include "passes/Passes.h"
#include "mlir/Dialect/Bufferization/Transforms/Passes.h"
#include "mlir/Dialect/MemRef/Transforms/Passes.h"
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Pass/PassManager.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/raw_ostream.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
void printDebugInfo(std::string text, mlir::ModuleOp module) {
#ifdef DEBUG_MODE
llvm::outs() << text;
module->print(llvm::outs());
llvm::outs() << "\n";
#endif
}
int main(int argc, char **argv) {
std::string target_device = "cpu";
if (argc == 2) {
pFile = fopen(argv[1], "r");
if (pFile == NULL)
perror("Error opening file");
} else if (argc == 3) {
pFile = fopen(argv[1], "r");
if (pFile == NULL)
perror("Error opening file");
target_device = std::string(argv[2]);
} else {
std::cout << "Usage: ./mini_nn InputFile\n";
return 1;
}
llvm::outs() << "Target device: " << target_device << "\n";
// Extract directory path from input file for output files
std::string input_file_path(argv[1]);
std::string location_prefix = "";
size_t last_slash = input_file_path.find_last_of('/');
if (last_slash != std::string::npos) {
location_prefix =
input_file_path.substr(0, last_slash + 1); // Include the trailing slash
}
// Extract base filename without extension for naming output files
std::string base_filename = input_file_path;
if (last_slash != std::string::npos) {
base_filename = input_file_path.substr(last_slash + 1);
}
size_t dot_pos = base_filename.find_last_of('.');
if (dot_pos != std::string::npos) {
base_filename = base_filename.substr(0, dot_pos);
}
#ifdef DEBUG_MODE
std::cout << "Input file: " << input_file_path << std::endl;
std::cout << "Output directory: "
<< (location_prefix.empty() ? "./" : location_prefix) << std::endl;
std::cout << "Base filename: " << base_filename << std::endl;
#endif
// Parse the input program
Parser parser;
auto program = parser.parseProgram();
fclose(pFile);
if (!program) {
std::cerr << "Failed to parse program" << std::endl;
return 1;
}
#ifdef DEBUG_MODE
std::cout << "=== AST ===\n";
std::cout << program->toString() << std::endl;
#endif
// Create MLIR context and register all necessary dialects
mlir::MLIRContext context;
// Load specific dialects we need, without loadAllAvailableDialects() which
// might cause issues
context.getOrLoadDialect<mlir::nn::NNDialect>();
context.getOrLoadDialect<mlir::arith::ArithDialect>();
context.getOrLoadDialect<mlir::linalg::LinalgDialect>();
context.getOrLoadDialect<mlir::tensor::TensorDialect>();
context.getOrLoadDialect<mlir::math::MathDialect>();
context.getOrLoadDialect<mlir::func::FuncDialect>();
context.getOrLoadDialect<mlir::LLVM::LLVMDialect>();
// Generate MLIR from AST
auto module = mlir::nn::mlirGen(context, *program);
if (!module) {
std::cerr << "Failed to generate MLIR" << std::endl;
return 1;
}
printDebugInfo("\n=== MLIR (Before Shape Inference) ===\n", *module);
// Create a pass manager and add the shape inference pass
mlir::PassManager pm(&context);
// Since ShapeInferencePass operates on FuncOp, we need to use a nested pass
// manager
pm.addNestedPass<mlir::nn::FuncOp>(mlir::nn::createShapeInferencePass());
// Run the shape inference pass
if (mlir::failed(pm.run(*module))) {
std::cerr << "Failed to run shape inference pass" << std::endl;
return 1;
}
printDebugInfo("\n=== MLIR (After Shape Inference) ===\n", *module);
// Add lowering to Linalg pass
mlir::PassManager loweringPM(&context);
loweringPM.addPass(mlir::nn::createLowerToLinalgPass());
// Run the lowering pass
if (mlir::failed(loweringPM.run(*module))) {
std::cerr << "Failed to run lowering pass" << std::endl;
return 1;
}
printDebugInfo("\n=== MLIR (After Lowering to Linalg) ===\n", *module);
// Save the Linalg IR to a file for external processing
std::string linalg_file = location_prefix + base_filename + "_linalg.mlir";
std::error_code EC;
llvm::raw_fd_ostream linalg_output(linalg_file, EC);
if (EC) {
std::cerr << "Failed to open file for writing: " << linalg_file
<< std::endl;
return 1;
}
module->print(linalg_output);
linalg_output.close();
std::cout << "Saved Linalg IR to: " << linalg_file << std::endl;
mlir::PassManager affinePM(&context);
affinePM.addPass(mlir::nn::createLowerToLoopsPass());
// Run the LLVM lowering pass
if (mlir::failed(affinePM.run(*module))) {
std::cerr << "Failed to run LLVM lowering pass" << std::endl;
return 1;
}
std::string affine_file = location_prefix + base_filename + "_affine.mlir";
llvm::raw_fd_ostream affine_output(affine_file, EC);
if (EC) {
std::cerr << "Failed to open file for writing: " << affine_file
<< std::endl;
return 1;
}
module->print(affine_output);
affine_output.close();
std::cout << "Saved Affine IR to: " << affine_file << std::endl;
printDebugInfo("\n=== MLIR (After Lowering to LLVM) ===\n", *module);
mlir::PassManager llvmPM(&context);
if (target_device == "cpu") {
llvmPM.addPass(mlir::nn::createLowerLoopsToCPUPass());
} else {
llvmPM.addPass(mlir::nn::createLowerLoopsToGPUPass());
}
// Run the LLVM lowering pass
if (mlir::failed(llvmPM.run(*module))) {
std::cerr << "Failed to run LLVM lowering pass" << std::endl;
return 1;
}
std::string llvm_file = location_prefix + base_filename + "_llvm.mlir";
llvm::raw_fd_ostream llvm_output(llvm_file, EC);
if (EC) {
std::cerr << "Failed to open file for writing: " << llvm_file
<< std::endl;
return 1;
}
module->print(llvm_output);
llvm_output.close();
std::cout << "Saved Affine IR to: " << llvm_file << std::endl;
printDebugInfo("\n=== MLIR (After Lowering to LLVM) ===\n", *module);
return 0;
}