CIRCT 22.0.0git
Loading...
Searching...
No Matches
SCFToCalyx.cpp
Go to the documentation of this file.
1//===- SCFToCalyx.cpp - SCF to Calyx pass entry point -----------*- 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 is the main SCF to Calyx conversion pass implementation.
10//
11//===----------------------------------------------------------------------===//
12
19#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
20#include "mlir/Conversion/LLVMCommon/Pattern.h"
21#include "mlir/Dialect/Arith/IR/Arith.h"
22#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
23#include "mlir/Dialect/Func/IR/FuncOps.h"
24#include "mlir/Dialect/Math/IR/Math.h"
25#include "mlir/Dialect/MemRef/IR/MemRef.h"
26#include "mlir/Dialect/SCF/IR/SCF.h"
27#include "mlir/IR/AsmState.h"
28#include "mlir/IR/Matchers.h"
29#include "mlir/Pass/Pass.h"
30#include "mlir/Support/LogicalResult.h"
31#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
32#include "llvm/ADT/TypeSwitch.h"
33#include "llvm/Support/LogicalResult.h"
34#include "llvm/Support/raw_os_ostream.h"
35#include "llvm/Support/raw_ostream.h"
36#include <algorithm>
37#include <filesystem>
38#include <fstream>
39
40#include <locale>
41#include <numeric>
42#include <variant>
43
44namespace circt {
45#define GEN_PASS_DEF_SCFTOCALYX
46#include "circt/Conversion/Passes.h.inc"
47} // namespace circt
48
49using namespace llvm;
50using namespace mlir;
51using namespace mlir::arith;
52using namespace mlir::cf;
53using namespace mlir::func;
54namespace circt {
55class ComponentLoweringStateInterface;
56namespace scftocalyx {
57
58static constexpr std::string_view unrolledParallelAttr = "calyx.unroll";
59
60//===----------------------------------------------------------------------===//
61// Utility types
62//===----------------------------------------------------------------------===//
63
64class ScfWhileOp : public calyx::WhileOpInterface<scf::WhileOp> {
65public:
66 explicit ScfWhileOp(scf::WhileOp op)
67 : calyx::WhileOpInterface<scf::WhileOp>(op) {}
68
69 Block::BlockArgListType getBodyArgs() override {
70 return getOperation().getAfterArguments();
71 }
72
73 Block *getBodyBlock() override { return &getOperation().getAfter().front(); }
74
75 Block *getConditionBlock() override {
76 return &getOperation().getBefore().front();
77 }
78
79 Value getConditionValue() override {
80 return getOperation().getConditionOp().getOperand(0);
81 }
82
83 std::optional<int64_t> getBound() override { return std::nullopt; }
84};
85
86class ScfForOp : public calyx::RepeatOpInterface<scf::ForOp> {
87public:
88 explicit ScfForOp(scf::ForOp op) : calyx::RepeatOpInterface<scf::ForOp>(op) {}
89
90 Block::BlockArgListType getBodyArgs() override {
91 return getOperation().getRegion().getArguments();
92 }
93
94 Block *getBodyBlock() override {
95 return &getOperation().getRegion().getBlocks().front();
96 }
97
98 std::optional<int64_t> getBound() override {
99 auto scfForOp = mlir::cast<scf::ForOp>(getOperation());
100 if (std::optional<APInt> bound = scfForOp.getStaticTripCount())
101 return bound->getZExtValue();
102 return std::nullopt;
103 }
104};
105
106//===----------------------------------------------------------------------===//
107// Lowering state classes
108//===----------------------------------------------------------------------===//
109
111 scf::IfOp ifOp;
112};
113
115 /// While operation to schedule.
117};
118
120 /// For operation to schedule.
122 /// Bound
123 uint64_t bound;
124};
125
127 /// Instance for invoking.
128 calyx::InstanceOp instanceOp;
129 // CallOp for getting the arguments.
130 func::CallOp callOp;
131};
132
134 /// Parallel operation to schedule.
135 scf::ParallelOp parOp;
136};
137
138/// A variant of types representing scheduleable operations.
140 std::variant<calyx::GroupOp, WhileScheduleable, ForScheduleable,
142
144public:
145 void setCondReg(scf::IfOp op, calyx::RegisterOp regOp) {
146 Operation *operation = op.getOperation();
147 [[maybe_unused]] auto [it, succeeded] =
148 condReg.insert(std::make_pair(operation, regOp));
149 assert(succeeded &&
150 "A condition register was already set for this scf::IfOp!");
151 }
152
153 calyx::RegisterOp getCondReg(scf::IfOp op) {
154 auto it = condReg.find(op.getOperation());
155 if (it != condReg.end())
156 return it->second;
157 return nullptr;
158 }
159
160 void setThenGroup(scf::IfOp op, calyx::GroupOp group) {
161 Operation *operation = op.getOperation();
162 assert(thenGroup.count(operation) == 0 &&
163 "A then group was already set for this scf::IfOp!\n");
164 thenGroup[operation] = group;
165 }
166
167 calyx::GroupOp getThenGroup(scf::IfOp op) {
168 auto it = thenGroup.find(op.getOperation());
169 assert(it != thenGroup.end() &&
170 "No then group was set for this scf::IfOp!\n");
171 return it->second;
172 }
173
174 void setElseGroup(scf::IfOp op, calyx::GroupOp group) {
175 Operation *operation = op.getOperation();
176 assert(elseGroup.count(operation) == 0 &&
177 "An else group was already set for this scf::IfOp!\n");
178 elseGroup[operation] = group;
179 }
180
181 calyx::GroupOp getElseGroup(scf::IfOp op) {
182 auto it = elseGroup.find(op.getOperation());
183 assert(it != elseGroup.end() &&
184 "No else group was set for this scf::IfOp!\n");
185 return it->second;
186 }
187
188 void setResultRegs(scf::IfOp op, calyx::RegisterOp reg, unsigned idx) {
189 assert(resultRegs[op.getOperation()].count(idx) == 0 &&
190 "A register was already registered for the given yield result.\n");
191 assert(idx < op->getNumOperands());
192 resultRegs[op.getOperation()][idx] = reg;
193 }
194
195 const DenseMap<unsigned, calyx::RegisterOp> &getResultRegs(scf::IfOp op) {
196 return resultRegs[op.getOperation()];
197 }
198
199 calyx::RegisterOp getResultRegs(scf::IfOp op, unsigned idx) {
200 auto regs = getResultRegs(op);
201 auto it = regs.find(idx);
202 assert(it != regs.end() && "resultReg not found");
203 return it->second;
204 }
205
206private:
207 // The register to hold the result of a non-combinational guard.
208 DenseMap<Operation *, calyx::RegisterOp> condReg;
209 DenseMap<Operation *, calyx::GroupOp> thenGroup;
210 DenseMap<Operation *, calyx::GroupOp> elseGroup;
211 DenseMap<Operation *, DenseMap<unsigned, calyx::RegisterOp>> resultRegs;
212};
213
216public:
217 SmallVector<calyx::GroupOp> getWhileLoopInitGroups(ScfWhileOp op) {
218 return getLoopInitGroups(std::move(op));
219 }
221 OpBuilder &builder, ScfWhileOp op, calyx::ComponentOp componentOp,
222 Twine uniqueSuffix, MutableArrayRef<OpOperand> ops) {
223 return buildLoopIterArgAssignments(builder, std::move(op), componentOp,
224 uniqueSuffix, ops);
225 }
226 void addWhileLoopIterReg(ScfWhileOp op, calyx::RegisterOp reg, unsigned idx) {
227 return addLoopIterReg(std::move(op), reg, idx);
228 }
229 const DenseMap<unsigned, calyx::RegisterOp> &
231 return getLoopIterRegs(std::move(op));
232 }
233 void setWhileLoopLatchGroup(ScfWhileOp op, calyx::GroupOp group) {
234 return setLoopLatchGroup(std::move(op), group);
235 }
237 return getLoopLatchGroup(std::move(op));
238 }
240 SmallVector<calyx::GroupOp> groups) {
241 return setLoopInitGroups(std::move(op), std::move(groups));
242 }
243};
244
247public:
248 SmallVector<calyx::GroupOp> getForLoopInitGroups(ScfForOp op) {
249 return getLoopInitGroups(std::move(op));
250 }
252 OpBuilder &builder, ScfForOp op, calyx::ComponentOp componentOp,
253 Twine uniqueSuffix, MutableArrayRef<OpOperand> ops) {
254 return buildLoopIterArgAssignments(builder, std::move(op), componentOp,
255 uniqueSuffix, ops);
256 }
257 void addForLoopIterReg(ScfForOp op, calyx::RegisterOp reg, unsigned idx) {
258 return addLoopIterReg(std::move(op), reg, idx);
259 }
260 const DenseMap<unsigned, calyx::RegisterOp> &getForLoopIterRegs(ScfForOp op) {
261 return getLoopIterRegs(std::move(op));
262 }
263 calyx::RegisterOp getForLoopIterReg(ScfForOp op, unsigned idx) {
264 return getLoopIterReg(std::move(op), idx);
265 }
266 void setForLoopLatchGroup(ScfForOp op, calyx::GroupOp group) {
267 return setLoopLatchGroup(std::move(op), group);
268 }
269 calyx::GroupOp getForLoopLatchGroup(ScfForOp op) {
270 return getLoopLatchGroup(std::move(op));
271 }
272 void setForLoopInitGroups(ScfForOp op, SmallVector<calyx::GroupOp> groups) {
273 return setLoopInitGroups(std::move(op), std::move(groups));
274 }
275};
276
277/// Stores the state information for condition checks involving sequential
278/// computation.
280public:
281 void setSeqResReg(Operation *op, calyx::RegisterOp reg) {
282 [[maybe_unused]] auto cellOp = dyn_cast<calyx::CellInterface>(op);
283 assert(cellOp && !cellOp.isCombinational());
284 [[maybe_unused]] auto [it, succeeded] =
285 resultRegs.insert(std::make_pair(op, reg));
286 assert(succeeded &&
287 "A register was already set for this sequential operation!");
288 }
289 // Get the register for a specific pipe operation
290 calyx::RegisterOp getSeqResReg(Operation *op) {
291 auto it = resultRegs.find(op);
292 assert(it != resultRegs.end() &&
293 "No register was set for this sequential operation!");
294 return it->second;
295 }
296
297private:
298 // Maps the result of a sequential operation to the register that stores
299 // the result.
300 DenseMap<Operation *, calyx::RegisterOp> resultRegs;
301};
302
303/// Handles the current state of lowering of a Calyx component. It is mainly
304/// used as a key/value store for recording information during partial lowering,
305/// which is required at later lowering passes.
316
317//===----------------------------------------------------------------------===//
318// Conversion patterns
319//===----------------------------------------------------------------------===//
320
321/// Iterate through the operations of a source function and instantiate
322/// components or primitives based on the type of the operations.
324public:
325 BuildOpGroups(MLIRContext *context, LogicalResult &resRef,
327 DenseMap<mlir::func::FuncOp, calyx::ComponentOp> &map,
329 mlir::Pass::Option<std::string> &writeJsonOpt)
330 : FuncOpPartialLoweringPattern(context, resRef, patternState, map, state),
331 writeJson(writeJsonOpt) {}
332 using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;
333
334 LogicalResult
336 PatternRewriter &rewriter) const override {
337 /// We walk the operations of the funcOp to ensure that all def's have
338 /// been visited before their uses.
339 bool opBuiltSuccessfully = true;
340 funcOp.walk([&](Operation *_op) {
341 opBuiltSuccessfully &=
342 TypeSwitch<mlir::Operation *, bool>(_op)
343 .template Case<arith::ConstantOp, ReturnOp, BranchOpInterface,
344 /// SCF
345 scf::YieldOp, scf::WhileOp, scf::ForOp, scf::IfOp,
346 scf::ParallelOp, scf::ReduceOp,
347 scf::ExecuteRegionOp,
348 /// memref
349 memref::AllocOp, memref::AllocaOp, memref::LoadOp,
350 memref::StoreOp, memref::GetGlobalOp,
351 /// standard arithmetic
352 AddIOp, SubIOp, CmpIOp, ShLIOp, ShRUIOp, ShRSIOp,
353 AndIOp, XOrIOp, OrIOp, ExtUIOp, ExtSIOp, TruncIOp,
354 MulIOp, DivUIOp, DivSIOp, RemUIOp, RemSIOp,
355 /// floating point
356 AddFOp, SubFOp, MulFOp, CmpFOp, FPToSIOp, SIToFPOp,
357 DivFOp, math::SqrtOp, math::AbsFOp,
358 /// others
359 SelectOp, IndexCastOp, BitcastOp, CallOp>(
360 [&](auto op) { return buildOp(rewriter, op).succeeded(); })
361 .template Case<FuncOp, scf::ConditionOp>([&](auto) {
362 /// Skip: these special cases will be handled separately.
363 return true;
364 })
365 .Default([&](auto op) {
366 op->emitError() << "Unhandled operation during BuildOpGroups()";
367 return false;
368 });
369
370 return opBuiltSuccessfully ? WalkResult::advance()
371 : WalkResult::interrupt();
372 });
373
374 if (!writeJson.empty()) {
375 auto &extMemData = getState<ComponentLoweringState>().getExtMemData();
376 if (extMemData.getAsObject()->empty())
377 return success();
378
379 if (auto fileLoc = dyn_cast<mlir::FileLineColLoc>(funcOp->getLoc())) {
380 std::string filename = fileLoc.getFilename().str();
381 std::filesystem::path path(filename);
382 std::string jsonFileName = writeJson.getValue() + ".json";
383 auto outFileName = path.parent_path().append(jsonFileName);
384 std::ofstream outFile(outFileName);
385
386 if (!outFile.is_open()) {
387 llvm::errs() << "Unable to open file: " << outFileName.string()
388 << " for writing\n";
389 return failure();
390 }
391 llvm::raw_os_ostream llvmOut(outFile);
392 llvm::json::OStream jsonOS(llvmOut, /*IndentSize=*/2);
393 jsonOS.value(extMemData);
394 jsonOS.flush();
395 outFile.close();
396 }
397 }
398
399 return success(opBuiltSuccessfully);
400 }
401
402private:
403 mlir::Pass::Option<std::string> &writeJson;
404 /// Op builder specializations.
405 LogicalResult buildOp(PatternRewriter &rewriter, scf::YieldOp yieldOp) const;
406 LogicalResult buildOp(PatternRewriter &rewriter,
407 BranchOpInterface brOp) const;
408 LogicalResult buildOp(PatternRewriter &rewriter,
409 arith::ConstantOp constOp) const;
410 LogicalResult buildOp(PatternRewriter &rewriter, SelectOp op) const;
411 LogicalResult buildOp(PatternRewriter &rewriter, AddIOp op) const;
412 LogicalResult buildOp(PatternRewriter &rewriter, SubIOp op) const;
413 LogicalResult buildOp(PatternRewriter &rewriter, MulIOp op) const;
414 LogicalResult buildOp(PatternRewriter &rewriter, DivUIOp op) const;
415 LogicalResult buildOp(PatternRewriter &rewriter, DivSIOp op) const;
416 LogicalResult buildOp(PatternRewriter &rewriter, RemUIOp op) const;
417 LogicalResult buildOp(PatternRewriter &rewriter, RemSIOp op) const;
418 LogicalResult buildOp(PatternRewriter &rewriter, AddFOp op) const;
419 LogicalResult buildOp(PatternRewriter &rewriter, SubFOp op) const;
420 LogicalResult buildOp(PatternRewriter &rewriter, MulFOp op) const;
421 LogicalResult buildOp(PatternRewriter &rewriter, CmpFOp op) const;
422 LogicalResult buildOp(PatternRewriter &rewriter, FPToSIOp op) const;
423 LogicalResult buildOp(PatternRewriter &rewriter, SIToFPOp op) const;
424 LogicalResult buildOp(PatternRewriter &rewriter, DivFOp op) const;
425 LogicalResult buildOp(PatternRewriter &rewriter, math::SqrtOp op) const;
426 LogicalResult buildOp(PatternRewriter &rewriter, math::AbsFOp op) const;
427 LogicalResult buildOp(PatternRewriter &rewriter, ShRUIOp op) const;
428 LogicalResult buildOp(PatternRewriter &rewriter, ShRSIOp op) const;
429 LogicalResult buildOp(PatternRewriter &rewriter, ShLIOp op) const;
430 LogicalResult buildOp(PatternRewriter &rewriter, AndIOp op) const;
431 LogicalResult buildOp(PatternRewriter &rewriter, OrIOp op) const;
432 LogicalResult buildOp(PatternRewriter &rewriter, XOrIOp op) const;
433 LogicalResult buildOp(PatternRewriter &rewriter, CmpIOp op) const;
434 LogicalResult buildOp(PatternRewriter &rewriter, TruncIOp op) const;
435 LogicalResult buildOp(PatternRewriter &rewriter, ExtUIOp op) const;
436 LogicalResult buildOp(PatternRewriter &rewriter, ExtSIOp op) const;
437 LogicalResult buildOp(PatternRewriter &rewriter, ReturnOp op) const;
438 LogicalResult buildOp(PatternRewriter &rewriter, IndexCastOp op) const;
439 LogicalResult buildOp(PatternRewriter &rewriter, BitcastOp op) const;
440 LogicalResult buildOp(PatternRewriter &rewriter, memref::AllocOp op) const;
441 LogicalResult buildOp(PatternRewriter &rewriter, memref::AllocaOp op) const;
442 LogicalResult buildOp(PatternRewriter &rewriter,
443 memref::GetGlobalOp op) const;
444 LogicalResult buildOp(PatternRewriter &rewriter, memref::LoadOp op) const;
445 LogicalResult buildOp(PatternRewriter &rewriter, memref::StoreOp op) const;
446 LogicalResult buildOp(PatternRewriter &rewriter, scf::WhileOp whileOp) const;
447 LogicalResult buildOp(PatternRewriter &rewriter, scf::ForOp forOp) const;
448 LogicalResult buildOp(PatternRewriter &rewriter, scf::IfOp ifOp) const;
449 LogicalResult buildOp(PatternRewriter &rewriter,
450 scf::ReduceOp reduceOp) const;
451 LogicalResult buildOp(PatternRewriter &rewriter,
452 scf::ParallelOp parallelOp) const;
453 LogicalResult buildOp(PatternRewriter &rewriter,
454 scf::ExecuteRegionOp executeRegionOp) const;
455 LogicalResult buildOp(PatternRewriter &rewriter, CallOp callOp) const;
456
457 // Sets up the necessary state and resources for a `CmpIOp` in
458 // `buildLibraryBinaryPipeOp` if `cmpIOp` has sequential logic based on its
459 // operands.
460 template <typename TCalyxLibOp>
461 void setupCmpIOp(PatternRewriter &rewriter, CmpIOp cmpIOp, Operation *group,
462 calyx::RegisterOp &condReg, calyx::RegisterOp &resReg,
463 TCalyxLibOp calyxOp) const {
464 bool lhsIsSeqOp = calyx::parentIsSeqCell(cmpIOp.getLhs());
465 [[maybe_unused]] bool rhsIsSeqOp = calyx::parentIsSeqCell(cmpIOp.getRhs());
466
467 StringRef opName = cmpIOp.getOperationName().split(".").second;
468 Type width = cmpIOp.getResult().getType();
469
470 condReg = createRegister(
471 cmpIOp.getLoc(), rewriter, getComponent(),
472 width.getIntOrFloatBitWidth(),
473 getState<ComponentLoweringState>().getUniqueName(opName));
474
475 for (auto *user : cmpIOp->getUsers()) {
476 if (auto ifOp = dyn_cast<scf::IfOp>(user))
477 getState<ComponentLoweringState>().setCondReg(ifOp, condReg);
478 }
479
480 assert(
481 lhsIsSeqOp != rhsIsSeqOp &&
482 "unexpected sequential operation on both sides; please open an issue");
483 // If `cmpIOp`'s lhs/rhs operand is the result of a sequential operation,
484 // its result will be stored in a register.
485 resReg =
486 cast<calyx::RegisterOp>(lhsIsSeqOp ? cmpIOp.getLhs().getDefiningOp()
487 : cmpIOp.getRhs().getDefiningOp());
488
489 auto groupOp = cast<calyx::GroupOp>(group);
490 getState<ComponentLoweringState>().addBlockScheduleable(cmpIOp->getBlock(),
491 groupOp);
492
493 rewriter.setInsertionPointToEnd(groupOp.getBodyBlock());
494 auto loc = cmpIOp.getLoc();
495 assert(
496 (isa<calyx::EqLibOp, calyx::NeqLibOp, calyx::SleLibOp, calyx::SltLibOp,
497 calyx::LeLibOp, calyx::LtLibOp, calyx::GeLibOp, calyx::GtLibOp,
498 calyx::SgeLibOp, calyx::SgtLibOp>(calyxOp.getOperation())) &&
499 "Must be a Calyx comparison library operation.");
500 int64_t outputIndex = 2;
501 calyx::AssignOp::create(rewriter, loc, condReg.getIn(),
502 calyxOp.getResult(outputIndex));
503 calyx::AssignOp::create(
504 rewriter, loc, condReg.getWriteEn(),
505 createConstant(loc, rewriter,
506 getState<ComponentLoweringState>().getComponentOp(), 1,
507 1));
508 calyx::GroupDoneOp::create(rewriter, loc, condReg.getDone());
509
510 getState<ComponentLoweringState>().addSeqGuardCmpLibOp(cmpIOp);
511 }
512
513 template <typename CmpILibOp>
514 LogicalResult buildCmpIOpHelper(PatternRewriter &rewriter, CmpIOp op) const {
515 bool isIfOpGuard = std::any_of(op->getUsers().begin(), op->getUsers().end(),
516 [](auto op) { return isa<scf::IfOp>(op); });
517 bool isSeqCondCheck = isIfOpGuard && (calyx::parentIsSeqCell(op.getLhs()) ||
518 calyx::parentIsSeqCell(op.getRhs()));
519
520 if (isSeqCondCheck)
521 return buildLibraryOp<calyx::GroupOp, CmpILibOp>(rewriter, op);
522 return buildLibraryOp<calyx::CombGroupOp, CmpILibOp>(rewriter, op);
523 }
524
525 /// buildLibraryOp will build a TCalyxLibOp inside a TGroupOp based on the
526 /// source operation TSrcOp.
527 template <typename TGroupOp, typename TCalyxLibOp, typename TSrcOp>
528 LogicalResult buildLibraryOp(PatternRewriter &rewriter, TSrcOp op,
529 TypeRange srcTypes, TypeRange dstTypes) const {
530 SmallVector<Type> types;
531 for (Type srcType : srcTypes)
532 types.push_back(calyx::toBitVector(srcType));
533 for (Type dstType : dstTypes)
534 types.push_back(calyx::toBitVector(dstType));
535
536 auto calyxOp =
537 getState<ComponentLoweringState>().getNewLibraryOpInstance<TCalyxLibOp>(
538 rewriter, op.getLoc(), types);
539
540 auto directions = calyxOp.portDirections();
541 SmallVector<Value, 4> opInputPorts;
542 SmallVector<Value, 4> opOutputPorts;
543 for (auto dir : enumerate(directions)) {
544 if (dir.value() == calyx::Direction::Input)
545 opInputPorts.push_back(calyxOp.getResult(dir.index()));
546 else
547 opOutputPorts.push_back(calyxOp.getResult(dir.index()));
548 }
549 assert(
550 opInputPorts.size() == op->getNumOperands() &&
551 opOutputPorts.size() == op->getNumResults() &&
552 "Expected an equal number of in/out ports in the Calyx library op with "
553 "respect to the number of operands/results of the source operation.");
554
555 /// Create assignments to the inputs of the library op.
556 auto group = createGroupForOp<TGroupOp>(rewriter, op);
557
558 bool isSeqCondCheck = isa<calyx::GroupOp>(group);
559 calyx::RegisterOp condReg = nullptr, resReg = nullptr;
560 if (isa<CmpIOp>(op) && isSeqCondCheck) {
561 auto cmpIOp = cast<CmpIOp>(op);
562 setupCmpIOp(rewriter, cmpIOp, group, condReg, resReg, calyxOp);
563 }
564
565 rewriter.setInsertionPointToEnd(group.getBodyBlock());
566
567 for (auto dstOp : enumerate(opInputPorts)) {
568 auto srcOp = calyx::parentIsSeqCell(dstOp.value())
569 ? condReg.getOut()
570 : op->getOperand(dstOp.index());
571 calyx::AssignOp::create(rewriter, op.getLoc(), dstOp.value(), srcOp);
572 }
573
574 /// Replace the result values of the source operator with the new operator.
575 for (auto res : enumerate(opOutputPorts)) {
576 getState<ComponentLoweringState>().registerEvaluatingGroup(res.value(),
577 group);
578 auto dstOp = isSeqCondCheck ? condReg.getOut() : res.value();
579 op->getResult(res.index()).replaceAllUsesWith(dstOp);
580 }
581
582 return success();
583 }
584
585 /// buildLibraryOp which provides in- and output types based on the operands
586 /// and results of the op argument.
587 template <typename TGroupOp, typename TCalyxLibOp, typename TSrcOp>
588 LogicalResult buildLibraryOp(PatternRewriter &rewriter, TSrcOp op) const {
589 return buildLibraryOp<TGroupOp, TCalyxLibOp, TSrcOp>(
590 rewriter, op, op.getOperandTypes(), op->getResultTypes());
591 }
592
593 /// Creates a group named by the basic block which the input op resides in.
594 template <typename TGroupOp>
595 TGroupOp createGroupForOp(PatternRewriter &rewriter, Operation *op) const {
596 Block *block = op->getBlock();
597 auto groupName = getState<ComponentLoweringState>().getUniqueName(
598 loweringState().blockName(block));
599 return calyx::createGroup<TGroupOp>(
600 rewriter, getState<ComponentLoweringState>().getComponentOp(),
601 op->getLoc(), groupName);
602 }
603
604 /// buildLibraryBinaryPipeOp will build a TCalyxLibBinaryPipeOp, to
605 /// deal with MulIOp, DivUIOp and RemUIOp.
606 template <typename TOpType, typename TSrcOp>
607 LogicalResult buildLibraryBinaryPipeOp(PatternRewriter &rewriter, TSrcOp op,
608 TOpType opPipe, Value out) const {
609 StringRef opName = TSrcOp::getOperationName().split(".").second;
610 Location loc = op.getLoc();
611 Type width = op.getResult().getType();
612 auto reg = createRegister(
613 op.getLoc(), rewriter, getComponent(), width.getIntOrFloatBitWidth(),
614 getState<ComponentLoweringState>().getUniqueName(opName));
615
616 // Operation pipelines are not combinational, so a GroupOp is required.
617 auto group = createGroupForOp<calyx::GroupOp>(rewriter, op);
618 OpBuilder builder(group->getRegion(0));
619 getState<ComponentLoweringState>().addBlockScheduleable(op->getBlock(),
620 group);
621
622 rewriter.setInsertionPointToEnd(group.getBodyBlock());
623 if constexpr (std::is_same_v<TSrcOp, math::SqrtOp>)
624 // According to the Hardfloat library: "If sqrtOp is 1, the operation is
625 // the square root of a, and operand b is ignored."
626 calyx::AssignOp::create(rewriter, loc, opPipe.getLeft(), op.getOperand());
627 else {
628 calyx::AssignOp::create(rewriter, loc, opPipe.getLeft(), op.getLhs());
629 calyx::AssignOp::create(rewriter, loc, opPipe.getRight(), op.getRhs());
630 }
631 // Write the output to this register.
632 calyx::AssignOp::create(rewriter, loc, reg.getIn(), out);
633 // The write enable port is high when the pipeline is done.
634 calyx::AssignOp::create(rewriter, loc, reg.getWriteEn(), opPipe.getDone());
635 // Set pipelineOp to high as long as its done signal is not high.
636 // This prevents the pipelineOP from executing for the cycle that we write
637 // to register. To get !(pipelineOp.done) we do 1 xor pipelineOp.done
638 hw::ConstantOp c1 = createConstant(loc, rewriter, getComponent(), 1, 1);
639 calyx::AssignOp::create(
640 rewriter, loc, opPipe.getGo(), c1,
641 comb::createOrFoldNot(group.getLoc(), opPipe.getDone(), builder));
642 // The group is done when the register write is complete.
643 calyx::GroupDoneOp::create(rewriter, loc, reg.getDone());
644
645 // Pass the result from the source operation to register holding the resullt
646 // from the Calyx primitive.
647 op.getResult().replaceAllUsesWith(reg.getOut());
648
649 if (isa<calyx::AddFOpIEEE754>(opPipe)) {
650 auto opFOp = cast<calyx::AddFOpIEEE754>(opPipe);
651 hw::ConstantOp subOp;
652 if (isa<arith::AddFOp>(op)) {
653 subOp = createConstant(loc, rewriter, getComponent(), /*width=*/1,
654 /*subtract=*/0);
655 } else {
656 subOp = createConstant(loc, rewriter, getComponent(), /*width=*/1,
657 /*subtract=*/1);
658 }
659 calyx::AssignOp::create(rewriter, loc, opFOp.getSubOp(), subOp);
660 } else if (auto opFOp =
661 dyn_cast<calyx::DivSqrtOpIEEE754>(opPipe.getOperation())) {
662 bool isSqrt = !isa<arith::DivFOp>(op);
663 hw::ConstantOp sqrtOp =
664 createConstant(loc, rewriter, getComponent(), /*width=*/1, isSqrt);
665 calyx::AssignOp::create(rewriter, loc, opFOp.getSqrtOp(), sqrtOp);
666 }
667
668 // Register the values for the pipeline.
669 getState<ComponentLoweringState>().registerEvaluatingGroup(out, group);
670 getState<ComponentLoweringState>().registerEvaluatingGroup(opPipe.getLeft(),
671 group);
672 getState<ComponentLoweringState>().registerEvaluatingGroup(
673 opPipe.getRight(), group);
674
675 getState<ComponentLoweringState>().setSeqResReg(out.getDefiningOp(), reg);
676
677 return success();
678 }
679
680 template <typename TCalyxLibOp, typename TSrcOp>
681 LogicalResult buildFpIntTypeCastOp(PatternRewriter &rewriter, TSrcOp op,
682 unsigned inputWidth, unsigned outputWidth,
683 StringRef signedPort) const {
684 Location loc = op.getLoc();
685 IntegerType one = rewriter.getI1Type(),
686 inWidth = rewriter.getIntegerType(inputWidth),
687 outWidth = rewriter.getIntegerType(outputWidth);
688 auto calyxOp =
689 getState<ComponentLoweringState>().getNewLibraryOpInstance<TCalyxLibOp>(
690 rewriter, loc, {one, one, one, inWidth, one, outWidth, one});
691 hw::ConstantOp c1 = createConstant(loc, rewriter, getComponent(), 1, 1);
692 StringRef opName = op.getOperationName().split(".").second;
693 rewriter.setInsertionPointToStart(getComponent().getBodyBlock());
694 auto reg = createRegister(
695 loc, rewriter, getComponent(), outWidth.getIntOrFloatBitWidth(),
696 getState<ComponentLoweringState>().getUniqueName(opName));
697
698 auto group = createGroupForOp<calyx::GroupOp>(rewriter, op);
699 OpBuilder builder(group->getRegion(0));
700 getState<ComponentLoweringState>().addBlockScheduleable(op->getBlock(),
701 group);
702
703 rewriter.setInsertionPointToEnd(group.getBodyBlock());
704 calyx::AssignOp::create(rewriter, loc, calyxOp.getIn(), op.getIn());
705 if (isa<calyx::FpToIntOpIEEE754>(calyxOp)) {
706 calyx::AssignOp::create(
707 rewriter, loc, cast<calyx::FpToIntOpIEEE754>(calyxOp).getSignedOut(),
708 c1);
709 } else if (isa<calyx::IntToFpOpIEEE754>(calyxOp)) {
710 calyx::AssignOp::create(
711 rewriter, loc, cast<calyx::IntToFpOpIEEE754>(calyxOp).getSignedIn(),
712 c1);
713 }
714 op.getResult().replaceAllUsesWith(reg.getOut());
715
716 calyx::AssignOp::create(rewriter, loc, reg.getIn(), calyxOp.getOut());
717 calyx::AssignOp::create(rewriter, loc, reg.getWriteEn(), c1);
718
719 calyx::AssignOp::create(
720 rewriter, loc, calyxOp.getGo(), c1,
721 comb::createOrFoldNot(loc, calyxOp.getDone(), builder));
722 calyx::GroupDoneOp::create(rewriter, loc, reg.getDone());
723
724 return success();
725 }
726
727 /// Creates assignments within the provided group to the address ports of the
728 /// memoryOp based on the provided addressValues.
729 void assignAddressPorts(PatternRewriter &rewriter, Location loc,
730 calyx::GroupInterface group,
731 calyx::MemoryInterface memoryInterface,
732 Operation::operand_range addressValues) const {
733 IRRewriter::InsertionGuard guard(rewriter);
734 rewriter.setInsertionPointToEnd(group.getBody());
735 auto addrPorts = memoryInterface.addrPorts();
736 if (addressValues.empty()) {
737 assert(
738 addrPorts.size() == 1 &&
739 "We expected a 1 dimensional memory of size 1 because there were no "
740 "address assignment values");
741 // Assign to address 1'd0 in memory.
742 calyx::AssignOp::create(
743 rewriter, loc, addrPorts[0],
744 createConstant(loc, rewriter, getComponent(), 1, 0));
745 } else {
746 assert(addrPorts.size() == addressValues.size() &&
747 "Mismatch between number of address ports of the provided memory "
748 "and address assignment values");
749 for (auto address : enumerate(addressValues))
750 calyx::AssignOp::create(rewriter, loc, addrPorts[address.index()],
751 address.value());
752 }
753 }
754
755 calyx::RegisterOp createSignalRegister(PatternRewriter &rewriter,
756 Value signal, bool invert,
757 StringRef nameSuffix,
758 calyx::CompareFOpIEEE754 calyxCmpFOp,
759 calyx::GroupOp group) const {
760 Location loc = calyxCmpFOp.getLoc();
761 IntegerType one = rewriter.getI1Type();
762 auto component = getComponent();
763 OpBuilder builder(group->getRegion(0));
764 auto reg = createRegister(
765 loc, rewriter, component, 1,
766 getState<ComponentLoweringState>().getUniqueName(nameSuffix));
767 calyx::AssignOp::create(rewriter, loc, reg.getWriteEn(),
768 calyxCmpFOp.getDone());
769 if (invert) {
770 auto notLibOp = getState<ComponentLoweringState>()
771 .getNewLibraryOpInstance<calyx::NotLibOp>(
772 rewriter, loc, {one, one});
773 calyx::AssignOp::create(rewriter, loc, notLibOp.getIn(), signal);
774 calyx::AssignOp::create(rewriter, loc, reg.getIn(), notLibOp.getOut());
775 getState<ComponentLoweringState>().registerEvaluatingGroup(
776 notLibOp.getOut(), group);
777 } else
778 calyx::AssignOp::create(rewriter, loc, reg.getIn(), signal);
779 return reg;
780 };
781};
782
783LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
784 memref::LoadOp loadOp) const {
785 Value memref = loadOp.getMemref();
786 auto memoryInterface =
787 getState<ComponentLoweringState>().getMemoryInterface(memref);
788 auto group = createGroupForOp<calyx::GroupOp>(rewriter, loadOp);
789 assignAddressPorts(rewriter, loadOp.getLoc(), group, memoryInterface,
790 loadOp.getIndices());
791
792 rewriter.setInsertionPointToEnd(group.getBodyBlock());
793
794 bool needReg = true;
795 Value res;
796 Value regWriteEn =
797 createConstant(loadOp.getLoc(), rewriter, getComponent(), 1, 1);
798 if (memoryInterface.readEnOpt().has_value()) {
799 auto oneI1 =
800 calyx::createConstant(loadOp.getLoc(), rewriter, getComponent(), 1, 1);
801 calyx::AssignOp::create(rewriter, loadOp.getLoc(), memoryInterface.readEn(),
802 oneI1);
803 regWriteEn = memoryInterface.done();
804 if (calyx::noStoresToMemory(memref) &&
806 // Single load from memory; we do not need to write the output to a
807 // register. The readData value will be held until readEn is asserted
808 // again
809 needReg = false;
810 calyx::GroupDoneOp::create(rewriter, loadOp.getLoc(),
811 memoryInterface.done());
812 // We refrain from replacing the loadOp result with
813 // memoryInterface.readData, since multiple loadOp's need to be converted
814 // to a single memory's ReadData. If this replacement is done now, we lose
815 // the link between which SSA memref::LoadOp values map to which groups
816 // for loading a value from the Calyx memory. At this point of lowering,
817 // we keep the memref::LoadOp SSA value, and do value replacement _after_
818 // control has been generated (see LateSSAReplacement). This is *vital*
819 // for things such as calyx::InlineCombGroups to be able to properly track
820 // which memory assignment groups belong to which accesses.
821 res = loadOp.getResult();
822 }
823 } else if (memoryInterface.contentEnOpt().has_value()) {
824 auto oneI1 =
825 calyx::createConstant(loadOp.getLoc(), rewriter, getComponent(), 1, 1);
826 auto zeroI1 =
827 calyx::createConstant(loadOp.getLoc(), rewriter, getComponent(), 1, 0);
828 calyx::AssignOp::create(rewriter, loadOp.getLoc(),
829 memoryInterface.contentEn(), oneI1);
830 calyx::AssignOp::create(rewriter, loadOp.getLoc(),
831 memoryInterface.writeEn(), zeroI1);
832 regWriteEn = memoryInterface.done();
833 if (calyx::noStoresToMemory(memref) &&
835 // Single load from memory; we do not need to write the output to a
836 // register. The readData value will be held until contentEn is asserted
837 // again
838 needReg = false;
839 calyx::GroupDoneOp::create(rewriter, loadOp.getLoc(),
840 memoryInterface.done());
841 // We refrain from replacing the loadOp result with
842 // memoryInterface.readData, since multiple loadOp's need to be converted
843 // to a single memory's ReadData. If this replacement is done now, we lose
844 // the link between which SSA memref::LoadOp values map to which groups
845 // for loading a value from the Calyx memory. At this point of lowering,
846 // we keep the memref::LoadOp SSA value, and do value replacement _after_
847 // control has been generated (see LateSSAReplacement). This is *vital*
848 // for things such as calyx::InlineCombGroups to be able to properly track
849 // which memory assignment groups belong to which accesses.
850 res = loadOp.getResult();
851 }
852 }
853
854 if (needReg) {
855 // Multiple loads from the same memory; In this case, we _may_ have a
856 // structural hazard in the design we generate. To get around this, we
857 // conservatively place a register in front of each load operation, and
858 // replace all uses of the loaded value with the register output. Reading
859 // for sequential memories will cause a read to take at least 2 cycles,
860 // but it will usually be better because combinational reads on memories
861 // can significantly decrease the maximum achievable frequency.
862 auto reg = createRegister(
863 loadOp.getLoc(), rewriter, getComponent(),
864 loadOp.getMemRefType().getElementTypeBitWidth(),
865 getState<ComponentLoweringState>().getUniqueName("load"));
866 rewriter.setInsertionPointToEnd(group.getBodyBlock());
867 calyx::AssignOp::create(rewriter, loadOp.getLoc(), reg.getIn(),
868 memoryInterface.readData());
869 calyx::AssignOp::create(rewriter, loadOp.getLoc(), reg.getWriteEn(),
870 regWriteEn);
871 calyx::GroupDoneOp::create(rewriter, loadOp.getLoc(), reg.getDone());
872 loadOp.getResult().replaceAllUsesWith(reg.getOut());
873 res = reg.getOut();
874 }
875
876 getState<ComponentLoweringState>().registerEvaluatingGroup(res, group);
877 getState<ComponentLoweringState>().addBlockScheduleable(loadOp->getBlock(),
878 group);
879 return success();
880}
881
882LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
883 memref::StoreOp storeOp) const {
884 auto memoryInterface = getState<ComponentLoweringState>().getMemoryInterface(
885 storeOp.getMemref());
886 auto group = createGroupForOp<calyx::GroupOp>(rewriter, storeOp);
887
888 // This is a sequential group, so register it as being scheduleable for the
889 // block.
890 getState<ComponentLoweringState>().addBlockScheduleable(storeOp->getBlock(),
891 group);
892 assignAddressPorts(rewriter, storeOp.getLoc(), group, memoryInterface,
893 storeOp.getIndices());
894 rewriter.setInsertionPointToEnd(group.getBodyBlock());
895 calyx::AssignOp::create(rewriter, storeOp.getLoc(),
896 memoryInterface.writeData(),
897 storeOp.getValueToStore());
898 calyx::AssignOp::create(
899 rewriter, storeOp.getLoc(), memoryInterface.writeEn(),
900 createConstant(storeOp.getLoc(), rewriter, getComponent(), 1, 1));
901 if (memoryInterface.contentEnOpt().has_value()) {
902 // If memory has content enable, it must be asserted when writing
903 calyx::AssignOp::create(
904 rewriter, storeOp.getLoc(), memoryInterface.contentEn(),
905 createConstant(storeOp.getLoc(), rewriter, getComponent(), 1, 1));
906 }
907 calyx::GroupDoneOp::create(rewriter, storeOp.getLoc(),
908 memoryInterface.done());
909
910 return success();
911}
912
913LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
914 MulIOp mul) const {
915 Location loc = mul.getLoc();
916 Type width = mul.getResult().getType(), one = rewriter.getI1Type();
917 auto mulPipe =
918 getState<ComponentLoweringState>()
919 .getNewLibraryOpInstance<calyx::MultPipeLibOp>(
920 rewriter, loc, {one, one, one, width, width, width, one});
921 return buildLibraryBinaryPipeOp<calyx::MultPipeLibOp>(
922 rewriter, mul, mulPipe,
923 /*out=*/mulPipe.getOut());
924}
925
926LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
927 DivUIOp div) const {
928 Location loc = div.getLoc();
929 Type width = div.getResult().getType(), one = rewriter.getI1Type();
930 auto divPipe =
931 getState<ComponentLoweringState>()
932 .getNewLibraryOpInstance<calyx::DivUPipeLibOp>(
933 rewriter, loc, {one, one, one, width, width, width, one});
934 return buildLibraryBinaryPipeOp<calyx::DivUPipeLibOp>(
935 rewriter, div, divPipe,
936 /*out=*/divPipe.getOut());
937}
938
939LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
940 DivSIOp div) const {
941 Location loc = div.getLoc();
942 Type width = div.getResult().getType(), one = rewriter.getI1Type();
943 auto divPipe =
944 getState<ComponentLoweringState>()
945 .getNewLibraryOpInstance<calyx::DivSPipeLibOp>(
946 rewriter, loc, {one, one, one, width, width, width, one});
947 return buildLibraryBinaryPipeOp<calyx::DivSPipeLibOp>(
948 rewriter, div, divPipe,
949 /*out=*/divPipe.getOut());
950}
951
952LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
953 RemUIOp rem) const {
954 Location loc = rem.getLoc();
955 Type width = rem.getResult().getType(), one = rewriter.getI1Type();
956 auto remPipe =
957 getState<ComponentLoweringState>()
958 .getNewLibraryOpInstance<calyx::RemUPipeLibOp>(
959 rewriter, loc, {one, one, one, width, width, width, one});
960 return buildLibraryBinaryPipeOp<calyx::RemUPipeLibOp>(
961 rewriter, rem, remPipe,
962 /*out=*/remPipe.getOut());
963}
964
965LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
966 RemSIOp rem) const {
967 Location loc = rem.getLoc();
968 Type width = rem.getResult().getType(), one = rewriter.getI1Type();
969 auto remPipe =
970 getState<ComponentLoweringState>()
971 .getNewLibraryOpInstance<calyx::RemSPipeLibOp>(
972 rewriter, loc, {one, one, one, width, width, width, one});
973 return buildLibraryBinaryPipeOp<calyx::RemSPipeLibOp>(
974 rewriter, rem, remPipe,
975 /*out=*/remPipe.getOut());
976}
977
978LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
979 AddFOp addf) const {
980 Location loc = addf.getLoc();
981 IntegerType one = rewriter.getI1Type(), three = rewriter.getIntegerType(3),
982 five = rewriter.getIntegerType(5),
983 width = rewriter.getIntegerType(
984 addf.getType().getIntOrFloatBitWidth());
985 auto addFOp =
986 getState<ComponentLoweringState>()
987 .getNewLibraryOpInstance<calyx::AddFOpIEEE754>(
988 rewriter, loc,
989 {one, one, one, one, one, width, width, three, width, five, one});
990 return buildLibraryBinaryPipeOp<calyx::AddFOpIEEE754>(rewriter, addf, addFOp,
991 addFOp.getOut());
992}
993
994LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
995 SubFOp subf) const {
996 Location loc = subf.getLoc();
997 IntegerType one = rewriter.getI1Type(), three = rewriter.getIntegerType(3),
998 five = rewriter.getIntegerType(5),
999 width = rewriter.getIntegerType(
1000 subf.getType().getIntOrFloatBitWidth());
1001 auto subFOp =
1002 getState<ComponentLoweringState>()
1003 .getNewLibraryOpInstance<calyx::AddFOpIEEE754>(
1004 rewriter, loc,
1005 {one, one, one, one, one, width, width, three, width, five, one});
1006 return buildLibraryBinaryPipeOp<calyx::AddFOpIEEE754>(rewriter, subf, subFOp,
1007 subFOp.getOut());
1008}
1009
1010LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1011 MulFOp mulf) const {
1012 Location loc = mulf.getLoc();
1013 IntegerType one = rewriter.getI1Type(), three = rewriter.getIntegerType(3),
1014 five = rewriter.getIntegerType(5),
1015 width = rewriter.getIntegerType(
1016 mulf.getType().getIntOrFloatBitWidth());
1017 auto mulFOp =
1018 getState<ComponentLoweringState>()
1019 .getNewLibraryOpInstance<calyx::MulFOpIEEE754>(
1020 rewriter, loc,
1021 {one, one, one, one, width, width, three, width, five, one});
1022 return buildLibraryBinaryPipeOp<calyx::MulFOpIEEE754>(rewriter, mulf, mulFOp,
1023 mulFOp.getOut());
1024}
1025
1026LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1027 CmpFOp cmpf) const {
1028 Location loc = cmpf.getLoc();
1029 IntegerType one = rewriter.getI1Type(), five = rewriter.getIntegerType(5),
1030 width = rewriter.getIntegerType(
1031 cmpf.getLhs().getType().getIntOrFloatBitWidth());
1032 auto calyxCmpFOp = getState<ComponentLoweringState>()
1033 .getNewLibraryOpInstance<calyx::CompareFOpIEEE754>(
1034 rewriter, loc,
1035 {one, one, one, width, width, one, one, one, one,
1036 one, five, one});
1037 hw::ConstantOp c0 = createConstant(loc, rewriter, getComponent(), 1, 0);
1038 hw::ConstantOp c1 = createConstant(loc, rewriter, getComponent(), 1, 1);
1039 rewriter.setInsertionPointToStart(getComponent().getBodyBlock());
1040
1042 using CombLogic = PredicateInfo::CombLogic;
1043 using Port = PredicateInfo::InputPorts::Port;
1044 PredicateInfo info = calyx::getPredicateInfo(cmpf.getPredicate());
1045 if (info.logic == CombLogic::None) {
1046 if (cmpf.getPredicate() == CmpFPredicate::AlwaysTrue) {
1047 cmpf.getResult().replaceAllUsesWith(c1);
1048 return success();
1049 }
1050
1051 if (cmpf.getPredicate() == CmpFPredicate::AlwaysFalse) {
1052 cmpf.getResult().replaceAllUsesWith(c0);
1053 return success();
1054 }
1055 }
1056
1057 // General case
1058 StringRef opName = cmpf.getOperationName().split(".").second;
1059 auto reg =
1060 createRegister(loc, rewriter, getComponent(), 1,
1061 getState<ComponentLoweringState>().getUniqueName(opName));
1062
1063 // Operation pipelines are not combinational, so a GroupOp is required.
1064 auto group = createGroupForOp<calyx::GroupOp>(rewriter, cmpf);
1065 OpBuilder builder(group->getRegion(0));
1066 getState<ComponentLoweringState>().addBlockScheduleable(cmpf->getBlock(),
1067 group);
1068
1069 rewriter.setInsertionPointToEnd(group.getBodyBlock());
1070 calyx::AssignOp::create(rewriter, loc, calyxCmpFOp.getLeft(), cmpf.getLhs());
1071 calyx::AssignOp::create(rewriter, loc, calyxCmpFOp.getRight(), cmpf.getRhs());
1072
1073 bool signalingFlag = false;
1074 switch (cmpf.getPredicate()) {
1075 case CmpFPredicate::UGT:
1076 case CmpFPredicate::UGE:
1077 case CmpFPredicate::ULT:
1078 case CmpFPredicate::ULE:
1079 case CmpFPredicate::OGT:
1080 case CmpFPredicate::OGE:
1081 case CmpFPredicate::OLT:
1082 case CmpFPredicate::OLE:
1083 signalingFlag = true;
1084 break;
1085 case CmpFPredicate::UEQ:
1086 case CmpFPredicate::UNE:
1087 case CmpFPredicate::OEQ:
1088 case CmpFPredicate::ONE:
1089 case CmpFPredicate::UNO:
1090 case CmpFPredicate::ORD:
1091 case CmpFPredicate::AlwaysTrue:
1092 case CmpFPredicate::AlwaysFalse:
1093 signalingFlag = false;
1094 break;
1095 }
1096
1097 // The IEEE Standard mandates that equality comparisons ordinarily are quiet,
1098 // while inequality comparisons ordinarily are signaling.
1099 calyx::AssignOp::create(rewriter, loc, calyxCmpFOp.getSignaling(),
1100 signalingFlag ? c1 : c0);
1101
1102 // Prepare signals and create registers
1103 SmallVector<calyx::RegisterOp> inputRegs;
1104 for (const auto &input : info.inputPorts) {
1105 Value signal;
1106 switch (input.port) {
1107 case Port::Eq: {
1108 signal = calyxCmpFOp.getEq();
1109 break;
1110 }
1111 case Port::Gt: {
1112 signal = calyxCmpFOp.getGt();
1113 break;
1114 }
1115 case Port::Lt: {
1116 signal = calyxCmpFOp.getLt();
1117 break;
1118 }
1119 case Port::Unordered: {
1120 signal = calyxCmpFOp.getUnordered();
1121 break;
1122 }
1123 }
1124 std::string nameSuffix =
1125 (input.port == PredicateInfo::InputPorts::Port::Unordered)
1126 ? "unordered_port"
1127 : "compare_port";
1128 auto signalReg = createSignalRegister(rewriter, signal, input.invert,
1129 nameSuffix, calyxCmpFOp, group);
1130 inputRegs.push_back(signalReg);
1131 }
1132
1133 // Create the output logical operation
1134 Value outputValue, doneValue;
1135 switch (info.logic) {
1136 case CombLogic::None: {
1137 // it's guaranteed to be either ORD or UNO
1138 outputValue = inputRegs[0].getOut();
1139 doneValue = inputRegs[0].getDone();
1140 break;
1141 }
1142 case CombLogic::And: {
1143 auto outputLibOp = getState<ComponentLoweringState>()
1144 .getNewLibraryOpInstance<calyx::AndLibOp>(
1145 rewriter, loc, {one, one, one});
1146 calyx::AssignOp::create(rewriter, loc, outputLibOp.getLeft(),
1147 inputRegs[0].getOut());
1148 calyx::AssignOp::create(rewriter, loc, outputLibOp.getRight(),
1149 inputRegs[1].getOut());
1150
1151 outputValue = outputLibOp.getOut();
1152 break;
1153 }
1154 case CombLogic::Or: {
1155 auto outputLibOp = getState<ComponentLoweringState>()
1156 .getNewLibraryOpInstance<calyx::OrLibOp>(
1157 rewriter, loc, {one, one, one});
1158 calyx::AssignOp::create(rewriter, loc, outputLibOp.getLeft(),
1159 inputRegs[0].getOut());
1160 calyx::AssignOp::create(rewriter, loc, outputLibOp.getRight(),
1161 inputRegs[1].getOut());
1162
1163 outputValue = outputLibOp.getOut();
1164 break;
1165 }
1166 }
1167
1168 if (info.logic != CombLogic::None) {
1169 auto doneLibOp = getState<ComponentLoweringState>()
1170 .getNewLibraryOpInstance<calyx::AndLibOp>(
1171 rewriter, loc, {one, one, one});
1172 calyx::AssignOp::create(rewriter, loc, doneLibOp.getLeft(),
1173 inputRegs[0].getDone());
1174 calyx::AssignOp::create(rewriter, loc, doneLibOp.getRight(),
1175 inputRegs[1].getDone());
1176 doneValue = doneLibOp.getOut();
1177 }
1178
1179 // Write to the output register
1180 calyx::AssignOp::create(rewriter, loc, reg.getIn(), outputValue);
1181 calyx::AssignOp::create(rewriter, loc, reg.getWriteEn(), doneValue);
1182
1183 // Set the go and done signal
1184 calyx::AssignOp::create(
1185 rewriter, loc, calyxCmpFOp.getGo(), c1,
1186 comb::createOrFoldNot(loc, calyxCmpFOp.getDone(), builder));
1187 calyx::GroupDoneOp::create(rewriter, loc, reg.getDone());
1188
1189 cmpf.getResult().replaceAllUsesWith(reg.getOut());
1190
1191 // Register evaluating groups
1192 getState<ComponentLoweringState>().registerEvaluatingGroup(outputValue,
1193 group);
1194 getState<ComponentLoweringState>().registerEvaluatingGroup(doneValue, group);
1195 getState<ComponentLoweringState>().registerEvaluatingGroup(
1196 calyxCmpFOp.getLeft(), group);
1197 getState<ComponentLoweringState>().registerEvaluatingGroup(
1198 calyxCmpFOp.getRight(), group);
1199
1200 return success();
1201}
1202
1203LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1204 FPToSIOp fptosi) const {
1205 return buildFpIntTypeCastOp<calyx::FpToIntOpIEEE754>(
1206 rewriter, fptosi, fptosi.getIn().getType().getIntOrFloatBitWidth(),
1207 fptosi.getOut().getType().getIntOrFloatBitWidth(), "signedOut");
1208}
1209
1210LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1211 SIToFPOp sitofp) const {
1212 return buildFpIntTypeCastOp<calyx::IntToFpOpIEEE754>(
1213 rewriter, sitofp, sitofp.getIn().getType().getIntOrFloatBitWidth(),
1214 sitofp.getOut().getType().getIntOrFloatBitWidth(), "signedIn");
1215}
1216
1217LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1218 DivFOp divf) const {
1219 Location loc = divf.getLoc();
1220 IntegerType one = rewriter.getI1Type(), three = rewriter.getIntegerType(3),
1221 five = rewriter.getIntegerType(5),
1222 width = rewriter.getIntegerType(
1223 divf.getType().getIntOrFloatBitWidth());
1224 auto divFOp = getState<ComponentLoweringState>()
1225 .getNewLibraryOpInstance<calyx::DivSqrtOpIEEE754>(
1226 rewriter, loc,
1227 {/*clk=*/one, /*reset=*/one, /*go=*/one,
1228 /*control=*/one, /*sqrtOp=*/one, /*left=*/width,
1229 /*right=*/width, /*roundingMode=*/three, /*out=*/width,
1230 /*exceptionalFlags=*/five, /*done=*/one});
1231 return buildLibraryBinaryPipeOp<calyx::DivSqrtOpIEEE754>(
1232 rewriter, divf, divFOp, divFOp.getOut());
1233}
1234
1235LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1236 math::SqrtOp sqrt) const {
1237 Location loc = sqrt.getLoc();
1238 IntegerType one = rewriter.getI1Type(), three = rewriter.getIntegerType(3),
1239 five = rewriter.getIntegerType(5),
1240 width = rewriter.getIntegerType(
1241 sqrt.getType().getIntOrFloatBitWidth());
1242 auto sqrtOp = getState<ComponentLoweringState>()
1243 .getNewLibraryOpInstance<calyx::DivSqrtOpIEEE754>(
1244 rewriter, loc,
1245 {/*clk=*/one, /*reset=*/one, /*go=*/one,
1246 /*control=*/one, /*sqrtOp=*/one, /*left=*/width,
1247 /*right=*/width, /*roundingMode=*/three, /*out=*/width,
1248 /*exceptionalFlags=*/five, /*done=*/one});
1249 return buildLibraryBinaryPipeOp<calyx::DivSqrtOpIEEE754>(
1250 rewriter, sqrt, sqrtOp, sqrtOp.getOut());
1251}
1252
1253LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1254 math::AbsFOp absFOp) const {
1255 Location loc = absFOp.getLoc();
1256 auto input = absFOp.getOperand();
1257
1258 unsigned bitwidth = input.getType().getIntOrFloatBitWidth();
1259 Type intTy = rewriter.getIntegerType(bitwidth);
1260
1261 uint64_t signBit = 1ULL << (bitwidth - 1);
1262 uint64_t absMask = ~signBit & ((1ULL << bitwidth) - 1); // clear sign bit
1263
1264 Value maskOp = arith::ConstantIntOp::create(rewriter, loc, intTy, absMask);
1265
1266 auto combGroup = createGroupForOp<calyx::CombGroupOp>(rewriter, absFOp);
1267 rewriter.setInsertionPointToStart(combGroup.getBodyBlock());
1268
1269 auto andLibOp = getState<ComponentLoweringState>()
1270 .getNewLibraryOpInstance<calyx::AndLibOp>(
1271 rewriter, loc, {intTy, intTy, intTy});
1272 calyx::AssignOp::create(rewriter, loc, andLibOp.getLeft(), maskOp);
1273 calyx::AssignOp::create(rewriter, loc, andLibOp.getRight(), input);
1274
1275 getState<ComponentLoweringState>().registerEvaluatingGroup(andLibOp.getOut(),
1276 combGroup);
1277 rewriter.replaceAllUsesWith(absFOp, andLibOp.getOut());
1278
1279 return success();
1280}
1281
1282template <typename TAllocOp>
1283static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
1284 PatternRewriter &rewriter, TAllocOp allocOp) {
1285 rewriter.setInsertionPointToStart(
1286 componentState.getComponentOp().getBodyBlock());
1287 MemRefType memtype = allocOp.getType();
1288 SmallVector<int64_t> addrSizes;
1289 SmallVector<int64_t> sizes;
1290 for (int64_t dim : memtype.getShape()) {
1291 sizes.push_back(dim);
1292 addrSizes.push_back(calyx::handleZeroWidth(dim));
1293 }
1294 // If memref has no size (e.g., memref<i32>) create a 1 dimensional memory of
1295 // size 1.
1296 if (sizes.empty() && addrSizes.empty()) {
1297 sizes.push_back(1);
1298 addrSizes.push_back(1);
1299 }
1300 auto memoryOp = calyx::SeqMemoryOp::create(
1301 rewriter, allocOp.getLoc(), componentState.getUniqueName("mem"),
1302 memtype.getElementType().getIntOrFloatBitWidth(), sizes, addrSizes);
1303
1304 // Externalize memories conditionally (only in the top-level component because
1305 // Calyx compiler requires it as a well-formness check).
1306 memoryOp->setAttr("external",
1307 IntegerAttr::get(rewriter.getI1Type(), llvm::APInt(1, 1)));
1308 componentState.registerMemoryInterface(allocOp.getResult(),
1309 calyx::MemoryInterface(memoryOp));
1310
1311 unsigned elmTyBitWidth = memtype.getElementTypeBitWidth();
1312 assert(elmTyBitWidth <= 64 && "element bitwidth should not exceed 64");
1313 bool isFloat = !memtype.getElementType().isInteger();
1314
1315 auto shape = allocOp.getType().getShape();
1316 int totalSize =
1317 std::reduce(shape.begin(), shape.end(), 1, std::multiplies<int>());
1318 // The `totalSize <= 1` check is a hack to:
1319 // https://github.com/llvm/circt/pull/2661, where a multi-dimensional memory
1320 // whose size in some dimension equals 1, e.g. memref<1x1x1x1xi32>, will be
1321 // collapsed to `memref<1xi32>` with `totalSize == 1`. While the above case is
1322 // a trivial fix, Calyx expects 1-dimensional memories in general:
1323 // https://github.com/calyxir/calyx/issues/907
1324 if (!(shape.size() <= 1 || totalSize <= 1)) {
1325 allocOp.emitError("input memory dimension must be empty or one.");
1326 return failure();
1327 }
1328
1329 std::vector<uint64_t> flattenedVals(totalSize, 0);
1330 if (isa<memref::GetGlobalOp>(allocOp)) {
1331 auto getGlobalOp = cast<memref::GetGlobalOp>(allocOp);
1332 auto *symbolTableOp =
1333 getGlobalOp->template getParentWithTrait<mlir::OpTrait::SymbolTable>();
1334 auto globalOp = dyn_cast_or_null<memref::GlobalOp>(
1335 SymbolTable::lookupSymbolIn(symbolTableOp, getGlobalOp.getNameAttr()));
1336 // Flatten the values in the attribute
1337 auto cstAttr = llvm::dyn_cast_or_null<DenseElementsAttr>(
1338 globalOp.getConstantInitValue());
1339 int sizeCount = 0;
1340 for (auto attr : cstAttr.template getValues<Attribute>()) {
1341 assert((isa<mlir::FloatAttr, mlir::IntegerAttr>(attr)) &&
1342 "memory attributes must be float or int");
1343 if (auto fltAttr = dyn_cast<mlir::FloatAttr>(attr)) {
1344 flattenedVals[sizeCount++] =
1345 bit_cast<uint64_t>(fltAttr.getValueAsDouble());
1346 } else {
1347 auto intAttr = dyn_cast<mlir::IntegerAttr>(attr);
1348 APInt value = intAttr.getValue();
1349 flattenedVals[sizeCount++] = *value.getRawData();
1350 }
1351 }
1352
1353 rewriter.eraseOp(globalOp);
1354 }
1355
1356 llvm::json::Array result;
1357 result.reserve(std::max(static_cast<int>(shape.size()), 1));
1358
1359 Type elemType = memtype.getElementType();
1360 bool isSigned =
1361 !elemType.isSignlessInteger() && !elemType.isUnsignedInteger();
1362 for (uint64_t bitValue : flattenedVals) {
1363 llvm::json::Value value = 0;
1364 if (isFloat) {
1365 // We cast to `double` and let downstream calyx to deal with the actual
1366 // value's precision handling.
1367 value = bit_cast<double>(bitValue);
1368 } else {
1369 APInt apInt(/*numBits=*/elmTyBitWidth, bitValue, isSigned,
1370 /*implicitTrunc=*/true);
1371 // The conditional ternary operation will cause the `value` to interpret
1372 // the underlying data as unsigned regardless `isSigned` or not.
1373 if (isSigned)
1374 value = static_cast<int64_t>(apInt.getSExtValue());
1375 else
1376 value = apInt.getZExtValue();
1377 }
1378 result.push_back(std::move(value));
1379 }
1380
1381 componentState.setDataField(memoryOp.getName(), result);
1382 std::string numType =
1383 memtype.getElementType().isInteger() ? "bitnum" : "ieee754_float";
1384 componentState.setFormat(memoryOp.getName(), numType, isSigned,
1385 elmTyBitWidth);
1386
1387 return success();
1388}
1389
1390LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1391 memref::AllocOp allocOp) const {
1392 return buildAllocOp(getState<ComponentLoweringState>(), rewriter, allocOp);
1393}
1394
1395LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1396 memref::AllocaOp allocOp) const {
1397 return buildAllocOp(getState<ComponentLoweringState>(), rewriter, allocOp);
1398}
1399
1400LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1401 memref::GetGlobalOp getGlobalOp) const {
1402 return buildAllocOp(getState<ComponentLoweringState>(), rewriter,
1403 getGlobalOp);
1404}
1405
1406LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1407 scf::YieldOp yieldOp) const {
1408 if (yieldOp.getOperands().empty()) {
1409 if (auto forOp = dyn_cast<scf::ForOp>(yieldOp->getParentOp())) {
1410 ScfForOp forOpInterface(forOp);
1411
1412 // Get the ForLoop's Induction Register.
1413 auto inductionReg = getState<ComponentLoweringState>().getForLoopIterReg(
1414 forOpInterface, 0);
1415
1416 Type regWidth = inductionReg.getOut().getType();
1417 // Adder should have same width as the inductionReg.
1418 SmallVector<Type> types(3, regWidth);
1419 auto addOp = getState<ComponentLoweringState>()
1420 .getNewLibraryOpInstance<calyx::AddLibOp>(
1421 rewriter, forOp.getLoc(), types);
1422
1423 auto directions = addOp.portDirections();
1424 // For an add operation, we expect two input ports and one output port.
1425 SmallVector<Value, 2> opInputPorts;
1426 Value opOutputPort;
1427 for (auto dir : enumerate(directions)) {
1428 switch (dir.value()) {
1430 opInputPorts.push_back(addOp.getResult(dir.index()));
1431 break;
1432 }
1434 opOutputPort = addOp.getResult(dir.index());
1435 break;
1436 }
1437 }
1438 }
1439
1440 // "Latch Group" increments inductionReg by forLoop's step value.
1441 calyx::ComponentOp componentOp =
1442 getState<ComponentLoweringState>().getComponentOp();
1443 SmallVector<StringRef, 4> groupIdentifier = {
1444 "incr", getState<ComponentLoweringState>().getUniqueName(forOp),
1445 "induction", "var"};
1446 auto groupOp = calyx::createGroup<calyx::GroupOp>(
1447 rewriter, componentOp, forOp.getLoc(),
1448 llvm::join(groupIdentifier, "_"));
1449 rewriter.setInsertionPointToEnd(groupOp.getBodyBlock());
1450
1451 // Assign inductionReg.out to the left port of the adder.
1452 Value leftOp = opInputPorts.front();
1453 calyx::AssignOp::create(rewriter, forOp.getLoc(), leftOp,
1454 inductionReg.getOut());
1455 // Assign forOp.getConstantStep to the right port of the adder.
1456 Value rightOp = opInputPorts.back();
1457 calyx::AssignOp::create(
1458 rewriter, forOp.getLoc(), rightOp,
1459 createConstant(forOp->getLoc(), rewriter, componentOp,
1460 regWidth.getIntOrFloatBitWidth(),
1461 forOp.getConstantStep().value().getSExtValue()));
1462 // Assign adder's output port to inductionReg.
1463 buildAssignmentsForRegisterWrite(rewriter, groupOp, componentOp,
1464 inductionReg, opOutputPort);
1465 // Set group as For Loop's "latch" group.
1466 getState<ComponentLoweringState>().setForLoopLatchGroup(forOpInterface,
1467 groupOp);
1468 getState<ComponentLoweringState>().registerEvaluatingGroup(opOutputPort,
1469 groupOp);
1470 return success();
1471 }
1472 if (auto ifOp = dyn_cast<scf::IfOp>(yieldOp->getParentOp()))
1473 // Empty yield inside ifOp, essentially a no-op.
1474 return success();
1475 if (auto executeRegionOp =
1476 dyn_cast<scf::ExecuteRegionOp>(yieldOp->getParentOp()))
1477 // Empty yield inside an `ExecuteRegionOp` acts as the terminator op.
1478 return success();
1479 return yieldOp.getOperation()->emitError()
1480 << "Unsupported empty yieldOp outside ForOp or IfOp.";
1481 }
1482 // If yieldOp for a for loop is not empty, then we do not transform for loop.
1483 if (dyn_cast<scf::ForOp>(yieldOp->getParentOp())) {
1484 return yieldOp.getOperation()->emitError()
1485 << "Currently do not support non-empty yield operations inside for "
1486 "loops. Run --scf-for-to-while before running --scf-to-calyx.";
1487 }
1488
1489 if (auto whileOp = dyn_cast<scf::WhileOp>(yieldOp->getParentOp())) {
1490 ScfWhileOp whileOpInterface(whileOp);
1491
1492 auto assignGroup =
1493 getState<ComponentLoweringState>().buildWhileLoopIterArgAssignments(
1494 rewriter, whileOpInterface,
1495 getState<ComponentLoweringState>().getComponentOp(),
1496 getState<ComponentLoweringState>().getUniqueName(whileOp) +
1497 "_latch",
1498 yieldOp->getOpOperands());
1499 getState<ComponentLoweringState>().setWhileLoopLatchGroup(whileOpInterface,
1500 assignGroup);
1501 return success();
1502 }
1503
1504 if (auto ifOp = dyn_cast<scf::IfOp>(yieldOp->getParentOp())) {
1505 auto resultRegs = getState<ComponentLoweringState>().getResultRegs(ifOp);
1506
1507 if (yieldOp->getParentRegion() == &ifOp.getThenRegion()) {
1508 auto thenGroup = getState<ComponentLoweringState>().getThenGroup(ifOp);
1509 for (auto op : enumerate(yieldOp.getOperands())) {
1510 auto resultReg =
1511 getState<ComponentLoweringState>().getResultRegs(ifOp, op.index());
1512 buildAssignmentsForRegisterWrite(
1513 rewriter, thenGroup,
1514 getState<ComponentLoweringState>().getComponentOp(), resultReg,
1515 op.value());
1516 getState<ComponentLoweringState>().registerEvaluatingGroup(
1517 ifOp.getResult(op.index()), thenGroup);
1518 }
1519 }
1520
1521 if (!ifOp.getElseRegion().empty() &&
1522 (yieldOp->getParentRegion() == &ifOp.getElseRegion())) {
1523 auto elseGroup = getState<ComponentLoweringState>().getElseGroup(ifOp);
1524 for (auto op : enumerate(yieldOp.getOperands())) {
1525 auto resultReg =
1526 getState<ComponentLoweringState>().getResultRegs(ifOp, op.index());
1527 buildAssignmentsForRegisterWrite(
1528 rewriter, elseGroup,
1529 getState<ComponentLoweringState>().getComponentOp(), resultReg,
1530 op.value());
1531 getState<ComponentLoweringState>().registerEvaluatingGroup(
1532 ifOp.getResult(op.index()), elseGroup);
1533 }
1534 }
1535 }
1536 return success();
1537}
1538
1539LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1540 BranchOpInterface brOp) const {
1541 /// Branch argument passing group creation
1542 /// Branch operands are passed through registers. In BuildBasicBlockRegs we
1543 /// created registers for all branch arguments of each block. We now
1544 /// create groups for assigning values to these registers.
1545 Block *srcBlock = brOp->getBlock();
1546 for (auto succBlock : enumerate(brOp->getSuccessors())) {
1547 auto succOperands = brOp.getSuccessorOperands(succBlock.index());
1548 if (succOperands.empty())
1549 continue;
1550 // Create operand passing group
1551 std::string groupName = loweringState().blockName(srcBlock) + "_to_" +
1552 loweringState().blockName(succBlock.value());
1553 auto groupOp = calyx::createGroup<calyx::GroupOp>(rewriter, getComponent(),
1554 brOp.getLoc(), groupName);
1555 // Fetch block argument registers associated with the basic block
1556 auto dstBlockArgRegs =
1557 getState<ComponentLoweringState>().getBlockArgRegs(succBlock.value());
1558 // Create register assignment for each block argument
1559 for (auto arg : enumerate(succOperands.getForwardedOperands())) {
1560 auto reg = dstBlockArgRegs[arg.index()];
1562 rewriter, groupOp,
1563 getState<ComponentLoweringState>().getComponentOp(), reg,
1564 arg.value());
1565 }
1566 /// Register the group as a block argument group, to be executed
1567 /// when entering the successor block from this block (srcBlock).
1568 getState<ComponentLoweringState>().addBlockArgGroup(
1569 srcBlock, succBlock.value(), groupOp);
1570 }
1571 return success();
1572}
1573
1574/// For each return statement, we create a new group for assigning to the
1575/// previously created return value registers.
1576LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1577 ReturnOp retOp) const {
1578 if (retOp.getNumOperands() == 0)
1579 return success();
1580
1581 std::string groupName =
1582 getState<ComponentLoweringState>().getUniqueName("ret_assign");
1583 auto groupOp = calyx::createGroup<calyx::GroupOp>(rewriter, getComponent(),
1584 retOp.getLoc(), groupName);
1585 for (auto op : enumerate(retOp.getOperands())) {
1586 auto reg = getState<ComponentLoweringState>().getReturnReg(op.index());
1588 rewriter, groupOp, getState<ComponentLoweringState>().getComponentOp(),
1589 reg, op.value());
1590 }
1591 /// Schedule group for execution for when executing the return op block.
1592 getState<ComponentLoweringState>().addBlockScheduleable(retOp->getBlock(),
1593 groupOp);
1594 return success();
1595}
1596
1597LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1598 arith::ConstantOp constOp) const {
1599 if (isa<IntegerType>(constOp.getType())) {
1600 /// Move constant operations to the compOp body as hw::ConstantOp's.
1601 APInt value;
1602 calyx::matchConstantOp(constOp, value);
1603 auto hwConstOp =
1604 rewriter.replaceOpWithNewOp<hw::ConstantOp>(constOp, value);
1605 hwConstOp->moveAfter(getComponent().getBodyBlock(),
1606 getComponent().getBodyBlock()->begin());
1607 } else {
1608 std::string name = getState<ComponentLoweringState>().getUniqueName("cst");
1609 auto floatAttr = cast<FloatAttr>(constOp.getValueAttr());
1610 auto intType =
1611 rewriter.getIntegerType(floatAttr.getType().getIntOrFloatBitWidth());
1612 auto calyxConstOp = calyx::ConstantOp::create(rewriter, constOp.getLoc(),
1613 name, floatAttr, intType);
1614 calyxConstOp->moveAfter(getComponent().getBodyBlock(),
1615 getComponent().getBodyBlock()->begin());
1616 rewriter.replaceAllUsesWith(constOp, calyxConstOp.getOut());
1617 }
1618
1619 return success();
1620}
1621
1622LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1623 AddIOp op) const {
1624 return buildLibraryOp<calyx::CombGroupOp, calyx::AddLibOp>(rewriter, op);
1625}
1626LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1627 SubIOp op) const {
1628 return buildLibraryOp<calyx::CombGroupOp, calyx::SubLibOp>(rewriter, op);
1629}
1630LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1631 ShRUIOp op) const {
1632 return buildLibraryOp<calyx::CombGroupOp, calyx::RshLibOp>(rewriter, op);
1633}
1634LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1635 ShRSIOp op) const {
1636 return buildLibraryOp<calyx::CombGroupOp, calyx::SrshLibOp>(rewriter, op);
1637}
1638LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1639 ShLIOp op) const {
1640 return buildLibraryOp<calyx::CombGroupOp, calyx::LshLibOp>(rewriter, op);
1641}
1642LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1643 AndIOp op) const {
1644 return buildLibraryOp<calyx::CombGroupOp, calyx::AndLibOp>(rewriter, op);
1645}
1646LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1647 OrIOp op) const {
1648 return buildLibraryOp<calyx::CombGroupOp, calyx::OrLibOp>(rewriter, op);
1649}
1650LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1651 XOrIOp op) const {
1652 return buildLibraryOp<calyx::CombGroupOp, calyx::XorLibOp>(rewriter, op);
1653}
1654LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1655 SelectOp op) const {
1656 return buildLibraryOp<calyx::CombGroupOp, calyx::MuxLibOp>(rewriter, op);
1657}
1658
1659LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1660 CmpIOp op) const {
1661 switch (op.getPredicate()) {
1662 case CmpIPredicate::eq:
1663 return buildCmpIOpHelper<calyx::EqLibOp>(rewriter, op);
1664 case CmpIPredicate::ne:
1665 return buildCmpIOpHelper<calyx::NeqLibOp>(rewriter, op);
1666 case CmpIPredicate::uge:
1667 return buildCmpIOpHelper<calyx::GeLibOp>(rewriter, op);
1668 case CmpIPredicate::ult:
1669 return buildCmpIOpHelper<calyx::LtLibOp>(rewriter, op);
1670 case CmpIPredicate::ugt:
1671 return buildCmpIOpHelper<calyx::GtLibOp>(rewriter, op);
1672 case CmpIPredicate::ule:
1673 return buildCmpIOpHelper<calyx::LeLibOp>(rewriter, op);
1674 case CmpIPredicate::sge:
1675 return buildCmpIOpHelper<calyx::SgeLibOp>(rewriter, op);
1676 case CmpIPredicate::slt:
1677 return buildCmpIOpHelper<calyx::SltLibOp>(rewriter, op);
1678 case CmpIPredicate::sgt:
1679 return buildCmpIOpHelper<calyx::SgtLibOp>(rewriter, op);
1680 case CmpIPredicate::sle:
1681 return buildCmpIOpHelper<calyx::SleLibOp>(rewriter, op);
1682 }
1683 llvm_unreachable("unsupported comparison predicate");
1684}
1685
1686LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1687 TruncIOp op) const {
1688 return buildLibraryOp<calyx::CombGroupOp, calyx::SliceLibOp>(
1689 rewriter, op, {op.getOperand().getType()}, {op.getType()});
1690}
1691LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1692 ExtUIOp op) const {
1693 return buildLibraryOp<calyx::CombGroupOp, calyx::PadLibOp>(
1694 rewriter, op, {op.getOperand().getType()}, {op.getType()});
1695}
1696
1697LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1698 ExtSIOp op) const {
1699 return buildLibraryOp<calyx::CombGroupOp, calyx::ExtSILibOp>(
1700 rewriter, op, {op.getOperand().getType()}, {op.getType()});
1701}
1702
1703LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1704 IndexCastOp op) const {
1705 Type sourceType = calyx::normalizeType(rewriter, op.getOperand().getType());
1706 Type targetType = calyx::normalizeType(rewriter, op.getResult().getType());
1707 unsigned targetBits = targetType.getIntOrFloatBitWidth();
1708 unsigned sourceBits = sourceType.getIntOrFloatBitWidth();
1709 LogicalResult res = success();
1710
1711 if (targetBits == sourceBits) {
1712 /// Drop the index cast and replace uses of the target value with the source
1713 /// value.
1714 op.getResult().replaceAllUsesWith(op.getOperand());
1715 } else {
1716 /// pad/slice the source operand.
1717 if (sourceBits > targetBits)
1718 res = buildLibraryOp<calyx::CombGroupOp, calyx::SliceLibOp>(
1719 rewriter, op, {sourceType}, {targetType});
1720 else
1721 res = buildLibraryOp<calyx::CombGroupOp, calyx::PadLibOp>(
1722 rewriter, op, {sourceType}, {targetType});
1723 }
1724 rewriter.eraseOp(op);
1725 return res;
1726}
1727
1728// The Calyx language treats values as bit vectors, i.e., there is no type
1729// system, so this is essentially a no-op.
1730LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1731 BitcastOp op) const {
1732 rewriter.replaceAllUsesWith(op.getOut(), op.getIn());
1733 return success();
1734}
1735
1736LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1737 scf::WhileOp whileOp) const {
1738 // Only need to add the whileOp to the BlockSchedulables scheduler interface.
1739 // Everything else was handled in the `BuildWhileGroups` pattern.
1740 ScfWhileOp scfWhileOp(whileOp);
1741 getState<ComponentLoweringState>().addBlockScheduleable(
1742 whileOp.getOperation()->getBlock(), WhileScheduleable{scfWhileOp});
1743 return success();
1744}
1745
1746LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1747 scf::ForOp forOp) const {
1748 // Only need to add the forOp to the BlockSchedulables scheduler interface.
1749 // Everything else was handled in the `BuildForGroups` pattern.
1750 ScfForOp scfForOp(forOp);
1751 // If we cannot compute the trip count of the for loop, then we should
1752 // emit an error saying to use --scf-for-to-while
1753 std::optional<uint64_t> bound = scfForOp.getBound();
1754 if (!bound.has_value()) {
1755 return scfForOp.getOperation()->emitError()
1756 << "Loop bound not statically known. Should "
1757 "transform into while loop using `--scf-for-to-while` before "
1758 "running --lower-scf-to-calyx.";
1759 }
1760 getState<ComponentLoweringState>().addBlockScheduleable(
1761 forOp.getOperation()->getBlock(), ForScheduleable{
1762 scfForOp,
1763 bound.value(),
1764 });
1765 return success();
1766}
1767
1768LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1769 scf::IfOp ifOp) const {
1770 getState<ComponentLoweringState>().addBlockScheduleable(
1771 ifOp.getOperation()->getBlock(), IfScheduleable{ifOp});
1772 return success();
1773}
1774
1775LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1776 scf::ReduceOp reduceOp) const {
1777 // we don't handle reduce operation and simply return success for now since
1778 // BuildParGroups would have already emitted an error and exited early
1779 // if a reduce operation was encountered.
1780 return success();
1781}
1782
1783LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1784 scf::ParallelOp parOp) const {
1785 if (!parOp->hasAttr(unrolledParallelAttr)) {
1786 parOp.emitError(
1787 "AffineParallelUnroll must be run in order to lower scf.parallel");
1788 return failure();
1789 }
1790 getState<ComponentLoweringState>().addBlockScheduleable(
1791 parOp.getOperation()->getBlock(), ParScheduleable{parOp});
1792 return success();
1793}
1794
1795LogicalResult
1796BuildOpGroups::buildOp(PatternRewriter &rewriter,
1797 scf::ExecuteRegionOp executeRegionOp) const {
1798 // Simply return success because the only remaining `scf.execute_region` op
1799 // are generated by the `BuildParGroups` pass - the rest of them are inlined
1800 // by the `InlineExecuteRegionOpPattern`.
1801 return success();
1802}
1803
1804LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
1805 CallOp callOp) const {
1806 std::string instanceName = calyx::getInstanceName(callOp);
1807 calyx::InstanceOp instanceOp =
1808 getState<ComponentLoweringState>().getInstance(instanceName);
1809 SmallVector<Value, 4> outputPorts;
1810 auto portInfos = instanceOp.getReferencedComponent().getPortInfo();
1811 for (auto [idx, portInfo] : enumerate(portInfos)) {
1812 if (portInfo.direction == calyx::Direction::Output)
1813 outputPorts.push_back(instanceOp.getResult(idx));
1814 }
1815
1816 // Replacing a CallOp results in the out port of the instance.
1817 for (auto [idx, result] : llvm::enumerate(callOp.getResults()))
1818 rewriter.replaceAllUsesWith(result, outputPorts[idx]);
1819
1820 // CallScheduleanle requires an instance, while CallOp can be used to get the
1821 // input ports.
1822 getState<ComponentLoweringState>().addBlockScheduleable(
1823 callOp.getOperation()->getBlock(), CallScheduleable{instanceOp, callOp});
1824 return success();
1825}
1826
1827/// Inlines Calyx ExecuteRegionOp operations within their parent blocks.
1828/// An execution region op (ERO) is inlined by:
1829/// i : add a sink basic block for all yield operations inside the
1830/// ERO to jump to
1831/// ii : Rewrite scf.yield calls inside the ERO to branch to the sink block
1832/// iii: inline the ERO region
1833/// TODO(#1850) evaluate the usefulness of this lowering pattern.
1835 : public OpRewritePattern<scf::ExecuteRegionOp> {
1836 using OpRewritePattern::OpRewritePattern;
1837
1838 LogicalResult matchAndRewrite(scf::ExecuteRegionOp execOp,
1839 PatternRewriter &rewriter) const override {
1840 if (auto parOp = dyn_cast_or_null<scf::ParallelOp>(execOp->getParentOp())) {
1841 if (auto boolAttr = dyn_cast_or_null<mlir::BoolAttr>(
1842 parOp->getAttr(unrolledParallelAttr)))
1843 // If the `ExecuteRegionOp` was inserted when running the
1844 // `AffineParallelUnrollPass` (indicated by having `calyx.unroll`
1845 // attribute), we should skip inline.
1846 return success();
1847 }
1848 /// Determine type of "yield" operations inside the ERO.
1849 TypeRange yieldTypes = execOp.getResultTypes();
1850
1851 /// Create sink basic block and rewrite uses of yield results to sink block
1852 /// arguments.
1853 rewriter.setInsertionPointAfter(execOp);
1854 auto *sinkBlock = rewriter.splitBlock(
1855 execOp->getBlock(),
1856 execOp.getOperation()->getIterator()->getNextNode()->getIterator());
1857 sinkBlock->addArguments(
1858 yieldTypes,
1859 SmallVector<Location, 4>(yieldTypes.size(), rewriter.getUnknownLoc()));
1860 for (auto res : enumerate(execOp.getResults()))
1861 res.value().replaceAllUsesWith(sinkBlock->getArgument(res.index()));
1862
1863 /// Rewrite yield calls as branches.
1864 for (auto yieldOp :
1865 make_early_inc_range(execOp.getRegion().getOps<scf::YieldOp>())) {
1866 rewriter.setInsertionPointAfter(yieldOp);
1867 rewriter.replaceOpWithNewOp<BranchOp>(yieldOp, sinkBlock,
1868 yieldOp.getOperands());
1869 }
1870
1871 /// Inline the regionOp.
1872 auto *preBlock = execOp->getBlock();
1873 auto *execOpEntryBlock = &execOp.getRegion().front();
1874 auto *postBlock = execOp->getBlock()->splitBlock(execOp);
1875 rewriter.inlineRegionBefore(execOp.getRegion(), postBlock);
1876 rewriter.mergeBlocks(postBlock, preBlock);
1877 rewriter.eraseOp(execOp);
1878
1879 /// Finally, erase the unused entry block of the execOp region.
1880 rewriter.mergeBlocks(execOpEntryBlock, preBlock);
1881
1882 return success();
1883 }
1884};
1885
1886/// Creates a new Calyx component for each FuncOp in the program.
1888 using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;
1889
1890 LogicalResult
1892 PatternRewriter &rewriter) const override {
1893 /// Maintain a mapping between funcOp input arguments and the port index
1894 /// which the argument will eventually map to.
1895 DenseMap<Value, unsigned> funcOpArgRewrites;
1896
1897 /// Maintain a mapping between funcOp output indexes and the component
1898 /// output port index which the return value will eventually map to.
1899 DenseMap<unsigned, unsigned> funcOpResultMapping;
1900
1901 /// Maintain a mapping between an external memory argument (identified by a
1902 /// memref) and eventual component input- and output port indices that will
1903 /// map to the memory ports. The pair denotes the start index of the memory
1904 /// ports in the in- and output ports of the component. Ports are expected
1905 /// to be ordered in the same manner as they are added by
1906 /// calyx::appendPortsForExternalMemref.
1907 DenseMap<Value, std::pair<unsigned, unsigned>> extMemoryCompPortIndices;
1908
1909 /// Create I/O ports. Maintain separate in/out port vectors to determine
1910 /// which port index each function argument will eventually map to.
1911 SmallVector<calyx::PortInfo> inPorts, outPorts;
1912 FunctionType funcType = funcOp.getFunctionType();
1913 for (auto arg : enumerate(funcOp.getArguments())) {
1914 if (!isa<MemRefType>(arg.value().getType())) {
1915 /// Single-port arguments
1916 std::string inName;
1917 if (auto portNameAttr = funcOp.getArgAttrOfType<StringAttr>(
1918 arg.index(), scfToCalyx::sPortNameAttr))
1919 inName = portNameAttr.str();
1920 else
1921 inName = "in" + std::to_string(arg.index());
1922 funcOpArgRewrites[arg.value()] = inPorts.size();
1923 inPorts.push_back(calyx::PortInfo{
1924 rewriter.getStringAttr(inName),
1925 calyx::normalizeType(rewriter, arg.value().getType()),
1927 DictionaryAttr::get(rewriter.getContext(), {})});
1928 }
1929 }
1930 for (auto res : enumerate(funcType.getResults())) {
1931 std::string resName;
1932 if (auto portNameAttr = funcOp.getResultAttrOfType<StringAttr>(
1933 res.index(), scfToCalyx::sPortNameAttr))
1934 resName = portNameAttr.str();
1935 else
1936 resName = "out" + std::to_string(res.index());
1937 funcOpResultMapping[res.index()] = outPorts.size();
1938
1939 outPorts.push_back(calyx::PortInfo{
1940 rewriter.getStringAttr(resName),
1941 calyx::normalizeType(rewriter, res.value()), calyx::Direction::Output,
1942 DictionaryAttr::get(rewriter.getContext(), {})});
1943 }
1944
1945 /// We've now recorded all necessary indices. Merge in- and output ports
1946 /// and add the required mandatory component ports.
1947 auto ports = inPorts;
1948 llvm::append_range(ports, outPorts);
1949 calyx::addMandatoryComponentPorts(rewriter, ports);
1950
1951 /// Create a calyx::ComponentOp corresponding to the to-be-lowered function.
1952 auto compOp = calyx::ComponentOp::create(
1953 rewriter, funcOp.getLoc(), rewriter.getStringAttr(funcOp.getSymName()),
1954 ports);
1955
1956 std::string funcName = "func_" + funcOp.getSymName().str();
1957 rewriter.modifyOpInPlace(funcOp, [&]() { funcOp.setSymName(funcName); });
1958
1959 /// Mark this component as the toplevel if it's the top-level function of
1960 /// the module.
1961 if (compOp.getName() == loweringState().getTopLevelFunction())
1962 compOp->setAttr("toplevel", rewriter.getUnitAttr());
1963
1964 /// Store the function-to-component mapping.
1965 functionMapping[funcOp] = compOp;
1966 auto *compState = loweringState().getState<ComponentLoweringState>(compOp);
1967 compState->setFuncOpResultMapping(funcOpResultMapping);
1968
1969 unsigned extMemCounter = 0;
1970 for (auto arg : enumerate(funcOp.getArguments())) {
1971 if (isa<MemRefType>(arg.value().getType())) {
1972 std::string memName =
1973 llvm::join_items("_", "arg_mem", std::to_string(extMemCounter++));
1974
1975 rewriter.setInsertionPointToStart(compOp.getBodyBlock());
1976 MemRefType memtype = cast<MemRefType>(arg.value().getType());
1977 SmallVector<int64_t> addrSizes;
1978 SmallVector<int64_t> sizes;
1979 for (int64_t dim : memtype.getShape()) {
1980 sizes.push_back(dim);
1981 addrSizes.push_back(calyx::handleZeroWidth(dim));
1982 }
1983 if (sizes.empty() && addrSizes.empty()) {
1984 sizes.push_back(1);
1985 addrSizes.push_back(1);
1986 }
1987 auto memOp = calyx::SeqMemoryOp::create(
1988 rewriter, funcOp.getLoc(), memName,
1989 memtype.getElementType().getIntOrFloatBitWidth(), sizes, addrSizes);
1990 // we don't set the memory to "external", which implies it's a reference
1991
1992 compState->registerMemoryInterface(arg.value(),
1993 calyx::MemoryInterface(memOp));
1994 }
1995 }
1996
1997 /// Rewrite funcOp SSA argument values to the CompOp arguments.
1998 for (auto &mapping : funcOpArgRewrites)
1999 mapping.getFirst().replaceAllUsesWith(
2000 compOp.getArgument(mapping.getSecond()));
2001
2002 return success();
2003 }
2004};
2005
2006/// In BuildWhileGroups, a register is created for each iteration argumenet of
2007/// the while op. These registers are then written to on the while op
2008/// terminating yield operation alongside before executing the whileOp in the
2009/// schedule, to set the initial values of the argument registers.
2011 using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;
2012
2013 LogicalResult
2015 PatternRewriter &rewriter) const override {
2016 LogicalResult res = success();
2017 funcOp.walk([&](Operation *op) {
2018 // Only work on ops that support the ScfWhileOp.
2019 if (!isa<scf::WhileOp>(op))
2020 return WalkResult::advance();
2021
2022 auto scfWhileOp = cast<scf::WhileOp>(op);
2023 ScfWhileOp whileOp(scfWhileOp);
2024
2025 getState<ComponentLoweringState>().setUniqueName(whileOp.getOperation(),
2026 "while");
2027
2028 /// Check for do-while loops.
2029 /// TODO(mortbopet) can we support these? for now, do not support loops
2030 /// where iterargs are changed in the 'before' region. scf.WhileOp also
2031 /// has support for different types of iter_args and return args which we
2032 /// also do not support; iter_args and while return values are placed in
2033 /// the same registers.
2034 for (auto barg :
2035 enumerate(scfWhileOp.getBefore().front().getArguments())) {
2036 auto condOp = scfWhileOp.getConditionOp().getArgs()[barg.index()];
2037 if (barg.value() != condOp) {
2038 res = whileOp.getOperation()->emitError()
2039 << loweringState().irName(barg.value())
2040 << " != " << loweringState().irName(condOp)
2041 << "do-while loops not supported; expected iter-args to "
2042 "remain untransformed in the 'before' region of the "
2043 "scf.while op.";
2044 return WalkResult::interrupt();
2045 }
2046 }
2047
2048 /// Create iteration argument registers.
2049 /// The iteration argument registers will be referenced:
2050 /// - In the "before" part of the while loop, calculating the conditional,
2051 /// - In the "after" part of the while loop,
2052 /// - Outside the while loop, rewriting the while loop return values.
2053 for (auto arg : enumerate(whileOp.getBodyArgs())) {
2054 std::string name = getState<ComponentLoweringState>()
2055 .getUniqueName(whileOp.getOperation())
2056 .str() +
2057 "_arg" + std::to_string(arg.index());
2058 auto reg =
2059 createRegister(arg.value().getLoc(), rewriter, getComponent(),
2060 arg.value().getType().getIntOrFloatBitWidth(), name);
2061 getState<ComponentLoweringState>().addWhileLoopIterReg(whileOp, reg,
2062 arg.index());
2063 arg.value().replaceAllUsesWith(reg.getOut());
2064
2065 /// Also replace uses in the "before" region of the while loop
2066 whileOp.getConditionBlock()
2067 ->getArgument(arg.index())
2068 .replaceAllUsesWith(reg.getOut());
2069 }
2070
2071 /// Create iter args initial value assignment group(s), one per register.
2072 SmallVector<calyx::GroupOp> initGroups;
2073 auto numOperands = whileOp.getOperation()->getNumOperands();
2074 for (size_t i = 0; i < numOperands; ++i) {
2075 auto initGroupOp =
2076 getState<ComponentLoweringState>().buildWhileLoopIterArgAssignments(
2077 rewriter, whileOp,
2078 getState<ComponentLoweringState>().getComponentOp(),
2079 getState<ComponentLoweringState>().getUniqueName(
2080 whileOp.getOperation()) +
2081 "_init_" + std::to_string(i),
2082 whileOp.getOperation()->getOpOperand(i));
2083 initGroups.push_back(initGroupOp);
2084 }
2085
2086 getState<ComponentLoweringState>().setWhileLoopInitGroups(whileOp,
2087 initGroups);
2088
2089 return WalkResult::advance();
2090 });
2091 return res;
2092 }
2093};
2094
2095/// In BuildForGroups, a register is created for the iteration argument of
2096/// the for op. This register is then initialized to the lowerBound of the for
2097/// loop in a group that executes the for loop.
2099 using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;
2100
2101 LogicalResult
2103 PatternRewriter &rewriter) const override {
2104 LogicalResult res = success();
2105 funcOp.walk([&](Operation *op) {
2106 // Only work on ops that support the ScfForOp.
2107 if (!isa<scf::ForOp>(op))
2108 return WalkResult::advance();
2109
2110 auto scfForOp = cast<scf::ForOp>(op);
2111 ScfForOp forOp(scfForOp);
2112
2113 getState<ComponentLoweringState>().setUniqueName(forOp.getOperation(),
2114 "for");
2115
2116 // Create a register for the InductionVar, and set that Register as the
2117 // only IterReg for the For Loop
2118 auto inductionVar = forOp.getOperation().getInductionVar();
2119 SmallVector<std::string, 3> inductionVarIdentifiers = {
2120 getState<ComponentLoweringState>()
2121 .getUniqueName(forOp.getOperation())
2122 .str(),
2123 "induction", "var"};
2124 std::string name = llvm::join(inductionVarIdentifiers, "_");
2125 auto reg =
2126 createRegister(inductionVar.getLoc(), rewriter, getComponent(),
2127 inductionVar.getType().getIntOrFloatBitWidth(), name);
2128 getState<ComponentLoweringState>().addForLoopIterReg(forOp, reg, 0);
2129 inductionVar.replaceAllUsesWith(reg.getOut());
2130
2131 // Create InitGroup that sets the InductionVar to LowerBound
2132 calyx::ComponentOp componentOp =
2133 getState<ComponentLoweringState>().getComponentOp();
2134 SmallVector<calyx::GroupOp> initGroups;
2135 SmallVector<std::string, 4> groupIdentifiers = {
2136 "init",
2137 getState<ComponentLoweringState>()
2138 .getUniqueName(forOp.getOperation())
2139 .str(),
2140 "induction", "var"};
2141 std::string groupName = llvm::join(groupIdentifiers, "_");
2142 auto groupOp = calyx::createGroup<calyx::GroupOp>(
2143 rewriter, componentOp, forOp.getLoc(), groupName);
2144 buildAssignmentsForRegisterWrite(rewriter, groupOp, componentOp, reg,
2145 forOp.getOperation().getLowerBound());
2146 initGroups.push_back(groupOp);
2147 getState<ComponentLoweringState>().setForLoopInitGroups(forOp,
2148 initGroups);
2149
2150 return WalkResult::advance();
2151 });
2152 return res;
2153 }
2154};
2155
2157 using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;
2158
2159 LogicalResult
2161 PatternRewriter &rewriter) const override {
2162 LogicalResult res = success();
2163 funcOp.walk([&](Operation *op) {
2164 if (!isa<scf::IfOp>(op))
2165 return WalkResult::advance();
2166
2167 auto scfIfOp = cast<scf::IfOp>(op);
2168
2169 // There is no need to build `thenGroup` and `elseGroup` if `scfIfOp`
2170 // doesn't yield any result since these groups are created for managing
2171 // the result values.
2172 if (scfIfOp.getResults().empty())
2173 return WalkResult::advance();
2174
2175 calyx::ComponentOp componentOp =
2176 getState<ComponentLoweringState>().getComponentOp();
2177
2178 std::string thenGroupName =
2179 getState<ComponentLoweringState>().getUniqueName("then_br");
2180 auto thenGroupOp = calyx::createGroup<calyx::GroupOp>(
2181 rewriter, componentOp, scfIfOp.getLoc(), thenGroupName);
2182 getState<ComponentLoweringState>().setThenGroup(scfIfOp, thenGroupOp);
2183
2184 if (!scfIfOp.getElseRegion().empty()) {
2185 std::string elseGroupName =
2186 getState<ComponentLoweringState>().getUniqueName("else_br");
2187 auto elseGroupOp = calyx::createGroup<calyx::GroupOp>(
2188 rewriter, componentOp, scfIfOp.getLoc(), elseGroupName);
2189 getState<ComponentLoweringState>().setElseGroup(scfIfOp, elseGroupOp);
2190 }
2191
2192 for (auto ifOpRes : scfIfOp.getResults()) {
2193 auto reg = createRegister(
2194 scfIfOp.getLoc(), rewriter, getComponent(),
2195 ifOpRes.getType().getIntOrFloatBitWidth(),
2196 getState<ComponentLoweringState>().getUniqueName("if_res"));
2197 getState<ComponentLoweringState>().setResultRegs(
2198 scfIfOp, reg, ifOpRes.getResultNumber());
2199 }
2200
2201 return WalkResult::advance();
2202 });
2203 return res;
2204 }
2205};
2206
2207/// Builds a control schedule by traversing the CFG of the function and
2208/// associating this with the previously created groups.
2209/// For simplicity, the generated control flow is expanded for all possible
2210/// paths in the input DAG. This elaborated control flow is later reduced in
2211/// the runControlFlowSimplification passes.
2213 using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;
2214
2215 LogicalResult
2217 PatternRewriter &rewriter) const override {
2218 auto *entryBlock = &funcOp.getBlocks().front();
2219 rewriter.setInsertionPointToStart(
2220 getComponent().getControlOp().getBodyBlock());
2221 auto topLevelSeqOp = calyx::SeqOp::create(rewriter, funcOp.getLoc());
2222 DenseSet<Block *> path;
2223 return buildCFGControl(path, rewriter, topLevelSeqOp.getBodyBlock(),
2224 nullptr, entryBlock);
2225 }
2226
2227private:
2228 /// Sequentially schedules the groups that registered themselves with
2229 /// 'block'.
2230 LogicalResult scheduleBasicBlock(PatternRewriter &rewriter,
2231 const DenseSet<Block *> &path,
2232 mlir::Block *parentCtrlBlock,
2233 mlir::Block *block) const {
2234 auto compBlockScheduleables =
2235 getState<ComponentLoweringState>().getBlockScheduleables(block);
2236 auto loc = block->front().getLoc();
2237
2238 if (compBlockScheduleables.size() > 1 &&
2239 !isa<scf::ParallelOp>(block->getParentOp())) {
2240 auto seqOp = calyx::SeqOp::create(rewriter, loc);
2241 parentCtrlBlock = seqOp.getBodyBlock();
2242 }
2243
2244 for (auto &group : compBlockScheduleables) {
2245 rewriter.setInsertionPointToEnd(parentCtrlBlock);
2246 if (auto groupPtr = std::get_if<calyx::GroupOp>(&group); groupPtr) {
2247 calyx::EnableOp::create(rewriter, groupPtr->getLoc(),
2248 groupPtr->getSymName());
2249 } else if (auto whileSchedPtr = std::get_if<WhileScheduleable>(&group);
2250 whileSchedPtr) {
2251 auto &whileOp = whileSchedPtr->whileOp;
2252
2253 auto whileCtrlOp = buildWhileCtrlOp(
2254 whileOp,
2255 getState<ComponentLoweringState>().getWhileLoopInitGroups(whileOp),
2256 rewriter);
2257 rewriter.setInsertionPointToEnd(whileCtrlOp.getBodyBlock());
2258 auto whileBodyOp =
2259 calyx::SeqOp::create(rewriter, whileOp.getOperation()->getLoc());
2260 auto *whileBodyOpBlock = whileBodyOp.getBodyBlock();
2261
2262 /// Only schedule the 'after' block. The 'before' block is
2263 /// implicitly scheduled when evaluating the while condition.
2264 if (LogicalResult result =
2265 buildCFGControl(path, rewriter, whileBodyOpBlock, block,
2266 whileOp.getBodyBlock());
2267 result.failed())
2268 return result;
2269
2270 // Insert loop-latch at the end of the while group
2271 rewriter.setInsertionPointToEnd(whileBodyOpBlock);
2272 calyx::GroupOp whileLatchGroup =
2273 getState<ComponentLoweringState>().getWhileLoopLatchGroup(whileOp);
2274 calyx::EnableOp::create(rewriter, whileLatchGroup.getLoc(),
2275 whileLatchGroup.getName());
2276 } else if (auto *parSchedPtr = std::get_if<ParScheduleable>(&group)) {
2277 auto parOp = parSchedPtr->parOp;
2278 auto calyxParOp = calyx::ParOp::create(rewriter, parOp.getLoc());
2279
2280 WalkResult walkResult =
2281 parOp.walk([&](scf::ExecuteRegionOp execRegion) {
2282 rewriter.setInsertionPointToEnd(calyxParOp.getBodyBlock());
2283 auto seqOp = calyx::SeqOp::create(rewriter, execRegion.getLoc());
2284 rewriter.setInsertionPointToEnd(seqOp.getBodyBlock());
2285
2286 for (auto &execBlock : execRegion.getRegion().getBlocks()) {
2287 if (LogicalResult res = scheduleBasicBlock(
2288 rewriter, path, seqOp.getBodyBlock(), &execBlock);
2289 res.failed()) {
2290 return WalkResult::interrupt();
2291 }
2292 }
2293 return WalkResult::advance();
2294 });
2295
2296 if (walkResult.wasInterrupted())
2297 return failure();
2298 } else if (auto *forSchedPtr = std::get_if<ForScheduleable>(&group);
2299 forSchedPtr) {
2300 auto forOp = forSchedPtr->forOp;
2301
2302 auto forCtrlOp = buildForCtrlOp(
2303 forOp,
2304 getState<ComponentLoweringState>().getForLoopInitGroups(forOp),
2305 forSchedPtr->bound, rewriter);
2306 rewriter.setInsertionPointToEnd(forCtrlOp.getBodyBlock());
2307 auto forBodyOp =
2308 calyx::SeqOp::create(rewriter, forOp.getOperation()->getLoc());
2309 auto *forBodyOpBlock = forBodyOp.getBodyBlock();
2310
2311 // Schedule the body of the for loop.
2312 if (LogicalResult res = buildCFGControl(path, rewriter, forBodyOpBlock,
2313 block, forOp.getBodyBlock());
2314 res.failed())
2315 return res;
2316
2317 // Insert loop-latch at the end of the while group.
2318 rewriter.setInsertionPointToEnd(forBodyOpBlock);
2319 calyx::GroupOp forLatchGroup =
2320 getState<ComponentLoweringState>().getForLoopLatchGroup(forOp);
2321 calyx::EnableOp::create(rewriter, forLatchGroup.getLoc(),
2322 forLatchGroup.getName());
2323 } else if (auto *ifSchedPtr = std::get_if<IfScheduleable>(&group);
2324 ifSchedPtr) {
2325 auto ifOp = ifSchedPtr->ifOp;
2326
2327 Location loc = ifOp->getLoc();
2328
2329 auto cond = ifOp.getCondition();
2330
2331 FlatSymbolRefAttr symbolAttr = nullptr;
2332 auto condReg = getState<ComponentLoweringState>().getCondReg(ifOp);
2333 if (!condReg) {
2334 auto condGroup = getState<ComponentLoweringState>()
2335 .getEvaluatingGroup<calyx::CombGroupOp>(cond);
2336
2337 symbolAttr = FlatSymbolRefAttr::get(
2338 StringAttr::get(getContext(), condGroup.getSymName()));
2339 }
2340
2341 bool initElse = !ifOp.getElseRegion().empty();
2342 auto ifCtrlOp = calyx::IfOp::create(rewriter, loc, cond, symbolAttr,
2343 /*initializeElseBody=*/initElse);
2344
2345 rewriter.setInsertionPointToEnd(ifCtrlOp.getBodyBlock());
2346
2347 auto thenSeqOp =
2348 calyx::SeqOp::create(rewriter, ifOp.getThenRegion().getLoc());
2349 auto *thenSeqOpBlock = thenSeqOp.getBodyBlock();
2350
2351 auto *thenBlock = &ifOp.getThenRegion().front();
2352 LogicalResult res = buildCFGControl(path, rewriter, thenSeqOpBlock,
2353 /*preBlock=*/block, thenBlock);
2354 if (res.failed())
2355 return res;
2356
2357 // `thenGroup`s won't be created in the first place if there's no
2358 // yielded results for this `ifOp`.
2359 if (!ifOp.getResults().empty()) {
2360 rewriter.setInsertionPointToEnd(thenSeqOpBlock);
2361 calyx::GroupOp thenGroup =
2362 getState<ComponentLoweringState>().getThenGroup(ifOp);
2363 calyx::EnableOp::create(rewriter, thenGroup.getLoc(),
2364 thenGroup.getName());
2365 }
2366
2367 if (!ifOp.getElseRegion().empty()) {
2368 rewriter.setInsertionPointToEnd(ifCtrlOp.getElseBody());
2369
2370 auto elseSeqOp =
2371 calyx::SeqOp::create(rewriter, ifOp.getElseRegion().getLoc());
2372 auto *elseSeqOpBlock = elseSeqOp.getBodyBlock();
2373
2374 auto *elseBlock = &ifOp.getElseRegion().front();
2375 res = buildCFGControl(path, rewriter, elseSeqOpBlock,
2376 /*preBlock=*/block, elseBlock);
2377 if (res.failed())
2378 return res;
2379
2380 if (!ifOp.getResults().empty()) {
2381 rewriter.setInsertionPointToEnd(elseSeqOpBlock);
2382 calyx::GroupOp elseGroup =
2383 getState<ComponentLoweringState>().getElseGroup(ifOp);
2384 calyx::EnableOp::create(rewriter, elseGroup.getLoc(),
2385 elseGroup.getName());
2386 }
2387 }
2388 } else if (auto *callSchedPtr = std::get_if<CallScheduleable>(&group)) {
2389 auto instanceOp = callSchedPtr->instanceOp;
2390 OpBuilder::InsertionGuard g(rewriter);
2391 auto callBody = calyx::SeqOp::create(rewriter, instanceOp.getLoc());
2392 rewriter.setInsertionPointToStart(callBody.getBodyBlock());
2393
2394 auto callee = callSchedPtr->callOp.getCallee();
2395 auto *calleeOp = SymbolTable::lookupNearestSymbolFrom(
2396 callSchedPtr->callOp.getOperation()->getParentOp(),
2397 StringAttr::get(rewriter.getContext(), "func_" + callee.str()));
2398 FuncOp calleeFunc = dyn_cast_or_null<FuncOp>(calleeOp);
2399
2400 auto instanceOpComp =
2401 llvm::cast<calyx::ComponentOp>(instanceOp.getReferencedComponent());
2402 auto *instanceOpLoweringState =
2403 loweringState().getState(instanceOpComp);
2404
2405 SmallVector<Value, 4> instancePorts;
2406 SmallVector<Value, 4> inputPorts;
2407 SmallVector<Attribute, 4> refCells;
2408 for (auto operandEnum : enumerate(callSchedPtr->callOp.getOperands())) {
2409 auto operand = operandEnum.value();
2410 auto index = operandEnum.index();
2411 if (!isa<MemRefType>(operand.getType())) {
2412 inputPorts.push_back(operand);
2413 continue;
2414 }
2415
2416 auto memOpName = getState<ComponentLoweringState>()
2417 .getMemoryInterface(operand)
2418 .memName();
2419 auto memOpNameAttr =
2420 SymbolRefAttr::get(rewriter.getContext(), memOpName);
2421 Value argI = calleeFunc.getArgument(index);
2422 if (isa<MemRefType>(argI.getType())) {
2423 NamedAttrList namedAttrList;
2424 namedAttrList.append(
2425 rewriter.getStringAttr(
2426 instanceOpLoweringState->getMemoryInterface(argI)
2427 .memName()),
2428 memOpNameAttr);
2429 refCells.push_back(
2430 DictionaryAttr::get(rewriter.getContext(), namedAttrList));
2431 }
2432 }
2433 llvm::copy(instanceOp.getResults().take_front(inputPorts.size()),
2434 std::back_inserter(instancePorts));
2435
2436 ArrayAttr refCellsAttr =
2437 ArrayAttr::get(rewriter.getContext(), refCells);
2438
2439 calyx::InvokeOp::create(rewriter, instanceOp.getLoc(),
2440 instanceOp.getSymName(), instancePorts,
2441 inputPorts, refCellsAttr,
2442 ArrayAttr::get(rewriter.getContext(), {}),
2443 ArrayAttr::get(rewriter.getContext(), {}));
2444 } else
2445 llvm_unreachable("Unknown scheduleable");
2446 }
2447 return success();
2448 }
2449
2450 /// Schedules a block by inserting a branch argument assignment block (if any)
2451 /// before recursing into the scheduling of the block innards.
2452 /// Blocks 'from' and 'to' refer to blocks in the source program.
2453 /// parentCtrlBlock refers to the control block wherein control operations are
2454 /// to be inserted.
2455 LogicalResult schedulePath(PatternRewriter &rewriter,
2456 const DenseSet<Block *> &path, Location loc,
2457 Block *from, Block *to,
2458 Block *parentCtrlBlock) const {
2459 /// Schedule any registered block arguments to be executed before the body
2460 /// of the branch.
2461 rewriter.setInsertionPointToEnd(parentCtrlBlock);
2462 auto preSeqOp = calyx::SeqOp::create(rewriter, loc);
2463 rewriter.setInsertionPointToEnd(preSeqOp.getBodyBlock());
2464 for (auto barg :
2465 getState<ComponentLoweringState>().getBlockArgGroups(from, to))
2466 calyx::EnableOp::create(rewriter, barg.getLoc(), barg.getSymName());
2467
2468 return buildCFGControl(path, rewriter, parentCtrlBlock, from, to);
2469 }
2470
2471 LogicalResult buildCFGControl(DenseSet<Block *> path,
2472 PatternRewriter &rewriter,
2473 mlir::Block *parentCtrlBlock,
2474 mlir::Block *preBlock,
2475 mlir::Block *block) const {
2476 if (path.count(block) != 0)
2477 return preBlock->getTerminator()->emitError()
2478 << "CFG backedge detected. Loops must be raised to 'scf.while' or "
2479 "'scf.for' operations.";
2480
2481 rewriter.setInsertionPointToEnd(parentCtrlBlock);
2482 LogicalResult bbSchedResult =
2483 scheduleBasicBlock(rewriter, path, parentCtrlBlock, block);
2484 if (bbSchedResult.failed())
2485 return bbSchedResult;
2486
2487 path.insert(block);
2488 auto successors = block->getSuccessors();
2489 auto nSuccessors = successors.size();
2490 if (nSuccessors > 0) {
2491 auto brOp = dyn_cast<BranchOpInterface>(block->getTerminator());
2492 assert(brOp);
2493 if (nSuccessors > 1) {
2494 /// TODO(mortbopet): we could choose to support ie. std.switch, but it
2495 /// would probably be easier to just require it to be lowered
2496 /// beforehand.
2497 assert(nSuccessors == 2 &&
2498 "only conditional branches supported for now...");
2499 /// Wrap each branch inside an if/else.
2500 auto cond = brOp->getOperand(0);
2501 auto condGroup = getState<ComponentLoweringState>()
2502 .getEvaluatingGroup<calyx::CombGroupOp>(cond);
2503 auto symbolAttr = FlatSymbolRefAttr::get(
2504 StringAttr::get(getContext(), condGroup.getSymName()));
2505
2506 auto ifOp =
2507 calyx::IfOp::create(rewriter, brOp->getLoc(), cond, symbolAttr,
2508 /*initializeElseBody=*/true);
2509 rewriter.setInsertionPointToStart(ifOp.getThenBody());
2510 auto thenSeqOp = calyx::SeqOp::create(rewriter, brOp.getLoc());
2511 rewriter.setInsertionPointToStart(ifOp.getElseBody());
2512 auto elseSeqOp = calyx::SeqOp::create(rewriter, brOp.getLoc());
2513
2514 bool trueBrSchedSuccess =
2515 schedulePath(rewriter, path, brOp.getLoc(), block, successors[0],
2516 thenSeqOp.getBodyBlock())
2517 .succeeded();
2518 bool falseBrSchedSuccess = true;
2519 if (trueBrSchedSuccess) {
2520 falseBrSchedSuccess =
2521 schedulePath(rewriter, path, brOp.getLoc(), block, successors[1],
2522 elseSeqOp.getBodyBlock())
2523 .succeeded();
2524 }
2525
2526 return success(trueBrSchedSuccess && falseBrSchedSuccess);
2527 } else {
2528 /// Schedule sequentially within the current parent control block.
2529 return schedulePath(rewriter, path, brOp.getLoc(), block,
2530 successors.front(), parentCtrlBlock);
2531 }
2532 }
2533 return success();
2534 }
2535
2536 // Insert a Par of initGroups at Location loc. Used as helper for
2537 // `buildWhileCtrlOp` and `buildForCtrlOp`.
2538 void
2539 insertParInitGroups(PatternRewriter &rewriter, Location loc,
2540 const SmallVector<calyx::GroupOp> &initGroups) const {
2541 PatternRewriter::InsertionGuard g(rewriter);
2542 auto parOp = calyx::ParOp::create(rewriter, loc);
2543 rewriter.setInsertionPointToStart(parOp.getBodyBlock());
2544 for (calyx::GroupOp group : initGroups)
2545 calyx::EnableOp::create(rewriter, group.getLoc(), group.getName());
2546 }
2547
2548 calyx::WhileOp buildWhileCtrlOp(ScfWhileOp whileOp,
2549 SmallVector<calyx::GroupOp> initGroups,
2550 PatternRewriter &rewriter) const {
2551 Location loc = whileOp.getLoc();
2552 /// Insert while iter arg initialization group(s). Emit a
2553 /// parallel group to assign one or more registers all at once.
2554 insertParInitGroups(rewriter, loc, initGroups);
2555
2556 /// Insert the while op itself.
2557 auto cond = whileOp.getConditionValue();
2558 auto condGroup = getState<ComponentLoweringState>()
2559 .getEvaluatingGroup<calyx::CombGroupOp>(cond);
2560 auto symbolAttr = FlatSymbolRefAttr::get(
2561 StringAttr::get(getContext(), condGroup.getSymName()));
2562 return calyx::WhileOp::create(rewriter, loc, cond, symbolAttr);
2563 }
2564
2565 calyx::RepeatOp buildForCtrlOp(ScfForOp forOp,
2566 SmallVector<calyx::GroupOp> const &initGroups,
2567 uint64_t bound,
2568 PatternRewriter &rewriter) const {
2569 Location loc = forOp.getLoc();
2570 // Insert for iter arg initialization group(s). Emit a
2571 // parallel group to assign one or more registers all at once.
2572 insertParInitGroups(rewriter, loc, initGroups);
2573
2574 // Insert the repeatOp that corresponds to the For loop.
2575 return calyx::RepeatOp::create(rewriter, loc, bound);
2576 }
2577};
2578
2579/// LateSSAReplacement contains various functions for replacing SSA values that
2580/// were not replaced during op construction.
2582 using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;
2583
2584 LogicalResult partiallyLowerFuncToComp(FuncOp funcOp,
2585 PatternRewriter &) const override {
2586 funcOp.walk([&](scf::IfOp op) {
2587 for (auto res : getState<ComponentLoweringState>().getResultRegs(op))
2588 op.getOperation()->getResults()[res.first].replaceAllUsesWith(
2589 res.second.getOut());
2590 });
2591
2592 funcOp.walk([&](scf::WhileOp op) {
2593 /// The yielded values returned from the while op will be present in the
2594 /// iterargs registers post execution of the loop.
2595 /// This is done now, as opposed to during BuildWhileGroups since if the
2596 /// results of the whileOp were replaced before
2597 /// BuildOpGroups/BuildControl, the whileOp would get dead-code
2598 /// eliminated.
2599 ScfWhileOp whileOp(op);
2600 for (auto res :
2601 getState<ComponentLoweringState>().getWhileLoopIterRegs(whileOp))
2602 whileOp.getOperation()->getResults()[res.first].replaceAllUsesWith(
2603 res.second.getOut());
2604 });
2605
2606 funcOp.walk([&](memref::LoadOp loadOp) {
2607 if (calyx::singleLoadFromMemory(loadOp)) {
2608 /// In buildOpGroups we did not replace loadOp's results, to ensure a
2609 /// link between evaluating groups (which fix the input addresses of a
2610 /// memory op) and a readData result. Now, we may replace these SSA
2611 /// values with their memoryOp readData output.
2612 loadOp.getResult().replaceAllUsesWith(
2613 getState<ComponentLoweringState>()
2614 .getMemoryInterface(loadOp.getMemref())
2615 .readData());
2616 }
2617 });
2618
2619 return success();
2620 }
2621};
2622
2623/// Erases FuncOp operations.
2625 using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;
2626
2627 LogicalResult matchAndRewrite(FuncOp funcOp,
2628 PatternRewriter &rewriter) const override {
2629 rewriter.eraseOp(funcOp);
2630 return success();
2631 }
2632
2633 LogicalResult
2635 PatternRewriter &rewriter) const override {
2636 return success();
2637 }
2638};
2639
2640} // namespace scftocalyx
2641
2642namespace {
2643
2644using namespace circt::scftocalyx;
2645
2646//===----------------------------------------------------------------------===//
2647// Pass driver
2648//===----------------------------------------------------------------------===//
2649class SCFToCalyxPass : public circt::impl::SCFToCalyxBase<SCFToCalyxPass> {
2650public:
2651 SCFToCalyxPass(std::string topLevelFunction)
2652 : SCFToCalyxBase<SCFToCalyxPass>(), partialPatternRes(success()) {
2653 this->topLevelFunctionOpt = topLevelFunction;
2654 }
2655 void runOnOperation() override;
2656
2657 LogicalResult setTopLevelFunction(mlir::ModuleOp moduleOp,
2658 std::string &topLevelFunction) {
2659 if (!topLevelFunctionOpt.empty()) {
2660 if (SymbolTable::lookupSymbolIn(moduleOp, topLevelFunctionOpt) ==
2661 nullptr) {
2662 moduleOp.emitError() << "Top level function '" << topLevelFunctionOpt
2663 << "' not found in module.";
2664 return failure();
2665 }
2666 topLevelFunction = topLevelFunctionOpt;
2667 } else {
2668 /// No top level function set; infer top level if the module only contains
2669 /// a single function, else, throw error.
2670 auto funcOps = moduleOp.getOps<FuncOp>();
2671 if (std::distance(funcOps.begin(), funcOps.end()) == 1)
2672 topLevelFunction = (*funcOps.begin()).getSymName().str();
2673 else {
2674 moduleOp.emitError()
2675 << "Module contains multiple functions, but no top level "
2676 "function was set. Please see --top-level-function";
2677 return failure();
2678 }
2679 }
2680
2681 return createOptNewTopLevelFn(moduleOp, topLevelFunction);
2682 }
2683
2684 struct LoweringPattern {
2685 enum class Strategy { Once, Greedy };
2686 RewritePatternSet pattern;
2687 Strategy strategy;
2688 };
2689
2690 //// Labels the entry point of a Calyx program.
2691 /// Furthermore, this function performs validation on the input function,
2692 /// to ensure that we've implemented the capabilities necessary to convert
2693 /// it.
2694 LogicalResult labelEntryPoint(StringRef topLevelFunction) {
2695 // Program legalization - the partial conversion driver will not run
2696 // unless some pattern is provided - provide a dummy pattern.
2697 struct DummyPattern : public OpRewritePattern<mlir::ModuleOp> {
2698 using OpRewritePattern::OpRewritePattern;
2699 LogicalResult matchAndRewrite(mlir::ModuleOp,
2700 PatternRewriter &) const override {
2701 return failure();
2702 }
2703 };
2704
2705 ConversionTarget target(getContext());
2706 target.addLegalDialect<calyx::CalyxDialect>();
2707 target.addLegalDialect<scf::SCFDialect>();
2708 target.addIllegalDialect<hw::HWDialect>();
2709 target.addIllegalDialect<comb::CombDialect>();
2710
2711 // Only accept std operations which we've added lowerings for
2712 target.addIllegalDialect<FuncDialect>();
2713 target.addIllegalDialect<ArithDialect>();
2714 target.addLegalOp<
2715 AddIOp, SelectOp, SubIOp, CmpIOp, ShLIOp, ShRUIOp, ShRSIOp, AndIOp,
2716 XOrIOp, OrIOp, ExtUIOp, TruncIOp, CondBranchOp, BranchOp, MulIOp,
2717 DivUIOp, DivSIOp, RemUIOp, RemSIOp, ReturnOp, arith::ConstantOp,
2718 IndexCastOp, BitcastOp, FuncOp, ExtSIOp, CallOp, AddFOp, SubFOp, MulFOp,
2719 CmpFOp, FPToSIOp, SIToFPOp, DivFOp, math::SqrtOp>();
2720
2721 RewritePatternSet legalizePatterns(&getContext());
2722 legalizePatterns.add<DummyPattern>(&getContext());
2723 DenseSet<Operation *> legalizedOps;
2724 if (applyPartialConversion(getOperation(), target,
2725 std::move(legalizePatterns))
2726 .failed())
2727 return failure();
2728
2729 // Program conversion
2730 return calyx::applyModuleOpConversion(getOperation(), topLevelFunction);
2731 }
2732
2733 /// 'Once' patterns are expected to take an additional LogicalResult&
2734 /// argument, to forward their result state (greedyPatternRewriteDriver
2735 /// results are skipped for Once patterns).
2736 template <typename TPattern, typename... PatternArgs>
2737 void addOncePattern(SmallVectorImpl<LoweringPattern> &patterns,
2738 PatternArgs &&...args) {
2739 RewritePatternSet ps(&getContext());
2740 ps.add<TPattern>(&getContext(), partialPatternRes, args...);
2741 patterns.push_back(
2742 LoweringPattern{std::move(ps), LoweringPattern::Strategy::Once});
2743 }
2744
2745 template <typename TPattern, typename... PatternArgs>
2746 void addGreedyPattern(SmallVectorImpl<LoweringPattern> &patterns,
2747 PatternArgs &&...args) {
2748 RewritePatternSet ps(&getContext());
2749 ps.add<TPattern>(&getContext(), args...);
2750 patterns.push_back(
2751 LoweringPattern{std::move(ps), LoweringPattern::Strategy::Greedy});
2752 }
2753
2754 LogicalResult runPartialPattern(RewritePatternSet &pattern, bool runOnce) {
2755 assert(pattern.getNativePatterns().size() == 1 &&
2756 "Should only apply 1 partial lowering pattern at once");
2757
2758 // During component creation, the function body is inlined into the
2759 // component body for further processing. However, proper control flow
2760 // will only be established later in the conversion process, so ensure
2761 // that rewriter optimizations (especially DCE) are disabled.
2762 GreedyRewriteConfig config;
2763 config.setRegionSimplificationLevel(
2764 mlir::GreedySimplifyRegionLevel::Disabled);
2765 if (runOnce)
2766 config.setMaxIterations(1);
2767
2768 /// Can't return applyPatternsGreedily. Root isn't
2769 /// necessarily erased so it will always return failed(). Instead,
2770 /// forward the 'succeeded' value from PartialLoweringPatternBase.
2771 (void)applyPatternsGreedily(getOperation(), std::move(pattern), config);
2772 return partialPatternRes;
2773 }
2774
2775private:
2776 LogicalResult partialPatternRes;
2777 std::shared_ptr<calyx::CalyxLoweringState> loweringState = nullptr;
2778
2779 /// Creates a new new top-level function based on `baseName`.
2780 FuncOp createNewTopLevelFn(ModuleOp moduleOp, std::string &baseName) {
2781 std::string newName = "main";
2782
2783 if (auto *existingMainOp = SymbolTable::lookupSymbolIn(moduleOp, newName)) {
2784 auto existingMainFunc = dyn_cast<FuncOp>(existingMainOp);
2785 if (existingMainFunc == nullptr) {
2786 moduleOp.emitError() << "Symbol 'main' exists but is not a function";
2787 return nullptr;
2788 }
2789 unsigned counter = 0;
2790 std::string newOldName = baseName;
2791 while (SymbolTable::lookupSymbolIn(moduleOp, newOldName))
2792 newOldName = llvm::join_items("_", baseName, std::to_string(++counter));
2793 existingMainFunc.setName(newOldName);
2794 if (baseName == "main")
2795 baseName = newOldName;
2796 }
2797
2798 // Create the new "main" function
2799 OpBuilder builder(moduleOp.getContext());
2800 builder.setInsertionPointToStart(moduleOp.getBody());
2801
2802 FunctionType funcType = builder.getFunctionType({}, {});
2803
2804 if (auto newFunc =
2805 FuncOp::create(builder, moduleOp.getLoc(), newName, funcType))
2806 return newFunc;
2807
2808 return nullptr;
2809 }
2810
2811 /// Insert a call from the newly created top-level function/`caller` to the
2812 /// old top-level function/`callee`; and create `memref.alloc`s inside the new
2813 /// top-level function for arguments with `memref` types and for the
2814 /// `memref.alloc`s inside `callee`.
2815 void insertCallFromNewTopLevel(OpBuilder &builder, FuncOp caller,
2816 FuncOp callee) {
2817 if (caller.getBody().empty()) {
2818 caller.addEntryBlock();
2819 }
2820
2821 Block *callerEntryBlock = &caller.getBody().front();
2822 builder.setInsertionPointToStart(callerEntryBlock);
2823
2824 // For those non-memref arguments passing to the original top-level
2825 // function, we need to copy them to the new top-level function.
2826 SmallVector<Type, 4> nonMemRefCalleeArgTypes;
2827 for (auto arg : callee.getArguments()) {
2828 if (!isa<MemRefType>(arg.getType())) {
2829 nonMemRefCalleeArgTypes.push_back(arg.getType());
2830 }
2831 }
2832
2833 for (Type type : nonMemRefCalleeArgTypes) {
2834 callerEntryBlock->addArgument(type, caller.getLoc());
2835 }
2836
2837 FunctionType callerFnType = caller.getFunctionType();
2838 SmallVector<Type, 4> updatedCallerArgTypes(
2839 caller.getFunctionType().getInputs());
2840 updatedCallerArgTypes.append(nonMemRefCalleeArgTypes.begin(),
2841 nonMemRefCalleeArgTypes.end());
2842 caller.setType(FunctionType::get(caller.getContext(), updatedCallerArgTypes,
2843 callerFnType.getResults()));
2844
2845 Block *calleeFnBody = &callee.getBody().front();
2846 unsigned originalCalleeArgNum = callee.getArguments().size();
2847
2848 SmallVector<Value, 4> extraMemRefArgs;
2849 SmallVector<Type, 4> extraMemRefArgTypes;
2850 SmallVector<Value, 4> extraMemRefOperands;
2851 SmallVector<Operation *, 4> opsToModify;
2852 for (auto &op : callee.getBody().getOps()) {
2853 if (isa<memref::AllocaOp, memref::AllocOp, memref::GetGlobalOp>(op))
2854 opsToModify.push_back(&op);
2855 }
2856
2857 // Replace `alloc`/`getGlobal` in the original top-level with new
2858 // corresponding operations in the new top-level.
2859 builder.setInsertionPointToEnd(callerEntryBlock);
2860 for (auto *op : opsToModify) {
2861 // TODO (https://github.com/llvm/circt/issues/7764)
2862 Value newOpRes;
2863 TypeSwitch<Operation *>(op)
2864 .Case<memref::AllocaOp>([&](memref::AllocaOp allocaOp) {
2865 newOpRes = memref::AllocaOp::create(builder, callee.getLoc(),
2866 allocaOp.getType());
2867 })
2868 .Case<memref::AllocOp>([&](memref::AllocOp allocOp) {
2869 newOpRes = memref::AllocOp::create(builder, callee.getLoc(),
2870 allocOp.getType());
2871 })
2872 .Case<memref::GetGlobalOp>([&](memref::GetGlobalOp getGlobalOp) {
2873 newOpRes = memref::GetGlobalOp::create(builder, caller.getLoc(),
2874 getGlobalOp.getType(),
2875 getGlobalOp.getName());
2876 })
2877 .Default([&](Operation *defaultOp) {
2878 llvm::report_fatal_error("Unsupported operation in TypeSwitch");
2879 });
2880 extraMemRefOperands.push_back(newOpRes);
2881
2882 calleeFnBody->addArgument(newOpRes.getType(), callee.getLoc());
2883 BlockArgument newBodyArg = calleeFnBody->getArguments().back();
2884 op->getResult(0).replaceAllUsesWith(newBodyArg);
2885 op->erase();
2886 extraMemRefArgs.push_back(newBodyArg);
2887 extraMemRefArgTypes.push_back(newBodyArg.getType());
2888 }
2889
2890 SmallVector<Type, 4> updatedCalleeArgTypes(
2891 callee.getFunctionType().getInputs());
2892 updatedCalleeArgTypes.append(extraMemRefArgTypes.begin(),
2893 extraMemRefArgTypes.end());
2894 callee.setType(FunctionType::get(callee.getContext(), updatedCalleeArgTypes,
2895 callee.getFunctionType().getResults()));
2896
2897 unsigned otherArgsCount = 0;
2898 SmallVector<Value, 4> calleeArgFnOperands;
2899 builder.setInsertionPointToStart(callerEntryBlock);
2900 for (auto arg : callee.getArguments().take_front(originalCalleeArgNum)) {
2901 if (isa<MemRefType>(arg.getType())) {
2902 auto memrefType = cast<MemRefType>(arg.getType());
2903 auto allocOp =
2904 memref::AllocOp::create(builder, callee.getLoc(), memrefType);
2905 calleeArgFnOperands.push_back(allocOp);
2906 } else {
2907 auto callerArg = callerEntryBlock->getArgument(otherArgsCount++);
2908 calleeArgFnOperands.push_back(callerArg);
2909 }
2910 }
2911
2912 SmallVector<Value, 4> fnOperands;
2913 fnOperands.append(calleeArgFnOperands.begin(), calleeArgFnOperands.end());
2914 fnOperands.append(extraMemRefOperands.begin(), extraMemRefOperands.end());
2915 auto calleeName =
2916 SymbolRefAttr::get(builder.getContext(), callee.getSymName());
2917 auto resultTypes = callee.getResultTypes();
2918
2919 builder.setInsertionPointToEnd(callerEntryBlock);
2920 CallOp::create(builder, caller.getLoc(), calleeName, resultTypes,
2921 fnOperands);
2922 ReturnOp::create(builder, caller.getLoc());
2923 }
2924
2925 /// Conditionally creates an optional new top-level function; and inserts a
2926 /// call from the new top-level function to the old top-level function if we
2927 /// did create one
2928 LogicalResult createOptNewTopLevelFn(ModuleOp moduleOp,
2929 std::string &topLevelFunction) {
2930 auto hasMemrefArguments = [](FuncOp func) {
2931 return std::any_of(
2932 func.getArguments().begin(), func.getArguments().end(),
2933 [](BlockArgument arg) { return isa<MemRefType>(arg.getType()); });
2934 };
2935
2936 /// We only create a new top-level function and call the original top-level
2937 /// function from the new one if the original top-level has `memref` in its
2938 /// argument
2939 auto funcOps = moduleOp.getOps<FuncOp>();
2940 bool hasMemrefArgsInTopLevel =
2941 std::any_of(funcOps.begin(), funcOps.end(), [&](auto funcOp) {
2942 return funcOp.getName() == topLevelFunction &&
2943 hasMemrefArguments(funcOp);
2944 });
2945
2946 if (hasMemrefArgsInTopLevel) {
2947 auto newTopLevelFunc = createNewTopLevelFn(moduleOp, topLevelFunction);
2948 if (!newTopLevelFunc)
2949 return failure();
2950
2951 OpBuilder builder(moduleOp.getContext());
2952 Operation *oldTopLevelFuncOp =
2953 SymbolTable::lookupSymbolIn(moduleOp, topLevelFunction);
2954 if (auto oldTopLevelFunc = dyn_cast<FuncOp>(oldTopLevelFuncOp))
2955 insertCallFromNewTopLevel(builder, newTopLevelFunc, oldTopLevelFunc);
2956 else {
2957 moduleOp.emitOpError("Original top-level function not found!");
2958 return failure();
2959 }
2960 topLevelFunction = "main";
2961 }
2962
2963 return success();
2964 }
2965};
2966
2967void SCFToCalyxPass::runOnOperation() {
2968 // Clear internal state. See https://github.com/llvm/circt/issues/3235
2969 loweringState.reset();
2970 partialPatternRes = LogicalResult::failure();
2971
2972 std::string topLevelFunction;
2973 if (failed(setTopLevelFunction(getOperation(), topLevelFunction))) {
2974 signalPassFailure();
2975 return;
2976 }
2977
2978 /// Start conversion
2979 if (failed(labelEntryPoint(topLevelFunction))) {
2980 signalPassFailure();
2981 return;
2982 }
2983 loweringState = std::make_shared<calyx::CalyxLoweringState>(getOperation(),
2984 topLevelFunction);
2985
2986 /// --------------------------------------------------------------------------
2987 /// If you are a developer, it may be helpful to add a
2988 /// 'getOperation()->dump()' call after the execution of each stage to
2989 /// view the transformations that's going on.
2990 /// --------------------------------------------------------------------------
2991
2992 /// A mapping is maintained between a function operation and its corresponding
2993 /// Calyx component.
2994 DenseMap<FuncOp, calyx::ComponentOp> funcMap;
2995 SmallVector<LoweringPattern, 8> loweringPatterns;
2996 calyx::PatternApplicationState patternState;
2997
2998 /// Creates a new Calyx component for each FuncOp in the inpurt module.
2999 addOncePattern<FuncOpConversion>(loweringPatterns, patternState, funcMap,
3000 *loweringState);
3001
3002 /// This pass inlines scf.ExecuteRegionOp's by adding control-flow.
3003 addGreedyPattern<InlineExecuteRegionOpPattern>(loweringPatterns);
3004
3005 /// This pattern converts all index typed values to an i32 integer.
3006 addOncePattern<calyx::ConvertIndexTypes>(loweringPatterns, patternState,
3007 funcMap, *loweringState);
3008
3009 /// This pattern creates registers for all basic-block arguments.
3010 addOncePattern<calyx::BuildBasicBlockRegs>(loweringPatterns, patternState,
3011 funcMap, *loweringState);
3012
3013 addOncePattern<calyx::BuildCallInstance>(loweringPatterns, patternState,
3014 funcMap, *loweringState);
3015
3016 /// This pattern creates registers for the function return values.
3017 addOncePattern<calyx::BuildReturnRegs>(loweringPatterns, patternState,
3018 funcMap, *loweringState);
3019
3020 /// This pattern creates registers for iteration arguments of scf.while
3021 /// operations. Additionally, creates a group for assigning the initial
3022 /// value of the iteration argument registers.
3023 addOncePattern<BuildWhileGroups>(loweringPatterns, patternState, funcMap,
3024 *loweringState);
3025
3026 /// This pattern creates registers for iteration arguments of scf.for
3027 /// operations. Additionally, creates a group for assigning the initial
3028 /// value of the iteration argument registers.
3029 addOncePattern<BuildForGroups>(loweringPatterns, patternState, funcMap,
3030 *loweringState);
3031
3032 addOncePattern<BuildIfGroups>(loweringPatterns, patternState, funcMap,
3033 *loweringState);
3034
3035 /// This pattern converts operations within basic blocks to Calyx library
3036 /// operators. Combinational operations are assigned inside a
3037 /// calyx::CombGroupOp, and sequential inside calyx::GroupOps.
3038 /// Sequential groups are registered with the Block* of which the operation
3039 /// originated from. This is used during control schedule generation. By
3040 /// having a distinct group for each operation, groups are analogous to SSA
3041 /// values in the source program.
3042 addOncePattern<BuildOpGroups>(loweringPatterns, patternState, funcMap,
3043 *loweringState, writeJsonOpt);
3044
3045 /// This pattern traverses the CFG of the program and generates a control
3046 /// schedule based on the calyx::GroupOp's which were registered for each
3047 /// basic block in the source function.
3048 addOncePattern<BuildControl>(loweringPatterns, patternState, funcMap,
3049 *loweringState);
3050
3051 /// This pass recursively inlines use-def chains of combinational logic (from
3052 /// non-stateful groups) into groups referenced in the control schedule.
3053 addOncePattern<calyx::InlineCombGroups>(loweringPatterns, patternState,
3054 *loweringState);
3055
3056 /// This pattern performs various SSA replacements that must be done
3057 /// after control generation.
3058 addOncePattern<LateSSAReplacement>(loweringPatterns, patternState, funcMap,
3059 *loweringState);
3060
3061 /// Eliminate any unused combinational groups. This is done before
3062 /// calyx::RewriteMemoryAccesses to avoid inferring slice components for
3063 /// groups that will be removed.
3064 addGreedyPattern<calyx::EliminateUnusedCombGroups>(loweringPatterns);
3065
3066 /// This pattern rewrites accesses to memories which are too wide due to
3067 /// index types being converted to a fixed-width integer type.
3068 addOncePattern<calyx::RewriteMemoryAccesses>(loweringPatterns, patternState,
3069 *loweringState);
3070
3071 /// This pattern removes the source FuncOp which has now been converted into
3072 /// a Calyx component.
3073 addOncePattern<CleanupFuncOps>(loweringPatterns, patternState, funcMap,
3074 *loweringState);
3075
3076 /// Sequentially apply each lowering pattern.
3077 for (auto &pat : loweringPatterns) {
3078 LogicalResult partialPatternRes = runPartialPattern(
3079 pat.pattern,
3080 /*runOnce=*/pat.strategy == LoweringPattern::Strategy::Once);
3081 if (succeeded(partialPatternRes))
3082 continue;
3083 signalPassFailure();
3084 return;
3085 }
3086
3087 //===--------------------------------------------------------------------===//
3088 // Cleanup patterns
3089 //===--------------------------------------------------------------------===//
3090 RewritePatternSet cleanupPatterns(&getContext());
3091 cleanupPatterns.add<calyx::MultipleGroupDonePattern,
3093 if (failed(
3094 applyPatternsGreedily(getOperation(), std::move(cleanupPatterns)))) {
3095 signalPassFailure();
3096 return;
3097 }
3098
3099 if (ciderSourceLocationMetadata) {
3100 // Debugging information for the Cider debugger.
3101 // Reference: https://docs.calyxir.org/debug/cider.html
3102 SmallVector<Attribute, 16> sourceLocations;
3103 getOperation()->walk([&](calyx::ComponentOp component) {
3104 return getCiderSourceLocationMetadata(component, sourceLocations);
3105 });
3106
3107 MLIRContext *context = getOperation()->getContext();
3108 getOperation()->setAttr("calyx.metadata",
3109 ArrayAttr::get(context, sourceLocations));
3110 }
3111}
3112} // namespace
3113
3114//===----------------------------------------------------------------------===//
3115// Pass initialization
3116//===----------------------------------------------------------------------===//
3117
3118std::unique_ptr<OperationPass<ModuleOp>>
3119createSCFToCalyxPass(std::string topLevelFunction) {
3120 return std::make_unique<SCFToCalyxPass>(topLevelFunction);
3121}
3122
3123} // namespace circt
assert(baseType &&"element must be base type")
static Block * getBodyBlock(FModuleLike mod)
RewritePatternSet pattern
Strategy strategy
std::shared_ptr< calyx::CalyxLoweringState > loweringState
LogicalResult partialPatternRes
An interface for conversion passes that lower Calyx programs.
std::string irName(ValueOrBlock &v)
Returns a meaningful name for a value within the program scope.
std::string blockName(Block *b)
Returns a meaningful name for a block within the program scope (removes the ^ prefix from block names...
T * getState(calyx::ComponentOp op)
Returns the component lowering state associated with op.
void setFuncOpResultMapping(const DenseMap< unsigned, unsigned > &mapping)
Assign a mapping between the source funcOp result indices and the corresponding output port indices o...
std::string getUniqueName(StringRef prefix)
Returns a unique name within compOp with the provided prefix.
calyx::ComponentOp component
The component which this lowering state is associated to.
void registerMemoryInterface(Value memref, const calyx::MemoryInterface &memoryInterface)
Registers a memory interface as being associated with a memory identified by 'memref'.
calyx::ComponentOp getComponentOp()
Returns the calyx::ComponentOp associated with this lowering state.
void setDataField(StringRef name, llvm::json::Array data)
ComponentLoweringStateInterface(calyx::ComponentOp component)
void setFormat(StringRef name, std::string numType, bool isSigned, unsigned width)
FuncOpPartialLoweringPatterns are patterns which intend to match on FuncOps and then perform their ow...
calyx::ComponentOp getComponent() const
Returns the component operation associated with the currently executing partial lowering.
DenseMap< mlir::func::FuncOp, calyx::ComponentOp > & functionMapping
CalyxLoweringState & loweringState() const
Return the calyx lowering state for this pattern.
FuncOpPartialLoweringPattern(MLIRContext *context, LogicalResult &resRef, PatternApplicationState &patternState, DenseMap< mlir::func::FuncOp, calyx::ComponentOp > &map, calyx::CalyxLoweringState &state)
calyx::GroupOp getLoopLatchGroup(ScfWhileOp op)
Retrieve the loop latch group registered for op.
void setLoopLatchGroup(ScfWhileOp op, calyx::GroupOp group)
Registers grp to be the loop latch group of op.
calyx::RegisterOp getLoopIterReg(ScfForOp op, unsigned idx)
Return a mapping of block argument indices to block argument.
void addLoopIterReg(ScfWhileOp op, calyx::RegisterOp reg, unsigned idx)
Register reg as being the idx'th iter_args register for 'op'.
void setLoopInitGroups(ScfWhileOp op, SmallVector< calyx::GroupOp > groups)
Registers groups to be the loop init groups of op.
SmallVector< calyx::GroupOp > getLoopInitGroups(ScfWhileOp op)
Retrieve the loop init groups registered for op.
calyx::GroupOp buildLoopIterArgAssignments(OpBuilder &builder, ScfWhileOp op, calyx::ComponentOp componentOp, Twine uniqueSuffix, MutableArrayRef< OpOperand > ops)
Creates a new group that assigns the 'ops' values to the iter arg registers of the loop operation.
const DenseMap< unsigned, calyx::RegisterOp > & getLoopIterRegs(ScfWhileOp op)
Return a mapping of block argument indices to block argument.
Holds common utilities used for scheduling when lowering to Calyx.
Builds a control schedule by traversing the CFG of the function and associating this with the previou...
calyx::RepeatOp buildForCtrlOp(ScfForOp forOp, SmallVector< calyx::GroupOp > const &initGroups, uint64_t bound, PatternRewriter &rewriter) const
LogicalResult partiallyLowerFuncToComp(FuncOp funcOp, PatternRewriter &rewriter) const override
LogicalResult schedulePath(PatternRewriter &rewriter, const DenseSet< Block * > &path, Location loc, Block *from, Block *to, Block *parentCtrlBlock) const
Schedules a block by inserting a branch argument assignment block (if any) before recursing into the ...
calyx::WhileOp buildWhileCtrlOp(ScfWhileOp whileOp, SmallVector< calyx::GroupOp > initGroups, PatternRewriter &rewriter) const
LogicalResult scheduleBasicBlock(PatternRewriter &rewriter, const DenseSet< Block * > &path, mlir::Block *parentCtrlBlock, mlir::Block *block) const
Sequentially schedules the groups that registered themselves with 'block'.
LogicalResult buildCFGControl(DenseSet< Block * > path, PatternRewriter &rewriter, mlir::Block *parentCtrlBlock, mlir::Block *preBlock, mlir::Block *block) const
void insertParInitGroups(PatternRewriter &rewriter, Location loc, const SmallVector< calyx::GroupOp > &initGroups) const
In BuildForGroups, a register is created for the iteration argument of the for op.
LogicalResult partiallyLowerFuncToComp(FuncOp funcOp, PatternRewriter &rewriter) const override
LogicalResult partiallyLowerFuncToComp(FuncOp funcOp, PatternRewriter &rewriter) const override
Iterate through the operations of a source function and instantiate components or primitives based on...
BuildOpGroups(MLIRContext *context, LogicalResult &resRef, calyx::PatternApplicationState &patternState, DenseMap< mlir::func::FuncOp, calyx::ComponentOp > &map, calyx::CalyxLoweringState &state, mlir::Pass::Option< std::string > &writeJsonOpt)
LogicalResult buildCmpIOpHelper(PatternRewriter &rewriter, CmpIOp op) const
void setupCmpIOp(PatternRewriter &rewriter, CmpIOp cmpIOp, Operation *group, calyx::RegisterOp &condReg, calyx::RegisterOp &resReg, TCalyxLibOp calyxOp) const
LogicalResult buildFpIntTypeCastOp(PatternRewriter &rewriter, TSrcOp op, unsigned inputWidth, unsigned outputWidth, StringRef signedPort) const
TGroupOp createGroupForOp(PatternRewriter &rewriter, Operation *op) const
Creates a group named by the basic block which the input op resides in.
LogicalResult buildLibraryOp(PatternRewriter &rewriter, TSrcOp op) const
buildLibraryOp which provides in- and output types based on the operands and results of the op argume...
LogicalResult buildOp(PatternRewriter &rewriter, scf::YieldOp yieldOp) const
Op builder specializations.
calyx::RegisterOp createSignalRegister(PatternRewriter &rewriter, Value signal, bool invert, StringRef nameSuffix, calyx::CompareFOpIEEE754 calyxCmpFOp, calyx::GroupOp group) const
void assignAddressPorts(PatternRewriter &rewriter, Location loc, calyx::GroupInterface group, calyx::MemoryInterface memoryInterface, Operation::operand_range addressValues) const
Creates assignments within the provided group to the address ports of the memoryOp based on the provi...
LogicalResult buildLibraryOp(PatternRewriter &rewriter, TSrcOp op, TypeRange srcTypes, TypeRange dstTypes) const
buildLibraryOp will build a TCalyxLibOp inside a TGroupOp based on the source operation TSrcOp.
LogicalResult partiallyLowerFuncToComp(FuncOp funcOp, PatternRewriter &rewriter) const override
LogicalResult buildLibraryBinaryPipeOp(PatternRewriter &rewriter, TSrcOp op, TOpType opPipe, Value out) const
buildLibraryBinaryPipeOp will build a TCalyxLibBinaryPipeOp, to deal with MulIOp, DivUIOp and RemUIOp...
mlir::Pass::Option< std::string > & writeJson
In BuildWhileGroups, a register is created for each iteration argumenet of the while op.
LogicalResult partiallyLowerFuncToComp(FuncOp funcOp, PatternRewriter &rewriter) const override
Erases FuncOp operations.
LogicalResult matchAndRewrite(FuncOp funcOp, PatternRewriter &rewriter) const override
LogicalResult partiallyLowerFuncToComp(FuncOp funcOp, PatternRewriter &rewriter) const override
Handles the current state of lowering of a Calyx component.
ComponentLoweringState(calyx::ComponentOp component)
void setForLoopInitGroups(ScfForOp op, SmallVector< calyx::GroupOp > groups)
calyx::GroupOp buildForLoopIterArgAssignments(OpBuilder &builder, ScfForOp op, calyx::ComponentOp componentOp, Twine uniqueSuffix, MutableArrayRef< OpOperand > ops)
void setForLoopLatchGroup(ScfForOp op, calyx::GroupOp group)
SmallVector< calyx::GroupOp > getForLoopInitGroups(ScfForOp op)
void addForLoopIterReg(ScfForOp op, calyx::RegisterOp reg, unsigned idx)
calyx::GroupOp getForLoopLatchGroup(ScfForOp op)
calyx::RegisterOp getForLoopIterReg(ScfForOp op, unsigned idx)
const DenseMap< unsigned, calyx::RegisterOp > & getForLoopIterRegs(ScfForOp op)
DenseMap< Operation *, calyx::GroupOp > elseGroup
DenseMap< Operation *, calyx::GroupOp > thenGroup
void setCondReg(scf::IfOp op, calyx::RegisterOp regOp)
const DenseMap< unsigned, calyx::RegisterOp > & getResultRegs(scf::IfOp op)
void setElseGroup(scf::IfOp op, calyx::GroupOp group)
void setResultRegs(scf::IfOp op, calyx::RegisterOp reg, unsigned idx)
void setThenGroup(scf::IfOp op, calyx::GroupOp group)
DenseMap< Operation *, DenseMap< unsigned, calyx::RegisterOp > > resultRegs
calyx::RegisterOp getResultRegs(scf::IfOp op, unsigned idx)
calyx::RegisterOp getCondReg(scf::IfOp op)
calyx::GroupOp getThenGroup(scf::IfOp op)
DenseMap< Operation *, calyx::RegisterOp > condReg
calyx::GroupOp getElseGroup(scf::IfOp op)
Inlines Calyx ExecuteRegionOp operations within their parent blocks.
LogicalResult matchAndRewrite(scf::ExecuteRegionOp execOp, PatternRewriter &rewriter) const override
LateSSAReplacement contains various functions for replacing SSA values that were not replaced during ...
LogicalResult partiallyLowerFuncToComp(FuncOp funcOp, PatternRewriter &) const override
std::optional< int64_t > getBound() override
Block::BlockArgListType getBodyArgs() override
Block * getBodyBlock() override
Block * getBodyBlock() override
Block::BlockArgListType getBodyArgs() override
Value getConditionValue() override
std::optional< int64_t > getBound() override
Block * getConditionBlock() override
Stores the state information for condition checks involving sequential computation.
void setSeqResReg(Operation *op, calyx::RegisterOp reg)
calyx::RegisterOp getSeqResReg(Operation *op)
DenseMap< Operation *, calyx::RegisterOp > resultRegs
calyx::GroupOp buildWhileLoopIterArgAssignments(OpBuilder &builder, ScfWhileOp op, calyx::ComponentOp componentOp, Twine uniqueSuffix, MutableArrayRef< OpOperand > ops)
void setWhileLoopInitGroups(ScfWhileOp op, SmallVector< calyx::GroupOp > groups)
SmallVector< calyx::GroupOp > getWhileLoopInitGroups(ScfWhileOp op)
void addWhileLoopIterReg(ScfWhileOp op, calyx::RegisterOp reg, unsigned idx)
void setWhileLoopLatchGroup(ScfWhileOp op, calyx::GroupOp group)
const DenseMap< unsigned, calyx::RegisterOp > & getWhileLoopIterRegs(ScfWhileOp op)
calyx::GroupOp getWhileLoopLatchGroup(ScfWhileOp op)
bool parentIsSeqCell(Value value)
void addMandatoryComponentPorts(PatternRewriter &rewriter, SmallVectorImpl< calyx::PortInfo > &ports)
void buildAssignmentsForRegisterWrite(OpBuilder &builder, calyx::GroupOp groupOp, calyx::ComponentOp componentOp, calyx::RegisterOp &reg, Value inputValue)
Creates register assignment operations within the provided groupOp.
DenseMap< const mlir::RewritePattern *, SmallPtrSet< Operation *, 16 > > PatternApplicationState
Extra state that is passed to all PartialLoweringPatterns so they can record when they have run on an...
PredicateInfo getPredicateInfo(mlir::arith::CmpFPredicate pred)
Type normalizeType(OpBuilder &builder, Type type)
LogicalResult applyModuleOpConversion(mlir::ModuleOp, StringRef topLevelFunction)
Helper to update the top-level ModuleOp to set the entrypoing function.
WalkResult getCiderSourceLocationMetadata(calyx::ComponentOp component, SmallVectorImpl< Attribute > &sourceLocations)
bool matchConstantOp(Operation *op, APInt &value)
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.
bool noStoresToMemory(Value memoryReference)
bool singleLoadFromMemory(Value memoryReference)
Type toBitVector(T type)
Performs a bit cast from a non-signless integer type value, such as a floating point value,...
std::string getInstanceName(mlir::func::CallOp callOp)
A helper function to get the instance name.
Value createOrFoldNot(Location loc, Value value, OpBuilder &builder, bool twoState=false)
Create a `‘Not’' gate on a value.
Definition CombOps.cpp:66
static constexpr std::string_view sPortNameAttr
Definition SCFToCalyx.h:29
static constexpr std::string_view unrolledParallelAttr
static LogicalResult buildAllocOp(ComponentLoweringState &componentState, PatternRewriter &rewriter, TAllocOp allocOp)
std::variant< calyx::GroupOp, WhileScheduleable, ForScheduleable, IfScheduleable, CallScheduleable, ParScheduleable > Scheduleable
A variant of types representing scheduleable operations.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
std::unique_ptr< OperationPass< ModuleOp > > createSCFToCalyxPass(std::string topLevelFunction="")
Create an SCF to Calyx conversion pass.
When building groups which contain accesses to multiple sequential components, a group_done op is cre...
GroupDoneOp's are terminator operations and should therefore be the last operator in a group.
This holds information about the port for either a Component or Cell.
Definition CalyxOps.h:89
Predicate information for the floating point comparisons.
calyx::InstanceOp instanceOp
Instance for invoking.
ScfForOp forOp
For operation to schedule.
Creates a new Calyx component for each FuncOp in the program.
LogicalResult partiallyLowerFuncToComp(FuncOp funcOp, PatternRewriter &rewriter) const override
scf::ParallelOp parOp
Parallel operation to schedule.
ScfWhileOp whileOp
While operation to schedule.