Skip to content

Commit 3d2772a

Browse files
andrey-golubevAsyaProninahrotuna
committed
[mlir][NFC] Apply rule of five to *Pass classes
Define all special member functions for mlir::Pass, mlir::OperationPass, mlir::PassWrapper and PassGen types since these classes explicitly specify copy-ctor. This, subsequently, should silence static analysis checkers that report rule-of-3 / rule-of-5 violations. Given the nature of the types, however, mark other special member functions deleted: the semantics of a Pass type object seems to be that it is only ever created by being wrapped in a smart pointer, so the special member functions are never to be used externally (except for the copy-ctor - it is "special" since it is a "delegating" ctor for derived pass types to use during cloning - see https://reviews.llvm.org/D104302 for details). Deleting other member functions means that `Pass x(std::move(y))` - that used to silently work (via copy-ctor) - would fail to compile now. Yet, as the copy ctors through the hierarchy are under 'protected' access, the issue is unlikely to appear in practice. Co-authored-by: Asya Pronina <[email protected]> Co-authored-by: Harald Rotuna <[email protected]>
1 parent 8f30b62 commit 3d2772a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

mlir/include/mlir/Pass/Pass.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ class Pass {
161161
explicit Pass(TypeID passID, std::optional<StringRef> opName = std::nullopt)
162162
: passID(passID), opName(opName) {}
163163
Pass(const Pass &other) : Pass(other.passID, other.opName) {}
164+
Pass &operator=(const Pass &) = delete;
165+
Pass(Pass &&) = delete;
166+
Pass &operator=(Pass &&) = delete;
164167

165168
/// Returns the current pass state.
166169
detail::PassExecutionState &getPassState() {
@@ -349,9 +352,15 @@ class Pass {
349352
/// - A 'std::unique_ptr<Pass> clonePass() const' method.
350353
template <typename OpT = void>
351354
class OperationPass : public Pass {
355+
public:
356+
~OperationPass() = default;
357+
352358
protected:
353359
OperationPass(TypeID passID) : Pass(passID, OpT::getOperationName()) {}
354360
OperationPass(const OperationPass &) = default;
361+
OperationPass &operator=(const OperationPass &) = delete;
362+
OperationPass(OperationPass &&) = delete;
363+
OperationPass &operator=(OperationPass &&) = delete;
355364

356365
/// Support isa/dyn_cast functionality.
357366
static bool classof(const Pass *pass) {
@@ -388,9 +397,15 @@ class OperationPass : public Pass {
388397
/// - A 'std::unique_ptr<Pass> clonePass() const' method.
389398
template <>
390399
class OperationPass<void> : public Pass {
400+
public:
401+
~OperationPass() = default;
402+
391403
protected:
392404
OperationPass(TypeID passID) : Pass(passID) {}
393405
OperationPass(const OperationPass &) = default;
406+
OperationPass &operator=(const OperationPass &) = delete;
407+
OperationPass(OperationPass &&) = delete;
408+
OperationPass &operator=(OperationPass &&) = delete;
394409

395410
/// Indicate if the current pass can be scheduled on the given operation type.
396411
/// By default, generic operation passes can be scheduled on any operation.
@@ -444,10 +459,14 @@ class PassWrapper : public BaseT {
444459
static bool classof(const Pass *pass) {
445460
return pass->getTypeID() == TypeID::get<PassT>();
446461
}
462+
~PassWrapper() = default;
447463

448464
protected:
449465
PassWrapper() : BaseT(TypeID::get<PassT>()) {}
450466
PassWrapper(const PassWrapper &) = default;
467+
PassWrapper &operator=(const PassWrapper &) = delete;
468+
PassWrapper(PassWrapper &&) = delete;
469+
PassWrapper &operator=(PassWrapper &&) = delete;
451470

452471
/// Returns the derived pass name.
453472
StringRef getName() const override { return llvm::getTypeName<PassT>(); }

mlir/tools/mlir-tblgen/PassGen.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ class {0}Base : public {1} {
183183
184184
{0}Base() : {1}(::mlir::TypeID::get<DerivedT>()) {{}
185185
{0}Base(const {0}Base &other) : {1}(other) {{}
186+
{0}Base& operator=(const {0}Base &) = delete;
187+
{0}Base({0}Base &&) = delete;
188+
{0}Base& operator=({0}Base &&) = delete;
189+
~{0}Base() = default;
186190
187191
/// Returns the command-line argument attached to this pass.
188192
static constexpr ::llvm::StringLiteral getArgumentName() {
@@ -380,6 +384,10 @@ class {0}Base : public {1} {
380384
381385
{0}Base() : {1}(::mlir::TypeID::get<DerivedT>()) {{}
382386
{0}Base(const {0}Base &other) : {1}(other) {{}
387+
{0}Base& operator=(const {0}Base &) = delete;
388+
{0}Base({0}Base &&) = delete;
389+
{0}Base& operator=({0}Base &&) = delete;
390+
~{0}Base() = default;
383391
384392
/// Returns the command-line argument attached to this pass.
385393
static constexpr ::llvm::StringLiteral getArgumentName() {

0 commit comments

Comments
 (0)