CIRCT  20.0.0git
CalyxHelpers.h
Go to the documentation of this file.
1 //===- CalyxHelpers.h - Calyx helper methods --------------------*- 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 header file defines various helper methods for building Calyx programs.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef CIRCT_DIALECT_CALYX_CALYXHELPERS_H
14 #define CIRCT_DIALECT_CALYX_CALYXHELPERS_H
15 
18 #include "circt/Dialect/HW/HWOps.h"
19 #include "circt/Support/LLVM.h"
20 #include "mlir/Dialect/Func/IR/FuncOps.h"
21 
22 #include <memory>
23 
24 namespace circt {
25 namespace calyx {
26 
27 /// Creates a RegisterOp, with input and output port bit widths defined by
28 /// `width`.
29 calyx::RegisterOp createRegister(Location loc, OpBuilder &builder,
30  ComponentOp component, size_t width,
31  Twine prefix);
32 
33 calyx::RegisterOp createRegister(Location loc, OpBuilder &builder,
34  ComponentOp component, Type type,
35  Twine prefix);
36 
37 /// A helper function to create constants in the HW dialect.
38 hw::ConstantOp createConstant(Location loc, OpBuilder &builder,
39  ComponentOp component, size_t width,
40  size_t value);
41 
42 /// A helper function to create calyx.instance operation.
43 calyx::InstanceOp createInstance(Location loc, OpBuilder &builder,
44  ComponentOp component,
45  SmallVectorImpl<Type> &resultTypes,
46  StringRef instanceName,
47  StringRef componentName);
48 
49 /// A helper function to get the instance name.
50 std::string getInstanceName(mlir::func::CallOp callOp);
51 
52 // Returns whether this operation is a leaf node in the Calyx control.
53 // TODO(github.com/llvm/circt/issues/1679): Add Invoke.
54 bool isControlLeafNode(Operation *op);
55 
56 // Creates a DictionaryAttr containing a unit attribute 'name'. Used for
57 // defining mandatory port attributes for calyx::ComponentOp's.
58 DictionaryAttr getMandatoryPortAttr(MLIRContext *ctx, StringRef name);
59 
60 // Adds the mandatory Calyx component I/O ports (->[clk, reset, go], [done]->)
61 // to ports.
62 void addMandatoryComponentPorts(PatternRewriter &rewriter,
63  SmallVectorImpl<calyx::PortInfo> &ports);
64 
65 // Returns the bit width for the given dimension. This will always be greater
66 // than zero. See: https://github.com/llvm/circt/issues/2660
67 unsigned handleZeroWidth(int64_t dim);
68 
69 /// Updates the guard of each assignment within a group with `op`.
70 template <typename Op>
71 static void updateGroupAssignmentGuards(OpBuilder &builder, GroupOp &group,
72  Op &op) {
73  group.walk([&](AssignOp assign) {
74  if (assign.getGuard())
75  // If the assignment is guarded already, take the bitwise & of the current
76  // guard and the group's go signal.
77  assign->setOperand(2, builder.create<comb::AndOp>(
78  group.getLoc(), assign.getGuard(), op, false));
79  else
80  // Otherwise, just insert it as the guard.
81  assign->insertOperands(2, {op});
82  });
83 }
84 
85 } // namespace calyx
86 } // namespace circt
87 
88 #endif // CIRCT_DIALECT_CALYX_CALYXHELPERS_H
int32_t width
Definition: FIRRTL.cpp:36
static void updateGroupAssignmentGuards(OpBuilder &builder, GroupOp &group, Op &op)
Updates the guard of each assignment within a group with op.
Definition: CalyxHelpers.h:71
void addMandatoryComponentPorts(PatternRewriter &rewriter, SmallVectorImpl< calyx::PortInfo > &ports)
bool isControlLeafNode(Operation *op)
unsigned handleZeroWidth(int64_t dim)
hw::ConstantOp createConstant(Location loc, OpBuilder &builder, ComponentOp component, size_t width, size_t value)
A helper function to create constants in the HW dialect.
DictionaryAttr getMandatoryPortAttr(MLIRContext *ctx, StringRef name)
calyx::RegisterOp createRegister(Location loc, OpBuilder &builder, ComponentOp component, size_t width, Twine prefix)
Creates a RegisterOp, with input and output port bit widths defined by width.
calyx::InstanceOp createInstance(Location loc, OpBuilder &builder, ComponentOp component, SmallVectorImpl< Type > &resultTypes, StringRef instanceName, StringRef componentName)
A helper function to create calyx.instance operation.
std::string getInstanceName(mlir::func::CallOp callOp)
A helper function to get the instance name.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21