diff --git a/lib/PhysicalPointerArgsPass.cpp b/lib/PhysicalPointerArgsPass.cpp index caaa0cde9..bd8b4fd5f 100644 --- a/lib/PhysicalPointerArgsPass.cpp +++ b/lib/PhysicalPointerArgsPass.cpp @@ -126,6 +126,20 @@ PreservedAnalyses clspv::PhysicalPointerArgsPass::run(Module &M, auto *NewArg = NewFunc->getArg(ArgNum); if (OriginalArgTy != NewArg->getType()) { auto *IntAsPtr = Builder.CreateIntToPtr(NewArg, OriginalArgTy); + + // Convert a void pointer to a typed if possible + Type *resource_type = + clspv::InferType(F.getArg(ArgNum), M.getContext(), &TypeCache); + if (resource_type && OriginalArgTy != resource_type) { + // Create the type only once. + auto *arr_type = ArrayType::get(resource_type, 0); + resource_type = StructType::get(arr_type); + + Value *zero = Builder.getInt32(0); + IntAsPtr = + Builder.CreateGEP(resource_type, IntAsPtr, {zero, zero, zero}); + OriginalArgTy = resource_type; + } WrappedArgs.push_back(IntAsPtr); // We can't attach metadata to arguments directly, so add to this