[clang] [NFC] [clang] Use std::string instead of StringRef to reduce stack usage (PR #114285)

2024-10-30 Thread Chinmay Deshpande via cfe-commits

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


[clang] [NFC] [clang] Use std::string instead of StringRef to reduce stack usage (PR #114285)

2024-10-30 Thread Chinmay Deshpande via cfe-commits

chinmaydd wrote:

Thank you everyone for the review.

On my local setup, this function occupies close to 185KB of local stack (MSVC 
14.38.33130), which is really wasteful. Like @kparzysz highlighted, we observed 
a crash in `__chkstk()` which is inserted by MSVC in the prologue when a 
function requests for more than 8KB (on x64) of stack space for local 
variables. 

Another such function we observed was 
`TargetLibraryInfoImpl::addVectorizableFunctionsFromVecLib`. Its stack usage 
was fixed earlier this year (https://github.com/llvm/llvm-project/pull/86829).

MSVC does not provide a flag such as `-fstack-usage`, but I was able to perform 
some basic binary analysis to identify that close to 800 functions use more 
than 8KB of stack and around 14 use more than 64KB. I feel like in the longer 
term, we should look at reducing this since the clang driver may be invoked 
in-process with limited stack space available for the executing thread.

@erichkeane thanks for taking the time to suggest a clear way forward. I'll 
write a patch that attempts to achieve the same. I'll close this PR for now. 

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


[clang] [NFC] [clang] Use std::string instead of StringRef to reduce stack usage (PR #114285)

2024-10-30 Thread Chinmay Deshpande via cfe-commits

https://github.com/chinmaydd updated 
https://github.com/llvm/llvm-project/pull/114285

>From e075173eca885ad1283d9ebb76a79b8ec51d7432 Mon Sep 17 00:00:00 2001
From: Chinmay Diwakar Deshpande 
Date: Wed, 30 Oct 2024 10:28:15 -0700
Subject: [PATCH 1/2] [NFC] [clang] Use std::string instead of StringRef to
 reduce stack usage

Comparisons between StringRef and string literals are lowered by MSVC to happen 
on the stack.
All string literals are allocated unique stack slots increasing the overall 
stack usage of
calculateAttributeSpellingListIndex. Use of std::string forces allocation of a 
string type per
literal, reducing the overall stack usage of the function.

Change-Id: I7ac39fc96ab2e559318413fa26ef304c3fb0bd6e
---
 clang/lib/Basic/Attributes.cpp | 6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 867d241a2cf847..0083a5ae0a55fb 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -157,8 +157,10 @@ unsigned 
AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
   // Both variables will be used in tablegen generated
   // attribute spell list index matching code.
   auto Syntax = static_cast(getSyntax());
-  StringRef Scope = normalizeAttrScopeName(getScopeName(), Syntax);
-  StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
+  // We use std::string instead of StringRef to prevent local stack 
+  // allocation of literal strings for comparison.
+  const std::string Scope = normalizeAttrScopeName(getScopeName(), Syntax);
+  const std::string Name = normalizeAttrName(getAttrName(), Scope, Syntax);
 
 #include "clang/Sema/AttrSpellingListIndex.inc"
 }

>From a1ab03f99de460d975cfbe409144f36a173a5cdf Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Wed, 30 Oct 2024 12:12:04 -0700
Subject: [PATCH 2/2] Fix syntax and formatting issues

---
 clang/lib/Basic/Attributes.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 0083a5ae0a55fb..bb4faa675ea9f2 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -157,10 +157,10 @@ unsigned 
AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
   // Both variables will be used in tablegen generated
   // attribute spell list index matching code.
   auto Syntax = static_cast(getSyntax());
-  // We use std::string instead of StringRef to prevent local stack 
+  // We use std::string instead of StringRef to prevent local stack
   // allocation of literal strings for comparison.
-  const std::string Scope = normalizeAttrScopeName(getScopeName(), Syntax);
-  const std::string Name = normalizeAttrName(getAttrName(), Scope, Syntax);
+  const std::string Scope(normalizeAttrScopeName(getScopeName(), Syntax));
+  const std::string Name(normalizeAttrName(getAttrName(), Scope, Syntax));
 
 #include "clang/Sema/AttrSpellingListIndex.inc"
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)

2024-11-08 Thread Chinmay Deshpande via cfe-commits

https://github.com/chinmaydd updated 
https://github.com/llvm/llvm-project/pull/115414

>From ce67d44d55ef488bac9428b4c7b78e37ea8471f6 Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Thu, 7 Nov 2024 22:05:03 -0500
Subject: [PATCH 1/2] [NFC][Clang] Use StringSwitch instead of array for
 parsing attribute scope

Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8
---
 .../include/clang/Basic/AttributeCommonInfo.h |  2 +-
 clang/lib/Basic/Attributes.cpp| 34 +--
 2 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 11c64547721739..350154859ccdec 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
-  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
+  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV, INVALID };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 2d18fb3f9d5bb2..a2ffaa147f3ef7 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -18,6 +18,7 @@
 #include "clang/Basic/TargetInfo.h"
 
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringSwitch.h"
 
 using namespace clang;
 
@@ -155,26 +156,23 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
   normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
-// Sorted list of attribute scope names
-static constexpr std::pair ScopeList[] =
-{{"", AttributeCommonInfo::Scope::NONE},
- {"clang", AttributeCommonInfo::Scope::CLANG},
- {"gnu", AttributeCommonInfo::Scope::GNU},
- {"gsl", AttributeCommonInfo::Scope::GSL},
- {"hlsl", AttributeCommonInfo::Scope::HLSL},
- {"msvc", AttributeCommonInfo::Scope::MSVC},
- {"omp", AttributeCommonInfo::Scope::OMP},
- {"riscv", AttributeCommonInfo::Scope::RISCV}};
-
 AttributeCommonInfo::Scope
 getScopeFromNormalizedScopeName(StringRef ScopeName) {
-  auto It = std::lower_bound(
-  std::begin(ScopeList), std::end(ScopeList), ScopeName,
-  [](const std::pair &Element,
- StringRef Value) { return Element.first < Value; });
-  assert(It != std::end(ScopeList) && It->first == ScopeName);
-
-  return It->second;
+  AttributeCommonInfo::Scope ParsedScope =
+  llvm::StringSwitch(ScopeName)
+  .Case("", AttributeCommonInfo::Scope::NONE)
+  .Case("clang", AttributeCommonInfo::Scope::CLANG)
+  .Case("gnu", AttributeCommonInfo::Scope::GNU)
+  .Case("gsl", AttributeCommonInfo::Scope::GSL)
+  .Case("hlsl", AttributeCommonInfo::Scope::HLSL)
+  .Case("msvc", AttributeCommonInfo::Scope::MSVC)
+  .Case("omp", AttributeCommonInfo::Scope::OMP)
+  .Case("riscv", AttributeCommonInfo::Scope::RISCV)
+  .Default(AttributeCommonInfo::Scope::INVALID);
+
+  assert(ParsedScope != AttributeCommonInfo::Scope::INVALID);
+
+  return ParsedScope;
 }
 
 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {

>From c70b5653d0903f0a8d3e27f0dff3e1c95298b207 Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Fri, 8 Nov 2024 13:24:00 -0500
Subject: [PATCH 2/2] [NFC][Clang] Address comments

Change-Id: I99a618d8127680cb91146e1a82e0e0b6c0717888
---
 .../include/clang/Basic/AttributeCommonInfo.h |  2 +-
 clang/lib/Basic/Attributes.cpp| 24 +++
 clang/test/CodeGenCXX/split-stacks.cpp|  2 +-
 3 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 350154859ccdec..11c64547721739 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
-  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV, INVALID };
+  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index a2ffaa147f3ef7..6904bce3ac51ec 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -158,21 +158,15 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
 
 AttributeCommonInfo::Scope
 getScopeFromNormalizedScopeName(StringRef ScopeName) {
-  AttributeCommonInfo::Scope ParsedScope =
-  llvm::StringSwitch(ScopeName)
-  .Case("", AttributeCommonInfo::Scope::NONE)
-  .Case("clang", AttributeCommonInfo::Scope::CLANG)
-  .Case("gnu", AttributeCommonInfo::Scope::GNU)
-  .Case("gsl", AttributeCommonInfo::Scope::GSL)
-  .

[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)

2024-11-08 Thread Chinmay Deshpande via cfe-commits

https://github.com/chinmaydd updated 
https://github.com/llvm/llvm-project/pull/115414

>From ce67d44d55ef488bac9428b4c7b78e37ea8471f6 Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Thu, 7 Nov 2024 22:05:03 -0500
Subject: [PATCH 1/2] [NFC][Clang] Use StringSwitch instead of array for
 parsing attribute scope

Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8
---
 .../include/clang/Basic/AttributeCommonInfo.h |  2 +-
 clang/lib/Basic/Attributes.cpp| 34 +--
 2 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 11c64547721739..350154859ccdec 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
-  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
+  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV, INVALID };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 2d18fb3f9d5bb2..a2ffaa147f3ef7 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -18,6 +18,7 @@
 #include "clang/Basic/TargetInfo.h"
 
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringSwitch.h"
 
 using namespace clang;
 
@@ -155,26 +156,23 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
   normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
-// Sorted list of attribute scope names
-static constexpr std::pair ScopeList[] =
-{{"", AttributeCommonInfo::Scope::NONE},
- {"clang", AttributeCommonInfo::Scope::CLANG},
- {"gnu", AttributeCommonInfo::Scope::GNU},
- {"gsl", AttributeCommonInfo::Scope::GSL},
- {"hlsl", AttributeCommonInfo::Scope::HLSL},
- {"msvc", AttributeCommonInfo::Scope::MSVC},
- {"omp", AttributeCommonInfo::Scope::OMP},
- {"riscv", AttributeCommonInfo::Scope::RISCV}};
-
 AttributeCommonInfo::Scope
 getScopeFromNormalizedScopeName(StringRef ScopeName) {
-  auto It = std::lower_bound(
-  std::begin(ScopeList), std::end(ScopeList), ScopeName,
-  [](const std::pair &Element,
- StringRef Value) { return Element.first < Value; });
-  assert(It != std::end(ScopeList) && It->first == ScopeName);
-
-  return It->second;
+  AttributeCommonInfo::Scope ParsedScope =
+  llvm::StringSwitch(ScopeName)
+  .Case("", AttributeCommonInfo::Scope::NONE)
+  .Case("clang", AttributeCommonInfo::Scope::CLANG)
+  .Case("gnu", AttributeCommonInfo::Scope::GNU)
+  .Case("gsl", AttributeCommonInfo::Scope::GSL)
+  .Case("hlsl", AttributeCommonInfo::Scope::HLSL)
+  .Case("msvc", AttributeCommonInfo::Scope::MSVC)
+  .Case("omp", AttributeCommonInfo::Scope::OMP)
+  .Case("riscv", AttributeCommonInfo::Scope::RISCV)
+  .Default(AttributeCommonInfo::Scope::INVALID);
+
+  assert(ParsedScope != AttributeCommonInfo::Scope::INVALID);
+
+  return ParsedScope;
 }
 
 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {

>From a64632f9b3253a181c2a00ca44c4009736617191 Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Fri, 8 Nov 2024 13:24:00 -0500
Subject: [PATCH 2/2] [NFC][Clang] Address comments

Change-Id: I99a618d8127680cb91146e1a82e0e0b6c0717888
---
 .../include/clang/Basic/AttributeCommonInfo.h |  2 +-
 clang/lib/Basic/Attributes.cpp| 24 +++
 2 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 350154859ccdec..11c64547721739 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
-  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV, INVALID };
+  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index a2ffaa147f3ef7..6904bce3ac51ec 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -158,21 +158,15 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
 
 AttributeCommonInfo::Scope
 getScopeFromNormalizedScopeName(StringRef ScopeName) {
-  AttributeCommonInfo::Scope ParsedScope =
-  llvm::StringSwitch(ScopeName)
-  .Case("", AttributeCommonInfo::Scope::NONE)
-  .Case("clang", AttributeCommonInfo::Scope::CLANG)
-  .Case("gnu", AttributeCommonInfo::Scope::GNU)
-  .Case("gsl", AttributeCommonInfo::Scope::GSL)
-  .Case("hlsl", AttributeCommonInfo::Scope::HLSL)
-   

[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)

2024-11-08 Thread Chinmay Deshpande via cfe-commits


@@ -155,26 +156,23 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
   normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
-// Sorted list of attribute scope names
-static constexpr std::pair ScopeList[] =
-{{"", AttributeCommonInfo::Scope::NONE},
- {"clang", AttributeCommonInfo::Scope::CLANG},
- {"gnu", AttributeCommonInfo::Scope::GNU},
- {"gsl", AttributeCommonInfo::Scope::GSL},
- {"hlsl", AttributeCommonInfo::Scope::HLSL},
- {"msvc", AttributeCommonInfo::Scope::MSVC},
- {"omp", AttributeCommonInfo::Scope::OMP},
- {"riscv", AttributeCommonInfo::Scope::RISCV}};
-
 AttributeCommonInfo::Scope
 getScopeFromNormalizedScopeName(StringRef ScopeName) {
-  auto It = std::lower_bound(
-  std::begin(ScopeList), std::end(ScopeList), ScopeName,
-  [](const std::pair &Element,
- StringRef Value) { return Element.first < Value; });
-  assert(It != std::end(ScopeList) && It->first == ScopeName);
-
-  return It->second;
+  AttributeCommonInfo::Scope ParsedScope =
+  llvm::StringSwitch(ScopeName)
+  .Case("", AttributeCommonInfo::Scope::NONE)
+  .Case("clang", AttributeCommonInfo::Scope::CLANG)
+  .Case("gnu", AttributeCommonInfo::Scope::GNU)
+  .Case("gsl", AttributeCommonInfo::Scope::GSL)
+  .Case("hlsl", AttributeCommonInfo::Scope::HLSL)
+  .Case("msvc", AttributeCommonInfo::Scope::MSVC)
+  .Case("omp", AttributeCommonInfo::Scope::OMP)
+  .Case("riscv", AttributeCommonInfo::Scope::RISCV)
+  .Default(AttributeCommonInfo::Scope::INVALID);
+
+  assert(ParsedScope != AttributeCommonInfo::Scope::INVALID);

chinmaydd wrote:

Thanks @erichkeane and @nikic. This looks quite clean now :)

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


[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)

2024-11-08 Thread Chinmay Deshpande via cfe-commits

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


[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)

2024-11-08 Thread Chinmay Deshpande via cfe-commits

https://github.com/chinmaydd updated 
https://github.com/llvm/llvm-project/pull/115414

>From ce67d44d55ef488bac9428b4c7b78e37ea8471f6 Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Thu, 7 Nov 2024 22:05:03 -0500
Subject: [PATCH 1/2] [NFC][Clang] Use StringSwitch instead of array for
 parsing attribute scope

Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8
---
 .../include/clang/Basic/AttributeCommonInfo.h |  2 +-
 clang/lib/Basic/Attributes.cpp| 34 +--
 2 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 11c64547721739..350154859ccdec 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
-  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
+  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV, INVALID };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 2d18fb3f9d5bb2..a2ffaa147f3ef7 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -18,6 +18,7 @@
 #include "clang/Basic/TargetInfo.h"
 
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringSwitch.h"
 
 using namespace clang;
 
@@ -155,26 +156,23 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
   normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
-// Sorted list of attribute scope names
-static constexpr std::pair ScopeList[] =
-{{"", AttributeCommonInfo::Scope::NONE},
- {"clang", AttributeCommonInfo::Scope::CLANG},
- {"gnu", AttributeCommonInfo::Scope::GNU},
- {"gsl", AttributeCommonInfo::Scope::GSL},
- {"hlsl", AttributeCommonInfo::Scope::HLSL},
- {"msvc", AttributeCommonInfo::Scope::MSVC},
- {"omp", AttributeCommonInfo::Scope::OMP},
- {"riscv", AttributeCommonInfo::Scope::RISCV}};
-
 AttributeCommonInfo::Scope
 getScopeFromNormalizedScopeName(StringRef ScopeName) {
-  auto It = std::lower_bound(
-  std::begin(ScopeList), std::end(ScopeList), ScopeName,
-  [](const std::pair &Element,
- StringRef Value) { return Element.first < Value; });
-  assert(It != std::end(ScopeList) && It->first == ScopeName);
-
-  return It->second;
+  AttributeCommonInfo::Scope ParsedScope =
+  llvm::StringSwitch(ScopeName)
+  .Case("", AttributeCommonInfo::Scope::NONE)
+  .Case("clang", AttributeCommonInfo::Scope::CLANG)
+  .Case("gnu", AttributeCommonInfo::Scope::GNU)
+  .Case("gsl", AttributeCommonInfo::Scope::GSL)
+  .Case("hlsl", AttributeCommonInfo::Scope::HLSL)
+  .Case("msvc", AttributeCommonInfo::Scope::MSVC)
+  .Case("omp", AttributeCommonInfo::Scope::OMP)
+  .Case("riscv", AttributeCommonInfo::Scope::RISCV)
+  .Default(AttributeCommonInfo::Scope::INVALID);
+
+  assert(ParsedScope != AttributeCommonInfo::Scope::INVALID);
+
+  return ParsedScope;
 }
 
 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {

>From f90efc1a9bbdbaf351e594e07d7c74cd7fa8662c Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Fri, 8 Nov 2024 13:24:00 -0500
Subject: [PATCH 2/2] [NFC][Clang] Address comments

Change-Id: I99a618d8127680cb91146e1a82e0e0b6c0717888
---
 .../include/clang/Basic/AttributeCommonInfo.h |  2 +-
 clang/lib/Basic/Attributes.cpp| 24 +++
 clang/test/CodeGenCXX/split-stacks.cpp|  2 +-
 3 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 350154859ccdec..11c64547721739 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
-  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV, INVALID };
+  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index a2ffaa147f3ef7..6904bce3ac51ec 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -158,21 +158,15 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
 
 AttributeCommonInfo::Scope
 getScopeFromNormalizedScopeName(StringRef ScopeName) {
-  AttributeCommonInfo::Scope ParsedScope =
-  llvm::StringSwitch(ScopeName)
-  .Case("", AttributeCommonInfo::Scope::NONE)
-  .Case("clang", AttributeCommonInfo::Scope::CLANG)
-  .Case("gnu", AttributeCommonInfo::Scope::GNU)
-  .Case("gsl", AttributeCommonInfo::Scope::GSL)
-  .

[clang] [Clang] Improve EmitClangAttrSpellingListIndex (PR #114899)

2024-11-07 Thread Chinmay Deshpande via cfe-commits

https://github.com/chinmaydd updated 
https://github.com/llvm/llvm-project/pull/114899

>From cf4d70c23b2896483b452622300aa4c8c41c01e5 Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Fri, 1 Nov 2024 19:49:52 -0400
Subject: [PATCH 1/5] [Clang] Improve EmitClangAttrSpellingListIndex

EmitClangAttrSpellingListIndex() performs a lot of
unnecessary string comparisons which is wasteful in time and space. This
commit attempts to refactor this method to be more performant.
---
 .../include/clang/Basic/AttributeCommonInfo.h | 10 ++
 clang/lib/Basic/Attributes.cpp| 28 +-
 clang/utils/TableGen/ClangAttrEmitter.cpp | 95 ++-
 3 files changed, 126 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 5f024b4b5fd782..834b3ca62adced 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,6 +67,16 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
+  enum Scope {
+SC_NONE,
+SC_CLANG,
+SC_GNU,
+SC_MSVC,
+SC_OMP,
+SC_HLSL,
+SC_GSL,
+SC_RISCV
+  };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 867d241a2cf847..6f5a70674cbd4b 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -16,6 +16,7 @@
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/ParsedAttrInfo.h"
 #include "clang/Basic/TargetInfo.h"
+#include "llvm/ADT/StringMap.h"
 
 using namespace clang;
 
@@ -153,12 +154,35 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
   normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
+static const llvm::StringMap ScopeMap = {
+{"", AttributeCommonInfo::SC_NONE},
+{"clang", AttributeCommonInfo::SC_CLANG},
+{"gnu", AttributeCommonInfo::SC_GNU},
+{"msvc", AttributeCommonInfo::SC_MSVC},
+{"omp", AttributeCommonInfo::SC_OMP},
+{"hlsl", AttributeCommonInfo::SC_HLSL},
+{"gsl", AttributeCommonInfo::SC_GSL},
+{"riscv", AttributeCommonInfo::SC_RISCV}};
+
+AttributeCommonInfo::Scope
+getScopeFromNormalizedScopeName(const StringRef ScopeName) {
+  auto It = ScopeMap.find(ScopeName);
+  if (It == ScopeMap.end()) {
+llvm_unreachable("Unknown normalized scope name. Shouldn't get here");
+  }
+
+  return It->second;
+}
+
 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
   // Both variables will be used in tablegen generated
   // attribute spell list index matching code.
   auto Syntax = static_cast(getSyntax());
-  StringRef Scope = normalizeAttrScopeName(getScopeName(), Syntax);
-  StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
+  StringRef ScopeName = normalizeAttrScopeName(getScopeName(), Syntax);
+  StringRef Name = normalizeAttrName(getAttrName(), ScopeName, Syntax);
+
+  AttributeCommonInfo::Scope ComputedScope =
+  getScopeFromNormalizedScopeName(ScopeName);
 
 #include "clang/Sema/AttrSpellingListIndex.inc"
 }
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 5a80c8c0b7ad36..4db75a92639ad6 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -20,6 +20,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -3843,11 +3844,95 @@ void EmitClangAttrSpellingListIndex(const RecordKeeper 
&Records,
 const Record &R = *I.second;
 std::vector Spellings = GetFlattenedSpellings(R);
 OS << "  case AT_" << I.first << ": {\n";
-for (unsigned I = 0; I < Spellings.size(); ++ I) {
-  OS << "if (Name == \"" << Spellings[I].name() << "\" && "
- << "getSyntax() == AttributeCommonInfo::AS_" << Spellings[I].variety()
- << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
- << "return " << I << ";\n";
+
+// If there are none or one spelling to check, resort to the default
+// behavior of returning index as 0.
+if (Spellings.size() <= 1) {
+  OS << "return 0;\n";
+  OS << "break;\n";
+  OS << "  }\n";
+  continue;
+}
+
+bool HasSingleUniqueSpellingName = true;
+StringMap> SpellingMap;
+
+StringRef FirstName = Spellings.front().name();
+for (const auto &S : Spellings) {
+  StringRef Name = S.name();
+  if (Name != FirstName)
+HasSingleUniqueSpellingName = false;
+  SpellingMap[Name].push_back(&S);
+}
+
+// If parsed attribute has only one possible spelling name, only compare
+// syntax and scope.
+if (HasSingleUniqueSpellingName) {
+  for (const auto &[Idx, S] : enumerate(SpellingMap[FirstName

[clang] [Clang] Improve EmitClangAttrSpellingListIndex (PR #114899)

2024-11-07 Thread Chinmay Deshpande via cfe-commits

chinmaydd wrote:

Thanks @erichkeane and @arsenm !

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


[clang] [Clang] Improve EmitClangAttrSpellingListIndex (PR #114899)

2024-11-07 Thread Chinmay Deshpande via cfe-commits

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


[clang] [Clang] Improve EmitClangAttrSpellingListIndex (PR #114899)

2024-11-07 Thread Chinmay Deshpande via cfe-commits

chinmaydd wrote:

Fixed by 
https://github.com/llvm/llvm-project/commit/dd1c99bac4dc1d5ceeadc79dd31fa12f3e615f18

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


[clang] [Clang] Improve EmitClangAttrSpellingListIndex (PR #114899)

2024-11-07 Thread Chinmay Deshpande via cfe-commits


@@ -153,12 +155,37 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
   normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
+// Sorted list of attribute scope names
+static constexpr std::pair ScopeList[] =
+{{"", AttributeCommonInfo::Scope::NONE},
+ {"clang", AttributeCommonInfo::Scope::CLANG},
+ {"gnu", AttributeCommonInfo::Scope::GNU},
+ {"gsl", AttributeCommonInfo::Scope::GSL},
+ {"hlsl", AttributeCommonInfo::Scope::HLSL},
+ {"msvc", AttributeCommonInfo::Scope::MSVC},
+ {"omp", AttributeCommonInfo::Scope::OMP},
+ {"riscv", AttributeCommonInfo::Scope::RISCV}};
+
+AttributeCommonInfo::Scope
+getScopeFromNormalizedScopeName(StringRef ScopeName) {
+  auto It = std::lower_bound(
+  std::begin(ScopeList), std::end(ScopeList), ScopeName,
+  [](const std::pair &Element,
+ StringRef Value) { return Element.first < Value; });
+  assert(It != std::end(ScopeList) && It->first == ScopeName);
+
+  return It->second;

chinmaydd wrote:

Interesting, I had no idea about `StringSwitch`. I'll raise a new PR to fix 
that. 

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


[clang] [Clang] Improve EmitClangAttrSpellingListIndex (PR #114899)

2024-11-07 Thread Chinmay Deshpande via cfe-commits

https://github.com/chinmaydd updated 
https://github.com/llvm/llvm-project/pull/114899

>From cf4d70c23b2896483b452622300aa4c8c41c01e5 Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Fri, 1 Nov 2024 19:49:52 -0400
Subject: [PATCH 1/6] [Clang] Improve EmitClangAttrSpellingListIndex

EmitClangAttrSpellingListIndex() performs a lot of
unnecessary string comparisons which is wasteful in time and space. This
commit attempts to refactor this method to be more performant.
---
 .../include/clang/Basic/AttributeCommonInfo.h | 10 ++
 clang/lib/Basic/Attributes.cpp| 28 +-
 clang/utils/TableGen/ClangAttrEmitter.cpp | 95 ++-
 3 files changed, 126 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 5f024b4b5fd782..834b3ca62adced 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,6 +67,16 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
+  enum Scope {
+SC_NONE,
+SC_CLANG,
+SC_GNU,
+SC_MSVC,
+SC_OMP,
+SC_HLSL,
+SC_GSL,
+SC_RISCV
+  };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 867d241a2cf847..6f5a70674cbd4b 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -16,6 +16,7 @@
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/ParsedAttrInfo.h"
 #include "clang/Basic/TargetInfo.h"
+#include "llvm/ADT/StringMap.h"
 
 using namespace clang;
 
@@ -153,12 +154,35 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
   normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
+static const llvm::StringMap ScopeMap = {
+{"", AttributeCommonInfo::SC_NONE},
+{"clang", AttributeCommonInfo::SC_CLANG},
+{"gnu", AttributeCommonInfo::SC_GNU},
+{"msvc", AttributeCommonInfo::SC_MSVC},
+{"omp", AttributeCommonInfo::SC_OMP},
+{"hlsl", AttributeCommonInfo::SC_HLSL},
+{"gsl", AttributeCommonInfo::SC_GSL},
+{"riscv", AttributeCommonInfo::SC_RISCV}};
+
+AttributeCommonInfo::Scope
+getScopeFromNormalizedScopeName(const StringRef ScopeName) {
+  auto It = ScopeMap.find(ScopeName);
+  if (It == ScopeMap.end()) {
+llvm_unreachable("Unknown normalized scope name. Shouldn't get here");
+  }
+
+  return It->second;
+}
+
 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
   // Both variables will be used in tablegen generated
   // attribute spell list index matching code.
   auto Syntax = static_cast(getSyntax());
-  StringRef Scope = normalizeAttrScopeName(getScopeName(), Syntax);
-  StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
+  StringRef ScopeName = normalizeAttrScopeName(getScopeName(), Syntax);
+  StringRef Name = normalizeAttrName(getAttrName(), ScopeName, Syntax);
+
+  AttributeCommonInfo::Scope ComputedScope =
+  getScopeFromNormalizedScopeName(ScopeName);
 
 #include "clang/Sema/AttrSpellingListIndex.inc"
 }
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 5a80c8c0b7ad36..4db75a92639ad6 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -20,6 +20,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -3843,11 +3844,95 @@ void EmitClangAttrSpellingListIndex(const RecordKeeper 
&Records,
 const Record &R = *I.second;
 std::vector Spellings = GetFlattenedSpellings(R);
 OS << "  case AT_" << I.first << ": {\n";
-for (unsigned I = 0; I < Spellings.size(); ++ I) {
-  OS << "if (Name == \"" << Spellings[I].name() << "\" && "
- << "getSyntax() == AttributeCommonInfo::AS_" << Spellings[I].variety()
- << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
- << "return " << I << ";\n";
+
+// If there are none or one spelling to check, resort to the default
+// behavior of returning index as 0.
+if (Spellings.size() <= 1) {
+  OS << "return 0;\n";
+  OS << "break;\n";
+  OS << "  }\n";
+  continue;
+}
+
+bool HasSingleUniqueSpellingName = true;
+StringMap> SpellingMap;
+
+StringRef FirstName = Spellings.front().name();
+for (const auto &S : Spellings) {
+  StringRef Name = S.name();
+  if (Name != FirstName)
+HasSingleUniqueSpellingName = false;
+  SpellingMap[Name].push_back(&S);
+}
+
+// If parsed attribute has only one possible spelling name, only compare
+// syntax and scope.
+if (HasSingleUniqueSpellingName) {
+  for (const auto &[Idx, S] : enumerate(SpellingMap[FirstName

[clang] [Clang] Improve EmitClangAttrSpellingListIndex (PR #114899)

2024-11-06 Thread Chinmay Deshpande via cfe-commits

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


[clang] [Clang] Improve EmitClangAttrSpellingListIndex (PR #114899)

2024-11-06 Thread Chinmay Deshpande via cfe-commits

https://github.com/chinmaydd updated 
https://github.com/llvm/llvm-project/pull/114899

>From cf4d70c23b2896483b452622300aa4c8c41c01e5 Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Fri, 1 Nov 2024 19:49:52 -0400
Subject: [PATCH 1/4] [Clang] Improve EmitClangAttrSpellingListIndex

EmitClangAttrSpellingListIndex() performs a lot of
unnecessary string comparisons which is wasteful in time and space. This
commit attempts to refactor this method to be more performant.
---
 .../include/clang/Basic/AttributeCommonInfo.h | 10 ++
 clang/lib/Basic/Attributes.cpp| 28 +-
 clang/utils/TableGen/ClangAttrEmitter.cpp | 95 ++-
 3 files changed, 126 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 5f024b4b5fd782..834b3ca62adced 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,6 +67,16 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
+  enum Scope {
+SC_NONE,
+SC_CLANG,
+SC_GNU,
+SC_MSVC,
+SC_OMP,
+SC_HLSL,
+SC_GSL,
+SC_RISCV
+  };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 867d241a2cf847..6f5a70674cbd4b 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -16,6 +16,7 @@
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/ParsedAttrInfo.h"
 #include "clang/Basic/TargetInfo.h"
+#include "llvm/ADT/StringMap.h"
 
 using namespace clang;
 
@@ -153,12 +154,35 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
   normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
+static const llvm::StringMap ScopeMap = {
+{"", AttributeCommonInfo::SC_NONE},
+{"clang", AttributeCommonInfo::SC_CLANG},
+{"gnu", AttributeCommonInfo::SC_GNU},
+{"msvc", AttributeCommonInfo::SC_MSVC},
+{"omp", AttributeCommonInfo::SC_OMP},
+{"hlsl", AttributeCommonInfo::SC_HLSL},
+{"gsl", AttributeCommonInfo::SC_GSL},
+{"riscv", AttributeCommonInfo::SC_RISCV}};
+
+AttributeCommonInfo::Scope
+getScopeFromNormalizedScopeName(const StringRef ScopeName) {
+  auto It = ScopeMap.find(ScopeName);
+  if (It == ScopeMap.end()) {
+llvm_unreachable("Unknown normalized scope name. Shouldn't get here");
+  }
+
+  return It->second;
+}
+
 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
   // Both variables will be used in tablegen generated
   // attribute spell list index matching code.
   auto Syntax = static_cast(getSyntax());
-  StringRef Scope = normalizeAttrScopeName(getScopeName(), Syntax);
-  StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
+  StringRef ScopeName = normalizeAttrScopeName(getScopeName(), Syntax);
+  StringRef Name = normalizeAttrName(getAttrName(), ScopeName, Syntax);
+
+  AttributeCommonInfo::Scope ComputedScope =
+  getScopeFromNormalizedScopeName(ScopeName);
 
 #include "clang/Sema/AttrSpellingListIndex.inc"
 }
diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp 
b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 5a80c8c0b7ad36..4db75a92639ad6 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -20,6 +20,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -3843,11 +3844,95 @@ void EmitClangAttrSpellingListIndex(const RecordKeeper 
&Records,
 const Record &R = *I.second;
 std::vector Spellings = GetFlattenedSpellings(R);
 OS << "  case AT_" << I.first << ": {\n";
-for (unsigned I = 0; I < Spellings.size(); ++ I) {
-  OS << "if (Name == \"" << Spellings[I].name() << "\" && "
- << "getSyntax() == AttributeCommonInfo::AS_" << Spellings[I].variety()
- << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
- << "return " << I << ";\n";
+
+// If there are none or one spelling to check, resort to the default
+// behavior of returning index as 0.
+if (Spellings.size() <= 1) {
+  OS << "return 0;\n";
+  OS << "break;\n";
+  OS << "  }\n";
+  continue;
+}
+
+bool HasSingleUniqueSpellingName = true;
+StringMap> SpellingMap;
+
+StringRef FirstName = Spellings.front().name();
+for (const auto &S : Spellings) {
+  StringRef Name = S.name();
+  if (Name != FirstName)
+HasSingleUniqueSpellingName = false;
+  SpellingMap[Name].push_back(&S);
+}
+
+// If parsed attribute has only one possible spelling name, only compare
+// syntax and scope.
+if (HasSingleUniqueSpellingName) {
+  for (const auto &[Idx, S] : enumerate(SpellingMap[FirstName

[clang] [NFC][Clang] Use StringSwitch instead of array for parsing attribute scope (PR #115414)

2024-11-07 Thread Chinmay Deshpande via cfe-commits

https://github.com/chinmaydd created 
https://github.com/llvm/llvm-project/pull/115414

None

>From ce67d44d55ef488bac9428b4c7b78e37ea8471f6 Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande 
Date: Thu, 7 Nov 2024 22:05:03 -0500
Subject: [PATCH] [NFC][Clang] Use StringSwitch instead of array for parsing
 attribute scope

Change-Id: Iacfabcfd3a1cedeb7c864b3b59bad88b2177e7a8
---
 .../include/clang/Basic/AttributeCommonInfo.h |  2 +-
 clang/lib/Basic/Attributes.cpp| 34 +--
 2 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h 
b/clang/include/clang/Basic/AttributeCommonInfo.h
index 11c64547721739..350154859ccdec 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
 IgnoredAttribute,
 UnknownAttribute,
   };
-  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
+  enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV, INVALID };
 
 private:
   const IdentifierInfo *AttrName = nullptr;
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index 2d18fb3f9d5bb2..a2ffaa147f3ef7 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -18,6 +18,7 @@
 #include "clang/Basic/TargetInfo.h"
 
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringSwitch.h"
 
 using namespace clang;
 
@@ -155,26 +156,23 @@ std::string AttributeCommonInfo::getNormalizedFullName() 
const {
   normalizeName(getAttrName(), getScopeName(), getSyntax()));
 }
 
-// Sorted list of attribute scope names
-static constexpr std::pair ScopeList[] =
-{{"", AttributeCommonInfo::Scope::NONE},
- {"clang", AttributeCommonInfo::Scope::CLANG},
- {"gnu", AttributeCommonInfo::Scope::GNU},
- {"gsl", AttributeCommonInfo::Scope::GSL},
- {"hlsl", AttributeCommonInfo::Scope::HLSL},
- {"msvc", AttributeCommonInfo::Scope::MSVC},
- {"omp", AttributeCommonInfo::Scope::OMP},
- {"riscv", AttributeCommonInfo::Scope::RISCV}};
-
 AttributeCommonInfo::Scope
 getScopeFromNormalizedScopeName(StringRef ScopeName) {
-  auto It = std::lower_bound(
-  std::begin(ScopeList), std::end(ScopeList), ScopeName,
-  [](const std::pair &Element,
- StringRef Value) { return Element.first < Value; });
-  assert(It != std::end(ScopeList) && It->first == ScopeName);
-
-  return It->second;
+  AttributeCommonInfo::Scope ParsedScope =
+  llvm::StringSwitch(ScopeName)
+  .Case("", AttributeCommonInfo::Scope::NONE)
+  .Case("clang", AttributeCommonInfo::Scope::CLANG)
+  .Case("gnu", AttributeCommonInfo::Scope::GNU)
+  .Case("gsl", AttributeCommonInfo::Scope::GSL)
+  .Case("hlsl", AttributeCommonInfo::Scope::HLSL)
+  .Case("msvc", AttributeCommonInfo::Scope::MSVC)
+  .Case("omp", AttributeCommonInfo::Scope::OMP)
+  .Case("riscv", AttributeCommonInfo::Scope::RISCV)
+  .Default(AttributeCommonInfo::Scope::INVALID);
+
+  assert(ParsedScope != AttributeCommonInfo::Scope::INVALID);
+
+  return ParsedScope;
 }
 
 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits