CIRCT 20.0.0git
Loading...
Searching...
No Matches
FIRRTLAttributes.cpp
Go to the documentation of this file.
1//===- FIRRTLAttributes.cpp - Implement FIRRTL dialect attributes ---------===//
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
11
12#include "mlir/IR/Builders.h"
13#include "mlir/IR/DialectImplementation.h"
14#include "llvm/ADT/TypeSwitch.h"
15#include <iterator>
16
17using namespace circt;
18using namespace firrtl;
19
20#define GET_ATTRDEF_CLASSES
21#include "circt/Dialect/FIRRTL/FIRRTLAttributes.cpp.inc"
22
23//===----------------------------------------------------------------------===//
24// Utilities related to Direction
25//===----------------------------------------------------------------------===//
26
28 switch (direction) {
29 case Direction::In:
30 return Direction::Out;
31 case Direction::Out:
32 return Direction::In;
33 }
34 llvm_unreachable("unknown direction");
35}
36
37mlir::DenseBoolArrayAttr
38direction::packAttribute(MLIRContext *context, ArrayRef<Direction> directions) {
39 // Pack the array of directions into an APInt. Input is zero, output is one.
40 SmallVector<bool> dirs;
41 dirs.reserve(directions.size());
42 for (auto d : directions)
43 dirs.push_back(d == Direction::Out);
44 return mlir::DenseBoolArrayAttr::get(context, dirs);
45}
46
47mlir::DenseBoolArrayAttr direction::packAttribute(MLIRContext *context,
48 ArrayRef<bool> directions) {
49 // Pack the array of directions into an APInt. Input is zero, output is one.
50 return mlir::DenseBoolArrayAttr::get(context, directions);
51}
52
53SmallVector<Direction>
54direction::unpackAttribute(mlir::DenseBoolArrayAttr directions) {
55 SmallVector<Direction> result;
56 result.reserve(directions.size());
57 for (auto d : directions.asArrayRef())
58 result.push_back(direction::get(d));
59 return result;
60}
61
62llvm::raw_ostream &circt::firrtl::operator<<(llvm::raw_ostream &os,
63 const Direction &dir) {
64 return os << direction::toString(dir);
65}
66
67void FIRRTLDialect::registerAttributes() {
68 addAttributes<
69#define GET_ATTRDEF_LIST
70#include "circt/Dialect/FIRRTL/FIRRTLAttributes.cpp.inc"
71 >();
72}
73
74//===----------------------------------------------------------------------===//
75// ParamDeclAttr
76//===----------------------------------------------------------------------===//
77
78Attribute ParamDeclAttr::parse(AsmParser &p, Type trailing) {
79 std::string name;
80 Type type;
81 TypedAttr value;
82 // < "FOO" : i32 > : i32
83 // < "FOO" : i32 = 0 > : i32
84 // < "FOO" : none >
85 if (p.parseLess() || p.parseString(&name) || p.parseColonType(type))
86 return Attribute();
87
88 if (succeeded(p.parseOptionalEqual())) {
89 if (p.parseAttribute(value, type))
90 return Attribute();
91 }
92
93 if (p.parseGreater())
94 return Attribute();
95
96 if (value)
97 return ParamDeclAttr::get(name, value);
98 return ParamDeclAttr::get(name, type);
99}
100
101void ParamDeclAttr::print(AsmPrinter &p) const {
102 p << "<" << getName() << ": " << getType();
103 if (getValue()) {
104 p << " = ";
105 p.printAttributeWithoutType(getValue());
106 }
107 p << ">";
108}
mlir::DenseBoolArrayAttr packAttribute(MLIRContext *context, ArrayRef< Direction > directions)
Return a DenseBoolArrayAttr containing the packed representation of an array of directions.
Direction flip(Direction direction)
Flip a port direction.
SmallVector< Direction > unpackAttribute(mlir::DenseBoolArrayAttr directions)
Turn a packed representation of port attributes into a vector that can be worked with.
static Direction get(bool isOutput)
Return an output direction if isOutput is true, otherwise return an input direction.
static StringRef toString(Direction direction)
Direction
This represents the direction of a single port.
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const InstanceInfo::LatticeValue &value)
StringAttr getName(ArrayAttr names, size_t idx)
Return the name at the specified index of the ArrayAttr or null if it cannot be determined.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.