-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathkanagawatool.cpp
More file actions
475 lines (406 loc) · 17.5 KB
/
kanagawatool.cpp
File metadata and controls
475 lines (406 loc) · 17.5 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//===- kanagawatool.cpp - utility for working with the Kanagawa dialect ---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements 'kanagawatool', which composes together a variety of
// CIRCT libraries that can be used to realise an Kanagawa-based lowering flow.
//
//===----------------------------------------------------------------------===//
#include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassInstrumentation.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Support/FileUtilities.h"
#include "mlir/Support/Timing.h"
#include "mlir/Support/ToolUtilities.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/ToolOutputFile.h"
#include "circt/Conversion/ExportVerilog.h"
#include "circt/Conversion/HWToSV.h"
#include "circt/Conversion/Passes.h"
#include "circt/Dialect/Comb/CombPasses.h"
#include "circt/Dialect/DC/DCDialect.h"
#include "circt/Dialect/DC/DCPasses.h"
#include "circt/Dialect/ESI/ESIDialect.h"
#include "circt/Dialect/ESI/ESIPasses.h"
#include "circt/Dialect/Kanagawa/KanagawaDialect.h"
#include "circt/Dialect/Kanagawa/KanagawaOps.h"
#include "circt/Dialect/Kanagawa/KanagawaPassPipelines.h"
#include "circt/Dialect/Kanagawa/KanagawaPasses.h"
#include "circt/Dialect/Pipeline/PipelineDialect.h"
#include "circt/Dialect/Pipeline/PipelinePasses.h"
#include "circt/Dialect/SV/SVDialect.h"
#include "circt/Dialect/SV/SVPasses.h"
#include "circt/Dialect/Seq/SeqDialect.h"
#include "circt/Dialect/Seq/SeqPasses.h"
#include "circt/Support/LoweringOptions.h"
#include "circt/Support/LoweringOptionsParser.h"
#include "circt/Support/Version.h"
#include "circt/Transforms/Passes.h"
using namespace llvm;
using namespace mlir;
using namespace circt;
using namespace kanagawa;
// --------------------------------------------------------------------------
// Tool options
// --------------------------------------------------------------------------
static cl::OptionCategory mainCategory("kanagawatool Options");
static cl::opt<std::string> inputFilename(cl::Positional,
cl::desc("<input file>"),
cl::init("-"), cl::cat(mainCategory));
static cl::opt<std::string> outputFilename(
"o", cl::desc("Output filename, or directory for split output"),
cl::value_desc("filename"), cl::init("-"), cl::cat(mainCategory));
static cl::opt<bool>
splitInputFile("split-input-file",
cl::desc("Split the input file into pieces and process each "
"chunk independently"),
cl::init(false), cl::Hidden, cl::cat(mainCategory));
static cl::opt<bool>
verifyDiagnostics("verify-diagnostics",
cl::desc("Check that emitted diagnostics match "
"expected-* lines on the corresponding line"),
cl::init(false), cl::Hidden, cl::cat(mainCategory));
static cl::opt<bool>
verbosePassExecutions("verbose-pass-executions",
cl::desc("Log executions of toplevel module passes"),
cl::init(false), cl::cat(mainCategory));
static cl::opt<bool>
verifyPasses("verify-each",
cl::desc("Run the verifier after each transformation pass"),
cl::init(true), cl::cat(mainCategory));
static cl::opt<bool>
allowUnregisteredDialects("allow-unregistered-dialects",
cl::desc("Allow unknown dialects in the input"),
cl::init(false), cl::Hidden,
cl::cat(mainCategory));
enum OutputFormatKind {
OutputLoweredKanagawa,
OutputIR,
OutputVerilog,
OutputSplitVerilog
};
static cl::opt<OutputFormatKind> outputFormat(
cl::desc("Specify output format:"),
cl::values(
clEnumValN(OutputLoweredKanagawa, "post-kanagawa-ir",
"Emit IR after Kanagawa constructs have been lowered away"),
clEnumValN(OutputIR, "ir", "Emit pre-emission IR"),
clEnumValN(OutputVerilog, "verilog", "Emit Verilog"),
clEnumValN(OutputSplitVerilog, "split-verilog",
"Emit Verilog (one file per module; specify "
"directory with -o=<dir>)")),
cl::init(OutputVerilog), cl::cat(mainCategory));
static cl::opt<bool>
traceIVerilog("sv-trace-iverilog",
cl::desc("Add tracing to an iverilog simulated module"),
cl::init(false), cl::cat(mainCategory));
enum FlowKind { HiKanagawa, LoKanagawa };
static cl::opt<FlowKind> flowKind(
cl::desc("Specify flow kind:"),
cl::values(clEnumValN(HiKanagawa, "hi", "High-level Kanagawa flow"),
clEnumValN(LoKanagawa, "lo", "Low-level Kanagawa flow")),
cl::Required, cl::cat(mainCategory));
static LoweringOptionsOption loweringOptions(mainCategory);
// --------------------------------------------------------------------------
// (Configurable) pass pipelines
// --------------------------------------------------------------------------
/// Create a simple canonicalizer pass.
static std::unique_ptr<Pass> createSimpleCanonicalizerPass() {
mlir::GreedyRewriteConfig config;
config.setUseTopDownTraversal(true);
config.setRegionSimplificationLevel(
mlir::GreedySimplifyRegionLevel::Disabled);
return mlir::createCanonicalizerPass(config);
}
static void loadHighLevelControlflowTransformsPipeline(OpPassManager &pm) {
pm.addPass(mlir::createLowerAffinePass());
pm.addPass(mlir::createSCFToControlFlowPass());
pm.addPass(createSimpleCanonicalizerPass());
}
static void loadHandshakeTransformsPipeline(OpPassManager &pm) {
// Make the CFG a binary tree by inserting merge blocks.
pm.addPass(circt::createInsertMergeBlocksPass());
// Perform dataflow conversion
pm.nest<kanagawa::DesignOp>().nest<kanagawa::ClassOp>().addPass(
kanagawa::createConvertCFToHandshakePass());
// Canonicalize - necessary after handshake conversion to clean up a lot of
// stuff e.g. simple branches.
pm.addPass(createSimpleCanonicalizerPass());
pm.nest<handshake::FuncOp>().addPass(
handshake::createHandshakeInsertBuffersPass("all", 2));
}
static void loadDCTransformsPipeline(OpPassManager &pm) {
pm.nest<kanagawa::DesignOp>().nest<ClassOp>().addPass(
kanagawa::createConvertHandshakeToDCPass());
pm.addPass(createSimpleCanonicalizerPass());
pm.nest<kanagawa::DesignOp>()
.nest<ClassOp>()
.nest<DataflowMethodOp>()
.addPass(dc::createDCMaterializeForksSinksPass());
// pm.nest<ClassOp>().addPass(circt::createDCToHWPass());
}
static void loadESILoweringPipeline(OpPassManager &pm) {
pm.addPass(circt::esi::createESIBundleLoweringPass());
pm.addPass(circt::esi::createESIPortLoweringPass());
pm.addPass(circt::esi::createESIPhysicalLoweringPass());
pm.addPass(circt::esi::createESItoHWPass());
}
static void loadHWLoweringPipeline(OpPassManager &pm) {
pm.addPass(createSimpleCanonicalizerPass());
pm.nest<hw::HWModuleOp>().addPass(circt::seq::createLowerSeqHLMem());
pm.addPass(seq::createHWMemSimImpl());
pm.addPass(circt::createLowerSeqToSVPass());
pm.nest<hw::HWModuleOp>().addPass(sv::createHWCleanup());
pm.addPass(mlir::createCSEPass());
pm.addPass(circt::comb::createLowerComb());
pm.nest<hw::HWModuleOp>().addPass(circt::createLowerHWToSVPass());
// Legalize unsupported operations within the modules.
pm.nest<hw::HWModuleOp>().addPass(sv::createHWLegalizeModules());
pm.addPass(createSimpleCanonicalizerPass());
// Tidy up the IR to improve verilog emission quality.
auto &modulePM = pm.nest<hw::HWModuleOp>();
modulePM.addPass(sv::createPrettifyVerilog());
}
static void loadSchedulingPipeline(OpPassManager &pm) {
// Inject operator library
pm.addPass(kanagawa::createAddOperatorLibraryPass());
// Map any arith operators to comb
pm.nest<kanagawa::DesignOp>()
.nest<kanagawa::ClassOp>()
.nest<kanagawa::DataflowMethodOp>()
.nest<kanagawa::IsolatedStaticBlockOp>()
.addPass(circt::createMapArithToCombPass());
// Prepare for scheduling
pm.nest<kanagawa::DesignOp>()
.nest<kanagawa::ClassOp>()
.nest<kanagawa::DataflowMethodOp>()
.nest<kanagawa::IsolatedStaticBlockOp>()
.addPass(kanagawa::createPrepareSchedulingPass());
// Schedule!
pm.nest<kanagawa::DesignOp>()
.nest<kanagawa::ClassOp>()
.nest<kanagawa::DataflowMethodOp>()
.nest<kanagawa::IsolatedStaticBlockOp>()
.addPass(pipeline::createScheduleLinearPipelinePass());
}
static void loadPipelineLoweringPipeline(OpPassManager &pm) {
pm.addPass(pipeline::createExplicitRegsPass());
pm.addPass(createPipelineToHWPass());
}
// --------------------------------------------------------------------------
// Tool driver code
// --------------------------------------------------------------------------
static void loadLowLevelPassPipeline(
PassManager &pm, ModuleOp module,
std::optional<std::unique_ptr<llvm::ToolOutputFile>> &outputFile) {
loadPipelineLoweringPipeline(pm);
loadESILoweringPipeline(pm);
loadHWLoweringPipeline(pm);
if (traceIVerilog)
pm.addPass(circt::sv::createSVTraceIVerilog());
if (loweringOptions.getNumOccurrences())
loweringOptions.setAsAttribute(module);
if (outputFormat == OutputVerilog) {
pm.addPass(createExportVerilogPass((*outputFile)->os()));
} else if (outputFormat == OutputSplitVerilog) {
pm.addPass(createExportSplitVerilogPass(outputFilename));
}
}
static void loadKanagawaHiFlow(
PassManager &pm, ModuleOp module,
std::optional<std::unique_ptr<llvm::ToolOutputFile>> &outputFile) {
if (verbosePassExecutions)
llvm::errs() << "[kanagawatool] Will run high-level Kanagawa flow\n";
loadHighLevelControlflowTransformsPipeline(pm);
loadKanagawaHighLevelPassPipeline(pm);
if (outputFormat != OutputLoweredKanagawa) {
loadHandshakeTransformsPipeline(pm);
loadSchedulingPipeline(pm);
loadDCTransformsPipeline(pm);
if (outputFormat != OutputLoweredKanagawa)
loadLowLevelPassPipeline(pm, module, outputFile);
}
}
static void loadKanagawaLoFlow(
PassManager &pm, ModuleOp module,
std::optional<std::unique_ptr<llvm::ToolOutputFile>> &outputFile) {
if (verbosePassExecutions)
llvm::errs() << "[kanagawatool] Will run low-level Kanagawa flow\n";
loadKanagawaLowLevelPassPipeline(pm);
if (outputFormat != OutputLoweredKanagawa)
loadLowLevelPassPipeline(pm, module, outputFile);
}
/// Process a single buffer of the input.
static LogicalResult processBuffer(
MLIRContext &context, TimingScope &ts, llvm::SourceMgr &sourceMgr,
std::optional<std::unique_ptr<llvm::ToolOutputFile>> &outputFile) {
// Parse the input.
mlir::OwningOpRef<mlir::ModuleOp> module;
llvm::sys::TimePoint<> parseStartTime;
if (verbosePassExecutions) {
llvm::errs() << "[kanagawatool] Running MLIR parser\n";
parseStartTime = llvm::sys::TimePoint<>::clock::now();
}
auto parserTimer = ts.nest("MLIR Parser");
module = parseSourceFile<ModuleOp>(sourceMgr, &context);
if (!module)
return failure();
if (verbosePassExecutions) {
auto elpased = std::chrono::duration<double>(
llvm::sys::TimePoint<>::clock::now() - parseStartTime) /
std::chrono::seconds(1);
llvm::errs() << "[kanagawatool] -- Done in "
<< llvm::format("%.3f", elpased) << " sec\n";
}
// Apply any pass manager command line options.
PassManager pm(&context);
pm.enableVerifier(verifyPasses);
pm.enableTiming(ts);
if (failed(applyPassManagerCLOptions(pm)))
return failure();
pm.addPass(createSimpleCanonicalizerPass());
if (flowKind == HiKanagawa)
loadKanagawaHiFlow(pm, module.get(), outputFile);
else if (flowKind == LoKanagawa)
loadKanagawaLoFlow(pm, module.get(), outputFile);
// Go execute!
if (failed(pm.run(module.get())))
return failure();
if (outputFormat != OutputVerilog || outputFormat == OutputSplitVerilog)
module->print((*outputFile)->os());
return success();
// We intentionally "leak" the Module into the MLIRContext instead of
// deallocating it. There is no need to deallocate it right before process
// exit.
(void)module.release();
return success();
}
/// Process a single split of the input. This allocates a source manager and
/// creates a regular or verifying diagnostic handler, depending on whether
/// the user set the verifyDiagnostics option.
static LogicalResult processInputSplit(
MLIRContext &context, TimingScope &ts,
std::unique_ptr<llvm::MemoryBuffer> buffer,
std::optional<std::unique_ptr<llvm::ToolOutputFile>> &outputFile) {
llvm::SourceMgr sourceMgr;
sourceMgr.AddNewSourceBuffer(std::move(buffer), llvm::SMLoc());
if (!verifyDiagnostics) {
SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
return processBuffer(context, ts, sourceMgr, outputFile);
}
SourceMgrDiagnosticVerifierHandler sourceMgrHandler(sourceMgr, &context);
context.printOpOnDiagnostic(false);
(void)processBuffer(context, ts, sourceMgr, outputFile);
return sourceMgrHandler.verify();
}
/// Process the entire input provided by the user, splitting it up if the
/// corresponding option was specified.
static LogicalResult
processInput(MLIRContext &context, TimingScope &ts,
std::unique_ptr<llvm::MemoryBuffer> input,
std::optional<std::unique_ptr<llvm::ToolOutputFile>> &outputFile) {
if (!splitInputFile)
return processInputSplit(context, ts, std::move(input), outputFile);
return splitAndProcessBuffer(
std::move(input),
[&](std::unique_ptr<MemoryBuffer> buffer, raw_ostream &) {
return processInputSplit(context, ts, std::move(buffer), outputFile);
},
llvm::outs());
}
static LogicalResult executeKanagawatool(MLIRContext &context) {
if (allowUnregisteredDialects)
context.allowUnregisteredDialects();
// Create the timing manager we use to sample execution times.
DefaultTimingManager tm;
applyDefaultTimingManagerCLOptions(tm);
auto ts = tm.getRootScope();
// Set up the input file.
std::string errorMessage;
auto input = openInputFile(inputFilename, &errorMessage);
if (!input) {
llvm::errs() << errorMessage << "\n";
return failure();
}
std::optional<std::unique_ptr<llvm::ToolOutputFile>> outputFile;
if (outputFormat != OutputSplitVerilog) {
outputFile.emplace(openOutputFile(outputFilename, &errorMessage));
if (!*outputFile) {
llvm::errs() << errorMessage << "\n";
return failure();
}
}
// Process the input.
if (failed(processInput(context, ts, std::move(input), outputFile)))
return failure();
// If the result succeeded and we're emitting a file, close it.
if (outputFile.has_value())
(*outputFile)->keep();
return success();
}
/// Main driver for kanagawatool command. This sets up LLVM and MLIR, and
/// parses command line options before passing off to 'executeKanagawatool'.
/// This is set up so we can `exit(0)` at the end of the program to avoid
/// teardown of the MLIRContext and modules inside of it (reducing compile
/// time).
int main(int argc, char **argv) {
InitLLVM y(argc, argv);
// Set the bug report message to indicate users should file issues on
// llvm/circt and not llvm/llvm-project.
setBugReportMsg(circtBugReportMsg);
// Hide default LLVM options, other than for this tool.
// MLIR options are added below.
cl::HideUnrelatedOptions(mainCategory);
// Register any pass manager command line options.
registerMLIRContextCLOptions();
registerPassManagerCLOptions();
registerDefaultTimingManagerCLOptions();
registerAsmPrinterCLOptions();
// Parse pass names in main to ensure static initialization completed.
cl::ParseCommandLineOptions(argc, argv, "CIRCT Kanagawa tool\n");
DialectRegistry registry;
// Register MLIR dialects.
registry.insert<mlir::memref::MemRefDialect>();
registry.insert<mlir::func::FuncDialect>();
registry.insert<mlir::arith::ArithDialect>();
registry.insert<mlir::cf::ControlFlowDialect>();
registry.insert<mlir::scf::SCFDialect>();
// Register MLIR passes.
mlir::registerCSEPass();
mlir::registerSCCPPass();
mlir::registerInlinerPass();
mlir::registerCanonicalizerPass();
// Register CIRCT dialects.
registry
.insert<hw::HWDialect, comb::CombDialect, seq::SeqDialect, sv::SVDialect,
handshake::HandshakeDialect, kanagawa::KanagawaDialect,
dc::DCDialect, esi::ESIDialect, pipeline::PipelineDialect>();
// Do the guts of the kanagawatool process.
MLIRContext context(registry);
auto result = executeKanagawatool(context);
// Use "exit" instead of return'ing to signal completion. This avoids
// invoking the MLIRContext destructor, which spends a bunch of time
// deallocating memory etc which process exit will do for us.
exit(failed(result));
}