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

[Handshake] Add booleans to mem dependence attr #231

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
12 changes: 8 additions & 4 deletions include/dynamatic/Dialect/Handshake/HandshakeAttributes.td
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,29 @@ def MemDependenceAttr : Handshake_Attr<"MemDependence", "dep"> {
The dependency is furthermore characterized by the loop depth at which the
dependency is (`loopDepth`) and a list of dependence components
(`components`) whose size indicates the number of commom loops surrounding
both operations.
both operations. Also, there is a boolean (`isActive`) that demonstrates
whether the dependency needs to be considered. An example of when there is
no need to consider a dependency is when it is guaranteed by data dependency.
}];

let parameters = (ins
"::mlir::StringAttr":$dstAccess,
"unsigned":$loopDepth,
ArrayRefParameter<"::dynamatic::handshake::DependenceComponentAttr">:$components
ArrayRefParameter<"::dynamatic::handshake::DependenceComponentAttr">:$components,
"bool":$isActive
);

let builders = [
AttrBuilder<(ins "::mlir::StringRef":$dstAccess,
"unsigned":$loopDepth,
"::mlir::ArrayRef<::mlir::affine::DependenceComponent>":$components), [{
"::mlir::ArrayRef<::mlir::affine::DependenceComponent>":$components,
"bool":$isActive), [{
SmallVector<::dynamatic::handshake::DependenceComponentAttr> compAttrs;
for (auto &comp : components)
compAttrs.push_back(::dynamatic::handshake::DependenceComponentAttr::get(
context, comp.lb, comp.ub));
return $_get(context, ::mlir::StringAttr::get(context, dstAccess),
loopDepth, compAttrs);
loopDepth, compAttrs, isActive);
}]>
];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
//===- HandshakeAnalyzeLSQUsage.h - LSQ flow analysis -----------*- C++ -*-===//
//===- HandshakeInactivateEnforcedDeps.h - Inactivate Deps ------*- C++ -*-===//
//
// Dynamatic is 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 file declares the --handshake-analyze-lsq-usage pass.
// This file declares the --handshake-inactiveate-enfroced-deps pass.
//
//===----------------------------------------------------------------------===//

#ifndef DYNAMATIC_TRANSFORMS_HANDSHAKEANALYZELSQUSAGE_H
#define DYNAMATIC_TRANSFORMS_HANDSHAKEANALYZELSQUSAGE_H
#ifndef DYNAMATIC_TRANSFORMS_HANDSHAKEINACTIVATEENFORCEDDEPS_H
#define DYNAMATIC_TRANSFORMS_HANDSHAKEINACTIVATEENFORCEDDEPS_H

#include "dynamatic/Support/DynamaticPass.h"

namespace dynamatic {

#define GEN_PASS_DECL_HANDSHAKEANALYZELSQUSAGE
#define GEN_PASS_DEF_HANDSHAKEANALYZELSQUSAGE
#define GEN_PASS_DECL_HANDSHAKEINACTIVATEENFORCEDDEPS
#define GEN_PASS_DEF_HANDSHAKEINACTIVATEENFORCEDDEPS
#include "dynamatic/Transforms/Passes.h.inc"

std::unique_ptr<dynamatic::DynamaticPass> createHandshakeAnalyzeLSQUsage();
std::unique_ptr<dynamatic::DynamaticPass>
createHandshakeInactivateEnforcedDeps();

} // namespace dynamatic

#endif // DYNAMATIC_TRANSFORMS_HANDSHAKEANALYZELSQUSAGE_H
#endif // DYNAMATIC_TRANSFORMS_HANDSHAKEINACTIVATEENFORCEDDEPS_H
2 changes: 1 addition & 1 deletion include/dynamatic/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "dynamatic/Transforms/ForceMemoryInterface.h"
#include "dynamatic/Transforms/FuncMaximizeSSA.h"
#include "dynamatic/Transforms/FuncSetArgNames.h"
#include "dynamatic/Transforms/HandshakeAnalyzeLSQUsage.h"
#include "dynamatic/Transforms/HandshakeInactivateEnforcedDeps.h"
#include "dynamatic/Transforms/HandshakeCanonicalize.h"
#include "dynamatic/Transforms/HandshakeHoistExtInstances.h"
#include "dynamatic/Transforms/HandshakeInferBasicBlocks.h"
Expand Down
17 changes: 9 additions & 8 deletions include/dynamatic/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,23 @@ def FuncSetArgNames : DynamaticPass<"func-set-arg-names"> {
// Handshake passes
//===----------------------------------------------------------------------===//

def HandshakeAnalyzeLSQUsage : DynamaticPass<"handshake-analyze-lsq-usage"> {
let summary = "Analyzes memory accesses to LSQs to find unnecessary ones";
def HandshakeInactivateEnforcedDeps :
DynamaticPass<"handshake-inactivate-enforced-deps"> {
let summary =
"Marks the enforced dependencies as `inactive`";
let description = [{
Performs flow analysis to identify WAR dependencies between memory accesses
that are ensured by the circuit's semantics and which therefore do not need
to be enforced by an LSQ. This allows to reduce the number of memory ports
that go to LSQs, thereby improving the circuit's performance and area thanks
to LSQ size reduction (or elimination, in some instances). The pass does not
modify the IR's structure, it only sets `handshake::MemInterfaceAttr`
attributes on memory ports to encode the analysis' results.
to be enforced by an LSQ. It also finds the WAW dependencies between an
operation and itself. The pass then modifies the IR's structure by setting
these dependencies as inactive and determining
`handshake::MemInterfaceAttr` attributes on memory ports at the end.

The pass requires that all eligible operations within Handshake functions
are tagged with the basic block they belong to, and that all memory access
operations are uniquely named.
}];
let constructor = "dynamatic::createHandshakeAnalyzeLSQUsage()";
let constructor = "dynamatic::createHandshakeInactivateEnforcedDeps()";
}

def HandshakeCanonicalize : DynamaticPass<"handshake-canonicalize"> {
Expand Down
23 changes: 21 additions & 2 deletions lib/Dialect/Handshake/HandshakeAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ static ParseResult parseDependenceComponent(AsmParser &odsParser,
void MemDependenceAttr::print(AsmPrinter &odsPrinter) const {
// Print destination memory access and loop depth
odsPrinter << "<\"" << getDstAccess().str() << "\" (" << getLoopDepth()
<< ")";
<< ") ";
std::string isActiveStr = "inactive";
if (getIsActive())
isActiveStr = "active";

odsPrinter << "\"" << isActiveStr << "\"";

// Print dependence components, if present
auto components = getComponents();
Expand Down Expand Up @@ -122,6 +127,19 @@ Attribute MemDependenceAttr::parse(AsmParser &odsParser, Type odsType) {
odsParser.parseRParen())
return nullptr;

// Parse isActive
std::string boolStr;
if (odsParser.parseString(&boolStr))
return nullptr;
bool isActive;
if (boolStr == "active")
isActive = true;
else if (boolStr == "inactive")
isActive = false;
rpirayadi marked this conversation as resolved.
Show resolved Hide resolved
else
return nullptr;


// Parse dependence components if present
SmallVector<DependenceComponentAttr> components;
if (!odsParser.parseOptionalLSquare()) {
Expand All @@ -147,7 +165,8 @@ Attribute MemDependenceAttr::parse(AsmParser &odsParser, Type odsType) {

if (odsParser.parseGreater())
return nullptr;
return MemDependenceAttr::get(ctx, dstAccess, loopDepth, components);
return MemDependenceAttr::get(ctx, dstAccess, loopDepth, components,
isActive);
}

//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion lib/Dialect/Handshake/MemoryInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool MemoryOpLowering::renameDependencies(Operation *topLevelOp) {
if (opWasReplaced) {
StringAttr newName = StringAttr::get(ctx, replacedName->second);
newMemDeps.push_back(MemDependenceAttr::get(
ctx, newName, oldDep.getLoopDepth(), oldDep.getComponents()));
ctx, newName, oldDep.getLoopDepth(), oldDep.getComponents(), oldDep.getIsActive()));
} else {
newMemDeps.push_back(oldDep);
}
Expand Down
11 changes: 6 additions & 5 deletions lib/Support/CFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ static GIIDStatus isGIIDRec(Value predecessor, OpOperand &oprd,
[&](handshake::ConditionalBranchOp condBrOp) {
// The data operand or the condition operand must depend on the
// predecessor
return foldGIIDStatusAnd(recurse, condBrOp->getOperands());
return foldGIIDStatusOr(recurse, condBrOp->getOperands());
})
.Case<handshake::MergeOp, handshake::ControlMergeOp>([&](auto) {
// The data input on the path must depend on the predecessor
Expand Down Expand Up @@ -632,10 +632,11 @@ static GIIDStatus isGIIDRec(Value predecessor, OpOperand &oprd,
handshake::BranchOp, handshake::AddIOp, handshake::AndIOp,
handshake::CmpIOp, handshake::DivSIOp, handshake::DivUIOp,
handshake::ExtSIOp, handshake::ExtUIOp, handshake::MulIOp,
handshake::OrIOp, handshake::ShLIOp, handshake::ShRUIOp,
handshake::SubIOp, handshake::TruncIOp, handshake::XOrIOp,
handshake::AddFOp, handshake::CmpFOp, handshake::DivFOp,
handshake::MulFOp, handshake::SubFOp>([&](auto) {
handshake::OrIOp, handshake::AddFOp, handshake::CmpFOp,
handshake::DivFOp, handshake::MulFOp, handshake::ShLIOp,
handshake::ShRSIOp, handshake::ShRUIOp, handshake::SubFOp,
handshake::SubIOp, handshake::TruncIOp, handshake::TruncFOp,
handshake::XOrIOp, handshake::SIToFPOp, handshake::FPToSIOp>([&](auto) {
// At least one operand must depend on the predecessor
return foldGIIDStatusOr(recurse, defOp->getOperands());
})
Expand Down
2 changes: 1 addition & 1 deletion lib/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ add_dynamatic_library(DynamaticTransforms
ForceMemoryInterface.cpp
FuncMaximizeSSA.cpp
FuncSetArgNames.cpp
HandshakeAnalyzeLSQUsage.cpp
HandshakeInactivateEnforcedDeps.cpp
HandshakeCanonicalize.cpp
HandshakeHoistExtInstances.cpp
HandshakeMaterialize.cpp
Expand Down
Loading
Loading