This is an archive of the discontinued LLVM Phabricator instance.

NFC: Migrate CodeMetrics to work on InstructionCost
ClosedPublic

Authored by sdesmalen on Feb 4 2021, 6:12 AM.

Details

Summary

This patch migrates cost values and arithmetic to work on InstructionCost.
When the interfaces to TargetTransformInfo are changed, any InstructionCost
state will propagate naturally.

See this patch for the introduction of the type: https://reviews.llvm.org/D91174
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2020-November/146408.html

Diff Detail

Event Timeline

sdesmalen created this revision.Feb 4 2021, 6:12 AM
sdesmalen requested review of this revision.Feb 4 2021, 6:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 4 2021, 6:12 AM
sdesmalen added inline comments.Feb 4 2021, 6:17 AM
llvm/lib/Analysis/CodeMetrics.cpp
204

FYI, the reason for dereferencing the Optional cost value returned by getValue() unconditionally is that if the number of instructions for a basic-block cannot be costed, the cost-model is broken, which should never happen.

david-arm added inline comments.Feb 4 2021, 7:09 AM
llvm/lib/Analysis/CodeMetrics.cpp
118

Hi @sdesmalen, I'm not entirely sure why we're using an InstructionCost here to be honest, when NumInsts is an unsigned and any new costs are immediately dereferenced below at line 183 after calling TTI.getUserCost?

183

Isn't this a rolling value that needs incrementing? It's an unsigned value in CodeMetrics.h.

204

I think actually NumInstsThisBB is guaranteed to be valid because NumInstsBeforeThisBB is initialised with an unsigned, and NumInstsProxy must be valid because if it wasn't we'd have already crashed with an assert on line 183.

sdesmalen added inline comments.Feb 4 2021, 7:37 AM
llvm/lib/Analysis/CodeMetrics.cpp
118

Part of the value of InstructionCost comes when we'll add saturation, so that the value doesn't wrap for 'magical' values returned by TTI.get*Cost functions (targets may return some very high value to indicate something is expensive). By accumulating the values from TTI.get*Cost into an InstructionCost, it already prepares these cases for the future. I tried to imply that a bit with "using InstructionCost's arithmetic properties".

david-arm added inline comments.Feb 4 2021, 8:49 AM
llvm/lib/Analysis/CodeMetrics.cpp
183

Sorry ignore that comment. :) I missed the fact that it's picking up the incremented NumInstsProxy.

david-arm accepted this revision.Feb 9 2021, 2:37 AM

LGTM!

llvm/lib/Analysis/CodeMetrics.cpp
116–123

nit: Could you specifically mention saturation here as an example? Not sure about others, but it certainly helped me to understand the change more when I considered the saturation.

This revision is now accepted and ready to land.Feb 9 2021, 2:37 AM
sdesmalen updated this revision to Diff 322484.Feb 9 2021, 1:20 PM

Updated comment.

david-arm accepted this revision.Feb 10 2021, 1:45 AM

LGTM! Thanks for updating the comments!

This revision was automatically updated to reflect the committed changes.