CIRCT  18.0.0git
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 
17 using namespace circt;
18 using 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 
37 IntegerAttr direction::packAttribute(MLIRContext *context,
38  ArrayRef<Direction> directions) {
39  // Pack the array of directions into an APInt. Input is zero, output is one.
40  auto size = directions.size();
41  APInt portDirections(size, 0);
42  for (size_t i = 0; i != size; ++i)
43  if (directions[i] == Direction::Out)
44  portDirections.setBit(i);
45  return IntegerAttr::get(IntegerType::get(context, size), portDirections);
46 }
47 
48 SmallVector<Direction> direction::unpackAttribute(IntegerAttr directions) {
49  assert(directions.getType().isSignlessInteger() &&
50  "Direction attributes must be signless integers");
51  auto value = directions.getValue();
52  auto size = value.getBitWidth();
53  SmallVector<Direction> result;
54  result.reserve(size);
55  for (size_t i = 0; i != size; ++i)
56  result.push_back(direction::get(value[i]));
57  return result;
58 }
59 
60 llvm::raw_ostream &circt::firrtl::operator<<(llvm::raw_ostream &os,
61  const Direction &dir) {
62  return os << direction::toString(dir);
63 }
64 
65 void FIRRTLDialect::registerAttributes() {
66  addAttributes<
67 #define GET_ATTRDEF_LIST
68 #include "circt/Dialect/FIRRTL/FIRRTLAttributes.cpp.inc"
69  >();
70 }
71 
72 //===----------------------------------------------------------------------===//
73 // ParamDeclAttr
74 //===----------------------------------------------------------------------===//
75 
76 Attribute ParamDeclAttr::parse(AsmParser &p, Type trailing) {
77  std::string name;
78  Type type;
79  TypedAttr value;
80  // < "FOO" : i32 > : i32
81  // < "FOO" : i32 = 0 > : i32
82  // < "FOO" : none >
83  if (p.parseLess() || p.parseString(&name) || p.parseColonType(type))
84  return Attribute();
85 
86  if (succeeded(p.parseOptionalEqual())) {
87  if (p.parseAttribute(value, type))
88  return Attribute();
89  }
90 
91  if (p.parseGreater())
92  return Attribute();
93 
94  if (value)
95  return ParamDeclAttr::get(name, value);
96  return ParamDeclAttr::get(name, type);
97 }
98 
99 void ParamDeclAttr::print(AsmPrinter &p) const {
100  p << "<" << getName() << ": " << getType();
101  if (getValue()) {
102  p << " = ";
103  p.printAttributeWithoutType(getValue());
104  }
105  p << ">";
106 }
lowerAnnotationsNoRefTypePorts FirtoolPreserveValuesMode value
Definition: Firtool.cpp:95
assert(baseType &&"element must be base type")
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:53
IntegerAttr packAttribute(MLIRContext *context, ArrayRef< Direction > directions)
Return a IntegerAttr containing the packed representation of an array of directions.
StringRef toString(Direction direction)
Direction flip(Direction direction)
Flip a port direction.
SmallVector< Direction > unpackAttribute(IntegerAttr directions)
Turn a packed representation of port attributes into a vector that can be worked with.
Direction get(bool isOutput)
Return an output direction if isOutput is true, otherwise return an input direction.
Direction
This represents the direction of a single port.
T & operator<<(T &os, FIRVersion version)
Definition: FIRParser.h:115
StringAttr getName(ArrayAttr names, size_t idx)
Return the name at the specified index of the ArrayAttr or null if it cannot be determined.
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21