Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buffer operation to Handshake dialect #39

Merged
merged 7 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions include/circt/Dialect/Handshake/HandshakeOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,33 @@ def ReturnOp : Handshake_Op<"return", [Terminator]> {
let parser = [{ return ::parse$cppClass(parser, result); }];
}

def BufferOp : Handshake_Op<"buffer", [NoSideEffect]> {
let summary = "buffer operation";

let description = [{
The "handshake.buffer" operation represents a buffer operation. $slots
must be an unsigned integer larger than 0. $sequantial=True indicates a
hanchenye marked this conversation as resolved.
Show resolved Hide resolved
nontransparent buffer, while $sequantial=False indicates a transparent
buffer.
}];

let arguments = (ins AnyType, BoolAttr:$sequential, BoolAttr:$control,
Confined<I32Attr, [IntMinValue<0>]>:$slots);
let results = (outs AnyType);

let extraClassDeclaration = [{
bool isSequential() {
return getAttrOfType<BoolAttr>("sequential").getValue();
}
bool isControl() {
return getAttrOfType<BoolAttr>("control").getValue();
}
APInt getNumSlots() {
return getAttrOfType<IntegerAttr>("slots").getValue();
}
}];
}

def ForkOp : Handshake_Op<"fork", [NoSideEffect]> {
let summary = "fork operation";

Expand Down
102 changes: 84 additions & 18 deletions lib/Conversion/StandardToHandshake/StandardToHandshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ void replaceCallOps(handshake::FuncOp f, ConversionPatternRewriter &rewriter) {
}
}

struct DFCanonicalizePattern : public ConversionPattern {
struct HandshakeCanonicalizePattern : public ConversionPattern {
using ConversionPattern::ConversionPattern;
LogicalResult match(Operation *op) const override {
// Ignore forks, ops without results, and branches (ops with succ blocks)
Expand Down Expand Up @@ -1326,8 +1326,70 @@ struct FuncOpLowering : public OpConversionPattern<mlir::FuncOp> {
};

namespace {
struct DFRemoveBlockPass
: public PassWrapper<DFRemoveBlockPass, OperationPass<handshake::FuncOp>> {
struct HandshakeInsertBufferPass
: public PassWrapper<HandshakeInsertBufferPass,
OperationPass<handshake::FuncOp>> {

DenseMap<Operation *, bool> opVisited;
DenseMap<Operation *, bool> opOnStack;

/// DFS-based graph cycle detection and naive buffer insertion. Exactly one
/// 2-slot non-transparent buffer will be inserted into each graph cycle.
void insertBufferOp(Operation *op, OpBuilder &builder) {
// Mark operation as visited and push into the stack.
opVisited[op] = true;
opOnStack[op] = true;

// Traverse all uses of the current operation.
for (auto &operand : op->getUses()) {
auto user = operand.getOwner();

// If graph cycle detected, insert a BufferOp into the edge.
if (opOnStack[user] && !isa<handshake::BufferOp>(op) &&
!isa<handshake::BufferOp>(user)) {
auto value = operand.get();

builder.setInsertionPointAfter(op);
auto bufferOp = builder.create<handshake::BufferOp>(
op->getLoc(), value.getType(), value, /*sequential=*/true,
/*control=*/value.getType().isa<NoneType>(),
/*slots=*/APInt(32, 2));
value.replaceUsesWithIf(
bufferOp,
function_ref<bool(OpOperand &)>([](OpOperand &operand) -> bool {
return !isa<handshake::BufferOp>(operand.getOwner());
}));
}
// For unvisited operations, recursively call insertBufferOp() method.
else if (!opVisited[user])
insertBufferOp(user, builder);
}
// Pop operation out of the stack.
opOnStack[op] = false;
}

void runOnOperation() override {
auto f = getOperation();
for (auto &block : f) {
for (auto &op : block) {
opVisited[&op] = false;
opOnStack[&op] = false;
}
}
// Traverse each use of each argument of the entry block.
auto builder = OpBuilder(f.getContext());
for (auto &arg : f.getBody().front().getArguments()) {
for (auto &operand : arg.getUses()) {
if (!opVisited[operand.getOwner()])
insertBufferOp(operand.getOwner(), builder);
}
}
}
};

struct HandshakeRemoveBlockPass
: public PassWrapper<HandshakeRemoveBlockPass,
OperationPass<handshake::FuncOp>> {
void runOnOperation() override {
auto funcOp = getOperation();
auto &entryBlock = funcOp.getBody().front().getOperations();
Expand All @@ -1350,7 +1412,8 @@ struct DFRemoveBlockPass
}
};

struct DFPass : public PassWrapper<DFPass, OperationPass<ModuleOp>> {
struct HandshakePass
: public PassWrapper<HandshakePass, OperationPass<ModuleOp>> {
void runOnOperation() override {
ModuleOp m = getOperation();

Expand All @@ -1365,8 +1428,9 @@ struct DFPass : public PassWrapper<DFPass, OperationPass<ModuleOp>> {
}
};

struct DFCanonicalizePass
: public PassWrapper<DFCanonicalizePass, OperationPass<handshake::FuncOp>> {
struct HandshakeCanonicalizePass
: public PassWrapper<HandshakeCanonicalizePass,
OperationPass<handshake::FuncOp>> {
void runOnOperation() override {
auto Op = getOperation();
OpBuilder builder(Op);
Expand All @@ -1388,8 +1452,8 @@ struct DFCanonicalizePass
// [](Operation *op) { return op->hasOneUse(); }));

// OwningRewritePatternList patterns;
// patterns.insert<DFCanonicalizePattern>("std.muli", 1, m.getContext());
// applyPatternsAndFoldGreedily(
// patterns.insert<HandshakeCanonicalizePattern>("std.muli", 1,
// m.getContext()); applyPatternsAndFoldGreedily(

// if (failed(applyPartialConversion(m, target, patterns)))
// signalPassFailure();
Expand All @@ -1400,8 +1464,8 @@ struct DFCanonicalizePass
// Temporary: print resources (operation counts)
namespace {

struct DFAnalysisPass
: public PassWrapper<DFAnalysisPass, OperationPass<ModuleOp>> {
struct HandshakeAnalysisPass
: public PassWrapper<HandshakeAnalysisPass, OperationPass<ModuleOp>> {
void runOnOperation() override {
ModuleOp m = getOperation();

Expand Down Expand Up @@ -1443,12 +1507,14 @@ struct DFAnalysisPass
} // namespace

void handshake::registerStandardToHandshakePasses() {
PassRegistration<DFAnalysisPass>("analyze-dataflow",
"Print resource (operation) statistics");
PassRegistration<DFPass>("create-dataflow",
"Convert standard MLIR into dataflow IR");
PassRegistration<DFCanonicalizePass>("canonicalize-dataflow",
"Canonicalize handshake IR");
PassRegistration<DFRemoveBlockPass>("remove-block-structure",
"Remove block structure in handshake IR");
PassRegistration<HandshakeAnalysisPass>(
"analyze-dataflow", "Print resource (operation) statistics");
PassRegistration<HandshakePass>("create-dataflow",
"Convert standard MLIR into dataflow IR");
PassRegistration<HandshakeCanonicalizePass>("canonicalize-dataflow",
"Canonicalize handshake IR");
PassRegistration<HandshakeRemoveBlockPass>(
"remove-block-structure", "Remove block structure in handshake IR");
PassRegistration<HandshakeInsertBufferPass>(
"handshake-insert-buffer", "Insert buffers to break graph cycles.");
}
63 changes: 63 additions & 0 deletions test/Conversion/StandardToHandshake/test_insert_buffer.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// NOTE: Assertions have been autogenerated by utils/update_mlir_test_checks.py
// RUN: circt-opt -handshake-insert-buffer %s | FileCheck %s

module {
handshake.func @simple_loop(%arg0: none, ...) -> none {
%0 = "handshake.branch"(%arg0) {control = true} : (none) -> none
"handshake.terminator"()[^bb1] : () -> ()
^bb1: // pred: ^bb0
%1:2 = "handshake.control_merge"(%0) {control = true} : (none) -> (none, index)
%2:3 = "handshake.fork"(%1#0) {control = true} : (none) -> (none, none, none)
"handshake.sink"(%1#1) : (index) -> ()
%3 = "handshake.constant"(%2#1) {value = 1 : index} : (none) -> index
%4 = "handshake.constant"(%2#0) {value = 42 : index} : (none) -> index
%5 = "handshake.branch"(%2#2) {control = true} : (none) -> none
%6 = "handshake.branch"(%3) {control = false} : (index) -> index
%7 = "handshake.branch"(%4) {control = false} : (index) -> index
"handshake.terminator"()[^bb2] : () -> ()
^bb2: // 2 preds: ^bb1, ^bb3
%8 = "handshake.mux"(%11#1, %22, %7) : (index, index, index) -> index
%9:2 = "handshake.fork"(%8) {control = false} : (index) -> (index, index)

// CHECK: %10:2 = "handshake.control_merge"(%28, %5) {control = true} : (none, none) -> (none, index)
// CHECK-NEXT: %11 = "handshake.buffer"(%10#0) {control = true, sequential = true, slots = 2 : i32} : (none) -> none
%10:2 = "handshake.control_merge"(%23, %5) {control = true} : (none, none) -> (none, index)

// CHECK: %12:2 = "handshake.fork"(%10#1) {control = false} : (index) -> (index, index)
// CHECK-NEXT: %13 = "handshake.buffer"(%12#1) {control = false, sequential = true, slots = 2 : i32} : (index) -> index
%11:2 = "handshake.fork"(%10#1) {control = false} : (index) -> (index, index)
%12 = "handshake.mux"(%11#0, %24, %6) : (index, index, index) -> index

// CHECK: %15:2 = "handshake.fork"(%14) {control = false} : (index) -> (index, index)
// CHECK-NEXT: %16 = "handshake.buffer"(%15#1) {control = false, sequential = true, slots = 2 : i32} : (index) -> index
// CHECK-NEXT: %17 = "handshake.buffer"(%15#0) {control = false, sequential = true, slots = 2 : i32} : (index) -> index
%13:2 = "handshake.fork"(%12) {control = false} : (index) -> (index, index)
%14 = cmpi "slt", %13#1, %9#1 : index
%15:3 = "handshake.fork"(%14) {control = false} : (i1) -> (i1, i1, i1)
%trueResult, %falseResult = "handshake.conditional_branch"(%15#2, %9#0) {control = false} : (i1, index) -> (index, index)
"handshake.sink"(%falseResult) : (index) -> ()
%trueResult_0, %falseResult_1 = "handshake.conditional_branch"(%15#1, %10#0) {control = true} : (i1, none) -> (none, none)
%trueResult_2, %falseResult_3 = "handshake.conditional_branch"(%15#0, %13#0) {control = false} : (i1, index) -> (index, index)
"handshake.sink"(%falseResult_3) : (index) -> ()
"handshake.terminator"()[^bb3, ^bb4] : () -> ()
^bb3: // pred: ^bb2
%16 = "handshake.merge"(%trueResult_2) : (index) -> index
%17 = "handshake.merge"(%trueResult) : (index) -> index
%18:2 = "handshake.control_merge"(%trueResult_0) {control = true} : (none) -> (none, index)
%19:2 = "handshake.fork"(%18#0) {control = true} : (none) -> (none, none)
"handshake.sink"(%18#1) : (index) -> ()
%20 = "handshake.constant"(%19#0) {value = 1 : index} : (none) -> index
%21 = addi %16, %20 : index

// CHECK: %26 = "handshake.branch"(%21) {control = false} : (index) -> index
// CHECK-NEXT: %27 = "handshake.buffer"(%26) {control = false, sequential = true, slots = 2 : i32} : (index) -> index
%22 = "handshake.branch"(%17) {control = false} : (index) -> index
%23 = "handshake.branch"(%19#1) {control = true} : (none) -> none
%24 = "handshake.branch"(%21) {control = false} : (index) -> index
"handshake.terminator"()[^bb2] : () -> ()
^bb4: // pred: ^bb2
%25:2 = "handshake.control_merge"(%falseResult_1) {control = true} : (none) -> (none, index)
"handshake.sink"(%25#1) : (index) -> ()
handshake.return %25#0 : none
}
}