CIRCT  18.0.0git
CalyxOps.h
Go to the documentation of this file.
1 //===- CalyxOps.h - Declare Calyx dialect operations ------------*- C++ -*-===//
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 declares the operation class for the Calyx IR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef CIRCT_DIALECT_CALYX_OPS_H
14 #define CIRCT_DIALECT_CALYX_OPS_H
15 
17 #include "circt/Dialect/HW/HWOps.h"
18 #include "mlir/IR/BuiltinOps.h"
19 #include "mlir/IR/OpImplementation.h"
20 #include "mlir/IR/RegionKindInterface.h"
21 #include "mlir/IR/SymbolTable.h"
22 #include "mlir/Interfaces/FunctionInterfaces.h"
23 #include "mlir/Interfaces/InferTypeOpInterface.h"
24 #include "mlir/Interfaces/SideEffectInterfaces.h"
25 
26 namespace circt {
27 namespace calyx {
28 
29 /// A helper function to verify each control-like operation
30 /// has a valid parent and, if applicable, body.
31 LogicalResult verifyControlLikeOp(Operation *op);
32 
33 /// Signals that the following operation is "control-like."
34 template <typename ConcreteType>
35 class ControlLike : public mlir::OpTrait::TraitBase<ConcreteType, ControlLike> {
36 public:
37  static LogicalResult verifyTrait(Operation *op) {
38  return verifyControlLikeOp(op);
39  }
40 };
41 
42 /// A helper function to verify a combinational operation.
43 LogicalResult verifyCombinationalOp(Operation *op);
44 
45 /// Signals that the following operation is combinational.
46 template <typename ConcreteType>
48  : public mlir::OpTrait::TraitBase<ConcreteType, Combinational> {
49 public:
50  static LogicalResult verifyTrait(Operation *op) {
51  Attribute staticAttribute = op->getAttr("static");
52  if (staticAttribute == nullptr)
53  return success();
54 
55  // If the operation has the static attribute, verify it is zero.
56  APInt staticValue = staticAttribute.cast<IntegerAttr>().getValue();
57  assert(staticValue == 0 && "If combinational, it should take 0 cycles.");
58 
59  return success();
60  }
61 };
62 
63 /// The direction of a Component or Cell port. this is similar to the
64 /// implementation found in the FIRRTL dialect.
65 enum Direction { Input = 0, Output = 1 };
66 namespace direction {
67 /// Returns an output direction if `isOutput` is true, otherwise returns an
68 /// input direction.
69 Direction get(bool isOutput);
70 
71 /// Returns an IntegerAttr containing the packed representation of the
72 /// direction counts. Direction::Input is zero, and Direction::Output is one.
73 IntegerAttr packAttribute(MLIRContext *context, size_t nIns, size_t nOuts);
74 
75 } // namespace direction
76 
77 /// This holds information about the port for either a Component or Cell.
78 struct PortInfo {
79  StringAttr name;
80  Type type;
82  DictionaryAttr attributes;
83 
84  /// Returns whether the given port has attribute with Identifier `name`.
85  bool hasAttribute(StringRef identifier) const {
86  assert(attributes && "PortInfo::attributes should be instantiated.");
87  return llvm::any_of(attributes, [&](auto idToAttribute) {
88  return identifier == idToAttribute.getName();
89  });
90  }
91 
92  /// Returns the attribute associated with the given name if it exists,
93  /// otherwise std::nullopt.
94  std::optional<Attribute> getAttribute(StringRef identifier) const {
95  assert(attributes && "PortInfo::attributes should be instantiated.");
96  auto it = llvm::find_if(attributes, [&](auto idToAttribute) {
97  return identifier == idToAttribute.getName();
98  });
99  if (it == attributes.end())
100  return std::nullopt;
101  return it->getValue();
102  }
103 
104  /// Returns all identifiers for this dictionary attribute.
105  SmallVector<StringRef> getAllIdentifiers() const {
106  assert(attributes && "PortInfo::attributes should be instantiated.");
107  SmallVector<StringRef> identifiers;
108  llvm::transform(attributes, std::back_inserter(identifiers),
109  [](auto idToAttribute) { return idToAttribute.getName(); });
110  return identifiers;
111  }
112 };
113 
114 /// A helper function to verify each operation with the Ccomponent trait.
115 LogicalResult verifyComponent(Operation *op);
116 
117 /// A helper function to verify each operation with the Cell trait.
118 LogicalResult verifyCell(Operation *op);
119 
120 /// A helper function to verify each operation with the Group Interface trait.
121 LogicalResult verifyGroupInterface(Operation *op);
122 
123 /// A helper function to verify each operation with the If trait.
124 LogicalResult verifyIf(Operation *op);
125 
126 /// Returns port information for the block argument provided.
127 PortInfo getPortInfo(BlockArgument arg);
128 
129 } // namespace calyx
130 } // namespace circt
131 
132 #include "circt/Dialect/Calyx/CalyxInterfaces.h.inc"
133 
134 #define GET_OP_CLASSES
135 #include "circt/Dialect/Calyx/Calyx.h.inc"
136 
137 #endif // CIRCT_DIALECT_CALYX_OPS_H
assert(baseType &&"element must be base type")
Signals that the following operation is combinational.
Definition: CalyxOps.h:48
static LogicalResult verifyTrait(Operation *op)
Definition: CalyxOps.h:50
Signals that the following operation is "control-like.".
Definition: CalyxOps.h:35
static LogicalResult verifyTrait(Operation *op)
Definition: CalyxOps.h:37
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, size_t nIns, size_t nOuts)
Returns an IntegerAttr containing the packed representation of the direction counts.
Definition: CalyxOps.cpp:57
LogicalResult verifyComponent(Operation *op)
A helper function to verify each operation with the Ccomponent trait.
Definition: CalyxOps.cpp:201
LogicalResult verifyControlLikeOp(Operation *op)
A helper function to verify each control-like operation has a valid parent and, if applicable,...
Definition: CalyxOps.cpp:217
LogicalResult verifyGroupInterface(Operation *op)
A helper function to verify each operation with the Group Interface trait.
Definition: CalyxOps.cpp:1390
LogicalResult verifyCell(Operation *op)
A helper function to verify each operation with the Cell trait.
Definition: CalyxOps.cpp:209
Direction
The direction of a Component or Cell port.
Definition: CalyxOps.h:65
LogicalResult verifyIf(Operation *op)
A helper function to verify each operation with the If trait.
Definition: CalyxOps.cpp:254
LogicalResult verifyCombinationalOp(Operation *op)
A helper function to verify a combinational operation.
PortInfo getPortInfo(BlockArgument arg)
Returns port information for the block argument provided.
Definition: CalyxOps.cpp:140
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
This holds information about the port for either a Component or Cell.
Definition: CalyxOps.h:78
SmallVector< StringRef > getAllIdentifiers() const
Returns all identifiers for this dictionary attribute.
Definition: CalyxOps.h:105
Direction direction
Definition: CalyxOps.h:81
bool hasAttribute(StringRef identifier) const
Returns whether the given port has attribute with Identifier name.
Definition: CalyxOps.h:85
std::optional< Attribute > getAttribute(StringRef identifier) const
Returns the attribute associated with the given name if it exists, otherwise std::nullopt.
Definition: CalyxOps.h:94
DictionaryAttr attributes
Definition: CalyxOps.h:82