[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-23 Thread Alexandros Lamprineas via cfe-commits

https://github.com/labrinea closed 
https://github.com/llvm/llvm-project/pull/93044
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-23 Thread Alexandros Lamprineas via cfe-commits

https://github.com/labrinea updated 
https://github.com/llvm/llvm-project/pull/93044

>From ba2b8b53d80e17b1477a7d6fed51f0450bc539a3 Mon Sep 17 00:00:00 2001
From: Alexandros Lamprineas 
Date: Wed, 22 May 2024 15:55:58 +0100
Subject: [PATCH] [clang][FMV] Allow declaration of function versions in
 namespaces.

Fixes the following bug:

namespace Name {
int __attribute((target_version("default"))) foo() { return 0; }
}

namespace Name {
int __attribute((target_version("sve"))) foo() { return 1; }
}

int bar() { return Name::foo(); }

error: redefinition of 'foo'
  int __attribute((target_version("sve"))) foo() { return 1; }

note: previous definition is here
  int __attribute((target_version("default"))) foo() { return 0; }

While fixing this I also found that in the absence of default version
declaration, the one we implicitly create has incorrect mangling if
we are in a namespace:

namespace OtherName {
int __attribute((target_version("sve"))) foo() { return 2; }
}

int baz() { return OtherName::foo(); }

In this example instead of creating a declaration for the symbol
@_ZN9OtherName3fooEv.default we are creating one for the symbol
@_Z3foov.default (the namespace mangling prefix is omitted).
This has now been fixed.
---
 clang/lib/CodeGen/CodeGenModule.cpp |  2 +-
 clang/lib/Sema/SemaDecl.cpp |  4 +-
 clang/test/CodeGenCXX/fmv-namespace.cpp | 93 +
 clang/test/Sema/fmv-namespace.cpp   | 12 
 4 files changed, 108 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/fmv-namespace.cpp
 create mode 100644 clang/test/Sema/fmv-namespace.cpp

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 60ef28a0effaa..e4774a587707a 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -4150,7 +4150,7 @@ llvm::GlobalValue::LinkageTypes 
getMultiversionLinkage(CodeGenModule ,
 }
 
 static FunctionDecl *createDefaultTargetVersionFrom(const FunctionDecl *FD) {
-  DeclContext *DeclCtx = FD->getASTContext().getTranslationUnitDecl();
+  auto *DeclCtx = const_cast(FD->getDeclContext());
   TypeSourceInfo *TInfo = FD->getTypeSourceInfo();
   StorageClass SC = FD->getStorageClass();
   DeclarationName Name = FD->getNameInfo().getName();
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b5c3a27ab06e9..2a87b26f17a2b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11868,8 +11868,8 @@ static bool CheckMultiVersionFunction(Sema , 
FunctionDecl *NewFD,
 return false;
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
-  OldDecl->getDeclContext()->getRedeclContext() !=
-  NewFD->getDeclContext()->getRedeclContext()) {
+  !OldDecl->getDeclContext()->getRedeclContext()->Equals(
+  NewFD->getDeclContext()->getRedeclContext())) {
 // If there's no previous declaration, AND this isn't attempting to cause
 // multiversioning, this isn't an error condition.
 if (MVKind == MultiVersionKind::None)
diff --git a/clang/test/CodeGenCXX/fmv-namespace.cpp 
b/clang/test/CodeGenCXX/fmv-namespace.cpp
new file mode 100644
index 0..5bcd0da06eebc
--- /dev/null
+++ b/clang/test/CodeGenCXX/fmv-namespace.cpp
@@ -0,0 +1,93 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --check-globals all --include-generated-funcs --version 5
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+namespace Name {
+int __attribute((target_version("default"))) foo() { return 0; }
+}
+
+namespace Name {
+int __attribute((target_version("sve"))) foo() { return 1; }
+}
+
+int bar() { return Name::foo(); }
+
+namespace OtherName {
+int __attribute((target_version("sve"))) foo() { return 2; }
+}
+
+int baz() { return OtherName::foo(); }
+
+//.
+// CHECK: @__aarch64_cpu_features = external dso_local global { i64 }
+// CHECK: @_ZN4Name3fooEv.ifunc = weak_odr alias i32 (), ptr @_ZN4Name3fooEv
+// CHECK: @_ZN9OtherName3fooEv.ifunc = weak_odr alias i32 (), ptr 
@_ZN9OtherName3fooEv
+// CHECK: @_ZN4Name3fooEv = weak_odr ifunc i32 (), ptr @_ZN4Name3fooEv.resolver
+// CHECK: @_ZN9OtherName3fooEv = weak_odr ifunc i32 (), ptr 
@_ZN9OtherName3fooEv.resolver
+//.
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv.default(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 0
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv._Msve(
+// CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 1
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_Z3barv(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:[[CALL:%.*]] = call noundef i32 @_ZN4Name3fooEv()
+// CHECK-NEXT:ret i32 [[CALL]]
+//
+//
+// CHECK-LABEL: define weak_odr ptr @_ZN4Name3fooEv.resolver() comdat {
+// CHECK-NEXT:  [[RESOLVER_ENTRY:.*:]]
+// CHECK-NEXT:call 

[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-23 Thread Alexandros Lamprineas via cfe-commits

https://github.com/labrinea updated 
https://github.com/llvm/llvm-project/pull/93044

>From 730c8c5081862b330d66455a0495e9d317da1795 Mon Sep 17 00:00:00 2001
From: Alexandros Lamprineas 
Date: Wed, 22 May 2024 15:55:58 +0100
Subject: [PATCH] [clang][FMV] Allow declaration of function versions in
 namespaces.

Fixes the following bug:

namespace Name {
int __attribute((target_version("default"))) foo() { return 0; }
}

namespace Name {
int __attribute((target_version("sve"))) foo() { return 1; }
}

int bar() { return Name::foo(); }

error: redefinition of 'foo'
  int __attribute((target_version("sve"))) foo() { return 1; }

note: previous definition is here
  int __attribute((target_version("default"))) foo() { return 0; }

While fixing this I also found that in the absence of default version
declaration, the one we implicitly create has incorrect mangling if
we are in a namespace:

namespace OtherName {
int __attribute((target_version("sve"))) foo() { return 2; }
}

int baz() { return OtherName::foo(); }

In this example instead of creating a declaration for the symbol
@_ZN9OtherName3fooEv.default we are creating one for the symbol
@_Z3foov.default (the namespace mangling prefix is omitted).
This has now been fixed.
---
 clang/lib/CodeGen/CodeGenModule.cpp |  2 +-
 clang/lib/Sema/SemaDecl.cpp |  4 +-
 clang/test/CodeGenCXX/fmv-namespace.cpp | 93 +
 clang/test/Sema/fmv-namespace.cpp   | 12 
 4 files changed, 108 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/fmv-namespace.cpp
 create mode 100644 clang/test/Sema/fmv-namespace.cpp

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 60ef28a0effaa..e4774a587707a 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -4150,7 +4150,7 @@ llvm::GlobalValue::LinkageTypes 
getMultiversionLinkage(CodeGenModule ,
 }
 
 static FunctionDecl *createDefaultTargetVersionFrom(const FunctionDecl *FD) {
-  DeclContext *DeclCtx = FD->getASTContext().getTranslationUnitDecl();
+  auto *DeclCtx = const_cast(FD->getDeclContext());
   TypeSourceInfo *TInfo = FD->getTypeSourceInfo();
   StorageClass SC = FD->getStorageClass();
   DeclarationName Name = FD->getNameInfo().getName();
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b5c3a27ab06e9..10105d5fe9efd 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11868,8 +11868,8 @@ static bool CheckMultiVersionFunction(Sema , 
FunctionDecl *NewFD,
 return false;
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
-  OldDecl->getDeclContext()->getRedeclContext() !=
-  NewFD->getDeclContext()->getRedeclContext()) {
+  !OldDecl->getDeclContext()->getRedeclContext()->Equals(
+   NewFD->getDeclContext()->getRedeclContext())) {
 // If there's no previous declaration, AND this isn't attempting to cause
 // multiversioning, this isn't an error condition.
 if (MVKind == MultiVersionKind::None)
diff --git a/clang/test/CodeGenCXX/fmv-namespace.cpp 
b/clang/test/CodeGenCXX/fmv-namespace.cpp
new file mode 100644
index 0..5bcd0da06eebc
--- /dev/null
+++ b/clang/test/CodeGenCXX/fmv-namespace.cpp
@@ -0,0 +1,93 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --check-globals all --include-generated-funcs --version 5
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+namespace Name {
+int __attribute((target_version("default"))) foo() { return 0; }
+}
+
+namespace Name {
+int __attribute((target_version("sve"))) foo() { return 1; }
+}
+
+int bar() { return Name::foo(); }
+
+namespace OtherName {
+int __attribute((target_version("sve"))) foo() { return 2; }
+}
+
+int baz() { return OtherName::foo(); }
+
+//.
+// CHECK: @__aarch64_cpu_features = external dso_local global { i64 }
+// CHECK: @_ZN4Name3fooEv.ifunc = weak_odr alias i32 (), ptr @_ZN4Name3fooEv
+// CHECK: @_ZN9OtherName3fooEv.ifunc = weak_odr alias i32 (), ptr 
@_ZN9OtherName3fooEv
+// CHECK: @_ZN4Name3fooEv = weak_odr ifunc i32 (), ptr @_ZN4Name3fooEv.resolver
+// CHECK: @_ZN9OtherName3fooEv = weak_odr ifunc i32 (), ptr 
@_ZN9OtherName3fooEv.resolver
+//.
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv.default(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 0
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv._Msve(
+// CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 1
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_Z3barv(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:[[CALL:%.*]] = call noundef i32 @_ZN4Name3fooEv()
+// CHECK-NEXT:ret i32 [[CALL]]
+//
+//
+// CHECK-LABEL: define weak_odr ptr @_ZN4Name3fooEv.resolver() comdat {
+// CHECK-NEXT:  [[RESOLVER_ENTRY:.*:]]
+// CHECK-NEXT:call 

[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-23 Thread Alexandros Lamprineas via cfe-commits


@@ -11868,8 +11868,10 @@ static bool CheckMultiVersionFunction(Sema , 
FunctionDecl *NewFD,
 return false;
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
-  OldDecl->getDeclContext()->getRedeclContext() !=
-  NewFD->getDeclContext()->getRedeclContext()) {
+  (OldDecl->getDeclContext()->getRedeclContext() !=
+   NewFD->getDeclContext()->getRedeclContext() &&
+   OldDecl->getDeclContext()->getEnclosingNamespaceContext() !=
+   NewFD->getDeclContext()->getEnclosingNamespaceContext())) {

labrinea wrote:

Thanks for the suggestion! I'll push a revised version.

https://github.com/llvm/llvm-project/pull/93044
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread Eli Friedman via cfe-commits


@@ -11868,8 +11868,10 @@ static bool CheckMultiVersionFunction(Sema , 
FunctionDecl *NewFD,
 return false;
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
-  OldDecl->getDeclContext()->getRedeclContext() !=
-  NewFD->getDeclContext()->getRedeclContext()) {
+  (OldDecl->getDeclContext()->getRedeclContext() !=
+   NewFD->getDeclContext()->getRedeclContext() &&
+   OldDecl->getDeclContext()->getEnclosingNamespaceContext() !=
+   NewFD->getDeclContext()->getEnclosingNamespaceContext())) {

efriedma-quic wrote:

```suggestion
  !OldDecl->getDeclContext()->getRedeclContext()->Equals(
  NewFD->getDeclContext()->getRedeclContext())) {
```

https://github.com/llvm/llvm-project/pull/93044
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread Jon Roelofs via cfe-commits

https://github.com/jroelofs approved this pull request.


https://github.com/llvm/llvm-project/pull/93044
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread Alexandros Lamprineas via cfe-commits

labrinea wrote:

I found another bug for implicitly declared default versions not having the 
namespece mangling. Fixed now, please take another look.

https://github.com/llvm/llvm-project/pull/93044
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread Alexandros Lamprineas via cfe-commits

https://github.com/labrinea edited 
https://github.com/llvm/llvm-project/pull/93044
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread Alexandros Lamprineas via cfe-commits

https://github.com/labrinea updated 
https://github.com/llvm/llvm-project/pull/93044

>From 1eb344d7a5e63c3e4e9e58f9141697d9d62b428c Mon Sep 17 00:00:00 2001
From: Alexandros Lamprineas 
Date: Wed, 22 May 2024 15:55:58 +0100
Subject: [PATCH] [clang][FMV] Allow declaration of function versions in
 namespaces.

Fixes the following bug:

namespace Name {
int __attribute((target_version("default"))) foo() { return 0; }
}

namespace Name {
int __attribute((target_version("sve"))) foo() { return 1; }
}

int bar() { return Name::foo(); }

error: redefinition of 'foo'
  int __attribute((target_version("sve"))) foo() { return 1; }

note: previous definition is here
  int __attribute((target_version("default"))) foo() { return 0; }

While fixing this I also found that in the absence of default version
declaration, the one we implicitly create has incorrect mangling if
we are in a namespace:

namespace OtherName {
int __attribute((target_version("sve"))) foo() { return 2; }
}

int baz() { return OtherName::foo(); }

In this example instead of creating a declaration for the symbol
@_ZN9OtherName3fooEv.default we are creating one for the symbol
@_Z3foov.default (the namespace mangling prefix is omitted).
This has now been fixed.
---
 clang/lib/CodeGen/CodeGenModule.cpp |  2 +-
 clang/lib/Sema/SemaDecl.cpp |  6 +-
 clang/test/CodeGenCXX/fmv-namespace.cpp | 93 +
 clang/test/Sema/fmv-namespace.cpp   | 12 
 4 files changed, 110 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/fmv-namespace.cpp
 create mode 100644 clang/test/Sema/fmv-namespace.cpp

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 60ef28a0effaa..e4774a587707a 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -4150,7 +4150,7 @@ llvm::GlobalValue::LinkageTypes 
getMultiversionLinkage(CodeGenModule ,
 }
 
 static FunctionDecl *createDefaultTargetVersionFrom(const FunctionDecl *FD) {
-  DeclContext *DeclCtx = FD->getASTContext().getTranslationUnitDecl();
+  auto *DeclCtx = const_cast(FD->getDeclContext());
   TypeSourceInfo *TInfo = FD->getTypeSourceInfo();
   StorageClass SC = FD->getStorageClass();
   DeclarationName Name = FD->getNameInfo().getName();
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b5c3a27ab06e9..99057c50cf593 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11868,8 +11868,10 @@ static bool CheckMultiVersionFunction(Sema , 
FunctionDecl *NewFD,
 return false;
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
-  OldDecl->getDeclContext()->getRedeclContext() !=
-  NewFD->getDeclContext()->getRedeclContext()) {
+  (OldDecl->getDeclContext()->getRedeclContext() !=
+   NewFD->getDeclContext()->getRedeclContext() &&
+   OldDecl->getDeclContext()->getEnclosingNamespaceContext() !=
+   NewFD->getDeclContext()->getEnclosingNamespaceContext())) {
 // If there's no previous declaration, AND this isn't attempting to cause
 // multiversioning, this isn't an error condition.
 if (MVKind == MultiVersionKind::None)
diff --git a/clang/test/CodeGenCXX/fmv-namespace.cpp 
b/clang/test/CodeGenCXX/fmv-namespace.cpp
new file mode 100644
index 0..5bcd0da06eebc
--- /dev/null
+++ b/clang/test/CodeGenCXX/fmv-namespace.cpp
@@ -0,0 +1,93 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --check-globals all --include-generated-funcs --version 5
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+namespace Name {
+int __attribute((target_version("default"))) foo() { return 0; }
+}
+
+namespace Name {
+int __attribute((target_version("sve"))) foo() { return 1; }
+}
+
+int bar() { return Name::foo(); }
+
+namespace OtherName {
+int __attribute((target_version("sve"))) foo() { return 2; }
+}
+
+int baz() { return OtherName::foo(); }
+
+//.
+// CHECK: @__aarch64_cpu_features = external dso_local global { i64 }
+// CHECK: @_ZN4Name3fooEv.ifunc = weak_odr alias i32 (), ptr @_ZN4Name3fooEv
+// CHECK: @_ZN9OtherName3fooEv.ifunc = weak_odr alias i32 (), ptr 
@_ZN9OtherName3fooEv
+// CHECK: @_ZN4Name3fooEv = weak_odr ifunc i32 (), ptr @_ZN4Name3fooEv.resolver
+// CHECK: @_ZN9OtherName3fooEv = weak_odr ifunc i32 (), ptr 
@_ZN9OtherName3fooEv.resolver
+//.
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv.default(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 0
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv._Msve(
+// CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 1
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_Z3barv(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:[[CALL:%.*]] = call noundef i32 @_ZN4Name3fooEv()
+// CHECK-NEXT:ret i32 [[CALL]]
+//
+//
+// 

[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread Jon Roelofs via cfe-commits

https://github.com/jroelofs approved this pull request.


https://github.com/llvm/llvm-project/pull/93044
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread Alexandros Lamprineas via cfe-commits

https://github.com/labrinea updated 
https://github.com/llvm/llvm-project/pull/93044

>From e1de85dc4b5fe09a8b6df2e10c16e58805e8873a Mon Sep 17 00:00:00 2001
From: Alexandros Lamprineas 
Date: Wed, 22 May 2024 15:55:58 +0100
Subject: [PATCH] [clang][FMV] Allow declaration of function versions in
 namespaces.

Fixes a bug:

../llvm-project/clang/test/Sema/fmv-namespace.cpp:8:18: warning:
attribute declaration must precede definition [-Wignored-attributes]

8 | int __attribute((target_version("sve"))) foo() { return 1; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:4:46: note:
previous definition is here
4 | int __attribute((target_version("default"))) foo() { return 0; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:8:42: error:
redefinition of 'foo'
8 | int __attribute((target_version("sve"))) foo() { return 1; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:4:46: note:
previous definition is here
4 | int __attribute((target_version("default"))) foo() { return 0; }
  |  ^
1 warning and 1 error generated.
---
 clang/lib/Sema/SemaDecl.cpp |  6 ++-
 clang/test/CodeGenCXX/fmv-namespace.cpp | 56 +
 clang/test/Sema/fmv-namespace.cpp   | 12 ++
 3 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/fmv-namespace.cpp
 create mode 100644 clang/test/Sema/fmv-namespace.cpp

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b5c3a27ab06e9..99057c50cf593 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11868,8 +11868,10 @@ static bool CheckMultiVersionFunction(Sema , 
FunctionDecl *NewFD,
 return false;
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
-  OldDecl->getDeclContext()->getRedeclContext() !=
-  NewFD->getDeclContext()->getRedeclContext()) {
+  (OldDecl->getDeclContext()->getRedeclContext() !=
+   NewFD->getDeclContext()->getRedeclContext() &&
+   OldDecl->getDeclContext()->getEnclosingNamespaceContext() !=
+   NewFD->getDeclContext()->getEnclosingNamespaceContext())) {
 // If there's no previous declaration, AND this isn't attempting to cause
 // multiversioning, this isn't an error condition.
 if (MVKind == MultiVersionKind::None)
diff --git a/clang/test/CodeGenCXX/fmv-namespace.cpp 
b/clang/test/CodeGenCXX/fmv-namespace.cpp
new file mode 100644
index 0..4d56e3be3e264
--- /dev/null
+++ b/clang/test/CodeGenCXX/fmv-namespace.cpp
@@ -0,0 +1,56 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --check-globals all --include-generated-funcs --version 5
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+namespace Name {
+int __attribute((target_version("default"))) foo() { return 0; }
+}
+
+namespace Name {
+int __attribute((target_version("sve"))) foo() { return 1; }
+}
+
+int bar() { return Name::foo(); }
+//.
+// CHECK: @__aarch64_cpu_features = external dso_local global { i64 }
+// CHECK: @_ZN4Name3fooEv.ifunc = weak_odr alias i32 (), ptr @_ZN4Name3fooEv
+// CHECK: @_ZN4Name3fooEv = weak_odr ifunc i32 (), ptr @_ZN4Name3fooEv.resolver
+//.
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv.default(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 0
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv._Msve(
+// CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 1
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_Z3barv(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:[[CALL:%.*]] = call noundef i32 @_ZN4Name3fooEv()
+// CHECK-NEXT:ret i32 [[CALL]]
+//
+//
+// CHECK-LABEL: define weak_odr ptr @_ZN4Name3fooEv.resolver() comdat {
+// CHECK-NEXT:  [[RESOLVER_ENTRY:.*:]]
+// CHECK-NEXT:call void @__init_cpu_features_resolver()
+// CHECK-NEXT:[[TMP0:%.*]] = load i64, ptr @__aarch64_cpu_features, align 8
+// CHECK-NEXT:[[TMP1:%.*]] = and i64 [[TMP0]], 1073741824
+// CHECK-NEXT:[[TMP2:%.*]] = icmp eq i64 [[TMP1]], 1073741824
+// CHECK-NEXT:[[TMP3:%.*]] = and i1 true, [[TMP2]]
+// CHECK-NEXT:br i1 [[TMP3]], label %[[RESOLVER_RETURN:.*]], label 
%[[RESOLVER_ELSE:.*]]
+// CHECK:   [[RESOLVER_RETURN]]:
+// CHECK-NEXT:ret ptr @_ZN4Name3fooEv._Msve
+// CHECK:   [[RESOLVER_ELSE]]:
+// CHECK-NEXT:ret ptr @_ZN4Name3fooEv.default
+//
+//.
+// CHECK: attributes #[[ATTR0]] = { mustprogress noinline nounwind optnone 
"no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// CHECK: attributes #[[ATTR1]] = { mustprogress noinline nounwind optnone 
"no-trapping-math"="true" "stack-protector-buffer-size"="8" 

[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff e5936b245e9af0cea69a7e4eae22a05b7ffcf5a3 
e03d8cfdc697bf714d1f13247233cf514b77f0de -- 
clang/test/CodeGenCXX/fmv-namespace.cpp clang/test/Sema/fmv-namespace.cpp 
clang/lib/Sema/SemaDecl.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 297a2feb66..99057c50cf 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11869,9 +11869,9 @@ static bool CheckMultiVersionFunction(Sema , 
FunctionDecl *NewFD,
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
   (OldDecl->getDeclContext()->getRedeclContext() !=
-   NewFD->getDeclContext()->getRedeclContext() &&
+   NewFD->getDeclContext()->getRedeclContext() &&
OldDecl->getDeclContext()->getEnclosingNamespaceContext() !=
-   NewFD->getDeclContext()->getEnclosingNamespaceContext())) {
+   NewFD->getDeclContext()->getEnclosingNamespaceContext())) {
 // If there's no previous declaration, AND this isn't attempting to cause
 // multiversioning, this isn't an error condition.
 if (MVKind == MultiVersionKind::None)

``




https://github.com/llvm/llvm-project/pull/93044
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread Alexandros Lamprineas via cfe-commits

https://github.com/labrinea edited 
https://github.com/llvm/llvm-project/pull/93044
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Alexandros Lamprineas (labrinea)


Changes

Fixes a bug:

../llvm-project/clang/test/Sema/fmv-namespace.cpp:8:18: warning: attribute 
declaration must precede definition [-Wignored-attributes]

8 | int __attribute((target_version("sve"))) foo() { return 1; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:4:46: note:
previous definition is here
4 | int __attribute((target_version("default"))) foo() { return 0; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:8:42: error:
redefinition of 'foo'
8 | int __attribute((target_version("sve"))) foo() { return 1; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:4:46: note:
previous definition is here
4 | int __attribute((target_version("default"))) foo() { return 0; }
  |  ^
1 warning and 1 error generated.

---
Full diff: https://github.com/llvm/llvm-project/pull/93044.diff


3 Files Affected:

- (modified) clang/lib/Sema/SemaDecl.cpp (+4-2) 
- (added) clang/test/CodeGenCXX/fmv-namespace.cpp (+56) 
- (added) clang/test/Sema/fmv-namespace.cpp (+12) 


``diff
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b5c3a27ab06e9..297a2feb66cbc 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11868,8 +11868,10 @@ static bool CheckMultiVersionFunction(Sema , 
FunctionDecl *NewFD,
 return false;
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
-  OldDecl->getDeclContext()->getRedeclContext() !=
-  NewFD->getDeclContext()->getRedeclContext()) {
+  (OldDecl->getDeclContext()->getRedeclContext() !=
+   NewFD->getDeclContext()->getRedeclContext() &&
+   OldDecl->getDeclContext()->getEnclosingNamespaceContext() !=
+   NewFD->getDeclContext()->getEnclosingNamespaceContext())) {
 // If there's no previous declaration, AND this isn't attempting to cause
 // multiversioning, this isn't an error condition.
 if (MVKind == MultiVersionKind::None)
diff --git a/clang/test/CodeGenCXX/fmv-namespace.cpp 
b/clang/test/CodeGenCXX/fmv-namespace.cpp
new file mode 100644
index 0..4d56e3be3e264
--- /dev/null
+++ b/clang/test/CodeGenCXX/fmv-namespace.cpp
@@ -0,0 +1,56 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --check-globals all --include-generated-funcs --version 5
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+namespace Name {
+int __attribute((target_version("default"))) foo() { return 0; }
+}
+
+namespace Name {
+int __attribute((target_version("sve"))) foo() { return 1; }
+}
+
+int bar() { return Name::foo(); }
+//.
+// CHECK: @__aarch64_cpu_features = external dso_local global { i64 }
+// CHECK: @_ZN4Name3fooEv.ifunc = weak_odr alias i32 (), ptr @_ZN4Name3fooEv
+// CHECK: @_ZN4Name3fooEv = weak_odr ifunc i32 (), ptr @_ZN4Name3fooEv.resolver
+//.
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv.default(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 0
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv._Msve(
+// CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 1
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_Z3barv(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:[[CALL:%.*]] = call noundef i32 @_ZN4Name3fooEv()
+// CHECK-NEXT:ret i32 [[CALL]]
+//
+//
+// CHECK-LABEL: define weak_odr ptr @_ZN4Name3fooEv.resolver() comdat {
+// CHECK-NEXT:  [[RESOLVER_ENTRY:.*:]]
+// CHECK-NEXT:call void @__init_cpu_features_resolver()
+// CHECK-NEXT:[[TMP0:%.*]] = load i64, ptr @__aarch64_cpu_features, align 8
+// CHECK-NEXT:[[TMP1:%.*]] = and i64 [[TMP0]], 1073741824
+// CHECK-NEXT:[[TMP2:%.*]] = icmp eq i64 [[TMP1]], 1073741824
+// CHECK-NEXT:[[TMP3:%.*]] = and i1 true, [[TMP2]]
+// CHECK-NEXT:br i1 [[TMP3]], label %[[RESOLVER_RETURN:.*]], label 
%[[RESOLVER_ELSE:.*]]
+// CHECK:   [[RESOLVER_RETURN]]:
+// CHECK-NEXT:ret ptr @_ZN4Name3fooEv._Msve
+// CHECK:   [[RESOLVER_ELSE]]:
+// CHECK-NEXT:ret ptr @_ZN4Name3fooEv.default
+//
+//.
+// CHECK: attributes #[[ATTR0]] = { mustprogress noinline nounwind optnone 
"no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+// CHECK: attributes #[[ATTR1]] = { mustprogress noinline nounwind optnone 
"no-trapping-math"="true" "stack-protector-buffer-size"="8" 
"target-features"="+fp-armv8,+fullfp16,+neon,+sve" }
+//.
+// CHECK: [[META0:![0-9]+]] = !{i32 1, !"wchar_size", i32 4}
+// CHECK: [[META1:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"}
+//.
diff --git a/clang/test/Sema/fmv-namespace.cpp 
b/clang/test/Sema/fmv-namespace.cpp
new file mode 100644
index 0..1c12fd66cf243
--- /dev/null

[clang] [clang][FMV] Allow declaration of function versions in namespaces. (PR #93044)

2024-05-22 Thread Alexandros Lamprineas via cfe-commits

https://github.com/labrinea created 
https://github.com/llvm/llvm-project/pull/93044

Fixes a bug:

../llvm-project/clang/test/Sema/fmv-namespace.cpp:8:18: warning: attribute 
declaration must precede definition [-Wignored-attributes]

8 | int __attribute((target_version("sve"))) foo() { return 1; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:4:46: note:
previous definition is here
4 | int __attribute((target_version("default"))) foo() { return 0; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:8:42: error:
redefinition of 'foo'
8 | int __attribute((target_version("sve"))) foo() { return 1; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:4:46: note:
previous definition is here
4 | int __attribute((target_version("default"))) foo() { return 0; }
  |  ^
1 warning and 1 error generated.

>From e03d8cfdc697bf714d1f13247233cf514b77f0de Mon Sep 17 00:00:00 2001
From: Alexandros Lamprineas 
Date: Wed, 22 May 2024 15:55:58 +0100
Subject: [PATCH] [clang][FMV] Allow declaration of function versions in
 namespaces.

Fixes a bug:

../llvm-project/clang/test/Sema/fmv-namespace.cpp:8:18: warning:
attribute declaration must precede definition [-Wignored-attributes]

8 | int __attribute((target_version("sve"))) foo() { return 1; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:4:46: note:
previous definition is here
4 | int __attribute((target_version("default"))) foo() { return 0; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:8:42: error:
redefinition of 'foo'
8 | int __attribute((target_version("sve"))) foo() { return 1; }
  |  ^
../llvm-project/clang/test/Sema/fmv-namespace.cpp:4:46: note:
previous definition is here
4 | int __attribute((target_version("default"))) foo() { return 0; }
  |  ^
1 warning and 1 error generated.
---
 clang/lib/Sema/SemaDecl.cpp |  6 ++-
 clang/test/CodeGenCXX/fmv-namespace.cpp | 56 +
 clang/test/Sema/fmv-namespace.cpp   | 12 ++
 3 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/fmv-namespace.cpp
 create mode 100644 clang/test/Sema/fmv-namespace.cpp

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b5c3a27ab06e9..297a2feb66cbc 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11868,8 +11868,10 @@ static bool CheckMultiVersionFunction(Sema , 
FunctionDecl *NewFD,
 return false;
 
   if (!OldDecl || !OldDecl->getAsFunction() ||
-  OldDecl->getDeclContext()->getRedeclContext() !=
-  NewFD->getDeclContext()->getRedeclContext()) {
+  (OldDecl->getDeclContext()->getRedeclContext() !=
+   NewFD->getDeclContext()->getRedeclContext() &&
+   OldDecl->getDeclContext()->getEnclosingNamespaceContext() !=
+   NewFD->getDeclContext()->getEnclosingNamespaceContext())) {
 // If there's no previous declaration, AND this isn't attempting to cause
 // multiversioning, this isn't an error condition.
 if (MVKind == MultiVersionKind::None)
diff --git a/clang/test/CodeGenCXX/fmv-namespace.cpp 
b/clang/test/CodeGenCXX/fmv-namespace.cpp
new file mode 100644
index 0..4d56e3be3e264
--- /dev/null
+++ b/clang/test/CodeGenCXX/fmv-namespace.cpp
@@ -0,0 +1,56 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --check-globals all --include-generated-funcs --version 5
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+namespace Name {
+int __attribute((target_version("default"))) foo() { return 0; }
+}
+
+namespace Name {
+int __attribute((target_version("sve"))) foo() { return 1; }
+}
+
+int bar() { return Name::foo(); }
+//.
+// CHECK: @__aarch64_cpu_features = external dso_local global { i64 }
+// CHECK: @_ZN4Name3fooEv.ifunc = weak_odr alias i32 (), ptr @_ZN4Name3fooEv
+// CHECK: @_ZN4Name3fooEv = weak_odr ifunc i32 (), ptr @_ZN4Name3fooEv.resolver
+//.
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv.default(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 0
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_ZN4Name3fooEv._Msve(
+// CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:ret i32 1
+//
+//
+// CHECK-LABEL: define dso_local noundef i32 @_Z3barv(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:[[CALL:%.*]] = call noundef i32 @_ZN4Name3fooEv()
+// CHECK-NEXT:ret i32 [[CALL]]
+//
+//
+// CHECK-LABEL: define weak_odr ptr @_ZN4Name3fooEv.resolver() comdat {
+// CHECK-NEXT:  [[RESOLVER_ENTRY:.*:]]
+//