[llvm] [AMDGPU] Split struct kernel arguments (PR #133786)

Yaxun Liu via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 7 09:44:33 PDT 2025


================
@@ -409,6 +443,12 @@ void MetadataStreamerMsgPackV4::emitKernelArg(
       Arg[".is_pipe"] = Arg.getDocument()->getNode(true);
   }
 
+  // Add original argument index and offset to the metadata
+  if (OriginalArgIndex != ~0U) {
+    Arg[".original_arg_index"] = Arg.getDocument()->getNode(OriginalArgIndex);
+    Arg[".original_arg_offset"] = Arg.getDocument()->getNode(OriginalArgOffset);
+  }
----------------
yxsamliu wrote:

> I don't want to give meaning to the index in the IR signature. It already doesn't have a 1:1 mapping with the source language. The ABI should only be in terms of sizes, alignments and offsets

In clang codegen, clang may make changes to the kernel signature so that it is no longer the same as in the source code. However, the kernel arg index does play critical role in the contract between the compiler and runtime. Basically, the kernel launching API accepts an array of pointers pointing to each kernel argument, and it lays out each kernel argument one by one into the kernel argument segment based on HSA metadata. This is because in host compilation it is not always possible to know the exact size, align, and offset of each kernel argument that is determined in device compilation, therefore those information are passed to the runtime through HSA metadata, and used during kernel launching.

Due to this, it is critical that mid-end and backend cannot change the number of kernel arguments without have a way to tell the runtime of such changes. Before this PR, this was maintained because a kernel has external linkage with default visibility in LLVM IR, therefore IPO cannot change their signatures. In this PR, we want to optimize the arguments passing from runtime to firmware by omitting the useless arguments and useless fields in struct-type arguments, since the runtime sees an array of pointers to arguments, we have to tell runtime which portion of which argument to keep. This function attribute is intended for generating HSA metadata for the runtime.

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


More information about the llvm-commits mailing list