[clang] [Clang] Add support for [[msvc::noinline]] attribute. (PR #91720)
Xu Zhang via cfe-commits
cfe-commits at lists.llvm.org
Sun May 19 04:58:43 PDT 2024
https://github.com/simonzgx updated https://github.com/llvm/llvm-project/pull/91720
>From 54b69712d2ffc3536d41d56194e67da802b92049 Mon Sep 17 00:00:00 2001
From: Xu Zhang <simonzgx at gmail.com>
Date: Fri, 10 May 2024 01:24:24 +0800
Subject: [PATCH 1/4] [Clang] Add support for [[msvc::noinline]] attribute.
(#90941)
---
clang/include/clang/Basic/Attr.td | 5 ++++-
clang/include/clang/Basic/AttrDocs.td | 2 ++
clang/test/CodeGen/attr-ms-noinline.cpp | 0
clang/test/Sema/attr-ms-noinline.cpp | 0
4 files changed, 6 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGen/attr-ms-noinline.cpp
create mode 100644 clang/test/Sema/attr-ms-noinline.cpp
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 7a7721239a28f..8532e5c47fe47 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1997,9 +1997,12 @@ def Convergent : InheritableAttr {
def NoInline : DeclOrStmtAttr {
let Spellings = [CustomKeyword<"__noinline__">, GCC<"noinline">,
CXX11<"clang", "noinline">, C23<"clang", "noinline">,
+ CXX11<"msvc", "noinline">, C23<"msvc", "noinline">,
Declspec<"noinline">];
let Accessors = [Accessor<"isClangNoInline", [CXX11<"clang", "noinline">,
- C23<"clang", "noinline">]>];
+ C23<"clang", "noinline">,
+ CXX11<"msvc", "noinline">,
+ C23<"msvc", "noinline">]>];
let Documentation = [NoInlineDocs];
let Subjects = SubjectList<[Function, Stmt], WarnDiag,
"functions and statements">;
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index b48aaf65558ac..7442f7e828462 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -597,6 +597,8 @@ spellings of the attribute are not supported on statements. If a statement is
marked ``[[clang::noinline]]`` and contains calls, those calls inside the
statement will not be inlined by the compiler.
+``[[msvc::noinline]]``
+
``__noinline__`` can be used as a keyword in CUDA/HIP languages. This is to
avoid diagnostics due to usage of ``__attribute__((__noinline__))``
with ``__noinline__`` defined as a macro as ``__attribute__((noinline))``.
diff --git a/clang/test/CodeGen/attr-ms-noinline.cpp b/clang/test/CodeGen/attr-ms-noinline.cpp
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Sema/attr-ms-noinline.cpp b/clang/test/Sema/attr-ms-noinline.cpp
new file mode 100644
index 0000000000000..e69de29bb2d1d
>From bbc27cb48f616a98d4d379b8ac72e0c408c8ca64 Mon Sep 17 00:00:00 2001
From: Xu Zhang <simonzgx at gmail.com>
Date: Fri, 10 May 2024 17:46:40 +0800
Subject: [PATCH 2/4] add unit test
---
clang/include/clang/Basic/AttrDocs.td | 2 -
clang/test/CodeGen/attr-ms-noinline.cpp | 53 ++++++++++++++++++++
clang/test/Sema/attr-ms-noinline.cpp | 66 +++++++++++++++++++++++++
3 files changed, 119 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 7442f7e828462..b48aaf65558ac 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -597,8 +597,6 @@ spellings of the attribute are not supported on statements. If a statement is
marked ``[[clang::noinline]]`` and contains calls, those calls inside the
statement will not be inlined by the compiler.
-``[[msvc::noinline]]``
-
``__noinline__`` can be used as a keyword in CUDA/HIP languages. This is to
avoid diagnostics due to usage of ``__attribute__((__noinline__))``
with ``__noinline__`` defined as a macro as ``__attribute__((noinline))``.
diff --git a/clang/test/CodeGen/attr-ms-noinline.cpp b/clang/test/CodeGen/attr-ms-noinline.cpp
index e69de29bb2d1d..99b0a05af715d 100644
--- a/clang/test/CodeGen/attr-ms-noinline.cpp
+++ b/clang/test/CodeGen/attr-ms-noinline.cpp
@@ -0,0 +1,53 @@
+// RUN: %clang_cc1 -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
+
+bool bar();
+void f(bool, bool);
+void g(bool);
+
+static int baz(int x) {
+ return x * 10;
+}
+
+[[msvc::noinline]] bool noi() { }
+
+void foo(int i) {
+ [[msvc::noinline]] bar();
+// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR:[0-9]+]]
+ [[msvc::noinline]] i = baz(i);
+// CHECK: call noundef i32 @_ZL3bazi({{.*}}) #[[NOINLINEATTR]]
+ [[msvc::noinline]] (i = 4, bar());
+// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
+ [[msvc::noinline]] (void)(bar());
+// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
+ [[msvc::noinline]] f(bar(), bar());
+// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
+// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
+// CHECK: call void @_Z1fbb({{.*}}) #[[NOINLINEATTR]]
+ [[msvc::noinline]] [] { bar(); bar(); }(); // noinline only applies to the anonymous function call
+// CHECK: call void @"_ZZ3fooiENK3$_0clEv"(ptr {{[^,]*}} %ref.tmp) #[[NOINLINEATTR]]
+ [[msvc::noinline]] for (bar(); bar(); bar()) {}
+// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
+// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
+// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
+ bar();
+// CHECK: call noundef zeroext i1 @_Z3barv()
+ [[msvc::noinline]] noi();
+// CHECK: call noundef zeroext i1 @_Z3noiv()
+ noi();
+// CHECK: call noundef zeroext i1 @_Z3noiv()
+}
+
+struct S {
+ friend bool operator==(const S &LHS, const S &RHS);
+};
+
+void func(const S &s1, const S &s2) {
+ [[msvc::noinline]]g(s1 == s2);
+// CHECK: call noundef zeroext i1 @_ZeqRK1SS1_({{.*}}) #[[NOINLINEATTR]]
+// CHECK: call void @_Z1gb({{.*}}) #[[NOINLINEATTR]]
+ bool b;
+ [[msvc::noinline]] b = s1 == s2;
+// CHECK: call noundef zeroext i1 @_ZeqRK1SS1_({{.*}}) #[[NOINLINEATTR]]
+}
+
+// CHECK: attributes #[[NOINLINEATTR]] = { noinline }
diff --git a/clang/test/Sema/attr-ms-noinline.cpp b/clang/test/Sema/attr-ms-noinline.cpp
index e69de29bb2d1d..080939396565c 100644
--- a/clang/test/Sema/attr-ms-noinline.cpp
+++ b/clang/test/Sema/attr-ms-noinline.cpp
@@ -0,0 +1,66 @@
+// RUN: %clang_cc1 -verify -fsyntax-only %s -Wno-c++17-extensions
+
+int bar();
+
+void foo() {
+ [[msvc::noinline]] bar();
+ [[msvc::noinline(0)]] bar(); // expected-error {{'noinline' attribute takes no arguments}}
+ int x;
+ [[msvc::noinline]] x = 0; // expected-warning {{'noinline' attribute is ignored because there exists no call expression inside the statement}}
+ [[msvc::noinline]] { asm("nop"); } // expected-warning {{'noinline' attribute is ignored because there exists no call expression inside the statement}}
+ [[msvc::noinline]] label: x = 1; // expected-warning {{'noinline' attribute only applies to functions and statements}}
+
+
+ [[msvc::noinline]] always_inline_fn(); // expected-warning {{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
+ [[msvc::noinline]] flatten_fn(); // expected-warning {{statement attribute 'noinline' has higher precedence than function attribute 'flatten'}}
+ [[msvc::noinline]] noinline_fn();
+}
+
+[[msvc::noinline]] static int i = bar(); // expected-warning {{'noinline' attribute only applies to functions and statements}}
+
+// This used to crash the compiler.
+template<int D>
+int foo(int x) {
+ [[msvc::noinline]] return foo<D-1>(x + 1);
+}
+
+template<int D>
+[[clang::always_inline]]
+int dependent(int x){ return x + D;} // #DEP
+[[clang::always_inline]]
+int non_dependent(int x){return x;} // #NO_DEP
+
+template<int D> [[clang::always_inline]]
+int baz(int x) { // #BAZ
+ // expected-warning at +2{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
+ // expected-note@#NO_DEP{{conflicting attribute is here}}
+ [[msvc::noinline]] non_dependent(x);
+ if constexpr (D>0) {
+ // expected-warning at +6{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
+ // expected-note@#NO_DEP{{conflicting attribute is here}}
+ // expected-warning at +4 3{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
+ // expected-note@#BAZ 3{{conflicting attribute is here}}
+ // expected-note@#BAZ_INST 3{{in instantiation}}
+ // expected-note at +1 3{{in instantiation}}
+ [[msvc::noinline]] return non_dependent(x), baz<D-1>(x + 1);
+ }
+ return x;
+}
+
+// We can't suppress if there is a variadic involved.
+template<int ... D>
+int variadic_baz(int x) {
+ // Diagnoses NO_DEP 2x, once during phase 1, the second during instantiation.
+ // Dianoses DEP 3x, once per variadic expansion.
+ // expected-warning at +5 2{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
+ // expected-note@#NO_DEP 2{{conflicting attribute is here}}
+ // expected-warning at +3 3{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
+ // expected-note@#DEP 3{{conflicting attribute is here}}
+ // expected-note@#VARIADIC_INST{{in instantiation}}
+ [[msvc::noinline]] return non_dependent(x) + (dependent<D>(x) + ...);
+}
+
+void use() {
+ baz<3>(0); // #BAZ_INST
+ variadic_baz<0, 1, 2>(0); // #VARIADIC_INST
+}
>From 4aea776561a7ac4fd6b29a4b3be83645efb775a3 Mon Sep 17 00:00:00 2001
From: Xu Zhang <simonzgx at gmail.com>
Date: Sat, 11 May 2024 15:01:24 +0800
Subject: [PATCH 3/4] fix unit test compile error
---
clang/test/Sema/attr-ms-noinline.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang/test/Sema/attr-ms-noinline.cpp b/clang/test/Sema/attr-ms-noinline.cpp
index 080939396565c..dc99691d4da54 100644
--- a/clang/test/Sema/attr-ms-noinline.cpp
+++ b/clang/test/Sema/attr-ms-noinline.cpp
@@ -2,6 +2,12 @@
int bar();
+// expected-note at +1{{conflicting attribute is here}}
+[[gnu::always_inline]] void always_inline_fn(void) { }
+// expected-note at +1{{conflicting attribute is here}}
+[[gnu::flatten]] void flatten_fn(void) { }
+[[gnu::noinline]] void noinline_fn(void) { }
+
void foo() {
[[msvc::noinline]] bar();
[[msvc::noinline(0)]] bar(); // expected-error {{'noinline' attribute takes no arguments}}
>From c382cbd064da7bdcde737ede002a6e49c83240ea Mon Sep 17 00:00:00 2001
From: Xu Zhang <simonzgx at gmail.com>
Date: Sun, 19 May 2024 19:56:56 +0800
Subject: [PATCH 4/4] update accessor name & delete redundant test
---
clang/include/clang/Basic/Attr.td | 8 +--
clang/lib/Sema/SemaStmtAttr.cpp | 2 +-
clang/test/CodeGen/attr-ms-noinline.cpp | 53 ------------------
clang/test/Sema/attr-ms-noinline.cpp | 72 -------------------------
4 files changed, 5 insertions(+), 130 deletions(-)
delete mode 100644 clang/test/CodeGen/attr-ms-noinline.cpp
delete mode 100644 clang/test/Sema/attr-ms-noinline.cpp
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 8532e5c47fe47..d351c51e30329 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1999,10 +1999,10 @@ def NoInline : DeclOrStmtAttr {
CXX11<"clang", "noinline">, C23<"clang", "noinline">,
CXX11<"msvc", "noinline">, C23<"msvc", "noinline">,
Declspec<"noinline">];
- let Accessors = [Accessor<"isClangNoInline", [CXX11<"clang", "noinline">,
- C23<"clang", "noinline">,
- CXX11<"msvc", "noinline">,
- C23<"msvc", "noinline">]>];
+ let Accessors = [Accessor<"isStmtNoInline", [CXX11<"clang", "noinline">,
+ C23<"clang", "noinline">,
+ CXX11<"msvc", "noinline">,
+ C23<"msvc", "noinline">]>];
let Documentation = [NoInlineDocs];
let Subjects = SubjectList<[Function, Stmt], WarnDiag,
"functions and statements">;
diff --git a/clang/lib/Sema/SemaStmtAttr.cpp b/clang/lib/Sema/SemaStmtAttr.cpp
index 36f8ecadcfab7..475f48a37db29 100644
--- a/clang/lib/Sema/SemaStmtAttr.cpp
+++ b/clang/lib/Sema/SemaStmtAttr.cpp
@@ -285,7 +285,7 @@ bool Sema::CheckAlwaysInlineAttr(const Stmt *OrigSt, const Stmt *CurSt,
static Attr *handleNoInlineAttr(Sema &S, Stmt *St, const ParsedAttr &A,
SourceRange Range) {
NoInlineAttr NIA(S.Context, A);
- if (!NIA.isClangNoInline()) {
+ if (!NIA.isStmtNoInline()) {
S.Diag(St->getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
<< "[[clang::noinline]]";
return nullptr;
diff --git a/clang/test/CodeGen/attr-ms-noinline.cpp b/clang/test/CodeGen/attr-ms-noinline.cpp
deleted file mode 100644
index 99b0a05af715d..0000000000000
--- a/clang/test/CodeGen/attr-ms-noinline.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-// RUN: %clang_cc1 -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
-
-bool bar();
-void f(bool, bool);
-void g(bool);
-
-static int baz(int x) {
- return x * 10;
-}
-
-[[msvc::noinline]] bool noi() { }
-
-void foo(int i) {
- [[msvc::noinline]] bar();
-// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR:[0-9]+]]
- [[msvc::noinline]] i = baz(i);
-// CHECK: call noundef i32 @_ZL3bazi({{.*}}) #[[NOINLINEATTR]]
- [[msvc::noinline]] (i = 4, bar());
-// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
- [[msvc::noinline]] (void)(bar());
-// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
- [[msvc::noinline]] f(bar(), bar());
-// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
-// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
-// CHECK: call void @_Z1fbb({{.*}}) #[[NOINLINEATTR]]
- [[msvc::noinline]] [] { bar(); bar(); }(); // noinline only applies to the anonymous function call
-// CHECK: call void @"_ZZ3fooiENK3$_0clEv"(ptr {{[^,]*}} %ref.tmp) #[[NOINLINEATTR]]
- [[msvc::noinline]] for (bar(); bar(); bar()) {}
-// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
-// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
-// CHECK: call noundef zeroext i1 @_Z3barv() #[[NOINLINEATTR]]
- bar();
-// CHECK: call noundef zeroext i1 @_Z3barv()
- [[msvc::noinline]] noi();
-// CHECK: call noundef zeroext i1 @_Z3noiv()
- noi();
-// CHECK: call noundef zeroext i1 @_Z3noiv()
-}
-
-struct S {
- friend bool operator==(const S &LHS, const S &RHS);
-};
-
-void func(const S &s1, const S &s2) {
- [[msvc::noinline]]g(s1 == s2);
-// CHECK: call noundef zeroext i1 @_ZeqRK1SS1_({{.*}}) #[[NOINLINEATTR]]
-// CHECK: call void @_Z1gb({{.*}}) #[[NOINLINEATTR]]
- bool b;
- [[msvc::noinline]] b = s1 == s2;
-// CHECK: call noundef zeroext i1 @_ZeqRK1SS1_({{.*}}) #[[NOINLINEATTR]]
-}
-
-// CHECK: attributes #[[NOINLINEATTR]] = { noinline }
diff --git a/clang/test/Sema/attr-ms-noinline.cpp b/clang/test/Sema/attr-ms-noinline.cpp
deleted file mode 100644
index dc99691d4da54..0000000000000
--- a/clang/test/Sema/attr-ms-noinline.cpp
+++ /dev/null
@@ -1,72 +0,0 @@
-// RUN: %clang_cc1 -verify -fsyntax-only %s -Wno-c++17-extensions
-
-int bar();
-
-// expected-note at +1{{conflicting attribute is here}}
-[[gnu::always_inline]] void always_inline_fn(void) { }
-// expected-note at +1{{conflicting attribute is here}}
-[[gnu::flatten]] void flatten_fn(void) { }
-[[gnu::noinline]] void noinline_fn(void) { }
-
-void foo() {
- [[msvc::noinline]] bar();
- [[msvc::noinline(0)]] bar(); // expected-error {{'noinline' attribute takes no arguments}}
- int x;
- [[msvc::noinline]] x = 0; // expected-warning {{'noinline' attribute is ignored because there exists no call expression inside the statement}}
- [[msvc::noinline]] { asm("nop"); } // expected-warning {{'noinline' attribute is ignored because there exists no call expression inside the statement}}
- [[msvc::noinline]] label: x = 1; // expected-warning {{'noinline' attribute only applies to functions and statements}}
-
-
- [[msvc::noinline]] always_inline_fn(); // expected-warning {{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
- [[msvc::noinline]] flatten_fn(); // expected-warning {{statement attribute 'noinline' has higher precedence than function attribute 'flatten'}}
- [[msvc::noinline]] noinline_fn();
-}
-
-[[msvc::noinline]] static int i = bar(); // expected-warning {{'noinline' attribute only applies to functions and statements}}
-
-// This used to crash the compiler.
-template<int D>
-int foo(int x) {
- [[msvc::noinline]] return foo<D-1>(x + 1);
-}
-
-template<int D>
-[[clang::always_inline]]
-int dependent(int x){ return x + D;} // #DEP
-[[clang::always_inline]]
-int non_dependent(int x){return x;} // #NO_DEP
-
-template<int D> [[clang::always_inline]]
-int baz(int x) { // #BAZ
- // expected-warning at +2{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
- // expected-note@#NO_DEP{{conflicting attribute is here}}
- [[msvc::noinline]] non_dependent(x);
- if constexpr (D>0) {
- // expected-warning at +6{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
- // expected-note@#NO_DEP{{conflicting attribute is here}}
- // expected-warning at +4 3{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
- // expected-note@#BAZ 3{{conflicting attribute is here}}
- // expected-note@#BAZ_INST 3{{in instantiation}}
- // expected-note at +1 3{{in instantiation}}
- [[msvc::noinline]] return non_dependent(x), baz<D-1>(x + 1);
- }
- return x;
-}
-
-// We can't suppress if there is a variadic involved.
-template<int ... D>
-int variadic_baz(int x) {
- // Diagnoses NO_DEP 2x, once during phase 1, the second during instantiation.
- // Dianoses DEP 3x, once per variadic expansion.
- // expected-warning at +5 2{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
- // expected-note@#NO_DEP 2{{conflicting attribute is here}}
- // expected-warning at +3 3{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
- // expected-note@#DEP 3{{conflicting attribute is here}}
- // expected-note@#VARIADIC_INST{{in instantiation}}
- [[msvc::noinline]] return non_dependent(x) + (dependent<D>(x) + ...);
-}
-
-void use() {
- baz<3>(0); // #BAZ_INST
- variadic_baz<0, 1, 2>(0); // #VARIADIC_INST
-}
More information about the cfe-commits
mailing list