-
Notifications
You must be signed in to change notification settings - Fork 299
/
HandshakeToDC.cpp
672 lines (566 loc) · 24.8 KB
/
HandshakeToDC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//===- HandshakeToDC.cpp - Translate Handshake into DC --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//===----------------------------------------------------------------------===//
//
// This is the main Handshake to DC Conversion Pass Implementation.
//
//===----------------------------------------------------------------------===//
#include "circt/Conversion/HandshakeToDC.h"
#include "../PassDetail.h"
#include "circt/Dialect/Comb/CombOps.h"
#include "circt/Dialect/DC/DCDialect.h"
#include "circt/Dialect/DC/DCOps.h"
#include "circt/Dialect/DC/DCTypes.h"
#include "circt/Dialect/HW/HWOps.h"
#include "circt/Dialect/HW/HWTypes.h"
#include "circt/Dialect/Handshake/HandshakeOps.h"
#include "circt/Dialect/Handshake/HandshakePasses.h"
#include "circt/Dialect/Handshake/Visitor.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/Support/MathExtras.h"
#include <optional>
using namespace mlir;
using namespace circt;
using namespace handshake;
using namespace dc;
using namespace hw;
using namespace handshaketodc;
namespace {
struct DCTuple {
DCTuple() = default;
DCTuple(Value token, Value data) : token(token), data(data) {}
DCTuple(dc::UnpackOp unpack)
: token(unpack.getToken()), data(unpack.getOutput()) {}
Value token;
Value data;
};
// Unpack a !dc.value<...> into a DCTuple.
static DCTuple unpack(OpBuilder &b, Value v) {
if (v.getType().isa<dc::ValueType>())
return DCTuple(b.create<dc::UnpackOp>(v.getLoc(), v));
assert(v.getType().isa<dc::TokenType>() && "Expected a dc::TokenType");
return DCTuple(v, {});
}
static Value pack(OpBuilder &b, Value token, Value data = {}) {
if (!data)
return token;
return b.create<dc::PackOp>(token.getLoc(), token, data);
}
class DCTypeConverter : public TypeConverter {
public:
DCTypeConverter() {
addConversion([](Type type) -> Type {
if (type.isa<NoneType>())
return dc::TokenType::get(type.getContext());
return dc::ValueType::get(type.getContext(), type);
});
addConversion([](ValueType type) { return type; });
addConversion([](TokenType type) { return type; });
addTargetMaterialization(
[](mlir::OpBuilder &builder, mlir::Type resultType,
mlir::ValueRange inputs,
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
// Materialize !dc.value<> -> !dc.token
if (resultType.isa<dc::TokenType>() &&
inputs.front().getType().isa<dc::ValueType>())
return unpack(builder, inputs.front()).token;
// Materialize !dc.token -> !dc.value<>
auto vt = resultType.dyn_cast<dc::ValueType>();
if (vt && !vt.getInnerType())
return pack(builder, inputs.front());
return inputs[0];
});
addSourceMaterialization(
[](mlir::OpBuilder &builder, mlir::Type resultType,
mlir::ValueRange inputs,
mlir::Location loc) -> std::optional<mlir::Value> {
if (inputs.size() != 1)
return std::nullopt;
// Materialize !dc.value<> -> !dc.token
if (resultType.isa<dc::TokenType>() &&
inputs.front().getType().isa<dc::ValueType>())
return unpack(builder, inputs.front()).token;
// Materialize !dc.token -> !dc.value<>
auto vt = resultType.dyn_cast<dc::ValueType>();
if (vt && !vt.getInnerType())
return pack(builder, inputs.front());
return inputs[0];
});
}
};
template <typename OpTy>
class DCOpConversionPattern : public OpConversionPattern<OpTy> {
public:
using OpConversionPattern<OpTy>::OpConversionPattern;
using OpAdaptor = typename OpTy::Adaptor;
DCOpConversionPattern(MLIRContext *context, TypeConverter &typeConverter,
ConvertedOps *convertedOps)
: OpConversionPattern<OpTy>(typeConverter, context),
convertedOps(convertedOps) {}
mutable ConvertedOps *convertedOps;
};
class CondBranchConversionPattern
: public DCOpConversionPattern<handshake::ConditionalBranchOp> {
public:
using DCOpConversionPattern<
handshake::ConditionalBranchOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::ConditionalBranchOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::ConditionalBranchOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto condition = unpack(rewriter, adaptor.getConditionOperand());
auto data = unpack(rewriter, adaptor.getDataOperand());
// Join the token of the condition and the input.
auto join = rewriter.create<dc::JoinOp>(
op.getLoc(), ValueRange{condition.token, data.token});
// Pack that together with the condition data.
auto packedCondition = pack(rewriter, join, condition.data);
// Branch on the input data and the joined control input.
auto branch = rewriter.create<dc::BranchOp>(op.getLoc(), packedCondition);
// Pack the branch output tokens with the input data, and replace the uses.
llvm::SmallVector<Value, 4> packed;
packed.push_back(pack(rewriter, branch.getTrueToken(), data.data));
packed.push_back(pack(rewriter, branch.getFalseToken(), data.data));
rewriter.replaceOp(op, packed);
return success();
}
};
class ForkOpConversionPattern
: public DCOpConversionPattern<handshake::ForkOp> {
public:
using DCOpConversionPattern<handshake::ForkOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::ForkOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::ForkOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto input = unpack(rewriter, adaptor.getOperand());
auto forkOut = rewriter.create<dc::ForkOp>(op.getLoc(), input.token,
op.getNumResults());
// Pack the fork result tokens with the input data, and replace the uses.
llvm::SmallVector<Value, 4> packed;
for (auto res : forkOut.getResults())
packed.push_back(pack(rewriter, res, input.data));
rewriter.replaceOp(op, packed);
return success();
}
};
class JoinOpConversion : public DCOpConversionPattern<handshake::JoinOp> {
public:
using DCOpConversionPattern<handshake::JoinOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::JoinOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::JoinOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
llvm::SmallVector<Value, 4> inputTokens;
for (auto input : adaptor.getData())
inputTokens.push_back(unpack(rewriter, input).token);
rewriter.replaceOpWithNewOp<dc::JoinOp>(op, inputTokens);
return success();
}
};
class MergeOpConversion : public DCOpConversionPattern<handshake::MergeOp> {
public:
using DCOpConversionPattern<handshake::MergeOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::MergeOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::MergeOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (op.getNumOperands() > 2)
return op.emitOpError("only two inputs supported");
SmallVector<Value, 4> tokens, data;
for (auto input : adaptor.getDataOperands()) {
auto up = unpack(rewriter, input);
tokens.push_back(up.token);
if (up.data)
data.push_back(up.data);
}
// Control side
Value selectedIndex = rewriter.create<dc::MergeOp>(op.getLoc(), tokens);
auto selectedIndexUnpacked = unpack(rewriter, selectedIndex);
Value mergeOutput;
if (!data.empty()) {
// Data-merge; mux the selected input.
auto dataMux = rewriter.create<arith::SelectOp>(
op.getLoc(), selectedIndexUnpacked.data, data[0], data[1]);
convertedOps->insert(dataMux);
// Pack the data mux with the control token.
mergeOutput = pack(rewriter, selectedIndexUnpacked.token, dataMux);
} else {
// Control-only merge; throw away the index value of the dc.merge
// operation and only forward the dc.token.
mergeOutput = selectedIndexUnpacked.token;
}
rewriter.replaceOp(op, mergeOutput);
return success();
}
};
class ControlMergeOpConversion
: public DCOpConversionPattern<handshake::ControlMergeOp> {
public:
using DCOpConversionPattern<handshake::ControlMergeOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::ControlMergeOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::ControlMergeOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (op.getDataOperands().size() != 2)
return op.emitOpError("expected two data operands");
llvm::SmallVector<Value> tokens, data;
for (auto input : adaptor.getDataOperands()) {
auto up = unpack(rewriter, input);
tokens.push_back(up.token);
if (up.data)
data.push_back(up.data);
}
// control-side
Value selectedIndex = rewriter.create<dc::MergeOp>(op.getLoc(), tokens);
auto mergeOpUnpacked = unpack(rewriter, selectedIndex);
auto selValue = mergeOpUnpacked.data;
Value dataSide = selectedIndex;
if (!data.empty()) {
// Data side mux using the selected input.
auto dataMux = rewriter.create<arith::SelectOp>(op.getLoc(), selValue,
data[0], data[1]);
convertedOps->insert(dataMux);
// Pack the data mux with the control token.
auto packed = pack(rewriter, mergeOpUnpacked.token, dataMux);
dataSide = packed;
}
// if the original op used `index` as the select operand type, we need to
// index-cast the unpacked select operand
if (op.getIndex().getType().isa<IndexType>()) {
selValue = rewriter.create<arith::IndexCastOp>(
op.getLoc(), rewriter.getIndexType(), selValue);
convertedOps->insert(selValue.getDefiningOp());
selectedIndex = pack(rewriter, mergeOpUnpacked.token, selValue);
}
rewriter.replaceOp(op, {dataSide, selectedIndex});
return success();
}
};
class SyncOpConversion : public DCOpConversionPattern<handshake::SyncOp> {
public:
using DCOpConversionPattern<handshake::SyncOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::SyncOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::SyncOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
llvm::SmallVector<Value, 4> inputTokens;
for (auto input : adaptor.getOperands())
inputTokens.push_back(unpack(rewriter, input).token);
auto syncToken = rewriter.create<dc::JoinOp>(op.getLoc(), inputTokens);
// Wrap all outputs with the synchronization token
llvm::SmallVector<Value, 4> wrappedInputs;
for (auto input : adaptor.getOperands())
wrappedInputs.push_back(pack(rewriter, syncToken, input));
rewriter.replaceOp(op, wrappedInputs);
return success();
}
};
class ConstantOpConversion
: public DCOpConversionPattern<handshake::ConstantOp> {
public:
using DCOpConversionPattern<handshake::ConstantOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::ConstantOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::ConstantOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// Wrap the constant with a token.
auto token = rewriter.create<dc::SourceOp>(op.getLoc());
auto cst =
rewriter.create<arith::ConstantOp>(op.getLoc(), adaptor.getValue());
convertedOps->insert(cst);
rewriter.replaceOp(op, pack(rewriter, token, cst));
return success();
}
};
struct UnitRateConversionPattern : public ConversionPattern {
public:
UnitRateConversionPattern(MLIRContext *context, TypeConverter &converter,
ConvertedOps *joinedOps)
: ConversionPattern(converter, MatchAnyOpTypeTag(), 1, context),
joinedOps(joinedOps) {}
using ConversionPattern::ConversionPattern;
// Generic pattern which replaces an operation by one of the same type, but
// with the in- and outputs synchronized through join semantics.
LogicalResult
matchAndRewrite(Operation *op, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) const override {
llvm::SmallVector<Value, 4> inputData;
llvm::SmallVector<Value, 4> inputTokens;
for (auto input : operands) {
auto dct = unpack(rewriter, input);
inputData.push_back(dct.data);
inputTokens.push_back(dct.token);
}
// Join the tokens of the inputs.
auto join = rewriter.create<dc::JoinOp>(op->getLoc(), inputTokens);
// Patchwork to fix bad IR design in Handshake.
auto opName = op->getName();
if (opName.getStringRef() == "handshake.select") {
opName = OperationName("arith.select", getContext());
} else if (opName.getStringRef() == "handshake.constant") {
opName = OperationName("arith.constant", getContext());
}
// Re-create the operation using the unpacked input data.
OperationState state(op->getLoc(), opName, inputData, op->getResultTypes(),
op->getAttrs(), op->getSuccessors());
Operation *newOp = rewriter.create(state);
joinedOps->insert(newOp);
// Pack the result token with the output data, and replace the uses.
llvm::SmallVector<Value> results;
for (auto result : newOp->getResults())
results.push_back(pack(rewriter, join, result));
rewriter.replaceOp(op, results);
return success();
}
mutable ConvertedOps *joinedOps;
};
class SinkOpConversionPattern
: public DCOpConversionPattern<handshake::SinkOp> {
public:
using DCOpConversionPattern<handshake::SinkOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::SinkOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::SinkOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto input = unpack(rewriter, adaptor.getOperand());
rewriter.replaceOpWithNewOp<dc::SinkOp>(op, input.token);
return success();
}
};
class SourceOpConversionPattern
: public DCOpConversionPattern<handshake::SourceOp> {
public:
using DCOpConversionPattern<handshake::SourceOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::SourceOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::SourceOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<dc::SourceOp>(op);
return success();
}
};
class BufferOpConversion : public DCOpConversionPattern<handshake::BufferOp> {
public:
using DCOpConversionPattern<handshake::BufferOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::BufferOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::BufferOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.getI32IntegerAttr(1);
rewriter.replaceOpWithNewOp<dc::BufferOp>(
op, adaptor.getOperand(), static_cast<size_t>(op.getNumSlots()));
return success();
}
};
class ReturnOpConversion : public OpConversionPattern<handshake::ReturnOp> {
public:
using OpConversionPattern<handshake::ReturnOp>::OpConversionPattern;
using OpAdaptor = typename handshake::ReturnOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::ReturnOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// Locate existing output op, Append operands to output op, and move to
// the end of the block.
auto hwModule = op->getParentOfType<hw::HWModuleOp>();
auto outputOp = *hwModule.getBodyBlock()->getOps<hw::OutputOp>().begin();
outputOp->setOperands(adaptor.getOperands());
outputOp->moveAfter(&hwModule.getBodyBlock()->back());
rewriter.eraseOp(op);
return success();
}
};
class MuxOpConversionPattern : public DCOpConversionPattern<handshake::MuxOp> {
public:
using DCOpConversionPattern<handshake::MuxOp>::DCOpConversionPattern;
using OpAdaptor = typename handshake::MuxOp::Adaptor;
LogicalResult
matchAndRewrite(handshake::MuxOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto select = unpack(rewriter, adaptor.getSelectOperand());
auto selectData = select.data;
auto selectToken = select.token;
bool isIndexType = selectData.getType().isa<IndexType>();
bool withData = !op.getResult().getType().isa<NoneType>();
llvm::SmallVector<DCTuple> inputs;
for (auto input : adaptor.getDataOperands())
inputs.push_back(unpack(rewriter, input));
Value dataMux;
Value controlMux = inputs.front().token;
// Convert the data-side mux to a sequence of arith.select operations.
// The data and control muxes are assumed one-hot and the base-case is set
// as the first input.
if (withData)
dataMux = inputs[0].data;
llvm::SmallVector<Value> controlMuxInputs = {inputs.front().token};
for (auto [i, input] :
llvm::enumerate(llvm::make_range(inputs.begin() + 1, inputs.end()))) {
if (!withData)
continue;
Value cmpIndex;
Value inputData = input.data;
Value inputControl = input.token;
if (isIndexType) {
cmpIndex = rewriter.create<arith::ConstantIndexOp>(op.getLoc(), i);
} else {
size_t width = selectData.getType().cast<IntegerType>().getWidth();
cmpIndex = rewriter.create<arith::ConstantIntOp>(op.getLoc(), i, width);
}
auto inputSelected = rewriter.create<arith::CmpIOp>(
op.getLoc(), arith::CmpIPredicate::eq, selectData, cmpIndex);
dataMux = rewriter.create<arith::SelectOp>(op.getLoc(), inputSelected,
inputData, dataMux);
// Legalize the newly created operations.
convertedOps->insert(cmpIndex.getDefiningOp());
convertedOps->insert(dataMux.getDefiningOp());
convertedOps->insert(inputSelected);
// And similarly for the control mux, by muxing the input token with a
// select value that has it's control from the original select token +
// the inputSelected value.
auto inputSelectedControl = pack(rewriter, selectToken, inputSelected);
controlMux = rewriter.create<dc::SelectOp>(
op.getLoc(), inputSelectedControl, inputControl, controlMux);
convertedOps->insert(controlMux.getDefiningOp());
}
// finally, pack the control and data side muxes into the output value.
rewriter.replaceOp(
op, pack(rewriter, controlMux, withData ? dataMux : Value{}));
return success();
}
};
static hw::ModulePortInfo getModulePortInfoHS(const TypeConverter &tc,
handshake::FuncOp funcOp) {
SmallVector<hw::PortInfo> inputs, outputs;
auto *ctx = funcOp->getContext();
auto ft = funcOp.getFunctionType();
// Add all inputs of funcOp.
for (auto [index, type] : llvm::enumerate(ft.getInputs())) {
inputs.push_back({{StringAttr::get(ctx, "in" + std::to_string(index)),
tc.convertType(type), hw::ModulePort::Direction::Input},
index,
{}});
}
// Add all outputs of funcOp.
for (auto [index, type] : llvm::enumerate(ft.getResults())) {
outputs.push_back(
{{StringAttr::get(ctx, "out" + std::to_string(index)),
tc.convertType(type), hw::ModulePort::Direction::Output},
index,
{}});
}
return hw::ModulePortInfo{inputs, outputs};
}
class FuncOpConversion : public OpConversionPattern<handshake::FuncOp> {
public:
using OpConversionPattern<handshake::FuncOp>::OpConversionPattern;
using OpAdaptor = typename handshake::FuncOp::Adaptor;
// Replaces a handshake.func with a hw.module, converting the argument and
// result types using the provided type converter.
// @mortbopet: Not a fan of converting to hw here seeing as we don't
// necessarily have hardware semantics here. But, DC doesn't define a function
// operation, and there is no "func.graph_func" or any other generic function
// operation which is a graph region...
LogicalResult
matchAndRewrite(handshake::FuncOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
ModulePortInfo ports = getModulePortInfoHS(*getTypeConverter(), op);
if (op.isExternal()) {
rewriter.create<hw::HWModuleExternOp>(
op.getLoc(), rewriter.getStringAttr(op.getName()), ports);
} else {
auto hwModule = rewriter.create<hw::HWModuleOp>(
op.getLoc(), rewriter.getStringAttr(op.getName()), ports);
auto ®ion = op->getRegions().front();
Region &moduleRegion = hwModule->getRegions().front();
rewriter.mergeBlocks(®ion.getBlocks().front(), hwModule.getBodyBlock(),
hwModule.getBodyBlock()->getArguments());
TypeConverter::SignatureConversion result(moduleRegion.getNumArguments());
(void)getTypeConverter()->convertSignatureArgs(
TypeRange(moduleRegion.getArgumentTypes()), result);
rewriter.applySignatureConversion(&moduleRegion, result);
}
rewriter.eraseOp(op);
return success();
}
};
class HandshakeToDCPass : public HandshakeToDCBase<HandshakeToDCPass> {
public:
void runOnOperation() override {
mlir::ModuleOp mod = getOperation();
auto targetModifier = [](mlir::ConversionTarget &target) {
target.addLegalDialect<hw::HWDialect, func::FuncDialect>();
};
auto patternBuilder = [&](TypeConverter &typeConverter,
handshaketodc::ConvertedOps &convertedOps,
RewritePatternSet &patterns) {
patterns.add<FuncOpConversion, ReturnOpConversion>(typeConverter,
mod.getContext());
};
LogicalResult res = runHandshakeToDC(mod, patternBuilder, targetModifier);
if (failed(res))
signalPassFailure();
}
};
} // namespace
std::unique_ptr<mlir::Pass> circt::createHandshakeToDCPass() {
return std::make_unique<HandshakeToDCPass>();
}
LogicalResult circt::handshaketodc::runHandshakeToDC(
mlir::Operation *op,
llvm::function_ref<void(TypeConverter &typeConverter,
handshaketodc::ConvertedOps &convertedOps,
RewritePatternSet &patterns)>
patternBuilder,
llvm::function_ref<void(mlir::ConversionTarget &)> configureTarget) {
// Maintain the set of operations which has been converted either through
// unit rate conversion, or as part of other conversions.
// Rationale:
// This is needed for all of the arith ops that get created as part of the
// handshake ops (e.g. arith.select for handshake.mux). There's a bit of a
// dilemma here seeing as all operations need to be converted/touched in a
// handshake.func - which is done so by UnitRateConversionPattern (when no
// other pattern applies). However, we obviously don't want to run said
// pattern on these newly created ops since they do not have handshake
// semantics.
handshaketodc::ConvertedOps convertedOps;
mlir::MLIRContext *ctx = op->getContext();
ConversionTarget target(*ctx);
target.addIllegalDialect<handshake::HandshakeDialect>();
target.addLegalDialect<dc::DCDialect>();
target.addLegalOp<mlir::ModuleOp>();
// And any user-specified target adjustments
if (configureTarget)
configureTarget(target);
// The various patterns will insert new operations into the module to
// facilitate the conversion - however, these operations must be
// distinguishable from already converted operations (which may be of the
// same type as the newly inserted operations). To do this, we mark all
// operations which have been converted as legal, and all other operations
// as illegal.
target.markUnknownOpDynamicallyLegal(
[&](Operation *op) { return convertedOps.contains(op); });
DCTypeConverter typeConverter;
RewritePatternSet patterns(ctx);
// Add handshake conversion patterns.
// Note: merge/control merge are not supported - these are non-deterministic
// operators and we do not care for them.
patterns.add<BufferOpConversion, CondBranchConversionPattern,
SinkOpConversionPattern, SourceOpConversionPattern,
MuxOpConversionPattern, ForkOpConversionPattern,
JoinOpConversion, MergeOpConversion, ControlMergeOpConversion,
ConstantOpConversion, SyncOpConversion>(ctx, typeConverter,
&convertedOps);
// ALL other single-result operations are converted via the
// UnitRateConversionPattern.
patterns.add<UnitRateConversionPattern>(ctx, typeConverter, &convertedOps);
// Build any user-specified patterns
patternBuilder(typeConverter, convertedOps, patterns);
return applyPartialConversion(op, target, std::move(patterns));
}