Allow a function to take the name of another existing function.
authorRiver Riddle <riverriddle@google.com>
Mon, 20 May 2019 00:41:19 +0000 (17:41 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Mon, 20 May 2019 20:48:02 +0000 (13:48 -0700)
--

PiperOrigin-RevId: 248968285

mlir/include/mlir/IR/Function.h
mlir/include/mlir/IR/Module.h
mlir/lib/IR/Function.cpp

index 24cdb4a..9aefd79 100644 (file)
@@ -54,6 +54,11 @@ public:
   /// Return the name of this function, without the @.
   Identifier getName() { return name; }
 
+  /// Swap the name of the given function with this one. The caller must ensure
+  /// that all existing references to the current name of each function have
+  /// been properly updated.
+  void takeName(Function &rhs);
+
   /// Return the type of this function.
   FunctionType getType() { return type; }
 
index d928c18..b11e074 100644 (file)
@@ -69,6 +69,7 @@ public:
 
 private:
   friend struct llvm::ilist_traits<Function>;
+  friend class Function;
 
   /// getSublistAccess() - Returns pointer to member of function list
   static FunctionListType Module::*getSublistAccess(Function *) {
index 0d44fa5..96823c6 100644 (file)
@@ -44,6 +44,14 @@ Function::~Function() {
 
 MLIRContext *Function::getContext() { return getType().getContext(); }
 
+/// Swap the name of the given function with this one.
+void Function::takeName(Function &rhs) {
+  auto *module = getModule();
+  assert(module && module == rhs.getModule() && "expected same parent module");
+  std::swap(module->symbolTable[name], module->symbolTable[rhs.getName()]);
+  std::swap(name, rhs.name);
+}
+
 Module *llvm::ilist_traits<Function>::getContainingModule() {
   size_t Offset(
       size_t(&((Module *)nullptr->*Module::getSublistAccess(nullptr))));