CIRCT  20.0.0git
CalyxHelpers.cpp
Go to the documentation of this file.
1 //===- CalyxHelpers.cpp - 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 // Various helper methods for building Calyx programs.
10 //
11 //===----------------------------------------------------------------------===//
12 
16 #include "circt/Support/LLVM.h"
17 #include "mlir/IR/PatternMatch.h"
18 
19 namespace circt {
20 namespace calyx {
21 
22 calyx::RegisterOp createRegister(Location loc, OpBuilder &builder,
23  ComponentOp component, size_t width,
24  Twine prefix) {
25  OpBuilder::InsertionGuard guard(builder);
26  builder.setInsertionPointToStart(component.getBodyBlock());
27  return builder.create<RegisterOp>(loc, (prefix + "_reg").str(), width);
28 }
29 
30 calyx::RegisterOp createRegister(Location loc, OpBuilder &builder,
31  ComponentOp component, Type type,
32  Twine prefix) {
33  OpBuilder::InsertionGuard guard(builder);
34  builder.setInsertionPointToStart(component.getBodyBlock());
35  return builder.create<RegisterOp>(loc, (prefix + "_reg").str(), type);
36 }
37 
38 hw::ConstantOp createConstant(Location loc, OpBuilder &builder,
39  ComponentOp component, size_t width,
40  size_t value) {
41  OpBuilder::InsertionGuard g(builder);
42  builder.setInsertionPointToStart(component.getBodyBlock());
43  return builder.create<hw::ConstantOp>(loc,
44  APInt(width, value, /*unsigned=*/true));
45 }
46 
47 calyx::InstanceOp createInstance(Location loc, OpBuilder &builder,
48  ComponentOp component,
49  SmallVectorImpl<Type> &resultTypes,
50  StringRef instanceName,
51  StringRef componentName) {
52  OpBuilder::InsertionGuard g(builder);
53  builder.setInsertionPointToStart(component.getBodyBlock());
54  return builder.create<InstanceOp>(loc, resultTypes, instanceName,
55  componentName);
56 }
57 
58 std::string getInstanceName(mlir::func::CallOp callOp) {
59  SmallVector<StringRef, 2> strVet = {callOp.getCallee(), "instance"};
60  return llvm::join(strVet, /*separator=*/"_");
61 }
62 
63 bool isControlLeafNode(Operation *op) { return isa<calyx::EnableOp>(op); }
64 
65 DictionaryAttr getMandatoryPortAttr(MLIRContext *ctx, StringRef name) {
66  return DictionaryAttr::get(
67  ctx, {NamedAttribute(StringAttr::get(ctx, name), UnitAttr::get(ctx))});
68 }
69 
70 void addMandatoryComponentPorts(PatternRewriter &rewriter,
71  SmallVectorImpl<calyx::PortInfo> &ports) {
72  MLIRContext *ctx = rewriter.getContext();
73  ports.push_back({
74  rewriter.getStringAttr(clkPort),
75  rewriter.getI1Type(),
78  });
79  ports.push_back({
80  rewriter.getStringAttr(resetPort),
81  rewriter.getI1Type(),
84  });
85  ports.push_back({
86  rewriter.getStringAttr(goPort),
87  rewriter.getI1Type(),
90  });
91  ports.push_back({
92  rewriter.getStringAttr(donePort),
93  rewriter.getI1Type(),
96  });
97 }
98 
99 unsigned handleZeroWidth(int64_t dim) {
100  return std::max(llvm::Log2_64_Ceil(dim), 1U);
101 }
102 
103 } // namespace calyx
104 } // namespace circt
int32_t width
Definition: FIRRTL.cpp:36
@ Input
Definition: HW.h:35
@ Output
Definition: HW.h:35
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:55
static constexpr std::string_view clkPort
Definition: CalyxOps.h:34
void addMandatoryComponentPorts(PatternRewriter &rewriter, SmallVectorImpl< calyx::PortInfo > &ports)
static constexpr std::string_view donePort
Definition: CalyxOps.h:32
bool isControlLeafNode(Operation *op)
static constexpr std::string_view resetPort
Definition: CalyxOps.h:33
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.
static constexpr std::string_view goPort
Definition: CalyxOps.h:31
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