-
Notifications
You must be signed in to change notification settings - Fork 292
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ab27b1
[Arc] Add Initial Cost Model
elhewaty 08fead7
[Arc] Add the DummyAnalysisTester pass
elhewaty 91850f2
[Arc] Fix clang-format failure
elhewaty 6f3ce4f
[Arc] Address @fabianschuiki comments
elhewaty c14941c
[Arc] Remove DummyAnalysisTester.cpp
elhewaty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ®ion : 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 ®ion : 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//===- DummyAnalysisTester.cpp --------------------------------------------===// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover file 😁? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.