6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
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"
14
9
#include " circt/Dialect/FIRRTL/FIRRTLIntrinsics.h"
15
10
#include " circt/Dialect/FIRRTL/FIRRTLTypes.h"
16
11
#include " circt/Dialect/FIRRTL/FIRRTLUtils.h"
@@ -715,19 +710,58 @@ class CirctDPICallConverter : public IntrinsicConverter {
715
710
}
716
711
};
717
712
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
+ }
719
756
720
757
// / 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 (
726
760
MLIRContext *context, Location loc, DictionaryAttr augmentedType,
727
761
DictionaryAttr root, StringAttr name, StringAttr defName,
728
- std::optional<StringAttr> description, Twine clazz, Twine path = {}) {
762
+ std::optional<StringAttr> description, Twine path = {}) {
729
763
auto classAttr =
730
- tryGetAs<StringAttr>(augmentedType, root, " class" , loc, clazz, path);
764
+ tryGetAs<StringAttr>(augmentedType, root, " class" , loc, path);
731
765
if (!classAttr)
732
766
return std::nullopt;
733
767
StringRef classBase = classAttr.getValue ();
@@ -737,7 +771,7 @@ static std::optional<DictionaryAttr> parseAugmentedType(
737
771
" 'sifive.enterprise.grandCentral.Augmented*', but was '" +
738
772
classAttr.getValue () + " ' (Did you misspell it?)" )
739
773
.attachNote ()
740
- << " see annotation : " << augmentedType;
774
+ << " see attribute : " << augmentedType;
741
775
return std::nullopt;
742
776
}
743
777
@@ -746,7 +780,7 @@ static std::optional<DictionaryAttr> parseAugmentedType(
746
780
// "elements": Seq[AugmentedField]
747
781
if (classBase == " BundleType" ) {
748
782
defName =
749
- tryGetAs<StringAttr>(augmentedType, root, " defName" , loc, clazz, path);
783
+ tryGetAs<StringAttr>(augmentedType, root, " defName" , loc, path);
750
784
if (!defName)
751
785
return std::nullopt;
752
786
@@ -756,34 +790,33 @@ static std::optional<DictionaryAttr> parseAugmentedType(
756
790
// "tpe": AugmentedType
757
791
SmallVector<Attribute> elements;
758
792
auto elementsAttr =
759
- tryGetAs<ArrayAttr>(augmentedType, root, " elements" , loc, clazz, path);
793
+ tryGetAs<ArrayAttr>(augmentedType, root, " elements" , loc, path);
760
794
if (!elementsAttr)
761
795
return std::nullopt;
762
796
for (size_t i = 0 , e = elementsAttr.size (); i != e; ++i) {
763
797
auto field = dyn_cast_or_null<DictionaryAttr>(elementsAttr[i]);
764
798
if (!field) {
765
799
mlir::emitError (
766
800
loc,
767
- // TODO: Not clazz, not annotation
768
- " Annotation '" + Twine (clazz) + " ' with path '.elements[" +
801
+ " View info attribute with path '.elements[" +
769
802
Twine (i) +
770
803
" ]' contained an unexpected type (expected a DictionaryAttr)." )
771
804
.attachNote ()
772
- << " The received element was: " << elementsAttr[i] << " \n " ;
805
+ << " The received element was: " << elementsAttr[i];
773
806
return std::nullopt;
774
807
}
775
808
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);
777
810
auto tpe =
778
- tryGetAs<DictionaryAttr>(field, root, " tpe" , loc, clazz, ePath);
811
+ tryGetAs<DictionaryAttr>(field, root, " tpe" , loc, ePath);
779
812
if (!name || !tpe)
780
813
return std::nullopt;
781
814
std::optional<StringAttr> description;
782
815
if (auto maybeDescription = field.get (" description" ))
783
816
description = cast<StringAttr>(maybeDescription);
784
817
auto eltAttr =
785
818
parseAugmentedType (context, loc, tpe, root, name, defName,
786
- description, clazz, path + " _" + name.getValue ());
819
+ description, path + " _" + name.getValue ());
787
820
if (!eltAttr)
788
821
return std::nullopt;
789
822
@@ -801,9 +834,9 @@ static std::optional<DictionaryAttr> parseAugmentedType(
801
834
attrs.append (" tpe" , tpeClass);
802
835
elements.push_back (*eltAttr);
803
836
}
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.
807
840
NamedAttrList attrs;
808
841
attrs.append (" class" , classAttr);
809
842
attrs.append (" defName" , defName);
@@ -818,7 +851,7 @@ static std::optional<DictionaryAttr> parseAugmentedType(
818
851
if (classBase == " GroundType" ) {
819
852
NamedAttrList elementIface;
820
853
821
- // Populate the annotation for the interface element.
854
+ // Populate the attribute for the interface element.
822
855
elementIface.append (" class" , classAttr);
823
856
if (description)
824
857
elementIface.append (" description" , *description);
@@ -831,15 +864,15 @@ static std::optional<DictionaryAttr> parseAugmentedType(
831
864
// "elements": Seq[AugmentedType]
832
865
if (classBase == " VectorType" ) {
833
866
auto elementsAttr =
834
- tryGetAs<ArrayAttr>(augmentedType, root, " elements" , loc, clazz, path);
867
+ tryGetAs<ArrayAttr>(augmentedType, root, " elements" , loc, path);
835
868
if (!elementsAttr)
836
869
return std::nullopt;
837
870
SmallVector<Attribute> elements;
838
871
for (auto [i, elt] : llvm::enumerate (elementsAttr)) {
839
872
auto eltAttr =
840
873
parseAugmentedType (context, loc, cast<DictionaryAttr>(elt), root,
841
874
name, StringAttr::get (context, " " ), std::nullopt,
842
- clazz, path + " _" + Twine (i));
875
+ path + " _" + Twine (i));
843
876
if (!eltAttr)
844
877
return std::nullopt;
845
878
elements.push_back (*eltAttr);
@@ -854,11 +887,11 @@ static std::optional<DictionaryAttr> parseAugmentedType(
854
887
}
855
888
856
889
// 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.
858
891
mlir::emitError (loc, " found unknown AugmentedType '" + classAttr.getValue () +
859
892
" ' (Did you misspell it?)" )
860
893
.attachNote ()
861
- << " see annotation : " << augmentedType;
894
+ << " see attribute : " << augmentedType;
862
895
return std::nullopt;
863
896
}
864
897
@@ -895,7 +928,7 @@ class ViewConverter : public IntrinsicConverter {
895
928
auto nameAttr = gi.getParamValue <StringAttr>(" name" );
896
929
auto result = parseAugmentedType (
897
930
gi.op .getContext (), gi.op .getLoc (), dict, dict, nameAttr,
898
- /* defName= */ {}, /* description= */ std::nullopt, viewAnnoClass );
931
+ /* defName= */ {}, /* description= */ std::nullopt);
899
932
900
933
if (!result)
901
934
return gi.emitError ()
0 commit comments