CIRCT  19.0.0git
OMAttributes.cpp
Go to the documentation of this file.
1 //===- OMAttributes.cpp - Object Model attribute definitions --------------===//
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 // This file contains the Object Model attribute definitions.
10 //
11 //===----------------------------------------------------------------------===//
12 
16 #include "mlir/IR/Builders.h"
17 #include "mlir/IR/BuiltinAttributes.h"
18 #include "mlir/IR/DialectImplementation.h"
19 #include "llvm/ADT/TypeSwitch.h"
20 
21 using namespace mlir;
22 using namespace circt::om;
23 
24 #define GET_ATTRDEF_CLASSES
25 #include "circt/Dialect/OM/OMAttributes.cpp.inc"
26 
27 Type circt::om::ReferenceAttr::getType() {
28  return ReferenceType::get(getContext());
29 }
30 
31 Type circt::om::SymbolRefAttr::getType() {
32  return SymbolRefType::get(getContext());
33 }
34 
35 Type circt::om::ListAttr::getType() {
36  return ListType::get(getContext(), getElementType());
37 }
38 
39 Type circt::om::MapAttr::getType() {
40  return MapType::get(getContext(), StringType::get(getContext()),
41  getValueType());
42 }
43 
44 circt::om::SymbolRefAttr circt::om::SymbolRefAttr::get(mlir::Operation *op) {
45  return om::SymbolRefAttr::get(op->getContext(),
47 }
48 
49 circt::om::SymbolRefAttr
50 circt::om::SymbolRefAttr::get(mlir::StringAttr symName) {
51  return om::SymbolRefAttr::get(symName.getContext(),
53 }
54 
55 LogicalResult
56 circt::om::ListAttr::verify(function_ref<InFlightDiagnostic()> emitError,
57  mlir::Type elementType, mlir::ArrayAttr elements) {
58  return success(llvm::all_of(elements, [&](mlir::Attribute attr) {
59  auto typedAttr = llvm::dyn_cast<mlir::TypedAttr>(attr);
60  if (!typedAttr) {
61  emitError()
62  << "an element of a list attribute must be a typed attr but got "
63  << attr;
64  return false;
65  }
66  if (typedAttr.getType() != elementType) {
67  emitError() << "an element of a list attribute must have a type "
68  << elementType << " but got " << typedAttr.getType();
69  return false;
70  }
71 
72  return true;
73  }));
74 }
75 
76 LogicalResult
77 circt::om::MapAttr::verify(function_ref<InFlightDiagnostic()> emitError,
78  mlir::Type valueType,
79  mlir::DictionaryAttr elements) {
80  for (auto attr : elements) {
81  auto typedAttr = llvm::dyn_cast<mlir::TypedAttr>(attr.getValue());
82  if (!typedAttr)
83  return emitError()
84  << "a value of a map attribute must be a typed attr but got "
85  << attr.getValue();
86  if (typedAttr.getType() != valueType)
87  return emitError() << "a value of a map attribute must have a type "
88  << valueType << " but field " << attr.getName()
89  << " has " << typedAttr.getType();
90  }
91  return success();
92 }
93 
94 void PathAttr::print(AsmPrinter &odsPrinter) const {
95  odsPrinter << '[';
96  llvm::interleaveComma(getPath(), odsPrinter, [&](PathElement element) {
97  odsPrinter.printKeywordOrString(element.module);
98  odsPrinter << ':';
99  odsPrinter.printKeywordOrString(element.instance);
100  });
101  odsPrinter << ']';
102 }
103 
104 Attribute PathAttr::parse(AsmParser &odsParser, Type odsType) {
105  auto *context = odsParser.getContext();
106  SmallVector<PathElement> path;
107  if (odsParser.parseCommaSeparatedList(
108  OpAsmParser::Delimiter::Square, [&]() -> ParseResult {
109  std::string module;
110  std::string instance;
111  if (odsParser.parseKeywordOrString(&module) ||
112  odsParser.parseColon() ||
113  odsParser.parseKeywordOrString(&instance))
114  return failure();
115  path.emplace_back(StringAttr::get(context, module),
116  StringAttr::get(context, instance));
117  return success();
118  }))
119  return nullptr;
120  return PathAttr::get(context, path);
121 }
122 
123 LogicalResult PathAttr::verify(function_ref<mlir::InFlightDiagnostic()>,
124  ArrayRef<PathElement> path) {
125  return success();
126 }
127 
128 Type circt::om::IntegerAttr::getType() {
129  return OMIntegerType::get(getContext());
130 }
131 
132 void circt::om::OMDialect::registerAttributes() {
133  addAttributes<
134 #define GET_ATTRDEF_LIST
135 #include "circt/Dialect/OM/OMAttributes.cpp.inc"
136  >();
137 }
MlirType elementType
Definition: CHIRRTL.cpp:29
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:54
A module name, and the name of an instance inside that module.
Definition: OMAttributes.h:22
mlir::StringAttr module
Definition: OMAttributes.h:35
mlir::StringAttr instance
Definition: OMAttributes.h:36