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

[Arc] Add Initial Cost Model #7360

merged 5 commits into from
Aug 13, 2024

Conversation

elhewaty
Copy link
Member

No description provided.

@elhewaty elhewaty marked this pull request as draft July 20, 2024 11:07
@elhewaty elhewaty force-pushed the cost-model branch 2 times, most recently from aafdae3 to d36fc26 Compare July 20, 2024 11:38
@elhewaty
Copy link
Member Author

@maerhart @fabianschuiki, What is wrong with the build?

@maerhart
Copy link
Member

@maerhart @fabianschuiki, What is wrong with the build?

You are using operations from the Comb and Func dialects but not linking against them in the CMakeLists.txt file. You'd need to add them as link targets there.

@elhewaty
Copy link
Member Author

elhewaty commented Jul 20, 2024

@maerhart

in lib/Dialect/Arc/CMakeLists.txt

add_circt_dialect_library(CIRCTArc
  ${CIRCT_Arc_Sources}

  ...

  DEPENDS
  ...
  LINK_COMPONENTS
  Support

  LINK_LIBS PUBLIC
  + CIRCTComb
  ...
  ...
  + MLIRFunc
)

lib/Dialect/Arc/ArcCostModel.cpp Outdated Show resolved Hide resolved
lib/Dialect/Arc/ArcCostModel.cpp Outdated Show resolved Hide resolved
@elhewaty elhewaty force-pushed the cost-model branch 2 times, most recently from e10606e to ff08abf Compare July 23, 2024 16:14
@elhewaty elhewaty marked this pull request as ready for review July 23, 2024 16:17
@mingzheTerapines
Copy link
Contributor

mingzheTerapines commented Jul 29, 2024

I was also interested with cost model for the whole project, this looks very nice. However, it seems to be the cost for time dimension, could you add cost for memory-space dimiension, which may be determined by data type or data amount?

@elhewaty
Copy link
Member Author

I was also interested with cost model for the whole project, this looks very nice. However, it seems to be the cost for time dimension, could you add cost for memory-space dimiension, which may be determined by data type or data amount?

@mingzheTerapines Can you please explain a little further? Honestly, I don't get this

@mingzheTerapines
Copy link
Contributor

mingzheTerapines commented Aug 1, 2024

@elhewaty I mean this is calculating static cost for the model, but if we know the size of input(such as vector) we can know the dynamcia memory/register space cost. Code writen by @angelzzzzz looks like this. You can consider output of function/module is liveout.

void Node::calculateLiveOutCost(const SmallPtrSet<Value, 16> &liveOutValues,
                                float compressionRate) {
  if (liveOutValues.empty()) {
    return;
  }
  int64_t width = 0;
  for (auto value : liveOutValues) {
    width += value.getType().getIntOrFloatBitWidth();
  }
  liveOutCost = width / 64 * compressionRate;
}

@elhewaty
Copy link
Member Author

elhewaty commented Aug 3, 2024

@mingzheTerapines, It seems an interesting idea, I will try to investigate further. @fabianschuiki what do you think, will this affect the vectors' cost significantly??

I am working on minimizing the shuffling cost in vectors. and I will return to this PR.

@elhewaty
Copy link
Member Author

elhewaty commented Aug 9, 2024

@fabianschuiki @maerhart PING

include/circt/Dialect/Arc/ArcCostModel.h Outdated Show resolved Hide resolved
include/circt/Dialect/Arc/ArcPasses.td Outdated Show resolved Hide resolved
lib/Dialect/Arc/ArcCostModel.cpp Outdated Show resolved Hide resolved
Comment on lines 21 to 32
// 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.

size_t shufflingCost{0};
size_t vectoroizeOpsBodyCost{0};
size_t allVectorizeOpsCost{0};
DenseMap<Operation *, OperationCosts> opCostCash;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
DenseMap<Operation *, OperationCosts> opCostCash;
DenseMap<Operation *, OperationCosts> opCostCache;

Comment on lines 37 to 38
if (opCostCash.count(op))
return opCostCash[op];
Copy link
Contributor

Choose a reason for hiding this comment

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

You can often use iterators to combine checking if a value exists in a map, and then doing something with the value if it does exist:

Suggested change
if (opCostCash.count(op))
return opCostCash[op];
if (auto it = opCostCache.find(op); it != opCostCache.end())
return it->second;

Comment on lines 46 to 49
else if (isa<arc::VectorizeOp>(op)) {
// VectorizeOpCost = packingCost + shufflingCost + bodyCost
OperationCosts inputVecCosts =
getInputVectorsCost(dyn_cast<VectorizeOp>(op));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
else if (isa<arc::VectorizeOp>(op)) {
// VectorizeOpCost = packingCost + shufflingCost + bodyCost
OperationCosts inputVecCosts =
getInputVectorsCost(dyn_cast<VectorizeOp>(op));
else if (auto vecOp = dyn_cast<arc::VectorizeOp>(op)) {
// VectorizeOpCost = packingCost + shufflingCost + bodyCost
OperationCosts inputVecCosts = getInputVectorsCost(vecOp);

@elhewaty
Copy link
Member Author

Thanks for the review, I will update and push it now

@elhewaty
Copy link
Member Author

@fabianschuiki What is wrong with CMakeList file?

@@ -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 😭

Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

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

LGTM! And CI also seems to be happy 😃 Thanks a lot for iterating on this!

@elhewaty
Copy link
Member Author

Thanks for reviewing, Let's work on the splitting 🚀 🔥

@elhewaty elhewaty merged commit 917c940 into llvm:main Aug 13, 2024
4 checks passed
@elhewaty elhewaty deleted the cost-model branch August 13, 2024 17:52
@mingzheTerapines
Copy link
Contributor

mingzheTerapines commented Aug 14, 2024

@elhewaty Could you add an option for printing the cost explictly in Ops in the future? Maybe using dbg dialect or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants