13#ifndef CIRCT_DIALECT_FIRRTL_FIRRTLUTILS_H
14#define CIRCT_DIALECT_FIRRTL_FIRRTLUTILS_H
17#include "mlir/IR/BuiltinOps.h"
21struct InnerSymbolNamespace;
26void emitConnect(OpBuilder &builder, Location loc, Value lhs, Value rhs);
27void emitConnect(ImplicitLocOpBuilder &builder, Value lhs, Value rhs);
30IntegerAttr
getIntAttr(Type type,
const APInt &value);
47 bool lookThroughCasts);
53 bool lookThroughNodes,
bool lookThroughCasts);
83 FlatSymbolRefAttr
getMacro(StringAttr optionName, StringAttr caseName)
const;
90 llvm::MapVector<std::pair<StringAttr, StringAttr>, FlatSymbolRefAttr>
cache;
99template <
typename A,
typename... B>
101 bool lookThroughNodes,
102 bool lookThroughCasts) {
109 auto *op = val.getDefiningOp();
113 return isa<A, B...>(op);
123 bool lookThroughNodes,
bool lookThroughCasts,
147 bool nameSafe =
false);
176std::pair<hw::InnerSymAttr, StringAttr>
213static inline hw::InnerRefAttr
220static inline hw::InnerRefAttr
243static inline hw::InnerRefAttr
251static inline hw::InnerRefAttr
264 return TypeSwitch<Type, FIRRTLBaseType>(type)
266 .Case<LHSType>([](
auto lhs) {
return lhs.getType(); })
267 .Case<RefType>([](
auto ref) {
return ref.getType(); })
268 .Default([](Type type) {
return nullptr; });
281 return TypeSwitch<FIRRTLType, FIRRTLType>(type)
283 .Case<RefType>([&](
auto ref) {
284 return RefType::get(fn(ref.getType()), ref.getForceable(),
295 return TypeSwitch<FIRRTLType, FIRRTLType>(type)
298 auto result = fn(ref.getType());
301 return RefType::get(result, ref.getForceable(), ref.getLayer());
310 Type type, std::optional<Location> loc = {},
311 llvm::function_ref<hw::TypeAliasType(Type, BaseTypeAliasType, Location)>
324 StringRef spelling,
bool skipParsing, StringAttr &locatorFilenameCache,
325 FileLineColLoc &fileLineColLocCache, MLIRContext *
context);
339 llvm::StringRef formatString,
340 llvm::ArrayRef<mlir::Value> specOperands,
341 mlir::StringAttr &formatStringResult,
342 llvm::SmallVectorImpl<mlir::Value> &operands);
357 mlir::ImplicitLocOpBuilder &builderOM);
static std::unique_ptr< Context > context
static Value lookThroughWires(Value value)
Trace a value through wires to its original definition.
This class represents a reference to a specific field or element of an aggregate value.
llvm::MapVector< std::pair< StringAttr, StringAttr >, FlatSymbolRefAttr > cache
FlatSymbolRefAttr getMacro(StringAttr optionName, StringAttr caseName) const
Helper class to cache tie-off values for different FIRRTL types.
ImplicitLocOpBuilder & builder
TieOffCache(ImplicitLocOpBuilder &builder)
Value getInvalid(FIRRTLBaseType type)
Get or create an InvalidValueOp for the given base type.
SmallDenseMap< Type, Value, 8 > cache
Value getUnknown(PropertyType type)
Get or create an UnknownValueOp for the given property type.
The target of an inner symbol, the entity the symbol is a handle for.
llvm::function_ref< hw::InnerSymbolNamespace &(FModuleLike mod)> GetNamespaceCallback
FIRRTLType mapBaseTypeNullable(FIRRTLType type, function_ref< FIRRTLBaseType(FIRRTLBaseType)> fn)
Return a FIRRTLType with its base type component mutated by the given function.
FieldRef getFieldRefForTarget(const hw::InnerSymTarget &ist)
Get FieldRef pointing to the specified inner symbol target, which must be valid.
FieldRef getDeltaRef(Value value, bool lookThroughCasts=false)
Get the delta indexing from a value, as a FieldRef.
FIRRTLBaseType getBaseType(Type type)
If it is a base type, return it as is.
FieldRef getFieldRefFromValue(Value value, bool lookThroughCasts=false)
Get the FieldRef from a value.
mlir::TypedValue< FIRRTLBaseType > FIRRTLBaseValue
void walkGroundTypes(FIRRTLType firrtlType, llvm::function_ref< void(uint64_t, FIRRTLBaseType, bool)> fn)
Walk leaf ground types in the firrtlType and apply the function fn.
FIRRTLType mapBaseType(FIRRTLType type, function_ref< FIRRTLBaseType(FIRRTLBaseType)> fn)
Return a FIRRTLType with its base type component mutated by the given function.
static bool isModuleScopedDrivenBy(Value val, bool lookThroughWires, bool lookThroughNodes, bool lookThroughCasts)
Return true if a value is module-scoped driven by a value of a specific type.
PathOp createPathRef(Operation *op, hw::HierPathOp nla, mlir::ImplicitLocOpBuilder &builderOM)
Add the tracker annotation to the op and get a PathOp to the op.
IntegerAttr getIntAttr(Type type, const APInt &value)
Utiility for generating a constant attribute.
std::pair< bool, std::optional< mlir::LocationAttr > > maybeStringToLocation(llvm::StringRef spelling, bool skipParsing, mlir::StringAttr &locatorFilenameCache, FileLineColLoc &fileLineColLocCache, MLIRContext *context)
std::pair< hw::InnerSymAttr, StringAttr > getOrAddInnerSym(MLIRContext *context, hw::InnerSymAttr attr, uint64_t fieldID, llvm::function_ref< hw::InnerSymbolNamespace &()> getNamespace)
Ensure that the the InnerSymAttr has a symbol on the field specified.
hw::InnerRefAttr getInnerRefTo(const hw::InnerSymTarget &target, GetNamespaceCallback getNamespace)
Obtain an inner reference to the target (operation or port), adding an inner symbol as necessary.
T getBaseOfType(Type type)
Get base type if isa<> the requested type, else null.
PropAssignOp getPropertyAssignment(FIRRTLPropertyValue value)
Return the single assignment to a Property value.
mlir::ParseResult parseFormatString(mlir::OpBuilder &builder, mlir::Location loc, llvm::StringRef formatString, llvm::ArrayRef< mlir::Value > specOperands, mlir::StringAttr &formatStringResult, llvm::SmallVectorImpl< mlir::Value > &operands)
Value getValueSource(Value val, bool lookThroughWires, bool lookThroughNodes, bool lookThroughCasts)
Return the value that drives another FIRRTL value within module scope.
Value getModuleScopedDriver(Value val, bool lookThroughWires, bool lookThroughNodes, bool lookThroughCasts)
Return the value that drives another FIRRTL value within module scope.
Value getDriverFromConnect(Value val)
Return the module-scoped driver of a value only looking through one connect.
Value getValueByFieldID(ImplicitLocOpBuilder builder, Value value, unsigned fieldID)
This gets the value targeted by a field id.
std::pair< std::string, bool > getFieldName(const FieldRef &fieldRef, bool nameSafe=false)
Get a string identifier representing the FieldRef.
llvm::function_ref< bool(const FieldRef &dst, const FieldRef &src)> WalkDriverCallback
Walk all the drivers of a value, passing in the connect operations drive the value.
mlir::TypedValue< PropertyType > FIRRTLPropertyValue
Type lowerType(Type type, std::optional< Location > loc={}, llvm::function_ref< hw::TypeAliasType(Type, BaseTypeAliasType, Location)> getTypeDeclFn={})
Given a type, return the corresponding lowered type for the HW dialect.
hw::InnerSymTarget getTargetFor(FieldRef ref)
Return the inner sym target for the specified value and fieldID.
bool walkDrivers(FIRRTLBaseValue value, bool lookThroughWires, bool lookThroughNodes, bool lookThroughCasts, WalkDriverCallback callback)
IntegerAttr getIntOnesAttr(Type type)
Utility for generating a constant all ones attribute.
void emitConnect(OpBuilder &builder, Location loc, Value lhs, Value rhs)
Emit a connect between two values.
void makeCommonPrefix(SmallString< 64 > &a, StringRef b)
Truncate a to the common prefix of a and b.
IntegerAttr getIntZerosAttr(Type type)
Utility for generating a constant zero attribute.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.