CIRCT  19.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 /// A helper function to create constants in the HW dialect.
34 hw::ConstantOp createConstant(Location loc, OpBuilder &builder,
35  ComponentOp component, size_t width,
36  size_t value);
37 
38 /// A helper function to create calyx.instance operation.
39 calyx::InstanceOp createInstance(Location loc, OpBuilder &builder,
40  ComponentOp component,
41  SmallVectorImpl<Type> &resultTypes,
42  StringRef instanceName,
43  StringRef componentName);
44 
45 /// A helper function to get the instance name.
46 std::string getInstanceName(mlir::func::CallOp callOp);
47 
48 // Returns whether this operation is a leaf node in the Calyx control.
49 // TODO(github.com/llvm/circt/issues/1679): Add Invoke.
50 bool isControlLeafNode(Operation *op);
51 
52 // Creates a DictionaryAttr containing a unit attribute 'name'. Used for
53 // defining mandatory port attributes for calyx::ComponentOp's.
54 DictionaryAttr getMandatoryPortAttr(MLIRContext *ctx, StringRef name);
55 
56 // Adds the mandatory Calyx component I/O ports (->[clk, reset, go], [done]->)
57 // to ports.
58 void addMandatoryComponentPorts(PatternRewriter &rewriter,
59  SmallVectorImpl<calyx::PortInfo> &ports);
60 
61 // Returns the bit width for the given dimension. This will always be greater
62 // than zero. See: https://github.com/llvm/circt/issues/2660
63 unsigned handleZeroWidth(int64_t dim);
64 
65 /// Updates the guard of each assignment within a group with `op`.
66 template <typename Op>
67 static void updateGroupAssignmentGuards(OpBuilder &builder, GroupOp &group,
68  Op &op) {
69  group.walk([&](AssignOp assign) {
70  if (assign.getGuard())
71  // If the assignment is guarded already, take the bitwise & of the current
72  // guard and the group's go signal.
73  assign->setOperand(2, builder.create<comb::AndOp>(
74  group.getLoc(), assign.getGuard(), op, false));
75  else
76  // Otherwise, just insert it as the guard.
77  assign->insertOperands(2, {op});
78  });
79 }
80 
81 } // namespace calyx
82 } // namespace circt
83 
84 #endif // CIRCT_DIALECT_CALYX_CALYXHELPERS_H
int32_t width
Definition: FIRRTL.cpp:36
Builder builder
static void updateGroupAssignmentGuards(OpBuilder &builder, GroupOp &group, Op &op)
Updates the guard of each assignment within a group with op.
Definition: CalyxHelpers.h:67
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