Skip to content

Conversation

w-gc
Copy link

@w-gc w-gc commented Sep 12, 2025

No description provided.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 12, 2025

@llvm/pr-subscribers-llvm-ir

Author: None (w-gc)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/IR/Value.cpp (+9)
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();

@w-gc w-gc changed the title [IR] Fix string overlap issue in Value::setNameImpl [IR] Fix string overlap issue in Value::setNameImpl Sep 15, 2025
@zhaosiying12138
Copy link

LGTM.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Can you please add a unit test for this?

if (getSymTab(this, ST))
return; // Cannot set a name on this value (e.g. constant).

// Copy NewName forcely if the memory overlaps.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Copy NewName forcely if the memory overlaps.
// Copy NewName if the memory overlaps.

or "forcefully", but I think you can just omit it.

if (!NameRef.empty()) {
StringRef OldNameRef = getName();
if ((OldNameRef.bytes_begin() < NameRef.bytes_end()) &&
(NameRef.bytes_begin() < OldNameRef.bytes_end())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this is technically UB in the case where it doesn't overlap. One has to use std::less in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants