18#include "mlir/IR/ImplicitLocOpBuilder.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/TypeSwitch.h"
21#include "llvm/Support/Path.h"
24using namespace firrtl;
31 Value &cached =
cache[type];
33 cached = InvalidValueOp::create(
builder, type);
38 Value &cached =
cache[type];
40 cached =
builder.create<UnknownValueOp>(type);
50 ImplicitLocOpBuilder locBuilder(loc, builder.getInsertionBlock(),
51 builder.getInsertionPoint());
53 builder.restoreInsertionPoint(locBuilder.saveInsertionPoint());
59 auto dstFType = type_cast<FIRRTLType>(dst.getType());
60 auto srcFType = type_cast<FIRRTLType>(src.getType());
61 auto dstType = type_dyn_cast<FIRRTLBaseType>(dstFType);
62 auto srcType = type_dyn_cast<FIRRTLBaseType>(srcFType);
66 if (type_isa<RefType>(dstFType)) {
67 if (dstFType != srcFType)
68 src = RefCastOp::create(builder, dstFType, src);
69 RefDefineOp::create(builder, dst, src);
70 }
else if (type_isa<PropertyType>(dstFType) &&
71 type_isa<PropertyType>(srcFType)) {
73 PropAssignOp::create(builder, dst, src);
74 }
else if (type_isa<DomainType>(dstFType) &&
75 type_isa<DomainType>(srcFType)) {
76 DomainDefineOp::create(builder, dst, src);
79 ConnectOp::create(builder, dst, src);
85 if (isa<AnalogType>(dstType)) {
86 AttachOp::create(builder, ArrayRef{dst, src});
91 if (dstType == srcType && dstType.isPassive() &&
92 !dstType.hasUninferredWidth()) {
93 MatchingConnectOp::create(builder, dst, src);
97 if (
auto dstBundle = type_dyn_cast<BundleType>(dstType)) {
102 auto srcBundle = type_dyn_cast<BundleType>(srcType);
103 if (!srcBundle ||
numElements != srcBundle.getNumElements()) {
104 ConnectOp::create(builder, dst, src);
108 auto dstField = SubfieldOp::create(builder, dst, i);
109 auto srcField = SubfieldOp::create(builder, src, i);
110 if (dstBundle.getElement(i).isFlip)
111 std::swap(dstField, srcField);
117 if (
auto dstVector = type_dyn_cast<FVectorType>(dstType)) {
122 auto srcVector = type_dyn_cast<FVectorType>(srcType);
123 if (!srcVector ||
numElements != srcVector.getNumElements()) {
124 ConnectOp::create(builder, dst, src);
128 auto dstField = SubindexOp::create(builder, dst, i);
129 auto srcField = SubindexOp::create(builder, src, i);
135 if ((dstType.hasUninferredReset() || srcType.hasUninferredReset()) &&
136 dstType != srcType) {
137 srcType = dstType.getConstType(srcType.isConst());
138 src = UninferredResetCastOp::create(builder, srcType, src);
142 auto dstWidth = dstType.getBitWidthOrSentinel();
143 auto srcWidth = srcType.getBitWidthOrSentinel();
144 if (dstWidth < 0 || srcWidth < 0) {
151 if (dstType != srcType && dstType.getWidthlessType() != srcType &&
153 src = ConstCastOp::create(builder, dstType.getWidthlessType(), src);
156 ConnectOp::create(builder, dst, src);
161 if (dstWidth < srcWidth) {
164 type_cast<IntType>(dstType).
getConstType(srcType.isConst());
165 bool isSignedDest = tmpType.
isSigned();
168 UIntType::get(dstType.getContext(), dstWidth, srcType.isConst());
169 src = TailPrimOp::create(builder, tmpType, src, srcWidth - dstWidth);
172 src = AsSIntPrimOp::create(builder,
173 dstType.getConstType(tmpType.
isConst()), src);
174 }
else if (srcWidth < dstWidth) {
176 src = PadPrimOp::create(builder, src, dstWidth);
179 if (
auto srcType = type_cast<FIRRTLBaseType>(src.getType());
180 srcType && dstType != srcType &&
182 src = ConstCastOp::create(builder, dstType, src);
187 if (dstType == src.getType() && dstType.isPassive() &&
188 !dstType.hasUninferredWidth()) {
189 MatchingConnectOp::create(builder, dst, src);
191 ConnectOp::create(builder, dst, src);
195 auto intType = type_cast<IntType>(type);
196 assert((!intType.hasWidth() ||
197 (
unsigned)intType.getWidthOrSentinel() == value.getBitWidth()) &&
198 "value / type width mismatch");
200 intType.isSigned() ? IntegerType::Signed : IntegerType::Unsigned;
202 IntegerType::get(type.getContext(), value.getBitWidth(), intSign);
203 return IntegerAttr::get(attrType, value);
209 int32_t width = abs(type_cast<IntType>(type).getWidthOrSentinel());
216 int32_t width = abs(type_cast<IntType>(type).getWidthOrSentinel());
218 type, APInt(width, -1,
false,
true));
224 for (
auto *user : value.getUsers())
225 if (
auto propassign = dyn_cast<PropAssignOp>(user))
226 if (propassign.getDest() == value)
241 for (
auto *user : val.getUsers()) {
242 if (
auto connect = dyn_cast<FConnectLike>(user)) {
243 if (connect.getDest() != val)
245 return connect.getSrc();
252 bool lookThroughNodes,
253 bool lookThroughCasts) {
258 auto updateVal = [&](Value thisVal) {
259 for (
auto *user : thisVal.getUsers()) {
260 if (
auto connect = dyn_cast<FConnectLike>(user)) {
261 if (connect.getDest() != val)
263 val = connect.getSrc();
273 if (
auto blockArg = dyn_cast<BlockArgument>(val)) {
274 FModuleOp op = cast<FModuleOp>(val.getParentBlock()->getParentOp());
275 auto direction = op.getPortDirection(blockArg.getArgNumber());
283 auto *op = val.getDefiningOp();
286 if (
auto inst = dyn_cast<InstanceOp>(op)) {
287 auto resultNo = cast<OpResult>(val).getResultNumber();
290 return inst.getResult(resultNo);
297 updateVal(op->getResult(0));
302 if (lookThroughNodes && isa<NodeOp>(op)) {
303 val = cast<NodeOp>(op).getInput();
307 if (lookThroughCasts &&
308 isa<AsUIntPrimOp, AsSIntPrimOp, AsClockPrimOp, AsAsyncResetPrimOp>(
310 val = op->getOperand(0);
315 if (isa<PadPrimOp, TailPrimOp>(op)) {
316 val = op->getOperand(0);
331 bool lookThroughNodes,
bool lookThroughCasts,
335 assert(value.getType().isPassive() &&
"this code was not tested with flips");
346 struct StackElement {
348 : dst(dst), src(src), current(current), it(current.user_begin()),
361 Value::user_iterator it;
365 SmallVector<StackElement> workStack;
371 auto value = src.getValue();
372 workStack.emplace_back(dst, src, value, src.
getFieldID());
378 auto fieldRef = original;
386 auto val = fieldRef.getValue();
389 if (
auto blockArg = dyn_cast<BlockArgument>(val)) {
390 auto *parent = val.getParentBlock()->getParentOp();
391 auto module = cast<FModuleLike>(parent);
392 auto direction =
module.getPortDirection(blockArg.getArgNumber());
395 if (!callback(original, fieldRef))
399 addToWorklist(original, fieldRef);
403 auto *op = val.getDefiningOp();
406 if (
auto inst = dyn_cast<InstanceOp>(op)) {
407 auto resultNo = cast<OpResult>(val).getResultNumber();
410 if (!callback(original, fieldRef))
414 addToWorklist(original, fieldRef);
420 addToWorklist(original, fieldRef);
425 if (lookThroughNodes && isa<NodeOp>(op)) {
426 auto input = cast<NodeOp>(op).getInput();
428 fieldRef = next.getSubField(fieldRef.getFieldID());
433 if (lookThroughCasts &&
434 isa<AsUIntPrimOp, AsSIntPrimOp, AsClockPrimOp, AsAsyncResetPrimOp>(
436 auto input = op->getOperand(0);
438 fieldRef = next.getSubField(fieldRef.getFieldID());
443 if (isa<PadPrimOp, TailPrimOp>(op)) {
444 auto input = op->getOperand(0);
446 fieldRef = next.getSubField(fieldRef.getFieldID());
455 if (!callback(original, fieldRef))
463 if (workStack.empty())
465 auto &back = workStack.back();
466 auto current = back.current;
468 if (back.it == current.user_end()) {
469 workStack.pop_back();
475 auto *user = *back.it++;
476 auto fieldID = back.fieldID;
478 if (
auto subfield = dyn_cast<SubfieldOp>(user)) {
479 BundleType bundleType = subfield.getInput().getType();
480 auto index = subfield.getFieldIndex();
481 auto subID = bundleType.getFieldID(index);
483 if (fieldID && index != bundleType.getIndexForFieldID(fieldID))
485 auto subRef = fieldRef.getSubField(subID);
486 auto subOriginal = original.getSubField(subID);
487 auto value = subfield.getResult();
490 workStack.emplace_back(subOriginal, subRef, value, 0);
493 workStack.emplace_back(subOriginal, subRef, value, fieldID - subID);
495 }
else if (
auto subindex = dyn_cast<SubindexOp>(user)) {
496 FVectorType vectorType = subindex.getInput().getType();
497 auto index = subindex.getIndex();
498 auto subID = vectorType.getFieldID(index);
500 if (fieldID && index != vectorType.getIndexForFieldID(fieldID))
502 auto subRef = fieldRef.getSubField(subID);
503 auto subOriginal = original.getSubField(subID);
504 auto value = subindex.getResult();
507 workStack.emplace_back(subOriginal, subRef, value, 0);
510 workStack.emplace_back(subOriginal, subRef, value, fieldID - subID);
512 }
else if (
auto connect = dyn_cast<FConnectLike>(user)) {
514 if (connect.getDest() != current)
532 if (LLVM_UNLIKELY(!value))
536 auto *op = value.getDefiningOp();
543 return TypeSwitch<Operation *, FieldRef>(op)
544 .Case<RefCastOp, ConstCastOp, UninferredResetCastOp>(
545 [lookThroughCasts](
auto op) {
546 if (!lookThroughCasts)
550 .Case<SubfieldOp, OpenSubfieldOp, SubindexOp, OpenSubindexOp, RefSubOp,
552 [](
auto subOp) {
return subOp.getAccessedField(); })
557 bool lookThroughCasts) {
558 if (LLVM_UNLIKELY(!value))
564 auto deltaRef =
getDeltaRef(value, lookThroughCasts);
568 id = deltaRef.getSubField(
id).getFieldID();
570 value = deltaRef.getValue();
575static void getDeclName(Value value, SmallString<64> &
string,
bool nameSafe) {
578 if (
auto arg = dyn_cast<BlockArgument>(value)) {
580 auto *op = arg.getOwner()->getParentOp();
581 TypeSwitch<Operation *>(op).Case<FModuleOp, ClassOp>([&](
auto op) {
582 auto name = cast<StringAttr>(op.getPortNames()[arg.getArgNumber()]);
583 string += name.getValue();
588 auto *op = value.getDefiningOp();
589 TypeSwitch<Operation *>(op)
590 .Case<ObjectOp>([&](ObjectOp op) {
591 string += op.getInstanceName();
594 .Case<InstanceOp, MemOp>([&](
auto op) {
595 string += op.getName();
596 string += nameSafe ?
"_" :
".";
597 string += op.getPortName(cast<OpResult>(value).getResultNumber());
600 .Case<FNamableOp>([&](
auto op) {
601 string += op.getName();
604 .Case<mlir::UnrealizedConversionCastOp>(
605 [&](mlir::UnrealizedConversionCastOp cast) {
607 if (cast.getNumResults() == 1 && cast.getNumOperands() == 1 &&
608 cast.getResult(0).getType() == cast.getOperand(0).getType()) {
609 value = cast.getInputs()[0];
624std::pair<std::string, bool>
626 SmallString<64> name;
629 bool rootKnown = !name.empty();
631 auto type = value.getType();
635 if (
auto refTy = type_dyn_cast<RefType>(type))
636 type = refTy.getType();
638 if (
auto bundleType = type_dyn_cast<BundleType>(type)) {
639 auto index = bundleType.getIndexForFieldID(localID);
641 auto &element = bundleType.getElements()[index];
643 name += nameSafe ?
"_" :
".";
644 name += element.name.getValue();
647 localID = localID - bundleType.getFieldID(index);
648 }
else if (
auto bundleType = type_dyn_cast<OpenBundleType>(type)) {
649 auto index = bundleType.getIndexForFieldID(localID);
651 auto &element = bundleType.getElements()[index];
653 name += nameSafe ?
"_" :
".";
654 name += element.name.getValue();
657 localID = localID - bundleType.getFieldID(index);
658 }
else if (
auto vecType = type_dyn_cast<FVectorType>(type)) {
659 auto index = vecType.getIndexForFieldID(localID);
660 name += nameSafe ?
"_" :
"[";
661 name += std::to_string(index);
665 type = vecType.getElementType();
666 localID = localID - vecType.getFieldID(index);
667 }
else if (
auto vecType = type_dyn_cast<OpenVectorType>(type)) {
668 auto index = vecType.getIndexForFieldID(localID);
669 name += nameSafe ?
"_" :
"[";
670 name += std::to_string(index);
674 type = vecType.getElementType();
675 localID = localID - vecType.getFieldID(index);
676 }
else if (
auto classType = type_dyn_cast<ClassType>(type)) {
677 auto index = classType.getIndexForFieldID(localID);
678 auto &element = classType.getElement(index);
679 name += nameSafe ?
"_" :
".";
680 name += element.name.getValue();
682 localID = localID - classType.getFieldID(index);
688 llvm_unreachable(
"unsupported type");
692 return {name.str().str(), rootKnown};
700 Value value,
unsigned fieldID) {
702 while (fieldID != 0) {
704 .
Case<BundleType, OpenBundleType>([&](
auto bundle) {
705 auto index = bundle.getIndexForFieldID(fieldID);
706 value = SubfieldOp::create(builder, value, index);
707 fieldID -= bundle.getFieldID(index);
709 .Case<FVectorType, OpenVectorType>([&](
auto vector) {
710 auto index = vector.getIndexForFieldID(fieldID);
711 value = SubindexOp::create(builder, value, index);
712 fieldID -= vector.getFieldID(index);
714 .Case<RefType>([&](
auto reftype) {
716 .template Case<BundleType, FVectorType>([&](
auto type) {
717 auto index = type.getIndexForFieldID(fieldID);
718 value = RefSubOp::create(builder, value, index);
719 fieldID -= type.getFieldID(index);
721 .Default([&](
auto _) {
722 llvm::report_fatal_error(
723 "unrecognized type for indexing through with fieldID");
727 .Default([&](
auto _) {
728 llvm::report_fatal_error(
729 "unrecognized type for indexing through with fieldID");
749 return fn(0, type,
false);
751 uint64_t fieldID = 0;
752 auto recurse = [&](
auto &&f,
FIRRTLBaseType type,
bool isFlip) ->
void {
754 .
Case<BundleType>([&](BundleType bundle) {
755 for (
size_t i = 0, e = bundle.getNumElements(); i < e; ++i) {
757 f(f, bundle.getElementType(i),
758 isFlip ^ bundle.getElement(i).isFlip);
761 .
template Case<FVectorType>([&](FVectorType vector) {
762 for (
size_t i = 0, e = vector.getNumElements(); i < e; ++i) {
764 f(f, vector.getElementType(), isFlip);
767 .
template Case<FEnumType>([&](FEnumType fenum) {
771 fn(fieldID, fenum, isFlip);
774 assert(groundType.isGround() &&
775 "only ground types are expected here");
776 fn(fieldID, groundType, isFlip);
779 recurse(recurse, type,
false);
786 if (
auto arg = dyn_cast<BlockArgument>(root)) {
787 auto mod = cast<FModuleLike>(arg.getOwner()->getParentOp());
798 return TypeSwitch<Operation *, FieldRef>(ist.
getOp())
799 .Case<FModuleOp>([&](
auto fmod) {
805 auto symOp = dyn_cast<hw::InnerSymbolOpInterface>(ist.
getOp());
806 assert(symOp && symOp.getTargetResultIndex() &&
807 (symOp.supportsPerFieldSymbols() || ist.
getField() == 0));
813 MLIRContext *
context, hw::InnerSymAttr attr, uint64_t fieldID,
815 SmallVector<hw::InnerSymPropertiesAttr> props;
818 if (
auto sym = attr.getSymIfExists(fieldID))
820 llvm::append_range(props, attr.getProps());
824 auto sym = StringAttr::get(
context, getNamespace().newName(
"sym"));
825 props.push_back(hw::InnerSymPropertiesAttr::get(
830 [](
auto &p,
auto &q) {
return p.getFieldID() < q.getFieldID(); });
831 return {hw::InnerSymAttr::get(
context, props), sym};
838 if (
auto mod = dyn_cast<FModuleLike>(target.
getOp())) {
839 auto portIdx = target.
getPort();
840 assert(portIdx < mod.getNumPorts());
844 mod.setPortSymbolAttr(portIdx, attr);
849 if (
auto symOp = dyn_cast<hw::InnerSymbolOpInterface>(target.
getOp())) {
853 symOp.setInnerSymbolAttr(attr);
858 assert(0 &&
"target must be port of FModuleLike or InnerSymbol");
866 module = cast<FModuleLike>(target.getOp());
868 module = target.getOp()->getParentOfType<FModuleOp>();
872 return getNamespace(module);
881 auto mod = target.
isPort() ? dyn_cast<FModuleLike>(target.
getOp())
882 : target.
getOp()->getParentOfType<FModuleOp>();
884 "must be an operation inside an FModuleOp or port of FModuleLike");
885 return hw::InnerRefAttr::get(SymbolTable::getSymbolName(mod),
890std::pair<bool, std::optional<mlir::LocationAttr>>
892 StringAttr &locatorFilenameCache,
893 FileLineColLoc &fileLineColLocCache,
896 if (!spelling.starts_with(
"@[") || !spelling.ends_with(
"]"))
897 return {
false, std::nullopt};
899 spelling = spelling.drop_front(2).drop_back(1);
903 auto decodeLocator = [&](StringRef input,
unsigned &resultLineNo,
904 unsigned &resultColNo) -> StringRef {
906 auto spaceLoc = input.find_last_of(
' ');
907 if (spaceLoc == StringRef::npos)
910 auto filename = input.take_front(spaceLoc);
911 auto lineAndColumn = input.drop_front(spaceLoc + 1);
915 StringRef lineStr, colStr;
916 std::tie(lineStr, colStr) = lineAndColumn.split(
':');
919 if (lineStr.getAsInteger(10, resultLineNo))
921 if (!colStr.empty()) {
922 if (colStr.front() !=
'{') {
923 if (colStr.getAsInteger(10, resultColNo))
927 if (colStr.drop_front().split(
',').first.getAsInteger(10, resultColNo))
935 unsigned lineNo = 0, columnNo = 0;
936 StringRef filename = decodeLocator(spelling, lineNo, columnNo);
937 if (filename.empty())
938 return {
false, std::nullopt};
943 return {
true, std::nullopt};
947 auto getFileLineColLoc = [&](StringRef filename,
unsigned lineNo,
948 unsigned columnNo) -> FileLineColLoc {
950 StringAttr filenameId = locatorFilenameCache;
951 if (filenameId.str() != filename) {
953 locatorFilenameCache = filenameId = StringAttr::get(
context, filename);
957 return fileLineColLocCache =
958 FileLineColLoc::get(filenameId, lineNo, columnNo);
962 auto result = fileLineColLocCache;
963 if (result && result.getLine() == lineNo && result.getColumn() == columnNo)
966 return fileLineColLocCache =
967 FileLineColLoc::get(filenameId, lineNo, columnNo);
978 SmallVector<Location> extraLocs;
979 auto spaceLoc = filename.find_last_of(
' ');
980 while (spaceLoc != StringRef::npos) {
983 unsigned nextLineNo = 0, nextColumnNo = 0;
985 decodeLocator(filename.take_front(spaceLoc), nextLineNo, nextColumnNo);
988 if (nextFilename.empty())
994 getFileLineColLoc(filename.drop_front(spaceLoc + 1), lineNo, columnNo);
995 extraLocs.push_back(loc);
996 filename = nextFilename;
998 columnNo = nextColumnNo;
999 spaceLoc = filename.find_last_of(
' ');
1002 mlir::LocationAttr result = getFileLineColLoc(filename, lineNo, columnNo);
1003 if (!extraLocs.empty()) {
1004 extraLocs.push_back(result);
1005 std::reverse(extraLocs.begin(), extraLocs.end());
1006 result = FusedLoc::get(
context, extraLocs);
1008 return {
true, result};
1015 Type type, std::optional<Location> loc,
1016 llvm::function_ref<hw::TypeAliasType(Type, BaseTypeAliasType, Location)>
1018 auto firType = type_dyn_cast<FIRRTLBaseType>(type);
1024 if (BaseTypeAliasType aliasType = dyn_cast<BaseTypeAliasType>(firType)) {
1026 loc = UnknownLoc::get(type.getContext());
1027 type =
lowerType(aliasType.getInnerType(), loc, getTypeDeclFn);
1028 return getTypeDeclFn(type, aliasType, *loc);
1031 firType = firType.getPassiveType();
1033 if (
auto bundle = type_dyn_cast<BundleType>(firType)) {
1034 mlir::SmallVector<hw::StructType::FieldInfo, 8> hwfields;
1035 for (
auto element : bundle) {
1036 Type etype =
lowerType(element.type, loc, getTypeDeclFn);
1039 hwfields.push_back(hw::StructType::FieldInfo{element.name, etype});
1041 return hw::StructType::get(type.getContext(), hwfields);
1043 if (
auto vec = type_dyn_cast<FVectorType>(firType)) {
1044 auto elemTy =
lowerType(vec.getElementType(), loc, getTypeDeclFn);
1047 return hw::ArrayType::get(elemTy, vec.getNumElements());
1049 if (
auto fenum = type_dyn_cast<FEnumType>(firType)) {
1050 mlir::SmallVector<hw::UnionType::FieldInfo, 8> hwfields;
1052 for (
auto element : fenum) {
1053 Type etype =
lowerType(element.type, loc, getTypeDeclFn);
1056 hwfields.push_back(hw::UnionType::FieldInfo{element.name, etype, 0});
1057 if (element.type.getBitWidthOrSentinel() != 0)
1060 auto tagTy = IntegerType::get(type.getContext(), fenum.getTagWidth());
1063 auto bodyTy = hw::UnionType::get(type.getContext(), hwfields);
1064 hw::StructType::FieldInfo fields[2] = {
1065 {StringAttr::get(type.getContext(),
"tag"), tagTy},
1066 {StringAttr::get(type.getContext(),
"body"), bodyTy}};
1067 return hw::StructType::get(type.getContext(), fields);
1069 if (type_isa<ClockType>(firType))
1070 return seq::ClockType::get(firType.getContext());
1072 auto width = firType.getBitWidthOrSentinel();
1074 return IntegerType::get(type.getContext(), width);
1082 size_t e = std::min(a.size(), b.size());
1089 auto sep = llvm::sys::path::get_separator();
1090 while (!a.empty() && !a.ends_with(sep))
1095 mlir::ImplicitLocOpBuilder &builderOM) {
1097 auto *
context = op->getContext();
1098 auto id = DistinctAttr::create(UnitAttr::get(
context));
1099 TargetKind kind = TargetKind::Reference;
1102 NamedAttrList fields;
1103 fields.append(
"id",
id);
1104 fields.append(
"class", StringAttr::get(
context,
"circt.tracker"));
1106 fields.append(
"circt.nonlocal", mlir::FlatSymbolRefAttr::get(nla));
1110 if (isa<InstanceOp, FModuleLike>(op))
1111 kind = TargetKind::Instance;
1115 return PathOp::create(builderOM, kind,
id);
1124 llvm::StringRef formatString,
1125 llvm::ArrayRef<mlir::Value> specOperands,
1126 mlir::StringAttr &formatStringResult,
1127 llvm::SmallVectorImpl<mlir::Value> &operands) {
1130 llvm::SmallString<64> validatedFormatString;
1132 for (
size_t i = 0, e = formatString.size(), opIdx = 0; i != e; ++i) {
1133 auto c = formatString[i];
1138 validatedFormatString.push_back(c);
1141 llvm::SmallString<6> width;
1142 c = formatString[++i];
1145 c = formatString[++i];
1152 return mlir::emitError(loc) <<
"ASCII character format specifiers "
1153 "('%c') may not specify a width";
1159 validatedFormatString.append(width);
1160 operands.push_back(specOperands[opIdx++]);
1164 return mlir::emitError(loc)
1165 <<
"literal percents ('%%') may not specify a width";
1169 return mlir::emitError(loc)
1170 <<
"unknown printf substitution '%" << width << c <<
"'";
1172 validatedFormatString.push_back(c);
1180 if (formatString[i + 1] !=
'{') {
1181 validatedFormatString.push_back(c);
1187 while (formatString[i] !=
'}')
1189 if (formatString[i] !=
'}')
1190 return mlir::emitError(loc)
1191 <<
"expected '}' to terminate special substitution";
1193 auto specialString = formatString.slice(start, i);
1194 if (specialString ==
"SimulationTime") {
1195 operands.push_back(TimeOp::create(builder, loc));
1196 }
else if (specialString ==
"HierarchicalModuleName") {
1197 operands.push_back(HierarchicalModuleNameOp::create(builder, loc));
1199 return mlir::emitError(loc)
1200 <<
"unknown printf substitution '" << specialString
1201 <<
"' (did you misspell it?)";
1204 validatedFormatString.append(
"{{}}");
1209 validatedFormatString.push_back(c);
1213 formatStringResult = builder.getStringAttr(validatedFormatString);
1214 return mlir::success();
assert(baseType &&"element must be base type")
MlirType uint64_t numElements
static std::unique_ptr< Context > context
static void getDeclName(Value value, SmallString< 64 > &string, bool nameSafe)
Get the string name of a value which is a direct child of a declaration op.
static Value lookThroughWires(Value value)
Trace a value through wires to its original definition.
This class represents a reference to a specific field or element of an aggregate value.
unsigned getFieldID() const
Get the field ID of this FieldRef, which is a unique identifier mapped to a specific field in a bundl...
Value getValue() const
Get the Value which created this location.
This class provides a read-only projection over the MLIR attributes that represent a set of annotatio...
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 isConst() const
Returns true if this is a 'const' type that can only hold compile-time constant values.
This class implements the same functionality as TypeSwitch except that it uses firrtl::type_dyn_cast ...
FIRRTLTypeSwitch< T, ResultT > & Case(CallableT &&caseFn)
Add a case on the given type.
This is the common base class between SIntType and UIntType.
IntType getConstType(bool isConst) const
Return a 'const' or non-'const' version of this type.
ImplicitLocOpBuilder & builder
Value getInvalid(FIRRTLBaseType type)
Get or create an InvalidValueOp for the given base type.
SmallDenseMap< Type, Value, 8 > cache
Value getUnknown(PropertyType type)
Get or create an UnknownValueOp for the given property type.
The target of an inner symbol, the entity the symbol is a handle for.
auto getField() const
Return the target's fieldID.
auto getPort() const
Return the target's port, if valid. Check "isPort()".
bool isPort() const
Return if this targets a port.
Operation * getOp() const
Return the target's base operation. For ports, this is the module.
llvm::function_ref< hw::InnerSymbolNamespace &(FModuleLike mod)> GetNamespaceCallback
FieldRef getFieldRefForTarget(const hw::InnerSymTarget &ist)
Get FieldRef pointing to the specified inner symbol target, which must be valid.
FieldRef getDeltaRef(Value value, bool lookThroughCasts=false)
Get the delta indexing from a value, as a FieldRef.
FIRRTLBaseType getBaseType(Type type)
If it is a base type, return it as is.
FieldRef getFieldRefFromValue(Value value, bool lookThroughCasts=false)
Get the FieldRef from a value.
mlir::TypedValue< FIRRTLBaseType > FIRRTLBaseValue
void walkGroundTypes(FIRRTLType firrtlType, llvm::function_ref< void(uint64_t, FIRRTLBaseType, bool)> fn)
Walk leaf ground types in the firrtlType and apply the function fn.
PathOp createPathRef(Operation *op, hw::HierPathOp nla, mlir::ImplicitLocOpBuilder &builderOM)
Add the tracker annotation to the op and get a PathOp to the op.
IntegerAttr getIntAttr(Type type, const APInt &value)
Utiility for generating a constant attribute.
std::pair< bool, std::optional< mlir::LocationAttr > > maybeStringToLocation(llvm::StringRef spelling, bool skipParsing, mlir::StringAttr &locatorFilenameCache, FileLineColLoc &fileLineColLocCache, MLIRContext *context)
std::pair< hw::InnerSymAttr, StringAttr > getOrAddInnerSym(MLIRContext *context, hw::InnerSymAttr attr, uint64_t fieldID, llvm::function_ref< hw::InnerSymbolNamespace &()> getNamespace)
Ensure that the the InnerSymAttr has a symbol on the field specified.
hw::InnerRefAttr getInnerRefTo(const hw::InnerSymTarget &target, GetNamespaceCallback getNamespace)
Obtain an inner reference to the target (operation or port), adding an inner symbol as necessary.
PropAssignOp getPropertyAssignment(FIRRTLPropertyValue value)
Return the single assignment to a Property value.
mlir::ParseResult parseFormatString(mlir::OpBuilder &builder, mlir::Location loc, llvm::StringRef formatString, llvm::ArrayRef< mlir::Value > specOperands, mlir::StringAttr &formatStringResult, llvm::SmallVectorImpl< mlir::Value > &operands)
Value getModuleScopedDriver(Value val, bool lookThroughWires, bool lookThroughNodes, bool lookThroughCasts)
Return the value that drives another FIRRTL value within module scope.
Value getDriverFromConnect(Value val)
Return the module-scoped driver of a value only looking through one connect.
Value getValueByFieldID(ImplicitLocOpBuilder builder, Value value, unsigned fieldID)
This gets the value targeted by a field id.
std::pair< std::string, bool > getFieldName(const FieldRef &fieldRef, bool nameSafe=false)
Get a string identifier representing the FieldRef.
llvm::function_ref< bool(const FieldRef &dst, const FieldRef &src)> WalkDriverCallback
Walk all the drivers of a value, passing in the connect operations drive the value.
mlir::TypedValue< PropertyType > FIRRTLPropertyValue
Type lowerType(Type type, std::optional< Location > loc={}, llvm::function_ref< hw::TypeAliasType(Type, BaseTypeAliasType, Location)> getTypeDeclFn={})
Given a type, return the corresponding lowered type for the HW dialect.
hw::InnerSymTarget getTargetFor(FieldRef ref)
Return the inner sym target for the specified value and fieldID.
bool areTypesConstCastable(FIRRTLType destType, FIRRTLType srcType, bool srcOuterTypeIsConst=false)
Returns whether the srcType can be const-casted to the destType.
bool walkDrivers(FIRRTLBaseValue value, bool lookThroughWires, bool lookThroughNodes, bool lookThroughCasts, WalkDriverCallback callback)
IntegerAttr getIntOnesAttr(Type type)
Utility for generating a constant all ones attribute.
void emitConnect(OpBuilder &builder, Location loc, Value lhs, Value rhs)
Emit a connect between two values.
void makeCommonPrefix(SmallString< 64 > &a, StringRef b)
Truncate a to the common prefix of a and b.
IntegerAttr getIntZerosAttr(Type type)
Utility for generating a constant zero attribute.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.