https://github.com/LuoYuanke updated https://github.com/llvm/llvm-project/pull/244791
>From 4a0e4f79d80705067b8b289d607c5da135467701 Mon Sep 17 00:00:00 2001 From: Yuanke Luo <[email protected]> Date: Fri, 20 Nov 2026 17:13:05 +0800 Subject: [PATCH 1/2] Remove parenthesized #pragma unroll warning nvcc accepts both `#pragma unroll N` and `#pragma unroll (N)` and generates identical code, and the CUDA C++ Programming Guide documents the parenthesized form. Assisted-By Claude --- clang/docs/ReleaseNotes.md | 5 ++++ .../clang/Basic/DiagnosticParseKinds.td | 5 ---- clang/lib/Parse/ParsePragma.cpp | 6 ----- clang/test/Parser/pragma-unroll-cuda.cu | 25 +++++++++++++++++++ clang/test/Parser/warn-cuda-compat.cu | 13 ---------- 5 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 clang/test/Parser/pragma-unroll-cuda.cu delete mode 100644 clang/test/Parser/warn-cuda-compat.cu diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 17bb8a19325aec..8fac01894813d5 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1544,6 +1544,11 @@ 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. `nvcc` accepts both `#pragma unroll N` and + `#pragma unroll (N)` with identical results, and the CUDA C++ Programming + Guide documents the parenthesized form, so the warning was incorrect. + - 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..e0acc0a8934091 --- /dev/null +++ b/clang/test/Parser/pragma-unroll-cuda.cu @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -x cuda -verify -Werror %s +// RUN: %clang_cc1 -x hip -verify -Werror %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, so this must not diagnose. + +// 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; + } +} 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; - } -} >From cada553a893c6768ae43a04f5b5321c696f655ad Mon Sep 17 00:00:00 2001 From: Yuanke Luo <[email protected]> Date: Sun, 22 Nov 2026 09:12:06 +0800 Subject: [PATCH 2/2] Address review comments on #pragma unroll test --- clang/docs/ReleaseNotes.md | 8 ++++--- clang/test/Parser/pragma-unroll-cuda.cu | 29 ++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 8fac01894813d5..e47d8eeb29412e 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1545,9 +1545,11 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and (`__cudaRegisterLinkedBinary`). - Removed the `-Wcuda-compat` warning about a parenthesized argument to - `#pragma unroll` in CUDA and HIP. `nvcc` accepts both `#pragma unroll N` and - `#pragma unroll (N)` with identical results, and the CUDA C++ Programming - Guide documents the parenthesized form, so the warning was incorrect. + `#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) diff --git a/clang/test/Parser/pragma-unroll-cuda.cu b/clang/test/Parser/pragma-unroll-cuda.cu index e0acc0a8934091..80a33d1815b034 100644 --- a/clang/test/Parser/pragma-unroll-cuda.cu +++ b/clang/test/Parser/pragma-unroll-cuda.cu @@ -1,9 +1,15 @@ -// RUN: %clang_cc1 -x cuda -verify -Werror %s -// RUN: %clang_cc1 -x hip -verify -Werror %s +// 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, so this must not diagnose. +// 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 @@ -23,3 +29,20 @@ void test(int *List, int Length) { 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); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
