CIRCT 22.0.0git
Loading...
Searching...
No Matches
ImportVerilog.cpp
Go to the documentation of this file.
1//===- ImportVerilog.cpp - Slang Verilog frontend integration -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements bridging from the slang Verilog frontend to CIRCT dialects.
10//
11//===----------------------------------------------------------------------===//
12
19#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
20#include "mlir/IR/Diagnostics.h"
21#include "mlir/IR/Verifier.h"
22#include "mlir/Pass/PassManager.h"
23#include "mlir/Support/Timing.h"
24#include "mlir/Tools/mlir-translate/Translation.h"
25#include "mlir/Transforms/Passes.h"
26#include "llvm/ADT/Hashing.h"
27#include "llvm/Support/SourceMgr.h"
28
29#include "slang/diagnostics/DiagnosticClient.h"
30#include "slang/driver/Driver.h"
31#include "slang/parsing/Preprocessor.h"
32#include "slang/syntax/SyntaxPrinter.h"
33#include "slang/util/VersionInfo.h"
34
35using namespace mlir;
36using namespace circt;
37using namespace ImportVerilog;
38
39using llvm::SourceMgr;
40
42 std::string buffer;
43 llvm::raw_string_ostream os(buffer);
44 os << "slang version ";
45 os << slang::VersionInfo::getMajor() << ".";
46 os << slang::VersionInfo::getMinor() << ".";
47 os << slang::VersionInfo::getPatch() << "+";
48 os << slang::VersionInfo::getHash();
49 return buffer;
50}
51
52//===----------------------------------------------------------------------===//
53// Diagnostics
54//===----------------------------------------------------------------------===//
55
56/// Convert a slang `SourceLocation` to an MLIR `Location`.
57static Location convertLocation(MLIRContext *context,
58 const slang::SourceManager &sourceManager,
59 slang::SourceLocation loc) {
60 if (loc && loc.buffer() != slang::SourceLocation::NoLocation.buffer()) {
61 auto fileName = sourceManager.getFileName(loc);
62 auto line = sourceManager.getLineNumber(loc);
63 auto column = sourceManager.getColumnNumber(loc);
64 return FileLineColLoc::get(context, fileName, line, column);
65 }
66 return UnknownLoc::get(context);
67}
68
69Location Context::convertLocation(slang::SourceLocation loc) {
70 return ::convertLocation(getContext(), sourceManager, loc);
71}
72
73Location Context::convertLocation(slang::SourceRange range) {
74 return convertLocation(range.start());
75}
76
77namespace {
78/// A converter that can be plugged into a slang `DiagnosticEngine` as a client
79/// that will map slang diagnostics to their MLIR counterpart and emit them.
80class MlirDiagnosticClient : public slang::DiagnosticClient {
81public:
82 MlirDiagnosticClient(MLIRContext *context) : context(context) {}
83
84 void report(const slang::ReportedDiagnostic &diag) override {
85 // Generate the primary MLIR diagnostic.
86 auto &diagEngine = context->getDiagEngine();
87 auto mlirDiag = diagEngine.emit(convertLocation(diag.location),
88 getSeverity(diag.severity));
89 mlirDiag << diag.formattedMessage;
90
91 // Append the name of the option that can be used to control this
92 // diagnostic.
93 auto optionName = engine->getOptionName(diag.originalDiagnostic.code);
94 if (!optionName.empty())
95 mlirDiag << " [-W" << optionName << "]";
96
97 // Write out macro expansions, if we have any, in reverse order.
98 for (auto loc : std::views::reverse(diag.expansionLocs)) {
99 auto &note = mlirDiag.attachNote(
100 convertLocation(sourceManager->getFullyOriginalLoc(loc)));
101 auto macroName = sourceManager->getMacroName(loc);
102 if (macroName.empty())
103 note << "expanded from here";
104 else
105 note << "expanded from macro '" << macroName << "'";
106 }
107
108 // Write out the include stack.
109 slang::SmallVector<slang::SourceLocation> includeStack;
110 getIncludeStack(diag.location.buffer(), includeStack);
111 for (auto &loc : std::views::reverse(includeStack))
112 mlirDiag.attachNote(convertLocation(loc)) << "included from here";
113 }
114
115 /// Convert a slang `SourceLocation` to an MLIR `Location`.
116 Location convertLocation(slang::SourceLocation loc) const {
117 return ::convertLocation(context, *sourceManager, loc);
118 }
119
120 static DiagnosticSeverity getSeverity(slang::DiagnosticSeverity severity) {
121 switch (severity) {
122 case slang::DiagnosticSeverity::Fatal:
123 case slang::DiagnosticSeverity::Error:
124 return DiagnosticSeverity::Error;
125 case slang::DiagnosticSeverity::Warning:
126 return DiagnosticSeverity::Warning;
127 case slang::DiagnosticSeverity::Ignored:
128 case slang::DiagnosticSeverity::Note:
129 return DiagnosticSeverity::Remark;
130 }
131 llvm_unreachable("all slang diagnostic severities should be handled");
132 return DiagnosticSeverity::Error;
133 }
134
135private:
136 MLIRContext *context;
137};
138} // namespace
139
140// Allow for `slang::BufferID` to be used as hash map keys.
141namespace llvm {
142template <>
143struct DenseMapInfo<slang::BufferID> {
144 static slang::BufferID getEmptyKey() { return slang::BufferID(); }
145 static slang::BufferID getTombstoneKey() {
146 return slang::BufferID(UINT32_MAX - 1, ""sv);
147 // UINT32_MAX is already used by `BufferID::getPlaceholder`.
148 }
149 static unsigned getHashValue(slang::BufferID id) {
150 return llvm::hash_value(id.getId());
151 }
152 static bool isEqual(slang::BufferID a, slang::BufferID b) { return a == b; }
153};
154} // namespace llvm
155
156//===----------------------------------------------------------------------===//
157// Driver
158//===----------------------------------------------------------------------===//
159
160namespace {
161const static ImportVerilogOptions defaultOptions;
162
163struct ImportDriver {
164 ImportDriver(MLIRContext *mlirContext, TimingScope &ts,
165 const ImportVerilogOptions *options)
166 : mlirContext(mlirContext), ts(ts),
167 options(options ? *options : defaultOptions) {}
168
169 LogicalResult prepareDriver(SourceMgr &sourceMgr);
170 LogicalResult importVerilog(ModuleOp module);
171 LogicalResult preprocessVerilog(llvm::raw_ostream &os);
172
173 MLIRContext *mlirContext;
174 TimingScope &ts;
175 const ImportVerilogOptions &options;
176
177 // Use slang's driver which conveniently packages a lot of the things we
178 // need for compilation.
179 slang::driver::Driver driver;
180};
181} // namespace
182
183/// Populate the Slang driver with source files from the given `sourceMgr`, and
184/// configure driver options based on the `ImportVerilogOptions` passed to the
185/// `ImportDriver` constructor.
186LogicalResult ImportDriver::prepareDriver(SourceMgr &sourceMgr) {
187 // Use slang's driver which conveniently packages a lot of the things we
188 // need for compilation.
189 auto diagClient = std::make_shared<MlirDiagnosticClient>(mlirContext);
190 driver.diagEngine.addClient(diagClient);
191
192 for (const auto &value : options.commandFiles)
193 if (!driver.processCommandFiles(value, /*makeRelative=*/true,
194 /*separateUnit=*/true))
195 return failure();
196
197 // Populate the source manager with the source files.
198 // NOTE: This is a bit ugly since we're essentially copying the Verilog
199 // source text in memory. At a later stage we'll want to extend slang's
200 // SourceManager such that it can contain non-owned buffers. This will do
201 // for now.
202 DenseSet<StringRef> seenBuffers;
203 for (unsigned i = 0, e = sourceMgr.getNumBuffers(); i < e; ++i) {
204 const llvm::MemoryBuffer *mlirBuffer = sourceMgr.getMemoryBuffer(i + 1);
205 auto name = mlirBuffer->getBufferIdentifier();
206 if (!name.empty() && !seenBuffers.insert(name).second)
207 continue; // Slang doesn't like listing the same buffer twice
208 auto slangBuffer =
209 driver.sourceManager.assignText(name, mlirBuffer->getBuffer());
210 driver.sourceLoader.addBuffer(slangBuffer);
211 }
212
213 for (const auto &libDir : options.libDirs)
214 driver.sourceLoader.addSearchDirectories(libDir);
215
216 for (const auto &libExt : options.libExts)
217 driver.sourceLoader.addSearchExtension(libExt);
218
219 for (const auto &includeDir : options.includeDirs)
220 if (driver.sourceManager.addUserDirectories(includeDir))
221 return failure();
222
223 for (const auto &includeSystemDir : options.includeSystemDirs)
224 if (driver.sourceManager.addSystemDirectories(includeSystemDir))
225 return failure();
226
227 // Populate the driver options.
228 driver.options.excludeExts.insert(options.excludeExts.begin(),
229 options.excludeExts.end());
230 driver.options.ignoreDirectives = options.ignoreDirectives;
231
232 driver.options.maxIncludeDepth = options.maxIncludeDepth;
233 driver.options.defines = options.defines;
234 driver.options.undefines = options.undefines;
235 driver.options.librariesInheritMacros = options.librariesInheritMacros;
236
237 driver.options.timeScale = options.timeScale;
238 driver.options.compilationFlags.emplace(
239 slang::ast::CompilationFlags::AllowUseBeforeDeclare,
240 options.allowUseBeforeDeclare);
241 driver.options.compilationFlags.emplace(
242 slang::ast::CompilationFlags::IgnoreUnknownModules,
243 options.ignoreUnknownModules);
244 driver.options.compilationFlags.emplace(
245 slang::ast::CompilationFlags::LintMode,
247 driver.options.compilationFlags.emplace(
248 slang::ast::CompilationFlags::DisableInstanceCaching, false);
249 driver.options.topModules = options.topModules;
250 driver.options.paramOverrides = options.paramOverrides;
251
252 driver.options.errorLimit = options.errorLimit;
253 driver.options.warningOptions = options.warningOptions;
254
255 driver.options.singleUnit = options.singleUnit;
256
257 return success(driver.processOptions());
258}
259
260/// Parse and elaborate the prepared source files, and populate the given MLIR
261/// `module` with corresponding operations.
262LogicalResult ImportDriver::importVerilog(ModuleOp module) {
263 // Parse the input.
264 auto parseTimer = ts.nest("Verilog parser");
265 bool parseSuccess = driver.parseAllSources();
266 parseTimer.stop();
267
268 // Elaborate the input.
269 auto compileTimer = ts.nest("Verilog elaboration");
270 auto compilation = driver.createCompilation();
271 for (auto &diag : compilation->getAllDiagnostics())
272 driver.diagEngine.issue(diag);
273 if (!parseSuccess || driver.diagEngine.getNumErrors() > 0)
274 return failure();
275 compileTimer.stop();
276
277 // If we were only supposed to lint the input, return here. This leaves the
278 // module empty, but any Slang linting messages got reported as diagnostics.
279 if (options.mode == ImportVerilogOptions::Mode::OnlyLint)
280 return success();
281
282 // Traverse the parsed Verilog AST and map it to the equivalent CIRCT ops.
283 mlirContext
284 ->loadDialect<moore::MooreDialect, hw::HWDialect, cf::ControlFlowDialect,
285 func::FuncDialect, verif::VerifDialect, ltl::LTLDialect,
286 debug::DebugDialect>();
287 auto conversionTimer = ts.nest("Verilog to dialect mapping");
288 Context context(options, *compilation, module, driver.sourceManager);
289 if (failed(context.convertCompilation()))
290 return failure();
291 conversionTimer.stop();
292
293 // Run the verifier on the constructed module to ensure it is clean.
294 auto verifierTimer = ts.nest("Post-parse verification");
295 return verify(module);
296}
297
298/// Preprocess the prepared source files and print them to the given output
299/// stream.
300LogicalResult ImportDriver::preprocessVerilog(llvm::raw_ostream &os) {
301 auto parseTimer = ts.nest("Verilog preprocessing");
302
303 // Run the preprocessor to completion across all sources previously added with
304 // `pushSource`, report diagnostics, and print the output.
305 auto preprocessAndPrint = [&](slang::parsing::Preprocessor &preprocessor) {
306 slang::syntax::SyntaxPrinter output;
307 output.setIncludeComments(false);
308 while (true) {
309 slang::parsing::Token token = preprocessor.next();
310 output.print(token);
311 if (token.kind == slang::parsing::TokenKind::EndOfFile)
312 break;
313 }
314
315 for (auto &diag : preprocessor.getDiagnostics()) {
316 if (diag.isError()) {
317 driver.diagEngine.issue(diag);
318 return failure();
319 }
320 }
321 os << output.str();
322 return success();
323 };
324
325 // Depending on whether the single-unit option is set, either add all source
326 // files to a single preprocessor such that they share define macros and
327 // directives, or create a separate preprocessor for each, such that each
328 // source file is in its own compilation unit.
329 auto optionBag = driver.createOptionBag();
330 if (driver.options.singleUnit == true) {
331 slang::BumpAllocator alloc;
332 slang::Diagnostics diagnostics;
333 slang::parsing::Preprocessor preprocessor(driver.sourceManager, alloc,
334 diagnostics, optionBag);
335 // Sources have to be pushed in reverse, as they form a stack in the
336 // preprocessor. Last pushed source is processed first.
337 auto sources = driver.sourceLoader.loadSources();
338 for (auto &buffer : std::views::reverse(sources))
339 preprocessor.pushSource(buffer);
340 if (failed(preprocessAndPrint(preprocessor)))
341 return failure();
342 } else {
343 for (auto &buffer : driver.sourceLoader.loadSources()) {
344 slang::BumpAllocator alloc;
345 slang::Diagnostics diagnostics;
346 slang::parsing::Preprocessor preprocessor(driver.sourceManager, alloc,
347 diagnostics, optionBag);
348 preprocessor.pushSource(buffer);
349 if (failed(preprocessAndPrint(preprocessor)))
350 return failure();
351 }
352 }
353
354 return success();
355}
356
357//===----------------------------------------------------------------------===//
358// Entry Points
359//===----------------------------------------------------------------------===//
360
361/// Parse the specified Verilog inputs into the specified MLIR context.
362LogicalResult circt::importVerilog(SourceMgr &sourceMgr,
363 MLIRContext *mlirContext, TimingScope &ts,
364 ModuleOp module,
365 const ImportVerilogOptions *options) {
366 ImportDriver importDriver(mlirContext, ts, options);
367 if (failed(importDriver.prepareDriver(sourceMgr)))
368 return failure();
369 return importDriver.importVerilog(module);
370}
371
372/// Run the files in a source manager through Slang's Verilog preprocessor and
373/// emit the result to the given output stream.
374LogicalResult circt::preprocessVerilog(SourceMgr &sourceMgr,
375 MLIRContext *mlirContext,
376 TimingScope &ts, llvm::raw_ostream &os,
377 const ImportVerilogOptions *options) {
378 ImportDriver importDriver(mlirContext, ts, options);
379 if (failed(importDriver.prepareDriver(sourceMgr)))
380 return failure();
381 return importDriver.preprocessVerilog(os);
382}
383
384/// Entry point as an MLIR translation.
386 static TranslateToMLIRRegistration fromVerilog(
387 "import-verilog", "import Verilog or SystemVerilog",
388 [](llvm::SourceMgr &sourceMgr, MLIRContext *context) {
389 TimingScope ts;
391 ModuleOp::create(UnknownLoc::get(context)));
392 ImportVerilogOptions options;
393 options.debugInfo = true;
394 options.warningOptions.push_back("no-missing-top");
395 if (failed(
396 importVerilog(sourceMgr, context, ts, module.get(), &options)))
397 module = {};
398 return module;
399 });
400}
401
402//===----------------------------------------------------------------------===//
403// Pass Pipeline
404//===----------------------------------------------------------------------===//
405
406/// Optimize and simplify the Moore dialect IR.
407void circt::populateVerilogToMoorePipeline(OpPassManager &pm) {
408 {
409 // Perform an initial cleanup and preprocessing across all
410 // modules/functions.
411 auto &anyPM = pm.nestAny();
412 anyPM.addPass(mlir::createCSEPass());
413 anyPM.addPass(mlir::createCanonicalizerPass());
414 }
415
416 pm.addPass(moore::createVTablesPass());
417
418 // Remove unused symbols.
419 pm.addPass(mlir::createSymbolDCEPass());
420
421 {
422 // Perform module-specific transformations.
423 auto &modulePM = pm.nest<moore::SVModuleOp>();
424 modulePM.addPass(moore::createLowerConcatRefPass());
425 // TODO: Enable the following once it not longer interferes with @(...)
426 // event control checks. The introduced dummy variables make the event
427 // control observe a static local variable that never changes, instead of
428 // observing a module-wide signal.
429 // modulePM.addPass(moore::createSimplifyProceduresPass());
430 modulePM.addPass(mlir::createSROA());
431 }
432
433 {
434 // Perform a final cleanup across all modules/functions.
435 auto &anyPM = pm.nestAny();
436 anyPM.addPass(mlir::createMem2Reg());
437 anyPM.addPass(mlir::createCSEPass());
438 anyPM.addPass(mlir::createCanonicalizerPass());
439 }
440}
441
442/// Convert Moore dialect IR into core dialect IR
443void circt::populateMooreToCorePipeline(OpPassManager &pm) {
444 // Perform the conversion.
445 pm.addPass(createConvertMooreToCorePass());
446
447 {
448 // Conversion to the core dialects likely uncovers new canonicalization
449 // opportunities.
450 auto &anyPM = pm.nestAny();
451 anyPM.addPass(mlir::createCSEPass());
452 anyPM.addPass(mlir::createCanonicalizerPass());
453 }
454}
455
456/// Convert LLHD dialect IR into core dialect IR
458 OpPassManager &pm, const LlhdToCorePipelineOptions &options) {
459 // Inline function calls and lower SCF to CF.
460 pm.addNestedPass<hw::HWModuleOp>(llhd::createWrapProceduralOpsPass());
461 pm.addPass(mlir::createSCFToControlFlowPass());
462 pm.addPass(llhd::createInlineCallsPass());
463 pm.addPass(mlir::createSymbolDCEPass());
464
465 // Simplify processes, replace signals with process results, and detect
466 // registers.
467 auto &modulePM = pm.nest<hw::HWModuleOp>();
468 // See https://github.com/llvm/circt/issues/8804.
469 // modulePM.addPass(mlir::createSROA());
470 modulePM.addPass(llhd::createMem2RegPass());
471 modulePM.addPass(llhd::createHoistSignalsPass());
472 modulePM.addPass(llhd::createDeseqPass());
473 modulePM.addPass(llhd::createLowerProcessesPass());
474 modulePM.addPass(mlir::createCSEPass());
475 modulePM.addPass(mlir::createCanonicalizerPass());
476
477 // Unroll loops and remove control flow.
478 modulePM.addPass(llhd::createUnrollLoopsPass());
479 modulePM.addPass(mlir::createCSEPass());
480 modulePM.addPass(mlir::createCanonicalizerPass());
481 modulePM.addPass(llhd::createRemoveControlFlowPass());
482 modulePM.addPass(mlir::createCSEPass());
483 modulePM.addPass(mlir::createCanonicalizerPass());
484
485 // Convert `arith.select` generated by some of the control flow canonicalizers
486 // to `comb.mux`.
487 modulePM.addPass(createMapArithToCombPass());
488
489 // Simplify module-level signals.
490 modulePM.addPass(llhd::createCombineDrivesPass());
491 modulePM.addPass(llhd::createSig2Reg());
492 modulePM.addPass(mlir::createCSEPass());
493 modulePM.addPass(mlir::createCanonicalizerPass());
494
495 // Map `seq.firreg` with array type and `hw.array_inject` self-feedback to
496 // `seq.firmem` ops.
497 if (options.detectMemories) {
498 modulePM.addPass(seq::createRegOfVecToMem());
499 modulePM.addPass(mlir::createCSEPass());
500 modulePM.addPass(mlir::createCanonicalizerPass());
501 }
502}
static Location convertLocation(MLIRContext *context, const slang::SourceManager &sourceManager, slang::SourceLocation loc)
Convert a slang SourceLocation to an MLIR Location.
static llvm::lsp::DiagnosticSeverity getSeverity(slang::DiagnosticSeverity severity)
std::unique_ptr< mlir::Pass > createLowerConcatRefPass()
std::unique_ptr< mlir::Pass > createVTablesPass()
std::unique_ptr< mlir::Pass > createRegOfVecToMem()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
void populateVerilogToMoorePipeline(mlir::OpPassManager &pm)
Optimize and simplify the Moore dialect IR.
std::unique_ptr< mlir::Pass > createMapArithToCombPass()
void populateMooreToCorePipeline(mlir::OpPassManager &pm)
Convert Moore dialect IR into core dialect IR.
void populateLlhdToCorePipeline(mlir::OpPassManager &pm, const LlhdToCorePipelineOptions &options)
std::string getSlangVersion()
Return a human-readable string describing the slang frontend version linked into CIRCT.
std::unique_ptr< OperationPass< ModuleOp > > createConvertMooreToCorePass()
Create an Moore to Comb/HW/LLHD conversion pass.
mlir::LogicalResult importVerilog(llvm::SourceMgr &sourceMgr, mlir::MLIRContext *context, mlir::TimingScope &ts, mlir::ModuleOp module, const ImportVerilogOptions *options=nullptr)
Parse files in a source manager as Verilog source code and populate the given MLIR module with corres...
mlir::LogicalResult preprocessVerilog(llvm::SourceMgr &sourceMgr, mlir::MLIRContext *context, mlir::TimingScope &ts, llvm::raw_ostream &os, const ImportVerilogOptions *options=nullptr)
Run the files in a source manager through Slang's Verilog preprocessor and emit the result to the giv...
void registerFromVerilogTranslation()
Register the import-verilog MLIR translation.
llvm::hash_code hash_value(const DenseSet< T > &set)
Definition sv.py:1
Options that control how Verilog input files are parsed and processed.
std::vector< std::string > warningOptions
A list of warning options that will be passed to the DiagnosticEngine.
@ OnlyLint
Only lint the input, without elaboration and lowering to CIRCT IR.
bool debugInfo
Generate debug information in the form of debug dialect ops in the IR.
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
const slang::SourceManager & sourceManager
MLIRContext * getContext()
Return the MLIR context.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.
Convert LLHD dialect IR into core dialect IR.
static bool isEqual(slang::BufferID a, slang::BufferID b)
static slang::BufferID getTombstoneKey()
static unsigned getHashValue(slang::BufferID id)