[PATCH] D149889: [TTI] Use users of GEP to guess access type in getGEPCost

Luke Lau via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 4 12:18:38 PDT 2023


luke created this revision.
luke added reviewers: ABataev, reames, craig.topper, RKSimon, nikic, asb.
Herald added subscribers: kosarev, pmatos, StephenFan, frasercrmck, kerbowa, luismarques, apazos, sameer.abuasal, pengfei, s.egerton, dmgreen, Jim, asbirlea, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, arichardson, jvesely.
Herald added a project: All.
luke requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, MaskRay.
Herald added a project: LLVM.

Currently getGEPCost uses the target type of the GEP as a heuristic for
the type that will be accessed, to pass onto isLegalAddressingMode.
Targets use this to work out if a GEP can then be folded into the
load/store instruction that uses the GEP.
For example, on RISC-V loads and stores can have an offset added to a
base register folded into a single instruction, so the following GEP is
free:

%p = getelementptr i32, ptr %base, i32 42       ; getInstructionCost = 0

%x = load i32, ptr %p                           ; getInstructionCost = 1
------------------------------------------------------------------------

lw t0, a0(42)

However vector loads and stores cannot have an offset folded into them,
so the following GEP is costed:

%p = getelementptr <2 x i32>, ptr %base, i32 42 ; getInstructionCost = 1

%x = load <2 x i32>, ptr %p                     ; getInstructionCost = 1
------------------------------------------------------------------------

addi  a0, 42
vle32 v8, (a0)

The issue arises whenever there is a mismatch between the target type of
the GEP and the type that is actually accessed:

%p = getelementptr i32, ptr %base, i32 42       ; getInstructionCost = 0

%x = load <2 x i32>, ptr %p                     ; getInstructionCost = 1
------------------------------------------------------------------------

addi  a0, 42
vle32 v8, (a0)

Even though this GEP will result in an add instruction, because TTI
thinks it's loading an i32, it will think it can be folded and not
charge for it.

The target type can become mismatched with the memory access during
transformations, noticeably during SLP where a scalar base pointer will
be reused to perform a vector load or store.

This patch changes the interface of getGEPCost and its users to
explicitly pass in the users of the GEP, so that it can more accurately
determine the types of the memory accesses occurring.

Test cases that test the cost of a GEP or rely on the cost of a GEP have
been updated to include dummy users of the instructions, as a GEP on its
own with no users is no longer charged for.

It also introduces a check for GEPs that have all zero indices, and
costs them as free. This would ideally be included in a separate patch,
but the regression doesn't manifest itself until the changes in this
patch are made: See https://reviews.llvm.org/D38085#inline-332210


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149889

Files:
  llvm/include/llvm/Analysis/TargetTransformInfo.h
  llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
  llvm/include/llvm/CodeGen/BasicTTIImpl.h
  llvm/lib/Analysis/TargetTransformInfo.cpp
  llvm/lib/Target/X86/X86TargetTransformInfo.cpp
  llvm/lib/Transforms/Scalar/NaryReassociate.cpp
  llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
  llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
  llvm/test/Analysis/CostModel/AMDGPU/gep.ll
  llvm/test/Analysis/CostModel/ARM/gep.ll
  llvm/test/Analysis/CostModel/ARM/mve-gather-scatter-cost.ll
  llvm/test/Analysis/CostModel/RISCV/gep.ll
  llvm/test/Analysis/CostModel/X86/gep.ll
  llvm/test/Transforms/Inline/AArch64/gep-cost.ll
  llvm/test/Transforms/LICM/sink-foldable.ll
  llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
  llvm/test/Transforms/LoopVectorize/X86/pointer-runtime-checks-unprofitable.ll
  llvm/test/Transforms/LoopVectorize/X86/pr23997.ll
  llvm/test/Transforms/LoopVectorize/X86/pr54634.ll
  llvm/test/Transforms/SLPVectorizer/RISCV/gep.ll
  llvm/test/Transforms/SLPVectorizer/X86/geps-non-pow-2.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149889.519600.patch
Type: text/x-patch
Size: 336130 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230504/8109b852/attachment-0001.bin>


More information about the llvm-commits mailing list