[clang] [Clang] Add support for [[msvc::noinline]] attribute. (PR #91720)
Xu Zhang via cfe-commits
cfe-commits at lists.llvm.org
Sat May 11 00:01:46 PDT 2024
https://github.com/simonzgx updated https://github.com/llvm/llvm-project/pull/91720
>From 2dd3d3385642d88d538bc37673018f1231a56b14 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/3] [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 52552ba488560..81fcb8aba04f3 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 f351822ac74bd..d5540f70ac927 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 20292794444c3c8fe13920f92547ddc2a5db305f 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/3] 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 d5540f70ac927..f351822ac74bd 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 3f834a5d06fba81e3a1dd04fab41b1b2a01f7a22 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/3] 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}}
More information about the cfe-commits
mailing list