// Op traits.
list<OpTrait> traits = props;
+
+ // Additional code that will be added to the public part of the generated
+ // C++ code of the op declaration.
+ code extraClassDeclaration = ?;
}
// The arguments of an op.
// Returns this op's C++ class name.
StringRef getCppClassName() const;
+ // Returns this op's extra class declaration code.
+ StringRef getExtraClassDeclaration() const;
+
// Returns the qualified C++ class name for the given TableGen def `name`.
// The first `_` in `name` is treated as separating the dialect namespace
// and the op class name if the dialect namespace is not empty. Otherwise,
return results->getNumArgs();
}
+StringRef tblgen::Operator::getExtraClassDeclaration() const {
+ constexpr auto attr = "extraClassDeclaration";
+ if (def.isValueUnset(attr))
+ return {};
+ return def.getValueAsString(attr);
+}
+
tblgen::TypeConstraint
tblgen::Operator::getResultTypeConstraint(int index) const {
DagInit *results = def.getValueAsDag("results");
let hasCanonicalizer = 1;
let hasConstantFolder = 1;
let hasFolder = 1;
+
+ let extraClassDeclaration = [{
+ // Display a graph for debugging purposes.
+ void displayGraph();
+ }];
}
// CHECK-LABEL: NS::AOp declarations
// CHECK: static void getCanonicalizationPatterns(OwningRewritePatternList &results, MLIRContext *context);
// CHECK: LogicalResult constantFold(ArrayRef<Attribute> operands, SmallVectorImpl<Attribute> &results, MLIRContext *context);
// CHECK: bool fold(SmallVectorImpl<Value *> &results);
+// CHECK: // Display a graph for debugging purposes.
+// CHECK: void displayGraph();
// CHECK: };
// Check op trait for different number of operands
// Class for holding an op for C++ code emission
class OpClass {
public:
- explicit OpClass(StringRef name);
+ explicit OpClass(StringRef name, StringRef extraClassDeclaration = "");
// Adds an op trait.
void addTrait(Twine trait);
void writeDefTo(raw_ostream &os) const;
private:
- std::string className;
+ StringRef className;
+ StringRef extraClassDeclaration;
SmallVector<std::string, 4> traits;
SmallVector<OpMethod, 8> methods;
};
os << "}";
}
-OpClass::OpClass(StringRef name) : className(name) {}
+OpClass::OpClass(StringRef name, StringRef extraClassDeclaration)
+ : className(name), extraClassDeclaration(extraClassDeclaration) {}
// Adds the given trait to this op. Prefixes "OpTrait::" to `trait` implicitly.
void OpClass::addTrait(Twine trait) {
method.writeDeclTo(os);
os << "\n";
}
+ // TODO: Add line control markers to make errors easier to debug.
+ os << extraClassDeclaration << "\n";
os << "};";
}
} // end anonymous namespace
OpEmitter::OpEmitter(const Record &def)
- : def(def), op(def), opClass(op.getCppClassName()) {
+ : def(def), op(def),
+ opClass(op.getCppClassName(), op.getExtraClassDeclaration()) {
genTraits();
// Generate C++ code for various op methods. The order here determines the
// methods in the generated file.