Skip to content

Commit 26c5dad

Browse files
PiotrFusikigcbot
authored andcommitted
Fix translation of arrays from SPIRV
Lower bounds for every dimension follow counts for every dimension. Count and lower bound are optional for every dimension and possibly runtime-calculated.
1 parent 843ab06 commit 26c5dad

File tree

2 files changed

+82
-22
lines changed

2 files changed

+82
-22
lines changed

IGC/AdaptorOCL/SPIRV/SPIRVReader.cpp

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,36 @@ class SPIRVToLLVMDbgTran {
575575

576576
DIType* createMember(SPIRVExtInst* inst);
577577

578+
Metadata* createArrayBound(SPIRVId id, int64_t& intResult)
579+
{
580+
SPIRVEntry* e = BM->get<SPIRVEntry>(id);
581+
switch (e->getOpCode())
582+
{
583+
case OpConstant:
584+
intResult = static_cast<SPIRVConstant*>(e)->getZExtIntValue();
585+
return nullptr;
586+
case OpExtInst:
587+
{
588+
SPIRVExtInst* ext = static_cast<SPIRVExtInst*>(e);
589+
switch (ext->getExtOp())
590+
{
591+
case OCLExtOpDbgKind::DebugInfoNone:
592+
return nullptr;
593+
case OCLExtOpDbgKind::LocalVariable:
594+
return createLocalVar(ext);
595+
case OCLExtOpDbgKind::DbgExpr:
596+
return createExpression(ext);
597+
default:
598+
llvm_unreachable("Unknown array bound");
599+
return nullptr;
600+
}
601+
}
602+
default:
603+
llvm_unreachable("Unknown array bound");
604+
return nullptr;
605+
}
606+
}
607+
578608
DIType* createTypeArray(SPIRVExtInst* inst)
579609
{
580610
if (auto n = getExistingNode<DIType*>(inst))
@@ -584,23 +614,40 @@ class SPIRVToLLVMDbgTran {
584614

585615
auto baseType = createType(BM->get<SPIRVExtInst>(arrayType.getBaseType()));
586616
auto numDims = arrayType.getNumDims();
587-
588617
SmallVector<llvm::Metadata *, 8> subscripts;
589-
uint64_t totalCount = 1;
618+
uint64_t totalBits = getSizeInBits(baseType);
590619

591-
for (unsigned int i = 0; i != numDims; i++)
620+
for (unsigned int i = 0; i < numDims; i++)
592621
{
593-
SPIRVConstant* c = BM->get<SPIRVConstant>(arrayType.getComponentCount(i));
594-
auto val = c->getZExtIntValue();
595-
c = BM->get<SPIRVConstant>(arrayType.getComponentCount(numDims - 1));
596-
auto lowerBound = c->getZExtIntValue();
597-
subscripts.push_back(Builder.getOrCreateSubrange(lowerBound, val));
598-
totalCount *= (uint64_t)(val);
622+
int64_t loConst = 0;
623+
Metadata* loExpr = arrayType.hasLowerBounds() ? createArrayBound(arrayType.getDimLowerBound(i), loConst) : nullptr;
624+
625+
int64_t countConst = 0;
626+
Metadata* countExpr = createArrayBound(arrayType.getDimCount(i), countConst);
627+
628+
DISubrange* subrange;
629+
(void) loExpr;
630+
#if LLVM_VERSION_MAJOR >= 11
631+
if (loExpr)
632+
{
633+
if (countConst > 0)
634+
{
635+
totalBits *= static_cast<uint64_t>(countConst);
636+
countExpr = ConstantAsMetadata::get(ConstantInt::get(Type::getInt64Ty(M->getContext()), countConst));
637+
}
638+
subrange = Builder.getOrCreateSubrange(nullptr, loExpr, countExpr, nullptr);
639+
}
640+
else
641+
#endif
642+
if (countConst > 0)
643+
subrange = Builder.getOrCreateSubrange(loConst, countConst);
644+
else
645+
subrange = Builder.getOrCreateSubrange(loConst, countExpr);
646+
subscripts.push_back(subrange);
599647
}
600648

601649
DINodeArray subscriptArray = Builder.getOrCreateArray(subscripts);
602-
603-
return addMDNode(inst, Builder.createArrayType(totalCount * getSizeInBits(baseType), 0, baseType, subscriptArray));
650+
return addMDNode(inst, Builder.createArrayType(totalBits, 0, baseType, subscriptArray));
604651
}
605652

606653
DIType* createTypeVector(SPIRVExtInst* inst)

IGC/AdaptorOCL/SPIRV/libSPIRV/SPIRVDebugInfoExt.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -824,36 +824,34 @@ namespace igc_spv {
824824
class OpDebugInfoBase
825825
{
826826
public:
827-
OpDebugInfoBase(SPIRVExtInst* i)
827+
OpDebugInfoBase(SPIRVExtInst* i) : extInst(i)
828828
{
829-
extInst = i;
830829
}
831-
bool isOpDebugInfo() { return true; }
832830

833831
protected:
834-
SPIRVExtInst* extInst = nullptr;
832+
SPIRVExtInst* const extInst;
835833
template <typename T>
836-
T arg(unsigned int id)
834+
T arg(unsigned int id) const
837835
{
838836
return static_cast<T>(extInst->getArguments()[id]);
839837
}
840-
SPIRVString* str(SPIRVId id)
838+
SPIRVString* str(SPIRVId id) const
841839
{
842840
auto item = extInst->getModule()->getEntry(arg<SPIRVId>(id));
843841
if (item->isString())
844842
return static_cast<SPIRVString*>(item);
845843
else
846844
return nullptr;
847845
}
848-
uint64_t const_val(SPIRVId id)
846+
uint64_t const_val(SPIRVId id) const
849847
{
850848
auto item = extInst->getModule()->getEntry(arg<SPIRVId>(id));
851849
if (item->isConstant())
852850
return static_cast<SPIRVConstant*>(item)->getZExtIntValue();
853851
else
854852
return (uint64_t)-1;
855853
}
856-
unsigned int getNumArgs() { return extInst->getArguments().size(); }
854+
unsigned int getNumArgs() const { return extInst->getArguments().size(); }
857855
};
858856

859857
class OpCompilationUnit : OpDebugInfoBase
@@ -988,9 +986,24 @@ namespace igc_spv {
988986
{
989987
public:
990988
OpDebugTypeArray(SPIRVExtInst* extInst) : OpDebugInfoBase(extInst) {}
991-
SPIRVId getBaseType() { return arg<SPIRVId>(SPIRVDebug::Operand::TypeArray::BaseTypeIdx); }
992-
SPIRVWord getNumDims() { return (getNumArgs() - SPIRVDebug::Operand::TypeArray::ComponentCountIdx); }
993-
SPIRVId getComponentCount(unsigned int i) { return arg<SPIRVId>(i + SPIRVDebug::Operand::TypeArray::ComponentCountIdx); }
989+
SPIRVId getBaseType() const { return arg<SPIRVId>(SPIRVDebug::Operand::TypeArray::BaseTypeIdx); }
990+
bool hasLowerBounds() const
991+
{
992+
return (getNumArgs() - SPIRVDebug::Operand::TypeArray::ComponentCountIdx) % 2 == 0;
993+
}
994+
SPIRVWord getNumDims() const
995+
{
996+
unsigned int n = getNumArgs() - SPIRVDebug::Operand::TypeArray::ComponentCountIdx;
997+
return hasLowerBounds() ? n / 2 : n;
998+
}
999+
SPIRVId getDimCount(unsigned int i) const { return arg<SPIRVId>(SPIRVDebug::Operand::TypeArray::ComponentCountIdx + i); }
1000+
SPIRVId getDimLowerBound(unsigned int i) const
1001+
{
1002+
if (hasLowerBounds()) {
1003+
return arg<SPIRVId>(SPIRVDebug::Operand::TypeArray::ComponentCountIdx + getNumDims() + i);
1004+
}
1005+
return SPIRVID_INVALID;
1006+
}
9941007
};
9951008

9961009
class OpDebugTypeVector : OpDebugInfoBase

0 commit comments

Comments
 (0)