[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/Men-cotton closed https://github.com/llvm/llvm-project/pull/200581 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/Men-cotton updated
https://github.com/llvm/llvm-project/pull/200581
>From a3ff15cf9f25af61e6c419ecaad4ca85776838be Mon Sep 17 00:00:00 2001
From: mencotton
Date: Sun, 24 May 2026 00:56:32 +0900
Subject: [PATCH 1/3] [CIR][OpenCL] Attach kernel argument metadata to CIR
functions
Emit the CIR OpenCL kernel argument metadata attribute for kernel functions.
Preserve CIR language address-space kinds until lowering and include argument
names only when `-cl-kernel-arg-info` is enabled.
---
clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 3 +
clang/lib/CIR/CodeGen/CIRGenModule.cpp| 91 +++
clang/lib/CIR/CodeGen/CIRGenModule.h | 4 +
...ernel-arg-metadata-target-address-space.cl | 5 +
.../kernel-arg-info-single-as.cl | 19 +++
.../test/CIR/CodeGenOpenCL/kernel-arg-info.cl | 152 ++
.../CIR/CodeGenOpenCL/kernel-arg-metadata.cl | 12 ++
7 files changed, 286 insertions(+)
create mode 100644
clang/test/CIR/CodeGenOpenCL/invalid-kernel-arg-metadata-target-address-space.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-info-single-as.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-info.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-metadata.cl
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 92568dcc3dfd8..e5d3f6fe408c3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -800,6 +800,9 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl
gd, cir::FuncOp fn,
finishFunction(bodyRange.getEnd());
}
+ if (getLangOpts().OpenCL && funcDecl->hasAttr())
+cgm.emitOpenCLKernelArgMetadata(fn, funcDecl);
+
eraseEmptyAndUnusedBlocks(fn);
return fn;
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 8b9140caa7471..524604b51ba40 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -3266,6 +3266,97 @@ void CIRGenModule::setCIRFunctionAttributesForDefinition(
assert(!cir::MissingFeatures::opFuncColdHotAttr());
}
+void CIRGenModule::emitOpenCLKernelArgMetadata(cir::FuncOp func,
+ const clang::FunctionDecl *fd) {
+ assert(fd && "expected a kernel function declaration");
+ const PrintingPolicy &policy = getASTContext().getPrintingPolicy();
+
+ SmallVector addressQuals;
+ SmallVector accessQuals;
+ SmallVector argTypeNames;
+ SmallVector argBaseTypeNames;
+ SmallVector argTypeQuals;
+ SmallVector argNames;
+
+ for (const ParmVarDecl *param : fd->parameters()) {
+argNames.push_back(builder.getStringAttr(param->getName()));
+
+QualType type = param->getType();
+std::string typeQuals;
+
+if (type->isImageType() || type->isPipeType()) {
+ errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for image and pipe types");
+ return;
+}
+
+accessQuals.push_back(builder.getStringAttr("none"));
+
+auto getTypeSpelling = [&](QualType paramType) {
+ std::string typeName =
paramType.getUnqualifiedType().getAsString(policy);
+
+ if (paramType.isCanonical()) {
+StringRef typeNameRef = typeName;
+if (typeNameRef.consume_front("unsigned "))
+ return std::string("u") + typeNameRef.str();
+if (typeNameRef.consume_front("signed "))
+ return typeNameRef.str();
+ }
+
+ return typeName;
+};
+
+if (type->isPointerType()) {
+ QualType pointeeType = type->getPointeeType();
+ if (clang::isTargetAddressSpace(pointeeType.getAddressSpace())) {
+errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for target-specific "
+ "address_space(N) kernel parameters; classic CodeGen "
+ "currently accepts this case");
+return;
+ }
+
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
+ &getMLIRContext(),
+ cir::toCIRLangAddressSpace(pointeeType.getAddressSpace(;
+
+ argTypeNames.push_back(
+ builder.getStringAttr(getTypeSpelling(pointeeType) + "*"));
+ argBaseTypeNames.push_back(builder.getStringAttr(
+ getTypeSpelling(pointeeType.getCanonicalType()) + "*"));
+
+ if (type.isRestrictQualified())
+typeQuals = "restrict";
+ if (pointeeType.isConstQualified() ||
+ pointeeType.getAddressSpace() == LangAS::opencl_constant)
+typeQuals += typeQuals.empty() ? "const" : " const";
+ if (pointeeType.isVolatileQualified())
+typeQuals += typeQuals.empty() ? "volatile" : " volatile";
+} else {
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
+ &getMLIRContext(), cir::LangAddressSpace::Default));
+
+ argTypeNames.push_back(builder.getStringAttr(getTypeSpelling(type)));
+ argBaseTypeNames.push_back(
+
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/Men-cotton updated
https://github.com/llvm/llvm-project/pull/200581
>From a3ff15cf9f25af61e6c419ecaad4ca85776838be Mon Sep 17 00:00:00 2001
From: mencotton
Date: Sun, 24 May 2026 00:56:32 +0900
Subject: [PATCH 1/3] [CIR][OpenCL] Attach kernel argument metadata to CIR
functions
Emit the CIR OpenCL kernel argument metadata attribute for kernel functions.
Preserve CIR language address-space kinds until lowering and include argument
names only when `-cl-kernel-arg-info` is enabled.
---
clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 3 +
clang/lib/CIR/CodeGen/CIRGenModule.cpp| 91 +++
clang/lib/CIR/CodeGen/CIRGenModule.h | 4 +
...ernel-arg-metadata-target-address-space.cl | 5 +
.../kernel-arg-info-single-as.cl | 19 +++
.../test/CIR/CodeGenOpenCL/kernel-arg-info.cl | 152 ++
.../CIR/CodeGenOpenCL/kernel-arg-metadata.cl | 12 ++
7 files changed, 286 insertions(+)
create mode 100644
clang/test/CIR/CodeGenOpenCL/invalid-kernel-arg-metadata-target-address-space.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-info-single-as.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-info.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-metadata.cl
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 92568dcc3dfd8..e5d3f6fe408c3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -800,6 +800,9 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl
gd, cir::FuncOp fn,
finishFunction(bodyRange.getEnd());
}
+ if (getLangOpts().OpenCL && funcDecl->hasAttr())
+cgm.emitOpenCLKernelArgMetadata(fn, funcDecl);
+
eraseEmptyAndUnusedBlocks(fn);
return fn;
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 8b9140caa7471..524604b51ba40 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -3266,6 +3266,97 @@ void CIRGenModule::setCIRFunctionAttributesForDefinition(
assert(!cir::MissingFeatures::opFuncColdHotAttr());
}
+void CIRGenModule::emitOpenCLKernelArgMetadata(cir::FuncOp func,
+ const clang::FunctionDecl *fd) {
+ assert(fd && "expected a kernel function declaration");
+ const PrintingPolicy &policy = getASTContext().getPrintingPolicy();
+
+ SmallVector addressQuals;
+ SmallVector accessQuals;
+ SmallVector argTypeNames;
+ SmallVector argBaseTypeNames;
+ SmallVector argTypeQuals;
+ SmallVector argNames;
+
+ for (const ParmVarDecl *param : fd->parameters()) {
+argNames.push_back(builder.getStringAttr(param->getName()));
+
+QualType type = param->getType();
+std::string typeQuals;
+
+if (type->isImageType() || type->isPipeType()) {
+ errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for image and pipe types");
+ return;
+}
+
+accessQuals.push_back(builder.getStringAttr("none"));
+
+auto getTypeSpelling = [&](QualType paramType) {
+ std::string typeName =
paramType.getUnqualifiedType().getAsString(policy);
+
+ if (paramType.isCanonical()) {
+StringRef typeNameRef = typeName;
+if (typeNameRef.consume_front("unsigned "))
+ return std::string("u") + typeNameRef.str();
+if (typeNameRef.consume_front("signed "))
+ return typeNameRef.str();
+ }
+
+ return typeName;
+};
+
+if (type->isPointerType()) {
+ QualType pointeeType = type->getPointeeType();
+ if (clang::isTargetAddressSpace(pointeeType.getAddressSpace())) {
+errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for target-specific "
+ "address_space(N) kernel parameters; classic CodeGen "
+ "currently accepts this case");
+return;
+ }
+
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
+ &getMLIRContext(),
+ cir::toCIRLangAddressSpace(pointeeType.getAddressSpace(;
+
+ argTypeNames.push_back(
+ builder.getStringAttr(getTypeSpelling(pointeeType) + "*"));
+ argBaseTypeNames.push_back(builder.getStringAttr(
+ getTypeSpelling(pointeeType.getCanonicalType()) + "*"));
+
+ if (type.isRestrictQualified())
+typeQuals = "restrict";
+ if (pointeeType.isConstQualified() ||
+ pointeeType.getAddressSpace() == LangAS::opencl_constant)
+typeQuals += typeQuals.empty() ? "const" : " const";
+ if (pointeeType.isVolatileQualified())
+typeQuals += typeQuals.empty() ? "volatile" : " volatile";
+} else {
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
+ &getMLIRContext(), cir::LangAddressSpace::Default));
+
+ argTypeNames.push_back(builder.getStringAttr(getTypeSpelling(type)));
+ argBaseTypeNames.push_back(
+
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
github-actions[bot] wrote: # :penguin: Linux x64 Test Results * 92415 tests passed * 814 tests skipped * 1 test failed ## Failed Tests (click on a test name to see its output) ### lldb-api lldb-api.tools/lldb-dap/attach/TestDAP_attach.py ``` Script: -- /usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib --env LLVM_INCLUDE_DIR=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include --env LLVM_TOOLS_DIR=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin --libcxx-include-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1 --libcxx-include-target-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/x86_64-unknown-linux-gnu/c++/v1 --libcxx-library-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib/x86_64-unknown-linux-gnu --triple x86_64-unknown-linux-gnu --build-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lldb-test-build --lldb-module-cache-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lldb-test-build/module-cache-lldb/lldb-api --clang-module-cache-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lldb-test-build/module-cache-clang/lldb-api --executable /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/lldb --lldb-python-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/local/lib/python3.12/dist-packages --compiler /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/clang --dsymutil /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin --lldb-obj-root /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/lldb --lldb-libs-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib --cmake-build-type Release /home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/test/API/tools/lldb-dap/attach -p TestDAP_attach.py -- Exit Code: -9 Timeout: Reached timeout of 1200 seconds Command Output (stdout): -- Skipping the following test categories: msvcstl, dsym, pdb, gmodules, debugserver, objc -- Command Output (stderr): -- PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_attach_with_invalid_session (TestDAP_attach.TestDAP_attach.test_attach_with_invalid_session) PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_attach_with_missing_session_debugger (TestDAP_attach.TestDAP_attach.test_attach_with_missing_session_debugger) PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_by_basename_waitFor (TestDAP_attach.TestDAP_attach.test_by_basename_waitFor) PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_by_name (TestDAP_attach.TestDAP_attach.test_by_name) PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_by_name_waitFor (TestDAP_attach.TestDAP_attach.test_by_name_waitFor) FAIL: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_by_pid (TestDAP_attach.TestDAP_attach.test_by_pid) Log Files: - /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lldb-test-build/tools/lldb-dap/attach/TestDAP_attach.test_by_pid/Error_test_by_pid.log == ERROR: test_by_pid (TestDAP_attach.TestDAP_attach.test_by_pid) Tests attaching to a process by process ID. -- Traceback (most recent call last): File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/lldbtest.py", line 1203, in tearDown hook() # try the plain call and hope it works ^^ File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/tools/lldb_dap/testcase.py", line 114, in cleanup_session session.disconnect(terminateDebuggee=True) File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/tools/lldb_dap/session_helpers.py", line 1717, in disconnect response = self.send_request(args).result() File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/tools/lldb_dap/session.py", line 99, in result response = self.result_or_error() ^^ File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/tools/lldb_dap/session.py", line 116, in
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
github-actions[bot] wrote: # :penguin: Linux x64 Test Results * 92415 tests passed * 814 tests skipped * 1 test failed ## Failed Tests (click on a test name to see its output) ### lldb-api lldb-api.tools/lldb-dap/attach/TestDAP_attach.py ``` Script: -- /usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib --env LLVM_INCLUDE_DIR=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include --env LLVM_TOOLS_DIR=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin --libcxx-include-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1 --libcxx-include-target-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/x86_64-unknown-linux-gnu/c++/v1 --libcxx-library-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib/x86_64-unknown-linux-gnu --triple x86_64-unknown-linux-gnu --build-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lldb-test-build --lldb-module-cache-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lldb-test-build/module-cache-lldb/lldb-api --clang-module-cache-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lldb-test-build/module-cache-clang/lldb-api --executable /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/lldb --lldb-python-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/local/lib/python3.12/dist-packages --compiler /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/clang --dsymutil /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin --lldb-obj-root /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/lldb --lldb-libs-dir /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib --cmake-build-type Release /home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/test/API/tools/lldb-dap/attach -p TestDAP_attach.py -- Exit Code: -9 Timeout: Reached timeout of 1200 seconds Command Output (stdout): -- Skipping the following test categories: msvcstl, dsym, pdb, gmodules, debugserver, objc -- Command Output (stderr): -- PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_attach_with_invalid_session (TestDAP_attach.TestDAP_attach.test_attach_with_invalid_session) PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_attach_with_missing_session_debugger (TestDAP_attach.TestDAP_attach.test_attach_with_missing_session_debugger) PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_by_basename_waitFor (TestDAP_attach.TestDAP_attach.test_by_basename_waitFor) PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_by_name (TestDAP_attach.TestDAP_attach.test_by_name) PASS: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_by_name_waitFor (TestDAP_attach.TestDAP_attach.test_by_name_waitFor) FAIL: LLDB (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang-x86_64) :: test_by_pid (TestDAP_attach.TestDAP_attach.test_by_pid) Log Files: - /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lldb-test-build/tools/lldb-dap/attach/TestDAP_attach.test_by_pid/Error_test_by_pid.log == ERROR: test_by_pid (TestDAP_attach.TestDAP_attach.test_by_pid) Tests attaching to a process by process ID. -- Traceback (most recent call last): File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/lldbtest.py", line 1203, in tearDown hook() # try the plain call and hope it works ^^ File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/tools/lldb_dap/testcase.py", line 114, in cleanup_session session.disconnect(terminateDebuggee=True) File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/tools/lldb_dap/session_helpers.py", line 1717, in disconnect response = self.send_request(args).result() File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/tools/lldb_dap/session.py", line 99, in result response = self.result_or_error() ^^ File "/home/gha/actions-runner/_work/llvm-project/llvm-project/lldb/packages/Python/lldbsuite/test/tools/lldb_dap/session.py", line 116, in
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/Men-cotton updated
https://github.com/llvm/llvm-project/pull/200581
>From a3ff15cf9f25af61e6c419ecaad4ca85776838be Mon Sep 17 00:00:00 2001
From: mencotton
Date: Sun, 24 May 2026 00:56:32 +0900
Subject: [PATCH 1/2] [CIR][OpenCL] Attach kernel argument metadata to CIR
functions
Emit the CIR OpenCL kernel argument metadata attribute for kernel functions.
Preserve CIR language address-space kinds until lowering and include argument
names only when `-cl-kernel-arg-info` is enabled.
---
clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 3 +
clang/lib/CIR/CodeGen/CIRGenModule.cpp| 91 +++
clang/lib/CIR/CodeGen/CIRGenModule.h | 4 +
...ernel-arg-metadata-target-address-space.cl | 5 +
.../kernel-arg-info-single-as.cl | 19 +++
.../test/CIR/CodeGenOpenCL/kernel-arg-info.cl | 152 ++
.../CIR/CodeGenOpenCL/kernel-arg-metadata.cl | 12 ++
7 files changed, 286 insertions(+)
create mode 100644
clang/test/CIR/CodeGenOpenCL/invalid-kernel-arg-metadata-target-address-space.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-info-single-as.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-info.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-metadata.cl
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 92568dcc3dfd8..e5d3f6fe408c3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -800,6 +800,9 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl
gd, cir::FuncOp fn,
finishFunction(bodyRange.getEnd());
}
+ if (getLangOpts().OpenCL && funcDecl->hasAttr())
+cgm.emitOpenCLKernelArgMetadata(fn, funcDecl);
+
eraseEmptyAndUnusedBlocks(fn);
return fn;
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 8b9140caa7471..524604b51ba40 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -3266,6 +3266,97 @@ void CIRGenModule::setCIRFunctionAttributesForDefinition(
assert(!cir::MissingFeatures::opFuncColdHotAttr());
}
+void CIRGenModule::emitOpenCLKernelArgMetadata(cir::FuncOp func,
+ const clang::FunctionDecl *fd) {
+ assert(fd && "expected a kernel function declaration");
+ const PrintingPolicy &policy = getASTContext().getPrintingPolicy();
+
+ SmallVector addressQuals;
+ SmallVector accessQuals;
+ SmallVector argTypeNames;
+ SmallVector argBaseTypeNames;
+ SmallVector argTypeQuals;
+ SmallVector argNames;
+
+ for (const ParmVarDecl *param : fd->parameters()) {
+argNames.push_back(builder.getStringAttr(param->getName()));
+
+QualType type = param->getType();
+std::string typeQuals;
+
+if (type->isImageType() || type->isPipeType()) {
+ errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for image and pipe types");
+ return;
+}
+
+accessQuals.push_back(builder.getStringAttr("none"));
+
+auto getTypeSpelling = [&](QualType paramType) {
+ std::string typeName =
paramType.getUnqualifiedType().getAsString(policy);
+
+ if (paramType.isCanonical()) {
+StringRef typeNameRef = typeName;
+if (typeNameRef.consume_front("unsigned "))
+ return std::string("u") + typeNameRef.str();
+if (typeNameRef.consume_front("signed "))
+ return typeNameRef.str();
+ }
+
+ return typeName;
+};
+
+if (type->isPointerType()) {
+ QualType pointeeType = type->getPointeeType();
+ if (clang::isTargetAddressSpace(pointeeType.getAddressSpace())) {
+errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for target-specific "
+ "address_space(N) kernel parameters; classic CodeGen "
+ "currently accepts this case");
+return;
+ }
+
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
+ &getMLIRContext(),
+ cir::toCIRLangAddressSpace(pointeeType.getAddressSpace(;
+
+ argTypeNames.push_back(
+ builder.getStringAttr(getTypeSpelling(pointeeType) + "*"));
+ argBaseTypeNames.push_back(builder.getStringAttr(
+ getTypeSpelling(pointeeType.getCanonicalType()) + "*"));
+
+ if (type.isRestrictQualified())
+typeQuals = "restrict";
+ if (pointeeType.isConstQualified() ||
+ pointeeType.getAddressSpace() == LangAS::opencl_constant)
+typeQuals += typeQuals.empty() ? "const" : " const";
+ if (pointeeType.isVolatileQualified())
+typeQuals += typeQuals.empty() ? "volatile" : " volatile";
+} else {
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
+ &getMLIRContext(), cir::LangAddressSpace::Default));
+
+ argTypeNames.push_back(builder.getStringAttr(getTypeSpelling(type)));
+ argBaseTypeNames.push_back(
+
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/Men-cotton updated
https://github.com/llvm/llvm-project/pull/200581
>From 285b6e3eba7011ff389431ed3dc1f1c6402ecadf Mon Sep 17 00:00:00 2001
From: mencotton
Date: Sun, 24 May 2026 00:56:32 +0900
Subject: [PATCH] [CIR][OpenCL] Attach kernel argument metadata to CIR
functions
Emit the CIR OpenCL kernel argument metadata attribute for kernel functions.
Preserve CIR language address-space kinds until lowering and include argument
names only when `-cl-kernel-arg-info` is enabled.
---
clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 3 +
clang/lib/CIR/CodeGen/CIRGenModule.cpp| 91 +++
clang/lib/CIR/CodeGen/CIRGenModule.h | 4 +
...ernel-arg-metadata-target-address-space.cl | 5 +
.../kernel-arg-info-single-as.cl | 19 +++
.../test/CIR/CodeGenOpenCL/kernel-arg-info.cl | 152 ++
.../CIR/CodeGenOpenCL/kernel-arg-metadata.cl | 12 ++
7 files changed, 286 insertions(+)
create mode 100644
clang/test/CIR/CodeGenOpenCL/invalid-kernel-arg-metadata-target-address-space.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-info-single-as.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-info.cl
create mode 100644 clang/test/CIR/CodeGenOpenCL/kernel-arg-metadata.cl
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 4b020c96964a7..aaf2823a1ef5e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -806,6 +806,9 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl
gd, cir::FuncOp fn,
finishFunction(bodyRange.getEnd());
}
+ if (getLangOpts().OpenCL && funcDecl->hasAttr())
+cgm.emitOpenCLKernelArgMetadata(fn, funcDecl);
+
eraseEmptyAndUnusedBlocks(fn);
return fn;
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index b377f84e8d370..9456f5c689b3c 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -3139,6 +3139,97 @@ void CIRGenModule::setCIRFunctionAttributesForDefinition(
assert(!cir::MissingFeatures::opFuncColdHotAttr());
}
+void CIRGenModule::emitOpenCLKernelArgMetadata(cir::FuncOp func,
+ const clang::FunctionDecl *fd) {
+ assert(fd && "expected a kernel function declaration");
+ const PrintingPolicy &policy = getASTContext().getPrintingPolicy();
+
+ SmallVector addressQuals;
+ SmallVector accessQuals;
+ SmallVector argTypeNames;
+ SmallVector argBaseTypeNames;
+ SmallVector argTypeQuals;
+ SmallVector argNames;
+
+ for (const ParmVarDecl *param : fd->parameters()) {
+argNames.push_back(builder.getStringAttr(param->getName()));
+
+QualType type = param->getType();
+std::string typeQuals;
+
+if (type->isImageType() || type->isPipeType()) {
+ errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for image and pipe types");
+ return;
+}
+
+accessQuals.push_back(builder.getStringAttr("none"));
+
+auto getTypeSpelling = [&](QualType paramType) {
+ std::string typeName =
paramType.getUnqualifiedType().getAsString(policy);
+
+ if (paramType.isCanonical()) {
+StringRef typeNameRef = typeName;
+if (typeNameRef.consume_front("unsigned "))
+ return std::string("u") + typeNameRef.str();
+if (typeNameRef.consume_front("signed "))
+ return typeNameRef.str();
+ }
+
+ return typeName;
+};
+
+if (type->isPointerType()) {
+ QualType pointeeType = type->getPointeeType();
+ if (clang::isTargetAddressSpace(pointeeType.getAddressSpace())) {
+errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for target-specific "
+ "address_space(N) kernel parameters; classic CodeGen "
+ "currently accepts this case");
+return;
+ }
+
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
+ &getMLIRContext(),
+ cir::toCIRLangAddressSpace(pointeeType.getAddressSpace(;
+
+ argTypeNames.push_back(
+ builder.getStringAttr(getTypeSpelling(pointeeType) + "*"));
+ argBaseTypeNames.push_back(builder.getStringAttr(
+ getTypeSpelling(pointeeType.getCanonicalType()) + "*"));
+
+ if (type.isRestrictQualified())
+typeQuals = "restrict";
+ if (pointeeType.isConstQualified() ||
+ pointeeType.getAddressSpace() == LangAS::opencl_constant)
+typeQuals += typeQuals.empty() ? "const" : " const";
+ if (pointeeType.isVolatileQualified())
+typeQuals += typeQuals.empty() ? "volatile" : " volatile";
+} else {
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
+ &getMLIRContext(), cir::LangAddressSpace::Default));
+
+ argTypeNames.push_back(builder.getStringAttr(getTypeSpelling(type)));
+ argBaseTypeNames.push_back(
+
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/Men-cotton edited https://github.com/llvm/llvm-project/pull/200581 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/200581 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/RiverDave edited https://github.com/llvm/llvm-project/pull/200581 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 %s -fclangir -cl-std=CL2.0 -triple x86_64-unknown-linux-gnu
-emit-cir -o - -verify
+
+kernel void invalid_target_addr_space_kernel_arg(
+// expected-error@+1 {{ClangIR code gen Not Yet Implemented: OpenCL kernel
argument metadata for target-specific address_space(N) kernel parameters;
classic CodeGen currently accepts this case}}
RiverDave wrote:
I would adjust this assumption based on the way OG behavior. As I pointed out
earlier the kernel arg attribute should collapse to default
https://github.com/llvm/llvm-project/pull/200581
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/RiverDave requested changes to this pull request. Mostly good, few comments on AS handling yet again. Since you're stacking this, you could probably make the base of your pr point to this dependency https://github.com/llvm/llvm-project/pull/199530 so the diff is cleaner https://github.com/llvm/llvm-project/pull/200581 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
@@ -3139,6 +3139,97 @@ void CIRGenModule::setCIRFunctionAttributesForDefinition(
assert(!cir::MissingFeatures::opFuncColdHotAttr());
}
+void CIRGenModule::emitOpenCLKernelArgMetadata(cir::FuncOp func,
+ const clang::FunctionDecl *fd) {
+ assert(fd && "expected a kernel function declaration");
+ const PrintingPolicy &policy = getASTContext().getPrintingPolicy();
+
+ SmallVector addressQuals;
+ SmallVector accessQuals;
+ SmallVector argTypeNames;
+ SmallVector argBaseTypeNames;
+ SmallVector argTypeQuals;
+ SmallVector argNames;
+
+ for (const ParmVarDecl *param : fd->parameters()) {
+argNames.push_back(builder.getStringAttr(param->getName()));
+
+QualType type = param->getType();
+std::string typeQuals;
+
+if (type->isImageType() || type->isPipeType()) {
+ errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for image and pipe types");
+ return;
+}
+
+accessQuals.push_back(builder.getStringAttr("none"));
+
+auto getTypeSpelling = [&](QualType paramType) {
+ std::string typeName =
paramType.getUnqualifiedType().getAsString(policy);
+
+ if (paramType.isCanonical()) {
+StringRef typeNameRef = typeName;
+if (typeNameRef.consume_front("unsigned "))
+ return std::string("u") + typeNameRef.str();
+if (typeNameRef.consume_front("signed "))
+ return typeNameRef.str();
+ }
+
+ return typeName;
+};
+
+if (type->isPointerType()) {
+ QualType pointeeType = type->getPointeeType();
+ if (clang::isTargetAddressSpace(pointeeType.getAddressSpace())) {
+errorNYI(param->getSourceRange(),
+ "OpenCL kernel argument metadata for target-specific "
+ "address_space(N) kernel parameters; classic CodeGen "
+ "currently accepts this case");
+return;
+ }
+
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
+ &getMLIRContext(),
+ cir::toCIRLangAddressSpace(pointeeType.getAddressSpace(;
+
+ argTypeNames.push_back(
+ builder.getStringAttr(getTypeSpelling(pointeeType) + "*"));
+ argBaseTypeNames.push_back(builder.getStringAttr(
+ getTypeSpelling(pointeeType.getCanonicalType()) + "*"));
+
+ if (type.isRestrictQualified())
+typeQuals = "restrict";
+ if (pointeeType.isConstQualified() ||
+ pointeeType.getAddressSpace() == LangAS::opencl_constant)
+typeQuals += typeQuals.empty() ? "const" : " const";
+ if (pointeeType.isVolatileQualified())
+typeQuals += typeQuals.empty() ? "volatile" : " volatile";
+} else {
+ addressQuals.push_back(cir::LangAddressSpaceAttr::get(
RiverDave wrote:
So OpenCL handles AS in an odd way. The kernel_arg_addr_space values aren't a
backend thing and shouldn't be lowered per-target. Take a look at:
https://github.com/llvm/llvm-project/blob/1060a6be0a471106fd65a3f16937ee1c588dabed/clang/lib/CodeGen/CodeGenModule.cpp#L2740
the comment makes it explicit. it maps the AST AS straight to the qualifiers
the SPIR spec expects, regardless of the target.
You should have a helper that handles the mapping like this:
```
opencl_global -> offload_global
opencl_constant -> offload_constant
opencl_local -> offload_local
opencl_generic-> offload_generic
opencl_global_device -> offload_global_device
opencl_global_host-> offload_global_host
default: -> offload_default // target AS lands here, but so
does everything non-named
```
One thing to watch: a __attribute__((address_space(N))) arg is a target AS, not
a named OpenCL space, so it falls into default: and collapses to
offload_default (id 0), same as ArgInfoAddressSpace does. The number stays on
the pointer, it just doesn't make it into the metadata. I'd keep it a whitelist
with a default case rather than checking for target AS specifically, so it
doesn't drift from upstream.
https://github.com/llvm/llvm-project/pull/200581
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/Men-cotton updated
https://github.com/llvm/llvm-project/pull/200581
>From 1ac5d63467e54f069cdd344082d82c82b77020b1 Mon Sep 17 00:00:00 2001
From: mencotton
Date: Sun, 24 May 2026 00:49:55 +0900
Subject: [PATCH 1/7] [CIR][OpenCL] Add kernel argument metadata attribute
Add a CIR attribute that carries OpenCL kernel argument metadata in source
argument order. Verify that each metadata field has the expected element type
and that all present arrays describe the same number of arguments.
---
.../include/clang/CIR/Dialect/IR/CIRAttrs.td | 1 +
.../clang/CIR/Dialect/IR/CIRDialect.td| 1 +
.../clang/CIR/Dialect/IR/CIROpenCLAttrs.td| 46 +++
clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp | 60 ++
clang/lib/CIR/Dialect/IR/CMakeLists.txt | 1 +
.../IR/invalid-opencl-kernel-arg-metadata.cir | 78 +++
.../CIR/IR/opencl-kernel-arg-metadata.cir | 27 +++
7 files changed, 214 insertions(+)
create mode 100644 clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
create mode 100644 clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
create mode 100644 clang/test/CIR/IR/invalid-opencl-kernel-arg-metadata.cir
create mode 100644 clang/test/CIR/IR/opencl-kernel-arg-metadata.cir
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index 4032d8219fff3..19a0c25c8b10e 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -1687,6 +1687,7 @@ def CIR_AnnotationArrayAttr
: TypedArrayAttrBase;
+include "clang/CIR/Dialect/IR/CIROpenCLAttrs.td"
include "clang/CIR/Dialect/IR/CIRCUDAAttrs.td"
#endif // CLANG_CIR_DIALECT_IR_CIRATTRS_TD
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
index aaa7b48262c80..c20af04f97a1a 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
@@ -82,6 +82,7 @@ def CIR_Dialect : Dialect {
static llvm::StringRef getAMDGPUCodeObjectVersionAttrName() { return
"cir.amdhsa_code_object_version"; }
static llvm::StringRef getAMDGPUPrintfKindAttrName() { return
"cir.amdgpu_printf_kind"; }
+static llvm::StringRef getOpenCLKernelArgMetadataAttrName() { return
"cir.cl.kernel_arg_metadata"; }
void registerAttributes();
void registerTypes();
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
b/clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
new file mode 100644
index 0..c0ec9c7f28f85
--- /dev/null
+++ b/clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
@@ -0,0 +1,46 @@
+//===- CIROpenCLAttrs.td - CIR dialect attrs for OpenCL *- tablegen
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file declares the CIR dialect attributes for OpenCL.
+//
+//===--===//
+
+#ifndef CLANG_CIR_DIALECT_IR_CIROPENCLATTRS_TD
+#define CLANG_CIR_DIALECT_IR_CIROPENCLATTRS_TD
+
+//===--===//
+// OpenCLKernelArgMetadataAttr
+//===--===//
+
+def CIR_OpenCLKernelArgMetadataAttr
+: CIR_Attr<"OpenCLKernelArgMetadata", "cl.kernel_arg_metadata"> {
+ let summary = "OpenCL kernel argument metadata";
+ let description = [{
+Stores the OpenCL kernel argument metadata emitted to LLVM IR as
+`kernel_arg_*` metadata.
+
+All parameters are arrays containing the argument information in source
+order. The `name` field is optional and is emitted only when requested by
+`-cl-kernel-arg-info`.
+ }];
+
+ let parameters = (ins
+"::mlir::ArrayAttr":$addr_space,
+"::mlir::ArrayAttr":$access_qual,
+"::mlir::ArrayAttr":$type,
+"::mlir::ArrayAttr":$base_type,
+"::mlir::ArrayAttr":$type_qual,
+OptionalParameter<"::mlir::ArrayAttr">:$name
+ );
+
+ let assemblyFormat = "`<` struct(params) `>`";
+ let genVerifyDecl = 1;
+ let canHaveIllegalCXXABIType = 0;
+}
+
+#endif // CLANG_CIR_DIALECT_IR_CIROPENCLATTRS_TD
diff --git a/clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
b/clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
new file mode 100644
index 0..57692cd4783b8
--- /dev/null
+++ b/clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
@@ -0,0 +1,60 @@
+//===- CIROpenCLAttrs.cpp - OpenCL specific attributes in CIR
-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/Men-cotton updated
https://github.com/llvm/llvm-project/pull/200581
>From 1ac5d63467e54f069cdd344082d82c82b77020b1 Mon Sep 17 00:00:00 2001
From: mencotton
Date: Sun, 24 May 2026 00:49:55 +0900
Subject: [PATCH 1/7] [CIR][OpenCL] Add kernel argument metadata attribute
Add a CIR attribute that carries OpenCL kernel argument metadata in source
argument order. Verify that each metadata field has the expected element type
and that all present arrays describe the same number of arguments.
---
.../include/clang/CIR/Dialect/IR/CIRAttrs.td | 1 +
.../clang/CIR/Dialect/IR/CIRDialect.td| 1 +
.../clang/CIR/Dialect/IR/CIROpenCLAttrs.td| 46 +++
clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp | 60 ++
clang/lib/CIR/Dialect/IR/CMakeLists.txt | 1 +
.../IR/invalid-opencl-kernel-arg-metadata.cir | 78 +++
.../CIR/IR/opencl-kernel-arg-metadata.cir | 27 +++
7 files changed, 214 insertions(+)
create mode 100644 clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
create mode 100644 clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
create mode 100644 clang/test/CIR/IR/invalid-opencl-kernel-arg-metadata.cir
create mode 100644 clang/test/CIR/IR/opencl-kernel-arg-metadata.cir
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index 4032d8219fff3..19a0c25c8b10e 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -1687,6 +1687,7 @@ def CIR_AnnotationArrayAttr
: TypedArrayAttrBase;
+include "clang/CIR/Dialect/IR/CIROpenCLAttrs.td"
include "clang/CIR/Dialect/IR/CIRCUDAAttrs.td"
#endif // CLANG_CIR_DIALECT_IR_CIRATTRS_TD
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
index aaa7b48262c80..c20af04f97a1a 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
@@ -82,6 +82,7 @@ def CIR_Dialect : Dialect {
static llvm::StringRef getAMDGPUCodeObjectVersionAttrName() { return
"cir.amdhsa_code_object_version"; }
static llvm::StringRef getAMDGPUPrintfKindAttrName() { return
"cir.amdgpu_printf_kind"; }
+static llvm::StringRef getOpenCLKernelArgMetadataAttrName() { return
"cir.cl.kernel_arg_metadata"; }
void registerAttributes();
void registerTypes();
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
b/clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
new file mode 100644
index 0..c0ec9c7f28f85
--- /dev/null
+++ b/clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
@@ -0,0 +1,46 @@
+//===- CIROpenCLAttrs.td - CIR dialect attrs for OpenCL *- tablegen
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file declares the CIR dialect attributes for OpenCL.
+//
+//===--===//
+
+#ifndef CLANG_CIR_DIALECT_IR_CIROPENCLATTRS_TD
+#define CLANG_CIR_DIALECT_IR_CIROPENCLATTRS_TD
+
+//===--===//
+// OpenCLKernelArgMetadataAttr
+//===--===//
+
+def CIR_OpenCLKernelArgMetadataAttr
+: CIR_Attr<"OpenCLKernelArgMetadata", "cl.kernel_arg_metadata"> {
+ let summary = "OpenCL kernel argument metadata";
+ let description = [{
+Stores the OpenCL kernel argument metadata emitted to LLVM IR as
+`kernel_arg_*` metadata.
+
+All parameters are arrays containing the argument information in source
+order. The `name` field is optional and is emitted only when requested by
+`-cl-kernel-arg-info`.
+ }];
+
+ let parameters = (ins
+"::mlir::ArrayAttr":$addr_space,
+"::mlir::ArrayAttr":$access_qual,
+"::mlir::ArrayAttr":$type,
+"::mlir::ArrayAttr":$base_type,
+"::mlir::ArrayAttr":$type_qual,
+OptionalParameter<"::mlir::ArrayAttr">:$name
+ );
+
+ let assemblyFormat = "`<` struct(params) `>`";
+ let genVerifyDecl = 1;
+ let canHaveIllegalCXXABIType = 0;
+}
+
+#endif // CLANG_CIR_DIALECT_IR_CIROPENCLATTRS_TD
diff --git a/clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
b/clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
new file mode 100644
index 0..57692cd4783b8
--- /dev/null
+++ b/clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
@@ -0,0 +1,60 @@
+//===- CIROpenCLAttrs.cpp - OpenCL specific attributes in CIR
-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clang
@llvm/pr-subscribers-backend-amdgpu
Author: Akimasa Watanuki (Men-cotton)
Changes
Emit the CIR OpenCL kernel argument metadata attribute for kernel functions.
Preserve CIR language address-space kinds until lowering and include argument
names only when `-cl-kernel-arg-info` is enabled.
Depends on #199530.
---
Patch is 34.84 KiB, truncated to 20.00 KiB below, full version:
https://github.com/llvm/llvm-project/pull/200581.diff
20 Files Affected:
- (modified) clang/include/clang/CIR/Dialect/IR/CIRAttrConstraints.td (+25-6)
- (modified) clang/include/clang/CIR/Dialect/IR/CIRAttrs.td (+7-2)
- (modified) clang/include/clang/CIR/Dialect/IR/CIRDialect.td (+1)
- (modified) clang/include/clang/CIR/Dialect/IR/CIREnumAttr.td (+3-1)
- (added) clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td (+46)
- (modified) clang/lib/CIR/CodeGen/CIRGenFunction.cpp (+3)
- (modified) clang/lib/CIR/CodeGen/CIRGenModule.cpp (+83)
- (modified) clang/lib/CIR/CodeGen/CIRGenModule.h (+4)
- (added) clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp (+38)
- (modified) clang/lib/CIR/Dialect/IR/CIRTypes.cpp (+2)
- (modified) clang/lib/CIR/Dialect/IR/CMakeLists.txt (+1)
- (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/AMDGPU.cpp
(+2)
- (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/NVPTX.cpp
(+1)
- (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/SPIRV.cpp
(+2)
- (added) clang/test/CIR/CodeGenOpenCL/kernel-arg-info-single-as.cl (+19)
- (added) clang/test/CIR/CodeGenOpenCL/kernel-arg-info.cl (+152)
- (added) clang/test/CIR/CodeGenOpenCL/kernel-arg-metadata.cl (+7)
- (modified) clang/test/CIR/IR/invalid-addrspace.cir (+1-1)
- (added) clang/test/CIR/IR/invalid-opencl-kernel-arg-metadata.cir (+78)
- (added) clang/test/CIR/IR/opencl-kernel-arg-metadata.cir (+39)
``diff
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrConstraints.td
b/clang/include/clang/CIR/Dialect/IR/CIRAttrConstraints.td
index 2548d464fb07f..3cb21dd2f4fc7 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrConstraints.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrConstraints.td
@@ -71,15 +71,34 @@ def CIR_TryHandlerAttr
// ArrayAttr constraints
//===--===//
-def CIR_IntArrayAttr : TypedArrayAttrBase;
-
-def CIR_IntOrGlobalViewArrayAttr :
TypedArrayAttrBase{
+class CIR_TypedArrayAttrBase
+: TypedArrayAttrBase {
string cppType = "::mlir::ArrayAttr";
}
-def CIR_TryHandlerArrayAttr : TypedArrayAttrBase
+: CIR_TypedArrayAttrBase {
+ let predicate = Or<[
+CPred<"!$_self">,
+CIR_TypedArrayAttrBase.predicate
+ ]>;
+ string defaultValue = "::mlir::ArrayAttr()";
+}
+
+def CIR_IntArrayAttr : CIR_TypedArrayAttrBase;
+
+def CIR_IntOrGlobalViewArrayAttr
+: CIR_TypedArrayAttrBase;
+
+def CIR_StringArrayAttr
+: CIR_TypedArrayAttrBase;
+
+def CIR_StringArrayAttrOrNull
+: CIR_TypedArrayAttrOrNullBase;
+
+def CIR_TryHandlerArrayAttr : CIR_TypedArrayAttrBase;
#endif // CLANG_CIR_DIALECT_IR_CIRATTRCONSTRAINTS_TD
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index 4032d8219fff3..0044c5bd1eadb 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -996,6 +996,10 @@ def CIR_LangAddressSpaceAttr :
CIR_EnumAttr;
+
//===--===//
// TargetAddressSpaceAttr
//===--===//
@@ -1684,9 +1688,10 @@ def CIR_AnnotationAttr : CIR_Attr<"Annotation",
"annotation"> {
}
def CIR_AnnotationArrayAttr
-: TypedArrayAttrBase;
+: CIR_TypedArrayAttrBase;
+include "clang/CIR/Dialect/IR/CIROpenCLAttrs.td"
include "clang/CIR/Dialect/IR/CIRCUDAAttrs.td"
#endif // CLANG_CIR_DIALECT_IR_CIRATTRS_TD
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
index aaa7b48262c80..c20af04f97a1a 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
@@ -82,6 +82,7 @@ def CIR_Dialect : Dialect {
static llvm::StringRef getAMDGPUCodeObjectVersionAttrName() { return
"cir.amdhsa_code_object_version"; }
static llvm::StringRef getAMDGPUPrintfKindAttrName() { return
"cir.amdgpu_printf_kind"; }
+static llvm::StringRef getOpenCLKernelArgMetadataAttrName() { return
"cir.cl.kernel_arg_metadata"; }
void registerAttributes();
void registerTypes();
diff --git a/clang/include/clang/CIR/Dialect/IR/CIREnumAttr.td
b/clang/include/clang/CIR/Dialect/IR/CIREnumAttr.td
index 1de6ffdc08d72..cc6f256ddfef4 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIREnumAttr.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIREnumAttr.td
@@ -43,7 +43,9 @@ def
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
Men-cotton wrote: * **#200582** https://app.graphite.com/github/pr/llvm/llvm-project/200582?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#200581** https://app.graphite.com/github/pr/llvm/llvm-project/200581?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> 👈 https://app.graphite.com/github/pr/llvm/llvm-project/200581?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * `main` ## How to use the Graphite Merge Queue Add the label FP Bundles to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using [this link](https://app.graphite.com/invite/github/llvm?ref=merge-queue-instructions-in-stack-comment&prId=200581). An organization admin has enabled the [Graphite Merge Queue](https://graphite.dev/docs/graphite-merge-queue) in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn more about https://stacking.dev/?utm_source=stack-comment";>stacking. https://github.com/llvm/llvm-project/pull/200581 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][OpenCL] Attach kernel argument metadata to CIR functions (PR #200581)
https://github.com/Men-cotton created
https://github.com/llvm/llvm-project/pull/200581
Emit the CIR OpenCL kernel argument metadata attribute for kernel functions.
Preserve CIR language address-space kinds until lowering and include argument
names only when `-cl-kernel-arg-info` is enabled.
Depends on #199530.
>From 9c9c9aaa48b05a0bf833bb8e2111a8e0163385a0 Mon Sep 17 00:00:00 2001
From: mencotton
Date: Sun, 24 May 2026 00:49:55 +0900
Subject: [PATCH 1/7] [CIR][OpenCL] Add kernel argument metadata attribute
Add a CIR attribute that carries OpenCL kernel argument metadata in source
argument order. Verify that each metadata field has the expected element type
and that all present arrays describe the same number of arguments.
---
.../include/clang/CIR/Dialect/IR/CIRAttrs.td | 1 +
.../clang/CIR/Dialect/IR/CIRDialect.td| 1 +
.../clang/CIR/Dialect/IR/CIROpenCLAttrs.td| 46 +++
clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp | 60 ++
clang/lib/CIR/Dialect/IR/CMakeLists.txt | 1 +
.../IR/invalid-opencl-kernel-arg-metadata.cir | 78 +++
.../CIR/IR/opencl-kernel-arg-metadata.cir | 27 +++
7 files changed, 214 insertions(+)
create mode 100644 clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
create mode 100644 clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
create mode 100644 clang/test/CIR/IR/invalid-opencl-kernel-arg-metadata.cir
create mode 100644 clang/test/CIR/IR/opencl-kernel-arg-metadata.cir
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index 4032d8219fff3..19a0c25c8b10e 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -1687,6 +1687,7 @@ def CIR_AnnotationArrayAttr
: TypedArrayAttrBase;
+include "clang/CIR/Dialect/IR/CIROpenCLAttrs.td"
include "clang/CIR/Dialect/IR/CIRCUDAAttrs.td"
#endif // CLANG_CIR_DIALECT_IR_CIRATTRS_TD
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
index aaa7b48262c80..c20af04f97a1a 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td
@@ -82,6 +82,7 @@ def CIR_Dialect : Dialect {
static llvm::StringRef getAMDGPUCodeObjectVersionAttrName() { return
"cir.amdhsa_code_object_version"; }
static llvm::StringRef getAMDGPUPrintfKindAttrName() { return
"cir.amdgpu_printf_kind"; }
+static llvm::StringRef getOpenCLKernelArgMetadataAttrName() { return
"cir.cl.kernel_arg_metadata"; }
void registerAttributes();
void registerTypes();
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
b/clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
new file mode 100644
index 0..c0ec9c7f28f85
--- /dev/null
+++ b/clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
@@ -0,0 +1,46 @@
+//===- CIROpenCLAttrs.td - CIR dialect attrs for OpenCL *- tablegen
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file declares the CIR dialect attributes for OpenCL.
+//
+//===--===//
+
+#ifndef CLANG_CIR_DIALECT_IR_CIROPENCLATTRS_TD
+#define CLANG_CIR_DIALECT_IR_CIROPENCLATTRS_TD
+
+//===--===//
+// OpenCLKernelArgMetadataAttr
+//===--===//
+
+def CIR_OpenCLKernelArgMetadataAttr
+: CIR_Attr<"OpenCLKernelArgMetadata", "cl.kernel_arg_metadata"> {
+ let summary = "OpenCL kernel argument metadata";
+ let description = [{
+Stores the OpenCL kernel argument metadata emitted to LLVM IR as
+`kernel_arg_*` metadata.
+
+All parameters are arrays containing the argument information in source
+order. The `name` field is optional and is emitted only when requested by
+`-cl-kernel-arg-info`.
+ }];
+
+ let parameters = (ins
+"::mlir::ArrayAttr":$addr_space,
+"::mlir::ArrayAttr":$access_qual,
+"::mlir::ArrayAttr":$type,
+"::mlir::ArrayAttr":$base_type,
+"::mlir::ArrayAttr":$type_qual,
+OptionalParameter<"::mlir::ArrayAttr">:$name
+ );
+
+ let assemblyFormat = "`<` struct(params) `>`";
+ let genVerifyDecl = 1;
+ let canHaveIllegalCXXABIType = 0;
+}
+
+#endif // CLANG_CIR_DIALECT_IR_CIROPENCLATTRS_TD
diff --git a/clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
b/clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
new file mode 100644
index 0..57692cd4783b8
--- /dev/null
+++ b/clang/lib/CIR/Dialect/IR/CIROpenCLAttrs.cpp
@@ -0,0 +1,60 @@
+//===- CIROpenCLAttrs.cpp - OpenCL specific attributes in CIR
-===//
+//
