[llvm] [SPIR-V] Emit proper pointer type for OpenCL kernel arguments (PR #67726)

Michal Paszkowski via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 5 09:53:59 PDT 2023


================
@@ -194,23 +194,39 @@ getKernelArgTypeQual(const Function &KernelFunction, unsigned ArgIdx) {
   return {};
 }
 
-static Type *getArgType(const Function &F, unsigned ArgIdx) {
+static SPIRVType *getArgSPIRVType(const Function &F, unsigned ArgIdx,
+                                  SPIRVGlobalRegistry *GR,
+                                  MachineIRBuilder &MIRBuilder) {
+  // Read argument's access qualifier from metadata or default
+  SPIRV::AccessQualifier::AccessQualifier ArgAccessQual =
+      getArgAccessQual(F, ArgIdx);
+
   Type *OriginalArgType = getOriginalFunctionType(F)->getParamType(ArgIdx);
-  if (F.getCallingConv() != CallingConv::SPIR_KERNEL ||
-      isSpecialOpaqueType(OriginalArgType))
-    return OriginalArgType;
+
+  // In case of non-kernel SPIR-V function, use the original IR type.
+  if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
+    return GR->getOrCreateSPIRVType(OriginalArgType, MIRBuilder, ArgAccessQual);
+
+  // Use original type if it is already TargetExtType of a builtin type.
+  if (isSpecialOpaqueType(OriginalArgType))
+    return GR->getOrCreateSPIRVType(OriginalArgType, MIRBuilder, ArgAccessQual);
 
   MDString *MDKernelArgType =
       getKernelArgAttribute(F, ArgIdx, "kernel_arg_type");
-  if (!MDKernelArgType || !MDKernelArgType->getString().endswith("_t"))
-    return OriginalArgType;
-
-  std::string KernelArgTypeStr = "opencl." + MDKernelArgType->getString().str();
-  Type *ExistingOpaqueType =
-      StructType::getTypeByName(F.getContext(), KernelArgTypeStr);
-  return ExistingOpaqueType
-             ? ExistingOpaqueType
-             : StructType::create(F.getContext(), KernelArgTypeStr);
+  if (!MDKernelArgType || (MDKernelArgType->getString().ends_with("*") &&
----------------
michalpaszkowski wrote:

@Keenuts I generally agree, but possibly we could stick with one pass focused only on this, to avoid unnecessary iteration over the whole module. The pass could first go through each source (at set precedence), collect types, remove the sources, and then generate a canonical format.

https://github.com/llvm/llvm-project/pull/67726


More information about the llvm-commits mailing list