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

Nathan Gauër via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 5 04:22:09 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("*") &&
----------------
Keenuts wrote:

Right, I see, so we would now have 4 sources:
 - old translator, with the old format
 - new translator, new format
 - opencl frontend
 - hlsl frontend
 
Do you think it would be valuable to add 3 normalization passes before lowering?
Each pass would convert from 1 of those format to the one we deem canonical, so the backend then only has to handle one type, and if we retire one format, we can do it by removing the pass? 

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


More information about the llvm-commits mailing list