[llvm] [IR] Fix string overlap issue in Value::setNameImpl (PR #158288)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 12 05:59:23 PDT 2025


https://github.com/w-gc created https://github.com/llvm/llvm-project/pull/158288

None

>From f3ea12956a98061c2452238aefcad9e075a4de8e Mon Sep 17 00:00:00 2001
From: w-gc <25614556+w-gc at users.noreply.github.com>
Date: Fri, 12 Sep 2025 20:58:12 +0800
Subject: [PATCH] [IR] Fix string overlap issue in Value::setNameImpl

---
 llvm/lib/IR/Value.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index 4e8f359481b81..a7aa7fb04d99e 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -374,6 +374,15 @@ void Value::setNameImpl(const Twine &NewName) {
   // NOTE: Could optimize for the case the name is shrinking to not deallocate
   // then reallocated.
   if (hasName()) {
+    if (!NameRef.empty()) {
+      StringRef OldNameRef = getName();
+      if ((OldNameRef.bytes_begin() < NameRef.bytes_end()) &&
+          (NameRef.bytes_begin() < OldNameRef.bytes_end())) {
+        NewName.toVector(NameData);
+        NameRef = StringRef(NameData.data(), NameData.size());
+      }
+    }
+
     // Remove old name.
     ST->removeValueName(getValueName());
     destroyValueName();



More information about the llvm-commits mailing list