Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIRRTL] AnnoTarget: use LLVM style casts #7002

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/circt/Dialect/FIRRTL/FIRRTLAnnotationHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct AnnoPathValue {

template <typename... T>
bool isOpOfType() const {
if (auto opRef = ref.dyn_cast<OpAnnoTarget>())
if (auto opRef = dyn_cast<OpAnnoTarget>(ref))
return isa<T...>(opRef.getOp());
return false;
}
Expand Down Expand Up @@ -127,9 +127,9 @@ static T &operator<<(T &os, const PortAnnoTarget &target) {

template <typename T>
static T &operator<<(T &os, const AnnoTarget &target) {
if (auto op = target.dyn_cast<OpAnnoTarget>())
if (auto op = dyn_cast<OpAnnoTarget>(target))
os << op;
else if (auto port = target.dyn_cast<PortAnnoTarget>())
else if (auto port = dyn_cast<PortAnnoTarget>(target))
os << port;
else
os << "<<Unknown Anno Target>>";
Expand Down Expand Up @@ -481,7 +481,7 @@ template <bool allowNonLocal, bool allowPortAnnoTarget, typename T,
static LogicalResult applyWithoutTarget(const AnnoPathValue &target,
DictionaryAttr anno,
ApplyState &state) {
if (target.ref.isa<PortAnnoTarget>()) {
if (isa<PortAnnoTarget>(target.ref)) {
if (!allowPortAnnoTarget)
return failure();
} else if (!target.isOpOfType<T, Tr...>())
Expand Down
37 changes: 18 additions & 19 deletions include/circt/Dialect/FIRRTL/FIRRTLAnnotations.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,25 +415,6 @@ struct AnnoTargetImpl {
struct AnnoTarget {
AnnoTarget(detail::AnnoTargetImpl impl = nullptr) : impl(impl){};

template <typename U>
bool isa() const { // NOLINT(readability-identifier-naming)
assert(*this && "isa<> used on a null type.");
return U::classof(*this);
}
template <typename U>
U dyn_cast() const { // NOLINT(readability-identifier-naming)
return isa<U>() ? U(impl) : U(nullptr);
}
template <typename U>
U dyn_cast_or_null() const { // NOLINT(readability-identifier-naming)
return (*this && isa<U>()) ? U(impl) : U(nullptr);
}
template <typename U>
U cast() const {
assert(isa<U>());
return U(impl);
}

operator bool() const { return impl; }
bool operator==(const AnnoTarget &other) const { return impl == other.impl; }
bool operator!=(const AnnoTarget &other) const { return !(*this == other); }
Expand Down Expand Up @@ -522,6 +503,24 @@ LogicalResult extractDUT(FModuleOp mod, FModuleOp &dut);

namespace llvm {

/// Add support for llvm style casts to AnnoTarget.
template <typename To, typename From>
struct CastInfo<
To, From,
std::enable_if_t<std::is_base_of_v<::circt::firrtl::AnnoTarget, From>>>
: NullableValueCastFailed<To>,
DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
static inline bool isPossible(From target) {
// Allow constant upcasting. This also gets around the fact that AnnoTarget
// does not implement classof.
dtzSiFive marked this conversation as resolved.
Show resolved Hide resolved
if constexpr (std::is_base_of_v<To, From>)
return true;
else
return To::classof(target);
}
static inline To doCast(From target) { return To(target.getImpl()); }
};

/// Make `Annotation` behave like a `Attribute` in terms of pointer-likeness.
template <>
struct PointerLikeTypeTraits<circt::firrtl::Annotation>
Expand Down
2 changes: 1 addition & 1 deletion lib/Dialect/FIRRTL/FIRRTLAnnotationHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ firrtl::resolveEntities(TokenAnnoTarget path, CircuitOp circuit,
// AnnoTarget::getType() is not safe (CHIRRTL ops crash, null if instance),
// avoid. For now, only references in ports can be targets, check that.
// TODO: containsReference().
if (ref.isa<PortAnnoTarget>() && isa<RefType>(ref.getType())) {
if (isa<PortAnnoTarget>(ref) && isa<RefType>(ref.getType())) {
mlir::emitError(circuit.getLoc())
<< "cannot target reference-type '" << path.name << "' in "
<< mod.getModuleName();
Expand Down
4 changes: 2 additions & 2 deletions lib/Dialect/FIRRTL/Transforms/LegacyWiring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LogicalResult circt::firrtl::applyWiring(const AnnoPathValue &target,

// Convert target to Value
Value targetValue;
if (auto portTarget = target.ref.dyn_cast<PortAnnoTarget>()) {
if (auto portTarget = dyn_cast<PortAnnoTarget>(target.ref)) {
auto portNum = portTarget.getImpl().getPortNo();
if (auto module = dyn_cast<FModuleOp>(portTarget.getOp())) {
if (clazz == wiringSourceAnnoClass) {
Expand Down Expand Up @@ -63,7 +63,7 @@ LogicalResult circt::firrtl::applyWiring(const AnnoPathValue &target,
return mlir::emitError(state.circuit.getLoc())
<< "Annotation has invalid target: " << anno;
}
} else if (auto opResult = target.ref.dyn_cast<OpAnnoTarget>()) {
} else if (auto opResult = dyn_cast<OpAnnoTarget>(target.ref)) {
if (target.isOpOfType<WireOp, RegOp, RegResetOp>()) {
auto *targetBase = opResult.getOp();
builder.setInsertionPointAfter(targetBase);
Expand Down
10 changes: 5 additions & 5 deletions lib/Dialect/FIRRTL/Transforms/LowerAnnotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ static void addAnnotation(AnnoTarget ref, unsigned fieldIdx,
annotation = DictionaryAttr::get(context, anno);
}

if (ref.isa<OpAnnoTarget>()) {
if (isa<OpAnnoTarget>(ref)) {
auto newAnno = appendArrayAttr(getAnnotationsFrom(ref.getOp()), annotation);
ref.getOp()->setAttr(getAnnotationAttrName(), newAnno);
return;
}

auto portRef = ref.cast<PortAnnoTarget>();
auto portRef = cast<PortAnnoTarget>(ref);
auto portAnnoRaw = ref.getOp()->getAttr(getPortAnnotationAttrName());
ArrayAttr portAnno = dyn_cast_or_null<ArrayAttr>(portAnnoRaw);
if (!portAnno || portAnno.size() != getNumPorts(ref.getOp())) {
Expand Down Expand Up @@ -244,7 +244,7 @@ static LogicalResult applyDUTAnno(const AnnoPathValue &target,
if (!target.isLocal())
return mlir::emitError(loc) << "must be local";

if (!target.ref.isa<OpAnnoTarget>() || !isa<FModuleOp>(op))
if (!isa<OpAnnoTarget>(target.ref) || !isa<FModuleOp>(op))
return mlir::emitError(loc) << "can only target to a module";

auto moduleOp = cast<FModuleOp>(op);
Expand Down Expand Up @@ -277,7 +277,7 @@ static LogicalResult applyConventionAnno(const AnnoPathValue &target,
return diag;
};

auto opTarget = target.ref.dyn_cast<OpAnnoTarget>();
auto opTarget = dyn_cast<OpAnnoTarget>(target.ref);
if (!opTarget)
return error() << "must target a module object";

Expand Down Expand Up @@ -320,7 +320,7 @@ static LogicalResult applyAttributeAnnotation(const AnnoPathValue &target,
return diag;
};

if (!target.ref.isa<OpAnnoTarget>())
if (!isa<OpAnnoTarget>(target.ref))
return error()
<< "must target an operation. Currently ports are not supported";

Expand Down
2 changes: 1 addition & 1 deletion lib/Dialect/FIRRTL/Transforms/LowerClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ LogicalResult LowerClassesPass::processPaths(

// Attach an inner sym to the operation.
Attribute targetSym;
if (auto portTarget = target.dyn_cast<PortAnnoTarget>()) {
if (auto portTarget = dyn_cast<PortAnnoTarget>(target)) {
targetSym = getInnerRefTo(
{portTarget.getPortNo(), portTarget.getOp(), fieldID},
[&](FModuleLike module) -> hw::InnerSymbolNamespace & {
Expand Down
2 changes: 1 addition & 1 deletion lib/Dialect/FIRRTL/Transforms/ResolveTraces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct ResolveTracesPass : public ResolveTracesBase<ResolveTracesPass> {

// If this targets a module or an instance, then we're done. There is no
// "reference" part of the FIRRTL target.
if (path.ref.isa<OpAnnoTarget>() &&
if (isa<OpAnnoTarget>(path.ref) &&
path.isOpOfType<FModuleOp, FExtModuleOp, InstanceOp>())
return;

Expand Down
Loading