diff --git a/mlir/include/mlir/IR/Function.h b/mlir/include/mlir/IR/Function.h --- a/mlir/include/mlir/IR/Function.h +++ b/mlir/include/mlir/IR/Function.h @@ -17,6 +17,7 @@ #include "mlir/IR/Block.h" #include "mlir/IR/FunctionSupport.h" #include "mlir/IR/OpDefinition.h" +#include "mlir/IR/OwningOpRef.h" #include "mlir/IR/SymbolTable.h" #include "llvm/Support/PointerLikeTypeTraits.h" @@ -111,6 +112,14 @@ return success(); } }; + +/// This class acts as an owning reference to a function, and will automatically +/// destroy the held function if valid. +class OwningFuncRef : public OwningOpRef { +public: + using OwningOpRef::OwningOpRef; +}; + } // end namespace mlir namespace llvm { diff --git a/mlir/include/mlir/IR/Module.h b/mlir/include/mlir/IR/Module.h --- a/mlir/include/mlir/IR/Module.h +++ b/mlir/include/mlir/IR/Module.h @@ -13,6 +13,7 @@ #ifndef MLIR_IR_MODULE_H #define MLIR_IR_MODULE_H +#include "mlir/IR/OwningOpRef.h" #include "mlir/IR/SymbolTable.h" #include "llvm/Support/PointerLikeTypeTraits.h" @@ -114,39 +115,9 @@ /// This class acts as an owning reference to a module, and will automatically /// destroy the held module if valid. -class OwningModuleRef { +class OwningModuleRef : public OwningOpRef { public: - OwningModuleRef(std::nullptr_t = nullptr) {} - OwningModuleRef(ModuleOp module) : module(module) {} - OwningModuleRef(OwningModuleRef &&other) : module(other.release()) {} - ~OwningModuleRef() { - if (module) - module.erase(); - } - - // Assign from another module reference. - OwningModuleRef &operator=(OwningModuleRef &&other) { - if (module) - module.erase(); - module = other.release(); - return *this; - } - - /// Allow accessing the internal module. - ModuleOp get() const { return module; } - ModuleOp operator*() const { return module; } - ModuleOp *operator->() { return &module; } - explicit operator bool() const { return module; } - - /// Release the referenced module. - ModuleOp release() { - ModuleOp released; - std::swap(released, module); - return released; - } - -private: - ModuleOp module; + using OwningOpRef::OwningOpRef; }; } // end namespace mlir diff --git a/mlir/include/mlir/IR/OwningOpRef.h b/mlir/include/mlir/IR/OwningOpRef.h new file mode 100644 --- /dev/null +++ b/mlir/include/mlir/IR/OwningOpRef.h @@ -0,0 +1,57 @@ +//===- OwningOpRef.h - MLIR Module Class ------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_IR_OWNING_OP_REF_H +#define MLIR_IR_OWNING_OP_REF_H + +#include +#include + +namespace mlir { + +/// This class acts as an owning reference to an op, and will automatically +/// destroy the held op if valid. +template +class OwningOpRef { +public: + OwningOpRef(std::nullptr_t = nullptr) {} + OwningOpRef(T op) : op(op) {} + OwningOpRef(OwningOpRef &&other) : op(other.release()) {} + ~OwningOpRef() { + if (op) + op.erase(); + } + + // Assign from another op reference. + OwningOpRef &operator=(OwningOpRef &&other) { + if (op) + op.erase(); + op = other.release(); + return *this; + } + + /// Allow accessing the internal op. + T get() const { return op; } + T operator*() const { return op; } + T *operator->() { return &op; } + explicit operator bool() const { return op; } + + /// Release the referenced op. + T release() { + T released; + std::swap(released, op); + return released; + } + +private: + T op; +}; + +} // end namespace mlir + +#endif // MLIR_IR_OWNING_OP_REF_H