CIRCT  20.0.0git
Public Types | Public Member Functions | Public Attributes | List of all members
circt::ImportVerilog::Context Struct Reference

A helper class to facilitate the conversion from a Slang AST to MLIR operations. More...

#include <ImportVerilogInternals.h>

Collaboration diagram for circt::ImportVerilog::Context:
Collaboration graph
[legend]

Public Types

using ValueSymbols = llvm::ScopedHashTable< const slang::ast::ValueSymbol *, Value >
 A table of defined values, such as variables, that may be referred to by name in expressions. More...
 
using ValueSymbolScope = ValueSymbols::ScopeTy
 

Public Member Functions

 Context (const ImportVerilogOptions &options, slang::ast::Compilation &compilation, mlir::ModuleOp intoModuleOp, const slang::SourceManager &sourceManager, SmallDenseMap< slang::BufferID, StringRef > &bufferFilePaths)
 
 Context (const Context &)=delete
 
MLIRContext * getContext ()
 Return the MLIR context. More...
 
Location convertLocation (slang::SourceLocation loc)
 Convert a slang SourceLocation into an MLIR Location. More...
 
Location convertLocation (slang::SourceRange range)
 Convert a slang SourceRange into an MLIR Location. More...
 
Type convertType (const slang::ast::Type &type, LocationAttr loc={})
 Convert a slang type into an MLIR type. More...
 
Type convertType (const slang::ast::DeclaredType &type)
 
LogicalResult convertCompilation ()
 Convert hierarchy and structure AST nodes to MLIR ops. More...
 
ModuleLoweringconvertModuleHeader (const slang::ast::InstanceBodySymbol *module)
 Convert a module and its ports to an empty module op in the IR. More...
 
LogicalResult convertModuleBody (const slang::ast::InstanceBodySymbol *module)
 Convert a module's body to the corresponding IR ops. More...
 
LogicalResult convertPackage (const slang::ast::PackageSymbol &package)
 Convert a package and its contents. More...
 
FunctionLoweringdeclareFunction (const slang::ast::SubroutineSymbol &subroutine)
 Convert a function and its arguments to a function declaration in the IR. More...
 
LogicalResult convertFunction (const slang::ast::SubroutineSymbol &subroutine)
 Convert a function. More...
 
LogicalResult convertStatement (const slang::ast::Statement &stmt)
 
Value convertRvalueExpression (const slang::ast::Expression &expr, Type requiredType={})
 
Value convertLvalueExpression (const slang::ast::Expression &expr)
 
LogicalResult collectHierarchicalValues (const slang::ast::Expression &expr, const slang::ast::Symbol &outermostModule)
 
LogicalResult traverseInstanceBody (const slang::ast::Symbol &symbol)
 
LogicalResult convertTimingControl (const slang::ast::TimingControl &ctrl, const slang::ast::Statement &stmt)
 
Value convertToBool (Value value)
 Helper function to convert a value to its "truthy" boolean value. More...
 
Value convertToBool (Value value, Domain domain)
 Helper function to convert a value to its "truthy" boolean value and convert it to the given domain. More...
 
Value convertToSimpleBitVector (Value value)
 Helper function to convert a value to its simple bit vector representation, if it has one. More...
 
Value materializeConversion (Type type, Value value, bool isSigned, Location loc)
 Helper function to insert the necessary operations to cast a value from one type to another. More...
 
Value materializeSVInt (const slang::SVInt &svint, const slang::ast::Type &type, Location loc)
 Helper function to materialize an SVInt as an SSA value. More...
 
Value materializeConstant (const slang::ConstantValue &constant, const slang::ast::Type &type, Location loc)
 Helper function to materialize a ConstantValue as an SSA value. More...
 
FailureOr< Value > convertFormatString (slang::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 interpolated into a !moore.format_string value. More...
 
slang::ConstantValue evaluateConstant (const slang::ast::Expression &expr)
 Evaluate the constant value of an expression. More...
 

Public Attributes

const ImportVerilogOptionsoptions
 
slang::ast::Compilation & compilation
 
mlir::ModuleOp intoModuleOp
 
const slang::SourceManager & sourceManager
 
SmallDenseMap< slang::BufferID, StringRef > & bufferFilePaths
 
OpBuilder builder
 The builder used to create IR operations. More...
 
SymbolTable symbolTable
 A symbol table of the MLIR module we are emitting into. More...
 
std::map< slang::SourceLocation, Operation * > orderedRootOps
 The top-level operations ordered by their Slang source location. More...
 
DenseMap< const slang::ast::InstanceBodySymbol *, std::unique_ptr< ModuleLowering > > modules
 How we have lowered modules to MLIR. More...
 
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. More...
 
DenseMap< const slang::ast::SubroutineSymbol *, std::unique_ptr< FunctionLowering > > functions
 Functions that have already been converted. More...
 
ValueSymbols valueSymbols
 
DenseMap< const slang::ast::InstanceBodySymbol *, SmallVector< HierPathInfo > > hierPaths
 Collect all hierarchical names used for the per module/instance. More...
 
DenseSet< StringAttr > sameHierPaths
 It's used to collect the repeat hierarchical names on the same path. More...
 
SmallVector< Value > lvalueStack
 A stack of assignment left-hand side values. More...
 
SmallVector< LoopFrameloopStack
 A stack of loop continuation and exit blocks. More...
 
std::function< void(moore::ReadOp)> rvalueReadCallback
 A listener called for every variable or net being read. More...
 

Detailed Description

A helper class to facilitate the conversion from a Slang AST to MLIR operations.

Keeps track of the destination MLIR module, builders, and various worklists and utilities needed for conversion.

Definition at line 77 of file ImportVerilogInternals.h.

Member Typedef Documentation

◆ ValueSymbols

using circt::ImportVerilog::Context::ValueSymbols = llvm::ScopedHashTable<const slang::ast::ValueSymbol *, Value>

A table of defined values, such as variables, that may be referred to by name in expressions.

The expressions use this table to lookup the MLIR value that was created for a given declaration in the Slang AST node.

Definition at line 200 of file ImportVerilogInternals.h.

◆ ValueSymbolScope

using circt::ImportVerilog::Context::ValueSymbolScope = ValueSymbols::ScopeTy

Definition at line 202 of file ImportVerilogInternals.h.

Constructor & Destructor Documentation

◆ Context() [1/2]

circt::ImportVerilog::Context::Context ( const ImportVerilogOptions options,
slang::ast::Compilation &  compilation,
mlir::ModuleOp  intoModuleOp,
const slang::SourceManager &  sourceManager,
SmallDenseMap< slang::BufferID, StringRef > &  bufferFilePaths 
)
inline

Definition at line 78 of file ImportVerilogInternals.h.

◆ Context() [2/2]

circt::ImportVerilog::Context::Context ( const Context )
delete

Member Function Documentation

◆ collectHierarchicalValues()

LogicalResult Context::collectHierarchicalValues ( const slang::ast::Expression &  expr,
const slang::ast::Symbol &  outermostModule 
)

Definition at line 114 of file HierarchicalNames.cpp.

References convertLocation().

◆ convertCompilation()

LogicalResult Context::convertCompilation ( )

Convert hierarchy and structure AST nodes to MLIR ops.

Convert an entire Slang compilation to MLIR ops.

This is the main entry point for the conversion.

Definition at line 572 of file Structure.cpp.

References compilation, convertLocation(), convertModuleBody(), convertModuleHeader(), moduleWorklist, and traverseInstanceBody().

◆ convertFormatString()

FailureOr< Value > Context::convertFormatString ( slang::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 interpolated into a !moore.format_string value.

Returns failure if an error occurs. Returns a null value if the formatted string is trivially empty. Otherwise returns the formatted string.

Definition at line 193 of file FormatStrings.cpp.

◆ convertFunction()

LogicalResult Context::convertFunction ( const slang::ast::SubroutineSymbol &  subroutine)

◆ convertLocation() [1/2]

Location Context::convertLocation ( slang::SourceLocation  loc)

◆ convertLocation() [2/2]

Location Context::convertLocation ( slang::SourceRange  range)

Convert a slang SourceRange into an MLIR Location.

Definition at line 67 of file ImportVerilog.cpp.

References convertLocation().

◆ convertLvalueExpression()

Value Context::convertLvalueExpression ( const slang::ast::Expression &  expr)

Definition at line 1147 of file Expressions.cpp.

References convertLocation().

Referenced by convertModuleBody().

◆ convertModuleBody()

LogicalResult Context::convertModuleBody ( const slang::ast::InstanceBodySymbol *  module)

Convert a module's body to the corresponding IR ops.

The module op must have already been created earlier through a convertModuleHeader call.

Definition at line 786 of file Structure.cpp.

References builder, convertLocation(), convertLvalueExpression(), hierPaths, modules, and valueSymbols.

Referenced by convertCompilation().

◆ convertModuleHeader()

ModuleLowering * Context::convertModuleHeader ( const slang::ast::InstanceBodySymbol *  module)

Convert a module and its ports to an empty module op in the IR.

Also adds the op to the worklist of module bodies to be lowered. This acts like a module "declaration", allowing instances to already refer to a module even before its body has been lowered.

Definition at line 614 of file Structure.cpp.

References builder, convertLocation(), convertType(), circt::calyx::direction::get(), getContext(), hierPaths, circt::hw::ModulePort::Input, intoModuleOp, modules, moduleWorklist, orderedRootOps, circt::hw::ModulePort::Output, symbolTable, and toString().

Referenced by convertCompilation().

◆ convertPackage()

LogicalResult Context::convertPackage ( const slang::ast::PackageSymbol &  package)

Convert a package and its contents.

Definition at line 855 of file Structure.cpp.

References builder, convertLocation(), intoModuleOp, and valueSymbols.

◆ convertRvalueExpression()

Value Context::convertRvalueExpression ( const slang::ast::Expression &  expr,
Type  requiredType = {} 
)

Definition at line 1137 of file Expressions.cpp.

References convertLocation(), and materializeConversion().

◆ convertStatement()

LogicalResult Context::convertStatement ( const slang::ast::Statement &  stmt)

Definition at line 695 of file Statements.cpp.

References assert(), and convertLocation().

Referenced by convertFunction(), and convertTimingControl().

◆ convertTimingControl()

LogicalResult Context::convertTimingControl ( const slang::ast::TimingControl &  ctrl,
const slang::ast::Statement &  stmt 
)

Definition at line 151 of file TimingControls.cpp.

References builder, convertStatement(), handleRoot(), and rvalueReadCallback.

◆ convertToBool() [1/2]

Value Context::convertToBool ( Value  value)

Helper function to convert a value to its "truthy" boolean value.

Definition at line 1154 of file Expressions.cpp.

References builder.

Referenced by convertToBool().

◆ convertToBool() [2/2]

Value Context::convertToBool ( Value  value,
Domain  domain 
)

Helper function to convert a value to its "truthy" boolean value and convert it to the given domain.

Definition at line 1206 of file Expressions.cpp.

References builder, convertToBool(), circt::calyx::direction::get(), and getContext().

◆ convertToSimpleBitVector()

Value Context::convertToSimpleBitVector ( Value  value)

Helper function to convert a value to its simple bit vector representation, if it has one.

Otherwise returns null. Also returns null if the given value is null.

Definition at line 1216 of file Expressions.cpp.

References builder, and circt::calyx::direction::get().

◆ convertType() [1/2]

Type Context::convertType ( const slang::ast::DeclaredType &  type)

Definition at line 173 of file Types.cpp.

References convertLocation(), and convertType().

◆ convertType() [2/2]

Type Context::convertType ( const slang::ast::Type &  type,
LocationAttr  loc = {} 
)

Convert a slang type into an MLIR type.

Returns null on failure. Uses the provided location for error reporting, or tries to guess one from the given type. Types tend to have unreliable location information, so it's generally a good idea to pass in a location.

Definition at line 167 of file Types.cpp.

References convertLocation().

Referenced by convertFunction(), convertModuleHeader(), convertType(), declareFunction(), and materializeSVInt().

◆ declareFunction()

FunctionLowering * Context::declareFunction ( const slang::ast::SubroutineSymbol &  subroutine)

Convert a function and its arguments to a function declaration in the IR.

This does not convert the function body.

Definition at line 870 of file Structure.cpp.

References builder, convertLocation(), convertType(), functions, circt::calyx::direction::get(), getContext(), guessNamespacePrefix(), intoModuleOp, orderedRootOps, and symbolTable.

Referenced by convertFunction().

◆ evaluateConstant()

slang::ConstantValue Context::evaluateConstant ( const slang::ast::Expression &  expr)

Evaluate the constant value of an expression.

Definition at line 1197 of file Expressions.cpp.

References compilation.

◆ getContext()

MLIRContext* circt::ImportVerilog::Context::getContext ( )
inline

Return the MLIR context.

Definition at line 89 of file ImportVerilogInternals.h.

References intoModuleOp.

Referenced by convertModuleHeader(), convertToBool(), declareFunction(), and materializeSVInt().

◆ materializeConstant()

Value Context::materializeConstant ( const slang::ConstantValue &  constant,
const slang::ast::Type &  type,
Location  loc 
)

Helper function to materialize a ConstantValue as an SSA value.

Returns null if the constant cannot be materialized.

Definition at line 1189 of file Expressions.cpp.

References materializeSVInt().

◆ materializeConversion()

Value Context::materializeConversion ( Type  type,
Value  value,
bool  isSigned,
Location  loc 
)

Helper function to insert the necessary operations to cast a value from one type to another.

Definition at line 1240 of file Expressions.cpp.

References builder, and circt::calyx::direction::get().

Referenced by convertRvalueExpression().

◆ materializeSVInt()

Value Context::materializeSVInt ( const slang::SVInt &  svint,
const slang::ast::Type &  type,
Location  loc 
)

Helper function to materialize an SVInt as an SSA value.

Materialize a Slang integer literal as a constant op.

Definition at line 1168 of file Expressions.cpp.

References builder, convertSVIntToFVInt(), convertType(), circt::moore::FourValued, circt::calyx::direction::get(), getContext(), and circt::moore::TwoValued.

Referenced by materializeConstant().

◆ traverseInstanceBody()

LogicalResult Context::traverseInstanceBody ( const slang::ast::Symbol &  symbol)

Definition at line 183 of file HierarchicalNames.cpp.

References convertLocation().

Referenced by convertCompilation().

Member Data Documentation

◆ bufferFilePaths

SmallDenseMap<slang::BufferID, StringRef>& circt::ImportVerilog::Context::bufferFilePaths

Definition at line 173 of file ImportVerilogInternals.h.

◆ builder

OpBuilder circt::ImportVerilog::Context::builder

◆ compilation

slang::ast::Compilation& circt::ImportVerilog::Context::compilation

Definition at line 170 of file ImportVerilogInternals.h.

Referenced by convertCompilation(), and evaluateConstant().

◆ functions

DenseMap<const slang::ast::SubroutineSymbol *, std::unique_ptr<FunctionLowering> > circt::ImportVerilog::Context::functions

Functions that have already been converted.

Definition at line 195 of file ImportVerilogInternals.h.

Referenced by declareFunction().

◆ hierPaths

DenseMap<const slang::ast::InstanceBodySymbol *, SmallVector<HierPathInfo> > circt::ImportVerilog::Context::hierPaths

Collect all hierarchical names used for the per module/instance.

Definition at line 207 of file ImportVerilogInternals.h.

Referenced by convertModuleBody(), and convertModuleHeader().

◆ intoModuleOp

mlir::ModuleOp circt::ImportVerilog::Context::intoModuleOp

◆ loopStack

SmallVector<LoopFrame> circt::ImportVerilog::Context::loopStack

A stack of loop continuation and exit blocks.

Each loop will push the relevant info onto this stack, lower its loop body statements, and pop the info off the stack again. Continue and break statements encountered as part of the loop body statements will use this information to branch to the correct block.

Definition at line 226 of file ImportVerilogInternals.h.

◆ lvalueStack

SmallVector<Value> circt::ImportVerilog::Context::lvalueStack

A stack of assignment left-hand side values.

Each assignment will push its lowered left-hand side onto this stack before lowering its right-hand side. This allows expressions to resolve the opaque LValueReferenceExpressions in the AST.

Definition at line 219 of file ImportVerilogInternals.h.

◆ modules

DenseMap<const slang::ast::InstanceBodySymbol *, std::unique_ptr<ModuleLowering> > circt::ImportVerilog::Context::modules

How we have lowered modules to MLIR.

Definition at line 187 of file ImportVerilogInternals.h.

Referenced by convertModuleBody(), and convertModuleHeader().

◆ moduleWorklist

std::queue<const slang::ast::InstanceBodySymbol *> circt::ImportVerilog::Context::moduleWorklist

A list of modules for which the header has been created, but the body has not been converted yet.

Definition at line 190 of file ImportVerilogInternals.h.

Referenced by convertCompilation(), and convertModuleHeader().

◆ options

const ImportVerilogOptions& circt::ImportVerilog::Context::options

Definition at line 169 of file ImportVerilogInternals.h.

◆ orderedRootOps

std::map<slang::SourceLocation, Operation *> circt::ImportVerilog::Context::orderedRootOps

The top-level operations ordered by their Slang source location.

This is used to produce IR that follows the source file order.

Definition at line 182 of file ImportVerilogInternals.h.

Referenced by convertModuleHeader(), and declareFunction().

◆ rvalueReadCallback

std::function<void(moore::ReadOp)> circt::ImportVerilog::Context::rvalueReadCallback

A listener called for every variable or net being read.

This can be used to collect all variables read as part of an expression or statement, for example to populate the list of observed signals in an implicit event control @*.

Definition at line 232 of file ImportVerilogInternals.h.

Referenced by convertTimingControl().

◆ sameHierPaths

DenseSet<StringAttr> circt::ImportVerilog::Context::sameHierPaths

It's used to collect the repeat hierarchical names on the same path.

Such as Top.sub.a and sub.a, they are equivalent. The variable "a" will be added to the port list. But we only record once. If we don't do that. We will view the strange IR, such as module @Sub(out y, out y);

Definition at line 213 of file ImportVerilogInternals.h.

◆ sourceManager

const slang::SourceManager& circt::ImportVerilog::Context::sourceManager

Definition at line 172 of file ImportVerilogInternals.h.

◆ symbolTable

SymbolTable circt::ImportVerilog::Context::symbolTable

A symbol table of the MLIR module we are emitting into.

Definition at line 178 of file ImportVerilogInternals.h.

Referenced by convertModuleHeader(), and declareFunction().

◆ valueSymbols

ValueSymbols circt::ImportVerilog::Context::valueSymbols

Definition at line 203 of file ImportVerilogInternals.h.

Referenced by convertFunction(), convertModuleBody(), and convertPackage().


The documentation for this struct was generated from the following files: