Index: mlir/include/mlir/IR/Builders.h =================================================================== --- mlir/include/mlir/IR/Builders.h +++ mlir/include/mlir/IR/Builders.h @@ -281,11 +281,25 @@ class InsertionGuard { public: InsertionGuard(OpBuilder &builder) - : builder(builder), ip(builder.saveInsertionPoint()) {} - ~InsertionGuard() { builder.restoreInsertionPoint(ip); } + : builder(&builder), ip(builder.saveInsertionPoint()) {} + + ~InsertionGuard() { + if (builder) + builder->restoreInsertionPoint(ip); + } + + InsertionGuard(const InsertionGuard &) = delete; + InsertionGuard &operator=(const InsertionGuard &) = delete; + + InsertionGuard(InsertionGuard &&other) noexcept + : builder(other.builder), ip(other.ip) { + other.builder = nullptr; + } + + InsertionGuard &operator=(InsertionGuard &&other) = delete; private: - OpBuilder &builder; + OpBuilder *builder; OpBuilder::InsertPoint ip; };