Implement a proper function list in module, which auto-maintain the parent
pointer, and ensure that functions are deleted when the module is destroyed.
This exposed the fact that MLFunction had no dtor, and that the dtor in
CFGFunction was broken with cyclic references. Fix both of these problems.
PiperOrigin-RevId: 206051666
diff --git a/lib/IR/Function.cpp b/lib/IR/Function.cpp
index 72ec443..8476b06 100644
--- a/lib/IR/Function.cpp
+++ b/lib/IR/Function.cpp
@@ -17,6 +17,7 @@
#include "mlir/IR/CFGFunction.h"
#include "mlir/IR/MLFunction.h"
+#include "mlir/IR/Module.h"
#include "mlir/IR/Types.h"
#include "llvm/ADT/StringRef.h"
using namespace mlir;
@@ -27,6 +28,64 @@
MLIRContext *Function::getContext() const { return getType()->getContext(); }
+/// Delete this object.
+void Function::destroy() {
+ switch (getKind()) {
+ case Kind::ExtFunc:
+ delete cast<ExtFunction>(this);
+ break;
+ case Kind::MLFunc:
+ delete cast<MLFunction>(this);
+ break;
+ case Kind::CFGFunc:
+ delete cast<CFGFunction>(this);
+ break;
+ }
+}
+
+Module *llvm::ilist_traits<Function>::getContainingModule() {
+ size_t Offset(
+ size_t(&((Module *)nullptr->*Module::getSublistAccess(nullptr))));
+ iplist<Function> *Anchor(static_cast<iplist<Function> *>(this));
+ return reinterpret_cast<Module *>(reinterpret_cast<char *>(Anchor) - Offset);
+}
+
+/// This is a trait method invoked when a Function is added to a Module. We
+/// keep the module pointer up to date.
+void llvm::ilist_traits<Function>::addNodeToList(Function *function) {
+ assert(!function->getModule() && "already in a module!");
+ function->module = getContainingModule();
+}
+
+/// This is a trait method invoked when a Function is removed from a Module.
+/// We keep the module pointer up to date.
+void llvm::ilist_traits<Function>::removeNodeFromList(Function *function) {
+ assert(function->module && "not already in a module!");
+ function->module = nullptr;
+}
+
+/// This is a trait method invoked when an instruction is moved from one block
+/// to another. We keep the block pointer up to date.
+void llvm::ilist_traits<Function>::transferNodesFromList(
+ ilist_traits<Function> &otherList, function_iterator first,
+ function_iterator last) {
+ // If we are transferring functions within the same module, the Module
+ // pointer doesn't need to be updated.
+ Module *curParent = getContainingModule();
+ if (curParent == otherList.getContainingModule())
+ return;
+
+ // Update the 'module' member of each function.
+ for (; first != last; ++first)
+ first->module = curParent;
+}
+
+/// Unlink this function from its Module and delete it.
+void Function::eraseFromModule() {
+ assert(getModule() && "Function has no parent");
+ getModule()->getFunctions().erase(this);
+}
+
//===----------------------------------------------------------------------===//
// ExtFunction implementation.
//===----------------------------------------------------------------------===//
@@ -43,9 +102,23 @@
: Function(name, type, Kind::CFGFunc) {
}
+CFGFunction::~CFGFunction() {
+ // Instructions may have cyclic references, which need to be dropped before we
+ // can start deleting them.
+ for (auto &bb : *this) {
+ for (auto &inst : bb)
+ inst.dropAllReferences();
+ }
+}
+
//===----------------------------------------------------------------------===//
// MLFunction implementation.
//===----------------------------------------------------------------------===//
MLFunction::MLFunction(StringRef name, FunctionType *type)
: Function(name, type, Kind::MLFunc), StmtBlock(StmtBlockKind::MLFunc) {}
+
+MLFunction::~MLFunction() {
+ // TODO: When move SSA stuff is supported.
+ // dropAllReferences();
+}