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;
61 auto oldFieldID = anno.
getMember<IntegerAttr>(
"circt.fieldID");
62 if (oldFieldID && !fieldID) {
76 void setDict(DictionaryAttr dict);
89 template <
typename... Args>
95 template <
typename AttrClass = Attribute>
97 return getDict().getAs<AttrClass>(name);
99 template <
typename AttrClass = Attribute>
101 return getDict().getAs<AttrClass>(name);
105 void setMember(StringAttr name, Attribute value);
106 void setMember(StringRef name, Attribute value);
116 using iterator = llvm::ArrayRef<NamedAttribute>::iterator;
122 explicit operator bool()
const {
return bool(
attr); }
135 template <
typename T,
typename... Rest>
137 return compare(name) || (*this)(rest...);
142 bool compare(StringRef name)
const {
return cls &&
cls.getValue() == name; }
163 MLIRContext *context);
201 bool applyToPort(FModuleLike op,
size_t portNo)
const;
213 template <
typename... Args>
250 static bool setDontTouch(Operation *op,
bool dontTouch);
262 return !(*
this == other);
289 template <
typename... Args>
301 llvm::function_ref<
bool(
Annotation)> predicate);
311 llvm::function_ref<
bool(
unsigned,
Annotation)> predicate);
324 :
public llvm::indexed_accessor_iterator<AnnotationSetIterator,
325 AnnotationSet, Annotation,
326 Annotation, Annotation> {
358 operator bool()
const {
return getOp(); }
363 return !(*
this == other);
386 operator bool()
const {
return impl; }
463 LogicalResult
extractDUT(FModuleOp mod, FModuleOp &dut);
475 template <
typename To,
typename From>
478 std::enable_if_t<std::is_base_of_v<::circt::firrtl::AnnoTarget, From>>>
484 if constexpr (std::is_base_of_v<To, From>)
487 return To::classof(target);
489 static inline To
doCast(From target) {
return To(target.getImpl()); }
498 return const_cast<void *
>(v.
getAttr().getAsOpaquePointer());
501 return Annotation(mlir::DictionaryAttr::getFromOpaquePointer(p));
511 mlir::DictionaryAttr(
static_cast<mlir::Attribute::ImplType *
>(
516 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 canBeDeleted() const
Check if every annotation can be deleted.
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)
bool canBeDeleted()
Returns true if this is an annotation which can be safely deleted without consequence.
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(FModuleOp mod, FModuleOp &dut)
Utility that searches for a MarkDUTAnnotation on a specific module, mod, and tries to update a design...
bool isOMIRStringEncodedPassthrough(StringRef type)
Check if an OMIR type is a string-encoded value that the FIRRTL dialect simply passes through as a st...
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)