[llvm] [SPIR-V] Fix some GEP legalization (PR #150943)

Nathan Gauër via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 29 04:57:08 PDT 2025


================
@@ -555,7 +591,97 @@ void SPIRVEmitIntrinsics::maybeAssignPtrType(Type *&Ty, Value *Op, Type *RefTy,
   Ty = RefTy;
 }
 
-Type *getGEPType(GetElementPtrInst *Ref) {
+bool SPIRVEmitIntrinsics::walkLogicalAccessChain(
+    GetElementPtrInst &GEP,
+    const std::function<void(Type *, uint64_t)> &OnStaticIndexing,
+    const std::function<void(Type *, Value *)> &OnDynamicIndexing) {
+  auto &DL = CurrF->getDataLayout();
+  Value *Src = getPointerRoot(GEP.getPointerOperand());
+  Type *CurType = deduceElementType(Src, true);
+
+  for (Value *V : GEP.indices()) {
+    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
+      uint64_t Offset = CI->getZExtValue();
+
+      do {
+        if (ArrayType *AT = dyn_cast<ArrayType>(CurType)) {
+          uint32_t EltTypeSize = DL.getTypeSizeInBits(AT->getElementType()) / 8;
+          assert(Offset < AT->getNumElements() * EltTypeSize);
+          uint64_t Index = Offset / EltTypeSize;
+          Offset = Offset - (Index * EltTypeSize);
+          CurType = AT->getElementType();
+          OnStaticIndexing(CurType, Index);
+        } else if (StructType *ST = dyn_cast<StructType>(CurType)) {
+          uint32_t StructSize = DL.getTypeSizeInBits(ST) / 8;
+          assert(Offset < StructSize);
+          const auto &STL = DL.getStructLayout(ST);
+          unsigned Element = STL->getElementContainingOffset(Offset);
+          Offset -= STL->getElementOffset(Element);
+          CurType = ST->getElementType(Element);
+          OnStaticIndexing(CurType, Element);
+        } else {
+          // Vector type indexing should not use GEP.
----------------
Keenuts wrote:

Not specially hard, but unless we have an example of codegen which requires this, don't see the value doing it in this temporary fix.
(Given we disable VectorCombine for logical SPIR-V)

https://github.com/llvm/llvm-project/pull/150943


More information about the llvm-commits mailing list