Skip to content

[mlir][ods] Optimize FoldAdaptor constructor #93219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions mlir/test/mlir-tblgen/op-decl-and-defs.td
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def NS_AOp : NS_Op<"a_op", [IsolatedFromAbove, IsolatedFromAbove]> {
// CHECK: namespace detail {
// CHECK: class AOpGenericAdaptorBase {
// CHECK: public:
// CHECK: AOpGenericAdaptorBase(AOp{{[[:space:]]}}
// CHECK: AOpGenericAdaptorBase(::mlir::DictionaryAttr attrs = {}, const ::mlir::EmptyProperties &properties = {}, ::mlir::RegionRange regions = {}) : odsAttrs(attrs), odsRegions(regions)
// CHECK: AOpGenericAdaptorBase(::mlir::Operation *op) : odsAttrs(op->getRawDictionaryAttrs()), odsOpName(op->getName()), odsRegions(op->getRegions()) {}
// CHECK: ::mlir::IntegerAttr getAttr1Attr();
// CHECK: uint32_t getAttr1();
// CHECK: ::mlir::FloatAttr getSomeAttr2Attr();
Expand Down Expand Up @@ -128,15 +129,8 @@ def NS_AOp : NS_Op<"a_op", [IsolatedFromAbove, IsolatedFromAbove]> {

// DEFS-LABEL: NS::AOp definitions

// DEFS: AOpGenericAdaptorBase::AOpGenericAdaptorBase(::mlir::DictionaryAttr attrs, const ::mlir::EmptyProperties &properties, ::mlir::RegionRange regions) : odsAttrs(attrs), odsRegions(regions)

// Check that `getAttrDictionary()` is used when not using properties.

// DEFS: AOpGenericAdaptorBase::AOpGenericAdaptorBase(AOp op)
// DEFS-SAME: op->getAttrDictionary()
// DEFS-SAME: p.getProperties()
// DEFS-SAME: op->getRegions()

// DECLS: ::mlir::RegionRange AOpGenericAdaptorBase::getSomeRegions()
// DECLS-NEXT: return odsRegions.drop_front(1);
// DECLS: ::mlir::RegionRange AOpGenericAdaptorBase::getRegions()
Expand Down Expand Up @@ -346,10 +340,11 @@ def NS_NOp : NS_Op<"op_with_properties", []> {

// Check that `getDiscardableAttrDictionary()` is used with properties.

// DEFS: NOpGenericAdaptorBase::NOpGenericAdaptorBase(NOp op) : NOpGenericAdaptorBase(
// DEFS-SAME: op->getDiscardableAttrDictionary()
// DEFS-SAME: op.getProperties()
// DEFS-SAME: op->getRegions()
// DEFS: NOpGenericAdaptorBase::NOpGenericAdaptorBase(NOp op) :
// DEFS-SAME: odsAttrs(op->getDiscardableAttrDictionary())
// DEFS-SAME: odsOpName(op->getName())
// DEFS-SAME: properties(op.getProperties())
// DEFS-SAME: odsRegions(op->getRegions())

// Test that type defs have the proper namespaces when used as a constraint.
// ---
Expand Down
3 changes: 0 additions & 3 deletions mlir/test/mlir-tblgen/op-operand.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ def OpA : NS_Op<"one_normal_operand_op", []> {

// CHECK-LABEL: OpA definitions

// CHECK: OpAGenericAdaptorBase::OpAGenericAdaptorBase
// CHECK-SAME: odsAttrs(attrs)

// CHECK: void OpA::build
// CHECK: ::mlir::Value input
// CHECK: odsState.addOperands(input);
Expand Down
29 changes: 20 additions & 9 deletions mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4101,7 +4101,8 @@ OpOperandAdaptorEmitter::OpOperandAdaptorEmitter(
"{}");
}
paramList.emplace_back("::mlir::RegionRange", "regions", "{}");
auto *baseConstructor = genericAdaptorBase.addConstructor(paramList);
auto *baseConstructor =
genericAdaptorBase.addConstructor<Method::Inline>(paramList);
baseConstructor->addMemberInitializer("odsAttrs", "attrs");
if (useProperties)
baseConstructor->addMemberInitializer("properties", "properties");
Expand Down Expand Up @@ -4163,14 +4164,24 @@ OpOperandAdaptorEmitter::OpOperandAdaptorEmitter(
// and the value range from the parameter.
{
// Base class is in the cpp file and can simply access the members of the op
// class to initialize the template independent fields.
auto *constructor = genericAdaptorBase.addConstructor(
MethodParameter(op.getCppClassName(), "op"));
constructor->addMemberInitializer(
genericAdaptorBase.getClassName(),
llvm::Twine(!useProperties ? "op->getAttrDictionary()"
: "op->getDiscardableAttrDictionary()") +
", op.getProperties(), op->getRegions()");
// class to initialize the template independent fields. If the op doesn't
// have properties, we can emit a generic constructor inline. Otherwise,
// emit it out-of-line because we need the op to be defined.
Constructor *constructor;
if (useProperties) {
constructor = genericAdaptorBase.addConstructor(
MethodParameter(op.getCppClassName(), "op"));
} else {
constructor = genericAdaptorBase.addConstructor<Method::Inline>(
MethodParameter("::mlir::Operation *", "op"));
}
constructor->addMemberInitializer("odsAttrs",
"op->getRawDictionaryAttrs()");
// Retrieve the operation name from the op directly.
constructor->addMemberInitializer("odsOpName", "op->getName()");
if (useProperties)
constructor->addMemberInitializer("properties", "op.getProperties()");
constructor->addMemberInitializer("odsRegions", "op->getRegions()");

// Generic adaptor is templated and therefore defined inline in the header.
// We cannot use the Op class here as it is an incomplete type (we have a
Expand Down
Loading