13 #ifndef CIRCT_DIALECT_FIRRTL_FIRRTLUTILS_H
14 #define CIRCT_DIALECT_FIRRTL_FIRRTLUTILS_H
17 #include "mlir/IR/BuiltinOps.h"
18 #include "llvm/Support/Parallel.h"
22 struct InnerSymbolNamespace;
48 bool lookThroughCasts);
54 bool lookThroughNodes,
bool lookThroughCasts);
58 template <
typename A,
typename... B>
60 bool lookThroughNodes,
61 bool lookThroughCasts) {
68 auto *op = val.getDefiningOp();
72 return isa<A, B...>(op);
82 bool lookThroughNodes,
bool lookThroughCasts,
106 bool nameSafe =
false);
130 std::pair<hw::InnerSymAttr, StringAttr>
131 getOrAddInnerSym(MLIRContext *context, hw::InnerSymAttr attr, uint64_t fieldID,
167 static inline hw::InnerRefAttr
174 static inline hw::InnerRefAttr
197 static inline hw::InnerRefAttr
205 static inline hw::InnerRefAttr
218 return TypeSwitch<Type, FIRRTLBaseType>(type)
220 .Case<RefType>([](
auto ref) {
return ref.getType(); })
221 .Default([](Type type) {
return nullptr; });
225 template <
typename T>
234 return TypeSwitch<FIRRTLType, FIRRTLType>(type)
236 .Case<RefType>([&](
auto ref) {
237 return RefType::get(fn(ref.getType()), ref.getForceable());
247 return TypeSwitch<FIRRTLType, FIRRTLType>(type)
250 auto result = fn(ref.getType());
262 Type type, std::optional<Location> loc = {},
263 llvm::function_ref<hw::TypeAliasType(Type, BaseTypeAliasType, Location)>
276 StringRef spelling,
bool skipParsing, StringAttr &locatorFilenameCache,
277 FileLineColLoc &fileLineColLocCache, MLIRContext *context);
286 template <
class IterTy,
class ResultTy,
class ReduceFuncTy,
287 class TransformFuncTy>
289 ResultTy init, ReduceFuncTy reduce,
290 TransformFuncTy transform) {
292 if (context->isMultithreadingEnabled())
293 return llvm::parallelTransformReduce(begin, end, init, reduce, transform);
296 for (IterTy i = begin; i != end; ++i)
297 init = reduce(std::move(init), transform(*i));
298 return std::move(init);
302 template <
class RangeTy,
class ResultTy,
class ReduceFuncTy,
303 class TransformFuncTy>
305 ResultTy init, ReduceFuncTy reduce,
306 TransformFuncTy transform) {
307 return transformReduce(context, std::begin(r), std::end(r), init, reduce,
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.
The target of an inner symbol, the entity the symbol is a handle for.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
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 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.
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.
Value getValueSource(Value val, bool lookThroughWires, bool lookThroughNodes, bool lookThroughCasts)
Return the value that drives another FIRRTL value within module scope.
static ResultTy transformReduce(MLIRContext *context, IterTy begin, IterTy end, ResultTy init, ReduceFuncTy reduce, TransformFuncTy transform)
Wrapper for llvm::parallelTransformReduce that performs the transform_reduce serially when MLIR multi...
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.
IntegerAttr getIntZerosAttr(Type type)
Utility for generating a constant zero attribute.
This file defines an intermediate representation for circuits acting as an abstraction for constraint...