Skip to content

Commit 6727339

Browse files
authored
[VectorCombine][TTI] Prevent extract/ins rewrite to GEP (#150216)
Using GEP to index into a vector is not disallowed, but not recommended. The SPIR-V backend needs to generate structured access into types, which is impossible with an untyped GEP instruction unless we add more info to the IR. Finding a solution is a work-in-progress, but in the meantime, we'd like to reduce the amount of failures. Preventing this optimizations from rewritting extract/insert instructions into a GEP helps us lower more code to SPIR-V. This change should be OK as it's only active when targeting SPIR-V and disabling a non-recommended transformation. Related to #145002
1 parent 49b0014 commit 6727339

File tree

7 files changed

+910
-0
lines changed

7 files changed

+910
-0
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,10 @@ class TargetTransformInfo {
19501950
const Function &F,
19511951
SmallVectorImpl<std::pair<StringRef, int64_t>> &LB) const;
19521952

1953+
/// Returns true if GEP should not be used to index into vectors for this
1954+
/// target.
1955+
LLVM_ABI bool allowVectorElementIndexingUsingGEP() const;
1956+
19531957
private:
19541958
std::unique_ptr<const TargetTransformInfoImplBase> TTIImpl;
19551959
};

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,8 @@ class TargetTransformInfoImplBase {
11451145
const Function &F,
11461146
SmallVectorImpl<std::pair<StringRef, int64_t>> &LB) const {}
11471147

1148+
virtual bool allowVectorElementIndexingUsingGEP() const { return true; }
1149+
11481150
protected:
11491151
// Obtain the minimum required size to hold the value (without the sign)
11501152
// In case of a vector it returns the min required size for one element.

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,10 @@ void TargetTransformInfo::collectKernelLaunchBounds(
14861486
return TTIImpl->collectKernelLaunchBounds(F, LB);
14871487
}
14881488

1489+
bool TargetTransformInfo::allowVectorElementIndexingUsingGEP() const {
1490+
return TTIImpl->allowVectorElementIndexingUsingGEP();
1491+
}
1492+
14891493
TargetTransformInfoImplBase::~TargetTransformInfoImplBase() = default;
14901494

14911495
TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}

llvm/lib/Target/SPIRV/SPIRVTargetTransformInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class SPIRVTTIImpl final : public BasicTTIImplBase<SPIRVTTIImpl> {
5959
Intrinsic::ID IID) const override;
6060
Value *rewriteIntrinsicWithAddressSpace(IntrinsicInst *II, Value *OldV,
6161
Value *NewV) const override;
62+
63+
bool allowVectorElementIndexingUsingGEP() const override { return false; }
6264
};
6365

6466
} // namespace llvm

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,8 @@ static Align computeAlignmentAfterScalarization(Align VectorAlignment,
16641664
// %1 = getelementptr inbounds i32, i32* %0, i64 0, i64 1
16651665
// store i32 %b, i32* %1
16661666
bool VectorCombine::foldSingleElementStore(Instruction &I) {
1667+
if (!TTI.allowVectorElementIndexingUsingGEP())
1668+
return false;
16671669
auto *SI = cast<StoreInst>(&I);
16681670
if (!SI->isSimple() || !isa<VectorType>(SI->getValueOperand()->getType()))
16691671
return false;
@@ -1719,6 +1721,9 @@ bool VectorCombine::foldSingleElementStore(Instruction &I) {
17191721

17201722
/// Try to scalarize vector loads feeding extractelement instructions.
17211723
bool VectorCombine::scalarizeLoadExtract(Instruction &I) {
1724+
if (!TTI.allowVectorElementIndexingUsingGEP())
1725+
return false;
1726+
17221727
Value *Ptr;
17231728
if (!match(&I, m_Load(m_Value(Ptr))))
17241729
return false;
@@ -1827,6 +1832,8 @@ bool VectorCombine::scalarizeLoadExtract(Instruction &I) {
18271832
}
18281833

18291834
bool VectorCombine::scalarizeExtExtract(Instruction &I) {
1835+
if (!TTI.allowVectorElementIndexingUsingGEP())
1836+
return false;
18301837
auto *Ext = dyn_cast<ZExtInst>(&I);
18311838
if (!Ext)
18321839
return false;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if not "SPIRV" in config.root.targets:
2+
config.unsupported = True

0 commit comments

Comments
 (0)