[llvm-branch-commits] [llvm] 80b0f74 - Small improvements to Intrinsic::getName

Xun Li via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Dec 2 16:54:13 PST 2020


Author: Xun Li
Date: 2020-12-02T16:49:12-08:00
New Revision: 80b0f74c8c539b2385f897467aa99b2b6b298c04

URL: https://github.com/llvm/llvm-project/commit/80b0f74c8c539b2385f897467aa99b2b6b298c04
DIFF: https://github.com/llvm/llvm-project/commit/80b0f74c8c539b2385f897467aa99b2b6b298c04.diff

LOG: Small improvements to Intrinsic::getName

While I was adding a new intrinsic instruction (not overloaded), I accidentally used CreateUnaryIntrinsic to create the intrinsics, which turns out to be passing the type list to getName, and ended up naming the intrinsics function with type suffix, which leads to wierd bugs latter on. It took me a long time to debug.
It seems a good idea to add an assertion in getName so that it fails if types are passed but it's not a overloaded function.
Also, the overloade version of getName is less efficient because it creates an std::string. We should avoid calling it if we know that there are no types provided.

Differential Revision: https://reviews.llvm.org/D92523

Added: 
    

Modified: 
    llvm/lib/IR/Function.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index a28577444407..37fa3d272781 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -781,6 +781,8 @@ StringRef Intrinsic::getName(ID id) {
 
 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
   assert(id < num_intrinsics && "Invalid intrinsic ID!");
+  assert((Tys.empty() || Intrinsic::isOverloaded(id)) &&
+         "This version of getName is for overloaded intrinsics only");
   std::string Result(IntrinsicNameTable[id]);
   for (Type *Ty : Tys) {
     Result += "." + getMangledTypeStr(Ty);
@@ -1243,7 +1245,7 @@ Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
   // There can never be multiple globals with the same name of 
diff erent types,
   // because intrinsics must be a specific type.
   return cast<Function>(
-      M->getOrInsertFunction(getName(id, Tys),
+      M->getOrInsertFunction(Tys.empty() ? getName(id) : getName(id, Tys),
                              getType(M->getContext(), id, Tys))
           .getCallee());
 }


        


More information about the llvm-branch-commits mailing list