Skip to content

Commit e9b88fc

Browse files
committed
touchups annotation -> attribute.
1 parent f036623 commit e9b88fc

File tree

1 file changed

+64
-31
lines changed

1 file changed

+64
-31
lines changed

lib/Dialect/FIRRTL/FIRRTLIntrinsics.cpp

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// TODO: Local tryGetAs that produces error for view not "Annotation" ?
10-
11-
#include "circt/Dialect/FIRRTL/FIRRTLAnnotationHelper.h"
12-
// remove me
13-
#include "circt/Dialect/FIRRTL/AnnotationDetails.h"
149
#include "circt/Dialect/FIRRTL/FIRRTLIntrinsics.h"
1510
#include "circt/Dialect/FIRRTL/FIRRTLTypes.h"
1611
#include "circt/Dialect/FIRRTL/FIRRTLUtils.h"
@@ -715,19 +710,58 @@ class CirctDPICallConverter : public IntrinsicConverter {
715710
}
716711
};
717712

718-
// TODO: THIS SHOULD GO ELSEWHERE!!
713+
//===----------------------------------------------------------------------===//
714+
// View intrinsic converter and helpers
715+
//===----------------------------------------------------------------------===//
716+
717+
/// Implements the same behavior as DictionaryAttr::getAs<A> to return the
718+
/// value of a specific type associated with a key in a dictionary. However,
719+
/// this is specialized to print a useful error message, specific to custom
720+
/// attribute process, on failure.
721+
/// These attributes are created from input JSON.
722+
template <typename A>
723+
A tryGetAs(DictionaryAttr &dict, const Attribute &root, StringRef key,
724+
Location loc, Twine path = Twine()) {
725+
SmallString<128> msg;
726+
// Check that the key exists.
727+
auto value = dict.get(key);
728+
if (!value) {
729+
if (path.isTriviallyEmpty())
730+
("View info did not contain required key '" + key + "'.").toVector(msg);
731+
else
732+
("View info attribute with path '" + path +
733+
"' did not contain required key '" + key + "'.")
734+
.toVector(msg);
735+
mlir::emitError(loc, msg).attachNote()
736+
<< "The full info attribute is reproduced here: " << root;
737+
return nullptr;
738+
}
739+
740+
// Check that the value has the correct type.
741+
auto valueA = dyn_cast<A>(value);
742+
if (!valueA) {
743+
if (path.isTriviallyEmpty())
744+
("View info did not contain the correct type for key '" + key + "'.")
745+
.toVector(msg);
746+
else
747+
("View info attribute with path '" + path +
748+
"' did not contain the correct type for key '" + key + "'.")
749+
.toVector(msg);
750+
mlir::emitError(loc, msg).attachNote()
751+
<< "The full info attribute is reproduced here: " << root;
752+
return nullptr;
753+
}
754+
return valueA;
755+
}
719756

720757
/// Recursively walk a sifive.enterprise.grandcentral.AugmentedType to extract
721-
/// any annotations it may contain. This is going to generate two types of
722-
/// annotations:
723-
/// 1) Annotations necessary to build interfaces and store them at "~"
724-
/// 2) Scattered annotations for how components bind to interfaces
725-
static std::optional<DictionaryAttr> parseAugmentedType(
758+
/// and slightly restructure information needed for a view.
759+
std::optional<DictionaryAttr> parseAugmentedType(
726760
MLIRContext *context, Location loc, DictionaryAttr augmentedType,
727761
DictionaryAttr root, StringAttr name, StringAttr defName,
728-
std::optional<StringAttr> description, Twine clazz, Twine path = {}) {
762+
std::optional<StringAttr> description, Twine path = {}) {
729763
auto classAttr =
730-
tryGetAs<StringAttr>(augmentedType, root, "class", loc, clazz, path);
764+
tryGetAs<StringAttr>(augmentedType, root, "class", loc, path);
731765
if (!classAttr)
732766
return std::nullopt;
733767
StringRef classBase = classAttr.getValue();
@@ -737,7 +771,7 @@ static std::optional<DictionaryAttr> parseAugmentedType(
737771
"'sifive.enterprise.grandCentral.Augmented*', but was '" +
738772
classAttr.getValue() + "' (Did you misspell it?)")
739773
.attachNote()
740-
<< "see annotation: " << augmentedType;
774+
<< "see attribute: " << augmentedType;
741775
return std::nullopt;
742776
}
743777

@@ -746,7 +780,7 @@ static std::optional<DictionaryAttr> parseAugmentedType(
746780
// "elements": Seq[AugmentedField]
747781
if (classBase == "BundleType") {
748782
defName =
749-
tryGetAs<StringAttr>(augmentedType, root, "defName", loc, clazz, path);
783+
tryGetAs<StringAttr>(augmentedType, root, "defName", loc, path);
750784
if (!defName)
751785
return std::nullopt;
752786

@@ -756,34 +790,33 @@ static std::optional<DictionaryAttr> parseAugmentedType(
756790
// "tpe": AugmentedType
757791
SmallVector<Attribute> elements;
758792
auto elementsAttr =
759-
tryGetAs<ArrayAttr>(augmentedType, root, "elements", loc, clazz, path);
793+
tryGetAs<ArrayAttr>(augmentedType, root, "elements", loc, path);
760794
if (!elementsAttr)
761795
return std::nullopt;
762796
for (size_t i = 0, e = elementsAttr.size(); i != e; ++i) {
763797
auto field = dyn_cast_or_null<DictionaryAttr>(elementsAttr[i]);
764798
if (!field) {
765799
mlir::emitError(
766800
loc,
767-
// TODO: Not clazz, not annotation
768-
"Annotation '" + Twine(clazz) + "' with path '.elements[" +
801+
"View info attribute with path '.elements[" +
769802
Twine(i) +
770803
"]' contained an unexpected type (expected a DictionaryAttr).")
771804
.attachNote()
772-
<< "The received element was: " << elementsAttr[i] << "\n";
805+
<< "The received element was: " << elementsAttr[i];
773806
return std::nullopt;
774807
}
775808
auto ePath = (path + ".elements[" + Twine(i) + "]").str();
776-
auto name = tryGetAs<StringAttr>(field, root, "name", loc, clazz, ePath);
809+
auto name = tryGetAs<StringAttr>(field, root, "name", loc, ePath);
777810
auto tpe =
778-
tryGetAs<DictionaryAttr>(field, root, "tpe", loc, clazz, ePath);
811+
tryGetAs<DictionaryAttr>(field, root, "tpe", loc, ePath);
779812
if (!name || !tpe)
780813
return std::nullopt;
781814
std::optional<StringAttr> description;
782815
if (auto maybeDescription = field.get("description"))
783816
description = cast<StringAttr>(maybeDescription);
784817
auto eltAttr =
785818
parseAugmentedType(context, loc, tpe, root, name, defName,
786-
description, clazz, path + "_" + name.getValue());
819+
description, path + "_" + name.getValue());
787820
if (!eltAttr)
788821
return std::nullopt;
789822

@@ -801,9 +834,9 @@ static std::optional<DictionaryAttr> parseAugmentedType(
801834
attrs.append("tpe", tpeClass);
802835
elements.push_back(*eltAttr);
803836
}
804-
// Add an annotation that stores information necessary to construct the
805-
// module for the view. This needs the name of the module (defName) and the
806-
// names of the components inside it.
837+
// Add an attribute that stores information necessary to construct the
838+
// interface for the view. This needs the name of the interface (defName)
839+
// and the names of the components inside it.
807840
NamedAttrList attrs;
808841
attrs.append("class", classAttr);
809842
attrs.append("defName", defName);
@@ -818,7 +851,7 @@ static std::optional<DictionaryAttr> parseAugmentedType(
818851
if (classBase == "GroundType") {
819852
NamedAttrList elementIface;
820853

821-
// Populate the annotation for the interface element.
854+
// Populate the attribute for the interface element.
822855
elementIface.append("class", classAttr);
823856
if (description)
824857
elementIface.append("description", *description);
@@ -831,15 +864,15 @@ static std::optional<DictionaryAttr> parseAugmentedType(
831864
// "elements": Seq[AugmentedType]
832865
if (classBase == "VectorType") {
833866
auto elementsAttr =
834-
tryGetAs<ArrayAttr>(augmentedType, root, "elements", loc, clazz, path);
867+
tryGetAs<ArrayAttr>(augmentedType, root, "elements", loc, path);
835868
if (!elementsAttr)
836869
return std::nullopt;
837870
SmallVector<Attribute> elements;
838871
for (auto [i, elt] : llvm::enumerate(elementsAttr)) {
839872
auto eltAttr =
840873
parseAugmentedType(context, loc, cast<DictionaryAttr>(elt), root,
841874
name, StringAttr::get(context, ""), std::nullopt,
842-
clazz, path + "_" + Twine(i));
875+
path + "_" + Twine(i));
843876
if (!eltAttr)
844877
return std::nullopt;
845878
elements.push_back(*eltAttr);
@@ -854,11 +887,11 @@ static std::optional<DictionaryAttr> parseAugmentedType(
854887
}
855888

856889
// Anything else is unexpected or a user error if they manually wrote
857-
// annotations. Print an error and error out.
890+
// the JSON/attribute. Print an error and error out.
858891
mlir::emitError(loc, "found unknown AugmentedType '" + classAttr.getValue() +
859892
"' (Did you misspell it?)")
860893
.attachNote()
861-
<< "see annotation: " << augmentedType;
894+
<< "see attribute: " << augmentedType;
862895
return std::nullopt;
863896
}
864897

@@ -895,7 +928,7 @@ class ViewConverter : public IntrinsicConverter {
895928
auto nameAttr = gi.getParamValue<StringAttr>("name");
896929
auto result = parseAugmentedType(
897930
gi.op.getContext(), gi.op.getLoc(), dict, dict, nameAttr,
898-
/* defName= */ {}, /* description= */ std::nullopt, viewAnnoClass);
931+
/* defName= */ {}, /* description= */ std::nullopt);
899932

900933
if (!result)
901934
return gi.emitError()

0 commit comments

Comments
 (0)