Skip to content

[RISCV] Use StringRef for RequiredExtensions in RVVIntrinsicDef #143503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025

Conversation

4vtomat
Copy link
Member

@4vtomat 4vtomat commented Jun 10, 2025

This prevents many duplicated copies of required extensions string.

@llvmbot llvmbot added clang Clang issues not falling into any other category backend:RISC-V clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 10, 2025

@llvm/pr-subscribers-backend-risc-v

Author: Brandon Wu (4vtomat)

Changes

This prevents many duplicated copies of required extensions string.


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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaRISCV.cpp (+9-3)
diff --git a/clang/lib/Sema/SemaRISCV.cpp b/clang/lib/Sema/SemaRISCV.cpp
index 9f70be746eb3f..628a3e5e8b417 100644
--- a/clang/lib/Sema/SemaRISCV.cpp
+++ b/clang/lib/Sema/SemaRISCV.cpp
@@ -27,6 +27,7 @@
 #include "clang/Sema/Sema.h"
 #include "clang/Support/RISCVVIntrinsicUtils.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/StringSaver.h"
 #include "llvm/TargetParser/RISCVISAInfo.h"
 #include "llvm/TargetParser/RISCVTargetParser.h"
 #include <optional>
@@ -47,7 +48,7 @@ struct RVVIntrinsicDef {
   std::string BuiltinName;
 
   /// Mapping to RequiredFeatures in riscv_vector.td
-  std::string RequiredExtensions;
+  StringRef RequiredExtensions;
 
   /// Function signature, first element is return type.
   RVVTypes Signature;
@@ -192,6 +193,10 @@ class RISCVIntrinsicManagerImpl : public sema::RISCVIntrinsicManager {
   // Mapping function name to RVVOverloadIntrinsicDef.
   StringMap<RVVOverloadIntrinsicDef> OverloadIntrinsics;
 
+  // Caching the required extension strings.
+  BumpPtrAllocator StrAlloc;
+  UniqueStringSaver StrPool;
+
   // Create RVVIntrinsicDef.
   void InitRVVIntrinsic(const RVVIntrinsicRecord &Record, StringRef SuffixStr,
                         StringRef OverloadedSuffixStr, bool IsMask,
@@ -206,7 +211,7 @@ class RISCVIntrinsicManagerImpl : public sema::RISCVIntrinsicManager {
                               IntrinsicKind K);
 
 public:
-  RISCVIntrinsicManagerImpl(clang::Sema &S) : S(S) {
+  RISCVIntrinsicManagerImpl(clang::Sema &S) : S(S), StrPool(StrAlloc) {
     ConstructedRISCVVBuiltins = false;
     ConstructedRISCVSiFiveVectorBuiltins = false;
     ConstructedRISCVAndesVectorBuiltins = false;
@@ -385,7 +390,8 @@ void RISCVIntrinsicManagerImpl::InitRVVIntrinsic(
   uint32_t Index = IntrinsicList.size();
   assert(IntrinsicList.size() == (size_t)Index &&
          "Intrinsics indices overflow.");
-  IntrinsicList.push_back({BuiltinName, Record.RequiredExtensions, Signature});
+  IntrinsicList.push_back(
+      {BuiltinName, StrPool.save(Record.RequiredExtensions), Signature});
 
   // Creating mapping to Intrinsics.
   Intrinsics.insert({Name, Index});

@llvmbot
Copy link
Member

llvmbot commented Jun 10, 2025

@llvm/pr-subscribers-clang

Author: Brandon Wu (4vtomat)

Changes

This prevents many duplicated copies of required extensions string.


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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaRISCV.cpp (+9-3)
diff --git a/clang/lib/Sema/SemaRISCV.cpp b/clang/lib/Sema/SemaRISCV.cpp
index 9f70be746eb3f..628a3e5e8b417 100644
--- a/clang/lib/Sema/SemaRISCV.cpp
+++ b/clang/lib/Sema/SemaRISCV.cpp
@@ -27,6 +27,7 @@
 #include "clang/Sema/Sema.h"
 #include "clang/Support/RISCVVIntrinsicUtils.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/StringSaver.h"
 #include "llvm/TargetParser/RISCVISAInfo.h"
 #include "llvm/TargetParser/RISCVTargetParser.h"
 #include <optional>
@@ -47,7 +48,7 @@ struct RVVIntrinsicDef {
   std::string BuiltinName;
 
   /// Mapping to RequiredFeatures in riscv_vector.td
-  std::string RequiredExtensions;
+  StringRef RequiredExtensions;
 
   /// Function signature, first element is return type.
   RVVTypes Signature;
@@ -192,6 +193,10 @@ class RISCVIntrinsicManagerImpl : public sema::RISCVIntrinsicManager {
   // Mapping function name to RVVOverloadIntrinsicDef.
   StringMap<RVVOverloadIntrinsicDef> OverloadIntrinsics;
 
+  // Caching the required extension strings.
+  BumpPtrAllocator StrAlloc;
+  UniqueStringSaver StrPool;
+
   // Create RVVIntrinsicDef.
   void InitRVVIntrinsic(const RVVIntrinsicRecord &Record, StringRef SuffixStr,
                         StringRef OverloadedSuffixStr, bool IsMask,
@@ -206,7 +211,7 @@ class RISCVIntrinsicManagerImpl : public sema::RISCVIntrinsicManager {
                               IntrinsicKind K);
 
 public:
-  RISCVIntrinsicManagerImpl(clang::Sema &S) : S(S) {
+  RISCVIntrinsicManagerImpl(clang::Sema &S) : S(S), StrPool(StrAlloc) {
     ConstructedRISCVVBuiltins = false;
     ConstructedRISCVSiFiveVectorBuiltins = false;
     ConstructedRISCVAndesVectorBuiltins = false;
@@ -385,7 +390,8 @@ void RISCVIntrinsicManagerImpl::InitRVVIntrinsic(
   uint32_t Index = IntrinsicList.size();
   assert(IntrinsicList.size() == (size_t)Index &&
          "Intrinsics indices overflow.");
-  IntrinsicList.push_back({BuiltinName, Record.RequiredExtensions, Signature});
+  IntrinsicList.push_back(
+      {BuiltinName, StrPool.save(Record.RequiredExtensions), Signature});
 
   // Creating mapping to Intrinsics.
   Intrinsics.insert({Name, Index});

@@ -385,7 +390,8 @@ void RISCVIntrinsicManagerImpl::InitRVVIntrinsic(
uint32_t Index = IntrinsicList.size();
assert(IntrinsicList.size() == (size_t)Index &&
"Intrinsics indices overflow.");
IntrinsicList.push_back({BuiltinName, Record.RequiredExtensions, Signature});
IntrinsicList.push_back(
{BuiltinName, StrPool.save(Record.RequiredExtensions), Signature});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the StringSaver? Record.RequiredExtensions should already point to a single global string in the .rodata that was uniqued by the linker. We can just make a StringRef from that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a good point, thanks!

This prevents many duplicated copies of required extensions string.
@4vtomat 4vtomat force-pushed the cache_required_ext_str branch from 947142e to 06013a6 Compare June 11, 2025 02:18
@4vtomat 4vtomat changed the title [RISCV] Cache required extensions string for RVVIntrinsicDef [RISCV] Use StringRef for RequiredExtensions in RVVIntrinsicDef Jun 11, 2025
Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@4vtomat 4vtomat merged commit 5f231db into llvm:main Jun 12, 2025
7 checks passed
@4vtomat 4vtomat deleted the cache_required_ext_str branch June 12, 2025 03:42
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
…#143503)

This prevents many duplicated copies of required extensions string.
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
…#143503)

This prevents many duplicated copies of required extensions string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:RISC-V clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants