Skip to content

Commit f4b76fc

Browse files
jcranmer-intelpreethi-intel
authored andcommitted
Correctly identify mangling of varargs in the demangler. (intel#1642)
Co-authored-by: Sven van Haastregt <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@a92e719
1 parent dca2cfe commit f4b76fc

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

llvm-spirv/lib/SPIRV/SPIRVUtil.cpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -896,17 +896,37 @@ void getParameterTypes(Function *F, SmallVectorImpl<TypedPointerType *> &ArgTys,
896896
if (!RootNode)
897897
return;
898898

899+
// Get the parameter list. If the function is a vararg function, drop the last
900+
// parameter.
901+
NodeArray Params = RootNode->getParams();
902+
if (F->isVarArg()) {
903+
bool HasVarArgParam = false;
904+
if (!Params.empty()) {
905+
if (auto *Name = dyn_cast<NameType>(Params[Params.size() - 1])) {
906+
if (stringify(Name) == "...")
907+
HasVarArgParam = true;
908+
}
909+
}
910+
if (HasVarArgParam) {
911+
Params = NodeArray(Params.begin(), Params.size() - 1);
912+
} else {
913+
LLVM_DEBUG(dbgs() << "[getParameterTypes] function " << MangledName
914+
<< " was expected to have a varargs parameter\n");
915+
return;
916+
}
917+
}
918+
899919
// Sanity check that the name mangling matches up to the expected number of
900920
// arguments.
901-
if (RootNode->getParams().size() != (size_t)(ArgTys.end() - ArgIter)) {
921+
if (Params.size() != (size_t)(ArgTys.end() - ArgIter)) {
902922
LLVM_DEBUG(dbgs() << "[getParameterTypes] function " << MangledName
903-
<< " appears to have " << RootNode->getParams().size()
923+
<< " appears to have " << Params.size()
904924
<< " arguments but has " << (ArgTys.end() - ArgIter)
905925
<< "\n");
906926
return;
907927
}
908928

909-
for (auto *ParamType : RootNode->getParams()) {
929+
for (auto *ParamType : Params) {
910930
Type *ArgTy = F->getArg(ArgIter - ArgTys.begin())->getType();
911931
TypedPointerType *PointeeTy = parseNode(M, ParamType, GetStructType);
912932
if (ArgTy->isPointerTy() && PointeeTy == nullptr) {

llvm-spirv/test/type-scavenger/varargs.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ target triple = "spir-unknown-unknown"
2020
; Function Attrs: nounwind
2121
define spir_kernel void @foo() {
2222
%iptr = alloca i32, align 4
23-
%res = call spir_func i32 (ptr addrspace(2), ...) @_Z18__spirv_ocl_printfPU3AS2c(ptr addrspace(2) @.str, ptr %iptr)
23+
%res = call spir_func i32 (ptr addrspace(2), ...) @_Z18__spirv_ocl_printfPU3AS2cz(ptr addrspace(2) @.str, ptr %iptr)
2424
ret void
2525
}
2626

27-
declare spir_func i32 @_Z18__spirv_ocl_printfPU3AS2c(ptr addrspace(2), ...)
27+
declare spir_func i32 @_Z18__spirv_ocl_printfPU3AS2cz(ptr addrspace(2), ...)

0 commit comments

Comments
 (0)