From 088f4c502f9fcba5f39d255ff6cdcf2ab050b201 Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Wed, 2 Oct 2019 09:15:53 -0700 Subject: [PATCH] Fix example in OpInterfaces documentation The concept-based polymorphism structure was missing an inheritance link between the concept and the model. The interface class did not re-export the base class constructor, which made it unusable with llvm::isa calls. Fix these and reformat the code around. PiperOrigin-RevId: 272452062 --- mlir/g3doc/Interfaces.md | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/mlir/g3doc/Interfaces.md b/mlir/g3doc/Interfaces.md index 004a65c..c4d517b 100644 --- a/mlir/g3doc/Interfaces.md +++ b/mlir/g3doc/Interfaces.md @@ -124,26 +124,29 @@ registration. ```c++ struct ExampleOpInterfaceTraits { -/// Define a base concept class that defines the virtual interface that needs -/// to be overridden. -struct Concept { - virtual ~Concept(); - virtual unsigned getNumInputs(Operation *op) = 0; -}; - -/// Define a model class that specializes a concept on a given operation type. -template -struct Model { - /// Override the method to dispatch on the concrete operation. - unsigned getNumInputs(Operation *op) final { - return llvm::cast(op).getNumInputs(); - } -}; + /// Define a base concept class that defines the virtual interface that needs + /// to be overridden. + struct Concept { + virtual ~Concept(); + virtual unsigned getNumInputs(Operation *op) = 0; + }; + + /// Define a model class that specializes a concept on a given operation type. + template + struct Model : public Concept { + /// Override the method to dispatch on the concrete operation. + unsigned getNumInputs(Operation *op) final { + return llvm::cast(op).getNumInputs(); + } + }; }; class ExampleOpInterface : public OpInterface { public: + /// Use base class constructor to support LLVM-style casts. + using OpInterface::OpInterface; + /// The interface dispatches to 'getImpl()', an instance of the concept. unsigned getNumInputs() { return getImpl()->getNumInputs(getOperation()); @@ -169,7 +172,7 @@ public: /// interface. Operation *op = ...; if (ExampleOpInterface example = dyn_cast(op)) - llvm::errs() << "num inputs = " << example.getNumInputs() << "\n"; + llvm::errs() << "num inputs = " << example.getNumInputs() << "\n"; ``` #### Utilizing the ODS Framework -- 2.7.4