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/Compiler.h"
22#include "llvm/Support/PointerLikeTypeTraits.h"
58 auto oldFieldID = anno.
getMember<IntegerAttr>(
"circt.fieldID");
59 if (oldFieldID && !fieldID) {
65 IntegerAttr::get(IntegerType::get(anno.
attr.getContext(), 32),
73 void setDict(DictionaryAttr dict);
86 template <
typename... Args>
92 template <
typename AttrClass = Attribute>
94 return getDict().getAs<AttrClass>(name);
96 template <
typename AttrClass = Attribute>
98 return getDict().getAs<AttrClass>(name);
102 void setMember(StringAttr name, Attribute value);
103 void setMember(StringRef name, Attribute value);
109 using iterator = llvm::ArrayRef<NamedAttribute>::iterator;
115 explicit operator bool()
const {
return bool(
attr); }
118#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
119 LLVM_DUMP_METHOD
void dump();
130 template <
typename T,
typename... Rest>
132 return compare(name) || (*this)(rest...);
137 bool compare(StringRef name)
const {
return cls &&
cls.getValue() == name; }
196 bool applyToPort(FModuleLike op,
size_t portNo)
const;
208 template <
typename... Args>
245 static bool setDontTouch(Operation *op,
bool dontTouch);
253 return !(*
this == other);
280 template <
typename... Args>
292 llvm::function_ref<
bool(
Annotation)> predicate);
302 llvm::function_ref<
bool(
unsigned,
Annotation)> predicate);
315 :
public llvm::indexed_accessor_iterator<AnnotationSetIterator,
316 AnnotationSet, Annotation,
317 Annotation, Annotation> {
349 operator bool()
const {
return getOp(); }
354 return !(*
this == other);
377 operator bool()
const {
return impl; }
452template <
typename To,
typename From>
455 std::enable_if_t<std::is_base_of_v<::circt::firrtl::AnnoTarget, From>>>
461 if constexpr (std::is_base_of_v<To, From>)
464 return To::classof(target);
466 static inline To
doCast(From target) {
return To(target.getImpl()); }
475 return const_cast<void *
>(v.
getAttr().getAsOpaquePointer());
478 return Annotation(mlir::DictionaryAttr::getFromOpaquePointer(p));
499 return hash_combine(impl.getOp(), impl.getPortNo());
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
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...
MLIRContext * getContext() const
Return the MLIRContext corresponding to this AnnotationSet.
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.
ArrayRef< Attribute > getArray() const
Return all the raw annotations that exist.
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)
AnnotationSetIterator iterator
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
LLVM_DUMP_METHOD void dump()
bool isClass(Args... names) const
Return true if this annotation matches any of the specified class names.
StringRef getAnnotationAttrName()
Return the name of the attribute used for annotations on FIRRTL ops.
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.
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.
Operation * getOp() const
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
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
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
AnnoTarget(detail::AnnoTargetImpl impl=nullptr)
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)
AnnoTarget(detail::AnnoTargetImpl impl=nullptr)
bool operator!=(const AnnoTargetImpl &other) const
void setOp(Operation *op)
Operation * getOp() const
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)
circt::firrtl::detail::AnnoTargetImpl AnnoTargetImpl
static unsigned getHashValue(AnnoTarget val)
circt::firrtl::AnnoTarget AnnoTarget
static unsigned getHashValue(Annotation val)
circt::firrtl::Annotation Annotation
static bool isEqual(Annotation lhs, Annotation rhs)
static Annotation getFromVoidPointer(void *p)
static void * getAsVoidPointer(Annotation v)
circt::firrtl::Annotation Annotation