Skip to content

[IR][Instructions] Add CallBase::getCalledFunctionName helper #127038

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

tsymalla
Copy link
Contributor

There's a lot of uses of getCalledFunction()->getName(), without ever checking if getCalledFunction() returns a nullptr. Add a new helper that returns an std::optional<StringRef>, that can be used to

  • Avoid method chaining
  • Have a more safe shortcut to get the callee name.

There's a lot of uses of `getCalledFunction()->getName()`, without ever
checking if `getCalledFunction()` returns a nullptr. Add a new helper
that returns an `std::optional<StringRef>`, that can be used to

- Avoid method chaining
- Have a more safe shortcut to get the callee name.
@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2025

@llvm/pr-subscribers-flang-openmp
@llvm/pr-subscribers-pgo
@llvm/pr-subscribers-backend-amdgpu
@llvm/pr-subscribers-compiler-rt-sanitizer
@llvm/pr-subscribers-llvm-transforms
@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-ir

Author: Thomas Symalla (tsymalla)

Changes

There's a lot of uses of getCalledFunction()-&gt;getName(), without ever checking if getCalledFunction() returns a nullptr. Add a new helper that returns an std::optional&lt;StringRef&gt;, that can be used to

  • Avoid method chaining
  • Have a more safe shortcut to get the callee name.

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

1 Files Affected:

  • (modified) llvm/include/llvm/IR/InstrTypes.h (+10)
diff --git a/llvm/include/llvm/IR/InstrTypes.h b/llvm/include/llvm/IR/InstrTypes.h
index 90fe864d4ae71..c55a5a589fc53 100644
--- a/llvm/include/llvm/IR/InstrTypes.h
+++ b/llvm/include/llvm/IR/InstrTypes.h
@@ -1345,6 +1345,16 @@ class CallBase : public Instruction {
     return nullptr;
   }
 
+  /// Shortcut to retrieving the name of the called function.
+  /// Returns std::nullopt, if the function cannot be found.
+  std::optional<StringRef> getCalledFunctionName() const {
+    Function *F = getCalledFunction();
+    if (F)
+      return F->getName();
+
+    return std::nullopt;
+  }
+
   /// Return true if the callsite is an indirect call.
   bool isIndirectCall() const;
 

@tsymalla tsymalla requested a review from nikic February 16, 2025 14:23
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.

If you're introducing a new helper API, please also update existing code that can make use of it.

if (F)
return F->getName();

return std::nullopt;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use an empty StringRef as the sentinel instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Technically, yes, but I think std::optional is a better fit, since using operator* on the return value would fail if no value is present.

Copy link
Contributor

Choose a reason for hiding this comment

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

Which is the kind of failure you are trying to avoid needing to worry about?

@antoniofrighetto antoniofrighetto changed the title Add CallBase::getCalledFunctionName [IR][Instructions] Add CallBase::getCalledFunctionName helper Feb 17, 2025
`LibCallsShrinkWrap::perform`: This uses the potentially `nullptr` value
inside the loop in a debug statement before asserting on it. Move the
assertion outside `perform(CallInst &)`.

Some other arbitrary uses to replace `getCalledFunction()->getName()`.
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.

From a quick git grep "getCalledFunction()->getName()", it looks like there are a lot more places that could use this.

@tsymalla
Copy link
Contributor Author

From a quick git grep "getCalledFunction()->getName()", it looks like there are a lot more places that could use this.

Yep. I wanted to see if the change is generally accepted at first, before changing all the code. I'll follow up with the remainder.

Copy link

github-actions bot commented Feb 25, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@tsymalla-AMD
Copy link
Contributor

Sorry for the massive update. All callsites of getCalledFunction()->getName() should now be updated plus some extra changes.

@tsymalla tsymalla requested a review from nikic March 4, 2025 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants