CIRCT 24.0.0git
Loading...
Searching...
No Matches
Structure.cpp File Reference
#include "ImportVerilogInternals.h"
#include "circt/Dialect/SV/SVAttributes.h"
#include "circt/Support/FVInt.h"
#include "slang/ast/Compilation.h"
#include "slang/ast/symbols/ClassSymbols.h"
#include "slang/ast/symbols/MemberSymbols.h"
#include "slang/syntax/AllSyntax.h"
#include "slang/syntax/SyntaxKind.h"
#include "slang/syntax/SyntaxVisitor.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/ScopeExit.h"
Include dependency graph for Structure.cpp:

Go to the source code of this file.

Functions

static void recordDPIExportDirectives (Context &context, const slang::ast::Scope &scope, const slang::syntax::SyntaxNode *syntax)
 Record export "DPI-C" directives in the given scope so that callable declarations can be tagged with the exported C name.
static void guessNamespacePrefix (const slang::ast::Symbol &symbol, SmallString< 64 > &prefix)
static moore::ProcedureKind convertProcedureKind (slang::ast::ProceduralBlockKind kind)
static moore::NetKind convertNetKind (slang::ast::NetType::NetKind kind)
static FunctionType getFunctionSignature (Context &context, const slang::ast::SubroutineSymbol &subroutine, ArrayRef< Type > prefixParams, ArrayRef< Type > suffixParams={})
 Helper function to generate the function signature from a SubroutineSymbol and optional extra arguments (used for this argument).
static FailureOr< SmallVector< moore::DPIArgInfo > > getDPISignature (Context &context, const slang::ast::SubroutineSymbol &subroutine)
static Value collapseZToX (OpBuilder &builder, Location loc, Value data, Type dstType)
 Yields data unless it is exactly Z, in which case yields X instead.
static std::optional< UdpLevel > decodeUdpLevel (char c)
 Decode a single UDP level character (0, 1, x, ?, or b, case-insensitive).
static bool parseInputRow (StringRef inputs, SmallVectorImpl< UdpLevel > &levels)
 Parse a level-only UDP input row string into UdpLevel entries.
static bool skipUnsupportedCombinationalRow (const slang::ast::PrimitiveSymbol::TableEntry &entry, ArrayRef< UdpLevel > levels, size_t numInputs)
 Skip unsupported or non-productive combinational table rows.
static Value buildLevelLiteral (OpBuilder &builder, Location loc, UdpLevel level, Value val, Value &notVal)
 Materialize a 2-state literal (val for One, ~val for Zero) or null for DontCare, caching the negation in notVal.
static Value buildProductiveTerm (OpBuilder &builder, Location loc, ArrayRef< UdpLevel > inputs, ValueRange inputVals, SmallVectorImpl< Value > &notVars)
 Build the productive AND-term for this row.
static LogicalResult classifySequentialTableRows (const slang::ast::PrimitiveSymbol &primitiveType, size_t numInputs, Location loc, SequentialTableInfo &info)
 Classify sequential UDP truth table rows into synchronous (edge-sensitive) transitions and asynchronous (level-sensitive) transitions.
static AsyncConditions buildAsyncConditions (OpBuilder &builder, Location loc, ArrayRef< AsyncRow > asyncRows, ArrayRef< Value > inputVals, Value qVal, moore::IntType i1Ty)
 Construct boolean conditions from level-sensitive (AsyncRow) rows by partitioning them into rows driving '0' (clearCond) and rows driving '1' (presetCond).
static void emitSequentialLevelSensitiveProcedure (Context &context, OpBuilder &builder, Location loc, Value asyncCond, Value asyncVal, Value qVar)
 Emit a level-sensitive moore.procedure always_latch for a latch UDP.

Variables

static constexpr StringLiteral dpiExportAttrName = "circt.dpi.export"

Function Documentation

◆ buildAsyncConditions()

AsyncConditions buildAsyncConditions ( OpBuilder & builder,
Location loc,
ArrayRef< AsyncRow > asyncRows,
ArrayRef< Value > inputVals,
Value qVal,
moore::IntType i1Ty )
static

Construct boolean conditions from level-sensitive (AsyncRow) rows by partitioning them into rows driving '0' (clearCond) and rows driving '1' (presetCond).

Because Slang's table conflict check (BitTrie) guarantees that '0'-output and '1'-output rows are mutually exclusive for any 2-state input/state combination, when both '0' and '1' rows exist (e.g. D or SR latches), they combine into a single enable condition (asyncCond = clearCond | presetCond) with dynamic value (asyncVal = presetCond). When only '0' or only '1' rows exist, asyncVal is a constant 0 or 1.

Definition at line 3272 of file Structure.cpp.

References buildLevelLiteral(), and buildProductiveTerm().

Referenced by circt::ImportVerilog::Context::convertSequentialUserDefinedPrimitiveDef().

◆ buildLevelLiteral()

Value buildLevelLiteral ( OpBuilder & builder,
Location loc,
UdpLevel level,
Value val,
Value & notVal )
static

Materialize a 2-state literal (val for One, ~val for Zero) or null for DontCare, caching the negation in notVal.

Definition at line 2958 of file Structure.cpp.

Referenced by buildAsyncConditions(), and buildProductiveTerm().

◆ buildProductiveTerm()

Value buildProductiveTerm ( OpBuilder & builder,
Location loc,
ArrayRef< UdpLevel > inputs,
ValueRange inputVals,
SmallVectorImpl< Value > & notVars )
static

Build the productive AND-term for this row.

Returns nullptr if the row is unproductive (all don't cares).

Definition at line 2976 of file Structure.cpp.

References buildLevelLiteral().

Referenced by buildAsyncConditions(), and circt::ImportVerilog::Context::convertUserDefinedPrimitiveDef().

◆ classifySequentialTableRows()

LogicalResult classifySequentialTableRows ( const slang::ast::PrimitiveSymbol & primitiveType,
size_t numInputs,
Location loc,
SequentialTableInfo & info )
static

Classify sequential UDP truth table rows into synchronous (edge-sensitive) transitions and asynchronous (level-sensitive) transitions.

Sequential UDPs in hardware are modeled either as edge-triggered flip-flops (where edge-sensitive rows syncRows describe clocked transitions on the edge-sensitive port and level-sensitive rows asyncRows describe asynchronous clear/preset overrides on asynchronous control ports) or as level-sensitive latches (where only asyncRows are present).

Safe Filtering and Benign Hold Rules: Truth tables may include entries that do not produce active 2-state transitions and are skipped before lowering:

  • Unknown ('x') rows: Like combinational UDP lowering (skipUnsupportedCombinationalRow), sequential UDP lowering models transitions in 2-state i1 logic (matching MooreToCore's 2-state lowering rather than 4-state X propagation). In IEEE 1800-2017 Table 29-1, 'x' in inputs or state only matches literal unknown x (unlike '?', which matches 0, 1, or x) so it cannot trigger in 2-state logic, and 'x' next-state outputs have no 2-state value, so rows containing 'x' are skipped.
  • Explicit hold rows: Rows specifying '-' as next-state explicitly indicate retaining the current state (e.g. on inactive clock edges (10) : -). Because the lowered moore.procedure only assigns to the state variable when an active transition condition holds, the state variable naturally retains its value when no active transition matches.
  • Idempotent hold rows: Rows where next-state matches current state (state == output, e.g. steady-state hold rows 0 : 0 : 0 or 1 : 1 : 1) preserve state and do not create active transitions.

Structural Lowering Requirements: Pure level-sensitive UDPs (latches) have no structural restrictions beyond Slang's table validation. For edge-triggered UDPs, while IEEE 1800-2017 Section 29.6 syntactically permits arbitrary combinations of level-sensitive and edge-sensitive rows, lowering to a structured flip-flop procedure (with outer asynchronous clear/preset priority branches and an inner clocked data branch) requires surviving active transitions to satisfy:

  1. Well-formed edge transitions (guaranteed by Slang and asserted here): a. At most one edge transition per table row. b. Supported transition specifiers: each edge transition must specify a recognized 2-state edge token.
  2. Single edge-sensitive port: all active edge transitions must share the same edge-sensitive input port.
  3. No mixed sensitivity on edge-sensitive port: an input cannot have both edge and level sensitivity (in an edge-triggered model, all active level rows must specify '?' on the edge-sensitive port).
  4. Pure asynchronous control: in an edge-triggered UDP, level-sensitive (asynchronous clear/preset) rows must not depend on the current state q (all active level rows must specify '?' on state).
  5. Separation of asynchronous control and synchronous data: a. Asynchronous control ports (input ports with a '0' or '1' level in asyncRows) cannot also act as varying synchronous data inputs across syncRows. b. Synchronous transitions (syncRows) cannot be conditioned on active asynchronous control levels.

Definition at line 3045 of file Structure.cpp.

References assert(), decodeUdpLevel(), parseInputRow(), and success().

Referenced by circt::ImportVerilog::Context::convertSequentialUserDefinedPrimitiveDef().

◆ collapseZToX()

Value collapseZToX ( OpBuilder & builder,
Location loc,
Value data,
Type dstType )
static

Yields data unless it is exactly Z, in which case yields X instead.

Per IEEE 1800-2023 Section 28.6's table 28-5 for three-state gates, a Z data input yields X on either the actively-driven or ambiguous-enable path. Known 0/1 data values pass through unchanged. For three-state gates this depends on the assumption that we collapse L to 0 and H to 1.

Definition at line 2545 of file Structure.cpp.

References circt::FVInt::getAllX(), and circt::FVInt::getAllZ().

Referenced by circt::ImportVerilog::Context::convertThreeStateGatePrimitive().

◆ convertNetKind()

moore::NetKind convertNetKind ( slang::ast::NetType::NetKind kind)
static

Definition at line 273 of file Structure.cpp.

◆ convertProcedureKind()

moore::ProcedureKind convertProcedureKind ( slang::ast::ProceduralBlockKind kind)
static

Definition at line 255 of file Structure.cpp.

◆ decodeUdpLevel()

std::optional< UdpLevel > decodeUdpLevel ( char c)
static

Decode a single UDP level character (0, 1, x, ?, or b, case-insensitive).

Returns std::nullopt for non-level characters such as edge specifiers (r, f, p, n, *, (, )).

Definition at line 2900 of file Structure.cpp.

Referenced by classifySequentialTableRows(), and parseInputRow().

◆ emitSequentialLevelSensitiveProcedure()

void emitSequentialLevelSensitiveProcedure ( Context & context,
OpBuilder & builder,
Location loc,
Value asyncCond,
Value asyncVal,
Value qVar )
static

Emit a level-sensitive moore.procedure always_latch for a latch UDP.

Definition at line 3316 of file Structure.cpp.

References context.

Referenced by circt::ImportVerilog::Context::convertSequentialUserDefinedPrimitiveDef().

◆ getDPISignature()

FailureOr< SmallVector< moore::DPIArgInfo > > getDPISignature ( Context & context,
const slang::ast::SubroutineSymbol & subroutine )
static

Definition at line 1979 of file Structure.cpp.

References context, and failure().

Referenced by circt::ImportVerilog::Context::declareCallableImpl().

◆ getFunctionSignature()

FunctionType getFunctionSignature ( Context & context,
const slang::ast::SubroutineSymbol & subroutine,
ArrayRef< Type > prefixParams,
ArrayRef< Type > suffixParams = {} )
static

Helper function to generate the function signature from a SubroutineSymbol and optional extra arguments (used for this argument).

Definition at line 1944 of file Structure.cpp.

References context.

Referenced by circt::ImportVerilog::Context::declareCallableImpl().

◆ guessNamespacePrefix()

void guessNamespacePrefix ( const slang::ast::Symbol & symbol,
SmallString< 64 > & prefix )
static

◆ parseInputRow()

bool parseInputRow ( StringRef inputs,
SmallVectorImpl< UdpLevel > & levels )
static

Parse a level-only UDP input row string into UdpLevel entries.

Returns false if any character is not a recognized UDP level symbol.

Definition at line 2920 of file Structure.cpp.

References decodeUdpLevel().

Referenced by classifySequentialTableRows(), and circt::ImportVerilog::Context::convertUserDefinedPrimitiveDef().

◆ recordDPIExportDirectives()

void recordDPIExportDirectives ( Context & context,
const slang::ast::Scope & scope,
const slang::syntax::SyntaxNode * syntax )
static

Record export "DPI-C" directives in the given scope so that callable declarations can be tagged with the exported C name.

Slang resolves the directives during elaboration but does not expose them on the subroutine symbols themselves, so walk the scope's syntax to recover them.

Definition at line 34 of file Structure.cpp.

References context.

Referenced by circt::ImportVerilog::Context::convertCompilation(), circt::ImportVerilog::Context::convertModuleBody(), and circt::ImportVerilog::Context::convertPackage().

◆ skipUnsupportedCombinationalRow()

bool skipUnsupportedCombinationalRow ( const slang::ast::PrimitiveSymbol::TableEntry & entry,
ArrayRef< UdpLevel > levels,
size_t numInputs )
static

Skip unsupported or non-productive combinational table rows.

Currently, only combinational UDPs with 0/1/?/b inputs are supported.

Definition at line 2934 of file Structure.cpp.

Referenced by circt::ImportVerilog::Context::convertUserDefinedPrimitiveDef().

Variable Documentation

◆ dpiExportAttrName

StringLiteral dpiExportAttrName = "circt.dpi.export"
staticconstexpr

Definition at line 24 of file Structure.cpp.

Referenced by circt::ImportVerilog::Context::declareCallableImpl().