Changeset View
Changeset View
Standalone View
Standalone View
mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Show First 20 Lines • Show All 951 Lines • ▼ Show 20 Lines | |||||
static void emitAttrGetterWithReturnType(FmtContext &fctx, | static void emitAttrGetterWithReturnType(FmtContext &fctx, | ||||
OpClassOrAdaptor &opClass, | OpClassOrAdaptor &opClass, | ||||
const Operator &op, StringRef name, | const Operator &op, StringRef name, | ||||
Attribute attr) { | Attribute attr) { | ||||
auto *method = opClass.addMethod(attr.getReturnType(), name); | auto *method = opClass.addMethod(attr.getReturnType(), name); | ||||
ERROR_IF_PRUNED(method, name, op); | ERROR_IF_PRUNED(method, name, op); | ||||
auto &body = method->body(); | auto &body = method->body(); | ||||
body << " auto attr = " << name << "Attr();\n"; | body << " auto attr = " << name << "Attr();\n"; | ||||
if (attr.hasDefaultValue()) { | if (attr.hasDefaultValue() && attr.isOptional()) { | ||||
// Returns the default value if not set. | // Returns the default value if not set. | ||||
// TODO: this is inefficient, we are recreating the attribute for every | // TODO: this is inefficient, we are recreating the attribute for every | ||||
// call. This should be set instead. | // call. This should be set instead. | ||||
if (!attr.isConstBuildable()) { | if (!attr.isConstBuildable()) { | ||||
PrintFatalError("DefaultValuedAttr of type " + attr.getAttrDefName() + | PrintFatalError("DefaultValuedAttr of type " + attr.getAttrDefName() + | ||||
" must have a constBuilder"); | " must have a constBuilder"); | ||||
} | } | ||||
std::string defaultValue = std::string( | std::string defaultValue = std::string( | ||||
▲ Show 20 Lines • Show All 642 Lines • ▼ Show 20 Lines | void OpEmitter::genPopulateDefaultAttributes() { | ||||
body << "::mlir::Builder " << odsBuilder | body << "::mlir::Builder " << odsBuilder | ||||
<< "(attrNames.front().getContext());\n"; | << "(attrNames.front().getContext());\n"; | ||||
StringMap<int> attrIndex; | StringMap<int> attrIndex; | ||||
for (const auto &it : llvm::enumerate(emitHelper.getAttrMetadata())) { | for (const auto &it : llvm::enumerate(emitHelper.getAttrMetadata())) { | ||||
attrIndex[it.value().first] = it.index(); | attrIndex[it.value().first] = it.index(); | ||||
} | } | ||||
for (const NamedAttribute &namedAttr : op.getAttributes()) { | for (const NamedAttribute &namedAttr : op.getAttributes()) { | ||||
auto &attr = namedAttr.attr; | auto &attr = namedAttr.attr; | ||||
if (!attr.hasDefaultValue()) | if (!attr.hasDefaultValue() || attr.isOptional()) | ||||
continue; | continue; | ||||
auto index = attrIndex[namedAttr.name]; | auto index = attrIndex[namedAttr.name]; | ||||
body << "if (!attributes.get(attrNames[" << index << "])) {\n"; | body << "if (!attributes.get(attrNames[" << index << "])) {\n"; | ||||
FmtContext fctx; | FmtContext fctx; | ||||
fctx.withBuilder(odsBuilder); | fctx.withBuilder(odsBuilder); | ||||
std::string defaultValue = std::string( | std::string defaultValue = std::string( | ||||
tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue())); | tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue())); | ||||
body.indent() << formatv(" attributes.append(attrNames[{0}], {1});\n", | body.indent() << formatv(" attributes.append(attrNames[{0}], {1});\n", | ||||
▲ Show 20 Lines • Show All 1,281 Lines • ▼ Show 20 Lines | auto emitAttrWithStorageType = [&](StringRef name, StringRef emitName, | ||||
auto &body = method->body().indent(); | auto &body = method->body().indent(); | ||||
body << "assert(odsAttrs && \"no attributes when constructing adapter\");\n" | body << "assert(odsAttrs && \"no attributes when constructing adapter\");\n" | ||||
<< formatv("auto attr = {0}.{1}<{2}>();\n", emitHelper.getAttr(name), | << formatv("auto attr = {0}.{1}<{2}>();\n", emitHelper.getAttr(name), | ||||
attr.hasDefaultValue() || attr.isOptional() | attr.hasDefaultValue() || attr.isOptional() | ||||
? "dyn_cast_or_null" | ? "dyn_cast_or_null" | ||||
: "cast", | : "cast", | ||||
attr.getStorageType()); | attr.getStorageType()); | ||||
if (attr.hasDefaultValue()) { | if (attr.hasDefaultValue() && attr.isOptional()) { | ||||
// Use the default value if attribute is not set. | // Use the default value if attribute is not set. | ||||
// TODO: this is inefficient, we are recreating the attribute for every | // TODO: this is inefficient, we are recreating the attribute for every | ||||
// call. This should be set instead. | // call. This should be set instead. | ||||
std::string defaultValue = std::string( | std::string defaultValue = std::string( | ||||
tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue())); | tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue())); | ||||
body << " if (!attr)\n attr = " << defaultValue << ";\n"; | body << " if (!attr)\n attr = " << defaultValue << ";\n"; | ||||
} | } | ||||
body << "return attr;\n"; | body << "return attr;\n"; | ||||
▲ Show 20 Lines • Show All 173 Lines • Show Last 20 Lines |