From 68580dd6fbdaaf65d73d83dcddc0f9b74362ee56 Mon Sep 17 00:00:00 2001 From: Alex Voicu Date: Thu, 23 Apr 2026 17:15:09 +0100 Subject: [PATCH] Correctly reverse translate GEP into non flat aggregate coming from G_PTR_ADD. --- lib/SPIRV/SPIRVReader.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index 10328cfb4..cb7bf30ce 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -2598,6 +2598,18 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F, if (auto GV = dyn_cast(CE->getOperand(0)->stripPointerCasts())) BaseTy = GV->getValueType(); + if (Index.size() == 2 && + !GetElementPtrInst::getIndexedType(BaseTy, Index)) { + // This might've come from a G_PTR_ADD, but because we've retrieved the + // full structured type we have to form the correct GEP. + Type *ElemTy = BaseTy; + APInt Residual = cast(Index[1])->getValue(); + Index.clear(); + llvm::transform( + M->getDataLayout().getGEPIndicesForOffset(ElemTy, Residual), + std::back_inserter(Index), + [this](auto &&Idx) { return ConstantInt::get(*Context, Idx); }); + } V = ConstantExpr::getGetElementPtr(BaseTy, CT, Index, IsInbound); } return mapValue(BV, V);