Skip to content

Commit 5165b68

Browse files
committed
[LLVM-15] Recover non-struct pointee types from demangling func args
The StructType* overload of getParameterTypes returned nullptr for pointer arguments whose pointee type is a primitive (e.g. bfloat16), preventing SPIRVTypeScavenger from inferring the correct type for such arguments. Add a Type* overload as the primary implementation and forward the existing StructType* overload to it for backward compatibility. AI-assisted: Claude Sonnet 4.6 (commercial SaaS)
1 parent 37d613a commit 5165b68

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

lib/SPIRV/SPIRVInternal.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,9 +998,12 @@ bool containsUnsignedAtomicType(StringRef Name);
998998
std::string mangleBuiltin(StringRef UniqName, ArrayRef<Type *> ArgTypes,
999999
BuiltinFuncMangleInfo *BtnInfo);
10001000

1001-
/// Extract the pointee types of arguments from a mangled function name. If the
1002-
/// corresponding type is not a pointer to a struct type, its value will be a
1003-
/// nullptr instead.
1001+
/// Extract the pointee types of arguments from a mangled function name,
1002+
/// including non-struct types such as bfloat16. Unknown types are nullptr.
1003+
void getParameterTypes(Function *F, SmallVectorImpl<Type *> &ArgTys);
1004+
1005+
/// Struct-typed variant of getParameterTypes. Non-struct pointee types (e.g.
1006+
/// bfloat) are returned as nullptr.
10041007
void getParameterTypes(Function *F, SmallVectorImpl<StructType *> &ArgTys);
10051008
inline void getParameterTypes(CallInst *CI,
10061009
SmallVectorImpl<StructType *> &ArgTys) {

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()]) {

lib/SPIRV/SPIRVUtil.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ static std::string demangleBuiltinOpenCLTypeName(StringRef MangledStructName) {
779779
return LlvmStructName;
780780
}
781781

782-
void getParameterTypes(Function *F, SmallVectorImpl<StructType *> &ArgTys) {
782+
void getParameterTypes(Function *F, SmallVectorImpl<Type *> &ArgTys) {
783783
// If there's no mangled name, we can't do anything. Also, if there's no
784784
// parameters, do nothing.
785785
StringRef Name = F->getName();
@@ -799,7 +799,7 @@ void getParameterTypes(Function *F, SmallVectorImpl<StructType *> &ArgTys) {
799799
assert(!HasSret && &Arg == F->getArg(0) &&
800800
"sret parameter should only appear on the first argument");
801801
HasSret = true;
802-
ArgTys.push_back(dyn_cast<StructType>(Ty));
802+
ArgTys.push_back(Ty);
803803
} else {
804804
ArgTys.push_back(nullptr);
805805
}
@@ -866,7 +866,7 @@ void getParameterTypes(Function *F, SmallVectorImpl<StructType *> &ArgTys) {
866866
}
867867

868868
for (StringRef Arg : ArgParams) {
869-
StructType *Pointee = nullptr;
869+
Type *Pointee = nullptr;
870870
if (Arg.endswith("*") && !Arg.endswith("**")) {
871871
// Strip off address space and other qualifiers.
872872
StringRef MangledStructName = Arg.split(' ').first;
@@ -897,6 +897,13 @@ void getParameterTypes(Function *F, SmallVectorImpl<StructType *> &ArgTys) {
897897
free(Buf);
898898
}
899899

900+
void getParameterTypes(Function *F, SmallVectorImpl<StructType *> &ArgTys) {
901+
SmallVector<Type *, 8> Tys;
902+
getParameterTypes(F, Tys);
903+
for (Type *T : Tys)
904+
ArgTys.push_back(dyn_cast_or_null<StructType>(T));
905+
}
906+
900907
// This is a transitional helper function to fill in mangling information for
901908
// mangleBuiltin while all the calls to mutateCallInst are being transitioned.
902909
static void typeMangle(BuiltinFuncMangleInfo *Mangle, ArrayRef<Value *> Args) {

0 commit comments

Comments
 (0)