[clang] [NFC] [clang] Use std::string instead of StringRef to reduce stack usage (PR #114285)
Chinmay Deshpande via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 30 12:12:13 PDT 2024
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 <chdeshpa at amd.com>
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<AttributeCommonInfo::Syntax>(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 <chdeshpa at amd.com>
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<AttributeCommonInfo::Syntax>(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"
}
More information about the cfe-commits
mailing list