CIRCT  19.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 hw::ConstantOp createConstant(Location loc, OpBuilder &builder,
31  ComponentOp component, size_t width,
32  size_t value) {
33  OpBuilder::InsertionGuard g(builder);
34  builder.setInsertionPointToStart(component.getBodyBlock());
35  return builder.create<hw::ConstantOp>(loc,
36  APInt(width, value, /*unsigned=*/true));
37 }
38 
39 calyx::InstanceOp createInstance(Location loc, OpBuilder &builder,
40  ComponentOp component,
41  SmallVectorImpl<Type> &resultTypes,
42  StringRef instanceName,
43  StringRef componentName) {
44  OpBuilder::InsertionGuard g(builder);
45  builder.setInsertionPointToStart(component.getBodyBlock());
46  return builder.create<InstanceOp>(loc, resultTypes, instanceName,
47  componentName);
48 }
49 
50 std::string getInstanceName(mlir::func::CallOp callOp) {
51  SmallVector<StringRef, 2> strVet = {callOp.getCallee(), "instance"};
52  return llvm::join(strVet, /*separator=*/"_");
53 }
54 
55 bool isControlLeafNode(Operation *op) { return isa<calyx::EnableOp>(op); }
56 
57 DictionaryAttr getMandatoryPortAttr(MLIRContext *ctx, StringRef name) {
58  return DictionaryAttr::get(
59  ctx, {NamedAttribute(StringAttr::get(ctx, name), UnitAttr::get(ctx))});
60 }
61 
62 void addMandatoryComponentPorts(PatternRewriter &rewriter,
63  SmallVectorImpl<calyx::PortInfo> &ports) {
64  MLIRContext *ctx = rewriter.getContext();
65  ports.push_back({
66  rewriter.getStringAttr(clkPort),
67  rewriter.getI1Type(),
70  });
71  ports.push_back({
72  rewriter.getStringAttr(resetPort),
73  rewriter.getI1Type(),
76  });
77  ports.push_back({
78  rewriter.getStringAttr(goPort),
79  rewriter.getI1Type(),
82  });
83  ports.push_back({
84  rewriter.getStringAttr(donePort),
85  rewriter.getI1Type(),
88  });
89 }
90 
91 unsigned handleZeroWidth(int64_t dim) {
92  return std::max(llvm::Log2_64_Ceil(dim), 1U);
93 }
94 
95 } // namespace calyx
96 } // namespace circt
int32_t width
Definition: FIRRTL.cpp:36
@ Input
Definition: HW.h:35
@ Output
Definition: HW.h:35
Builder builder
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:54
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