[clang] c994aa8 - [CUDA][clang] Remove parenthesized #pragma unroll warning (#244791)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 16:55:09 PST 2026
Author: Luo Yuanke
Date: 2026-11-24T08:55:04+08:00
New Revision: c994aa88b58daa575cb8f1a315439e96291047b4
URL: https://github.com/llvm/llvm-project/commit/c994aa88b58daa575cb8f1a315439e96291047b4
DIFF: https://github.com/llvm/llvm-project/commit/c994aa88b58daa575cb8f1a315439e96291047b4.diff
LOG: [CUDA][clang] Remove parenthesized #pragma unroll warning (#244791)
nvcc accepts both `#pragma unroll N` and `#pragma unroll (N)` and
generates identical code, and the CUDA C++ Programming Guide
documents the parenthesized form.
The `-Wcuda-compat` warning matched pre-CUDA-8.0 `nvcc` behavior, when
the unroll factor had to be a literal. CUDA 8.0 added support for integral
constant expressions, so the warning is obsolete.
Assisted-By Claude
Added:
clang/test/Parser/pragma-unroll-cuda.cu
Modified:
clang/docs/ReleaseNotes.md
clang/include/clang/Basic/DiagnosticParseKinds.td
clang/lib/Parse/ParsePragma.cpp
Removed:
clang/test/Parser/warn-cuda-compat.cu
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index f025dc010f1f82..3d4726f4c9cc47 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1556,6 +1556,13 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Added `--cuda-emit-nvcc-abi` to emit the NVCC-compatible host registration ABI
(`__cudaRegisterLinkedBinary`).
+- Removed the `-Wcuda-compat` warning about a parenthesized argument to
+ `#pragma unroll` in CUDA and HIP. The warning matched `nvcc` before CUDA 8.0,
+ when the unroll factor had to be a literal; CUDA 8.0 added support for
+ integral constant expressions, and both `#pragma unroll N` and
+ `#pragma unroll (N)` are now documented and generate identical code. The
+ warning is therefore obsolete.
+
- Fixed CUDA built-in variables such as `threadIdx` and `blockDim` being incorrectly treated as having side effects in assumptions. (#GH181340)
- Clang now provides device-side definitions of `__cxa_pure_virtual()` and
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 2be6ff8344cd6d..c15abf61fb01f0 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1625,11 +1625,6 @@ def err_pragma_pipeline_invalid_keyword : Error<
// API notes.
def err_type_unparsed : Error<"unparsed tokens following type">;
-// Pragma unroll support.
-def warn_pragma_unroll_cuda_value_in_parens : Warning<
- "argument to '#pragma unroll' should not be in parentheses in CUDA C/C++">,
- InGroup<CudaCompat>;
-
def err_ptrauth_qualifier_bad_arg_count : Error<
"'__ptrauth' qualifier must take between 1 and 3 arguments">;
diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index dd05bd3c21442a..0b13731e3d8bbc 100644
--- a/clang/lib/Parse/ParsePragma.cpp
+++ b/clang/lib/Parse/ParsePragma.cpp
@@ -3808,12 +3808,6 @@ void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
return;
- // In CUDA, the argument to '#pragma unroll' should not be contained in
- // parentheses.
- if (PP.getLangOpts().CUDA && ValueInParens)
- PP.Diag(Info->Toks[0].getLocation(),
- diag::warn_pragma_unroll_cuda_value_in_parens);
-
if (Tok.isNot(tok::eod)) {
PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
<< "unroll";
diff --git a/clang/test/Parser/pragma-unroll-cuda.cu b/clang/test/Parser/pragma-unroll-cuda.cu
new file mode 100644
index 00000000000000..80a33d1815b034
--- /dev/null
+++ b/clang/test/Parser/pragma-unroll-cuda.cu
@@ -0,0 +1,48 @@
+// RUN: %clang_cc1 -x cuda -verify -Werror -Wcuda-compat %s
+// RUN: %clang_cc1 -x cuda -triple nvptx64-nvidia-cuda -fcuda-is-device -verify -Werror -Wcuda-compat %s
+// RUN: %clang_cc1 -x hip -verify -Werror -Wcuda-compat %s
+// RUN: %clang_cc1 -x hip -triple amdgcn-amd-amdhsa -fcuda-is-device -verify -Werror -Wcuda-compat %s
+
+// The argument to '#pragma unroll' may be parenthesized in CUDA/HIP. nvcc
+// accepts both forms and generates identical code, and the CUDA C++ Programming
+// Guide documents the parenthesized form (including `#pragma unroll (N + 1)` on
+// a template parameter), so this must not diagnose.
+
+// cc1 does not pull in the CUDA headers, so spell the attribute out.
+#define __device__ __attribute__((device))
+
+// expected-no-diagnostics
+
+void test(int *List, int Length) {
+#pragma unroll(4)
+ for (int i = 0; i < Length; ++i) {
+ List[i] = i;
+ }
+
+#pragma unroll 4
+ for (int i = 0; i < Length; ++i) {
+ List[i] = i;
+ }
+
+#pragma unroll (2 + 2)
+ for (int i = 0; i < Length; ++i) {
+ List[i] = i;
+ }
+}
+
+__device__ void device_test(int *List, int Length) {
+#pragma unroll(4)
+ for (int i = 0; i < Length; ++i) {
+ List[i] = i;
+ }
+}
+
+template <int N>
+void template_test(int *List, int Length) {
+#pragma unroll (N + 1)
+ for (int i = 0; i < Length; ++i) {
+ List[i] = i;
+ }
+}
+
+template void template_test<3>(int *, int);
diff --git a/clang/test/Parser/warn-cuda-compat.cu b/clang/test/Parser/warn-cuda-compat.cu
deleted file mode 100644
index c3aad6f6636054..00000000000000
--- a/clang/test/Parser/warn-cuda-compat.cu
+++ /dev/null
@@ -1,13 +0,0 @@
-// RUN: %clang_cc1 -Wno-cuda-compat -Werror %s
-// RUN: %clang_cc1 -Wcuda-compat -verify %s
-// RUN: %clang_cc1 -x c++ -Wcuda-compat -Werror %s
-
-// Note that this puts the expected lines before the directives to work around
-// limitations in the -verify mode.
-
-void test(int *List, int Length) {
-/* expected-warning {{argument to '#pragma unroll' should not be in parentheses in CUDA C/C++}} */#pragma unroll(4)
- for (int i = 0; i < Length; ++i) {
- List[i] = i;
- }
-}
More information about the cfe-commits
mailing list