Skip to content

Commit f5f1d49

Browse files
jcranmer-inteldbudanov-cmplr
authored andcommitted
Rework the demangler to support recovering more pointer element types. (#1556)
Original commit: KhronosGroup/SPIRV-LLVM-Translator@b412ae5
1 parent f6ddd36 commit f5f1d49

File tree

11 files changed

+254
-97
lines changed

11 files changed

+254
-97
lines changed

llvm-spirv/lib/SPIRV/OCLToSPIRV.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ void OCLToSPIRVBase::visitCallReadImageWithSampler(CallInst *CI,
10101010
Function *Func = CI->getCalledFunction();
10111011
AttributeList Attrs = Func->getAttributes();
10121012
bool IsRetScalar = !CI->getType()->isVectorTy();
1013-
SmallVector<StructType *, 3> ArgStructTys;
1013+
SmallVector<Type *, 3> ArgStructTys;
10141014
getParameterTypes(CI, ArgStructTys);
10151015
mutateCallInstSPIRV(
10161016
M, CI,
@@ -1073,7 +1073,7 @@ void OCLToSPIRVBase::visitCallGetImageSize(CallInst *CI,
10731073
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
10741074
StringRef TyName;
10751075
SmallVector<StringRef, 4> SubStrs;
1076-
SmallVector<StructType *, 4> ParamTys;
1076+
SmallVector<Type *, 4> ParamTys;
10771077
getParameterTypes(CI, ParamTys);
10781078
auto IsImg = isOCLImageStructType(ParamTys[0], &TyName);
10791079
(void)IsImg;
@@ -1632,7 +1632,7 @@ static void processSubgroupBlockReadWriteINTEL(CallInst *CI,
16321632
// reads and vector block reads.
16331633
void OCLToSPIRVBase::visitSubgroupBlockReadINTEL(CallInst *CI) {
16341634
OCLBuiltinTransInfo Info;
1635-
SmallVector<StructType *, 2> ParamTys;
1635+
SmallVector<Type *, 2> ParamTys;
16361636
getParameterTypes(CI, ParamTys);
16371637
if (isOCLImageStructType(ParamTys[0]))
16381638
Info.UniqName = getSPIRVFuncName(spv::OpSubgroupImageBlockReadINTEL);
@@ -1647,7 +1647,7 @@ void OCLToSPIRVBase::visitSubgroupBlockReadINTEL(CallInst *CI) {
16471647
// instructions.
16481648
void OCLToSPIRVBase::visitSubgroupBlockWriteINTEL(CallInst *CI) {
16491649
OCLBuiltinTransInfo Info;
1650-
SmallVector<StructType *, 3> ParamTys;
1650+
SmallVector<Type *, 3> ParamTys;
16511651
getParameterTypes(CI, ParamTys);
16521652
if (isOCLImageStructType(ParamTys[0]))
16531653
Info.UniqName = getSPIRVFuncName(spv::OpSubgroupImageBlockWriteINTEL);
@@ -1768,7 +1768,7 @@ void OCLToSPIRVBase::visitSubgroupAVCWrapperBuiltinCall(
17681768
OCLSPIRVSubgroupAVCIntelBuiltinMap::find(ToMCEFName, &ToMCEOC);
17691769
assert(ToMCEOC != OpNop && "Invalid Subgroup AVC Intel built-in call");
17701770

1771-
SmallVector<StructType *, 2> ParamTys;
1771+
SmallVector<Type *, 2> ParamTys;
17721772
getParameterTypes(CI, ParamTys);
17731773

17741774
if (std::strcmp(TyKind, "payload") == 0) {
@@ -1837,7 +1837,7 @@ void OCLToSPIRVBase::visitSubgroupAVCBuiltinCallWithSampler(
18371837
mutateCallInstSPIRV(
18381838
M, CI,
18391839
[=](CallInst *, std::vector<Value *> &Args) {
1840-
SmallVector<StructType *, 4> ParamTys;
1840+
SmallVector<Type *, 4> ParamTys;
18411841
getParameterTypes(CI, ParamTys);
18421842
auto *TyIt =
18431843
std::find_if(ParamTys.begin(), ParamTys.end(), isSamplerStructTy);

llvm-spirv/lib/SPIRV/OCLTypeToSPIRV.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void OCLTypeToSPIRVBase::adaptFunctionArguments(Function *F) {
208208
return;
209209
bool Changed = false;
210210
auto Arg = F->arg_begin();
211-
SmallVector<StructType *, 4> ParamTys;
211+
SmallVector<Type *, 4> ParamTys;
212212
getParameterTypes(F, ParamTys);
213213

214214
// If we couldn't get any information from demangling, there is nothing that
@@ -217,7 +217,7 @@ void OCLTypeToSPIRVBase::adaptFunctionArguments(Function *F) {
217217
return;
218218

219219
for (unsigned I = 0; I < F->arg_size(); ++I, ++Arg) {
220-
StructType *NewTy = ParamTys[I];
220+
StructType *NewTy = dyn_cast_or_null<StructType>(ParamTys[I]);
221221
if (NewTy && NewTy->isOpaque()) {
222222
auto STName = NewTy->getStructName();
223223
if (!hasAccessQualifiedName(STName))

llvm-spirv/lib/SPIRV/OCLUtil.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,8 @@ Value *unwrapSpecialTypeInitializer(Value *V) {
13561356
return nullptr;
13571357
}
13581358

1359-
bool isSamplerStructTy(StructType *STy) {
1359+
bool isSamplerStructTy(Type *Ty) {
1360+
auto *STy = dyn_cast_or_null<StructType>(Ty);
13601361
return STy && STy->hasName() && STy->getName() == kSPR2TypeName::Sampler;
13611362
}
13621363

llvm-spirv/lib/SPIRV/OCLUtil.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ bool isEnqueueKernelBI(const StringRef MangledName);
507507
bool isKernelQueryBI(const StringRef MangledName);
508508

509509
/// Check that the type is the sampler_t
510-
bool isSamplerStructTy(StructType *Ty);
510+
bool isSamplerStructTy(Type *Ty);
511511

512512
// Checks if the binary operator is an unfused fmul + fadd instruction.
513513
bool isUnfusedMulAdd(BinaryOperator *B);

llvm-spirv/lib/SPIRV/SPIRVInternal.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "llvm/IR/Attributes.h"
5151
#include "llvm/IR/Constants.h"
5252
#include "llvm/IR/Instructions.h"
53+
#include "llvm/IR/TypedPointerType.h"
5354

5455
#include <functional>
5556
#include <utility>
@@ -969,11 +970,15 @@ std::string mangleBuiltin(StringRef UniqName, ArrayRef<Type *> ArgTypes,
969970
/// Extract the pointee types of arguments from a mangled function name. If the
970971
/// corresponding type is not a pointer to a struct type, its value will be a
971972
/// nullptr instead.
972-
void getParameterTypes(Function *F, SmallVectorImpl<StructType *> &ArgTys);
973-
inline void getParameterTypes(CallInst *CI,
974-
SmallVectorImpl<StructType *> &ArgTys) {
973+
void getParameterTypes(
974+
Function *F, SmallVectorImpl<Type *> &ArgTys,
975+
std::function<std::string(StringRef)> StructNameMapFn = nullptr);
976+
inline void getParameterTypes(CallInst *CI, SmallVectorImpl<Type *> &ArgTys) {
975977
return getParameterTypes(CI->getCalledFunction(), ArgTys);
976978
}
979+
void getParameterTypes(
980+
Function *F, SmallVectorImpl<TypedPointerType *> &ArgTys,
981+
std::function<std::string(StringRef)> StructNameMapFn = nullptr);
977982

978983
/// Mangle a function from OpenCL extended instruction set in SPIR-V friendly IR
979984
/// manner

llvm-spirv/lib/SPIRV/SPIRVToOCL.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ void SPIRVToOCLBase::visitCastInst(CastInst &Cast) {
256256

257257
void SPIRVToOCLBase::visitCallSPIRVImageQuerySize(CallInst *CI) {
258258
// Get image type
259-
SmallVector<StructType *, 4> ParamTys;
259+
SmallVector<Type *, 4> ParamTys;
260260
getParameterTypes(CI, ParamTys);
261-
StructType *ImgTy = ParamTys[0];
261+
StructType *ImgTy = cast<StructType>(ParamTys[0]);
262262
assert(ImgTy && ImgTy->isOpaque() &&
263263
"image type must be an opaque structure");
264264
StringRef ImgTyName = ImgTy->getName();
@@ -812,7 +812,7 @@ void SPIRVToOCLBase::visitCallSPIRVImageSampleExplicitLodBuiltIn(CallInst *CI,
812812
assert(CI->getCalledFunction() && "Unexpected indirect call");
813813
AttributeList Attrs = CI->getCalledFunction()->getAttributes();
814814
CallInst *CallSampledImg = cast<CallInst>(CI->getArgOperand(0));
815-
SmallVector<StructType *, 6> ParamTys;
815+
SmallVector<Type *, 6> ParamTys;
816816
getParameterTypes(CallSampledImg, ParamTys);
817817
StringRef ImageTypeName;
818818
bool IsDepthImage = false;
@@ -1322,22 +1322,8 @@ std::string SPIRVToOCLBase::translateOpaqueType(StringRef STName) {
13221322
}
13231323

13241324
void SPIRVToOCLBase::getParameterTypes(CallInst *CI,
1325-
SmallVectorImpl<StructType *> &Tys) {
1326-
::getParameterTypes(CI, Tys);
1327-
for (auto &Ty : Tys) {
1328-
if (!Ty)
1329-
continue;
1330-
StringRef STName = Ty->getStructName();
1331-
bool IsSPIRVOpaque =
1332-
Ty->isOpaque() && STName.startswith(kSPIRVTypeName::PrefixAndDelim);
1333-
1334-
if (!IsSPIRVOpaque)
1335-
continue;
1336-
1337-
std::string NewName = translateOpaqueType(STName);
1338-
if (NewName != STName)
1339-
Ty = getOrCreateOpaqueStructType(M, NewName);
1340-
};
1325+
SmallVectorImpl<Type *> &Tys) {
1326+
::getParameterTypes(CI->getCalledFunction(), Tys, translateOpaqueType);
13411327
}
13421328

13431329
void addSPIRVBIsLoweringPass(ModulePassManager &PassMgr,

llvm-spirv/lib/SPIRV/SPIRVToOCL.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,16 @@ class SPIRVToOCLBase : public InstVisitor<SPIRVToOCLBase> {
269269
std::string groupOCToOCLBuiltinName(CallInst *CI, Op OC);
270270
/// Transform SPV-IR image opaque type into OpenCL representation,
271271
/// example: spirv.Image._void_1_0_0_0_0_0_1 => opencl.image2d_wo_t
272-
std::string getOCLImageOpaqueType(SmallVector<std::string, 8> &Postfixes);
272+
static std::string
273+
getOCLImageOpaqueType(SmallVector<std::string, 8> &Postfixes);
273274
/// Transform SPV-IR pipe opaque type into OpenCL representation,
274275
/// example: spirv.Pipe._0 => opencl.pipe_ro_t
275-
std::string getOCLPipeOpaqueType(SmallVector<std::string, 8> &Postfixes);
276+
static std::string
277+
getOCLPipeOpaqueType(SmallVector<std::string, 8> &Postfixes);
276278

277-
void getParameterTypes(CallInst *CI, SmallVectorImpl<StructType *> &Tys);
279+
void getParameterTypes(CallInst *CI, SmallVectorImpl<Type *> &Tys);
278280

279-
std::string translateOpaqueType(StringRef STName);
281+
static std::string translateOpaqueType(StringRef STName);
280282

281283
/// Mutate the argument list based on (optional) image operands at position
282284
/// ImOpArgIndex. Set IsSigned according to any SignExtend/ZeroExtend Image

llvm-spirv/lib/SPIRV/SPIRVTypeScavenger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void SPIRVTypeScavenger::deduceFunctionType(Function &F) {
229229
// If the function is a mangled name, try to recover types from the Itanium
230230
// name mangling.
231231
if (F.getName().startswith("_Z")) {
232-
SmallVector<StructType *, 8> ParameterTypes;
232+
SmallVector<Type *, 8> ParameterTypes;
233233
getParameterTypes(&F, ParameterTypes);
234234
for (Argument *Arg : PointerArgs) {
235235
if (auto *Ty = ParameterTypes[Arg->getArgNo()]) {

0 commit comments

Comments
 (0)