CIRCT 23.0.0git
Loading...
Searching...
No Matches
ImportVerilogInternals.h
Go to the documentation of this file.
1//===- ImportVerilogInternals.h - Internal implementation details ---------===//
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// NOLINTNEXTLINE(llvm-header-guard)
10#ifndef CONVERSION_IMPORTVERILOG_IMPORTVERILOGINTERNALS_H
11#define CONVERSION_IMPORTVERILOG_IMPORTVERILOGINTERNALS_H
12
13#include "CaptureAnalysis.h"
20#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
21#include "mlir/Dialect/Func/IR/FuncOps.h"
22#include "slang/ast/ASTVisitor.h"
23#include "slang/text/SourceManager.h"
24#include "llvm/ADT/ScopedHashTable.h"
25#include "llvm/Support/Debug.h"
26#include <map>
27#include <queue>
28
29#define DEBUG_TYPE "import-verilog"
30
31namespace circt {
32namespace ImportVerilog {
33
34using moore::Domain;
35
36/// Get the slang canonical body for the given `instance`, if there is one.
37/// If there isn't one, fall back to the normal body.
38inline const slang::ast::InstanceBodySymbol *
39getCanonicalBody(const slang::ast::InstanceSymbol &inst) {
40 const slang::ast::InstanceBodySymbol *body = inst.getCanonicalBody();
41 return body == nullptr ? &inst.body : body;
42}
43
44/// Port lowering information.
46 const slang::ast::PortSymbol &ast;
47 Location loc;
48 BlockArgument arg;
49 /// Slot index in the module signature. `outputIdx` is set for outputs;
50 /// `inputIdx` is set for inputs and inouts. Regular and expanded
51 /// interface-modport ports share one numbering per direction.
52 std::optional<unsigned> outputIdx;
53 std::optional<unsigned> inputIdx;
54};
55
56/// Lowering information for a single signal flattened from an interface port.
58 StringAttr name;
60 mlir::Type type;
61 Location loc;
62 BlockArgument arg;
63 /// the origin interface port symbol this was flattened from.
64 const slang::ast::InterfacePortSymbol *origin;
65 /// the interface body member (VariableSymbol , NetSymbol)
66 const slang::ast::Symbol *bodySym;
67 /// The connected interface instance backing this port (if any). This enables
68 /// materializing virtual interface handles from interface ports.
69 const slang::ast::InstanceSymbol *ifaceInstance = nullptr;
70 /// For modport-typed iface ports, the ModportPortSymbol this was flattened
71 /// from. Slang resolves in-body accesses like `bus.member` to a
72 /// `HierarchicalValueExpression` whose symbol is the `ModportPortSymbol`,
73 /// not the underlying interface body variable, so we register both keys in
74 /// `valueSymbols` to make the lookup find this port's `BlockArgument`.
75 const slang::ast::Symbol *modportPortSym = nullptr;
76 /// Slot index in the module signature; see `PortLowering::outputIdx`.
77 std::optional<unsigned> outputIdx;
78 std::optional<unsigned> inputIdx;
79};
80
81/// Lowering information for an expanded interface instance. Maps each interface
82/// body member to its expanded SSA value (moore.variable or moore.net).
84 DenseMap<const slang::ast::Symbol *, Value> expandedMembers;
85 DenseMap<StringAttr, Value> expandedMembersByName;
86};
87
88/// Cached lowering information for representing SystemVerilog `virtual
89/// interface` handles as Moore types (a struct of references to interface
90/// members).
92 moore::UnpackedStructType type;
93 SmallVector<StringAttr, 8> fieldNames;
94};
95
96/// A mapping entry for resolving Slang virtual interface member accesses.
97///
98/// Slang may resolve `vif.member` expressions (where `vif` has a
99/// `VirtualInterfaceType`) directly to a `NamedValueExpression` for `member`.
100/// This table records which virtual interface base symbol that member access is
101/// rooted in, so ImportVerilog can materialize the appropriate Moore IR.
103 const slang::ast::ValueSymbol *base = nullptr;
104 /// The name of the field in the lowered virtual interface handle struct that
105 /// should be accessed for this member.
106 StringAttr fieldName;
107};
108
109/// Module lowering information.
111 moore::SVModuleOp op;
112 SmallVector<PortLowering> ports;
113 SmallVector<FlattenedIfacePort> ifacePorts;
114 DenseMap<const slang::syntax::SyntaxNode *, const slang::ast::PortSymbol *>
116 /// Number of explicit-port slots per direction. Hierarchical-name ports
117 /// are appended after these in the module signature.
118 unsigned numExplicitOutputs = 0;
119 unsigned numExplicitInputs = 0;
120};
121
122/// Function lowering information. The `op` field holds either a `func::FuncOp`
123/// (for SystemVerilog functions), a `moore::CoroutineOp` (for tasks), or a
124/// `moore::DPIFuncOp` (for DPI-imported functions), all accessed through the
125/// `FunctionOpInterface`.
127 mlir::FunctionOpInterface op;
128
129 /// The AST symbols captured by this function, determined by the capture
130 /// analysis pre-pass. These are added as extra parameters to the function
131 /// during declaration.
132 SmallVector<const slang::ast::ValueSymbol *, 4> capturedSymbols;
133
134 explicit FunctionLowering(mlir::FunctionOpInterface op) : op(op) {}
135};
136
137// Class lowering information.
139 circt::moore::ClassDeclOp op;
140 bool methodsFinalized = false;
141};
142
143/// Information about a loops continuation and exit blocks relevant while
144/// lowering the loop's body statements.
145struct LoopFrame {
146 /// The block to jump to from a `continue` statement.
148 /// The block to jump to from a `break` statement.
150};
151
152/// Hierarchical path information.
153/// The "hierName" means a different hierarchical name at different module
154/// levels.
155/// The "idx" means where the current hierarchical name is on the portlists.
156/// The "direction" means hierarchical names whether downward(In) or
157/// upward(Out).
159 mlir::StringAttr hierName;
160 std::optional<unsigned int> idx;
161 slang::ast::ArgumentDirection direction;
162
163 /// The value symbols associated with this hierarchical path. Multiple
164 /// symbols may be present when different instances resolve the same
165 /// logical variable to different elaborated symbol objects (e.g., due to
166 /// Slang's per-instance elaboration of shared module bodies).
167 llvm::SmallVector<const slang::ast::ValueSymbol *, 2> valueSyms;
168};
169
170/// ImportVerilog Elaboration Phases for Hierarchical Names
171///
172/// Hierarchical name resolution is performed in four distinct phases:
173///
174/// 1. Collection: The `traverseInstanceBody` pass walks the Slang AST to
175/// identify all hierarchical references (e.g., `Top.sub.var`). It records
176/// these in `hierPaths` mapped by module body.
177///
178/// 2. Port Generation: In `convertModuleHeader`, we inspect `hierPaths` for
179/// the module and add corresponding input/output ports to the generated
180/// `moore.module` to allow cross-module communication.
181///
182/// 3. Wiring: In `Structure.cpp` during instance creation, we look up the
183/// canonical module body's `hierPaths` to determine which hierarchical
184/// values need to be passed as inputs or captured as outputs from the
185/// instance.
186///
187/// 4. Resolution: In `Expressions.cpp`, visitors for rvalues and lvalues
188/// resolve hierarchical names by first checking the instance-aware
189/// `hierValueSymbols` map (for cross-instance references) and falling back
190/// to standard scoped lookups.
191
192// A slang::SourceLocation for deterministic comparisons. Comparisons use the
193// buffer's sortKey rather than bufferId.
195 uint64_t sortKey;
196 size_t offset;
197
198 static LocationKey get(const slang::SourceLocation &loc,
199 const slang::SourceManager &mgr) {
200 return {
201 mgr.getSortKey(loc.buffer()),
202 loc.offset(),
203 };
204 }
205
206 std::strong_ordering operator<=>(const LocationKey &) const = default;
207 bool operator==(const LocationKey &) const = default;
208};
209
210/// A helper class to facilitate the conversion from a Slang AST to MLIR
211/// operations. Keeps track of the destination MLIR module, builders, and
212/// various worklists and utilities needed for conversion.
213struct Context {
215 slang::ast::Compilation &compilation, mlir::ModuleOp intoModuleOp,
216 const slang::SourceManager &sourceManager)
219 builder(OpBuilder::atBlockEnd(intoModuleOp.getBody())),
221 Context(const Context &) = delete;
222
223 /// Return the MLIR context.
224 MLIRContext *getContext() { return intoModuleOp.getContext(); }
225
226 /// Convert a slang `SourceLocation` into an MLIR `Location`.
227 Location convertLocation(slang::SourceLocation loc);
228 /// Convert a slang `SourceRange` into an MLIR `Location`.
229 Location convertLocation(slang::SourceRange range);
230
231 /// Convert a slang type into an MLIR type. Returns null on failure. Uses the
232 /// provided location for error reporting, or tries to guess one from the
233 /// given type. Types tend to have unreliable location information, so it's
234 /// generally a good idea to pass in a location.
235 Type convertType(const slang::ast::Type &type, LocationAttr loc = {});
236 Type convertType(const slang::ast::DeclaredType &type);
237
238 /// Convert hierarchy and structure AST nodes to MLIR ops.
239 LogicalResult convertCompilation();
240 /// Convert a module and its ports to an empty module op in the IR. Also adds
241 /// the op to the worklist of module bodies to be lowered. This acts like a
242 /// module "declaration", allowing instances to already refer to a module even
243 /// before its body has been lowered.
244 /// `module` must be the canonical module body if there is one.
245 ModuleLowering *
246 convertModuleHeader(const slang::ast::InstanceBodySymbol *module);
247 /// Convert a module's body to the corresponding IR ops. The module op must
248 /// have already been created earlier through a `convertModuleHeader` call.
249 /// `module` must be the canonical module body if there is one.
250 LogicalResult convertModuleBody(const slang::ast::InstanceBodySymbol *module);
251 LogicalResult convertPackage(const slang::ast::PackageSymbol &package);
252 FunctionLowering *
253 declareFunction(const slang::ast::SubroutineSymbol &subroutine);
254 LogicalResult defineFunction(const slang::ast::SubroutineSymbol &subroutine);
255 LogicalResult
256 convertPrimitiveInstance(const slang::ast::PrimitiveInstanceSymbol &prim);
257 ClassLowering *declareClass(const slang::ast::ClassType &cls);
258 LogicalResult buildClassProperties(const slang::ast::ClassType &classdecl);
259 LogicalResult materializeClassMethods(const slang::ast::ClassType &classdecl);
260 LogicalResult convertGlobalVariable(const slang::ast::VariableSymbol &var);
261
262 /// Convert a Slang virtual interface type into the Moore type used to
263 /// represent virtual interface handles. Populates internal caches so that
264 /// interface instance references can be materialized consistently.
265 FailureOr<moore::UnpackedStructType>
266 convertVirtualInterfaceType(const slang::ast::VirtualInterfaceType &type,
267 Location loc);
268
269 /// Materialize a Moore value representing a concrete interface instance as a
270 /// virtual interface handle. This only succeeds for the Slang
271 /// `VirtualInterfaceType` wrappers that refer to a real interface instance
272 /// (`isRealIface`).
273 FailureOr<Value>
274 materializeVirtualInterfaceValue(const slang::ast::VirtualInterfaceType &type,
275 Location loc);
276
277 /// Register the interface members of a virtual interface base symbol for use
278 /// in later expression conversion.
279 LogicalResult
280 registerVirtualInterfaceMembers(const slang::ast::ValueSymbol &base,
281 const slang::ast::VirtualInterfaceType &type,
282 Location loc);
283
284 /// Checks whether one class (actualTy) is derived from another class
285 /// (baseTy). True if it's a subclass, false otherwise.
286 bool isClassDerivedFrom(const moore::ClassHandleType &actualTy,
287 const moore::ClassHandleType &baseTy);
288
289 /// Tries to find the closest base class of actualTy that carries a property
290 /// with name fieldName. The location is used for error reporting.
291 moore::ClassHandleType
292 getAncestorClassWithProperty(const moore::ClassHandleType &actualTy,
293 StringRef fieldName, Location loc);
294
295 Value getImplicitThisRef() const {
296 return currentThisRef; // block arg added in declareFunction
297 }
298
299 /// Maps assertion system calls to their corresponding clocks
300 DenseMap<const slang::ast::CallExpression *,
301 const slang::ast::TimingControl *>
303
304 /// Generates a map from assertions to clocks using Slang's analysis
306
307 Value getIndexedQueue() const { return currentQueue; }
308
309 // Convert a statement AST node to MLIR ops.
310 LogicalResult convertStatement(const slang::ast::Statement &stmt);
311
312 // Convert an expression AST node to MLIR ops.
313 Value convertRvalueExpression(const slang::ast::Expression &expr,
314 Type requiredType = {});
315 Value convertLvalueExpression(const slang::ast::Expression &expr);
316
317 // Convert an assertion expression AST node to MLIR ops.
318 Value convertAssertionExpression(const slang::ast::AssertionExpr &expr,
319 Location loc);
320
321 // Convert an assertion expression AST node to MLIR ops.
323 const slang::ast::CallExpression &expr,
324 const slang::ast::CallExpression::SystemCallInfo &info, Location loc);
325
326 // Traverse the whole AST to collect hierarchical names.
327 void traverseInstanceBody(const slang::ast::InstanceSymbol &symbol);
328
329 /// Build a composite key for hierValueSymbols from a hierarchical value
330 /// expression. Returns {firstInstanceSymbol, dottedHierName} or std::nullopt
331 /// if the expression has no instance path.
332 std::optional<std::pair<const slang::ast::InstanceSymbol *, mlir::StringAttr>>
333 buildHierValueKey(const slang::ast::HierarchicalValueExpression &expr);
334
335 // Convert timing controls into a corresponding set of ops that delay
336 // execution of the current block. Produces an error if the implicit event
337 // control `@*` or `@(*)` is used.
338 LogicalResult convertTimingControl(const slang::ast::TimingControl &ctrl);
339 // Convert timing controls into a corresponding set of ops that delay
340 // execution of the current block. Then converts the given statement, taking
341 // note of the rvalues it reads and adding them to a wait op in case an
342 // implicit event control `@*` or `@(*)` is used.
343 LogicalResult convertTimingControl(const slang::ast::TimingControl &ctrl,
344 const slang::ast::Statement &stmt);
345
346 /// Helper function to convert a value to a MLIR I1 value.
347 Value convertToI1(Value value);
348
349 // Convert a slang timing control for LTL
350 Value convertLTLTimingControl(const slang::ast::TimingControl &ctrl,
351 const Value &seqOrPro);
352
353 LogicalResult
354 convertNInputPrimitive(const slang::ast::PrimitiveInstanceSymbol &prim);
355
356 LogicalResult
357 convertNOutputPrimitive(const slang::ast::PrimitiveInstanceSymbol &prim);
358
359 LogicalResult
360 convertFixedPrimitive(const slang::ast::PrimitiveInstanceSymbol &prim);
361
362 LogicalResult
363 convertPullGatePrimitive(const slang::ast::PrimitiveInstanceSymbol &prim);
364
365 /// Helper function to convert a value to its "truthy" boolean value.
366 Value convertToBool(Value value);
367
368 /// Helper function to convert a value to its "truthy" boolean value and
369 /// convert it to the given domain.
370 Value convertToBool(Value value, Domain domain);
371
372 /// Helper function to convert a value to its simple bit vector
373 /// representation, if it has one. Otherwise returns null. Also returns null
374 /// if the given value is null.
375 Value convertToSimpleBitVector(Value value);
376
377 /// Helper function to insert the necessary operations to cast a value from
378 /// one type to another. If `fallible` is true, conversion failures are
379 /// reported by returning a null value without emitting diagnostics.
380 Value materializeConversion(Type type, Value value, bool isSigned,
381 Location loc, bool fallible = false);
382
383 /// Helper function to materialize an `SVInt` as an SSA value.
384 Value materializeSVInt(const slang::SVInt &svint,
385 const slang::ast::Type &type, Location loc);
386
387 /// Helper function to materialize a real value as an SSA value.
388 Value materializeSVReal(const slang::ConstantValue &svreal,
389 const slang::ast::Type &type, Location loc);
390
391 /// Helper function to materialize a string as an SSA value.
392 Value materializeString(const slang::ConstantValue &string,
393 const slang::ast::Type &astType, Location loc);
394
395 /// Helper function to materialize an unpacked array of `SVInt`s as an SSA
396 /// value.
398 const slang::ConstantValue &constant,
399 const slang::ast::FixedSizeUnpackedArrayType &astType, Location loc);
400
401 /// Helper function to materialize a `ConstantValue` as an SSA value. Returns
402 /// null if the constant cannot be materialized.
403 Value materializeConstant(const slang::ConstantValue &constant,
404 const slang::ast::Type &type, Location loc);
405
406 /// Convert a list of string literal arguments with formatting specifiers and
407 /// arguments to be interpolated into a `!moore.format_string` value. Returns
408 /// failure if an error occurs. Returns a null value if the formatted string
409 /// is trivially empty. Otherwise returns the formatted string.
410 FailureOr<Value> convertFormatString(
411 std::span<const slang::ast::Expression *const> arguments, Location loc,
412 moore::IntFormat defaultFormat = moore::IntFormat::Decimal,
413 bool appendNewline = false);
414
415 /// Convert system function calls. Returns a null `Value` on failure after
416 /// emitting an error.
417 Value convertSystemCall(const slang::ast::SystemSubroutine &subroutine,
418 Location loc,
419 std::span<const slang::ast::Expression *const> args);
420
421 /// Convert system function calls within properties and assertion with a
422 /// single argument.
423 FailureOr<Value> convertAssertionSystemCallArity1(
424 const slang::ast::SystemSubroutine &subroutine, Location loc, Value value,
425 Type originalType, Value clockVal);
426
427 /// Evaluate the constant value of an expression.
428 slang::ConstantValue evaluateConstant(const slang::ast::Expression &expr);
429
430 /// Convert the inside/set-membership expression.
431 Value convertInsideCheck(Value insideLhs, Location loc,
432 const slang::ast::Expression &expr);
433
435 slang::ast::Compilation &compilation;
436 mlir::ModuleOp intoModuleOp;
437 const slang::SourceManager &sourceManager;
438
439 /// The builder used to create IR operations.
440 OpBuilder builder;
441 /// A symbol table of the MLIR module we are emitting into.
442 SymbolTable symbolTable;
443
444 /// The top-level operations ordered by their Slang source location. This is
445 /// used to produce IR that follows the source file order.
446 std::map<LocationKey, Operation *> orderedRootOps;
447
448 /// How we have lowered modules to MLIR.
449 /// The keys must be the slang canonical module bodies where they exist.
450 DenseMap<const slang::ast::InstanceBodySymbol *,
451 std::unique_ptr<ModuleLowering>>
453
454 /// Expanded interface instances, keyed by the InstanceSymbol pointer.
455 /// Each entry maps body members to their expanded SSA values. Scoped
456 /// per-module so entries are cleaned up when a module's conversion ends.
458 llvm::ScopedHashTable<const slang::ast::InstanceSymbol *,
460 using InterfaceInstanceScope = InterfaceInstances::ScopeTy;
462 /// Owning storage for InterfaceLowering objects
463 /// because ScopedHashTable stores values by copy.
464 SmallVector<std::unique_ptr<InterfaceLowering>> interfaceInstanceStorage;
465
466 /// Module instances already emitted by the predeclaration pass. These are
467 /// skipped during the later source-order module-body walk to avoid emitting
468 /// duplicate instances.
469 DenseSet<const slang::ast::InstanceSymbol *> predeclaredInstances;
470
471 /// Cached virtual interface layouts (type + field order).
472 DenseMap<const slang::ast::InstanceBodySymbol *, VirtualInterfaceLowering>
474 DenseMap<const slang::ast::ModportSymbol *, VirtualInterfaceLowering>
476 /// A list of modules for which the header has been created, but the body has
477 /// not been converted yet.
478 std::queue<const slang::ast::InstanceBodySymbol *> moduleWorklist;
479
480 /// A list of functions for which the declaration has been created, but the
481 /// body has not been defined yet.
482 std::queue<const slang::ast::SubroutineSymbol *> functionWorklist;
483
484 /// Functions that have already been converted.
485 DenseMap<const slang::ast::SubroutineSymbol *,
486 std::unique_ptr<FunctionLowering>>
488
489 /// DPI-C export directives keyed by the SystemVerilog subroutine they expose.
490 DenseMap<const slang::ast::SubroutineSymbol *, std::string> dpiExportCNames;
491
492 /// Classes that have already been converted.
493 DenseMap<const slang::ast::ClassType *, std::unique_ptr<ClassLowering>>
495
496 /// A table of defined values, such as variables, that may be referred to by
497 /// name in expressions. The expressions use this table to lookup the MLIR
498 /// value that was created for a given declaration in the Slang AST node.
500 llvm::ScopedHashTable<const slang::ast::ValueSymbol *, Value>;
501 using ValueSymbolScope = ValueSymbols::ScopeTy;
503
504 /// A table mapping symbols for interface members accessed through a virtual
505 /// interface to the virtual interface base value symbol.
507 llvm::ScopedHashTable<const slang::ast::ValueSymbol *,
509 using VirtualInterfaceMemberScope = VirtualInterfaceMembers::ScopeTy;
511
512 /// A table of defined global variables that may be referred to by name in
513 /// expressions.
514 DenseMap<const slang::ast::ValueSymbol *, moore::GlobalVariableOp>
516 /// A list of global variables that still need their initializers to be
517 /// converted.
518 SmallVector<const slang::ast::ValueSymbol *> globalVariableWorklist;
519
520 /// Pre-computed capture analysis: maps each function to the set of non-local,
521 /// non-global variables it captures (directly or transitively).
523
524 /// Collect all hierarchical names used for the per module/instance.
525 /// The keys are the slang canonical instance bodies (or the real instance
526 /// if slang's getCanonicalBody() returned null).
527 DenseMap<const slang::ast::InstanceBodySymbol *, SmallVector<HierPathInfo>>
529
530 /// Persistent map for hierarchical value lookups. Keyed by a composite
531 /// of the instance symbol (the specific instance being wired, e.g., p1 or
532 /// p2) and the hierarchical path name (e.g., "child.child_val"). This
533 /// ensures instance-specific resolution even when Slang shares or doesn't
534 /// share instance bodies across multiple instances of the same module.
535 DenseMap<std::pair<const slang::ast::InstanceSymbol *, mlir::StringAttr>,
536 Value>
538
539 /// A stack of assignment left-hand side values. Each assignment will push its
540 /// lowered left-hand side onto this stack before lowering its right-hand
541 /// side. This allows expressions to resolve the opaque
542 /// `LValueReferenceExpression`s in the AST.
543 SmallVector<Value> lvalueStack;
544
545 /// A stack of loop continuation and exit blocks. Each loop will push the
546 /// relevant info onto this stack, lower its loop body statements, and pop the
547 /// info off the stack again. Continue and break statements encountered as
548 /// part of the loop body statements will use this information to branch to
549 /// the correct block.
550 SmallVector<LoopFrame> loopStack;
551
552 /// A listener called for every variable or net being read. This can be used
553 /// to collect all variables read as part of an expression or statement, for
554 /// example to populate the list of observed signals in an implicit event
555 /// control `@*`.
556 std::function<void(moore::ReadOp)> rvalueReadCallback;
557 /// A listener called for every variable or net being assigned. This can be
558 /// used to collect all variables assigned in a task scope.
559 std::function<void(mlir::Operation *)> variableAssignCallback;
560
561 /// Whether we are currently converting expressions inside a timing control,
562 /// such as `@(posedge clk)`. This is used by the implicit event control
563 /// callback to avoid adding reads from explicit event controls to the
564 /// implicit sensitivity list.
566
567 /// The time scale currently in effect.
568 slang::TimeScale timeScale;
569
570 /// Variable to track the value of the current function's implicit `this`
571 /// reference
572 Value currentThisRef = {};
573
574 /// Variable that tracks the queue which we are currently converting the index
575 /// expression for. This is necessary to implement the `$` operator, which
576 /// returns the index of the last element of the queue.
577 Value currentQueue = {};
578
579 /// Ensure that the global variables for `$monitor` state exist. This creates
580 /// the `__monitor_active_id` and `__monitor_enabled` globals on first call.
582
583 /// Process any pending `$monitor` calls and generate the monitoring
584 /// procedures at module level.
585 LogicalResult flushPendingMonitors();
586
587 /// Global variable ops for `$monitor` state management. These are created on
588 /// demand by `ensureMonitorGlobals()`.
589 moore::GlobalVariableOp monitorActiveIdGlobal = nullptr;
590 moore::GlobalVariableOp monitorEnabledGlobal = nullptr;
591
592 /// The next monitor ID to allocate. ID 0 is reserved for "no monitor active".
593 unsigned nextMonitorId = 1;
594
595 /// Information about a pending `$monitor` call that needs to be converted
596 /// after the current module's body has been processed.
598 unsigned id;
599 Location loc;
600 const slang::ast::CallExpression *call;
601 };
602
603 /// Pending `$monitor` calls that need to be converted at module level.
604 SmallVector<PendingMonitor> pendingMonitors;
605
606private:
607 /// Helper function to extract the commonalities in lowering of functions and
608 /// methods
610 declareCallableImpl(const slang::ast::SubroutineSymbol &subroutine,
611 mlir::StringRef qualifiedName,
612 llvm::SmallVectorImpl<Type> &extraParams);
613};
614
615} // namespace ImportVerilog
616} // namespace circt
617#endif // CONVERSION_IMPORTVERILOG_IMPORTVERILOGINTERNALS_H
const slang::ast::InstanceBodySymbol * getCanonicalBody(const slang::ast::InstanceSymbol &inst)
Get the slang canonical body for the given instance, if there is one.
DenseMap< const slang::ast::SubroutineSymbol *, SmallSetVector< const slang::ast::ValueSymbol *, 4 > > CaptureMap
The result of capture analysis: for each function, the set of non-local, non-global variable symbols ...
Domain
The number of values each bit of a type can assume.
Definition MooreTypes.h:50
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Options that control how Verilog input files are parsed and processed.
Information about a pending $monitor call that needs to be converted after the current module's body ...
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
Value convertToI1(Value value)
Helper function to convert a value to a MLIR I1 value.
llvm::ScopedHashTable< const slang::ast::InstanceSymbol *, InterfaceLowering * > InterfaceInstances
Expanded interface instances, keyed by the InstanceSymbol pointer.
FunctionLowering * declareCallableImpl(const slang::ast::SubroutineSymbol &subroutine, mlir::StringRef qualifiedName, llvm::SmallVectorImpl< Type > &extraParams)
Helper function to extract the commonalities in lowering of functions and methods.
ModuleLowering * convertModuleHeader(const slang::ast::InstanceBodySymbol *module)
Convert a module and its ports to an empty module op in the IR.
std::queue< const slang::ast::SubroutineSymbol * > functionWorklist
A list of functions for which the declaration has been created, but the body has not been defined yet...
Value convertLvalueExpression(const slang::ast::Expression &expr)
LogicalResult registerVirtualInterfaceMembers(const slang::ast::ValueSymbol &base, const slang::ast::VirtualInterfaceType &type, Location loc)
Register the interface members of a virtual interface base symbol for use in later expression convers...
Definition Types.cpp:474
Value materializeConstant(const slang::ConstantValue &constant, const slang::ast::Type &type, Location loc)
Helper function to materialize a ConstantValue as an SSA value.
SmallVector< LoopFrame > loopStack
A stack of loop continuation and exit blocks.
LogicalResult convertModuleBody(const slang::ast::InstanceBodySymbol *module)
Convert a module's body to the corresponding IR ops.
slang::ConstantValue evaluateConstant(const slang::ast::Expression &expr)
Evaluate the constant value of an expression.
SmallVector< PendingMonitor > pendingMonitors
Pending $monitor calls that need to be converted at module level.
Value convertInsideCheck(Value insideLhs, Location loc, const slang::ast::Expression &expr)
Convert the inside/set-membership expression.
Value convertLTLTimingControl(const slang::ast::TimingControl &ctrl, const Value &seqOrPro)
LogicalResult materializeClassMethods(const slang::ast::ClassType &classdecl)
DenseMap< const slang::ast::ValueSymbol *, moore::GlobalVariableOp > globalVariables
A table of defined global variables that may be referred to by name in expressions.
slang::ast::Compilation & compilation
LogicalResult convertTimingControl(const slang::ast::TimingControl &ctrl)
DenseMap< const slang::ast::InstanceBodySymbol *, VirtualInterfaceLowering > virtualIfaceLowerings
Cached virtual interface layouts (type + field order).
LogicalResult flushPendingMonitors()
Process any pending $monitor calls and generate the monitoring procedures at module level.
LogicalResult convertNInputPrimitive(const slang::ast::PrimitiveInstanceSymbol &prim)
llvm::ScopedHashTable< const slang::ast::ValueSymbol *, VirtualInterfaceMemberAccess > VirtualInterfaceMembers
A table mapping symbols for interface members accessed through a virtual interface to the virtual int...
Context(const ImportVerilogOptions &options, slang::ast::Compilation &compilation, mlir::ModuleOp intoModuleOp, const slang::SourceManager &sourceManager)
OpBuilder builder
The builder used to create IR operations.
std::queue< const slang::ast::InstanceBodySymbol * > moduleWorklist
A list of modules for which the header has been created, but the body has not been converted yet.
LogicalResult convertGlobalVariable(const slang::ast::VariableSymbol &var)
Convert a variable to a moore.global_variable operation.
Value materializeFixedSizeUnpackedArrayType(const slang::ConstantValue &constant, const slang::ast::FixedSizeUnpackedArrayType &astType, Location loc)
Helper function to materialize an unpacked array of SVInts as an SSA value.
Value convertAssertionCallExpression(const slang::ast::CallExpression &expr, const slang::ast::CallExpression::SystemCallInfo &info, Location loc)
FailureOr< Value > convertAssertionSystemCallArity1(const slang::ast::SystemSubroutine &subroutine, Location loc, Value value, Type originalType, Value clockVal)
Convert system function calls within properties and assertion with a single argument.
DenseSet< const slang::ast::InstanceSymbol * > predeclaredInstances
Module instances already emitted by the predeclaration pass.
CaptureMap functionCaptures
Pre-computed capture analysis: maps each function to the set of non-local, non-global variables it ca...
DenseMap< const slang::ast::ClassType *, std::unique_ptr< ClassLowering > > classes
Classes that have already been converted.
std::function< void(moore::ReadOp)> rvalueReadCallback
A listener called for every variable or net being read.
bool isClassDerivedFrom(const moore::ClassHandleType &actualTy, const moore::ClassHandleType &baseTy)
Checks whether one class (actualTy) is derived from another class (baseTy).
Context(const Context &)=delete
Value convertSystemCall(const slang::ast::SystemSubroutine &subroutine, Location loc, std::span< const slang::ast::Expression *const > args)
Convert system function calls.
Type convertType(const slang::ast::Type &type, LocationAttr loc={})
Convert a slang type into an MLIR type.
Definition Types.cpp:224
DenseMap< const slang::ast::SubroutineSymbol *, std::unique_ptr< FunctionLowering > > functions
Functions that have already been converted.
Value materializeSVInt(const slang::SVInt &svint, const slang::ast::Type &type, Location loc)
Helper function to materialize an SVInt as an SSA value.
slang::TimeScale timeScale
The time scale currently in effect.
DenseMap< std::pair< const slang::ast::InstanceSymbol *, mlir::StringAttr >, Value > hierValueSymbols
Persistent map for hierarchical value lookups.
Value materializeSVReal(const slang::ConstantValue &svreal, const slang::ast::Type &type, Location loc)
Helper function to materialize a real value as an SSA value.
ClassLowering * declareClass(const slang::ast::ClassType &cls)
VirtualInterfaceMembers::ScopeTy VirtualInterfaceMemberScope
Value convertToBool(Value value)
Helper function to convert a value to its "truthy" boolean value.
LogicalResult convertFixedPrimitive(const slang::ast::PrimitiveInstanceSymbol &prim)
std::function< void(mlir::Operation *)> variableAssignCallback
A listener called for every variable or net being assigned.
DenseMap< const slang::ast::SubroutineSymbol *, std::string > dpiExportCNames
DPI-C export directives keyed by the SystemVerilog subroutine they expose.
moore::ClassHandleType getAncestorClassWithProperty(const moore::ClassHandleType &actualTy, StringRef fieldName, Location loc)
Tries to find the closest base class of actualTy that carries a property with name fieldName.
void ensureMonitorGlobals()
Ensure that the global variables for $monitor state exist.
FailureOr< Value > convertFormatString(std::span< const slang::ast::Expression *const > arguments, Location loc, moore::IntFormat defaultFormat=moore::IntFormat::Decimal, bool appendNewline=false)
Convert a list of string literal arguments with formatting specifiers and arguments to be interpolate...
const ImportVerilogOptions & options
Value convertRvalueExpression(const slang::ast::Expression &expr, Type requiredType={})
SmallVector< std::unique_ptr< InterfaceLowering > > interfaceInstanceStorage
Owning storage for InterfaceLowering objects because ScopedHashTable stores values by copy.
llvm::ScopedHashTable< const slang::ast::ValueSymbol *, Value > ValueSymbols
A table of defined values, such as variables, that may be referred to by name in expressions.
Value convertToSimpleBitVector(Value value)
Helper function to convert a value to its simple bit vector representation, if it has one.
moore::GlobalVariableOp monitorActiveIdGlobal
Global variable ops for $monitor state management.
VirtualInterfaceMembers virtualIfaceMembers
Value materializeString(const slang::ConstantValue &string, const slang::ast::Type &astType, Location loc)
Helper function to materialize a string as an SSA value.
moore::GlobalVariableOp monitorEnabledGlobal
DenseMap< const slang::ast::ModportSymbol *, VirtualInterfaceLowering > virtualIfaceModportLowerings
Value currentThisRef
Variable to track the value of the current function's implicit this reference.
const slang::SourceManager & sourceManager
Value materializeConversion(Type type, Value value, bool isSigned, Location loc, bool fallible=false)
Helper function to insert the necessary operations to cast a value from one type to another.
void traverseInstanceBody(const slang::ast::InstanceSymbol &symbol)
void populateAssertionClocks()
Generates a map from assertions to clocks using Slang's analysis.
Value currentQueue
Variable that tracks the queue which we are currently converting the index expression for.
std::map< LocationKey, Operation * > orderedRootOps
The top-level operations ordered by their Slang source location.
DenseMap< const slang::ast::CallExpression *, const slang::ast::TimingControl * > assertionCallClocks
Maps assertion system calls to their corresponding clocks.
InterfaceInstances::ScopeTy InterfaceInstanceScope
FailureOr< Value > materializeVirtualInterfaceValue(const slang::ast::VirtualInterfaceType &type, Location loc)
Materialize a Moore value representing a concrete interface instance as a virtual interface handle.
Definition Types.cpp:357
LogicalResult convertPrimitiveInstance(const slang::ast::PrimitiveInstanceSymbol &prim)
Convert a primitive instance.
SymbolTable symbolTable
A symbol table of the MLIR module we are emitting into.
DenseMap< const slang::ast::InstanceBodySymbol *, SmallVector< HierPathInfo > > hierPaths
Collect all hierarchical names used for the per module/instance.
bool isInsideTimingControl
Whether we are currently converting expressions inside a timing control, such as @(posedge clk).
FunctionLowering * declareFunction(const slang::ast::SubroutineSymbol &subroutine)
Convert a function and its arguments to a function declaration in the IR.
LogicalResult convertNOutputPrimitive(const slang::ast::PrimitiveInstanceSymbol &prim)
Value convertAssertionExpression(const slang::ast::AssertionExpr &expr, Location loc)
LogicalResult buildClassProperties(const slang::ast::ClassType &classdecl)
std::optional< std::pair< const slang::ast::InstanceSymbol *, mlir::StringAttr > > buildHierValueKey(const slang::ast::HierarchicalValueExpression &expr)
Build a composite key for hierValueSymbols from a hierarchical value expression.
LogicalResult convertPackage(const slang::ast::PackageSymbol &package)
Convert a package and its contents.
LogicalResult convertCompilation()
Convert hierarchy and structure AST nodes to MLIR ops.
MLIRContext * getContext()
Return the MLIR context.
LogicalResult defineFunction(const slang::ast::SubroutineSymbol &subroutine)
Define a function’s body.
unsigned nextMonitorId
The next monitor ID to allocate. ID 0 is reserved for "no monitor active".
LogicalResult convertPullGatePrimitive(const slang::ast::PrimitiveInstanceSymbol &prim)
LogicalResult convertStatement(const slang::ast::Statement &stmt)
SmallVector< const slang::ast::ValueSymbol * > globalVariableWorklist
A list of global variables that still need their initializers to be converted.
FailureOr< moore::UnpackedStructType > convertVirtualInterfaceType(const slang::ast::VirtualInterfaceType &type, Location loc)
Convert a Slang virtual interface type into the Moore type used to represent virtual interface handle...
Definition Types.cpp:238
SmallVector< Value > lvalueStack
A stack of assignment left-hand side values.
DenseMap< const slang::ast::InstanceBodySymbol *, std::unique_ptr< ModuleLowering > > modules
How we have lowered modules to MLIR.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.
Lowering information for a single signal flattened from an interface port.
const slang::ast::InterfacePortSymbol * origin
the origin interface port symbol this was flattened from.
const slang::ast::Symbol * modportPortSym
For modport-typed iface ports, the ModportPortSymbol this was flattened from.
const slang::ast::Symbol * bodySym
the interface body member (VariableSymbol , NetSymbol)
std::optional< unsigned > outputIdx
Slot index in the module signature; see PortLowering::outputIdx.
const slang::ast::InstanceSymbol * ifaceInstance
The connected interface instance backing this port (if any).
SmallVector< const slang::ast::ValueSymbol *, 4 > capturedSymbols
The AST symbols captured by this function, determined by the capture analysis pre-pass.
FunctionLowering(mlir::FunctionOpInterface op)
slang::ast::ArgumentDirection direction
llvm::SmallVector< const slang::ast::ValueSymbol *, 2 > valueSyms
The value symbols associated with this hierarchical path.
Lowering information for an expanded interface instance.
DenseMap< const slang::ast::Symbol *, Value > expandedMembers
DenseMap< StringAttr, Value > expandedMembersByName
ImportVerilog Elaboration Phases for Hierarchical Names.
bool operator==(const LocationKey &) const =default
static LocationKey get(const slang::SourceLocation &loc, const slang::SourceManager &mgr)
std::strong_ordering operator<=>(const LocationKey &) const =default
Information about a loops continuation and exit blocks relevant while lowering the loop's body statem...
Block * breakBlock
The block to jump to from a break statement.
Block * continueBlock
The block to jump to from a continue statement.
DenseMap< const slang::syntax::SyntaxNode *, const slang::ast::PortSymbol * > portsBySyntaxNode
SmallVector< FlattenedIfacePort > ifacePorts
unsigned numExplicitOutputs
Number of explicit-port slots per direction.
std::optional< unsigned > outputIdx
Slot index in the module signature.
const slang::ast::PortSymbol & ast
Cached lowering information for representing SystemVerilog virtual interface handles as Moore types (...
A mapping entry for resolving Slang virtual interface member accesses.
StringAttr fieldName
The name of the field in the lowered virtual interface handle struct that should be accessed for this...