[PATCH] D128913: [IR] Have Value::takeName() transfer intrinsic properties

Fraser Cormack via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 30 07:27:45 PDT 2022


frasercrmck created this revision.
frasercrmck added reviewers: aeubanks, jlebar.
Herald added a subscriber: hiraditya.
Herald added a project: All.
frasercrmck requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Functions are considered intrinsics if their names begin with "llvm.",
even if they don't have a valid intrinsic ID. This is a cached property,
stored in GlobalValue's HasLLVMReservedName.

When calling Value::setName(), this property is recalculated. However,
this property is not recalculated when taking names from another Value.
While it's not documented as doing such, the semantics of "takeName"
suggest that A.takeName(B) is the same as A.setName(B.getName())
followed by B.setName("").

This patch updates Value::takeName to act in this fashion, to avoid any
unexpected behaviour: The new function is an intrinsic since it now starts with
"llvm." and the old function is no longer considered an intrinsic, since it no
longer begins with "llvm.".


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128913

Files:
  llvm/lib/IR/Value.cpp
  llvm/unittests/IR/ValueTest.cpp


Index: llvm/unittests/IR/ValueTest.cpp
===================================================================
--- llvm/unittests/IR/ValueTest.cpp
+++ llvm/unittests/IR/ValueTest.cpp
@@ -312,4 +312,48 @@
   ASSERT_TRUE(ExitDbg->getValue(0) == cast<Value>(B));
   ASSERT_TRUE(Ret->getOperand(0) == cast<Value>(B));
 }
+
+TEST(ValueTest, IntrinsicFunctionNames) {
+  LLVMContext C;
+  const char *ModuleString = "declare void @llvm.foo(i32)\n";
+
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
+
+  // Any function beginning with "llvm." has an LLVM-reserved name and is
+  // considered an intrinsic.
+  Function *F = M->getFunction("llvm.foo");
+  EXPECT_TRUE(F->isIntrinsic());
+
+  // Create an intrinsic function explicitly. Check that it is indeed an
+  // intrinsic.
+  auto *FTy = F->getFunctionType();
+  auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "llvm.foo", *M);
+  EXPECT_TRUE(F1->isIntrinsic());
+
+  // Create a new non-intrinsic function. Set the name from a known intrinsic -
+  // this should become an intrinsic. The old function should still be an
+  // intrinsic.
+  auto *F2 = Function::Create(FTy, GlobalValue::ExternalLinkage, "bar", *M);
+  F2->setName(F1->getName());
+  EXPECT_TRUE(F2->isIntrinsic());
+  EXPECT_TRUE(F1->isIntrinsic());
+
+  // Create a new non-intrinsic function. *Take* the name from an intrinsic -
+  // this should become be an intrinsic. The old function should no longer be
+  // an intrinsic.
+  auto *F3 = Function::Create(FTy, GlobalValue::ExternalLinkage, "baz", *M);
+  F3->takeName(F);
+  EXPECT_FALSE(F->isIntrinsic());
+  EXPECT_TRUE(F3->isIntrinsic());
+
+  F->setName("baz");
+  EXPECT_FALSE(F->isIntrinsic());
+
+  // Have an intrinsic take the name of a non-intrinsic and neither are
+  // intrinsics afterwards.
+  F3->takeName(F);
+  EXPECT_FALSE(F3->isIntrinsic());
+  EXPECT_FALSE(F->isIntrinsic());
+}
 } // end anonymous namespace
Index: llvm/lib/IR/Value.cpp
===================================================================
--- llvm/lib/IR/Value.cpp
+++ llvm/lib/IR/Value.cpp
@@ -420,6 +420,10 @@
     setValueName(V->getValueName());
     V->setValueName(nullptr);
     getValueName()->setValue(this);
+    if (Function *F = dyn_cast<Function>(V))
+      F->recalculateIntrinsicID();
+    if (Function *F = dyn_cast<Function>(this))
+      F->recalculateIntrinsicID();
     return;
   }
 
@@ -432,6 +436,11 @@
   V->setValueName(nullptr);
   getValueName()->setValue(this);
 
+  if (Function *F = dyn_cast<Function>(V))
+    F->recalculateIntrinsicID();
+  if (Function *F = dyn_cast<Function>(this))
+    F->recalculateIntrinsicID();
+
   if (ST)
     ST->reinsertValue(this);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128913.441390.patch
Type: text/x-patch
Size: 2715 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220630/c2c1a69e/attachment.bin>


More information about the llvm-commits mailing list