[PATCH] D137251: [clang][cuda/hip] Allow `__noinline__` lambdas

Pierre van Houtryve via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 2 06:42:54 PDT 2022


Pierre-vh created this revision.
Pierre-vh added reviewers: yaxunl, tra, aaron.ballman, rsmith.
Herald added a subscriber: mattd.
Herald added a project: All.
Pierre-vh requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

D124866 <https://reviews.llvm.org/D124866> seem to have had an unintended side effect: __noinline__ on lambdas was no longer accepted.

This fixes the regression and adds a test case for it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137251

Files:
  clang/lib/Parse/ParseExprCXX.cpp
  clang/test/Parser/lambda-attr.cu


Index: clang/test/Parser/lambda-attr.cu
===================================================================
--- clang/test/Parser/lambda-attr.cu
+++ clang/test/Parser/lambda-attr.cu
@@ -18,6 +18,10 @@
   ([&](int) __attribute__((device)){ device_fn(); })(0);
   // expected-warning at -1 {{nvcc does not allow '__device__' to appear after the parameter list in lambdas}}
   ([&] __attribute__((device)) (int) { device_fn(); })(0);
+
+  // test that noinline can appear anywhere.
+  ([&] __attribute__((device)) __noinline__ () { device_fn(); })();
+  ([&] __noinline__ __attribute__((device)) () { device_fn(); })();
 }
 
 __attribute__((host)) __attribute__((device)) void host_device_attrs() {
@@ -37,6 +41,11 @@
   // expected-warning at -1 {{nvcc does not allow '__host__' to appear after the parameter list in lambdas}}
   // expected-warning at -2 {{nvcc does not allow '__device__' to appear after the parameter list in lambdas}}
   ([&] __attribute__((host)) __attribute__((device)) (int) { hd_fn(); })(0);
+
+  // test that noinline can also appear anywhere.
+  ([] __attribute__((host)) __attribute__((device)) () { hd_fn(); })();
+  ([] __attribute__((host)) __noinline__ __attribute__((device)) () { hd_fn(); })();
+  ([] __attribute__((host)) __attribute__((device)) __noinline__ () { hd_fn(); })();
 }
 
 // TODO: Add tests for __attribute__((global)) once we support global lambdas.
Index: clang/lib/Parse/ParseExprCXX.cpp
===================================================================
--- clang/lib/Parse/ParseExprCXX.cpp
+++ clang/lib/Parse/ParseExprCXX.cpp
@@ -1291,7 +1291,23 @@
   if (getLangOpts().CUDA) {
     // In CUDA code, GNU attributes are allowed to appear immediately after the
     // "[...]", even if there is no "(...)" before the lambda body.
-    MaybeParseGNUAttributes(D);
+    //
+    // Note that we support __noinline__ as a keyword in this mode and thus
+    // it has to be separately handled.
+    while (true) {
+      if (Tok.is(tok::kw___attribute)) {
+        ParseGNUAttributes(Attr, nullptr, &D);
+      } else if (Tok.is(tok::kw___noinline__)) {
+        IdentifierInfo *AttrName = Tok.getIdentifierInfo();
+        SourceLocation AttrNameLoc = ConsumeToken();
+        Attr.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
+                    ParsedAttr::AS_Keyword);
+      } else {
+        break;
+      }
+    }
+
+    D.takeAttributes(Attr);
   }
 
   // Helper to emit a warning if we see a CUDA host/device/global attribute


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137251.472596.patch
Type: text/x-patch
Size: 2494 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221102/e597b2da/attachment.bin>


More information about the cfe-commits mailing list