Page MenuHomePhabricator

NFC: Change getUserCost to return InstructionCost

Authored by sdesmalen on Feb 25 2021, 6:30 AM.



This patch migrates the TTI cost interfaces to return an InstructionCost.

See this patch for the introduction of the type:
See this thread for context:

Depends on D97382

Diff Detail

Unit TestsFailed

30 msx64 debian > LLVM.Analysis/CostModel/AMDGPU::br.ll
Script: -- : 'RUN: at line 1'; /mnt/disks/ssd0/agent/llvm-project/build/bin/opt -cost-model -analyze -mtriple=amdgcn-unknown-amdhsa < /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/AMDGPU/br.ll | /mnt/disks/ssd0/agent/llvm-project/build/bin/FileCheck /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/AMDGPU/br.ll
50 msx64 debian > LLVM.Analysis/CostModel/AMDGPU::shufflevector.ll
Script: -- : 'RUN: at line 1'; /mnt/disks/ssd0/agent/llvm-project/build/bin/opt -cost-model -analyze -mtriple=amdgcn-unknown-amdhsa -mcpu=gfx900 /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/AMDGPU/shufflevector.ll | /mnt/disks/ssd0/agent/llvm-project/build/bin/FileCheck -check-prefixes=GFX9,GCN,TPT /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/AMDGPU/shufflevector.ll
160 msx64 debian > LLVM.Analysis/CostModel/ARM::shuffle.ll
Script: -- : 'RUN: at line 2'; /mnt/disks/ssd0/agent/llvm-project/build/bin/opt < /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/ARM/shuffle.ll -cost-model -analyze -mtriple=thumbv8.1m.main-none-eabi -mattr=+mve.fp | /mnt/disks/ssd0/agent/llvm-project/build/bin/FileCheck /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/ARM/shuffle.ll --check-prefix=CHECK-MVE
120 msx64 debian > LLVM.Analysis/CostModel/X86::shuffle-extract_subvector.ll
Script: -- : 'RUN: at line 2'; /mnt/disks/ssd0/agent/llvm-project/build/bin/opt < /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/X86/shuffle-extract_subvector.ll -mtriple=x86_64-unknown-linux-gnu -cost-model -analyze -mattr=+sse2 | /mnt/disks/ssd0/agent/llvm-project/build/bin/FileCheck /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/X86/shuffle-extract_subvector.ll -check-prefixes=SSE,SSE2
1,530 msx64 debian > LLVM.Analysis/CostModel/X86::shuffle-insert_subvector.ll
Script: -- : 'RUN: at line 2'; /mnt/disks/ssd0/agent/llvm-project/build/bin/opt < /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/X86/shuffle-insert_subvector.ll -mtriple=x86_64-unknown-linux-gnu -cost-model -analyze -mattr=+sse2 | /mnt/disks/ssd0/agent/llvm-project/build/bin/FileCheck /mnt/disks/ssd0/agent/llvm-project/llvm/test/Analysis/CostModel/X86/shuffle-insert_subvector.ll -check-prefixes=SSE,SSE2
View Full Test Results (10 Failed)

Event Timeline

sdesmalen created this revision.Feb 25 2021, 6:30 AM
sdesmalen requested review of this revision.Feb 25 2021, 6:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 25 2021, 6:30 AM
paulwalker-arm added inline comments.Feb 25 2021, 9:41 AM
213–221 ↗(On Diff #326373)

Are these strictly necessary? I'm wondering if there's just the odd comparison somewhere whose operands can be switched.

Or is it the case that these are a temporary measure that will be removed once the current LHS becomes an InstructionCost? If so I think it's worth adding a //TODO: Remove me when...


More of an open question but is this initialisation required/preferred? Or to put another way, what is the expected meaning of InstructionCost()? Invalid? Zero?

sdesmalen updated this revision to Diff 326641.Feb 26 2021, 3:08 AM
sdesmalen marked 2 inline comments as done.

Removed operator== and operator!=.

sdesmalen added inline comments.Feb 26 2021, 3:09 AM
213–221 ↗(On Diff #326373)

I've removed them, it was indeed the odd comparison in InlineCost.cpp.


The default InstructionCost() initialises with a valid 0. I would prefer to remove that constructor in favour of initializing it with an explicit value (when this is used for an accumulating cost value) or Invalid when used as 'uninitialized'. The only issue is that this requires some more changes, because it's used in a std::pair<> in e.g. LoopVectorize.cpp, where it needs a default constructor.

Does changing the -1's to invalid's constitute a functional change? Are they always checked for? If they are just blindly added to other costs, then they would result in valid costs becoming invalid costs.


I don't like the default constructor either. I think a default constructed InstructionCost should be invalid.

dmgreen added inline comments.Feb 28 2021, 10:42 AM

This seems strange. Does it need to return an invalid cost? Or can it just return a cost of 1 for all cost kinds?

sdesmalen updated this revision to Diff 327092.Mar 1 2021, 6:15 AM

Removed possibly functional changes (-1 -> Invalid) from this patch.

Does changing the -1's to invalid's constitute a functional change? Are they always checked for? If they are just blindly added to other costs, then they would result in valid costs becoming invalid costs.

In TargetTransformInfo::getUserCost (in TargetTransformInfo.cpp) it has the following check:

InstructionCost Cost = TTIImpl->getUserCost(U, Operands, CostKind);
assert((CostKind == TTI::TCK_RecipThroughput || Cost >= 0) &&
       "TTI should not produce negative costs!");

By returning Invalid the assert no longer fails but instead Invalid is propagated, which you are right is a functional change.
I'll revert to returning -1 instead of Invalid, and will leave that to a separate (non-NFC) patch.


For these patches, I haven't really tried to understand the reason behind why things have been cost-modelled the way they have, and have instead focussed on the interface-change alone. I agree returning -1 seems odd, I don't know why this cannot be costed.

ctetreau accepted this revision.Mar 2 2021, 9:33 AM

Assuming everybody else is satisfied, LGTM

This revision is now accepted and ready to land.Mar 2 2021, 9:33 AM
paulwalker-arm accepted this revision.Mar 2 2021, 3:59 PM
This revision was landed with ongoing or failed builds.Mar 31 2021, 2:14 AM
This revision was automatically updated to reflect the committed changes.