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