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

[Arc] Add Initial Cost Model #7360

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
67 changes: 67 additions & 0 deletions include/circt/Dialect/Arc/ArcCostModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//===- ArcCostModel.h -----------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_ARC_ARCCOSTMODEL_H
#define CIRCT_DIALECT_ARC_ARCCOSTMODEL_H

#include "circt/Dialect/Arc/ArcOps.h"
#include "mlir/IR/Operation.h"
#include "mlir/Pass/AnalysisManager.h"

using namespace mlir;

namespace circt {
namespace arc {

// FIXME: May be refined and we have more accurate operation costs
enum class OperationCost : size_t {
NOCOST,
NORMALCOST,
PACKCOST = 2,
EXTRACTCOST = 3,
CONCATCOST = 3,
SAMEVECTORNOSHUFFLE = 0,
SAMEVECTORSHUFFLECOST = 2,
DIFFERENTVECTORNOSHUFFLE = 2,
DIFFERENTVECTORSHUFFLECOST = 3
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not show up as cost in the public interface of the cost model (which is great)! Therefore I would move this into the *.cpp file.


class ArcCostModel {
public:
size_t getCost(Operation *op);
size_t getPackingCost() const { return packingCost; }
// This is a public interface for other passes to call
size_t getShufflingCost() const { return shufflingCost; }
size_t getVectorizeOpsBodyCost() const { return vectoroizeOpsBodyCost; }
size_t getAllVectorizeOpsCost() const { return allVectorizeOpsCost; }

private:
size_t computeOperationCost(Operation *op);

// gets the cost to pack the vectors we have some cases we need to consider:
// 1: the input is scalar so we can give it a cost of 1
// 2: the input is a result of another vector but with no shuffling so the
// is 0
// 3: the input is a result of another vector but with some shuffling so
// the cost is the (number of out of order elements) * 2
// 4: the input is a mix of some vectors:
// a) same order we multiply by 2
// b) shuffling we multiply by 3
size_t getInputVectorsCost(VectorizeOp vecOp);
size_t getShufflingCost(const ValueRange &inputVec, bool isSame = false);
DenseMap<Operation *, size_t> opCostCash;
size_t packingCost{0};
size_t shufflingCost{0};
size_t vectoroizeOpsBodyCost{0};
size_t allVectorizeOpsCost{0};
elhewaty marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace arc
} // namespace circt

#endif // CIRCT_DIALECT_ARC_ARCCOSTMODEL_H
1 change: 1 addition & 0 deletions include/circt/Dialect/Arc/ArcPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ createAddTapsPass(const AddTapsOptions &options = {});
std::unique_ptr<mlir::Pass> createAllocateStatePass();
std::unique_ptr<mlir::Pass> createArcCanonicalizerPass();
std::unique_ptr<mlir::Pass> createDedupPass();
std::unique_ptr<mlir::Pass> createDummyAnalysisTesterPass();
std::unique_ptr<mlir::Pass> createFindInitialVectorsPass();
std::unique_ptr<mlir::Pass> createGroupResetsAndEnablesPass();
std::unique_ptr<mlir::Pass>
Expand Down
18 changes: 18 additions & 0 deletions include/circt/Dialect/Arc/ArcPasses.td
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ def Dedup : Pass<"arc-dedup", "mlir::ModuleOp"> {
];
}

def DummyAnalysisTester : Pass<"arc-dummy-analysis-tester", "mlir::ModuleOp"> {
let summary = "A dymmy pass to test analysis passes";
let constructor = "circt::arc::createDummyAnalysisTesterPass()";
let dependentDialects = ["arc::ArcDialect"];
elhewaty marked this conversation as resolved.
Show resolved Hide resolved
let statistics = [
Statistic<"moduleCost", "Operation(s)",
"Number of operations in the module">,
Statistic<"packingCost", "Pack operations(s)",
"Number of scalar to vector packking in the module">,
Statistic<"shufflingCost", "Shuffle operation(s)",
"Number of shuffles done to set up the VectorizeOps">,
Statistic<"vectoroizeOpsBodyCost", "VectorizeOps Body Cost",
"Number of operations inside the body of the VectorizeOps">,
Statistic<"allVectorizeOpsCost", "All VectorizeOps Cost",
"Total Cost of all VectorizeOps in the module">
];
}

def FindInitialVectors : Pass<"arc-find-initial-vectors", "mlir::ModuleOp"> {
let summary = "Find initial groups of vectorizable ops";
let constructor = "circt::arc::createFindInitialVectorsPass()";
Expand Down
120 changes: 120 additions & 0 deletions lib/Dialect/Arc/ArcCostModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//===- ArcCostModel.cpp ---------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "circt/Dialect/Arc/ArcCostModel.h"
#include "circt/Dialect/Comb/CombOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include <algorithm>

using namespace llvm;
using namespace circt;
using namespace arc;
using namespace std;

size_t ArcCostModel::getCost(Operation *op) { return computeOperationCost(op); }

size_t ArcCostModel::computeOperationCost(Operation *op) {
if (opCostCash.count(op))
return opCostCash[op];
if (isa<circt::comb::ConcatOp>(op))
return opCostCash[op] = size_t(OperationCost::CONCATCOST);
if (isa<circt::comb::ExtractOp>(op))
return opCostCash[op] = size_t(OperationCost::EXTRACTCOST);
// We have some other functions that need to be handled in a different way
// arc::StateOp, arc::CallOp, mlir::func::CallOp and arc::VectorizeOp, each of
// these functions have bodies so the cost of the op equals the cost of its
// body.
if (isa<arc::StateOp>(op) || isa<arc::CallOp>(op) ||
isa<mlir::func::CallOp>(op)) {
size_t totalCost = 0;
const auto regions =
dyn_cast<CallOpInterface>(op).resolveCallable()->getRegions();
for (auto &region : regions)
for (auto &block : region)
for (auto &innerOp : block)
totalCost += computeOperationCost(&innerOp);
return opCostCash[op] = totalCost;
}
elhewaty marked this conversation as resolved.
Show resolved Hide resolved

if (isa<arc::VectorizeOp>(op)) {
size_t inputVecCost = getInputVectorsCost(dyn_cast<VectorizeOp>(op));
size_t vecOpBodyCost = 0;
auto regions = op->getRegions();
for (auto &region : regions)
for (auto &block : region)
for (auto &innerOp : block)
vecOpBodyCost += computeOperationCost(&innerOp);

vectoroizeOpsBodyCost += vecOpBodyCost;
allVectorizeOpsCost += inputVecCost + vecOpBodyCost;
return opCostCash[op] = inputVecCost + vecOpBodyCost;
}

return opCostCash[op] = size_t(OperationCost::NORMALCOST);
}

size_t ArcCostModel::getInputVectorsCost(VectorizeOp vecOp) {
// per VectorizeOp packing and shuffling costs
size_t localPackCost = 0;
size_t localShufflingCost = 0;
for (auto inputVec : vecOp.getInputs()) {
if (auto otherVecOp = inputVec[0].getDefiningOp<VectorizeOp>();
all_of(inputVec.begin(), inputVec.end(), [&](auto element) {
return element.template getDefiningOp<VectorizeOp>() == otherVecOp;
})) {
// This means that they came from the same vector or
// VectorizeOp == null so they are all scalars

// Check if they all scalars we multiply by the PACKCOST (SHL/R + OR)
if (!otherVecOp)
localPackCost += inputVec.size() * size_t(OperationCost::PACKCOST);
else
localShufflingCost += inputVec == otherVecOp.getResults()
? size_t(OperationCost::SAMEVECTORNOSHUFFLE)
: getShufflingCost(inputVec, true);
} else
// inputVector consists of elements from different vectotrize ops and
// may have scalars as well.
localShufflingCost += getShufflingCost(inputVec);
}
packingCost += localPackCost;
shufflingCost += localShufflingCost;
return localShufflingCost + localPackCost;
}

size_t ArcCostModel::getShufflingCost(const ValueRange &inputVec, bool isSame) {
size_t totalCost = 0;
if (isSame) {
auto vecOp = inputVec[0].getDefiningOp<VectorizeOp>();
for (auto [elem, orig] : llvm::zip(inputVec, vecOp.getResults()))
if (elem != orig)
++totalCost;

return totalCost * size_t(OperationCost::SAMEVECTORSHUFFLECOST);
}

for (size_t i = 0; i < inputVec.size(); ++i) {
auto otherVecOp = inputVec[i].getDefiningOp<VectorizeOp>();
// If the element is not a result of a vector operation then it's a result
// of a scalar operation, then it just needs to be packed into the vector.
if (!otherVecOp)
totalCost += size_t(OperationCost::PACKCOST);
else {
// If it's a result of a vector operation, then we have two cases:
// (1) Its order in `inputVec` is the same as its order in the result of
// the defining op.
// (2) the order is different.
size_t idx = find(otherVecOp.getResults().begin(),
otherVecOp.getResults().end(), inputVec[i]) -
otherVecOp.getResults().begin();
totalCost += i == idx ? size_t(OperationCost::DIFFERENTVECTORNOSHUFFLE)
: size_t(OperationCost::DIFFERENTVECTORSHUFFLECOST);
}
}
return totalCost;
}
3 changes: 3 additions & 0 deletions lib/Dialect/Arc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(CIRCT_Arc_Sources
ArcFolds.cpp
ArcOps.cpp
ArcTypes.cpp
ArcCostModel.cpp
ModelInfo.cpp
)

Expand All @@ -26,11 +27,13 @@ add_circt_dialect_library(CIRCTArc
Support

LINK_LIBS PUBLIC
CIRCTComb
CIRCTHW
CIRCTSeq
MLIRIR
MLIRInferTypeOpInterface
MLIRSideEffectInterfaces
MLIRFuncDialect
)

add_circt_library(CIRCTArcReductions
Expand Down
1 change: 1 addition & 0 deletions lib/Dialect/Arc/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_circt_dialect_library(CIRCTArcTransforms
AllocateState.cpp
ArcCanonicalizer.cpp
Dedup.cpp
DummyAnalysisTester.cpp
FindInitialVectors.cpp
GroupResetsAndEnables.cpp
InferMemories.cpp
Expand Down
72 changes: 72 additions & 0 deletions lib/Dialect/Arc/Transforms/DummyAnalysisTester.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===- DummyAnalysisTester.cpp --------------------------------------------===//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover file 😁?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I will update now. I will finish this PR even it costs my life, it's 12:36 AM here 😭

//
// 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 a dummy pass to test the analysis passes it doesn't do any thing. It
// just walks over the ops to compute some statistics, you can add any
// statistics you need to compute.
//
//===----------------------------------------------------------------------===//

#include "circt/Dialect/Arc/ArcCostModel.h"
#include "circt/Dialect/Arc/ArcPasses.h"
#include "circt/Dialect/HW/HWOps.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Pass/Pass.h"

#define DEBUG_TYPE "arc-dummy-analysis-tester"

namespace circt {
namespace arc {
#define GEN_PASS_DEF_DUMMYANALYSISTESTER
#include "circt/Dialect/Arc/ArcPasses.h.inc"
} // namespace arc
} // namespace circt

using namespace circt;
using namespace arc;

namespace {
struct DummyAnalysisTesterPass
: public arc::impl::DummyAnalysisTesterBase<DummyAnalysisTesterPass> {
void runOnOperation() override;

// You can add any statistics you need to compute here.
struct StatisticVars {
size_t moduleCost{0};
size_t packingCost{0};
size_t shufflingCost{0};
size_t vectoroizeOpsBodyCost{0};
size_t allVectorizeOpsCost{0};
};

StatisticVars statVars;
};
} // namespace

void DummyAnalysisTesterPass::runOnOperation() {
for (auto moduleOp : getOperation().getOps<hw::HWModuleOp>()) {
ArcCostModel arcCostModel;
moduleOp.walk([&](Operation *op) {
statVars.moduleCost += arcCostModel.getCost(op);
});
statVars.packingCost += arcCostModel.getPackingCost();
statVars.shufflingCost += arcCostModel.getShufflingCost();
statVars.vectoroizeOpsBodyCost += arcCostModel.getVectorizeOpsBodyCost();
statVars.allVectorizeOpsCost += arcCostModel.getAllVectorizeOpsCost();
}

moduleCost = statVars.moduleCost;
packingCost = statVars.packingCost;
shufflingCost = statVars.shufflingCost;
vectoroizeOpsBodyCost = statVars.vectoroizeOpsBodyCost;
allVectorizeOpsCost = statVars.allVectorizeOpsCost;
}

std::unique_ptr<Pass> arc::createDummyAnalysisTesterPass() {
return std::make_unique<DummyAnalysisTesterPass>();
}
Loading