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"
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"
37using namespace ImportVerilog;
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();
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);
66 return UnknownLoc::get(
context);
69Location Context::convertLocation(slang::SourceLocation loc) {
80class MlirDiagnosticClient :
public slang::DiagnosticClient {
84 void report(
const slang::ReportedDiagnostic &diag)
override {
86 auto &diagEngine =
context->getDiagEngine();
89 mlirDiag << diag.formattedMessage;
93 auto optionName = engine->getOptionName(diag.originalDiagnostic.code);
94 if (!optionName.empty())
95 mlirDiag <<
" [-W" << optionName <<
"]";
98 for (
auto loc : std::views::reverse(diag.expansionLocs)) {
99 auto ¬e = mlirDiag.attachNote(
101 auto macroName = sourceManager->getMacroName(loc);
102 if (macroName.empty())
103 note <<
"expanded from here";
105 note <<
"expanded from macro '" << macroName <<
"'";
109 slang::SmallVector<slang::SourceLocation> includeStack;
110 getIncludeStack(diag.location.buffer(), includeStack);
111 for (
auto &loc : std::views::reverse(includeStack))
117 return ::convertLocation(
context, *sourceManager, loc);
120 static DiagnosticSeverity
getSeverity(slang::DiagnosticSeverity 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;
131 llvm_unreachable(
"all slang diagnostic severities should be handled");
132 return DiagnosticSeverity::Error;
144 static slang::BufferID
getEmptyKey() {
return slang::BufferID(); }
146 return slang::BufferID(UINT32_MAX - 1,
""sv);
152 static bool isEqual(slang::BufferID a, slang::BufferID b) {
return a == b; }
164 ImportDriver(MLIRContext *mlirContext, TimingScope &ts,
166 : mlirContext(mlirContext), ts(ts),
167 options(options ? *options : defaultOptions) {}
169 LogicalResult prepareDriver(SourceMgr &sourceMgr);
173 MLIRContext *mlirContext;
179 slang::driver::Driver driver;
186LogicalResult ImportDriver::prepareDriver(SourceMgr &sourceMgr) {
189 auto diagClient = std::make_shared<MlirDiagnosticClient>(mlirContext);
190 driver.diagEngine.addClient(diagClient);
192 for (
const auto &value : options.commandFiles)
193 if (!driver.processCommandFiles(value, true,
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)
209 driver.sourceManager.assignText(name, mlirBuffer->getBuffer());
210 driver.sourceLoader.addBuffer(slangBuffer);
213 for (
const auto &libDir : options.libDirs)
214 driver.sourceLoader.addSearchDirectories(libDir);
216 for (
const auto &libExt : options.libExts)
217 driver.sourceLoader.addSearchExtension(libExt);
219 for (
const auto &[i, f] :
llvm::enumerate(options.libraryFiles)) {
221 auto libName =
"library " + std::to_string(i);
222 driver.sourceLoader.addLibraryFiles(libName, f);
225 for (
const auto &includeDir : options.includeDirs)
226 if (driver.sourceManager.addUserDirectories(includeDir))
229 for (
const auto &includeSystemDir : options.includeSystemDirs)
230 if (driver.sourceManager.addSystemDirectories(includeSystemDir))
234 driver.options.excludeExts.insert(options.excludeExts.begin(),
235 options.excludeExts.end());
236 driver.options.ignoreDirectives = options.ignoreDirectives;
238 driver.options.maxIncludeDepth = options.maxIncludeDepth;
239 driver.options.defines = options.defines;
240 driver.options.undefines = options.undefines;
241 driver.options.librariesInheritMacros = options.librariesInheritMacros;
243 driver.options.timeScale = options.timeScale;
244 driver.options.compilationFlags.emplace(
245 slang::ast::CompilationFlags::AllowUseBeforeDeclare,
246 options.allowUseBeforeDeclare);
247 driver.options.compilationFlags.emplace(
248 slang::ast::CompilationFlags::IgnoreUnknownModules,
249 options.ignoreUnknownModules);
250 driver.options.compilationFlags.emplace(
251 slang::ast::CompilationFlags::LintMode,
253 driver.options.compilationFlags.emplace(
254 slang::ast::CompilationFlags::DisableInstanceCaching,
false);
255 driver.options.topModules = options.topModules;
256 driver.options.paramOverrides = options.paramOverrides;
258 driver.options.errorLimit = options.errorLimit;
259 driver.options.warningOptions = options.warningOptions;
261 driver.options.singleUnit = options.singleUnit;
263 return success(driver.processOptions());
268LogicalResult ImportDriver::importVerilog(ModuleOp module) {
270 auto parseTimer = ts.nest(
"Verilog parser");
271 bool parseSuccess = driver.parseAllSources();
275 auto compileTimer = ts.nest(
"Verilog elaboration");
276 auto compilation = driver.createCompilation();
277 for (
auto &diag : compilation->getAllDiagnostics())
278 driver.diagEngine.issue(diag);
279 if (!parseSuccess || driver.diagEngine.getNumErrors() > 0)
290 ->loadDialect<moore::MooreDialect, hw::HWDialect, cf::ControlFlowDialect,
291 func::FuncDialect, verif::VerifDialect, ltl::LTLDialect,
292 debug::DebugDialect>();
293 auto conversionTimer = ts.nest(
"Verilog to dialect mapping");
294 Context context(options, *compilation, module, driver.sourceManager);
295 if (failed(
context.convertCompilation()))
297 conversionTimer.stop();
300 auto verifierTimer = ts.nest(
"Post-parse verification");
301 return verify(module);
306LogicalResult ImportDriver::preprocessVerilog(llvm::raw_ostream &os) {
307 auto parseTimer = ts.nest(
"Verilog preprocessing");
311 auto preprocessAndPrint = [&](slang::parsing::Preprocessor &preprocessor) {
312 slang::syntax::SyntaxPrinter output;
313 output.setIncludeComments(
false);
315 slang::parsing::Token token = preprocessor.next();
317 if (token.kind == slang::parsing::TokenKind::EndOfFile)
321 for (
auto &diag : preprocessor.getDiagnostics()) {
322 if (diag.isError()) {
323 driver.diagEngine.issue(diag);
335 auto optionBag = driver.createOptionBag();
336 if (driver.options.singleUnit ==
true) {
337 slang::BumpAllocator alloc;
338 slang::Diagnostics diagnostics;
339 slang::parsing::Preprocessor preprocessor(driver.sourceManager, alloc,
340 diagnostics, optionBag);
343 auto sources = driver.sourceLoader.loadSources();
344 for (
auto &buffer : std::views::reverse(sources))
345 preprocessor.pushSource(buffer);
346 if (failed(preprocessAndPrint(preprocessor)))
349 for (
auto &buffer : driver.sourceLoader.loadSources()) {
350 slang::BumpAllocator alloc;
351 slang::Diagnostics diagnostics;
352 slang::parsing::Preprocessor preprocessor(driver.sourceManager, alloc,
353 diagnostics, optionBag);
354 preprocessor.pushSource(buffer);
355 if (failed(preprocessAndPrint(preprocessor)))
369 MLIRContext *mlirContext, TimingScope &ts,
372 ImportDriver importDriver(mlirContext, ts, options);
373 if (failed(importDriver.prepareDriver(sourceMgr)))
375 return importDriver.importVerilog(module);
381 MLIRContext *mlirContext,
382 TimingScope &ts, llvm::raw_ostream &os,
384 ImportDriver importDriver(mlirContext, ts, options);
385 if (failed(importDriver.prepareDriver(sourceMgr)))
387 return importDriver.preprocessVerilog(os);
392 static TranslateToMLIRRegistration fromVerilog(
393 "import-verilog",
"import Verilog or SystemVerilog",
394 [](llvm::SourceMgr &sourceMgr, MLIRContext *
context) {
397 ModuleOp::create(UnknownLoc::get(
context)));
417 auto &anyPM = pm.nestAny();
418 anyPM.addPass(mlir::createCSEPass());
419 anyPM.addPass(mlir::createCanonicalizerPass());
425 pm.addPass(mlir::createSymbolDCEPass());
429 auto &modulePM = pm.nest<moore::SVModuleOp>();
436 modulePM.addPass(mlir::createSROA());
441 auto &anyPM = pm.nestAny();
442 anyPM.addPass(mlir::createMem2Reg());
443 anyPM.addPass(mlir::createCSEPass());
444 anyPM.addPass(mlir::createCanonicalizerPass());
456 auto &anyPM = pm.nestAny();
457 anyPM.addPass(mlir::createCSEPass());
458 anyPM.addPass(mlir::createCanonicalizerPass());
466 pm.addNestedPass<
hw::HWModuleOp>(llhd::createWrapProceduralOpsPass());
467 pm.addPass(mlir::createSCFToControlFlowPass());
468 pm.addPass(llhd::createInlineCallsPass());
469 pm.addPass(mlir::createSymbolDCEPass());
476 modulePM.addPass(mlir::createSROA());
478 modulePM.addPass(llhd::createMem2RegPass());
479 modulePM.addPass(llhd::createHoistSignalsPass());
480 modulePM.addPass(llhd::createDeseqPass());
481 modulePM.addPass(llhd::createLowerProcessesPass());
482 modulePM.addPass(mlir::createCSEPass());
483 modulePM.addPass(mlir::createCanonicalizerPass());
486 modulePM.addPass(llhd::createUnrollLoopsPass());
487 modulePM.addPass(mlir::createCSEPass());
488 modulePM.addPass(mlir::createCanonicalizerPass());
489 modulePM.addPass(llhd::createRemoveControlFlowPass());
490 modulePM.addPass(mlir::createCSEPass());
491 modulePM.addPass(mlir::createCanonicalizerPass());
498 modulePM.addPass(llhd::createCombineDrivesPass());
499 modulePM.addPass(llhd::createSig2Reg());
500 modulePM.addPass(mlir::createCSEPass());
501 modulePM.addPass(mlir::createCanonicalizerPass());
507 modulePM.addPass(mlir::createCSEPass());
508 modulePM.addPass(mlir::createCanonicalizerPass());
static std::unique_ptr< Context > context
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.
std::unique_ptr< mlir::Pass > createMapArithToCombPass(bool enableBestEffortLowering=false)
void populateVerilogToMoorePipeline(mlir::OpPassManager &pm)
Optimize and simplify the Moore dialect IR.
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)
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.
Option< bool > detectMemories
static bool isEqual(slang::BufferID a, slang::BufferID b)
static slang::BufferID getEmptyKey()
static slang::BufferID getTombstoneKey()
static unsigned getHashValue(slang::BufferID id)