-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
209 lines (179 loc) · 7.02 KB
/
main.cpp
File metadata and controls
209 lines (179 loc) · 7.02 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
#include "Passes.h"
#include "Tutorial.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LogicalResult.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/ToolOutputFile.h"
#include "mlir/Bytecode/BytecodeWriter.h"
#include "mlir/Debug/CLOptionsSetup.h"
#include "mlir/Debug/Counter.h"
#include "mlir/Debug/DebuggerExecutionContextHook.h"
#include "mlir/Debug/ExecutionContext.h"
#include "mlir/Debug/Observers/ActionLogging.h"
#include "mlir/Dialect/IRDL/IR/IRDL.h"
#include "mlir/Dialect/IRDL/IRDLLoading.h"
#include "mlir/Dialect/Linalg/TransformOps/DialectExtension.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Location.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/InitAllDialects.h"
#include "mlir/InitAllPasses.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Pass/PassRegistry.h"
#include "mlir/Support/FileUtilities.h"
#include "mlir/Support/Timing.h"
#include "mlir/Support/ToolUtilities.h"
#include "mlir/Tools/ParseUtilities.h"
#include "mlir/Tools/Plugins/DialectPlugin.h"
#include "mlir/Tools/Plugins/PassPlugin.h"
#include "mlir/Tools/mlir-opt/MlirOptMain.h"
using namespace mlir;
using namespace llvm;
const char *toolName = "Tensor Tiling Tutorial Compiler";
void createPassPipeline(PassManager &pm) {
// Apply required transform spec.
{ pm.addPass(tutorial::createTutorialApplyTilingSpec()); }
// Parallel tiling using scf.forall
{
tutorial::TutorialTileAndFuseOptions options;
options.tilingLevel = tutorial::TilingLevel::Parallel;
pm.addPass(tutorial::createTutorialTileAndFuse(options));
}
// Reduction tiling using scf.for
{
tutorial::TutorialTileAndFuseOptions options;
options.tilingLevel = tutorial::TilingLevel::Reduction;
pm.addPass(tutorial::createTutorialTileAndFuse(options));
}
// Generalization and cleanups.
{
pm.addPass(createLinalgGeneralizeNamedOpsPass());
pm.addPass(createCanonicalizerPass());
pm.addPass(createCSEPass());
pm.addPass(createLoopInvariantCodeMotionPass());
LinalgFoldUnitExtentDimsPassOptions options;
options.useRankReducingSlices = true;
pm.addPass(createLinalgFoldUnitExtentDimsPass(options));
}
// Vectorization
{
pm.addPass(tutorial::createTutorialVectorization());
pm.addPass(createCanonicalizerPass());
pm.addPass(createCSEPass());
pm.addPass(tensor::createFoldTensorSubsetOpsPass());
pm.addPass(createLoopInvariantSubsetHoistingPass());
pm.addPass(createCanonicalizerPass());
pm.addPass(createCSEPass());
}
// Bufferization
{
bufferization::OneShotBufferizePassOptions options;
options.bufferizeFunctionBoundaries = true;
options.functionBoundaryTypeConversion =
bufferization::LayoutMapOption::IdentityLayoutMap;
pm.addPass(bufferization::createOneShotBufferizePass(options));
pm.addPass(createCanonicalizerPass());
pm.addPass(createCSEPass());
pm.addPass(memref::createFoldMemRefAliasOpsPass());
}
}
LogicalResult tutorialOpt(int argc, char **argv) {
static llvm::cl::OptionCategory mainOptions("Tutorial Options");
// General command line flags.
static cl::opt<std::string> inputFilename(
cl::Positional, cl::desc("<input file>"), cl::init("-"));
static cl::opt<std::string> outputFilename(
"o", cl::desc("Output filename"), cl::value_desc("filename"),
cl::init("-"), llvm::cl::cat(mainOptions));
cl::ParseCommandLineOptions(argc, argv);
InitLLVM y(argc, argv);
// When reading from stdin and the input is a tty, it is often a user mistake
// and the process "appears to be stuck". Print a message to let the user know
// about it!
if (inputFilename == "-" &&
sys::Process::FileDescriptorIsDisplayed(fileno(stdin)))
llvm::errs() << "(processing input from stdin now, hit ctrl-c/ctrl-d to "
"interrupt)\n";
// Set up the input file.
std::string errorMessage;
auto file = openInputFile(inputFilename, &errorMessage);
if (!file) {
llvm::errs() << errorMessage << "\n";
return failure();
}
auto output = openOutputFile(outputFilename, &errorMessage);
if (!output) {
llvm::errs() << errorMessage << "\n";
return failure();
}
// Tell sourceMgr about this buffer, which is what the parser will pick up.
auto sourceMgr = std::make_shared<SourceMgr>();
sourceMgr->AddNewSourceBuffer(std::move(file), SMLoc());
DialectRegistry registry;
registry.insert<func::FuncDialect>();
registry.insert<arith::ArithDialect>();
registry.insert<linalg::LinalgDialect>();
registry.insert<tensor::TensorDialect>();
registry.insert<scf::SCFDialect>();
registry.insert<vector::VectorDialect>();
registry.insert<memref::MemRefDialect>();
registry.insert<LLVM::LLVMDialect>();
registry.insert<index::IndexDialect>();
registry.insert<affine::AffineDialect>();
registry.insert<transform::TransformDialect>();
registry.insert<tutorial::TutorialDialect>();
linalg::registerAllDialectInterfaceImplementations(registry);
tensor::registerInferTypeOpInterfaceExternalModels(registry);
vector::registerBufferizableOpInterfaceExternalModels(registry);
arith::registerBufferizableOpInterfaceExternalModels(registry);
tensor::registerBufferizableOpInterfaceExternalModels(registry);
scf::registerBufferizableOpInterfaceExternalModels(registry);
bufferization::func_ext::registerBufferizableOpInterfaceExternalModels(
registry);
vector::registerSubsetOpInterfaceExternalModels(registry);
scf::registerBufferDeallocationOpInterfaceExternalModels(registry);
affine::registerValueBoundsOpInterfaceExternalModels(registry);
tensor::registerTransformDialectExtension(registry);
scf::registerTransformDialectExtension(registry);
linalg::registerTransformDialectExtension(registry);
// Create a context just for the current buffer. Disable threading on creation
// since we'll inject the thread-pool separately.
MLIRContext context(registry, MLIRContext::Threading::DISABLED);
SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
ParserConfig parseConfig(&context);
OwningOpRef<Operation *> op =
parseSourceFileForTool(sourceMgr, parseConfig, true);
if (!op) {
return failure();
}
PassManager pm(op.get()->getName(), PassManager::Nesting::Implicit);
pm.enableVerifier();
pm.enableIRPrinting();
createPassPipeline(pm);
// Run the pipeline.
if (failed(pm.run(*op))) {
return failure();
}
AsmState asmState(op.get(), OpPrintingFlags(), /*locationMap=*/nullptr);
op.get()->print(output->os(), asmState);
output->os() << '\n';
output->keep();
return success();
}
int main(int argc, char **argv) {
return mlir::asMainReturnCode(tutorialOpt(argc, argv));
}