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] Use preferred cast style, NFC #5401

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion include/circt/Dialect/FIRRTL/FIRRTLAnnotationHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ A tryGetAs(DictionaryAttr &dict, const Attribute &root, StringRef key,
return nullptr;
}
// Check that the value has the correct type.
auto valueA = value.dyn_cast_or_null<A>();
auto valueA = dyn_cast_or_null<A>(value);
if (!valueA) {
SmallString<128> msg;
if (path.isTriviallyEmpty())
Expand Down
4 changes: 2 additions & 2 deletions include/circt/Dialect/FIRRTL/FIRRTLOpInterfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct PortInfo {
bool isOutput() {
if (direction != Direction::Out)
return false;
if (type.isa<FIRRTLType>())
if (isa<FIRRTLType>(type))
return !isInOut();
return true;
}
Expand All @@ -61,7 +61,7 @@ struct PortInfo {
bool isInput() {
if (direction != Direction::In)
return false;
if (type.isa<FIRRTLType>())
if (isa<FIRRTLType>(type))
return !isInOut();
return true;
}
Expand Down
7 changes: 3 additions & 4 deletions include/circt/Dialect/FIRRTL/FIRRTLTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ class FIRRTLBaseType
/// Support method to enable LLVM-style type casting.
static bool classof(Type type) {
return llvm::isa<FIRRTLDialect>(type.getDialect()) &&
!type.isa<PropertyType, RefType, OpenBundleType, OpenVectorType>();
!llvm::isa<PropertyType, RefType, OpenBundleType, OpenVectorType>(
type);
}

/// Returns true if this is a non-const "passive" that which is not analog.
Expand Down Expand Up @@ -297,9 +298,7 @@ class IntType : public FIRRTLBaseType, public WidthQualifiedTypeTrait<IntType> {
/// Return a 'const' or non-'const' version of this type.
IntType getConstType(bool isConst);

static bool classof(Type type) {
return type.isa<SIntType>() || type.isa<UIntType>();
}
static bool classof(Type type) { return llvm::isa<SIntType, UIntType>(type); }
};

//===----------------------------------------------------------------------===//
Expand Down
148 changes: 71 additions & 77 deletions lib/Conversion/FIRRTLToHW/LowerToHW.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/Dialect/FIRRTL/CHIRRTLDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ LogicalResult MemoryPortOp::inferReturnTypes(
DictionaryAttr attrs, mlir::OpaqueProperties properties,
mlir::RegionRange regions, SmallVectorImpl<Type> &results) {
auto inType = operands[0].getType();
auto memType = inType.dyn_cast<CMemoryType>();
auto memType = dyn_cast<CMemoryType>(inType);
if (!memType) {
if (loc)
mlir::emitError(*loc, "memory port requires memory operand");
Expand Down Expand Up @@ -195,7 +195,7 @@ LogicalResult MemoryDebugPortOp::inferReturnTypes(
DictionaryAttr attrs, mlir::OpaqueProperties properties,
mlir::RegionRange regions, SmallVectorImpl<Type> &results) {
auto inType = operands[0].getType();
auto memType = inType.dyn_cast<CMemoryType>();
auto memType = dyn_cast<CMemoryType>(inType);
if (!memType) {
if (loc)
mlir::emitError(*loc, "memory port requires memory operand");
Expand Down
6 changes: 3 additions & 3 deletions lib/Dialect/FIRRTL/Export/FIREmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void Emitter::emitModule(FExtModuleOp op) {

// Emit the parameters.
for (auto param : llvm::map_range(op.getParameters(), [](Attribute attr) {
return attr.cast<ParamDeclAttr>();
return cast<ParamDeclAttr>(attr);
})) {
startStatement();
// TODO: AssignLike ?
Expand Down Expand Up @@ -634,7 +634,7 @@ void Emitter::emitStatement(MemOp op) {
auto portNameBaseLen = portName.size();
for (auto result : llvm::zip(op.getResults(), op.getPortNames())) {
portName.resize(portNameBaseLen);
portName.append(std::get<1>(result).cast<StringAttr>().getValue());
portName.append(cast<StringAttr>(std::get<1>(result)).getValue());
addValueName(std::get<0>(result), portName);
}

Expand Down Expand Up @@ -888,7 +888,7 @@ void Emitter::emitExpression(SpecialConstantOp op) {
ps.addAsString(op.getValue());
ps << ")";
};
TypeSwitch<FIRRTLType>(op.getType().cast<FIRRTLType>())
TypeSwitch<FIRRTLType>(cast<FIRRTLType>(op.getType()))
.Case<ClockType>([&](auto type) {
ps << "asClock(";
emitInner();
Expand Down
12 changes: 6 additions & 6 deletions lib/Dialect/FIRRTL/FIRRTLAnnotationHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static LogicalResult updateExpandedPort(StringRef field, AnnoTarget &ref) {
/// represent bundle returns as split into constituent parts.
static FailureOr<unsigned> findBundleElement(Operation *op, Type type,
StringRef field) {
auto bundle = type.dyn_cast<BundleType>();
auto bundle = dyn_cast<BundleType>(type);
if (!bundle) {
op->emitError("field access '")
<< field << "' into non-bundle type '" << bundle << "'";
Expand All @@ -66,7 +66,7 @@ static FailureOr<unsigned> findVectorElement(Operation *op, Type type,
op->emitError("Cannot convert '") << indexStr << "' to an integer";
return failure();
}
auto vec = type.dyn_cast<FVectorType>();
auto vec = dyn_cast<FVectorType>(type);
if (!vec) {
op->emitError("index access '")
<< index << "' into non-vector type '" << type << "'";
Expand Down Expand Up @@ -95,14 +95,14 @@ static FailureOr<unsigned> findFieldID(AnnoTarget &ref,
auto result = findVectorElement(op, type, token.name);
if (failed(result))
return failure();
auto vector = type.cast<FVectorType>();
auto vector = cast<FVectorType>(type);
type = vector.getElementType();
fieldIdx += vector.getFieldID(*result);
} else {
auto result = findBundleElement(op, type, token.name);
if (failed(result))
return failure();
auto bundle = type.cast<BundleType>();
auto bundle = cast<BundleType>(type);
type = bundle.getElementType(*result);
fieldIdx += bundle.getFieldID(*result);
}
Expand Down Expand Up @@ -510,7 +510,7 @@ LogicalResult circt::firrtl::applyGCTDataTaps(const AnnoPathValue &target,
for (size_t i = 0, e = keyAttr.size(); i != e; ++i) {
auto b = keyAttr[i];
auto path = ("keys[" + Twine(i) + "]").str();
auto bDict = b.cast<DictionaryAttr>();
auto bDict = cast<DictionaryAttr>(b);
auto classAttr =
tryGetAs<StringAttr>(bDict, anno, "class", loc, dataTapsClass, path);
if (!classAttr)
Expand Down Expand Up @@ -573,7 +573,7 @@ LogicalResult circt::firrtl::applyGCTDataTaps(const AnnoPathValue &target,
if (!moduleTarget)
return failure();
AnnoPathValue internalPathSrc;
auto targetType = wireTarget->ref.getType().cast<FIRRTLBaseType>();
auto targetType = cast<FIRRTLBaseType>(wireTarget->ref.getType());
if (wireTarget->fieldIdx)
targetType = cast<FIRRTLBaseType>(
targetType.getFinalTypeByFieldID(wireTarget->fieldIdx));
Expand Down
16 changes: 8 additions & 8 deletions lib/Dialect/FIRRTL/FIRRTLAnnotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ AnnotationSet::AnnotationSet(Operation *op)
static AnnotationSet forPort(Operation *op, size_t portNo) {
auto ports = op->getAttrOfType<ArrayAttr>(getPortAnnotationAttrName());
if (ports && !ports.empty())
return AnnotationSet(ports[portNo].cast<ArrayAttr>());
return AnnotationSet(cast<ArrayAttr>(ports[portNo]));
return AnnotationSet(ArrayAttr::get(op->getContext(), {}));
}

Expand All @@ -79,7 +79,7 @@ AnnotationSet AnnotationSet::get(Value v) {
if (auto op = v.getDefiningOp())
return AnnotationSet(op);
// If its not an Operation, then must be a block argument.
auto arg = v.dyn_cast<BlockArgument>();
auto arg = dyn_cast<BlockArgument>(v);
auto module = cast<FModuleOp>(arg.getOwner()->getParentOp());
return forPort(module, arg.getArgNumber());
}
Expand Down Expand Up @@ -411,7 +411,7 @@ bool AnnotationSet::removePortAnnotations(
bool changed = false;
for (unsigned argNum = 0, argNumEnd = ports.size(); argNum < argNumEnd;
++argNum) {
AnnotationSet annos(AnnotationSet(ports[argNum].cast<ArrayAttr>()));
AnnotationSet annos(AnnotationSet(cast<ArrayAttr>(ports[argNum])));

// Go through all annotations on this port and extract the interesting
// ones. If any modifications were done, keep a reduced set of attributes
Expand All @@ -434,7 +434,7 @@ bool AnnotationSet::removePortAnnotations(
//===----------------------------------------------------------------------===//

DictionaryAttr Annotation::getDict() const {
return attr.cast<DictionaryAttr>();
return cast<DictionaryAttr>(attr);
}

void Annotation::setDict(DictionaryAttr dict) { attr = dict; }
Expand Down Expand Up @@ -609,12 +609,12 @@ FIRRTLType OpAnnoTarget::getType() const {
auto result = is.getTargetResult();
if (!result)
return {};
return result.getType().cast<FIRRTLType>();
return llvm::cast<FIRRTLType>(result.getType());
}
// Fallback to assuming the single result is the target.
if (op->getNumResults() != 1)
return {};
return op->getResult(0).getType().cast<FIRRTLType>();
return llvm::cast<FIRRTLType>(op->getResult(0).getType());
}

PortAnnoTarget::PortAnnoTarget(FModuleLike op, unsigned portNo)
Expand Down Expand Up @@ -674,9 +674,9 @@ PortAnnoTarget::getNLAReference(ModuleNamespace &moduleNamespace) const {
FIRRTLType PortAnnoTarget::getType() const {
auto *op = getOp();
if (auto module = llvm::dyn_cast<FModuleLike>(op))
return module.getPortType(getPortNo()).cast<FIRRTLType>();
return llvm::cast<FIRRTLType>(module.getPortType(getPortNo()));
if (llvm::isa<MemOp, InstanceOp>(op))
return op->getResult(getPortNo()).getType().cast<FIRRTLType>();
return llvm::cast<FIRRTLType>(op->getResult(getPortNo()).getType());
llvm_unreachable("unknow operation kind");
return {};
}
Expand Down
24 changes: 11 additions & 13 deletions lib/Dialect/FIRRTL/FIRRTLDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,36 @@ Operation *FIRRTLDialect::materializeConstant(OpBuilder &builder,
// Boolean constants. Boolean attributes are always a special constant type
// like ClockType and ResetType. Since BoolAttrs are also IntegerAttrs, its
// important that this goes first.
if (auto attrValue = value.dyn_cast<BoolAttr>()) {
assert((type.isa<ClockType>() || type.isa<AsyncResetType>() ||
type.isa<ResetType>()) &&
"BoolAttrs can only be materialized for special constant types.");
if (auto attrValue = dyn_cast<BoolAttr>(value)) {
assert((isa<ClockType, AsyncResetType, ResetType>(type) &&
"BoolAttrs can only be materialized for special constant types."));
return builder.create<SpecialConstantOp>(loc, type, attrValue);
}

// Integer constants.
if (auto attrValue = value.dyn_cast<IntegerAttr>()) {
if (auto attrValue = dyn_cast<IntegerAttr>(value)) {
// Integer attributes (ui1) might still be special constant types.
if (attrValue.getValue().getBitWidth() == 1 &&
(type.isa<ClockType>() || type.isa<AsyncResetType>() ||
type.isa<ResetType>()))
isa<ClockType, AsyncResetType, ResetType>(type))
return builder.create<SpecialConstantOp>(
loc, type, builder.getBoolAttr(attrValue.getValue().isAllOnes()));

assert((!type.cast<IntType>().hasWidth() ||
(unsigned)type.cast<IntType>().getWidthOrSentinel() ==
assert((!cast<IntType>(type).hasWidth() ||
(unsigned)cast<IntType>(type).getWidthOrSentinel() ==
attrValue.getValue().getBitWidth()) &&
"type/value width mismatch materializing constant");
return builder.create<ConstantOp>(loc, type, attrValue);
}

// Aggregate constants.
if (auto arrayAttr = value.dyn_cast<ArrayAttr>()) {
if (type.isa<BundleType, FVectorType>())
if (auto arrayAttr = dyn_cast<ArrayAttr>(value)) {
if (isa<BundleType, FVectorType>(type))
return builder.create<AggregateConstantOp>(loc, type, arrayAttr);
}

// String constants.
if (auto stringAttr = value.dyn_cast<StringAttr>()) {
if (type.isa<StringType>())
if (auto stringAttr = dyn_cast<StringAttr>(value)) {
if (isa<StringType>(type))
return builder.create<StringConstantOp>(loc, type, stringAttr);
}

Expand Down
Loading