CIRCT  20.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 = cast<IntegerAttr>(staticAttribute).getValue();
64  assert(staticValue == 0 && "If combinational, it should take 0 cycles.");
65 
66  return success();
67  }
68 };
69 
71  IEEE754,
72 };
73 
74 /// The direction of a Component or Cell port. this is similar to the
75 /// implementation found in the FIRRTL dialect.
76 enum Direction { Input = 0, Output = 1 };
77 namespace direction {
78 /// Returns an output direction if `isOutput` is true, otherwise returns an
79 /// input direction.
80 Direction get(bool isOutput);
81 
82 /// Returns an IntegerAttr containing the packed representation of the
83 /// direction counts. Direction::Input is zero, and Direction::Output is one.
84 IntegerAttr packAttribute(MLIRContext *context, size_t nIns, size_t nOuts);
85 
86 } // namespace direction
87 
88 /// This holds information about the port for either a Component or Cell.
89 struct PortInfo {
90  StringAttr name;
91  Type type;
93  DictionaryAttr attributes;
94 
95  /// Returns whether the given port has attribute with Identifier `name`.
96  bool hasAttribute(StringRef identifier) const {
97  assert(attributes && "PortInfo::attributes should be instantiated.");
98  return llvm::any_of(attributes, [&](auto idToAttribute) {
99  return identifier == idToAttribute.getName();
100  });
101  }
102 
103  /// Returns the attribute associated with the given name if it exists,
104  /// otherwise std::nullopt.
105  std::optional<Attribute> getAttribute(StringRef identifier) const {
106  assert(attributes && "PortInfo::attributes should be instantiated.");
107  auto it = llvm::find_if(attributes, [&](auto idToAttribute) {
108  return identifier == idToAttribute.getName();
109  });
110  if (it == attributes.end())
111  return std::nullopt;
112  return it->getValue();
113  }
114 
115  /// Returns all identifiers for this dictionary attribute.
116  SmallVector<StringRef> getAllIdentifiers() const {
117  assert(attributes && "PortInfo::attributes should be instantiated.");
118  SmallVector<StringRef> identifiers;
119  llvm::transform(attributes, std::back_inserter(identifiers),
120  [](auto idToAttribute) { return idToAttribute.getName(); });
121  return identifiers;
122  }
123 };
124 
125 /// A helper function to verify each operation with the Ccomponent trait.
126 LogicalResult verifyComponent(Operation *op);
127 
128 /// A helper function to verify each operation with the Cell trait.
129 LogicalResult verifyCell(Operation *op);
130 
131 /// A helper function to verify each operation with the Group Interface trait.
132 LogicalResult verifyGroupInterface(Operation *op);
133 
134 /// A helper function to verify each operation with the If trait.
135 LogicalResult verifyIf(Operation *op);
136 
137 /// Returns port information for the block argument provided.
138 PortInfo getPortInfo(BlockArgument arg);
139 
140 } // namespace calyx
141 } // namespace circt
142 
143 #include "circt/Dialect/Calyx/CalyxInterfaces.h.inc"
144 
145 #define GET_OP_CLASSES
146 #include "circt/Dialect/Calyx/Calyx.h.inc"
147 
148 #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:55
IntegerAttr packAttribute(MLIRContext *context, size_t nIns, size_t nOuts)
Returns an IntegerAttr containing the packed representation of the direction counts.
Definition: CalyxOps.cpp:59
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:203
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:219
FloatingPointStandard
Definition: CalyxOps.h:70
LogicalResult verifyGroupInterface(Operation *op)
A helper function to verify each operation with the Group Interface trait.
Definition: CalyxOps.cpp:1419
LogicalResult verifyCell(Operation *op)
A helper function to verify each operation with the Cell trait.
Definition: CalyxOps.cpp:211
static constexpr std::string_view resetPort
Definition: CalyxOps.h:33
Direction
The direction of a Component or Cell port.
Definition: CalyxOps.h:76
LogicalResult verifyIf(Operation *op)
A helper function to verify each operation with the If trait.
Definition: CalyxOps.cpp:256
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:142
static constexpr std::string_view goPort
Definition: CalyxOps.h:31
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
This holds information about the port for either a Component or Cell.
Definition: CalyxOps.h:89
SmallVector< StringRef > getAllIdentifiers() const
Returns all identifiers for this dictionary attribute.
Definition: CalyxOps.h:116
Direction direction
Definition: CalyxOps.h:92
bool hasAttribute(StringRef identifier) const
Returns whether the given port has attribute with Identifier name.
Definition: CalyxOps.h:96
std::optional< Attribute > getAttribute(StringRef identifier) const
Returns the attribute associated with the given name if it exists, otherwise std::nullopt.
Definition: CalyxOps.h:105
DictionaryAttr attributes
Definition: CalyxOps.h:93
This holds the name, type, direction of a module's ports.