13 #ifndef CIRCT_DIALECT_FIRRTL_ANNOTATIONS_H
14 #define CIRCT_DIALECT_FIRRTL_ANNOTATIONS_H
17 #include "mlir/IR/BuiltinAttributes.h"
18 #include "mlir/IR/BuiltinTypes.h"
19 #include "mlir/IR/Operation.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/PointerLikeTypeTraits.h"
25 struct InnerSymbolNamespace;
29 class AnnotationSetIterator;
57 auto oldFieldID = anno.
getMember<IntegerAttr>(
"circt.fieldID");
58 if (oldFieldID && !fieldID) {
72 void setDict(DictionaryAttr dict);
85 template <
typename... Args>
91 template <
typename AttrClass = Attribute>
93 return getDict().getAs<AttrClass>(name);
95 template <
typename AttrClass = Attribute>
97 return getDict().getAs<AttrClass>(name);
101 void setMember(StringAttr name, Attribute value);
102 void setMember(StringRef name, Attribute value);
108 using iterator = llvm::ArrayRef<NamedAttribute>::iterator;
114 explicit operator bool()
const {
return bool(
attr); }
127 template <
typename T,
typename... Rest>
129 return compare(name) || (*this)(rest...);
134 bool compare(StringRef name)
const {
return cls &&
cls.getValue() == name; }
155 MLIRContext *context);
193 bool applyToPort(FModuleLike op,
size_t portNo)
const;
205 template <
typename... Args>
242 static bool setDontTouch(Operation *op,
bool dontTouch);
250 return !(*
this == other);
277 template <
typename... Args>
289 llvm::function_ref<
bool(
Annotation)> predicate);
299 llvm::function_ref<
bool(
unsigned,
Annotation)> predicate);
312 :
public llvm::indexed_accessor_iterator<AnnotationSetIterator,
313 AnnotationSet, Annotation,
314 Annotation, Annotation> {
346 operator bool()
const {
return getOp(); }
351 return !(*
this == other);
374 operator bool()
const {
return impl; }
451 LogicalResult
extractDUT(FModuleLike mod, FModuleLike &dut);
463 template <
typename To,
typename From>
466 std::enable_if_t<std::is_base_of_v<::circt::firrtl::AnnoTarget, From>>>
472 if constexpr (std::is_base_of_v<To, From>)
475 return To::classof(target);
477 static inline To
doCast(From target) {
return To(target.getImpl()); }
486 return const_cast<void *
>(v.
getAttr().getAsOpaquePointer());
489 return Annotation(mlir::DictionaryAttr::getFromOpaquePointer(p));
499 mlir::DictionaryAttr(
static_cast<mlir::Attribute::ImplType *
>(
504 mlir::DictionaryAttr(
static_cast<mlir::Attribute::ImplType *
>(
assert(baseType &&"element must be base type")
friend class AnnotationSet
AnnotationSetIterator(AnnotationSet owner, ptrdiff_t curIndex)
Annotation operator*() const
This class provides a read-only projection over the MLIR attributes that represent a set of annotatio...
bool hasDontTouch() const
firrtl.transforms.DontTouchAnnotation
bool hasAnnotationImpl(StringAttr className) const
bool removeAnnotations(llvm::function_ref< bool(Annotation)> predicate)
Remove all annotations from this annotation set for which predicate returns true.
Annotation getAnnotationImpl(StringAttr className) const
bool applyToPort(FModuleLike op, size_t portNo) const
Store the annotations in this set in an operation's portAnnotations attribute, overwriting any existi...
ArrayAttr getArrayAttr() const
Return this annotation set as an ArrayAttr.
AnnotationSet(ArrayAttr annotations)
Form an annotation set with a non-null ArrayAttr.
bool removeAnnotation(Annotation anno)
Remove an annotation from this annotation set.
static bool hasAnnotation(Operation *op, Args... args)
Return true if we have an annotation with the specified class name.
bool removeAnnotationsWithClass(Args... names)
Remove all annotations with one of the given classes from this annotation set.
static AnnotationSet get(Value v)
Get an annotation set for the specified value.
Annotation getAnnotation(StringAttr className) const
bool hasAnnotation(StringAttr className) const
bool operator!=(const AnnotationSet &other) const
bool applyToOperation(Operation *op) const
Store the annotations in this set in an operation's annotations attribute, overwriting any existing a...
void addAnnotations(ArrayRef< Annotation > annotations)
Add more annotations to this annotation set.
bool hasAnnotation(StringRef className) const
Return true if we have an annotation with the specified class name.
AnnotationSet(MLIRContext *context)
Form an empty annotation set.
static bool removePortAnnotations(Operation *module, llvm::function_ref< bool(unsigned, Annotation)> predicate)
Remove all port annotations from a module or extmodule for which predicate returns true.
static AnnotationSet forPort(FModuleLike op, size_t portNo)
Get an annotation set for the specified port.
Annotation getAnnotation(StringRef className) const
If this annotation set has an annotation with the specified class name, return it.
bool operator==(const AnnotationSet &other) const
bool setDontTouch(bool dontTouch)
ArrayRef< Attribute > getArray() const
Return all the raw annotations that exist.
AnnotationSetIterator iterator
MLIRContext * getContext() const
Return the MLIRContext corresponding to this AnnotationSet.
This class provides a read-only projection of an annotation.
Attribute getAttr() const
Get the underlying attribute.
DictionaryAttr getDict() const
Get the data dictionary of this attribute.
void setDict(DictionaryAttr dict)
Set the data dictionary of this attribute.
bool operator==(const Annotation &other) const
unsigned getFieldID() const
Get the field id this attribute targets.
AttrClass getMember(StringAttr name) const
Return a member of the annotation.
void setMember(StringAttr name, Attribute value)
Add or set a member of the annotation to a value.
Annotation(Annotation anno, uint64_t fieldID)
void removeMember(StringAttr name)
Remove a member of the annotation.
bool operator!=(const Annotation &other) const
AttrClass getMember(StringRef name) const
StringRef getClass() const
Return the 'class' that this annotation is representing.
Annotation(Attribute attr)
StringAttr getClassAttr() const
Return the 'class' that this annotation is representing.
llvm::ArrayRef< NamedAttribute >::iterator iterator
bool isClass(Args... names) const
Return true if this annotation matches any of the specified class names.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
StringRef getAnnotationAttrName()
Return the name of the attribute used for annotations on FIRRTL ops.
LogicalResult extractDUT(FModuleLike mod, FModuleLike &dut)
Utility that searches for a MarkDUTAnnotation on a specific module, mod, and tries to update a design...
StringRef getPortAnnotationAttrName()
Return the name of the attribute used for port annotations on FIRRTL ops.
ArrayAttr getAnnotationsIfPresent(Operation *op)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
size_t hash_combine(size_t h1, size_t h2)
C++'s stdlib doesn't have a hash_combine function. This is a simple one.
llvm::hash_code hash_value(const T &e)
An annotation target is used to keep track of something that is targeted by an Annotation.
void setOp(Operation *op)
FIRRTLType getType() const
Get the type of the target.
bool operator!=(const AnnoTarget &other) const
Attribute getNLAReference(hw::InnerSymbolNamespace &moduleNamespace) const
Get a reference to this target suitable for use in an NLA.
detail::AnnoTargetImpl getImpl() const
detail::AnnoTargetImpl impl
Operation * getOp() const
FModuleLike getModule() const
Get the parent module of the target.
AnnotationSet getAnnotations() const
Get the annotations associated with the target.
void setAnnotations(AnnotationSet annotations) const
Set the annotations associated with the target.
bool operator==(const AnnoTarget &other) const
AnnoTarget(detail::AnnoTargetImpl impl=nullptr)
Helper struct to perform variadic class equality check.
bool compare(StringRef name) const
bool compare(StringAttr name) const
bool operator()(T name, Rest... rest) const
This represents an annotation targeting a specific operation.
Attribute getNLAReference(hw::InnerSymbolNamespace &moduleNamespace) const
OpAnnoTarget(Operation *op)
void setAnnotations(AnnotationSet annotations) const
FIRRTLType getType() const
static bool classof(const AnnoTarget &annoTarget)
AnnotationSet getAnnotations() const
This represents an annotation targeting a specific port of a module, memory, or instance.
static bool classof(const AnnoTarget &annoTarget)
FIRRTLType getType() const
void setPortNo(unsigned portNo)
AnnotationSet getAnnotations() const
void setAnnotations(AnnotationSet annotations) const
Attribute getNLAReference(hw::InnerSymbolNamespace &moduleNamespace) const
unsigned getPortNo() const
PortAnnoTarget(FModuleLike op, unsigned portNo)
Operation * getOp() const
bool operator!=(const AnnoTargetImpl &other) const
void setOp(Operation *op)
bool operator==(const AnnoTargetImpl &other) const
unsigned getPortNo() const
AnnoTargetImpl(Operation *op)
AnnoTargetImpl(Operation *op, unsigned portNo)
void setPortNo(unsigned portNo)
static To doCast(From target)
static bool isPossible(From target)
static bool isEqual(AnnoTarget lhs, AnnoTarget rhs)
static AnnoTarget getTombstoneKey()
static unsigned getHashValue(AnnoTarget val)
static AnnoTarget getEmptyKey()
static unsigned getHashValue(Annotation val)
static Annotation getEmptyKey()
static bool isEqual(Annotation lhs, Annotation rhs)
static Annotation getTombstoneKey()
static Annotation getFromVoidPointer(void *p)
static void * getAsVoidPointer(Annotation v)