CIRCT  19.0.0git
FIRAnnotations.cpp
Go to the documentation of this file.
1 //===- FIRAnnotations.cpp - FIRRTL Annotation Utilities -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Provide utilities related to dealing with FIRRTL Annotations.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "FIRAnnotations.h"
14 
19 #include "circt/Support/JSON.h"
20 #include "mlir/IR/BuiltinAttributes.h"
21 #include "mlir/IR/BuiltinTypes.h"
22 #include "mlir/IR/Diagnostics.h"
23 
24 namespace json = llvm::json;
25 
26 using namespace circt;
27 using namespace firrtl;
28 
29 /// Convert a JSON value containing OMIR JSON (an array of OMNodes), convert
30 /// this to an OMIRAnnotation, and add it to a mutable `annotationMap` argument.
31 bool circt::firrtl::fromOMIRJSON(json::Value &value,
32  SmallVectorImpl<Attribute> &annotations,
33  json::Path path, MLIRContext *context) {
34  // The JSON value must be an array of objects. Anything else is reported as
35  // invalid.
36  auto *array = value.getAsArray();
37  if (!array) {
38  path.report(
39  "Expected OMIR to be an array of nodes, but found something else.");
40  return false;
41  }
42 
43  NamedAttrList omirAnnoFields;
44  omirAnnoFields.append("class", StringAttr::get(context, omirAnnoClass));
45  omirAnnoFields.append("nodes", convertJSONToAttribute(context, value, path));
46 
47  DictionaryAttr omirAnno = DictionaryAttr::get(context, omirAnnoFields);
48  annotations.push_back(omirAnno);
49 
50  return true;
51 }
52 
53 /// Deserialize a JSON value into FIRRTL Annotations. Annotations are
54 /// represented as a Target-keyed arrays of attributes. The input JSON value is
55 /// checked, at runtime, to be an array of objects. Returns true if successful,
56 /// false if unsuccessful.
58  json::Value &value, SmallVectorImpl<Attribute> &annotations,
59  json::Path path, MLIRContext *context) {
60 
61  // The JSON value must be an array of objects. Anything else is reported as
62  // invalid.
63  auto array = value.getAsArray();
64  if (!array) {
65  path.report(
66  "Expected annotations to be an array, but found something else.");
67  return false;
68  }
69 
70  // Build an array of annotations.
71  for (size_t i = 0, e = (*array).size(); i != e; ++i) {
72  auto object = (*array)[i].getAsObject();
73  auto p = path.index(i);
74  if (!object) {
75  p.report("Expected annotations to be an array of objects, but found an "
76  "array of something else.");
77  return false;
78  }
79 
80  // Build up the Attribute to represent the Annotation
81  NamedAttrList metadata;
82 
83  for (auto field : *object) {
84  if (auto value = convertJSONToAttribute(context, field.second, p)) {
85  metadata.append(field.first, value);
86  continue;
87  }
88  return false;
89  }
90 
91  annotations.push_back(DictionaryAttr::get(context, metadata));
92  }
93 
94  return true;
95 }
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:54
constexpr const char * omirAnnoClass
bool fromOMIRJSON(llvm::json::Value &value, SmallVectorImpl< Attribute > &annotations, llvm::json::Path path, MLIRContext *context)
Convert a JSON value containing OMIR JSON (an array of OMNodes), convert this to an OMIRAnnotation,...
bool importAnnotationsFromJSONRaw(llvm::json::Value &value, SmallVectorImpl< Attribute > &annotations, llvm::json::Path path, MLIRContext *context)
Deserialize a JSON value into FIRRTL Annotations.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Attribute convertJSONToAttribute(MLIRContext *context, llvm::json::Value &value, llvm::json::Path p)
Convert arbitrary JSON to an MLIR Attribute.