2018-12-27 04:56:03 -08:00
|
|
|
//===- OpDefinitionsGen.cpp - MLIR op definitions generator ---------------===//
|
2018-10-15 08:54:37 -07:00
|
|
|
//
|
2020-01-26 03:58:30 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-23 09:35:36 -08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-15 08:54:37 -07:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-15 08:54:37 -07:00
|
|
|
//
|
2018-12-27 04:56:03 -08:00
|
|
|
// OpDefinitionsGen uses the description of operations to generate C++
|
|
|
|
// definitions for ops.
|
2018-10-15 08:54:37 -07:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-11-30 14:09:00 +00:00
|
|
|
#include "OpClass.h"
|
2020-01-30 11:30:23 -08:00
|
|
|
#include "OpFormatGen.h"
|
2021-04-06 12:53:27 -07:00
|
|
|
#include "OpGenHelpers.h"
|
2021-11-12 01:17:05 +00:00
|
|
|
#include "mlir/TableGen/Class.h"
|
2020-09-26 02:18:54 +00:00
|
|
|
#include "mlir/TableGen/CodeGenHelpers.h"
|
2019-04-12 06:05:49 -07:00
|
|
|
#include "mlir/TableGen/Format.h"
|
2018-12-27 04:56:03 -08:00
|
|
|
#include "mlir/TableGen/GenInfo.h"
|
2020-06-30 15:42:52 -07:00
|
|
|
#include "mlir/TableGen/Interfaces.h"
|
2019-01-03 15:53:54 -08:00
|
|
|
#include "mlir/TableGen/Operator.h"
|
2020-03-06 13:55:36 -08:00
|
|
|
#include "mlir/TableGen/SideEffects.h"
|
2021-04-15 11:29:23 -07:00
|
|
|
#include "mlir/TableGen/Trait.h"
|
2021-06-22 19:13:35 +00:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2020-03-05 12:41:56 -08:00
|
|
|
#include "llvm/ADT/Sequence.h"
|
2018-12-12 03:09:11 -08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2022-04-08 16:41:31 +00:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2021-10-14 15:58:44 -07:00
|
|
|
#include "llvm/Support/Debug.h"
|
2018-10-15 08:54:37 -07:00
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
#include "llvm/TableGen/TableGenBackend.h"
|
|
|
|
|
2019-12-02 09:33:24 -08:00
|
|
|
#define DEBUG_TYPE "mlir-tblgen-opdefgen"
|
|
|
|
|
2020-01-17 07:39:45 -05:00
|
|
|
using namespace llvm;
|
2019-01-03 15:53:54 -08:00
|
|
|
using namespace mlir;
|
2019-04-12 06:05:49 -07:00
|
|
|
using namespace mlir::tblgen;
|
2019-01-08 17:19:37 -08:00
|
|
|
|
2019-04-04 04:29:58 -07:00
|
|
|
static const char *const tblgenNamePrefix = "tblgen_";
|
2020-01-08 18:48:38 -08:00
|
|
|
static const char *const generatedArgName = "odsArg";
|
2021-01-11 11:54:51 -08:00
|
|
|
static const char *const odsBuilder = "odsBuilder";
|
2020-01-08 18:48:38 -08:00
|
|
|
static const char *const builderOpState = "odsState";
|
2018-10-15 08:54:37 -07:00
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
/// The names of the implicit attributes that contain variadic operand and
|
|
|
|
/// result segment sizes.
|
|
|
|
static const char *const operandSegmentAttrName = "operand_segment_sizes";
|
|
|
|
static const char *const resultSegmentAttrName = "result_segment_sizes";
|
|
|
|
|
|
|
|
/// Code for an Op to lookup an attribute. Uses cached identifiers and subrange
|
|
|
|
/// lookup.
|
2021-11-30 14:09:00 +00:00
|
|
|
///
|
2022-04-08 16:41:31 +00:00
|
|
|
/// {0}: Code snippet to get the attribute's name or identifier.
|
|
|
|
/// {1}: The lower bound on the sorted subrange.
|
|
|
|
/// {2}: The upper bound on the sorted subrange.
|
|
|
|
/// {3}: Code snippet to get the array of named attributes.
|
|
|
|
/// {4}: "Named" to get the named attribute.
|
|
|
|
static const char *const subrangeGetAttr =
|
|
|
|
"::mlir::impl::get{4}AttrFromSortedRange({3}.begin() + {1}, {3}.end() - "
|
|
|
|
"{2}, {0})";
|
2021-11-03 17:06:14 +00:00
|
|
|
|
2021-11-30 14:09:00 +00:00
|
|
|
/// The logic to calculate the actual value range for a declared operand/result
|
|
|
|
/// of an op with variadic operands/results. Note that this logic is not for
|
|
|
|
/// general use; it assumes all variadic operands/results must have the same
|
|
|
|
/// number of values.
|
|
|
|
///
|
|
|
|
/// {0}: The list of whether each declared operand/result is variadic.
|
|
|
|
/// {1}: The total number of non-variadic operands/results.
|
|
|
|
/// {2}: The total number of variadic operands/results.
|
|
|
|
/// {3}: The total number of actual values.
|
|
|
|
/// {4}: "operand" or "result".
|
2021-11-09 18:59:06 +00:00
|
|
|
static const char *const sameVariadicSizeValueRangeCalcCode = R"(
|
2019-06-09 05:50:09 -07:00
|
|
|
bool isVariadic[] = {{{0}};
|
|
|
|
int prevVariadicCount = 0;
|
2019-06-10 11:08:52 -07:00
|
|
|
for (unsigned i = 0; i < index; ++i)
|
2019-06-09 05:50:09 -07:00
|
|
|
if (isVariadic[i]) ++prevVariadicCount;
|
|
|
|
|
2020-04-29 16:09:11 -07:00
|
|
|
// Calculate how many dynamic values a static variadic {4} corresponds to.
|
|
|
|
// This assumes all static variadic {4}s have the same dynamic value count.
|
2019-06-09 05:50:09 -07:00
|
|
|
int variadicSize = ({3} - {1}) / {2};
|
|
|
|
// `index` passed in as the parameter is the static index which counts each
|
2020-04-29 16:09:11 -07:00
|
|
|
// {4} (variadic or not) as size 1. So here for each previous static variadic
|
|
|
|
// {4}, we need to offset by (variadicSize - 1) to get where the dynamic
|
|
|
|
// value pack for this static {4} starts.
|
|
|
|
int start = index + (variadicSize - 1) * prevVariadicCount;
|
2019-06-09 05:50:09 -07:00
|
|
|
int size = isVariadic[index] ? variadicSize : 1;
|
2020-04-29 16:09:11 -07:00
|
|
|
return {{start, size};
|
2019-06-09 05:50:09 -07:00
|
|
|
)";
|
|
|
|
|
2021-11-30 14:09:00 +00:00
|
|
|
/// The logic to calculate the actual value range for a declared operand/result
|
|
|
|
/// of an op with variadic operands/results. Note that this logic is assumes
|
|
|
|
/// the op has an attribute specifying the size of each operand/result segment
|
|
|
|
/// (variadic or not).
|
2021-11-09 18:59:06 +00:00
|
|
|
static const char *const attrSizedSegmentValueRangeCalcCode = R"(
|
2019-11-25 17:26:16 -08:00
|
|
|
unsigned start = 0;
|
|
|
|
for (unsigned i = 0; i < index; ++i)
|
2022-08-12 15:43:03 -04:00
|
|
|
start += sizeAttr[i];
|
|
|
|
return {start, sizeAttr[index]};
|
2020-04-29 16:09:11 -07:00
|
|
|
)";
|
2022-04-08 16:41:31 +00:00
|
|
|
/// The code snippet to initialize the sizes for the value range calculation.
|
|
|
|
///
|
|
|
|
/// {0}: The code to get the attribute.
|
|
|
|
static const char *const adapterSegmentSizeAttrInitCode = R"(
|
|
|
|
assert(odsAttrs && "missing segment size attribute for op");
|
2022-08-12 15:43:03 -04:00
|
|
|
auto sizeAttr = {0}.cast<::mlir::DenseI32ArrayAttr>();
|
2022-04-08 16:41:31 +00:00
|
|
|
)";
|
|
|
|
/// The code snippet to initialize the sizes for the value range calculation.
|
|
|
|
///
|
|
|
|
/// {0}: The code to get the attribute.
|
|
|
|
static const char *const opSegmentSizeAttrInitCode = R"(
|
2022-08-12 15:43:03 -04:00
|
|
|
auto sizeAttr = {0}.cast<::mlir::DenseI32ArrayAttr>();
|
2022-04-08 16:41:31 +00:00
|
|
|
)";
|
2021-11-30 14:09:00 +00:00
|
|
|
|
|
|
|
/// The logic to calculate the actual value range for a declared operand
|
|
|
|
/// of an op with variadic of variadic operands within the OpAdaptor.
|
|
|
|
///
|
|
|
|
/// {0}: The name of the segment attribute.
|
|
|
|
/// {1}: The index of the main operand.
|
2021-11-09 18:59:06 +00:00
|
|
|
static const char *const variadicOfVariadicAdaptorCalcCode = R"(
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
auto tblgenTmpOperands = getODSOperands({1});
|
2022-08-12 15:43:03 -04:00
|
|
|
auto sizes = {0}();
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
|
|
|
|
::llvm::SmallVector<::mlir::ValueRange> tblgenTmpOperandGroups;
|
2022-08-12 15:43:03 -04:00
|
|
|
for (int i = 0, e = sizes.size(); i < e; ++i) {{
|
|
|
|
tblgenTmpOperandGroups.push_back(tblgenTmpOperands.take_front(sizes[i]));
|
|
|
|
tblgenTmpOperands = tblgenTmpOperands.drop_front(sizes[i]);
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
}
|
|
|
|
return tblgenTmpOperandGroups;
|
|
|
|
)";
|
2020-04-29 16:09:11 -07:00
|
|
|
|
2021-11-30 14:09:00 +00:00
|
|
|
/// The logic to build a range of either operand or result values.
|
|
|
|
///
|
|
|
|
/// {0}: The begin iterator of the actual values.
|
|
|
|
/// {1}: The call to generate the start and length of the value range.
|
2021-11-09 18:59:06 +00:00
|
|
|
static const char *const valueRangeReturnCode = R"(
|
2020-04-29 16:09:11 -07:00
|
|
|
auto valueRange = {1};
|
|
|
|
return {{std::next({0}, valueRange.first),
|
|
|
|
std::next({0}, valueRange.first + valueRange.second)};
|
2019-11-25 17:26:16 -08:00
|
|
|
)";
|
|
|
|
|
2021-11-30 14:09:00 +00:00
|
|
|
/// A header for indicating code sections.
|
|
|
|
///
|
|
|
|
/// {0}: Some text, or a class name.
|
|
|
|
/// {1}: Some text.
|
2019-03-20 17:25:34 -07:00
|
|
|
static const char *const opCommentHeader = R"(
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// {0} {1}
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-01-07 09:52:26 -08:00
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
)";
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility structs and functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-10-15 08:54:37 -07:00
|
|
|
|
2020-01-17 16:39:59 -05:00
|
|
|
// Replaces all occurrences of `match` in `str` with `substitute`.
|
|
|
|
static std::string replaceAllSubstrs(std::string str, const std::string &match,
|
|
|
|
const std::string &substitute) {
|
|
|
|
std::string::size_type scanLoc = 0, matchLoc = std::string::npos;
|
|
|
|
while ((matchLoc = str.find(match, scanLoc)) != std::string::npos) {
|
|
|
|
str = str.replace(matchLoc, match.size(), substitute);
|
|
|
|
scanLoc = matchLoc + substitute.size();
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-12-05 13:25:44 -08:00
|
|
|
// Returns whether the record has a value of the given name that can be returned
|
|
|
|
// via getValueAsString.
|
|
|
|
static inline bool hasStringAttribute(const Record &record,
|
|
|
|
StringRef fieldName) {
|
2021-11-09 18:59:06 +00:00
|
|
|
auto *valueInit = record.getValueInit(fieldName);
|
2020-11-24 13:09:02 -05:00
|
|
|
return isa<StringInit>(valueInit);
|
2018-12-05 13:25:44 -08:00
|
|
|
}
|
|
|
|
|
2019-01-03 15:53:54 -08:00
|
|
|
static std::string getArgumentName(const Operator &op, int index) {
|
|
|
|
const auto &operand = op.getOperand(index);
|
2019-01-30 06:05:27 -08:00
|
|
|
if (!operand.name.empty())
|
2020-01-28 20:23:46 +01:00
|
|
|
return std::string(operand.name);
|
2021-11-03 17:06:14 +00:00
|
|
|
return std::string(formatv("{0}_{1}", generatedArgName, index));
|
2019-01-03 15:53:54 -08:00
|
|
|
}
|
|
|
|
|
2019-12-02 09:33:24 -08:00
|
|
|
// Returns true if we can use unwrapped value for the given `attr` in builders.
|
|
|
|
static bool canUseUnwrappedRawValue(const tblgen::Attribute &attr) {
|
|
|
|
return attr.getReturnType() != attr.getStorageType() &&
|
|
|
|
// We need to wrap the raw value into an attribute in the builder impl
|
|
|
|
// so we need to make sure that the attribute specifies how to do that.
|
|
|
|
!attr.getConstBuilderTemplate().empty();
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:59:06 +00:00
|
|
|
namespace {
|
2022-04-08 16:41:31 +00:00
|
|
|
/// Metadata on a registered attribute. Given that attributes are stored in
|
|
|
|
/// sorted order on operations, we can use information from ODS to deduce the
|
|
|
|
/// number of required attributes less and and greater than each attribute,
|
|
|
|
/// allowing us to search only a subrange of the attributes in ODS-generated
|
|
|
|
/// getters.
|
|
|
|
struct AttributeMetadata {
|
|
|
|
/// The attribute name.
|
|
|
|
StringRef attrName;
|
|
|
|
/// Whether the attribute is required.
|
|
|
|
bool isRequired;
|
|
|
|
/// The ODS attribute constraint. Not present for implicit attributes.
|
|
|
|
Optional<Attribute> constraint;
|
|
|
|
/// The number of required attributes less than this attribute.
|
|
|
|
unsigned lowerBound = 0;
|
|
|
|
/// The number of required attributes greater than this attribute.
|
|
|
|
unsigned upperBound = 0;
|
|
|
|
};
|
|
|
|
|
2021-11-09 18:59:06 +00:00
|
|
|
/// Helper class to select between OpAdaptor and Op code templates.
|
|
|
|
class OpOrAdaptorHelper {
|
|
|
|
public:
|
|
|
|
OpOrAdaptorHelper(const Operator &op, bool emitForOp)
|
2022-04-08 16:41:31 +00:00
|
|
|
: op(op), emitForOp(emitForOp) {
|
|
|
|
computeAttrMetadata();
|
|
|
|
}
|
2021-11-09 18:59:06 +00:00
|
|
|
|
|
|
|
/// Object that wraps a functor in a stream operator for interop with
|
|
|
|
/// llvm::formatv.
|
|
|
|
class Formatter {
|
|
|
|
public:
|
|
|
|
template <typename Functor>
|
|
|
|
Formatter(Functor &&func) : func(std::forward<Functor>(func)) {}
|
|
|
|
|
|
|
|
std::string str() const {
|
|
|
|
std::string result;
|
|
|
|
llvm::raw_string_ostream os(result);
|
|
|
|
os << *this;
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<raw_ostream &(raw_ostream &)> func;
|
|
|
|
|
|
|
|
friend raw_ostream &operator<<(raw_ostream &os, const Formatter &fmt) {
|
|
|
|
return fmt.func(os);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate code for getting an attribute.
|
2022-04-08 16:41:31 +00:00
|
|
|
Formatter getAttr(StringRef attrName, bool isNamed = false) const {
|
|
|
|
assert(attrMetadata.count(attrName) && "expected attribute metadata");
|
|
|
|
return [this, attrName, isNamed](raw_ostream &os) -> raw_ostream & {
|
|
|
|
const AttributeMetadata &attr = attrMetadata.find(attrName)->second;
|
|
|
|
return os << formatv(subrangeGetAttr, getAttrName(attrName),
|
|
|
|
attr.lowerBound, attr.upperBound, getAttrRange(),
|
|
|
|
isNamed ? "Named" : "");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate code for getting the name of an attribute.
|
|
|
|
Formatter getAttrName(StringRef attrName) const {
|
2021-11-09 18:59:06 +00:00
|
|
|
return [this, attrName](raw_ostream &os) -> raw_ostream & {
|
2022-04-08 16:41:31 +00:00
|
|
|
if (emitForOp)
|
|
|
|
return os << op.getGetterName(attrName) << "AttrName()";
|
|
|
|
return os << formatv("{0}::{1}AttrName(*odsOpName)", op.getCppClassName(),
|
|
|
|
op.getGetterName(attrName));
|
2021-11-09 18:59:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
// Get the code snippet for getting the named attribute range.
|
|
|
|
StringRef getAttrRange() const {
|
|
|
|
return emitForOp ? "(*this)->getAttrs()" : "odsAttrs";
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:59:06 +00:00
|
|
|
// Get the prefix code for emitting an error.
|
|
|
|
Formatter emitErrorPrefix() const {
|
|
|
|
return [this](raw_ostream &os) -> raw_ostream & {
|
|
|
|
if (emitForOp)
|
|
|
|
return os << "emitOpError(";
|
|
|
|
return os << formatv("emitError(loc, \"'{0}' op \"",
|
|
|
|
op.getOperationName());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the call to get an operand or segment of operands.
|
|
|
|
Formatter getOperand(unsigned index) const {
|
|
|
|
return [this, index](raw_ostream &os) -> raw_ostream & {
|
|
|
|
return os << formatv(op.getOperand(index).isVariadic()
|
|
|
|
? "this->getODSOperands({0})"
|
|
|
|
: "(*this->getODSOperands({0}).begin())",
|
|
|
|
index);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the call to get a result of segment of results.
|
|
|
|
Formatter getResult(unsigned index) const {
|
|
|
|
return [this, index](raw_ostream &os) -> raw_ostream & {
|
|
|
|
if (!emitForOp)
|
|
|
|
return os << "<no results should be generated>";
|
|
|
|
return os << formatv(op.getResult(index).isVariadic()
|
|
|
|
? "this->getODSResults({0})"
|
|
|
|
: "(*this->getODSResults({0}).begin())",
|
|
|
|
index);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return whether an op instance is available.
|
|
|
|
bool isEmittingForOp() const { return emitForOp; }
|
|
|
|
|
|
|
|
// Return the ODS operation wrapper.
|
|
|
|
const Operator &getOp() const { return op; }
|
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
// Get the attribute metadata sorted by name.
|
|
|
|
const llvm::MapVector<StringRef, AttributeMetadata> &getAttrMetadata() const {
|
|
|
|
return attrMetadata;
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:59:06 +00:00
|
|
|
private:
|
2022-04-08 16:41:31 +00:00
|
|
|
// Compute the attribute metadata.
|
|
|
|
void computeAttrMetadata();
|
|
|
|
|
2021-11-09 18:59:06 +00:00
|
|
|
// The operation ODS wrapper.
|
|
|
|
const Operator &op;
|
|
|
|
// True if code is being generate for an op. False for an adaptor.
|
|
|
|
const bool emitForOp;
|
2022-04-08 16:41:31 +00:00
|
|
|
|
|
|
|
// The attribute metadata, mapped by name.
|
|
|
|
llvm::MapVector<StringRef, AttributeMetadata> attrMetadata;
|
|
|
|
// The number of required attributes.
|
|
|
|
unsigned numRequired;
|
2021-11-09 18:59:06 +00:00
|
|
|
};
|
2022-04-08 16:41:31 +00:00
|
|
|
|
2021-12-07 18:27:58 +00:00
|
|
|
} // namespace
|
2021-11-09 18:59:06 +00:00
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
void OpOrAdaptorHelper::computeAttrMetadata() {
|
|
|
|
// Enumerate the attribute names of this op, ensuring the attribute names are
|
|
|
|
// unique in case implicit attributes are explicitly registered.
|
|
|
|
for (const NamedAttribute &namedAttr : op.getAttributes()) {
|
|
|
|
Attribute attr = namedAttr.attr;
|
|
|
|
bool isOptional =
|
|
|
|
attr.hasDefaultValue() || attr.isOptional() || attr.isDerivedAttr();
|
|
|
|
attrMetadata.insert(
|
|
|
|
{namedAttr.name, AttributeMetadata{namedAttr.name, !isOptional, attr}});
|
|
|
|
}
|
|
|
|
// Include key attributes from several traits as implicitly registered.
|
|
|
|
if (op.getTrait("::mlir::OpTrait::AttrSizedOperandSegments")) {
|
|
|
|
attrMetadata.insert(
|
|
|
|
{operandSegmentAttrName,
|
|
|
|
AttributeMetadata{operandSegmentAttrName, /*isRequired=*/true,
|
|
|
|
/*attr=*/llvm::None}});
|
|
|
|
}
|
|
|
|
if (op.getTrait("::mlir::OpTrait::AttrSizedResultSegments")) {
|
|
|
|
attrMetadata.insert(
|
|
|
|
{resultSegmentAttrName,
|
|
|
|
AttributeMetadata{resultSegmentAttrName, /*isRequired=*/true,
|
|
|
|
/*attr=*/llvm::None}});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the metadata in sorted order.
|
|
|
|
SmallVector<AttributeMetadata> sortedAttrMetadata =
|
|
|
|
llvm::to_vector(llvm::make_second_range(attrMetadata.takeVector()));
|
|
|
|
llvm::sort(sortedAttrMetadata,
|
|
|
|
[](const AttributeMetadata &lhs, const AttributeMetadata &rhs) {
|
|
|
|
return lhs.attrName < rhs.attrName;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Compute the subrange bounds for each attribute.
|
|
|
|
numRequired = 0;
|
|
|
|
for (AttributeMetadata &attr : sortedAttrMetadata) {
|
|
|
|
attr.lowerBound = numRequired;
|
|
|
|
numRequired += attr.isRequired;
|
|
|
|
};
|
|
|
|
for (AttributeMetadata &attr : sortedAttrMetadata)
|
|
|
|
attr.upperBound = numRequired - attr.lowerBound - attr.isRequired;
|
|
|
|
|
|
|
|
// Store the results back into the map.
|
|
|
|
for (const AttributeMetadata &attr : sortedAttrMetadata)
|
|
|
|
attrMetadata.insert({attr.attrName, attr});
|
|
|
|
}
|
|
|
|
|
2020-01-10 11:18:08 -05:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Op emitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
namespace {
|
|
|
|
// Helper class to emit a record into the given output stream.
|
|
|
|
class OpEmitter {
|
|
|
|
public:
|
2020-12-14 14:14:22 -08:00
|
|
|
static void
|
|
|
|
emitDecl(const Operator &op, raw_ostream &os,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter);
|
|
|
|
static void
|
|
|
|
emitDef(const Operator &op, raw_ostream &os,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter);
|
2018-10-15 08:54:37 -07:00
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
private:
|
2020-12-14 14:14:22 -08:00
|
|
|
OpEmitter(const Operator &op,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter);
|
2019-03-20 17:25:34 -07:00
|
|
|
|
|
|
|
void emitDecl(raw_ostream &os);
|
|
|
|
void emitDef(raw_ostream &os);
|
|
|
|
|
2021-06-22 19:13:35 +00:00
|
|
|
// Generate methods for accessing the attribute names of this operation.
|
|
|
|
void genAttrNameGetters();
|
|
|
|
|
2019-11-21 14:34:03 -08:00
|
|
|
// Generates the OpAsmOpInterface for this operation if possible.
|
|
|
|
void genOpAsmInterface();
|
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
// Generates the `getOperationName` method for this op.
|
|
|
|
void genOpNameGetter();
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
// Generates getters for the attributes.
|
|
|
|
void genAttrGetters();
|
|
|
|
|
2020-02-19 11:39:28 -05:00
|
|
|
// Generates setter for the attributes.
|
|
|
|
void genAttrSetters();
|
|
|
|
|
2021-01-05 18:44:26 +00:00
|
|
|
// Generates removers for optional attributes.
|
|
|
|
void genOptionalAttrRemovers();
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
// Generates getters for named operands.
|
|
|
|
void genNamedOperandGetters();
|
|
|
|
|
2020-04-29 16:09:11 -07:00
|
|
|
// Generates setters for named operands.
|
|
|
|
void genNamedOperandSetters();
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
// Generates getters for named results.
|
|
|
|
void genNamedResultGetters();
|
|
|
|
|
2019-07-05 02:20:38 -07:00
|
|
|
// Generates getters for named regions.
|
|
|
|
void genNamedRegionGetters();
|
|
|
|
|
2020-02-21 13:19:50 -08:00
|
|
|
// Generates getters for named successors.
|
|
|
|
void genNamedSuccessorGetters();
|
|
|
|
|
2022-07-08 11:31:12 -07:00
|
|
|
// Generates the method to populate default attributes.
|
|
|
|
void genPopulateDefaultAttributes();
|
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
// Generates builder methods for the operation.
|
2019-03-20 17:25:34 -07:00
|
|
|
void genBuilder();
|
|
|
|
|
2019-12-12 10:35:40 -08:00
|
|
|
// Generates the build() method that takes each operand/attribute
|
|
|
|
// as a stand-alone parameter.
|
|
|
|
void genSeparateArgParamBuilder();
|
2019-08-21 05:35:07 -07:00
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
// Generates the build() method that takes each operand/attribute as a
|
2019-12-02 09:33:24 -08:00
|
|
|
// stand-alone parameter. The generated build() method uses first operand's
|
|
|
|
// type as all results' types.
|
2019-11-15 07:33:21 -08:00
|
|
|
void genUseOperandAsResultTypeSeparateParamBuilder();
|
|
|
|
|
|
|
|
// Generates the build() method that takes all operands/attributes
|
2019-12-02 09:33:24 -08:00
|
|
|
// collectively as one parameter. The generated build() method uses first
|
|
|
|
// operand's type as all results' types.
|
2019-11-15 07:33:21 -08:00
|
|
|
void genUseOperandAsResultTypeCollectiveParamBuilder();
|
2019-07-31 15:30:46 -07:00
|
|
|
|
2019-12-06 10:52:38 -08:00
|
|
|
// Generates the build() method that takes aggregate operands/attributes
|
|
|
|
// parameters. This build() method uses inferred types as result types.
|
|
|
|
// Requires: The type needs to be inferable via InferTypeOpInterface.
|
2020-02-28 10:59:34 -08:00
|
|
|
void genInferredTypeCollectiveParamBuilder();
|
2019-12-06 10:52:38 -08:00
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
// Generates the build() method that takes each operand/attribute as a
|
2019-12-02 09:33:24 -08:00
|
|
|
// stand-alone parameter. The generated build() method uses first attribute's
|
|
|
|
// type as all result's types.
|
2019-07-31 15:30:46 -07:00
|
|
|
void genUseAttrAsResultTypeBuilder();
|
|
|
|
|
|
|
|
// Generates the build() method that takes all result types collectively as
|
|
|
|
// one parameter. Similarly for operands and attributes.
|
|
|
|
void genCollectiveParamBuilder();
|
|
|
|
|
2019-12-02 09:33:24 -08:00
|
|
|
// The kind of parameter to generate for result types in builders.
|
|
|
|
enum class TypeParamKind {
|
|
|
|
None, // No result type in parameter list.
|
|
|
|
Separate, // A separate parameter for each result type.
|
|
|
|
Collective, // An ArrayRef<Type> for all result types.
|
|
|
|
};
|
|
|
|
|
|
|
|
// The kind of parameter to generate for attributes in builders.
|
|
|
|
enum class AttrParamKind {
|
|
|
|
WrappedAttr, // A wrapped MLIR Attribute instance.
|
|
|
|
UnwrappedValue, // A raw value without MLIR Attribute wrapper.
|
|
|
|
};
|
2019-08-21 05:35:07 -07:00
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
// Builds the parameter list for build() method of this op. This method writes
|
2019-12-02 09:33:24 -08:00
|
|
|
// to `paramList` the comma-separated parameter list and updates
|
|
|
|
// `resultTypeNames` with the names for parameters for specifying result
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
// types. `inferredAttributes` is populated with any attributes that are
|
|
|
|
// elided from the build list. The given `typeParamKind` and `attrParamKind`
|
|
|
|
// controls how result types and attributes are placed in the parameter list.
|
2021-11-12 01:17:05 +00:00
|
|
|
void buildParamList(SmallVectorImpl<MethodParameter> ¶mList,
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
llvm::StringSet<> &inferredAttributes,
|
2019-07-31 15:30:46 -07:00
|
|
|
SmallVectorImpl<std::string> &resultTypeNames,
|
2019-12-02 09:33:24 -08:00
|
|
|
TypeParamKind typeParamKind,
|
|
|
|
AttrParamKind attrParamKind = AttrParamKind::WrappedAttr);
|
2019-07-31 15:30:46 -07:00
|
|
|
|
|
|
|
// Adds op arguments and regions into operation state for build() methods.
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
void
|
2021-11-12 01:17:05 +00:00
|
|
|
genCodeForAddingArgAndRegionForBuilder(MethodBody &body,
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
llvm::StringSet<> &inferredAttributes,
|
|
|
|
bool isRawValueAttr = false);
|
2019-07-31 15:30:46 -07:00
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
// Generates canonicalizer declaration for the operation.
|
|
|
|
void genCanonicalizerDecls();
|
|
|
|
|
|
|
|
// Generates the folder declaration for the operation.
|
|
|
|
void genFolderDecls();
|
|
|
|
|
|
|
|
// Generates the parser for the operation.
|
|
|
|
void genParser();
|
|
|
|
|
|
|
|
// Generates the printer for the operation.
|
|
|
|
void genPrinter();
|
|
|
|
|
|
|
|
// Generates verify method for the operation.
|
|
|
|
void genVerifier();
|
|
|
|
|
2022-02-25 18:17:30 +00:00
|
|
|
// Generates custom verify methods for the operation.
|
|
|
|
void genCustomVerifier();
|
|
|
|
|
2019-06-09 07:00:09 -07:00
|
|
|
// Generates verify statements for operands and results in the operation.
|
|
|
|
// The generated code will be attached to `body`.
|
2022-01-18 06:47:33 +00:00
|
|
|
void genOperandResultVerifier(MethodBody &body,
|
|
|
|
Operator::const_value_range values,
|
2019-06-09 07:00:09 -07:00
|
|
|
StringRef valueKind);
|
|
|
|
|
2019-05-30 16:50:16 -07:00
|
|
|
// Generates verify statements for regions in the operation.
|
|
|
|
// The generated code will be attached to `body`.
|
2021-11-12 01:17:05 +00:00
|
|
|
void genRegionVerifier(MethodBody &body);
|
2019-05-30 16:50:16 -07:00
|
|
|
|
2020-02-21 13:19:50 -08:00
|
|
|
// Generates verify statements for successors in the operation.
|
|
|
|
// The generated code will be attached to `body`.
|
2021-11-12 01:17:05 +00:00
|
|
|
void genSuccessorVerifier(MethodBody &body);
|
2020-02-21 13:19:50 -08:00
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
// Generates the traits used by the object.
|
|
|
|
void genTraits();
|
|
|
|
|
2020-12-03 16:18:11 -08:00
|
|
|
// Generate the OpInterface methods for all interfaces.
|
2019-09-30 12:42:31 -07:00
|
|
|
void genOpInterfaceMethods();
|
|
|
|
|
2020-12-03 16:18:11 -08:00
|
|
|
// Generate op interface methods for the given interface.
|
2021-04-15 11:29:23 -07:00
|
|
|
void genOpInterfaceMethods(const tblgen::InterfaceTrait *trait);
|
2020-12-03 16:18:11 -08:00
|
|
|
|
|
|
|
// Generate op interface method for the given interface method. If
|
|
|
|
// 'declaration' is true, generates a declaration, else a definition.
|
2021-11-12 01:17:05 +00:00
|
|
|
Method *genOpInterfaceMethod(const tblgen::InterfaceMethod &method,
|
|
|
|
bool declaration = true);
|
2020-05-27 08:45:55 -07:00
|
|
|
|
2020-03-06 13:55:36 -08:00
|
|
|
// Generate the side effect interface methods.
|
|
|
|
void genSideEffectInterfaceMethods();
|
|
|
|
|
2020-05-27 08:45:55 -07:00
|
|
|
// Generate the type inference interface methods.
|
|
|
|
void genTypeInterfaceMethods();
|
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
private:
|
2019-03-20 17:25:34 -07:00
|
|
|
// The TableGen record for this op.
|
2020-07-07 01:35:23 -07:00
|
|
|
// TODO: OpEmitter should not have a Record directly,
|
2019-06-03 08:03:20 -07:00
|
|
|
// it should rather go through the Operator for better abstraction.
|
2019-03-20 17:25:34 -07:00
|
|
|
const Record &def;
|
|
|
|
|
|
|
|
// The wrapper operator class for querying information from this op.
|
2022-04-08 16:41:31 +00:00
|
|
|
const Operator &op;
|
2019-03-20 17:25:34 -07:00
|
|
|
|
|
|
|
// The C++ code builder for this op
|
|
|
|
OpClass opClass;
|
2019-05-30 16:50:16 -07:00
|
|
|
|
|
|
|
// The format context for verification code generation.
|
|
|
|
FmtContext verifyCtx;
|
2020-12-14 14:14:22 -08:00
|
|
|
|
|
|
|
// The emitter containing all of the locally emitted verification functions.
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter;
|
2022-04-08 16:41:31 +00:00
|
|
|
|
|
|
|
// Helper for emitting op code.
|
|
|
|
OpOrAdaptorHelper emitHelper;
|
2019-03-20 17:25:34 -07:00
|
|
|
};
|
2021-10-21 21:41:15 +00:00
|
|
|
|
2021-12-07 18:27:58 +00:00
|
|
|
} // namespace
|
2019-03-20 17:25:34 -07:00
|
|
|
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
// Populate the format context `ctx` with substitutions of attributes, operands
|
|
|
|
// and results.
|
2021-11-09 18:59:06 +00:00
|
|
|
static void populateSubstitutions(const OpOrAdaptorHelper &emitHelper,
|
|
|
|
FmtContext &ctx) {
|
|
|
|
// Populate substitutions for attributes.
|
|
|
|
auto &op = emitHelper.getOp();
|
|
|
|
for (const auto &namedAttr : op.getAttributes())
|
[mlir] Remove types from attributes
This patch removes the `type` field from `Attribute` along with the
`Attribute::getType` accessor.
Going forward, this means that attributes in MLIR will no longer have
types as a first-class concept. This patch lays the groundwork to
incrementally remove or refactor code that relies on generic attributes
being typed. The immediate impact will be on attributes that rely on
`Attribute` containing a type, such as `IntegerAttr`,
`DenseElementsAttr`, and `ml_program::ExternAttr`, which will now need
to define a type parameter on their storage classes. This will save
memory as all other attribute kinds will no longer contain a type.
Moreover, it will not be possible to generically query the type of an
attribute directly. This patch provides an attribute interface
`TypedAttr` that implements only one method, `getType`, which can be
used to generically query the types of attributes that implement the
interface. This interface can be used to retain the concept of a "typed
attribute". The ODS-generated accessor for a `type` parameter
automatically implements this method.
Next steps will be to refactor the assembly formats of certain operations
that rely on `parseAttribute(type)` and `printAttributeWithoutType` to
remove special handling of type elision until `type` can be removed from
the dialect parsing hook entirely; and incrementally remove uses of
`TypedAttr`.
Reviewed By: lattner, rriddle, jpienaar
Differential Revision: https://reviews.llvm.org/D130092
2022-07-18 21:32:38 -07:00
|
|
|
ctx.addSubst(namedAttr.name,
|
|
|
|
emitHelper.getOp().getGetterName(namedAttr.name) + "()");
|
2021-11-09 18:59:06 +00:00
|
|
|
|
|
|
|
// Populate substitutions for named operands.
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
for (int i = 0, e = op.getNumOperands(); i < e; ++i) {
|
|
|
|
auto &value = op.getOperand(i);
|
2021-11-09 18:59:06 +00:00
|
|
|
if (!value.name.empty())
|
|
|
|
ctx.addSubst(value.name, emitHelper.getOperand(i).str());
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate substitutions for results.
|
|
|
|
for (int i = 0, e = op.getNumResults(); i < e; ++i) {
|
|
|
|
auto &value = op.getResult(i);
|
2021-11-09 18:59:06 +00:00
|
|
|
if (!value.name.empty())
|
|
|
|
ctx.addSubst(value.name, emitHelper.getResult(i).str());
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
/// Generate verification on native traits requiring attributes.
|
|
|
|
static void genNativeTraitAttrVerifier(MethodBody &body,
|
|
|
|
const OpOrAdaptorHelper &emitHelper) {
|
|
|
|
// Check that the variadic segment sizes attribute exists and contains the
|
|
|
|
// expected number of elements.
|
|
|
|
//
|
|
|
|
// {0}: Attribute name.
|
|
|
|
// {1}: Expected number of elements.
|
|
|
|
// {2}: "operand" or "result".
|
|
|
|
// {3}: Emit error prefix.
|
|
|
|
const char *const checkAttrSizedValueSegmentsCode = R"(
|
|
|
|
{
|
2022-08-12 15:43:03 -04:00
|
|
|
auto sizeAttr = tblgen_{0}.cast<::mlir::DenseI32ArrayAttr>();
|
|
|
|
auto numElements = sizeAttr.asArrayRef().size();
|
2022-04-08 16:41:31 +00:00
|
|
|
if (numElements != {1})
|
|
|
|
return {3}"'{0}' attribute for specifying {2} segments must have {1} "
|
|
|
|
"elements, but got ") << numElements;
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
// Verify a few traits first so that we can use getODSOperands() and
|
|
|
|
// getODSResults() in the rest of the verifier.
|
|
|
|
auto &op = emitHelper.getOp();
|
|
|
|
if (op.getTrait("::mlir::OpTrait::AttrSizedOperandSegments")) {
|
|
|
|
body << formatv(checkAttrSizedValueSegmentsCode, operandSegmentAttrName,
|
|
|
|
op.getNumOperands(), "operand",
|
|
|
|
emitHelper.emitErrorPrefix());
|
|
|
|
}
|
|
|
|
if (op.getTrait("::mlir::OpTrait::AttrSizedResultSegments")) {
|
|
|
|
body << formatv(checkAttrSizedValueSegmentsCode, resultSegmentAttrName,
|
|
|
|
op.getNumResults(), "result", emitHelper.emitErrorPrefix());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:59:06 +00:00
|
|
|
// Generate attribute verification. If an op instance is not available, then
|
|
|
|
// attribute checks that require one will not be emitted.
|
2022-04-08 16:41:31 +00:00
|
|
|
//
|
|
|
|
// Attribute verification is performed as follows:
|
|
|
|
//
|
|
|
|
// 1. Verify that all required attributes are present in sorted order. This
|
|
|
|
// ensures that we can use subrange lookup even with potentially missing
|
|
|
|
// attributes.
|
|
|
|
// 2. Verify native trait attributes so that other attributes may call methods
|
|
|
|
// that depend on the validity of these attributes, e.g. segment size attributes
|
|
|
|
// and operand or result getters.
|
|
|
|
// 3. Verify the constraints on all present attributes.
|
2021-11-11 22:08:54 +00:00
|
|
|
static void genAttributeVerifier(
|
2021-11-12 01:17:05 +00:00
|
|
|
const OpOrAdaptorHelper &emitHelper, FmtContext &ctx, MethodBody &body,
|
2021-11-11 22:08:54 +00:00
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter) {
|
2022-04-08 16:41:31 +00:00
|
|
|
if (emitHelper.getAttrMetadata().empty())
|
|
|
|
return;
|
|
|
|
|
2021-11-11 22:08:54 +00:00
|
|
|
// Verify the attribute if it is present. This assumes that default values
|
|
|
|
// are valid. This code snippet pastes the condition inline.
|
|
|
|
//
|
2021-11-03 17:06:14 +00:00
|
|
|
// TODO: verify the default value is valid (perhaps in debug mode only).
|
|
|
|
//
|
|
|
|
// {0}: Attribute variable name.
|
|
|
|
// {1}: Attribute condition code.
|
|
|
|
// {2}: Emit error prefix.
|
2021-11-11 22:08:54 +00:00
|
|
|
// {3}: Attribute name.
|
|
|
|
// {4}: Attribute/constraint description.
|
|
|
|
const char *const verifyAttrInline = R"(
|
2022-04-08 16:41:31 +00:00
|
|
|
if ({0} && !({1}))
|
|
|
|
return {2}"attribute '{3}' failed to satisfy constraint: {4}");
|
2021-11-11 22:08:54 +00:00
|
|
|
)";
|
|
|
|
// Verify the attribute using a uniqued constraint. Can only be used within
|
|
|
|
// the context of an op.
|
|
|
|
//
|
|
|
|
// {0}: Unique constraint name.
|
|
|
|
// {1}: Attribute variable name.
|
|
|
|
// {2}: Attribute name.
|
|
|
|
const char *const verifyAttrUnique = R"(
|
2022-04-08 16:41:31 +00:00
|
|
|
if (::mlir::failed({0}(*this, {1}, "{2}")))
|
|
|
|
return ::mlir::failure();
|
2021-11-11 22:08:54 +00:00
|
|
|
)";
|
2021-11-03 17:06:14 +00:00
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
// Traverse the array until the required attribute is found. Return an error
|
|
|
|
// if the traversal reached the end.
|
|
|
|
//
|
|
|
|
// {0}: Code to get the name of the attribute.
|
|
|
|
// {1}: The emit error prefix.
|
|
|
|
// {2}: The name of the attribute.
|
|
|
|
const char *const findRequiredAttr = R"(while (true) {{
|
|
|
|
if (namedAttrIt == namedAttrRange.end())
|
|
|
|
return {1}"requires attribute '{2}'");
|
|
|
|
if (namedAttrIt->getName() == {0}) {{
|
|
|
|
tblgen_{2} = namedAttrIt->getValue();
|
|
|
|
break;
|
|
|
|
})";
|
|
|
|
|
|
|
|
// Emit a check to see if the iteration has encountered an optional attribute.
|
|
|
|
//
|
|
|
|
// {0}: Code to get the name of the attribute.
|
|
|
|
// {1}: The name of the attribute.
|
|
|
|
const char *const checkOptionalAttr = R"(
|
|
|
|
else if (namedAttrIt->getName() == {0}) {{
|
|
|
|
tblgen_{1} = namedAttrIt->getValue();
|
|
|
|
})";
|
|
|
|
|
|
|
|
// Emit the start of the loop for checking trailing attributes.
|
|
|
|
const char *const checkTrailingAttrs = R"(while (true) {
|
|
|
|
if (namedAttrIt == namedAttrRange.end()) {
|
|
|
|
break;
|
|
|
|
})";
|
|
|
|
|
|
|
|
// Return true if a verifier can be emitted for the attribute: it is not a
|
|
|
|
// derived attribute, it has a predicate, its condition is not empty, and, for
|
|
|
|
// adaptors, the condition does not reference the op.
|
|
|
|
const auto canEmitVerifier = [&](Attribute attr) {
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
if (attr.isDerivedAttr())
|
2022-04-08 16:41:31 +00:00
|
|
|
return false;
|
|
|
|
Pred pred = attr.getPredicate();
|
|
|
|
if (pred.isNull())
|
|
|
|
return false;
|
|
|
|
std::string condition = pred.getCondition();
|
|
|
|
return !condition.empty() && (!StringRef(condition).contains("$_op") ||
|
|
|
|
emitHelper.isEmittingForOp());
|
|
|
|
};
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
// Emit the verifier for the attribute.
|
|
|
|
const auto emitVerifier = [&](Attribute attr, StringRef attrName,
|
|
|
|
StringRef varName) {
|
|
|
|
std::string condition = attr.getPredicate().getCondition();
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
Optional<StringRef> constraintFn;
|
|
|
|
if (emitHelper.isEmittingForOp() &&
|
|
|
|
(constraintFn = staticVerifierEmitter.getAttrConstraintFn(attr))) {
|
|
|
|
body << formatv(verifyAttrUnique, *constraintFn, varName, attrName);
|
|
|
|
} else {
|
|
|
|
body << formatv(verifyAttrInline, varName,
|
|
|
|
tgfmt(condition, &ctx.withSelf(varName)),
|
|
|
|
emitHelper.emitErrorPrefix(), attrName,
|
|
|
|
escapeString(attr.getSummary()));
|
|
|
|
}
|
|
|
|
};
|
2021-11-03 17:06:14 +00:00
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
// Prefix variables with `tblgen_` to avoid hiding the attribute accessor.
|
|
|
|
const auto getVarName = [&](StringRef attrName) {
|
|
|
|
return (tblgenNamePrefix + attrName).str();
|
|
|
|
};
|
|
|
|
|
|
|
|
body.indent() << formatv("auto namedAttrRange = {0};\n",
|
|
|
|
emitHelper.getAttrRange());
|
|
|
|
body << "auto namedAttrIt = namedAttrRange.begin();\n";
|
|
|
|
|
|
|
|
// Iterate over the attributes in sorted order. Keep track of the optional
|
|
|
|
// attributes that may be encountered along the way.
|
|
|
|
SmallVector<const AttributeMetadata *> optionalAttrs;
|
|
|
|
for (const std::pair<StringRef, AttributeMetadata> &it :
|
|
|
|
emitHelper.getAttrMetadata()) {
|
|
|
|
const AttributeMetadata &metadata = it.second;
|
|
|
|
if (!metadata.isRequired) {
|
|
|
|
optionalAttrs.push_back(&metadata);
|
|
|
|
continue;
|
2021-11-09 18:59:06 +00:00
|
|
|
}
|
2022-04-08 16:41:31 +00:00
|
|
|
|
|
|
|
body << formatv("::mlir::Attribute {0};\n", getVarName(it.first));
|
|
|
|
for (const AttributeMetadata *optional : optionalAttrs) {
|
|
|
|
body << formatv("::mlir::Attribute {0};\n",
|
|
|
|
getVarName(optional->attrName));
|
|
|
|
}
|
|
|
|
body << formatv(findRequiredAttr, emitHelper.getAttrName(it.first),
|
|
|
|
emitHelper.emitErrorPrefix(), it.first);
|
|
|
|
for (const AttributeMetadata *optional : optionalAttrs) {
|
|
|
|
body << formatv(checkOptionalAttr,
|
|
|
|
emitHelper.getAttrName(optional->attrName),
|
|
|
|
optional->attrName);
|
|
|
|
}
|
|
|
|
body << "\n ++namedAttrIt;\n}\n";
|
|
|
|
optionalAttrs.clear();
|
|
|
|
}
|
|
|
|
// Get trailing optional attributes.
|
|
|
|
if (!optionalAttrs.empty()) {
|
|
|
|
for (const AttributeMetadata *optional : optionalAttrs) {
|
|
|
|
body << formatv("::mlir::Attribute {0};\n",
|
|
|
|
getVarName(optional->attrName));
|
|
|
|
}
|
|
|
|
body << checkTrailingAttrs;
|
|
|
|
for (const AttributeMetadata *optional : optionalAttrs) {
|
|
|
|
body << formatv(checkOptionalAttr,
|
|
|
|
emitHelper.getAttrName(optional->attrName),
|
|
|
|
optional->attrName);
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
}
|
2022-04-08 16:41:31 +00:00
|
|
|
body << "\n ++namedAttrIt;\n}\n";
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
}
|
2022-04-08 16:41:31 +00:00
|
|
|
body.unindent();
|
|
|
|
|
|
|
|
// Emit the checks for segment attributes first so that the other constraints
|
|
|
|
// can call operand and result getters.
|
|
|
|
genNativeTraitAttrVerifier(body, emitHelper);
|
|
|
|
|
|
|
|
for (const auto &namedAttr : emitHelper.getOp().getAttributes())
|
|
|
|
if (canEmitVerifier(namedAttr.attr))
|
|
|
|
emitVerifier(namedAttr.attr, namedAttr.name, getVarName(namedAttr.name));
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
}
|
|
|
|
|
2022-01-06 01:42:12 +00:00
|
|
|
/// Op extra class definitions have a `$cppClass` substitution that is to be
|
|
|
|
/// replaced by the C++ class name.
|
|
|
|
static std::string formatExtraDefinitions(const Operator &op) {
|
|
|
|
FmtContext ctx = FmtContext().addSubst("cppClass", op.getCppClassName());
|
|
|
|
return tgfmt(op.getExtraClassDefinition(), &ctx).str();
|
|
|
|
}
|
|
|
|
|
2020-12-14 14:14:22 -08:00
|
|
|
OpEmitter::OpEmitter(const Operator &op,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter)
|
2019-06-03 08:03:20 -07:00
|
|
|
: def(op.getDef()), op(op),
|
2022-01-06 01:42:12 +00:00
|
|
|
opClass(op.getCppClassName(), op.getExtraClassDeclaration(),
|
|
|
|
formatExtraDefinitions(op)),
|
2022-04-08 16:41:31 +00:00
|
|
|
staticVerifierEmitter(staticVerifierEmitter),
|
|
|
|
emitHelper(op, /*emitForOp=*/true) {
|
2019-05-30 16:50:16 -07:00
|
|
|
verifyCtx.withOp("(*this->getOperation())");
|
2021-06-14 17:28:01 -07:00
|
|
|
verifyCtx.addSubst("_ctxt", "this->getOperation()->getContext()");
|
2019-05-30 16:50:16 -07:00
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
genTraits();
|
2020-05-27 08:45:55 -07:00
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
// Generate C++ code for various op methods. The order here determines the
|
|
|
|
// methods in the generated file.
|
2021-06-22 19:13:35 +00:00
|
|
|
genAttrNameGetters();
|
2019-11-21 14:34:03 -08:00
|
|
|
genOpAsmInterface();
|
2019-03-20 17:25:34 -07:00
|
|
|
genOpNameGetter();
|
|
|
|
genNamedOperandGetters();
|
2020-04-29 16:09:11 -07:00
|
|
|
genNamedOperandSetters();
|
2019-03-20 17:25:34 -07:00
|
|
|
genNamedResultGetters();
|
2019-07-05 02:20:38 -07:00
|
|
|
genNamedRegionGetters();
|
2020-02-21 13:19:50 -08:00
|
|
|
genNamedSuccessorGetters();
|
2019-03-20 17:25:34 -07:00
|
|
|
genAttrGetters();
|
2020-02-19 11:39:28 -05:00
|
|
|
genAttrSetters();
|
2021-01-05 18:44:26 +00:00
|
|
|
genOptionalAttrRemovers();
|
2019-03-20 17:25:34 -07:00
|
|
|
genBuilder();
|
2022-07-08 11:31:12 -07:00
|
|
|
genPopulateDefaultAttributes();
|
2019-03-20 17:25:34 -07:00
|
|
|
genParser();
|
|
|
|
genPrinter();
|
|
|
|
genVerifier();
|
2022-02-25 18:17:30 +00:00
|
|
|
genCustomVerifier();
|
2019-03-20 17:25:34 -07:00
|
|
|
genCanonicalizerDecls();
|
|
|
|
genFolderDecls();
|
2020-12-01 11:19:59 -08:00
|
|
|
genTypeInterfaceMethods();
|
2019-09-30 12:42:31 -07:00
|
|
|
genOpInterfaceMethods();
|
2020-01-30 11:30:23 -08:00
|
|
|
generateOpFormat(op, opClass);
|
2020-03-06 13:55:36 -08:00
|
|
|
genSideEffectInterfaceMethods();
|
2019-03-20 17:25:34 -07:00
|
|
|
}
|
2020-12-14 14:14:22 -08:00
|
|
|
void OpEmitter::emitDecl(
|
|
|
|
const Operator &op, raw_ostream &os,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter) {
|
|
|
|
OpEmitter(op, staticVerifierEmitter).emitDecl(os);
|
2019-03-20 17:25:34 -07:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:14:22 -08:00
|
|
|
void OpEmitter::emitDef(
|
|
|
|
const Operator &op, raw_ostream &os,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter) {
|
|
|
|
OpEmitter(op, staticVerifierEmitter).emitDef(os);
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
2021-11-30 14:09:00 +00:00
|
|
|
void OpEmitter::emitDecl(raw_ostream &os) {
|
|
|
|
opClass.finalize();
|
|
|
|
opClass.writeDeclTo(os);
|
|
|
|
}
|
2019-05-20 09:33:10 -07:00
|
|
|
|
2021-11-30 14:09:00 +00:00
|
|
|
void OpEmitter::emitDef(raw_ostream &os) {
|
|
|
|
opClass.finalize();
|
|
|
|
opClass.writeDefTo(os);
|
|
|
|
}
|
2019-03-20 17:25:34 -07:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
static void errorIfPruned(size_t line, Method *m, const Twine &methodName,
|
2021-10-14 15:58:44 -07:00
|
|
|
const Operator &op) {
|
|
|
|
if (m)
|
|
|
|
return;
|
|
|
|
PrintFatalError(op.getLoc(), "Unexpected overlap when generating `" +
|
|
|
|
methodName + "` for " +
|
|
|
|
op.getOperationName() + " (from line " +
|
|
|
|
Twine(line) + ")");
|
|
|
|
}
|
2022-04-08 16:41:31 +00:00
|
|
|
|
2021-10-14 15:58:44 -07:00
|
|
|
#define ERROR_IF_PRUNED(M, N, O) errorIfPruned(__LINE__, M, N, O)
|
|
|
|
|
2021-06-22 19:13:35 +00:00
|
|
|
void OpEmitter::genAttrNameGetters() {
|
2022-04-08 16:41:31 +00:00
|
|
|
const llvm::MapVector<StringRef, AttributeMetadata> &attributes =
|
|
|
|
emitHelper.getAttrMetadata();
|
2021-06-22 19:13:35 +00:00
|
|
|
|
|
|
|
// Emit the getAttributeNames method.
|
|
|
|
{
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *method = opClass.addStaticInlineMethod(
|
|
|
|
"::llvm::ArrayRef<::llvm::StringRef>", "getAttributeNames");
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, "getAttributeNames", op);
|
2021-06-22 19:13:35 +00:00
|
|
|
auto &body = method->body();
|
2022-04-08 16:41:31 +00:00
|
|
|
if (attributes.empty()) {
|
2021-06-22 19:13:35 +00:00
|
|
|
body << " return {};";
|
2022-04-08 16:41:31 +00:00
|
|
|
// Nothing else to do if there are no registered attributes. Exit early.
|
|
|
|
return;
|
2021-06-22 19:13:35 +00:00
|
|
|
}
|
2022-04-08 16:41:31 +00:00
|
|
|
body << " static ::llvm::StringRef attrNames[] = {";
|
|
|
|
llvm::interleaveComma(llvm::make_first_range(attributes), body,
|
|
|
|
[&](StringRef attrName) {
|
|
|
|
body << "::llvm::StringRef(\"" << attrName << "\")";
|
|
|
|
});
|
|
|
|
body << "};\n return ::llvm::makeArrayRef(attrNames);";
|
2021-06-22 19:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the getAttributeNameForIndex methods.
|
|
|
|
{
|
2021-11-30 14:09:00 +00:00
|
|
|
auto *method = opClass.addInlineMethod<Method::Private>(
|
2021-11-16 17:21:15 +00:00
|
|
|
"::mlir::StringAttr", "getAttributeNameForIndex",
|
2021-11-12 01:17:05 +00:00
|
|
|
MethodParameter("unsigned", "index"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, "getAttributeNameForIndex", op);
|
2021-06-22 19:13:35 +00:00
|
|
|
method->body()
|
|
|
|
<< " return getAttributeNameForIndex((*this)->getName(), index);";
|
|
|
|
}
|
|
|
|
{
|
2021-11-30 14:09:00 +00:00
|
|
|
auto *method = opClass.addStaticInlineMethod<Method::Private>(
|
2021-11-16 17:21:15 +00:00
|
|
|
"::mlir::StringAttr", "getAttributeNameForIndex",
|
2021-11-12 01:17:05 +00:00
|
|
|
MethodParameter("::mlir::OperationName", "name"),
|
|
|
|
MethodParameter("unsigned", "index"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, "getAttributeNameForIndex", op);
|
2021-11-30 14:09:00 +00:00
|
|
|
|
|
|
|
const char *const getAttrName = R"(
|
|
|
|
assert(index < {0} && "invalid attribute index");
|
|
|
|
return name.getRegisteredInfo()->getAttributeNames()[index];
|
|
|
|
)";
|
2022-04-08 16:41:31 +00:00
|
|
|
method->body() << formatv(getAttrName, attributes.size());
|
2021-06-22 19:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the <attr>AttrName methods, that expose the attribute names to
|
|
|
|
// users.
|
|
|
|
const char *attrNameMethodBody = " return getAttributeNameForIndex({0});";
|
2022-04-08 16:41:31 +00:00
|
|
|
for (auto &attrIt : llvm::enumerate(llvm::make_first_range(attributes))) {
|
|
|
|
for (StringRef name : op.getGetterNames(attrIt.value())) {
|
2021-10-14 15:58:44 -07:00
|
|
|
std::string methodName = (name + "AttrName").str();
|
|
|
|
|
|
|
|
// Generate the non-static variant.
|
|
|
|
{
|
|
|
|
auto *method =
|
2021-11-16 17:21:15 +00:00
|
|
|
opClass.addInlineMethod("::mlir::StringAttr", methodName);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, methodName, op);
|
2022-04-08 16:41:31 +00:00
|
|
|
method->body() << llvm::formatv(attrNameMethodBody, attrIt.index());
|
2021-10-14 15:58:44 -07:00
|
|
|
}
|
2021-06-22 19:13:35 +00:00
|
|
|
|
2021-10-14 15:58:44 -07:00
|
|
|
// Generate the static variant.
|
|
|
|
{
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *method = opClass.addStaticInlineMethod(
|
2021-11-16 17:21:15 +00:00
|
|
|
"::mlir::StringAttr", methodName,
|
2021-11-12 01:17:05 +00:00
|
|
|
MethodParameter("::mlir::OperationName", "name"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, methodName, op);
|
|
|
|
method->body() << llvm::formatv(attrNameMethodBody,
|
2022-04-08 16:41:31 +00:00
|
|
|
"name, " + Twine(attrIt.index()));
|
2021-10-14 15:58:44 -07:00
|
|
|
}
|
2021-06-22 19:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:36:34 +00:00
|
|
|
// Emit the getter for an attribute with the return type specified.
|
|
|
|
// It is templated to be shared between the Op and the adaptor class.
|
|
|
|
template <typename OpClassOrAdaptor>
|
|
|
|
static void emitAttrGetterWithReturnType(FmtContext &fctx,
|
|
|
|
OpClassOrAdaptor &opClass,
|
|
|
|
const Operator &op, StringRef name,
|
|
|
|
Attribute attr) {
|
|
|
|
auto *method = opClass.addMethod(attr.getReturnType(), name);
|
|
|
|
ERROR_IF_PRUNED(method, name, op);
|
|
|
|
auto &body = method->body();
|
|
|
|
body << " auto attr = " << name << "Attr();\n";
|
|
|
|
if (attr.hasDefaultValue()) {
|
|
|
|
// Returns the default value if not set.
|
|
|
|
// TODO: this is inefficient, we are recreating the attribute for every
|
|
|
|
// call. This should be set instead.
|
2022-02-07 10:27:07 +01:00
|
|
|
if (!attr.isConstBuildable()) {
|
|
|
|
PrintFatalError("DefaultValuedAttr of type " + attr.getAttrDefName() +
|
|
|
|
" must have a constBuilder");
|
|
|
|
}
|
2022-01-05 05:36:34 +00:00
|
|
|
std::string defaultValue = std::string(
|
|
|
|
tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue()));
|
|
|
|
body << " if (!attr)\n return "
|
|
|
|
<< tgfmt(attr.getConvertFromStorageCall(),
|
|
|
|
&fctx.withSelf(defaultValue))
|
|
|
|
<< ";\n";
|
|
|
|
}
|
|
|
|
body << " return "
|
|
|
|
<< tgfmt(attr.getConvertFromStorageCall(), &fctx.withSelf("attr"))
|
|
|
|
<< ";\n";
|
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genAttrGetters() {
|
2019-04-12 06:05:49 -07:00
|
|
|
FmtContext fctx;
|
2020-12-12 10:50:41 +01:00
|
|
|
fctx.withBuilder("::mlir::Builder((*this)->getContext())");
|
2018-11-28 09:21:42 -08:00
|
|
|
|
2019-12-09 10:28:58 -08:00
|
|
|
// Emit the derived attribute body.
|
|
|
|
auto emitDerivedAttr = [&](StringRef name, Attribute attr) {
|
2021-11-12 01:17:05 +00:00
|
|
|
if (auto *method = opClass.addMethod(attr.getReturnType(), name))
|
2021-06-22 19:13:35 +00:00
|
|
|
method->body() << " " << attr.getDerivedCodeBody() << "\n";
|
2019-12-09 10:28:58 -08:00
|
|
|
};
|
2019-03-20 17:25:34 -07:00
|
|
|
|
2021-10-14 15:58:44 -07:00
|
|
|
// Generate named accessor with Attribute return type. This is a wrapper class
|
|
|
|
// that allows referring to the attributes via accessors instead of having to
|
|
|
|
// use the string interface for better compile time verification.
|
2022-04-08 16:41:31 +00:00
|
|
|
auto emitAttrWithStorageType = [&](StringRef name, StringRef attrName,
|
|
|
|
Attribute attr) {
|
2021-11-30 14:09:00 +00:00
|
|
|
auto *method = opClass.addMethod(attr.getStorageType(), name + "Attr");
|
2020-09-17 13:18:09 -07:00
|
|
|
if (!method)
|
|
|
|
return;
|
2021-11-09 18:59:06 +00:00
|
|
|
method->body() << formatv(
|
2022-04-08 16:41:31 +00:00
|
|
|
" return {0}.{1}<{2}>();", emitHelper.getAttr(attrName),
|
2021-11-09 18:59:06 +00:00
|
|
|
attr.isOptional() || attr.hasDefaultValue() ? "dyn_cast_or_null"
|
|
|
|
: "cast",
|
|
|
|
attr.getStorageType());
|
2019-12-09 10:28:58 -08:00
|
|
|
};
|
|
|
|
|
2021-06-22 19:13:35 +00:00
|
|
|
for (const NamedAttribute &namedAttr : op.getAttributes()) {
|
2021-10-20 07:08:36 -07:00
|
|
|
for (StringRef name : op.getGetterNames(namedAttr.name)) {
|
2021-10-14 15:58:44 -07:00
|
|
|
if (namedAttr.attr.isDerivedAttr()) {
|
|
|
|
emitDerivedAttr(name, namedAttr.attr);
|
|
|
|
} else {
|
2022-04-08 16:41:31 +00:00
|
|
|
emitAttrWithStorageType(name, namedAttr.name, namedAttr.attr);
|
2022-01-05 05:36:34 +00:00
|
|
|
emitAttrGetterWithReturnType(fctx, opClass, op, name, namedAttr.attr);
|
2021-10-14 15:58:44 -07:00
|
|
|
}
|
2019-12-09 10:28:58 -08:00
|
|
|
}
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
2020-03-03 12:01:54 -08:00
|
|
|
|
2020-04-16 08:05:21 -07:00
|
|
|
auto derivedAttrs = make_filter_range(op.getAttributes(),
|
|
|
|
[](const NamedAttribute &namedAttr) {
|
|
|
|
return namedAttr.attr.isDerivedAttr();
|
|
|
|
});
|
2021-11-12 01:17:05 +00:00
|
|
|
if (derivedAttrs.empty())
|
|
|
|
return;
|
2020-04-16 08:05:21 -07:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
opClass.addTrait("::mlir::DerivedAttributeOpInterface::Trait");
|
|
|
|
// Generate helper method to query whether a named attribute is a derived
|
|
|
|
// attribute. This enables, for example, avoiding adding an attribute that
|
|
|
|
// overlaps with a derived attribute.
|
|
|
|
{
|
|
|
|
auto *method =
|
|
|
|
opClass.addStaticMethod("bool", "isDerivedAttribute",
|
|
|
|
MethodParameter("::llvm::StringRef", "name"));
|
|
|
|
ERROR_IF_PRUNED(method, "isDerivedAttribute", op);
|
|
|
|
auto &body = method->body();
|
|
|
|
for (auto namedAttr : derivedAttrs)
|
|
|
|
body << " if (name == \"" << namedAttr.name << "\") return true;\n";
|
|
|
|
body << " return false;";
|
|
|
|
}
|
|
|
|
// Generate method to materialize derived attributes as a DictionaryAttr.
|
|
|
|
{
|
|
|
|
auto *method = opClass.addMethod("::mlir::DictionaryAttr",
|
|
|
|
"materializeDerivedAttributes");
|
|
|
|
ERROR_IF_PRUNED(method, "materializeDerivedAttributes", op);
|
|
|
|
auto &body = method->body();
|
|
|
|
|
|
|
|
auto nonMaterializable =
|
|
|
|
make_filter_range(derivedAttrs, [](const NamedAttribute &namedAttr) {
|
|
|
|
return namedAttr.attr.getConvertFromStorageCall().empty();
|
|
|
|
});
|
|
|
|
if (!nonMaterializable.empty()) {
|
|
|
|
std::string attrs;
|
|
|
|
llvm::raw_string_ostream os(attrs);
|
|
|
|
interleaveComma(nonMaterializable, os, [&](const NamedAttribute &attr) {
|
|
|
|
os << op.getGetterName(attr.name);
|
|
|
|
});
|
|
|
|
PrintWarning(
|
|
|
|
op.getLoc(),
|
|
|
|
formatv(
|
|
|
|
"op has non-materializable derived attributes '{0}', skipping",
|
|
|
|
os.str()));
|
|
|
|
body << formatv(" emitOpError(\"op has non-materializable derived "
|
|
|
|
"attributes '{0}'\");\n",
|
|
|
|
attrs);
|
|
|
|
body << " return nullptr;";
|
|
|
|
return;
|
2020-04-16 08:05:21 -07:00
|
|
|
}
|
2021-11-12 01:17:05 +00:00
|
|
|
|
|
|
|
body << " ::mlir::MLIRContext* ctx = getContext();\n";
|
|
|
|
body << " ::mlir::Builder odsBuilder(ctx); (void)odsBuilder;\n";
|
|
|
|
body << " return ::mlir::DictionaryAttr::get(";
|
|
|
|
body << " ctx, {\n";
|
|
|
|
interleave(
|
|
|
|
derivedAttrs, body,
|
|
|
|
[&](const NamedAttribute &namedAttr) {
|
|
|
|
auto tmpl = namedAttr.attr.getConvertFromStorageCall();
|
|
|
|
std::string name = op.getGetterName(namedAttr.name);
|
|
|
|
body << " {" << name << "AttrName(),\n"
|
|
|
|
<< tgfmt(tmpl, &fctx.withSelf(name + "()")
|
|
|
|
.withBuilder("odsBuilder")
|
2022-07-05 20:04:50 +02:00
|
|
|
.addSubst("_ctxt", "ctx"))
|
2021-11-12 01:17:05 +00:00
|
|
|
<< "}";
|
|
|
|
},
|
|
|
|
",\n");
|
|
|
|
body << "});";
|
2020-03-13 10:09:02 -07:00
|
|
|
}
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
2020-02-19 11:39:28 -05:00
|
|
|
void OpEmitter::genAttrSetters() {
|
|
|
|
// Generate raw named setter type. This is a wrapper class that allows setting
|
|
|
|
// to the attributes via setters instead of having to use the string interface
|
|
|
|
// for better compile time verification.
|
2021-10-14 15:58:44 -07:00
|
|
|
auto emitAttrWithStorageType = [&](StringRef setterName, StringRef getterName,
|
|
|
|
Attribute attr) {
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *method =
|
2021-11-30 14:09:00 +00:00
|
|
|
opClass.addMethod("void", setterName + "Attr",
|
2021-11-12 01:17:05 +00:00
|
|
|
MethodParameter(attr.getStorageType(), "attr"));
|
2021-06-22 19:13:35 +00:00
|
|
|
if (method)
|
2021-11-09 18:59:06 +00:00
|
|
|
method->body() << formatv(" (*this)->setAttr({0}AttrName(), attr);",
|
|
|
|
getterName);
|
2020-02-19 11:39:28 -05:00
|
|
|
};
|
|
|
|
|
2021-10-14 15:58:44 -07:00
|
|
|
for (const NamedAttribute &namedAttr : op.getAttributes()) {
|
2021-11-12 01:17:05 +00:00
|
|
|
if (namedAttr.attr.isDerivedAttr())
|
|
|
|
continue;
|
|
|
|
for (auto names : llvm::zip(op.getSetterNames(namedAttr.name),
|
|
|
|
op.getGetterNames(namedAttr.name)))
|
|
|
|
emitAttrWithStorageType(std::get<0>(names), std::get<1>(names),
|
|
|
|
namedAttr.attr);
|
2021-10-14 15:58:44 -07:00
|
|
|
}
|
2020-02-19 11:39:28 -05:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:44:26 +00:00
|
|
|
void OpEmitter::genOptionalAttrRemovers() {
|
|
|
|
// Generate methods for removing optional attributes, instead of having to
|
|
|
|
// use the string interface. Enables better compile time verification.
|
|
|
|
auto emitRemoveAttr = [&](StringRef name) {
|
|
|
|
auto upperInitial = name.take_front().upper();
|
|
|
|
auto suffix = name.drop_front();
|
2021-11-30 14:09:00 +00:00
|
|
|
auto *method = opClass.addMethod("::mlir::Attribute",
|
|
|
|
"remove" + upperInitial + suffix + "Attr");
|
2021-01-05 18:44:26 +00:00
|
|
|
if (!method)
|
|
|
|
return;
|
2021-11-09 18:59:06 +00:00
|
|
|
method->body() << formatv(" return (*this)->removeAttr({0}AttrName());",
|
|
|
|
op.getGetterName(name));
|
2021-01-05 18:44:26 +00:00
|
|
|
};
|
|
|
|
|
2021-06-22 19:13:35 +00:00
|
|
|
for (const NamedAttribute &namedAttr : op.getAttributes())
|
|
|
|
if (namedAttr.attr.isOptional())
|
|
|
|
emitRemoveAttr(namedAttr.name);
|
2021-01-05 18:44:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:09:11 -07:00
|
|
|
// Generates the code to compute the start and end index of an operand or result
|
|
|
|
// range.
|
|
|
|
template <typename RangeT>
|
|
|
|
static void
|
|
|
|
generateValueRangeStartAndEnd(Class &opClass, StringRef methodName,
|
|
|
|
int numVariadic, int numNonVariadic,
|
|
|
|
StringRef rangeSizeCall, bool hasAttrSegmentSize,
|
2020-05-24 20:42:58 -07:00
|
|
|
StringRef sizeAttrInit, RangeT &&odsValues) {
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *method = opClass.addMethod("std::pair<unsigned, unsigned>", methodName,
|
|
|
|
MethodParameter("unsigned", "index"));
|
2020-09-17 13:18:09 -07:00
|
|
|
if (!method)
|
|
|
|
return;
|
|
|
|
auto &body = method->body();
|
2020-04-29 16:09:11 -07:00
|
|
|
if (numVariadic == 0) {
|
2020-09-17 13:18:09 -07:00
|
|
|
body << " return {index, 1};\n";
|
2020-04-29 16:09:11 -07:00
|
|
|
} else if (hasAttrSegmentSize) {
|
2020-09-17 13:18:09 -07:00
|
|
|
body << sizeAttrInit << attrSizedSegmentValueRangeCalcCode;
|
2020-04-29 16:09:11 -07:00
|
|
|
} else {
|
|
|
|
// Because the op can have arbitrarily interleaved variadic and non-variadic
|
|
|
|
// operands, we need to embed a list in the "sink" getter method for
|
|
|
|
// calculation at run-time.
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<StringRef, 4> isVariadic;
|
2020-04-29 16:09:11 -07:00
|
|
|
isVariadic.reserve(llvm::size(odsValues));
|
|
|
|
for (auto &it : odsValues)
|
|
|
|
isVariadic.push_back(it.isVariableLength() ? "true" : "false");
|
|
|
|
std::string isVariadicList = llvm::join(isVariadic, ", ");
|
2020-09-17 13:18:09 -07:00
|
|
|
body << formatv(sameVariadicSizeValueRangeCalcCode, isVariadicList,
|
|
|
|
numNonVariadic, numVariadic, rangeSizeCall, "operand");
|
2020-04-29 16:09:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 08:03:20 -07:00
|
|
|
// Generates the named operand getter methods for the given Operator `op` and
|
|
|
|
// puts them in `opClass`. Uses `rangeType` as the return type of getters that
|
2019-12-23 14:45:01 -08:00
|
|
|
// return a range of operands (individual operands are `Value ` and each
|
|
|
|
// element in the range must also be `Value `); use `rangeBeginCall` to get
|
2019-12-22 21:59:55 -08:00
|
|
|
// an iterator to the beginning of the operand range; use `rangeSizeCall` to
|
|
|
|
// obtain the number of operands. `getOperandCallPattern` contains the code
|
|
|
|
// necessary to obtain a single operand whose position will be substituted
|
|
|
|
// instead of
|
2019-06-03 08:03:20 -07:00
|
|
|
// "{0}" marker in the pattern. Note that the pattern should work for any kind
|
|
|
|
// of ops, in particular for one-operand ops that may not have the
|
|
|
|
// `getOperand(unsigned)` method.
|
|
|
|
static void generateNamedOperandGetters(const Operator &op, Class &opClass,
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
bool isAdaptor, StringRef sizeAttrInit,
|
2019-06-03 08:03:20 -07:00
|
|
|
StringRef rangeType,
|
|
|
|
StringRef rangeBeginCall,
|
|
|
|
StringRef rangeSizeCall,
|
|
|
|
StringRef getOperandCallPattern) {
|
2019-05-03 19:48:57 -07:00
|
|
|
const int numOperands = op.getNumOperands();
|
2020-04-10 14:11:45 -07:00
|
|
|
const int numVariadicOperands = op.getNumVariableLengthOperands();
|
2019-05-03 19:48:57 -07:00
|
|
|
const int numNormalOperands = numOperands - numVariadicOperands;
|
2019-04-25 14:45:37 -07:00
|
|
|
|
2019-11-25 17:26:16 -08:00
|
|
|
const auto *sameVariadicSize =
|
2020-09-14 20:01:07 +00:00
|
|
|
op.getTrait("::mlir::OpTrait::SameVariadicOperandSize");
|
2019-11-25 17:26:16 -08:00
|
|
|
const auto *attrSizedOperands =
|
2020-09-14 20:01:07 +00:00
|
|
|
op.getTrait("::mlir::OpTrait::AttrSizedOperandSegments");
|
2019-11-25 17:26:16 -08:00
|
|
|
|
|
|
|
if (numVariadicOperands > 1 && !sameVariadicSize && !attrSizedOperands) {
|
2019-06-09 05:50:09 -07:00
|
|
|
PrintFatalError(op.getLoc(), "op has multiple variadic operands but no "
|
|
|
|
"specification over their sizes");
|
2019-04-25 14:45:37 -07:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:26:16 -08:00
|
|
|
if (numVariadicOperands < 2 && attrSizedOperands) {
|
|
|
|
PrintFatalError(op.getLoc(), "op must have at least two variadic operands "
|
|
|
|
"to use 'AttrSizedOperandSegments' trait");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attrSizedOperands && sameVariadicSize) {
|
|
|
|
PrintFatalError(op.getLoc(),
|
|
|
|
"op cannot have both 'AttrSizedOperandSegments' and "
|
|
|
|
"'SameVariadicOperandSize' traits");
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:09:11 -07:00
|
|
|
// First emit a few "sink" getter methods upon which we layer all nicer named
|
2019-06-09 05:50:09 -07:00
|
|
|
// getter methods.
|
2020-05-24 20:42:58 -07:00
|
|
|
generateValueRangeStartAndEnd(opClass, "getODSOperandIndexAndLength",
|
|
|
|
numVariadicOperands, numNormalOperands,
|
|
|
|
rangeSizeCall, attrSizedOperands, sizeAttrInit,
|
|
|
|
const_cast<Operator &>(op).getOperands());
|
2019-04-25 14:45:37 -07:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addMethod(rangeType, "getODSOperands",
|
|
|
|
MethodParameter("unsigned", "index"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, "getODSOperands", op);
|
2020-09-17 13:18:09 -07:00
|
|
|
auto &body = m->body();
|
|
|
|
body << formatv(valueRangeReturnCode, rangeBeginCall,
|
|
|
|
"getODSOperandIndexAndLength(index)");
|
2019-04-25 14:45:37 -07:00
|
|
|
|
2019-06-09 05:50:09 -07:00
|
|
|
// Then we emit nicer named getter methods by redirecting to the "sink" getter
|
|
|
|
// method.
|
2019-05-03 19:48:57 -07:00
|
|
|
for (int i = 0; i != numOperands; ++i) {
|
2019-01-03 15:53:54 -08:00
|
|
|
const auto &operand = op.getOperand(i);
|
2019-03-20 17:25:34 -07:00
|
|
|
if (operand.name.empty())
|
|
|
|
continue;
|
2021-10-20 07:08:36 -07:00
|
|
|
for (StringRef name : op.getGetterNames(operand.name)) {
|
2021-10-14 15:58:44 -07:00
|
|
|
if (operand.isOptional()) {
|
2021-11-12 01:17:05 +00:00
|
|
|
m = opClass.addMethod("::mlir::Value", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << " auto operands = getODSOperands(" << i << ");\n"
|
|
|
|
<< " return operands.empty() ? ::mlir::Value() : "
|
|
|
|
"*operands.begin();";
|
|
|
|
} else if (operand.isVariadicOfVariadic()) {
|
2021-10-20 07:08:36 -07:00
|
|
|
std::string segmentAttr = op.getGetterName(
|
|
|
|
operand.constraint.getVariadicOfVariadicSegmentSizeAttr());
|
2021-10-14 15:58:44 -07:00
|
|
|
if (isAdaptor) {
|
2021-11-12 01:17:05 +00:00
|
|
|
m = opClass.addMethod("::llvm::SmallVector<::mlir::ValueRange>",
|
|
|
|
name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << llvm::formatv(variadicOfVariadicAdaptorCalcCode,
|
|
|
|
segmentAttr, i);
|
|
|
|
continue;
|
|
|
|
}
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
m = opClass.addMethod("::mlir::OperandRangeRange", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << " return getODSOperands(" << i << ").split("
|
|
|
|
<< segmentAttr << "Attr());";
|
|
|
|
} else if (operand.isVariadic()) {
|
2021-11-12 01:17:05 +00:00
|
|
|
m = opClass.addMethod(rangeType, name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << " return getODSOperands(" << i << ");";
|
|
|
|
} else {
|
2021-11-12 01:17:05 +00:00
|
|
|
m = opClass.addMethod("::mlir::Value", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << " return *getODSOperands(" << i << ").begin();";
|
|
|
|
}
|
2019-03-07 01:23:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 08:03:20 -07:00
|
|
|
void OpEmitter::genNamedOperandGetters() {
|
2021-10-20 07:08:36 -07:00
|
|
|
// Build the code snippet used for initializing the operand_segment_size)s
|
2021-06-22 19:13:35 +00:00
|
|
|
// array.
|
|
|
|
std::string attrSizeInitCode;
|
|
|
|
if (op.getTrait("::mlir::OpTrait::AttrSizedOperandSegments")) {
|
2022-04-08 16:41:31 +00:00
|
|
|
attrSizeInitCode = formatv(opSegmentSizeAttrInitCode,
|
|
|
|
emitHelper.getAttr(operandSegmentAttrName));
|
2021-06-22 19:13:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 08:03:20 -07:00
|
|
|
generateNamedOperandGetters(
|
2020-05-24 20:42:58 -07:00
|
|
|
op, opClass,
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
/*isAdaptor=*/false,
|
2021-06-22 19:13:35 +00:00
|
|
|
/*sizeAttrInit=*/attrSizeInitCode,
|
2020-06-26 13:20:44 +02:00
|
|
|
/*rangeType=*/"::mlir::Operation::operand_range",
|
2019-06-09 05:50:09 -07:00
|
|
|
/*rangeBeginCall=*/"getOperation()->operand_begin()",
|
|
|
|
/*rangeSizeCall=*/"getOperation()->getNumOperands()",
|
|
|
|
/*getOperandCallPattern=*/"getOperation()->getOperand({0})");
|
2019-06-03 08:03:20 -07:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:09:11 -07:00
|
|
|
void OpEmitter::genNamedOperandSetters() {
|
2020-09-14 20:01:07 +00:00
|
|
|
auto *attrSizedOperands =
|
|
|
|
op.getTrait("::mlir::OpTrait::AttrSizedOperandSegments");
|
2020-04-29 16:09:11 -07:00
|
|
|
for (int i = 0, e = op.getNumOperands(); i != e; ++i) {
|
|
|
|
const auto &operand = op.getOperand(i);
|
|
|
|
if (operand.name.empty())
|
|
|
|
continue;
|
2021-10-20 07:08:36 -07:00
|
|
|
for (StringRef name : op.getGetterNames(operand.name)) {
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addMethod(operand.isVariadicOfVariadic()
|
|
|
|
? "::mlir::MutableOperandRangeRange"
|
|
|
|
: "::mlir::MutableOperandRange",
|
|
|
|
(name + "Mutable").str());
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
auto &body = m->body();
|
|
|
|
body << " auto range = getODSOperandIndexAndLength(" << i << ");\n"
|
|
|
|
<< " auto mutableRange = "
|
|
|
|
"::mlir::MutableOperandRange(getOperation(), "
|
|
|
|
"range.first, range.second";
|
2022-04-08 16:41:31 +00:00
|
|
|
if (attrSizedOperands) {
|
|
|
|
body << formatv(
|
|
|
|
", ::mlir::MutableOperandRange::OperandSegment({0}u, *{1})", i,
|
|
|
|
emitHelper.getAttr(operandSegmentAttrName, /*isNamed=*/true));
|
|
|
|
}
|
2021-10-14 15:58:44 -07:00
|
|
|
body << ");\n";
|
|
|
|
|
|
|
|
// If this operand is a nested variadic, we split the range into a
|
|
|
|
// MutableOperandRangeRange that provides a range over all of the
|
|
|
|
// sub-ranges.
|
|
|
|
if (operand.isVariadicOfVariadic()) {
|
|
|
|
body << " return "
|
|
|
|
"mutableRange.split(*(*this)->getAttrDictionary().getNamed("
|
2021-10-20 07:08:36 -07:00
|
|
|
<< op.getGetterName(
|
2021-10-14 15:58:44 -07:00
|
|
|
operand.constraint.getVariadicOfVariadicSegmentSizeAttr())
|
|
|
|
<< "AttrName()));\n";
|
|
|
|
} else {
|
|
|
|
// Otherwise, we use the full range directly.
|
|
|
|
body << " return mutableRange;\n";
|
|
|
|
}
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
}
|
2020-04-29 16:09:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genNamedResultGetters() {
|
2019-05-03 19:48:57 -07:00
|
|
|
const int numResults = op.getNumResults();
|
2020-04-10 14:11:45 -07:00
|
|
|
const int numVariadicResults = op.getNumVariableLengthResults();
|
2019-05-03 19:48:57 -07:00
|
|
|
const int numNormalResults = numResults - numVariadicResults;
|
2019-04-25 14:45:37 -07:00
|
|
|
|
|
|
|
// If we have more than one variadic results, we need more complicated logic
|
|
|
|
// to calculate the value range for each result.
|
|
|
|
|
2020-09-14 20:01:07 +00:00
|
|
|
const auto *sameVariadicSize =
|
|
|
|
op.getTrait("::mlir::OpTrait::SameVariadicResultSize");
|
2019-11-25 17:26:16 -08:00
|
|
|
const auto *attrSizedResults =
|
2020-09-14 20:01:07 +00:00
|
|
|
op.getTrait("::mlir::OpTrait::AttrSizedResultSegments");
|
2019-11-25 17:26:16 -08:00
|
|
|
|
|
|
|
if (numVariadicResults > 1 && !sameVariadicSize && !attrSizedResults) {
|
2019-04-25 14:45:37 -07:00
|
|
|
PrintFatalError(op.getLoc(), "op has multiple variadic results but no "
|
|
|
|
"specification over their sizes");
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:26:16 -08:00
|
|
|
if (numVariadicResults < 2 && attrSizedResults) {
|
|
|
|
PrintFatalError(op.getLoc(), "op must have at least two variadic results "
|
|
|
|
"to use 'AttrSizedResultSegments' trait");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attrSizedResults && sameVariadicSize) {
|
|
|
|
PrintFatalError(op.getLoc(),
|
|
|
|
"op cannot have both 'AttrSizedResultSegments' and "
|
|
|
|
"'SameVariadicResultSize' traits");
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:13:35 +00:00
|
|
|
// Build the initializer string for the result segment size attribute.
|
|
|
|
std::string attrSizeInitCode;
|
|
|
|
if (attrSizedResults) {
|
2022-04-08 16:41:31 +00:00
|
|
|
attrSizeInitCode = formatv(opSegmentSizeAttrInitCode,
|
|
|
|
emitHelper.getAttr(resultSegmentAttrName));
|
2021-06-22 19:13:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:09:11 -07:00
|
|
|
generateValueRangeStartAndEnd(
|
|
|
|
opClass, "getODSResultIndexAndLength", numVariadicResults,
|
|
|
|
numNormalResults, "getOperation()->getNumResults()", attrSizedResults,
|
2021-06-22 19:13:35 +00:00
|
|
|
attrSizeInitCode, op.getResults());
|
2020-09-17 13:18:09 -07:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m =
|
|
|
|
opClass.addMethod("::mlir::Operation::result_range", "getODSResults",
|
|
|
|
MethodParameter("unsigned", "index"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, "getODSResults", op);
|
2020-09-17 13:18:09 -07:00
|
|
|
m->body() << formatv(valueRangeReturnCode, "getOperation()->result_begin()",
|
|
|
|
"getODSResultIndexAndLength(index)");
|
2019-04-25 14:45:37 -07:00
|
|
|
|
2019-05-03 19:48:57 -07:00
|
|
|
for (int i = 0; i != numResults; ++i) {
|
2019-03-07 01:23:43 -08:00
|
|
|
const auto &result = op.getResult(i);
|
2019-04-25 14:45:37 -07:00
|
|
|
if (result.name.empty())
|
2019-03-20 17:25:34 -07:00
|
|
|
continue;
|
2021-10-20 07:08:36 -07:00
|
|
|
for (StringRef name : op.getGetterNames(result.name)) {
|
2021-10-14 15:58:44 -07:00
|
|
|
if (result.isOptional()) {
|
2021-11-12 01:17:05 +00:00
|
|
|
m = opClass.addMethod("::mlir::Value", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body()
|
|
|
|
<< " auto results = getODSResults(" << i << ");\n"
|
|
|
|
<< " return results.empty() ? ::mlir::Value() : *results.begin();";
|
|
|
|
} else if (result.isVariadic()) {
|
2021-11-12 01:17:05 +00:00
|
|
|
m = opClass.addMethod("::mlir::Operation::result_range", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << " return getODSResults(" << i << ");";
|
|
|
|
} else {
|
2021-11-12 01:17:05 +00:00
|
|
|
m = opClass.addMethod("::mlir::Value", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << " return *getODSResults(" << i << ").begin();";
|
|
|
|
}
|
2019-04-25 14:45:37 -07:00
|
|
|
}
|
2018-12-20 02:57:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 02:20:38 -07:00
|
|
|
void OpEmitter::genNamedRegionGetters() {
|
|
|
|
unsigned numRegions = op.getNumRegions();
|
|
|
|
for (unsigned i = 0; i < numRegions; ++i) {
|
|
|
|
const auto ®ion = op.getRegion(i);
|
2020-04-05 01:03:24 -07:00
|
|
|
if (region.name.empty())
|
|
|
|
continue;
|
|
|
|
|
2021-10-20 07:08:36 -07:00
|
|
|
for (StringRef name : op.getGetterNames(region.name)) {
|
2021-10-14 15:58:44 -07:00
|
|
|
// Generate the accessors for a variadic region.
|
|
|
|
if (region.isVariadic()) {
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m =
|
|
|
|
opClass.addMethod("::mlir::MutableArrayRef<::mlir::Region>", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << formatv(" return (*this)->getRegions().drop_front({0});",
|
|
|
|
i);
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-05 01:03:24 -07:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addMethod("::mlir::Region &", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << formatv(" return (*this)->getRegion({0});", i);
|
|
|
|
}
|
2019-07-05 02:20:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 13:19:50 -08:00
|
|
|
void OpEmitter::genNamedSuccessorGetters() {
|
|
|
|
unsigned numSuccessors = op.getNumSuccessors();
|
|
|
|
for (unsigned i = 0; i < numSuccessors; ++i) {
|
|
|
|
const NamedSuccessor &successor = op.getSuccessor(i);
|
|
|
|
if (successor.name.empty())
|
|
|
|
continue;
|
|
|
|
|
2021-10-20 07:08:36 -07:00
|
|
|
for (StringRef name : op.getGetterNames(successor.name)) {
|
2021-10-14 15:58:44 -07:00
|
|
|
// Generate the accessors for a variadic successor list.
|
|
|
|
if (successor.isVariadic()) {
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addMethod("::mlir::SuccessorRange", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << formatv(
|
|
|
|
" return {std::next((*this)->successor_begin(), {0}), "
|
|
|
|
"(*this)->successor_end()};",
|
|
|
|
i);
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-21 13:19:50 -08:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addMethod("::mlir::Block *", name);
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, name, op);
|
|
|
|
m->body() << formatv(" return (*this)->getSuccessor({0});", i);
|
|
|
|
}
|
2020-02-21 13:19:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
static bool canGenerateUnwrappedBuilder(const Operator &op) {
|
2019-12-02 09:33:24 -08:00
|
|
|
// If this op does not have native attributes at all, return directly to avoid
|
|
|
|
// redefining builders.
|
|
|
|
if (op.getNumNativeAttributes() == 0)
|
2019-12-12 10:35:40 -08:00
|
|
|
return false;
|
2019-12-02 09:33:24 -08:00
|
|
|
|
|
|
|
bool canGenerate = false;
|
|
|
|
// We are generating builders that take raw values for attributes. We need to
|
|
|
|
// make sure the native attributes have a meaningful "unwrapped" value type
|
|
|
|
// different from the wrapped mlir::Attribute type to avoid redefining
|
|
|
|
// builders. This checks for the op has at least one such native attribute.
|
|
|
|
for (int i = 0, e = op.getNumNativeAttributes(); i < e; ++i) {
|
2022-04-08 16:41:31 +00:00
|
|
|
const NamedAttribute &namedAttr = op.getAttribute(i);
|
2019-12-02 09:33:24 -08:00
|
|
|
if (canUseUnwrappedRawValue(namedAttr.attr)) {
|
|
|
|
canGenerate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 10:35:40 -08:00
|
|
|
return canGenerate;
|
2019-12-02 09:33:24 -08:00
|
|
|
}
|
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
static bool canInferType(const Operator &op) {
|
2021-12-10 15:51:02 +00:00
|
|
|
return op.getTrait("::mlir::InferTypeOpInterface::Trait");
|
2020-05-27 08:45:55 -07:00
|
|
|
}
|
|
|
|
|
2019-12-12 10:35:40 -08:00
|
|
|
void OpEmitter::genSeparateArgParamBuilder() {
|
|
|
|
SmallVector<AttrParamKind, 2> attrBuilderType;
|
|
|
|
attrBuilderType.push_back(AttrParamKind::WrappedAttr);
|
|
|
|
if (canGenerateUnwrappedBuilder(op))
|
|
|
|
attrBuilderType.push_back(AttrParamKind::UnwrappedValue);
|
|
|
|
|
|
|
|
// Emit with separate builders with or without unwrapped attributes and/or
|
|
|
|
// inferring result type.
|
|
|
|
auto emit = [&](AttrParamKind attrType, TypeParamKind paramKind,
|
|
|
|
bool inferType) {
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
|
|
|
SmallVector<std::string, 4> resultNames;
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
llvm::StringSet<> inferredAttributes;
|
|
|
|
buildParamList(paramList, inferredAttributes, resultNames, paramKind,
|
|
|
|
attrType);
|
2019-12-12 10:35:40 -08:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addStaticMethod("void", "build", std::move(paramList));
|
2020-09-17 13:18:09 -07:00
|
|
|
// If the builder is redundant, skip generating the method.
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
auto &body = m->body();
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
genCodeForAddingArgAndRegionForBuilder(body, inferredAttributes,
|
|
|
|
/*isRawValueAttr=*/attrType ==
|
|
|
|
AttrParamKind::UnwrappedValue);
|
2019-12-12 10:35:40 -08:00
|
|
|
|
|
|
|
// Push all result types to the operation state
|
|
|
|
|
|
|
|
if (inferType) {
|
|
|
|
// Generate builder that infers type too.
|
2020-07-07 01:35:23 -07:00
|
|
|
// TODO: Subsume this with general checking if type can be
|
2020-02-28 10:59:34 -08:00
|
|
|
// inferred automatically.
|
2020-07-07 01:35:23 -07:00
|
|
|
// TODO: Expand to handle regions.
|
2019-12-12 10:35:40 -08:00
|
|
|
body << formatv(R"(
|
2020-06-26 13:20:44 +02:00
|
|
|
::llvm::SmallVector<::mlir::Type, 2> inferredReturnTypes;
|
2021-08-19 17:33:58 +03:00
|
|
|
if (::mlir::succeeded({0}::inferReturnTypes(odsBuilder.getContext(),
|
2020-05-06 13:48:36 -07:00
|
|
|
{1}.location, {1}.operands,
|
|
|
|
{1}.attributes.getDictionary({1}.getContext()),
|
2020-02-28 10:59:34 -08:00
|
|
|
/*regions=*/{{}, inferredReturnTypes)))
|
|
|
|
{1}.addTypes(inferredReturnTypes);
|
2019-12-12 10:35:40 -08:00
|
|
|
else
|
2020-06-26 13:20:44 +02:00
|
|
|
::llvm::report_fatal_error("Failed to infer result type(s).");)",
|
2019-12-12 10:35:40 -08:00
|
|
|
opClass.getClassName(), builderOpState);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-21 05:35:07 -07:00
|
|
|
|
2019-12-12 10:35:40 -08:00
|
|
|
switch (paramKind) {
|
|
|
|
case TypeParamKind::None:
|
|
|
|
return;
|
|
|
|
case TypeParamKind::Separate:
|
|
|
|
for (int i = 0, e = op.getNumResults(); i < e; ++i) {
|
2020-04-10 14:11:45 -07:00
|
|
|
if (op.getResult(i).isOptional())
|
|
|
|
body << " if (" << resultNames[i] << ")\n ";
|
2019-12-12 10:35:40 -08:00
|
|
|
body << " " << builderOpState << ".addTypes(" << resultNames[i]
|
|
|
|
<< ");\n";
|
|
|
|
}
|
|
|
|
return;
|
2020-07-13 18:53:50 -07:00
|
|
|
case TypeParamKind::Collective: {
|
|
|
|
int numResults = op.getNumResults();
|
|
|
|
int numVariadicResults = op.getNumVariableLengthResults();
|
|
|
|
int numNonVariadicResults = numResults - numVariadicResults;
|
|
|
|
bool hasVariadicResult = numVariadicResults != 0;
|
|
|
|
|
|
|
|
// Avoid emitting "resultTypes.size() >= 0u" which is always true.
|
|
|
|
if (!(hasVariadicResult && numNonVariadicResults == 0))
|
|
|
|
body << " "
|
|
|
|
<< "assert(resultTypes.size() "
|
|
|
|
<< (hasVariadicResult ? ">=" : "==") << " "
|
|
|
|
<< numNonVariadicResults
|
|
|
|
<< "u && \"mismatched number of results\");\n";
|
2019-12-12 10:35:40 -08:00
|
|
|
body << " " << builderOpState << ".addTypes(resultTypes);\n";
|
2020-07-13 18:53:50 -07:00
|
|
|
}
|
2019-12-12 10:35:40 -08:00
|
|
|
return;
|
2020-04-10 14:11:45 -07:00
|
|
|
}
|
2019-12-12 10:35:40 -08:00
|
|
|
llvm_unreachable("unhandled TypeParamKind");
|
|
|
|
};
|
2019-08-21 05:35:07 -07:00
|
|
|
|
2020-10-29 04:03:15 +09:00
|
|
|
// Some of the build methods generated here may be ambiguous, but TableGen's
|
2020-09-17 13:18:09 -07:00
|
|
|
// ambiguous function detection will elide those ones.
|
2019-12-12 10:35:40 -08:00
|
|
|
for (auto attrType : attrBuilderType) {
|
2020-09-17 13:18:09 -07:00
|
|
|
emit(attrType, TypeParamKind::Separate, /*inferType=*/false);
|
2021-12-10 15:51:02 +00:00
|
|
|
if (canInferType(op) && op.getNumRegions() == 0)
|
2020-09-17 13:18:09 -07:00
|
|
|
emit(attrType, TypeParamKind::None, /*inferType=*/true);
|
|
|
|
emit(attrType, TypeParamKind::Collective, /*inferType=*/false);
|
2019-12-12 10:35:40 -08:00
|
|
|
}
|
2019-08-21 05:35:07 -07:00
|
|
|
}
|
|
|
|
|
2019-11-15 07:33:21 -08:00
|
|
|
void OpEmitter::genUseOperandAsResultTypeCollectiveParamBuilder() {
|
|
|
|
int numResults = op.getNumResults();
|
|
|
|
|
|
|
|
// Signature
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::mlir::OpBuilder &", "odsBuilder");
|
|
|
|
paramList.emplace_back("::mlir::OperationState &", builderOpState);
|
|
|
|
paramList.emplace_back("::mlir::ValueRange", "operands");
|
|
|
|
// Provide default value for `attributes` when its the last parameter
|
|
|
|
StringRef attributesDefaultValue = op.getNumVariadicRegions() ? "" : "{}";
|
|
|
|
paramList.emplace_back("::llvm::ArrayRef<::mlir::NamedAttribute>",
|
|
|
|
"attributes", attributesDefaultValue);
|
|
|
|
if (op.getNumVariadicRegions())
|
|
|
|
paramList.emplace_back("unsigned", "numRegions");
|
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addStaticMethod("void", "build", std::move(paramList));
|
2020-09-17 13:18:09 -07:00
|
|
|
// If the builder is redundant, skip generating the method
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
auto &body = m->body();
|
2019-11-15 07:33:21 -08:00
|
|
|
|
|
|
|
// Operands
|
2020-04-16 23:24:58 +02:00
|
|
|
body << " " << builderOpState << ".addOperands(operands);\n";
|
2019-11-15 07:33:21 -08:00
|
|
|
|
|
|
|
// Attributes
|
|
|
|
body << " " << builderOpState << ".addAttributes(attributes);\n";
|
|
|
|
|
|
|
|
// Create the correct number of regions
|
|
|
|
if (int numRegions = op.getNumRegions()) {
|
2020-04-05 01:03:24 -07:00
|
|
|
body << llvm::formatv(
|
|
|
|
" for (unsigned i = 0; i != {0}; ++i)\n",
|
|
|
|
(op.getNumVariadicRegions() ? "numRegions" : Twine(numRegions)));
|
|
|
|
body << " (void)" << builderOpState << ".addRegion();\n";
|
2019-11-15 07:33:21 -08:00
|
|
|
}
|
2019-12-06 10:52:38 -08:00
|
|
|
|
|
|
|
// Result types
|
2020-01-11 08:54:04 -08:00
|
|
|
SmallVector<std::string, 2> resultTypes(numResults, "operands[0].getType()");
|
2019-12-06 10:52:38 -08:00
|
|
|
body << " " << builderOpState << ".addTypes({"
|
|
|
|
<< llvm::join(resultTypes, ", ") << "});\n\n";
|
|
|
|
}
|
|
|
|
|
2022-07-08 11:31:12 -07:00
|
|
|
void OpEmitter::genPopulateDefaultAttributes() {
|
|
|
|
// All done if no attributes have default values.
|
|
|
|
if (llvm::all_of(op.getAttributes(), [](const NamedAttribute &named) {
|
|
|
|
return !named.attr.hasDefaultValue();
|
|
|
|
}))
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallVector<MethodParameter> paramList;
|
|
|
|
paramList.emplace_back("const ::mlir::RegisteredOperationName &", "opName");
|
|
|
|
paramList.emplace_back("::mlir::NamedAttrList &", "attributes");
|
|
|
|
auto *m = opClass.addStaticMethod("void", "populateDefaultAttrs", paramList);
|
|
|
|
ERROR_IF_PRUNED(m, "populateDefaultAttrs", op);
|
|
|
|
auto &body = m->body();
|
|
|
|
body.indent();
|
|
|
|
|
|
|
|
// Set default attributes that are unset.
|
|
|
|
body << "auto attrNames = opName.getAttributeNames();\n";
|
|
|
|
body << "::mlir::Builder " << odsBuilder
|
|
|
|
<< "(attrNames.front().getContext());\n";
|
|
|
|
StringMap<int> attrIndex;
|
|
|
|
for (const auto &it : llvm::enumerate(emitHelper.getAttrMetadata())) {
|
|
|
|
attrIndex[it.value().first] = it.index();
|
|
|
|
}
|
|
|
|
for (const NamedAttribute &namedAttr : op.getAttributes()) {
|
|
|
|
auto &attr = namedAttr.attr;
|
|
|
|
if (!attr.hasDefaultValue())
|
|
|
|
continue;
|
|
|
|
auto index = attrIndex[namedAttr.name];
|
|
|
|
body << "if (!attributes.get(attrNames[" << index << "])) {\n";
|
|
|
|
FmtContext fctx;
|
|
|
|
fctx.withBuilder(odsBuilder);
|
|
|
|
std::string defaultValue = std::string(
|
|
|
|
tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue()));
|
|
|
|
body.indent() << formatv(" attributes.append(attrNames[{0}], {1});\n",
|
|
|
|
index, defaultValue);
|
|
|
|
body.unindent() << "}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 10:59:34 -08:00
|
|
|
void OpEmitter::genInferredTypeCollectiveParamBuilder() {
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::mlir::OpBuilder &", "odsBuilder");
|
|
|
|
paramList.emplace_back("::mlir::OperationState &", builderOpState);
|
|
|
|
paramList.emplace_back("::mlir::ValueRange", "operands");
|
2022-04-07 18:22:14 +00:00
|
|
|
StringRef attributesDefaultValue = op.getNumVariadicRegions() ? "" : "{}";
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::llvm::ArrayRef<::mlir::NamedAttribute>",
|
2022-04-07 18:22:14 +00:00
|
|
|
"attributes", attributesDefaultValue);
|
|
|
|
if (op.getNumVariadicRegions())
|
|
|
|
paramList.emplace_back("unsigned", "numRegions");
|
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addStaticMethod("void", "build", std::move(paramList));
|
2020-09-17 13:18:09 -07:00
|
|
|
// If the builder is redundant, skip generating the method
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
auto &body = m->body();
|
2020-05-06 13:48:36 -07:00
|
|
|
|
|
|
|
int numResults = op.getNumResults();
|
|
|
|
int numVariadicResults = op.getNumVariableLengthResults();
|
|
|
|
int numNonVariadicResults = numResults - numVariadicResults;
|
|
|
|
|
|
|
|
int numOperands = op.getNumOperands();
|
|
|
|
int numVariadicOperands = op.getNumVariableLengthOperands();
|
|
|
|
int numNonVariadicOperands = numOperands - numVariadicOperands;
|
|
|
|
|
|
|
|
// Operands
|
|
|
|
if (numVariadicOperands == 0 || numNonVariadicOperands != 0)
|
|
|
|
body << " assert(operands.size()"
|
|
|
|
<< (numVariadicOperands != 0 ? " >= " : " == ")
|
|
|
|
<< numNonVariadicOperands
|
|
|
|
<< "u && \"mismatched number of parameters\");\n";
|
|
|
|
body << " " << builderOpState << ".addOperands(operands);\n";
|
|
|
|
body << " " << builderOpState << ".addAttributes(attributes);\n";
|
|
|
|
|
|
|
|
// Create the correct number of regions
|
|
|
|
if (int numRegions = op.getNumRegions()) {
|
|
|
|
body << llvm::formatv(
|
|
|
|
" for (unsigned i = 0; i != {0}; ++i)\n",
|
|
|
|
(op.getNumVariadicRegions() ? "numRegions" : Twine(numRegions)));
|
|
|
|
body << " (void)" << builderOpState << ".addRegion();\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result types
|
2019-12-06 14:42:16 -08:00
|
|
|
body << formatv(R"(
|
2022-05-24 15:03:12 +00:00
|
|
|
::llvm::SmallVector<::mlir::Type, 2> inferredReturnTypes;
|
2021-12-10 15:51:02 +00:00
|
|
|
if (::mlir::succeeded({0}::inferReturnTypes(odsBuilder.getContext(),
|
|
|
|
{1}.location, operands,
|
|
|
|
{1}.attributes.getDictionary({1}.getContext()),
|
|
|
|
{1}.regions, inferredReturnTypes))) {{)",
|
2020-05-06 13:48:36 -07:00
|
|
|
opClass.getClassName(), builderOpState);
|
|
|
|
if (numVariadicResults == 0 || numNonVariadicResults != 0)
|
2021-12-10 15:51:02 +00:00
|
|
|
body << "\n assert(inferredReturnTypes.size()"
|
2020-05-06 13:48:36 -07:00
|
|
|
<< (numVariadicResults != 0 ? " >= " : " == ") << numNonVariadicResults
|
2021-12-10 15:51:02 +00:00
|
|
|
<< "u && \"mismatched number of return types\");";
|
|
|
|
body << "\n " << builderOpState << ".addTypes(inferredReturnTypes);";
|
2020-05-06 13:48:36 -07:00
|
|
|
|
|
|
|
body << formatv(R"(
|
2021-12-10 15:51:02 +00:00
|
|
|
} else {{
|
|
|
|
::llvm::report_fatal_error("Failed to infer result type(s).");
|
|
|
|
})",
|
2019-12-06 14:42:16 -08:00
|
|
|
opClass.getClassName(), builderOpState);
|
2019-11-15 07:33:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpEmitter::genUseOperandAsResultTypeSeparateParamBuilder() {
|
2022-04-25 19:00:20 +00:00
|
|
|
auto emit = [&](AttrParamKind attrType) {
|
|
|
|
SmallVector<MethodParameter> paramList;
|
|
|
|
SmallVector<std::string, 4> resultNames;
|
|
|
|
llvm::StringSet<> inferredAttributes;
|
|
|
|
buildParamList(paramList, inferredAttributes, resultNames,
|
|
|
|
TypeParamKind::None, attrType);
|
2019-04-25 14:45:37 -07:00
|
|
|
|
2022-04-25 19:00:20 +00:00
|
|
|
auto *m = opClass.addStaticMethod("void", "build", std::move(paramList));
|
|
|
|
// If the builder is redundant, skip generating the method
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
auto &body = m->body();
|
|
|
|
genCodeForAddingArgAndRegionForBuilder(body, inferredAttributes,
|
|
|
|
/*isRawValueAttr=*/attrType ==
|
|
|
|
AttrParamKind::UnwrappedValue);
|
2019-03-20 17:25:34 -07:00
|
|
|
|
2022-04-25 19:00:20 +00:00
|
|
|
auto numResults = op.getNumResults();
|
|
|
|
if (numResults == 0)
|
|
|
|
return;
|
2018-12-06 12:06:00 -08:00
|
|
|
|
2022-04-25 19:00:20 +00:00
|
|
|
// Push all result types to the operation state
|
|
|
|
const char *index = op.getOperand(0).isVariadic() ? ".front()" : "";
|
|
|
|
std::string resultType =
|
|
|
|
formatv("{0}{1}.getType()", getArgumentName(op, 0), index).str();
|
|
|
|
body << " " << builderOpState << ".addTypes({" << resultType;
|
|
|
|
for (int i = 1; i != numResults; ++i)
|
|
|
|
body << ", " << resultType;
|
|
|
|
body << "});\n\n";
|
|
|
|
};
|
|
|
|
|
|
|
|
emit(AttrParamKind::WrappedAttr);
|
|
|
|
// Generate additional builder(s) if any attributes can be "unwrapped"
|
|
|
|
if (canGenerateUnwrappedBuilder(op))
|
|
|
|
emit(AttrParamKind::UnwrappedValue);
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
2019-04-16 13:20:38 -07:00
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
void OpEmitter::genUseAttrAsResultTypeBuilder() {
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::mlir::OpBuilder &", "odsBuilder");
|
|
|
|
paramList.emplace_back("::mlir::OperationState &", builderOpState);
|
|
|
|
paramList.emplace_back("::mlir::ValueRange", "operands");
|
|
|
|
paramList.emplace_back("::llvm::ArrayRef<::mlir::NamedAttribute>",
|
|
|
|
"attributes", "{}");
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addStaticMethod("void", "build", std::move(paramList));
|
2020-09-17 13:18:09 -07:00
|
|
|
// If the builder is redundant, skip generating the method
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto &body = m->body();
|
2019-05-28 08:03:46 -07:00
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
// Push all result types to the operation state
|
|
|
|
std::string resultType;
|
|
|
|
const auto &namedAttr = op.getAttribute(0);
|
2019-11-15 07:33:21 -08:00
|
|
|
|
2021-10-20 07:08:36 -07:00
|
|
|
body << " auto attrName = " << op.getGetterName(namedAttr.name)
|
2021-10-14 15:58:44 -07:00
|
|
|
<< "AttrName(" << builderOpState
|
2021-06-22 19:13:35 +00:00
|
|
|
<< ".name);\n"
|
|
|
|
" for (auto attr : attributes) {\n"
|
2021-11-18 05:23:32 +00:00
|
|
|
" if (attr.getName() != attrName) continue;\n";
|
2019-07-31 15:30:46 -07:00
|
|
|
if (namedAttr.attr.isTypeAttr()) {
|
2021-11-18 05:23:32 +00:00
|
|
|
resultType = "attr.getValue().cast<::mlir::TypeAttr>().getValue()";
|
2019-07-31 15:30:46 -07:00
|
|
|
} else {
|
[mlir] Remove types from attributes
This patch removes the `type` field from `Attribute` along with the
`Attribute::getType` accessor.
Going forward, this means that attributes in MLIR will no longer have
types as a first-class concept. This patch lays the groundwork to
incrementally remove or refactor code that relies on generic attributes
being typed. The immediate impact will be on attributes that rely on
`Attribute` containing a type, such as `IntegerAttr`,
`DenseElementsAttr`, and `ml_program::ExternAttr`, which will now need
to define a type parameter on their storage classes. This will save
memory as all other attribute kinds will no longer contain a type.
Moreover, it will not be possible to generically query the type of an
attribute directly. This patch provides an attribute interface
`TypedAttr` that implements only one method, `getType`, which can be
used to generically query the types of attributes that implement the
interface. This interface can be used to retain the concept of a "typed
attribute". The ODS-generated accessor for a `type` parameter
automatically implements this method.
Next steps will be to refactor the assembly formats of certain operations
that rely on `parseAttribute(type)` and `printAttributeWithoutType` to
remove special handling of type elision until `type` can be removed from
the dialect parsing hook entirely; and incrementally remove uses of
`TypedAttr`.
Reviewed By: lattner, rriddle, jpienaar
Differential Revision: https://reviews.llvm.org/D130092
2022-07-18 21:32:38 -07:00
|
|
|
resultType = "attr.getValue().cast<::mlir::TypedAttr>().getType()";
|
2019-05-28 08:03:46 -07:00
|
|
|
}
|
2019-11-15 07:33:21 -08:00
|
|
|
|
|
|
|
// Operands
|
2020-04-16 23:24:58 +02:00
|
|
|
body << " " << builderOpState << ".addOperands(operands);\n";
|
|
|
|
|
2019-11-15 07:33:21 -08:00
|
|
|
// Attributes
|
|
|
|
body << " " << builderOpState << ".addAttributes(attributes);\n";
|
2019-12-06 10:52:38 -08:00
|
|
|
|
|
|
|
// Result types
|
|
|
|
SmallVector<std::string, 2> resultTypes(op.getNumResults(), resultType);
|
|
|
|
body << " " << builderOpState << ".addTypes({"
|
|
|
|
<< llvm::join(resultTypes, ", ") << "});\n";
|
|
|
|
body << " }\n";
|
2019-02-14 10:54:50 -08:00
|
|
|
}
|
|
|
|
|
2021-01-11 11:54:51 -08:00
|
|
|
/// Returns a signature of the builder. Updates the context `fctx` to enable
|
|
|
|
/// replacement of $_builder and $_state in the body.
|
2021-11-12 01:17:05 +00:00
|
|
|
static SmallVector<MethodParameter>
|
|
|
|
getBuilderSignature(const Builder &builder) {
|
2021-01-11 11:54:51 -08:00
|
|
|
ArrayRef<Builder::Parameter> params(builder.getParameters());
|
2020-10-16 11:40:34 +02:00
|
|
|
|
|
|
|
// Inject builder and state arguments.
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> arguments;
|
2021-01-11 11:54:51 -08:00
|
|
|
arguments.reserve(params.size() + 2);
|
2021-11-12 01:17:05 +00:00
|
|
|
arguments.emplace_back("::mlir::OpBuilder &", odsBuilder);
|
|
|
|
arguments.emplace_back("::mlir::OperationState &", builderOpState);
|
2020-10-16 11:40:34 +02:00
|
|
|
|
2021-01-11 11:54:51 -08:00
|
|
|
for (unsigned i = 0, e = params.size(); i < e; ++i) {
|
2020-10-16 11:40:34 +02:00
|
|
|
// If no name is provided, generate one.
|
2021-01-11 11:54:51 -08:00
|
|
|
Optional<StringRef> paramName = params[i].getName();
|
2020-10-16 11:40:34 +02:00
|
|
|
std::string name =
|
2021-01-11 11:54:51 -08:00
|
|
|
paramName ? paramName->str() : "odsArg" + std::to_string(i);
|
2020-10-16 11:40:34 +02:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
StringRef defaultValue;
|
2021-01-11 11:54:51 -08:00
|
|
|
if (Optional<StringRef> defaultParamValue = params[i].getDefaultValue())
|
2021-11-12 01:17:05 +00:00
|
|
|
defaultValue = *defaultParamValue;
|
|
|
|
|
|
|
|
arguments.emplace_back(params[i].getCppType(), std::move(name),
|
|
|
|
defaultValue);
|
2020-10-16 11:40:34 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
return arguments;
|
2020-10-16 11:40:34 +02:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genBuilder() {
|
|
|
|
// Handle custom builders if provided.
|
2021-01-11 11:54:51 -08:00
|
|
|
for (const Builder &builder : op.getBuilders()) {
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> arguments = getBuilderSignature(builder);
|
2021-01-11 11:54:51 -08:00
|
|
|
|
|
|
|
Optional<StringRef> body = builder.getBody();
|
2021-11-30 14:09:00 +00:00
|
|
|
auto properties = body ? Method::Static : Method::StaticDeclaration;
|
2021-01-11 11:54:51 -08:00
|
|
|
auto *method =
|
2021-11-12 01:17:05 +00:00
|
|
|
opClass.addMethod("void", "build", properties, std::move(arguments));
|
2021-10-14 15:58:44 -07:00
|
|
|
if (body)
|
|
|
|
ERROR_IF_PRUNED(method, "build", op);
|
2021-01-11 11:54:51 -08:00
|
|
|
|
|
|
|
FmtContext fctx;
|
|
|
|
fctx.withBuilder(odsBuilder);
|
|
|
|
fctx.addSubst("_state", builderOpState);
|
|
|
|
if (body)
|
|
|
|
method->body() << tgfmt(*body, &fctx);
|
2019-02-14 10:54:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate default builders that requires all result type, operands, and
|
|
|
|
// attributes as parameters.
|
2021-01-11 11:54:51 -08:00
|
|
|
if (op.skipDefaultBuilders())
|
|
|
|
return;
|
2019-02-14 10:54:50 -08:00
|
|
|
|
2019-12-12 10:35:40 -08:00
|
|
|
// We generate three classes of builders here:
|
|
|
|
// 1. one having a stand-alone parameter for each operand / attribute, and
|
|
|
|
genSeparateArgParamBuilder();
|
|
|
|
// 2. one having an aggregated parameter for all result types / operands /
|
2019-02-14 10:54:50 -08:00
|
|
|
// attributes, and
|
2019-07-31 15:30:46 -07:00
|
|
|
genCollectiveParamBuilder();
|
2019-12-12 10:35:40 -08:00
|
|
|
// 3. one having a stand-alone parameter for each operand and attribute,
|
2019-07-31 15:30:46 -07:00
|
|
|
// use the first operand or attribute's type as all result types
|
2019-12-06 10:52:38 -08:00
|
|
|
// to facilitate different call patterns.
|
2020-04-10 14:11:45 -07:00
|
|
|
if (op.getNumVariableLengthResults() == 0) {
|
2020-09-14 20:01:07 +00:00
|
|
|
if (op.getTrait("::mlir::OpTrait::SameOperandsAndResultType")) {
|
2020-09-17 13:18:09 -07:00
|
|
|
genUseOperandAsResultTypeSeparateParamBuilder();
|
|
|
|
genUseOperandAsResultTypeCollectiveParamBuilder();
|
2019-11-15 07:33:21 -08:00
|
|
|
}
|
2020-09-14 20:01:07 +00:00
|
|
|
if (op.getTrait("::mlir::OpTrait::FirstAttrDerivedResultType"))
|
2019-07-31 15:30:46 -07:00
|
|
|
genUseAttrAsResultTypeBuilder();
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 10:54:50 -08:00
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
void OpEmitter::genCollectiveParamBuilder() {
|
|
|
|
int numResults = op.getNumResults();
|
2020-04-10 14:11:45 -07:00
|
|
|
int numVariadicResults = op.getNumVariableLengthResults();
|
2019-07-31 15:30:46 -07:00
|
|
|
int numNonVariadicResults = numResults - numVariadicResults;
|
2018-12-11 13:59:29 -08:00
|
|
|
|
2019-07-31 15:30:46 -07:00
|
|
|
int numOperands = op.getNumOperands();
|
2020-04-10 14:11:45 -07:00
|
|
|
int numVariadicOperands = op.getNumVariableLengthOperands();
|
2019-07-31 15:30:46 -07:00
|
|
|
int numNonVariadicOperands = numOperands - numVariadicOperands;
|
2020-09-17 13:18:09 -07:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::mlir::OpBuilder &", "");
|
|
|
|
paramList.emplace_back("::mlir::OperationState &", builderOpState);
|
2020-09-22 20:58:30 -07:00
|
|
|
paramList.emplace_back("::mlir::TypeRange", "resultTypes");
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::mlir::ValueRange", "operands");
|
|
|
|
// Provide default value for `attributes` when its the last parameter
|
|
|
|
StringRef attributesDefaultValue = op.getNumVariadicRegions() ? "" : "{}";
|
|
|
|
paramList.emplace_back("::llvm::ArrayRef<::mlir::NamedAttribute>",
|
|
|
|
"attributes", attributesDefaultValue);
|
|
|
|
if (op.getNumVariadicRegions())
|
|
|
|
paramList.emplace_back("unsigned", "numRegions");
|
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.addStaticMethod("void", "build", std::move(paramList));
|
2020-09-17 13:18:09 -07:00
|
|
|
// If the builder is redundant, skip generating the method
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
auto &body = m->body();
|
2018-12-11 13:59:29 -08:00
|
|
|
|
|
|
|
// Operands
|
2019-04-25 14:45:37 -07:00
|
|
|
if (numVariadicOperands == 0 || numNonVariadicOperands != 0)
|
|
|
|
body << " assert(operands.size()"
|
|
|
|
<< (numVariadicOperands != 0 ? " >= " : " == ")
|
2019-04-10 08:00:34 -07:00
|
|
|
<< numNonVariadicOperands
|
|
|
|
<< "u && \"mismatched number of parameters\");\n";
|
2020-04-16 23:24:58 +02:00
|
|
|
body << " " << builderOpState << ".addOperands(operands);\n";
|
2018-12-11 13:59:29 -08:00
|
|
|
|
|
|
|
// Attributes
|
2019-11-15 07:33:21 -08:00
|
|
|
body << " " << builderOpState << ".addAttributes(attributes);\n";
|
2019-02-14 10:54:50 -08:00
|
|
|
|
2019-05-28 08:03:46 -07:00
|
|
|
// Create the correct number of regions
|
|
|
|
if (int numRegions = op.getNumRegions()) {
|
2020-04-05 01:03:24 -07:00
|
|
|
body << llvm::formatv(
|
|
|
|
" for (unsigned i = 0; i != {0}; ++i)\n",
|
|
|
|
(op.getNumVariadicRegions() ? "numRegions" : Twine(numRegions)));
|
|
|
|
body << " (void)" << builderOpState << ".addRegion();\n";
|
2019-05-28 08:03:46 -07:00
|
|
|
}
|
2019-12-12 10:35:40 -08:00
|
|
|
|
|
|
|
// Result types
|
|
|
|
if (numVariadicResults == 0 || numNonVariadicResults != 0)
|
|
|
|
body << " assert(resultTypes.size()"
|
|
|
|
<< (numVariadicResults != 0 ? " >= " : " == ") << numNonVariadicResults
|
|
|
|
<< "u && \"mismatched number of return types\");\n";
|
|
|
|
body << " " << builderOpState << ".addTypes(resultTypes);\n";
|
|
|
|
|
|
|
|
// Generate builder that infers type too.
|
2021-12-10 15:51:02 +00:00
|
|
|
// TODO: Expand to handle successors.
|
2020-09-17 13:18:09 -07:00
|
|
|
if (canInferType(op) && op.getNumSuccessors() == 0)
|
2020-02-28 10:59:34 -08:00
|
|
|
genInferredTypeCollectiveParamBuilder();
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
void OpEmitter::buildParamList(SmallVectorImpl<MethodParameter> ¶mList,
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
llvm::StringSet<> &inferredAttributes,
|
2019-07-31 15:30:46 -07:00
|
|
|
SmallVectorImpl<std::string> &resultTypeNames,
|
2019-12-02 09:33:24 -08:00
|
|
|
TypeParamKind typeParamKind,
|
|
|
|
AttrParamKind attrParamKind) {
|
2019-08-21 05:35:07 -07:00
|
|
|
resultTypeNames.clear();
|
|
|
|
auto numResults = op.getNumResults();
|
|
|
|
resultTypeNames.reserve(numResults);
|
2019-07-31 15:30:46 -07:00
|
|
|
|
2022-07-08 11:31:12 -07:00
|
|
|
paramList.emplace_back("::mlir::OpBuilder &", odsBuilder);
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::mlir::OperationState &", builderOpState);
|
2019-07-31 15:30:46 -07:00
|
|
|
|
2019-12-02 09:33:24 -08:00
|
|
|
switch (typeParamKind) {
|
2019-08-21 05:35:07 -07:00
|
|
|
case TypeParamKind::None:
|
|
|
|
break;
|
|
|
|
case TypeParamKind::Separate: {
|
2019-07-31 15:30:46 -07:00
|
|
|
// Add parameters for all return types
|
|
|
|
for (int i = 0; i < numResults; ++i) {
|
|
|
|
const auto &result = op.getResult(i);
|
2020-01-28 20:23:46 +01:00
|
|
|
std::string resultName = std::string(result.name);
|
2019-07-31 15:30:46 -07:00
|
|
|
if (resultName.empty())
|
2020-01-28 20:23:46 +01:00
|
|
|
resultName = std::string(formatv("resultType{0}", i));
|
2019-07-31 15:30:46 -07:00
|
|
|
|
2020-09-22 20:58:30 -07:00
|
|
|
StringRef type =
|
|
|
|
result.isVariadic() ? "::mlir::TypeRange" : "::mlir::Type";
|
2019-07-31 15:30:46 -07:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
paramList.emplace_back(type, resultName, result.isOptional());
|
2019-07-31 15:30:46 -07:00
|
|
|
resultTypeNames.emplace_back(std::move(resultName));
|
|
|
|
}
|
2019-08-21 05:35:07 -07:00
|
|
|
} break;
|
|
|
|
case TypeParamKind::Collective: {
|
2020-09-22 20:58:30 -07:00
|
|
|
paramList.emplace_back("::mlir::TypeRange", "resultTypes");
|
2019-08-21 05:35:07 -07:00
|
|
|
resultTypeNames.push_back("resultTypes");
|
|
|
|
} break;
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
|
|
|
|
2019-12-02 09:33:24 -08:00
|
|
|
// Add parameters for all arguments (operands and attributes).
|
|
|
|
int defaultValuedAttrStartIndex = op.getNumArgs();
|
2021-09-22 20:44:02 +00:00
|
|
|
// Successors and variadic regions go at the end of the parameter list, so no
|
|
|
|
// default arguments are possible.
|
|
|
|
bool hasTrailingParams = op.getNumSuccessors() || op.getNumVariadicRegions();
|
|
|
|
if (attrParamKind == AttrParamKind::UnwrappedValue && !hasTrailingParams) {
|
2019-12-02 09:33:24 -08:00
|
|
|
// Calculate the start index from which we can attach default values in the
|
|
|
|
// builder declaration.
|
|
|
|
for (int i = op.getNumArgs() - 1; i >= 0; --i) {
|
|
|
|
auto *namedAttr = op.getArg(i).dyn_cast<tblgen::NamedAttribute *>();
|
|
|
|
if (!namedAttr || !namedAttr->attr.hasDefaultValue())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!canUseUnwrappedRawValue(namedAttr->attr))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Creating an APInt requires us to provide bitwidth, value, and
|
|
|
|
// signedness, which is complicated compared to others. Similarly
|
|
|
|
// for APFloat.
|
2020-07-07 01:35:23 -07:00
|
|
|
// TODO: Adjust the 'returnType' field of such attributes
|
2019-12-02 09:33:24 -08:00
|
|
|
// to support them.
|
|
|
|
StringRef retType = namedAttr->attr.getReturnType();
|
2020-06-26 13:20:44 +02:00
|
|
|
if (retType == "::llvm::APInt" || retType == "::llvm::APFloat")
|
2019-12-02 09:33:24 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
defaultValuedAttrStartIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
/// Collect any inferred attributes.
|
|
|
|
for (const NamedTypeConstraint &operand : op.getOperands()) {
|
|
|
|
if (operand.isVariadicOfVariadic()) {
|
|
|
|
inferredAttributes.insert(
|
|
|
|
operand.constraint.getVariadicOfVariadicSegmentSizeAttr());
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 13:18:09 -07:00
|
|
|
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
for (int i = 0, e = op.getNumArgs(), numOperands = 0; i < e; ++i) {
|
|
|
|
Argument arg = op.getArg(i);
|
|
|
|
if (const auto *operand = arg.dyn_cast<NamedTypeConstraint *>()) {
|
|
|
|
StringRef type;
|
|
|
|
if (operand->isVariadicOfVariadic())
|
|
|
|
type = "::llvm::ArrayRef<::mlir::ValueRange>";
|
|
|
|
else if (operand->isVariadic())
|
|
|
|
type = "::mlir::ValueRange";
|
|
|
|
else
|
|
|
|
type = "::mlir::Value";
|
2019-12-02 09:33:24 -08:00
|
|
|
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
paramList.emplace_back(type, getArgumentName(op, numOperands++),
|
2021-11-12 01:17:05 +00:00
|
|
|
operand->isOptional());
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const NamedAttribute &namedAttr = *arg.get<NamedAttribute *>();
|
|
|
|
const Attribute &attr = namedAttr.attr;
|
2019-12-02 09:33:24 -08:00
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
// Inferred attributes don't need to be added to the param list.
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
if (inferredAttributes.contains(namedAttr.name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
StringRef type;
|
|
|
|
switch (attrParamKind) {
|
|
|
|
case AttrParamKind::WrappedAttr:
|
|
|
|
type = attr.getStorageType();
|
|
|
|
break;
|
|
|
|
case AttrParamKind::UnwrappedValue:
|
|
|
|
if (canUseUnwrappedRawValue(attr))
|
|
|
|
type = attr.getReturnType();
|
|
|
|
else
|
2020-09-17 13:18:09 -07:00
|
|
|
type = attr.getStorageType();
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-12-02 09:33:24 -08:00
|
|
|
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
// Attach default value if requested and possible.
|
|
|
|
std::string defaultValue;
|
|
|
|
if (attrParamKind == AttrParamKind::UnwrappedValue &&
|
|
|
|
i >= defaultValuedAttrStartIndex) {
|
|
|
|
defaultValue += attr.getDefaultValue();
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
2021-11-30 14:09:00 +00:00
|
|
|
paramList.emplace_back(type, namedAttr.name, StringRef(defaultValue),
|
2021-11-12 01:17:05 +00:00
|
|
|
attr.isOptional());
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
2020-02-21 13:19:50 -08:00
|
|
|
|
2020-04-05 01:03:24 -07:00
|
|
|
/// Insert parameters for each successor.
|
2020-03-05 12:48:28 -08:00
|
|
|
for (const NamedSuccessor &succ : op.getSuccessors()) {
|
2020-09-22 21:06:25 -07:00
|
|
|
StringRef type =
|
|
|
|
succ.isVariadic() ? "::mlir::BlockRange" : "::mlir::Block *";
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back(type, succ.name);
|
2020-02-21 13:19:50 -08:00
|
|
|
}
|
2020-04-05 01:03:24 -07:00
|
|
|
|
|
|
|
/// Insert parameters for variadic regions.
|
2020-09-17 13:18:09 -07:00
|
|
|
for (const NamedRegion ®ion : op.getRegions())
|
2020-04-05 01:03:24 -07:00
|
|
|
if (region.isVariadic())
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("unsigned",
|
|
|
|
llvm::formatv("{0}Count", region.name).str());
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
|
|
|
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
void OpEmitter::genCodeForAddingArgAndRegionForBuilder(
|
2021-11-12 01:17:05 +00:00
|
|
|
MethodBody &body, llvm::StringSet<> &inferredAttributes,
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
bool isRawValueAttr) {
|
2020-02-21 13:19:50 -08:00
|
|
|
// Push all operands to the result.
|
2019-07-31 15:30:46 -07:00
|
|
|
for (int i = 0, e = op.getNumOperands(); i < e; ++i) {
|
2020-04-10 14:11:45 -07:00
|
|
|
std::string argName = getArgumentName(op, i);
|
2022-04-08 16:41:31 +00:00
|
|
|
const NamedTypeConstraint &operand = op.getOperand(i);
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
if (operand.constraint.isVariadicOfVariadic()) {
|
|
|
|
body << " for (::mlir::ValueRange range : " << argName << ")\n "
|
|
|
|
<< builderOpState << ".addOperands(range);\n";
|
|
|
|
|
|
|
|
// Add the segment attribute.
|
|
|
|
body << " {\n"
|
2022-01-01 14:52:32 +01:00
|
|
|
<< " ::llvm::SmallVector<int32_t> rangeSegments;\n"
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
<< " for (::mlir::ValueRange range : " << argName << ")\n"
|
|
|
|
<< " rangeSegments.push_back(range.size());\n"
|
|
|
|
<< " " << builderOpState << ".addAttribute("
|
2021-10-20 07:08:36 -07:00
|
|
|
<< op.getGetterName(
|
|
|
|
operand.constraint.getVariadicOfVariadicSegmentSizeAttr())
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
<< "AttrName(" << builderOpState << ".name), " << odsBuilder
|
2022-08-12 15:43:03 -04:00
|
|
|
<< ".getDenseI32ArrayAttr(rangeSegments));"
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
<< " }\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operand.isOptional())
|
2020-04-10 14:11:45 -07:00
|
|
|
body << " if (" << argName << ")\n ";
|
|
|
|
body << " " << builderOpState << ".addOperands(" << argName << ");\n";
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
2019-05-28 08:03:46 -07:00
|
|
|
|
2020-03-05 12:41:56 -08:00
|
|
|
// If the operation has the operand segment size attribute, add it here.
|
2020-09-14 20:01:07 +00:00
|
|
|
if (op.getTrait("::mlir::OpTrait::AttrSizedOperandSegments")) {
|
2022-04-08 16:41:31 +00:00
|
|
|
std::string sizes = op.getGetterName(operandSegmentAttrName);
|
2021-10-20 07:08:36 -07:00
|
|
|
body << " " << builderOpState << ".addAttribute(" << sizes << "AttrName("
|
|
|
|
<< builderOpState << ".name), "
|
2022-08-12 15:43:03 -04:00
|
|
|
<< "odsBuilder.getDenseI32ArrayAttr({";
|
2020-04-16 08:05:21 -07:00
|
|
|
interleaveComma(llvm::seq<int>(0, op.getNumOperands()), body, [&](int i) {
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
const NamedTypeConstraint &operand = op.getOperand(i);
|
|
|
|
if (!operand.isVariableLength()) {
|
2020-04-16 08:05:21 -07:00
|
|
|
body << "1";
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string operandName = getArgumentName(op, i);
|
|
|
|
if (operand.isOptional()) {
|
|
|
|
body << "(" << operandName << " ? 1 : 0)";
|
|
|
|
} else if (operand.isVariadicOfVariadic()) {
|
|
|
|
body << llvm::formatv(
|
|
|
|
"static_cast<int32_t>(std::accumulate({0}.begin(), {0}.end(), 0, "
|
|
|
|
"[](int32_t curSum, ::mlir::ValueRange range) {{ return curSum + "
|
|
|
|
"range.size(); }))",
|
|
|
|
operandName);
|
|
|
|
} else {
|
|
|
|
body << "static_cast<int32_t>(" << getArgumentName(op, i) << ".size())";
|
|
|
|
}
|
2020-04-16 08:05:21 -07:00
|
|
|
});
|
2020-03-05 12:41:56 -08:00
|
|
|
body << "}));\n";
|
|
|
|
}
|
|
|
|
|
2020-02-21 13:19:50 -08:00
|
|
|
// Push all attributes to the result.
|
2019-07-31 15:30:46 -07:00
|
|
|
for (const auto &namedAttr : op.getAttributes()) {
|
2019-12-02 09:33:24 -08:00
|
|
|
auto &attr = namedAttr.attr;
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
if (attr.isDerivedAttr() || inferredAttributes.contains(namedAttr.name))
|
|
|
|
continue;
|
|
|
|
|
2022-08-17 17:00:47 -07:00
|
|
|
// TODO(jpienaar): The wrapping of optional is different for default or not,
|
|
|
|
// so don't unwrap for default ones that would fail below.
|
|
|
|
bool emitNotNullCheck = (attr.isOptional() && !attr.hasDefaultValue()) ||
|
|
|
|
(attr.hasDefaultValue() && !isRawValueAttr);
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
if (emitNotNullCheck)
|
|
|
|
body << formatv(" if ({0}) ", namedAttr.name) << "{\n";
|
|
|
|
|
|
|
|
if (isRawValueAttr && canUseUnwrappedRawValue(attr)) {
|
|
|
|
// If this is a raw value, then we need to wrap it in an Attribute
|
|
|
|
// instance.
|
|
|
|
FmtContext fctx;
|
|
|
|
fctx.withBuilder("odsBuilder");
|
|
|
|
|
|
|
|
std::string builderTemplate = std::string(attr.getConstBuilderTemplate());
|
|
|
|
|
|
|
|
// For StringAttr, its constant builder call will wrap the input in
|
|
|
|
// quotes, which is correct for normal string literals, but incorrect
|
|
|
|
// here given we use function arguments. So we need to strip the
|
|
|
|
// wrapping quotes.
|
|
|
|
if (StringRef(builderTemplate).contains("\"$0\""))
|
|
|
|
builderTemplate = replaceAllSubstrs(builderTemplate, "\"$0\"", "$0");
|
|
|
|
|
|
|
|
std::string value =
|
|
|
|
std::string(tgfmt(builderTemplate, &fctx, namedAttr.name));
|
|
|
|
body << formatv(" {0}.addAttribute({1}AttrName({0}.name), {2});\n",
|
2021-10-20 07:08:36 -07:00
|
|
|
builderOpState, op.getGetterName(namedAttr.name), value);
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
} else {
|
2021-10-14 15:58:44 -07:00
|
|
|
body << formatv(" {0}.addAttribute({1}AttrName({0}.name), {2});\n",
|
2021-10-20 07:08:36 -07:00
|
|
|
builderOpState, op.getGetterName(namedAttr.name),
|
2021-10-14 15:58:44 -07:00
|
|
|
namedAttr.name);
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
if (emitNotNullCheck)
|
|
|
|
body << " }\n";
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
2019-02-14 10:54:50 -08:00
|
|
|
|
2020-02-21 13:19:50 -08:00
|
|
|
// Create the correct number of regions.
|
2020-04-05 01:03:24 -07:00
|
|
|
for (const NamedRegion ®ion : op.getRegions()) {
|
|
|
|
if (region.isVariadic())
|
|
|
|
body << formatv(" for (unsigned i = 0; i < {0}Count; ++i)\n ",
|
|
|
|
region.name);
|
|
|
|
|
|
|
|
body << " (void)" << builderOpState << ".addRegion();\n";
|
2019-07-31 15:30:46 -07:00
|
|
|
}
|
2020-02-21 13:19:50 -08:00
|
|
|
|
|
|
|
// Push all successors to the result.
|
|
|
|
for (const NamedSuccessor &namedSuccessor : op.getSuccessors()) {
|
2020-03-05 12:48:28 -08:00
|
|
|
body << formatv(" {0}.addSuccessors({1});\n", builderOpState,
|
2020-02-21 13:19:50 -08:00
|
|
|
namedSuccessor.name);
|
|
|
|
}
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genCanonicalizerDecls() {
|
2021-03-22 22:15:39 -07:00
|
|
|
bool hasCanonicalizeMethod = def.getValueAsBit("hasCanonicalizeMethod");
|
|
|
|
if (hasCanonicalizeMethod) {
|
|
|
|
// static LogicResult FooOp::
|
|
|
|
// canonicalize(FooOp op, PatternRewriter &rewriter);
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
2021-03-22 22:15:39 -07:00
|
|
|
paramList.emplace_back(op.getCppClassName(), "op");
|
|
|
|
paramList.emplace_back("::mlir::PatternRewriter &", "rewriter");
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.declareStaticMethod("::mlir::LogicalResult",
|
|
|
|
"canonicalize", std::move(paramList));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, "canonicalize", op);
|
2021-03-22 22:15:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We get a prototype for 'getCanonicalizationPatterns' if requested directly
|
|
|
|
// or if using a 'canonicalize' method.
|
|
|
|
bool hasCanonicalizer = def.getValueAsBit("hasCanonicalizer");
|
|
|
|
if (!hasCanonicalizeMethod && !hasCanonicalizer)
|
2018-12-03 09:16:59 -08:00
|
|
|
return;
|
2019-03-20 17:25:34 -07:00
|
|
|
|
2021-03-22 22:15:39 -07:00
|
|
|
// We get a body for 'getCanonicalizationPatterns' when using a 'canonicalize'
|
|
|
|
// method, but not implementing 'getCanonicalizationPatterns' manually.
|
|
|
|
bool hasBody = hasCanonicalizeMethod && !hasCanonicalizer;
|
|
|
|
|
|
|
|
// Add a signature for getCanonicalizationPatterns if implemented by the
|
|
|
|
// dialect or if synthesized to call 'canonicalize'.
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
2021-03-22 16:58:34 -07:00
|
|
|
paramList.emplace_back("::mlir::RewritePatternSet &", "results");
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::mlir::MLIRContext *", "context");
|
2021-11-30 14:09:00 +00:00
|
|
|
auto kind = hasBody ? Method::Static : Method::StaticDeclaration;
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *method = opClass.addMethod("void", "getCanonicalizationPatterns", kind,
|
|
|
|
std::move(paramList));
|
2021-03-22 22:15:39 -07:00
|
|
|
|
|
|
|
// If synthesizing the method, fill it it.
|
2021-10-14 15:58:44 -07:00
|
|
|
if (hasBody) {
|
|
|
|
ERROR_IF_PRUNED(method, "getCanonicalizationPatterns", op);
|
2021-03-22 22:15:39 -07:00
|
|
|
method->body() << " results.add(canonicalize);\n";
|
2021-10-14 15:58:44 -07:00
|
|
|
}
|
2018-12-03 09:16:59 -08:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genFolderDecls() {
|
2019-09-30 18:43:58 -07:00
|
|
|
bool hasSingleResult =
|
2020-04-10 14:11:45 -07:00
|
|
|
op.getNumResults() == 1 && op.getNumVariableLengthResults() == 0;
|
2019-03-20 17:25:34 -07:00
|
|
|
|
2019-01-24 12:34:00 -08:00
|
|
|
if (def.getValueAsBit("hasFolder")) {
|
|
|
|
if (hasSingleResult) {
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.declareMethod(
|
|
|
|
"::mlir::OpFoldResult", "fold",
|
|
|
|
MethodParameter("::llvm::ArrayRef<::mlir::Attribute>", "operands"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, "operands", op);
|
2019-01-24 12:34:00 -08:00
|
|
|
} else {
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::llvm::ArrayRef<::mlir::Attribute>", "operands");
|
|
|
|
paramList.emplace_back("::llvm::SmallVectorImpl<::mlir::OpFoldResult> &",
|
|
|
|
"results");
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = opClass.declareMethod("::mlir::LogicalResult", "fold",
|
|
|
|
std::move(paramList));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, "fold", op);
|
2019-01-24 12:34:00 -08:00
|
|
|
}
|
2019-01-04 01:34:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 11:29:23 -07:00
|
|
|
void OpEmitter::genOpInterfaceMethods(const tblgen::InterfaceTrait *opTrait) {
|
|
|
|
Interface interface = opTrait->getInterface();
|
2020-05-27 08:45:55 -07:00
|
|
|
|
|
|
|
// Get the set of methods that should always be declared.
|
|
|
|
auto alwaysDeclaredMethodsVec = opTrait->getAlwaysDeclaredMethods();
|
|
|
|
llvm::StringSet<> alwaysDeclaredMethods;
|
|
|
|
alwaysDeclaredMethods.insert(alwaysDeclaredMethodsVec.begin(),
|
|
|
|
alwaysDeclaredMethodsVec.end());
|
|
|
|
|
2020-06-30 15:42:52 -07:00
|
|
|
for (const InterfaceMethod &method : interface.getMethods()) {
|
2020-05-27 08:45:55 -07:00
|
|
|
// Don't declare if the method has a body.
|
|
|
|
if (method.getBody())
|
|
|
|
continue;
|
|
|
|
// Don't declare if the method has a default implementation and the op
|
|
|
|
// didn't request that it always be declared.
|
|
|
|
if (method.getDefaultImplementation() &&
|
|
|
|
!alwaysDeclaredMethods.count(method.getName()))
|
|
|
|
continue;
|
2021-10-14 15:58:44 -07:00
|
|
|
// Interface methods are allowed to overlap with existing methods, so don't
|
|
|
|
// check if pruned.
|
|
|
|
(void)genOpInterfaceMethod(method);
|
2020-05-27 08:45:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
Method *OpEmitter::genOpInterfaceMethod(const InterfaceMethod &method,
|
|
|
|
bool declaration) {
|
|
|
|
SmallVector<MethodParameter> paramList;
|
2020-12-03 16:18:11 -08:00
|
|
|
for (const InterfaceMethod::Argument &arg : method.getArguments())
|
|
|
|
paramList.emplace_back(arg.type, arg.name);
|
|
|
|
|
2021-11-30 14:09:00 +00:00
|
|
|
auto props = (method.isStatic() ? Method::Static : Method::None) |
|
|
|
|
(declaration ? Method::Declaration : Method::None);
|
|
|
|
return opClass.addMethod(method.getReturnType(), method.getName(), props,
|
2021-11-12 01:17:05 +00:00
|
|
|
std::move(paramList));
|
2020-12-03 16:18:11 -08:00
|
|
|
}
|
|
|
|
|
2019-09-30 12:42:31 -07:00
|
|
|
void OpEmitter::genOpInterfaceMethods() {
|
|
|
|
for (const auto &trait : op.getTraits()) {
|
2021-04-15 11:29:23 -07:00
|
|
|
if (const auto *opTrait = dyn_cast<tblgen::InterfaceTrait>(&trait))
|
2020-05-27 08:45:55 -07:00
|
|
|
if (opTrait->shouldDeclareMethods())
|
2020-12-03 16:18:11 -08:00
|
|
|
genOpInterfaceMethods(opTrait);
|
2019-09-30 12:42:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 13:55:36 -08:00
|
|
|
void OpEmitter::genSideEffectInterfaceMethods() {
|
2020-11-18 18:31:40 -08:00
|
|
|
enum EffectKind { Operand, Result, Symbol, Static };
|
2020-03-06 13:55:36 -08:00
|
|
|
struct EffectLocation {
|
|
|
|
/// The effect applied.
|
|
|
|
SideEffect effect;
|
|
|
|
|
2020-11-18 18:31:40 -08:00
|
|
|
/// The index if the kind is not static.
|
2021-11-30 14:09:00 +00:00
|
|
|
unsigned index;
|
2020-03-06 13:55:36 -08:00
|
|
|
|
|
|
|
/// The kind of the location.
|
2021-11-30 14:09:00 +00:00
|
|
|
unsigned kind;
|
2020-03-06 13:55:36 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
StringMap<SmallVector<EffectLocation, 1>> interfaceEffects;
|
|
|
|
auto resolveDecorators = [&](Operator::var_decorator_range decorators,
|
2020-03-06 23:01:49 -08:00
|
|
|
unsigned index, unsigned kind) {
|
2020-03-06 13:55:36 -08:00
|
|
|
for (auto decorator : decorators)
|
[MLIR] Propagate input side effect information
Summary:
Previously operations like std.load created methods for obtaining their
effects but did not inherit from the SideEffect interfaces when their
parameters were decorated with the information. The resulting situation
was that passes had no information on the SideEffects of std.load/store
and had to treat them more cautiously. This adds the inheritance
information when creating the methods.
As a side effect, many tests are modified, as they were using std.load
for testing and this oepration would be folded away as part of pattern
rewriting. Tests are modified to use store or to reutn the result of the
std.load.
Reviewers: mravishankar, antiagainst, nicolasvasilache, herhut, aartbik, ftynse!
Subscribers: mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, csigg, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, bader, grosul1, frgossen, Kayjukh, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78802
2020-04-23 18:13:44 +02:00
|
|
|
if (SideEffect *effect = dyn_cast<SideEffect>(&decorator)) {
|
|
|
|
opClass.addTrait(effect->getInterfaceTrait());
|
2020-03-12 14:06:41 -07:00
|
|
|
interfaceEffects[effect->getBaseEffectName()].push_back(
|
2020-03-06 13:55:36 -08:00
|
|
|
EffectLocation{*effect, index, kind});
|
[MLIR] Propagate input side effect information
Summary:
Previously operations like std.load created methods for obtaining their
effects but did not inherit from the SideEffect interfaces when their
parameters were decorated with the information. The resulting situation
was that passes had no information on the SideEffects of std.load/store
and had to treat them more cautiously. This adds the inheritance
information when creating the methods.
As a side effect, many tests are modified, as they were using std.load
for testing and this oepration would be folded away as part of pattern
rewriting. Tests are modified to use store or to reutn the result of the
std.load.
Reviewers: mravishankar, antiagainst, nicolasvasilache, herhut, aartbik, ftynse!
Subscribers: mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, csigg, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, bader, grosul1, frgossen, Kayjukh, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78802
2020-04-23 18:13:44 +02:00
|
|
|
}
|
2020-03-06 13:55:36 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Collect effects that were specified via:
|
|
|
|
/// Traits.
|
2020-03-12 14:06:41 -07:00
|
|
|
for (const auto &trait : op.getTraits()) {
|
|
|
|
const auto *opTrait = dyn_cast<tblgen::SideEffectTrait>(&trait);
|
|
|
|
if (!opTrait)
|
|
|
|
continue;
|
|
|
|
auto &effects = interfaceEffects[opTrait->getBaseEffectName()];
|
|
|
|
for (auto decorator : opTrait->getEffects())
|
|
|
|
effects.push_back(EffectLocation{cast<SideEffect>(decorator),
|
|
|
|
/*index=*/0, EffectKind::Static});
|
|
|
|
}
|
2020-11-18 18:31:40 -08:00
|
|
|
/// Attributes and Operands.
|
2020-03-06 13:55:36 -08:00
|
|
|
for (unsigned i = 0, operandIt = 0, e = op.getNumArgs(); i != e; ++i) {
|
2020-11-18 18:31:40 -08:00
|
|
|
Argument arg = op.getArg(i);
|
|
|
|
if (arg.is<NamedTypeConstraint *>()) {
|
2020-03-06 13:55:36 -08:00
|
|
|
resolveDecorators(op.getArgDecorators(i), operandIt, EffectKind::Operand);
|
|
|
|
++operandIt;
|
2020-11-18 18:31:40 -08:00
|
|
|
continue;
|
2020-03-06 13:55:36 -08:00
|
|
|
}
|
2020-11-18 18:31:40 -08:00
|
|
|
const NamedAttribute *attr = arg.get<NamedAttribute *>();
|
|
|
|
if (attr->attr.getBaseAttr().isSymbolRefAttr())
|
|
|
|
resolveDecorators(op.getArgDecorators(i), i, EffectKind::Symbol);
|
2020-03-06 13:55:36 -08:00
|
|
|
}
|
|
|
|
/// Results.
|
|
|
|
for (unsigned i = 0, e = op.getNumResults(); i != e; ++i)
|
|
|
|
resolveDecorators(op.getResultDecorators(i), i, EffectKind::Result);
|
|
|
|
|
2020-11-18 18:31:40 -08:00
|
|
|
// The code used to add an effect instance.
|
|
|
|
// {0}: The effect class.
|
|
|
|
// {1}: Optional value or symbol reference.
|
|
|
|
// {1}: The resource class.
|
|
|
|
const char *addEffectCode =
|
|
|
|
" effects.emplace_back({0}::get(), {1}{2}::get());\n";
|
|
|
|
|
2020-03-06 13:55:36 -08:00
|
|
|
for (auto &it : interfaceEffects) {
|
|
|
|
// Generate the 'getEffects' method.
|
2022-05-24 15:03:12 +00:00
|
|
|
std::string type = llvm::formatv("::llvm::SmallVectorImpl<::mlir::"
|
2020-09-17 13:18:09 -07:00
|
|
|
"SideEffects::EffectInstance<{0}>> &",
|
|
|
|
it.first())
|
|
|
|
.str();
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *getEffects = opClass.addMethod("void", "getEffects",
|
|
|
|
MethodParameter(type, "effects"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(getEffects, "getEffects", op);
|
2020-09-17 13:18:09 -07:00
|
|
|
auto &body = getEffects->body();
|
2020-03-06 13:55:36 -08:00
|
|
|
|
|
|
|
// Add effect instances for each of the locations marked on the operation.
|
|
|
|
for (auto &location : it.second) {
|
2020-11-18 18:31:40 -08:00
|
|
|
StringRef effect = location.effect.getName();
|
|
|
|
StringRef resource = location.effect.getResource();
|
|
|
|
if (location.kind == EffectKind::Static) {
|
|
|
|
// A static instance has no attached value.
|
|
|
|
body << llvm::formatv(addEffectCode, effect, "", resource).str();
|
|
|
|
} else if (location.kind == EffectKind::Symbol) {
|
|
|
|
// A symbol reference requires adding the proper attribute.
|
|
|
|
const auto *attr = op.getArg(location.index).get<NamedAttribute *>();
|
2022-01-13 19:56:51 +01:00
|
|
|
std::string argName = op.getGetterName(attr->name);
|
2020-11-18 18:31:40 -08:00
|
|
|
if (attr->attr.isOptional()) {
|
2022-01-13 19:56:51 +01:00
|
|
|
body << " if (auto symbolRef = " << argName << "Attr())\n "
|
2020-11-18 18:31:40 -08:00
|
|
|
<< llvm::formatv(addEffectCode, effect, "symbolRef, ", resource)
|
|
|
|
.str();
|
|
|
|
} else {
|
2022-01-13 19:56:51 +01:00
|
|
|
body << llvm::formatv(addEffectCode, effect, argName + "Attr(), ",
|
2020-11-18 18:31:40 -08:00
|
|
|
resource)
|
|
|
|
.str();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise this is an operand/result, so we need to attach the Value.
|
2020-06-26 13:20:44 +02:00
|
|
|
body << " for (::mlir::Value value : getODS"
|
2020-03-06 13:55:36 -08:00
|
|
|
<< (location.kind == EffectKind::Operand ? "Operands" : "Results")
|
2020-11-18 18:31:40 -08:00
|
|
|
<< "(" << location.index << "))\n "
|
|
|
|
<< llvm::formatv(addEffectCode, effect, "value, ", resource).str();
|
2020-03-06 13:55:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 08:45:55 -07:00
|
|
|
void OpEmitter::genTypeInterfaceMethods() {
|
|
|
|
if (!op.allResultTypesKnown())
|
|
|
|
return;
|
2020-12-03 16:18:11 -08:00
|
|
|
// Generate 'inferReturnTypes' method declaration using the interface method
|
|
|
|
// declared in 'InferTypeOpInterface' op interface.
|
2022-01-03 06:06:36 +00:00
|
|
|
const auto *trait =
|
|
|
|
cast<InterfaceTrait>(op.getTrait("::mlir::InferTypeOpInterface::Trait"));
|
2021-04-15 11:29:23 -07:00
|
|
|
Interface interface = trait->getInterface();
|
2021-11-12 01:17:05 +00:00
|
|
|
Method *method = [&]() -> Method * {
|
2020-12-03 16:18:11 -08:00
|
|
|
for (const InterfaceMethod &interfaceMethod : interface.getMethods()) {
|
|
|
|
if (interfaceMethod.getName() == "inferReturnTypes") {
|
|
|
|
return genOpInterfaceMethod(interfaceMethod, /*declaration=*/false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(0 && "unable to find inferReturnTypes interface method");
|
|
|
|
return nullptr;
|
|
|
|
}();
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, "inferReturnTypes", op);
|
2020-09-17 13:18:09 -07:00
|
|
|
auto &body = method->body();
|
|
|
|
body << " inferredReturnTypes.resize(" << op.getNumResults() << ");\n";
|
2020-05-27 08:45:55 -07:00
|
|
|
|
|
|
|
FmtContext fctx;
|
|
|
|
fctx.withBuilder("odsBuilder");
|
2020-09-17 13:18:09 -07:00
|
|
|
body << " ::mlir::Builder odsBuilder(context);\n";
|
2020-05-27 08:45:55 -07:00
|
|
|
|
2022-04-26 11:00:35 -07:00
|
|
|
// Preprocess the result types and build all of the types used during
|
|
|
|
// inferrence. This limits the amount of duplicated work when a type is used
|
|
|
|
// to infer multiple others.
|
|
|
|
llvm::DenseMap<Constraint, int> constraintsTypes;
|
|
|
|
llvm::DenseMap<int, int> argumentsTypes;
|
|
|
|
int inferredTypeIdx = 0;
|
|
|
|
for (int i = 0, e = op.getNumResults(); i != e; ++i) {
|
|
|
|
auto type = op.getSameTypeAsResult(i).front();
|
|
|
|
|
|
|
|
// If the type isn't an argument, it refers to a buildable type.
|
|
|
|
if (!type.isArg()) {
|
|
|
|
auto it = constraintsTypes.try_emplace(type.getType(), inferredTypeIdx);
|
|
|
|
if (!it.second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If we haven't seen this constraint, generate a variable for it.
|
|
|
|
body << " ::mlir::Type odsInferredType" << inferredTypeIdx++ << " = "
|
|
|
|
<< tgfmt(*type.getType().getBuilderCall(), &fctx) << ";\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this is an argument.
|
|
|
|
int argIndex = type.getArg();
|
|
|
|
auto it = argumentsTypes.try_emplace(argIndex, inferredTypeIdx);
|
|
|
|
if (!it.second)
|
|
|
|
continue;
|
|
|
|
body << " ::mlir::Type odsInferredType" << inferredTypeIdx++ << " = ";
|
|
|
|
|
|
|
|
// If this is an operand, just index into operand list to access the type.
|
2021-11-09 18:59:06 +00:00
|
|
|
auto arg = op.getArgToOperandOrAttribute(argIndex);
|
2022-04-26 11:00:35 -07:00
|
|
|
if (arg.kind() == Operator::OperandOrAttribute::Kind::Operand) {
|
|
|
|
body << "operands[" << arg.operandOrAttributeIndex() << "].getType()";
|
|
|
|
|
|
|
|
// If this is an attribute, index into the attribute dictionary.
|
|
|
|
} else {
|
|
|
|
auto *attr =
|
|
|
|
op.getArg(arg.operandOrAttributeIndex()).get<NamedAttribute *>();
|
[mlir] Remove types from attributes
This patch removes the `type` field from `Attribute` along with the
`Attribute::getType` accessor.
Going forward, this means that attributes in MLIR will no longer have
types as a first-class concept. This patch lays the groundwork to
incrementally remove or refactor code that relies on generic attributes
being typed. The immediate impact will be on attributes that rely on
`Attribute` containing a type, such as `IntegerAttr`,
`DenseElementsAttr`, and `ml_program::ExternAttr`, which will now need
to define a type parameter on their storage classes. This will save
memory as all other attribute kinds will no longer contain a type.
Moreover, it will not be possible to generically query the type of an
attribute directly. This patch provides an attribute interface
`TypedAttr` that implements only one method, `getType`, which can be
used to generically query the types of attributes that implement the
interface. This interface can be used to retain the concept of a "typed
attribute". The ODS-generated accessor for a `type` parameter
automatically implements this method.
Next steps will be to refactor the assembly formats of certain operations
that rely on `parseAttribute(type)` and `printAttributeWithoutType` to
remove special handling of type elision until `type` can be removed from
the dialect parsing hook entirely; and incrementally remove uses of
`TypedAttr`.
Reviewed By: lattner, rriddle, jpienaar
Differential Revision: https://reviews.llvm.org/D130092
2022-07-18 21:32:38 -07:00
|
|
|
body << "attributes.get(\"" << attr->name
|
|
|
|
<< "\").cast<::mlir::TypedAttr>().getType()";
|
2022-04-26 11:00:35 -07:00
|
|
|
}
|
|
|
|
body << ";\n";
|
|
|
|
}
|
2020-05-27 08:45:55 -07:00
|
|
|
|
2022-04-26 11:00:35 -07:00
|
|
|
// Perform a second pass that handles assigning the inferred types to the
|
|
|
|
// results.
|
2020-05-27 08:45:55 -07:00
|
|
|
for (int i = 0, e = op.getNumResults(); i != e; ++i) {
|
|
|
|
auto types = op.getSameTypeAsResult(i);
|
2022-04-26 11:00:35 -07:00
|
|
|
|
|
|
|
// Append the inferred type.
|
|
|
|
auto type = types.front();
|
|
|
|
body << " inferredReturnTypes[" << i << "] = odsInferredType"
|
|
|
|
<< (type.isArg() ? argumentsTypes[type.getArg()]
|
|
|
|
: constraintsTypes[type.getType()])
|
|
|
|
<< ";\n";
|
|
|
|
|
2020-05-27 08:45:55 -07:00
|
|
|
if (types.size() == 1)
|
|
|
|
continue;
|
|
|
|
// TODO: We could verify equality here, but skipping that for verification.
|
|
|
|
}
|
2020-09-17 13:18:09 -07:00
|
|
|
body << " return ::mlir::success();";
|
2020-05-27 08:45:55 -07:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genParser() {
|
2022-02-04 20:47:01 -08:00
|
|
|
if (hasStringAttribute(def, "assemblyFormat"))
|
|
|
|
return;
|
|
|
|
|
2022-03-07 01:33:58 -08:00
|
|
|
if (!def.getValueAsBit("hasCustomAssemblyFormat"))
|
2018-10-15 08:54:37 -07:00
|
|
|
return;
|
2019-03-20 17:25:34 -07:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::mlir::OpAsmParser &", "parser");
|
|
|
|
paramList.emplace_back("::mlir::OperationState &", "result");
|
2022-02-04 20:47:01 -08:00
|
|
|
|
2022-03-07 01:33:58 -08:00
|
|
|
auto *method = opClass.declareStaticMethod("::mlir::ParseResult", "parse",
|
|
|
|
std::move(paramList));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, "parse", op);
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genPrinter() {
|
2020-01-30 11:30:23 -08:00
|
|
|
if (hasStringAttribute(def, "assemblyFormat"))
|
|
|
|
return;
|
|
|
|
|
2022-03-07 01:33:58 -08:00
|
|
|
// Check to see if this op uses a c++ format.
|
|
|
|
if (!def.getValueAsBit("hasCustomAssemblyFormat"))
|
2018-10-15 08:54:37 -07:00
|
|
|
return;
|
2022-03-07 01:33:58 -08:00
|
|
|
auto *method = opClass.declareMethod(
|
2021-11-12 01:17:05 +00:00
|
|
|
"void", "print", MethodParameter("::mlir::OpAsmPrinter &", "p"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, "print", op);
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genVerifier() {
|
2022-02-25 18:17:30 +00:00
|
|
|
auto *implMethod =
|
|
|
|
opClass.addMethod("::mlir::LogicalResult", "verifyInvariantsImpl");
|
|
|
|
ERROR_IF_PRUNED(implMethod, "verifyInvariantsImpl", op);
|
|
|
|
auto &implBody = implMethod->body();
|
2021-11-03 17:06:14 +00:00
|
|
|
|
2021-11-09 18:59:06 +00:00
|
|
|
populateSubstitutions(emitHelper, verifyCtx);
|
2022-02-25 18:17:30 +00:00
|
|
|
genAttributeVerifier(emitHelper, verifyCtx, implBody, staticVerifierEmitter);
|
|
|
|
genOperandResultVerifier(implBody, op.getOperands(), "operand");
|
|
|
|
genOperandResultVerifier(implBody, op.getResults(), "result");
|
2019-01-05 08:11:29 -08:00
|
|
|
|
2019-02-20 10:50:26 -08:00
|
|
|
for (auto &trait : op.getTraits()) {
|
2021-04-15 11:29:23 -07:00
|
|
|
if (auto *t = dyn_cast<tblgen::PredTrait>(&trait)) {
|
2022-02-25 18:17:30 +00:00
|
|
|
implBody << tgfmt(" if (!($0))\n "
|
|
|
|
"return emitOpError(\"failed to verify that $1\");\n",
|
|
|
|
&verifyCtx, tgfmt(t->getPredTemplate(), &verifyCtx),
|
|
|
|
t->getSummary());
|
2019-02-20 10:50:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:17:30 +00:00
|
|
|
genRegionVerifier(implBody);
|
|
|
|
genSuccessorVerifier(implBody);
|
|
|
|
|
|
|
|
implBody << " return ::mlir::success();\n";
|
|
|
|
|
|
|
|
// TODO: Some places use the `verifyInvariants` to do operation verification.
|
|
|
|
// This may not act as their expectation because this doesn't call any
|
|
|
|
// verifiers of native/interface traits. Needs to review those use cases and
|
|
|
|
// see if we should use the mlir::verify() instead.
|
|
|
|
auto *method = opClass.addMethod("::mlir::LogicalResult", "verifyInvariants");
|
|
|
|
ERROR_IF_PRUNED(method, "verifyInvariants", op);
|
|
|
|
auto &body = method->body();
|
2022-03-07 01:33:58 -08:00
|
|
|
if (def.getValueAsBit("hasVerifier")) {
|
2022-02-25 18:17:30 +00:00
|
|
|
body << " if(::mlir::succeeded(verifyInvariantsImpl()) && "
|
|
|
|
"::mlir::succeeded(verify()))\n";
|
|
|
|
body << " return ::mlir::success();\n";
|
|
|
|
body << " return ::mlir::failure();";
|
|
|
|
} else {
|
|
|
|
body << " return verifyInvariantsImpl();";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpEmitter::genCustomVerifier() {
|
2022-02-01 15:01:30 -08:00
|
|
|
if (def.getValueAsBit("hasVerifier")) {
|
2022-02-25 18:17:30 +00:00
|
|
|
auto *method = opClass.declareMethod("::mlir::LogicalResult", "verify");
|
2022-02-01 15:01:30 -08:00
|
|
|
ERROR_IF_PRUNED(method, "verify", op);
|
2019-08-14 10:30:30 -07:00
|
|
|
}
|
2022-03-10 22:10:45 +00:00
|
|
|
|
|
|
|
if (def.getValueAsBit("hasRegionVerifier")) {
|
|
|
|
auto *method =
|
|
|
|
opClass.declareMethod("::mlir::LogicalResult", "verifyRegions");
|
|
|
|
ERROR_IF_PRUNED(method, "verifyRegions", op);
|
|
|
|
}
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
void OpEmitter::genOperandResultVerifier(MethodBody &body,
|
2022-01-18 06:47:33 +00:00
|
|
|
Operator::const_value_range values,
|
2019-06-09 07:00:09 -07:00
|
|
|
StringRef valueKind) {
|
2021-11-11 22:08:54 +00:00
|
|
|
// Check that an optional value is at most 1 element.
|
|
|
|
//
|
|
|
|
// {0}: Value index.
|
|
|
|
// {1}: "operand" or "result"
|
|
|
|
const char *const verifyOptional = R"(
|
|
|
|
if (valueGroup{0}.size() > 1) {
|
|
|
|
return emitOpError("{1} group starting at #") << index
|
|
|
|
<< " requires 0 or 1 element, but found " << valueGroup{0}.size();
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
// Check the types of a range of values.
|
|
|
|
//
|
|
|
|
// {0}: Value index.
|
|
|
|
// {1}: Type constraint function.
|
|
|
|
// {2}: "operand" or "result"
|
|
|
|
const char *const verifyValues = R"(
|
|
|
|
for (auto v : valueGroup{0}) {
|
|
|
|
if (::mlir::failed({1}(*this, v.getType(), "{2}", index++)))
|
|
|
|
return ::mlir::failure();
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
const auto canSkip = [](const NamedTypeConstraint &value) {
|
|
|
|
return !value.hasPredicate() && !value.isOptional() &&
|
|
|
|
!value.isVariadicOfVariadic();
|
|
|
|
};
|
|
|
|
if (values.empty() || llvm::all_of(values, canSkip))
|
|
|
|
return;
|
|
|
|
|
2019-06-09 07:00:09 -07:00
|
|
|
FmtContext fctx;
|
2019-06-12 12:08:13 -07:00
|
|
|
|
2021-11-11 22:08:54 +00:00
|
|
|
body << " {\n unsigned index = 0; (void)index;\n";
|
2019-06-12 12:08:13 -07:00
|
|
|
|
2021-12-23 22:13:06 +00:00
|
|
|
for (const auto &staticValue : llvm::enumerate(values)) {
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
const NamedTypeConstraint &value = staticValue.value();
|
|
|
|
|
|
|
|
bool hasPredicate = value.hasPredicate();
|
|
|
|
bool isOptional = value.isOptional();
|
|
|
|
bool isVariadicOfVariadic = value.isVariadicOfVariadic();
|
|
|
|
if (!hasPredicate && !isOptional && !isVariadicOfVariadic)
|
2019-06-09 07:00:09 -07:00
|
|
|
continue;
|
2020-04-10 14:11:45 -07:00
|
|
|
body << formatv(" auto valueGroup{2} = getODS{0}{1}s({2});\n",
|
2019-06-09 07:00:09 -07:00
|
|
|
// Capitalize the first letter to match the function name
|
2019-06-12 12:08:13 -07:00
|
|
|
valueKind.substr(0, 1).upper(), valueKind.substr(1),
|
|
|
|
staticValue.index());
|
|
|
|
|
2020-04-10 14:11:45 -07:00
|
|
|
// If the constraint is optional check that the value group has at most 1
|
|
|
|
// value.
|
|
|
|
if (isOptional) {
|
2021-11-11 22:08:54 +00:00
|
|
|
body << formatv(verifyOptional, staticValue.index(), valueKind);
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
} else if (isVariadicOfVariadic) {
|
|
|
|
body << formatv(
|
|
|
|
" if (::mlir::failed(::mlir::OpTrait::impl::verifyValueSizeAttr("
|
|
|
|
"*this, \"{0}\", \"{1}\", valueGroup{2}.size())))\n"
|
|
|
|
" return ::mlir::failure();\n",
|
|
|
|
value.constraint.getVariadicOfVariadicSegmentSizeAttr(), value.name,
|
|
|
|
staticValue.index());
|
2020-04-10 14:11:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if there is no predicate there is nothing left to do.
|
|
|
|
if (!hasPredicate)
|
|
|
|
continue;
|
|
|
|
// Emit a loop to check all the dynamic values in the pack.
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
StringRef constraintFn =
|
2021-11-11 22:08:54 +00:00
|
|
|
staticVerifierEmitter.getTypeConstraintFn(value.constraint);
|
|
|
|
body << formatv(verifyValues, staticValue.index(), constraintFn, valueKind);
|
2019-06-09 07:00:09 -07:00
|
|
|
}
|
2019-06-12 12:08:13 -07:00
|
|
|
|
|
|
|
body << " }\n";
|
2019-06-09 07:00:09 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
void OpEmitter::genRegionVerifier(MethodBody &body) {
|
2021-11-11 22:08:54 +00:00
|
|
|
/// Code to verify a region.
|
|
|
|
///
|
|
|
|
/// {0}: Getter for the regions.
|
|
|
|
/// {1}: The region constraint.
|
|
|
|
/// {2}: The region's name.
|
|
|
|
/// {3}: The region description.
|
|
|
|
const char *const verifyRegion = R"(
|
|
|
|
for (auto ®ion : {0})
|
|
|
|
if (::mlir::failed({1}(*this, region, "{2}", index++)))
|
|
|
|
return ::mlir::failure();
|
|
|
|
)";
|
|
|
|
/// Get a single region.
|
|
|
|
///
|
|
|
|
/// {0}: The region's index.
|
|
|
|
const char *const getSingleRegion =
|
|
|
|
"::llvm::makeMutableArrayRef((*this)->getRegion({0}))";
|
|
|
|
|
2020-04-05 01:03:24 -07:00
|
|
|
// If we have no regions, there is nothing more to do.
|
2021-11-11 22:08:54 +00:00
|
|
|
const auto canSkip = [](const NamedRegion ®ion) {
|
|
|
|
return region.constraint.getPredicate().isNull();
|
|
|
|
};
|
|
|
|
auto regions = op.getRegions();
|
|
|
|
if (regions.empty() && llvm::all_of(regions, canSkip))
|
2020-04-05 01:03:24 -07:00
|
|
|
return;
|
2019-05-30 16:50:16 -07:00
|
|
|
|
2021-11-11 22:08:54 +00:00
|
|
|
body << " {\n unsigned index = 0; (void)index;\n";
|
2021-12-23 22:13:06 +00:00
|
|
|
for (const auto &it : llvm::enumerate(regions)) {
|
2021-11-11 22:08:54 +00:00
|
|
|
const auto ®ion = it.value();
|
|
|
|
if (canSkip(region))
|
2020-04-05 01:03:24 -07:00
|
|
|
continue;
|
2019-05-30 16:50:16 -07:00
|
|
|
|
2021-11-11 22:08:54 +00:00
|
|
|
auto getRegion = region.isVariadic()
|
|
|
|
? formatv("{0}()", op.getGetterName(region.name)).str()
|
|
|
|
: formatv(getSingleRegion, it.index()).str();
|
|
|
|
auto constraintFn =
|
|
|
|
staticVerifierEmitter.getRegionConstraintFn(region.constraint);
|
|
|
|
body << formatv(verifyRegion, getRegion, constraintFn, region.name);
|
2019-05-30 16:50:16 -07:00
|
|
|
}
|
2020-04-05 01:03:24 -07:00
|
|
|
body << " }\n";
|
2019-05-30 16:50:16 -07:00
|
|
|
}
|
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
void OpEmitter::genSuccessorVerifier(MethodBody &body) {
|
2021-11-11 22:08:54 +00:00
|
|
|
const char *const verifySuccessor = R"(
|
|
|
|
for (auto *successor : {0})
|
|
|
|
if (::mlir::failed({1}(*this, successor, "{2}", index++)))
|
|
|
|
return ::mlir::failure();
|
|
|
|
)";
|
|
|
|
/// Get a single successor.
|
|
|
|
///
|
|
|
|
/// {0}: The successor's name.
|
|
|
|
const char *const getSingleSuccessor = "::llvm::makeMutableArrayRef({0}())";
|
|
|
|
|
2020-02-21 13:19:50 -08:00
|
|
|
// If we have no successors, there is nothing more to do.
|
2021-11-11 22:08:54 +00:00
|
|
|
const auto canSkip = [](const NamedSuccessor &successor) {
|
|
|
|
return successor.constraint.getPredicate().isNull();
|
|
|
|
};
|
|
|
|
auto successors = op.getSuccessors();
|
|
|
|
if (successors.empty() && llvm::all_of(successors, canSkip))
|
2020-02-21 13:19:50 -08:00
|
|
|
return;
|
|
|
|
|
2021-11-11 22:08:54 +00:00
|
|
|
body << " {\n unsigned index = 0; (void)index;\n";
|
2020-02-21 13:19:50 -08:00
|
|
|
|
2021-11-30 14:09:00 +00:00
|
|
|
for (auto &it : llvm::enumerate(successors)) {
|
2021-11-11 22:08:54 +00:00
|
|
|
const auto &successor = it.value();
|
|
|
|
if (canSkip(successor))
|
2020-02-21 13:19:50 -08:00
|
|
|
continue;
|
|
|
|
|
2021-11-11 22:08:54 +00:00
|
|
|
auto getSuccessor =
|
|
|
|
formatv(successor.isVariadic() ? "{0}()" : getSingleSuccessor,
|
|
|
|
successor.name, it.index())
|
|
|
|
.str();
|
|
|
|
auto constraintFn =
|
|
|
|
staticVerifierEmitter.getSuccessorConstraintFn(successor.constraint);
|
|
|
|
body << formatv(verifySuccessor, getSuccessor, constraintFn,
|
|
|
|
successor.name);
|
2020-02-21 13:19:50 -08:00
|
|
|
}
|
|
|
|
body << " }\n";
|
|
|
|
}
|
|
|
|
|
2020-03-05 12:39:46 -08:00
|
|
|
/// Add a size count trait to the given operation class.
|
|
|
|
static void addSizeCountTrait(OpClass &opClass, StringRef traitKind,
|
2020-04-05 01:03:24 -07:00
|
|
|
int numTotal, int numVariadic) {
|
2020-03-05 12:39:46 -08:00
|
|
|
if (numVariadic != 0) {
|
2020-04-05 01:03:24 -07:00
|
|
|
if (numTotal == numVariadic)
|
2020-09-14 20:01:07 +00:00
|
|
|
opClass.addTrait("::mlir::OpTrait::Variadic" + traitKind + "s");
|
2020-03-05 12:39:46 -08:00
|
|
|
else
|
2020-09-14 20:01:07 +00:00
|
|
|
opClass.addTrait("::mlir::OpTrait::AtLeastN" + traitKind + "s<" +
|
2020-04-05 01:03:24 -07:00
|
|
|
Twine(numTotal - numVariadic) + ">::Impl");
|
2020-03-05 12:39:46 -08:00
|
|
|
return;
|
|
|
|
}
|
2020-04-05 01:03:24 -07:00
|
|
|
switch (numTotal) {
|
2020-03-05 12:39:46 -08:00
|
|
|
case 0:
|
2022-05-14 00:20:01 +00:00
|
|
|
opClass.addTrait("::mlir::OpTrait::Zero" + traitKind + "s");
|
2020-03-05 12:39:46 -08:00
|
|
|
break;
|
|
|
|
case 1:
|
2020-09-14 20:01:07 +00:00
|
|
|
opClass.addTrait("::mlir::OpTrait::One" + traitKind);
|
2020-03-05 12:39:46 -08:00
|
|
|
break;
|
|
|
|
default:
|
2020-09-14 20:01:07 +00:00
|
|
|
opClass.addTrait("::mlir::OpTrait::N" + traitKind + "s<" + Twine(numTotal) +
|
2020-03-05 12:39:46 -08:00
|
|
|
">::Impl");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genTraits() {
|
2020-04-05 01:03:24 -07:00
|
|
|
// Add region size trait.
|
|
|
|
unsigned numRegions = op.getNumRegions();
|
|
|
|
unsigned numVariadicRegions = op.getNumVariadicRegions();
|
|
|
|
addSizeCountTrait(opClass, "Region", numRegions, numVariadicRegions);
|
|
|
|
|
2020-12-23 18:13:39 -08:00
|
|
|
// Add result size traits.
|
2019-05-03 19:48:57 -07:00
|
|
|
int numResults = op.getNumResults();
|
2020-04-10 14:11:45 -07:00
|
|
|
int numVariadicResults = op.getNumVariableLengthResults();
|
2020-03-05 12:39:46 -08:00
|
|
|
addSizeCountTrait(opClass, "Result", numResults, numVariadicResults);
|
|
|
|
|
2020-12-23 18:13:39 -08:00
|
|
|
// For single result ops with a known specific type, generate a OneTypedResult
|
|
|
|
// trait.
|
|
|
|
if (numResults == 1 && numVariadicResults == 0) {
|
|
|
|
auto cppName = op.getResults().begin()->constraint.getCPPClassName();
|
|
|
|
opClass.addTrait("::mlir::OpTrait::OneTypedResult<" + cppName + ">::Impl");
|
|
|
|
}
|
|
|
|
|
2020-03-05 12:39:46 -08:00
|
|
|
// Add successor size trait.
|
|
|
|
unsigned numSuccessors = op.getNumSuccessors();
|
|
|
|
unsigned numVariadicSuccessors = op.getNumVariadicSuccessors();
|
|
|
|
addSizeCountTrait(opClass, "Successor", numSuccessors, numVariadicSuccessors);
|
2018-10-15 08:54:37 -07:00
|
|
|
|
2019-02-20 10:50:26 -08:00
|
|
|
// Add variadic size trait and normal op traits.
|
2019-05-03 19:48:57 -07:00
|
|
|
int numOperands = op.getNumOperands();
|
2020-04-10 14:11:45 -07:00
|
|
|
int numVariadicOperands = op.getNumVariableLengthOperands();
|
2018-12-13 11:43:50 -08:00
|
|
|
|
2019-02-03 07:29:17 -08:00
|
|
|
// Add operand size trait.
|
2022-05-14 00:20:01 +00:00
|
|
|
addSizeCountTrait(opClass, "Operand", numOperands, numVariadicOperands);
|
2020-03-12 14:06:14 -07:00
|
|
|
|
2022-02-25 18:17:30 +00:00
|
|
|
// The op traits defined internal are ensured that they can be verified
|
|
|
|
// earlier.
|
|
|
|
for (const auto &trait : op.getTraits()) {
|
|
|
|
if (auto *opTrait = dyn_cast<tblgen::NativeTrait>(&trait)) {
|
|
|
|
if (opTrait->isStructuralOpTrait())
|
|
|
|
opClass.addTrait(opTrait->getFullyQualifiedTraitName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpInvariants wrapps the verifyInvariants which needs to be run before
|
|
|
|
// native/interface traits and after all the traits with `StructuralOpTrait`.
|
|
|
|
opClass.addTrait("::mlir::OpTrait::OpInvariants");
|
|
|
|
|
2020-03-12 14:06:14 -07:00
|
|
|
// Add the native and interface traits.
|
|
|
|
for (const auto &trait : op.getTraits()) {
|
2022-02-25 18:17:30 +00:00
|
|
|
if (auto *opTrait = dyn_cast<tblgen::NativeTrait>(&trait)) {
|
|
|
|
if (!opTrait->isStructuralOpTrait())
|
|
|
|
opClass.addTrait(opTrait->getFullyQualifiedTraitName());
|
|
|
|
} else if (auto *opTrait = dyn_cast<tblgen::InterfaceTrait>(&trait)) {
|
2021-04-15 11:29:23 -07:00
|
|
|
opClass.addTrait(opTrait->getFullyQualifiedTraitName());
|
2022-02-25 18:17:30 +00:00
|
|
|
}
|
2020-03-12 14:06:14 -07:00
|
|
|
}
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
void OpEmitter::genOpNameGetter() {
|
2021-11-30 14:09:00 +00:00
|
|
|
auto *method = opClass.addStaticMethod<Method::Constexpr>(
|
2021-11-12 01:17:05 +00:00
|
|
|
"::llvm::StringLiteral", "getOperationName");
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, "getOperationName", op);
|
2021-02-04 17:12:15 +00:00
|
|
|
method->body() << " return ::llvm::StringLiteral(\"" << op.getOperationName()
|
|
|
|
<< "\");";
|
2019-03-20 17:25:34 -07:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:34:03 -08:00
|
|
|
void OpEmitter::genOpAsmInterface() {
|
|
|
|
// If the user only has one results or specifically added the Asm trait,
|
|
|
|
// then don't generate it for them. We specifically only handle multi result
|
|
|
|
// operations, because the name of a single result in the common case is not
|
|
|
|
// interesting(generally 'result'/'output'/etc.).
|
|
|
|
// TODO: We could also add a flag to allow operations to opt in to this
|
|
|
|
// generation, even if they only have a single operation.
|
|
|
|
int numResults = op.getNumResults();
|
2020-07-12 14:11:39 -07:00
|
|
|
if (numResults <= 1 || op.getTrait("::mlir::OpAsmOpInterface::Trait"))
|
2019-11-21 14:34:03 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
SmallVector<StringRef, 4> resultNames(numResults);
|
|
|
|
for (int i = 0; i != numResults; ++i)
|
|
|
|
resultNames[i] = op.getResultName(i);
|
|
|
|
|
|
|
|
// Don't add the trait if none of the results have a valid name.
|
|
|
|
if (llvm::all_of(resultNames, [](StringRef name) { return name.empty(); }))
|
|
|
|
return;
|
2020-07-12 14:11:39 -07:00
|
|
|
opClass.addTrait("::mlir::OpAsmOpInterface::Trait");
|
2019-11-21 14:34:03 -08:00
|
|
|
|
|
|
|
// Generate the right accessor for the number of results.
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *method = opClass.addMethod(
|
|
|
|
"void", "getAsmResultNames",
|
|
|
|
MethodParameter("::mlir::OpAsmSetValueNameFn", "setNameFn"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, "getAsmResultNames", op);
|
2020-09-17 13:18:09 -07:00
|
|
|
auto &body = method->body();
|
2019-11-21 14:34:03 -08:00
|
|
|
for (int i = 0; i != numResults; ++i) {
|
|
|
|
body << " auto resultGroup" << i << " = getODSResults(" << i << ");\n"
|
|
|
|
<< " if (!llvm::empty(resultGroup" << i << "))\n"
|
|
|
|
<< " setNameFn(*resultGroup" << i << ".begin(), \""
|
|
|
|
<< resultNames[i] << "\");\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 08:03:20 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// OpOperandAdaptor emitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// Helper class to emit Op operand adaptors to an output stream. Operand
|
2019-12-23 14:45:01 -08:00
|
|
|
// adaptors are wrappers around ArrayRef<Value> that provide named operand
|
2019-06-03 08:03:20 -07:00
|
|
|
// getters identical to those defined in the Op.
|
|
|
|
class OpOperandAdaptorEmitter {
|
|
|
|
public:
|
2022-04-08 16:41:31 +00:00
|
|
|
static void
|
|
|
|
emitDecl(const Operator &op,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter,
|
|
|
|
raw_ostream &os);
|
|
|
|
static void
|
|
|
|
emitDef(const Operator &op,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter,
|
|
|
|
raw_ostream &os);
|
2019-06-03 08:03:20 -07:00
|
|
|
|
|
|
|
private:
|
2021-11-11 22:08:54 +00:00
|
|
|
explicit OpOperandAdaptorEmitter(
|
2022-04-08 16:41:31 +00:00
|
|
|
const Operator &op,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter);
|
2019-06-03 08:03:20 -07:00
|
|
|
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
// Add verification function. This generates a verify method for the adaptor
|
|
|
|
// which verifies all the op-independent attribute constraints.
|
|
|
|
void addVerification();
|
|
|
|
|
2022-04-08 16:41:31 +00:00
|
|
|
// The operation for which to emit an adaptor.
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
const Operator &op;
|
2022-04-08 16:41:31 +00:00
|
|
|
|
|
|
|
// The generated adaptor class.
|
2020-05-28 09:05:24 -07:00
|
|
|
Class adaptor;
|
2022-04-08 16:41:31 +00:00
|
|
|
|
|
|
|
// The emitter containing all of the locally emitted verification functions.
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter;
|
|
|
|
|
|
|
|
// Helper for emitting adaptor code.
|
|
|
|
OpOrAdaptorHelper emitHelper;
|
2019-06-03 08:03:20 -07:00
|
|
|
};
|
2021-12-07 18:27:58 +00:00
|
|
|
} // namespace
|
2019-06-03 08:03:20 -07:00
|
|
|
|
2021-11-11 22:08:54 +00:00
|
|
|
OpOperandAdaptorEmitter::OpOperandAdaptorEmitter(
|
2022-04-08 16:41:31 +00:00
|
|
|
const Operator &op,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter)
|
2021-11-11 22:08:54 +00:00
|
|
|
: op(op), adaptor(op.getAdaptorName()),
|
2022-04-08 16:41:31 +00:00
|
|
|
staticVerifierEmitter(staticVerifierEmitter),
|
|
|
|
emitHelper(op, /*emitForOp=*/false) {
|
2021-11-30 14:09:00 +00:00
|
|
|
adaptor.addField("::mlir::ValueRange", "odsOperands");
|
|
|
|
adaptor.addField("::mlir::DictionaryAttr", "odsAttrs");
|
|
|
|
adaptor.addField("::mlir::RegionRange", "odsRegions");
|
2022-04-08 16:41:31 +00:00
|
|
|
adaptor.addField("::llvm::Optional<::mlir::OperationName>", "odsOpName");
|
|
|
|
|
2020-05-24 20:42:58 -07:00
|
|
|
const auto *attrSizedOperands =
|
2021-11-30 14:09:00 +00:00
|
|
|
op.getTrait("::m::OpTrait::AttrSizedOperandSegments");
|
2020-05-28 09:05:24 -07:00
|
|
|
{
|
2021-11-12 01:17:05 +00:00
|
|
|
SmallVector<MethodParameter> paramList;
|
2020-09-17 13:18:09 -07:00
|
|
|
paramList.emplace_back("::mlir::ValueRange", "values");
|
|
|
|
paramList.emplace_back("::mlir::DictionaryAttr", "attrs",
|
|
|
|
attrSizedOperands ? "" : "nullptr");
|
2021-02-26 16:25:00 -08:00
|
|
|
paramList.emplace_back("::mlir::RegionRange", "regions", "{}");
|
2021-11-30 14:09:00 +00:00
|
|
|
auto *constructor = adaptor.addConstructor(std::move(paramList));
|
2020-09-17 13:18:09 -07:00
|
|
|
|
|
|
|
constructor->addMemberInitializer("odsOperands", "values");
|
|
|
|
constructor->addMemberInitializer("odsAttrs", "attrs");
|
2021-02-26 16:25:00 -08:00
|
|
|
constructor->addMemberInitializer("odsRegions", "regions");
|
2022-04-08 16:41:31 +00:00
|
|
|
|
|
|
|
MethodBody &body = constructor->body();
|
|
|
|
body.indent() << "if (odsAttrs)\n";
|
|
|
|
body.indent() << formatv(
|
|
|
|
"odsOpName.emplace(\"{0}\", odsAttrs.getContext());\n",
|
|
|
|
op.getOperationName());
|
2020-05-28 09:05:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-04-08 16:41:31 +00:00
|
|
|
auto *constructor =
|
|
|
|
adaptor.addConstructor(MethodParameter(op.getCppClassName(), "op"));
|
2020-11-28 13:35:55 +01:00
|
|
|
constructor->addMemberInitializer("odsOperands", "op->getOperands()");
|
|
|
|
constructor->addMemberInitializer("odsAttrs", "op->getAttrDictionary()");
|
2021-02-26 16:25:00 -08:00
|
|
|
constructor->addMemberInitializer("odsRegions", "op->getRegions()");
|
2022-04-08 16:41:31 +00:00
|
|
|
constructor->addMemberInitializer("odsOpName", "op->getName()");
|
2020-05-28 09:05:24 -07:00
|
|
|
}
|
2020-05-24 20:42:58 -07:00
|
|
|
|
2021-02-26 16:25:00 -08:00
|
|
|
{
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = adaptor.addMethod("::mlir::ValueRange", "getOperands");
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, "getOperands", op);
|
2021-02-26 16:25:00 -08:00
|
|
|
m->body() << " return odsOperands;";
|
|
|
|
}
|
2022-04-08 16:41:31 +00:00
|
|
|
std::string sizeAttrInit;
|
|
|
|
if (op.getTrait("::mlir::OpTrait::AttrSizedOperandSegments")) {
|
|
|
|
sizeAttrInit = formatv(adapterSegmentSizeAttrInitCode,
|
|
|
|
emitHelper.getAttr(operandSegmentAttrName));
|
|
|
|
}
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-23 20:23:09 +00:00
|
|
|
generateNamedOperandGetters(op, adaptor,
|
|
|
|
/*isAdaptor=*/true, sizeAttrInit,
|
2020-06-26 13:20:44 +02:00
|
|
|
/*rangeType=*/"::mlir::ValueRange",
|
2020-05-24 20:42:58 -07:00
|
|
|
/*rangeBeginCall=*/"odsOperands.begin()",
|
|
|
|
/*rangeSizeCall=*/"odsOperands.size()",
|
|
|
|
/*getOperandCallPattern=*/"odsOperands[{0}]");
|
|
|
|
|
|
|
|
FmtContext fctx;
|
2020-06-26 13:20:44 +02:00
|
|
|
fctx.withBuilder("::mlir::Builder(odsAttrs.getContext())");
|
2020-05-24 20:42:58 -07:00
|
|
|
|
2022-01-05 05:36:34 +00:00
|
|
|
// Generate named accessor with Attribute return type.
|
|
|
|
auto emitAttrWithStorageType = [&](StringRef name, StringRef emitName,
|
|
|
|
Attribute attr) {
|
|
|
|
auto *method = adaptor.addMethod(attr.getStorageType(), emitName + "Attr");
|
|
|
|
ERROR_IF_PRUNED(method, "Adaptor::" + emitName + "Attr", op);
|
2022-04-08 16:41:31 +00:00
|
|
|
auto &body = method->body().indent();
|
|
|
|
body << "assert(odsAttrs && \"no attributes when constructing adapter\");\n"
|
|
|
|
<< formatv("auto attr = {0}.{1}<{2}>();\n", emitHelper.getAttr(name),
|
|
|
|
attr.hasDefaultValue() || attr.isOptional()
|
|
|
|
? "dyn_cast_or_null"
|
|
|
|
: "cast",
|
|
|
|
attr.getStorageType());
|
2020-05-24 20:42:58 -07:00
|
|
|
|
|
|
|
if (attr.hasDefaultValue()) {
|
|
|
|
// Use the default value if attribute is not set.
|
|
|
|
// TODO: this is inefficient, we are recreating the attribute for every
|
|
|
|
// call. This should be set instead.
|
|
|
|
std::string defaultValue = std::string(
|
|
|
|
tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue()));
|
|
|
|
body << " if (!attr)\n attr = " << defaultValue << ";\n";
|
|
|
|
}
|
2022-07-08 11:31:12 -07:00
|
|
|
body << "return attr;\n";
|
2020-05-24 20:42:58 -07:00
|
|
|
};
|
|
|
|
|
2021-02-26 16:25:00 -08:00
|
|
|
{
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = adaptor.addMethod("::mlir::DictionaryAttr", "getAttributes");
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, "Adaptor::getAttributes", op);
|
2021-02-26 16:25:00 -08:00
|
|
|
m->body() << " return odsAttrs;";
|
|
|
|
}
|
2020-05-24 20:42:58 -07:00
|
|
|
for (auto &namedAttr : op.getAttributes()) {
|
|
|
|
const auto &name = namedAttr.name;
|
|
|
|
const auto &attr = namedAttr.attr;
|
2022-01-05 05:36:34 +00:00
|
|
|
if (attr.isDerivedAttr())
|
|
|
|
continue;
|
|
|
|
for (const auto &emitName : op.getGetterNames(name)) {
|
|
|
|
emitAttrWithStorageType(name, emitName, attr);
|
|
|
|
emitAttrGetterWithReturnType(fctx, adaptor, op, emitName, attr);
|
2021-10-20 07:08:36 -07:00
|
|
|
}
|
2020-05-24 20:42:58 -07:00
|
|
|
}
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
|
2021-02-26 16:25:00 -08:00
|
|
|
unsigned numRegions = op.getNumRegions();
|
|
|
|
if (numRegions > 0) {
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = adaptor.addMethod("::mlir::RegionRange", "getRegions");
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(m, "Adaptor::getRegions", op);
|
2021-02-26 16:25:00 -08:00
|
|
|
m->body() << " return odsRegions;";
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < numRegions; ++i) {
|
|
|
|
const auto ®ion = op.getRegion(i);
|
|
|
|
if (region.name.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Generate the accessors for a variadic region.
|
2021-10-26 17:35:16 -07:00
|
|
|
for (StringRef name : op.getGetterNames(region.name)) {
|
|
|
|
if (region.isVariadic()) {
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = adaptor.addMethod("::mlir::RegionRange", name);
|
2021-10-26 17:35:16 -07:00
|
|
|
ERROR_IF_PRUNED(m, "Adaptor::" + name, op);
|
|
|
|
m->body() << formatv(" return odsRegions.drop_front({0});", i);
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-26 16:25:00 -08:00
|
|
|
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *m = adaptor.addMethod("::mlir::Region &", name);
|
2021-10-26 17:35:16 -07:00
|
|
|
ERROR_IF_PRUNED(m, "Adaptor::" + name, op);
|
|
|
|
m->body() << formatv(" return *odsRegions[{0}];", i);
|
|
|
|
}
|
2021-02-26 16:25:00 -08:00
|
|
|
}
|
|
|
|
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
// Add verification function.
|
|
|
|
addVerification();
|
2021-11-30 14:09:00 +00:00
|
|
|
adaptor.finalize();
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpOperandAdaptorEmitter::addVerification() {
|
2021-11-12 01:17:05 +00:00
|
|
|
auto *method = adaptor.addMethod("::mlir::LogicalResult", "verify",
|
|
|
|
MethodParameter("::mlir::Location", "loc"));
|
2021-10-14 15:58:44 -07:00
|
|
|
ERROR_IF_PRUNED(method, "verify", op);
|
2020-09-17 13:18:09 -07:00
|
|
|
auto &body = method->body();
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
|
|
|
|
FmtContext verifyCtx;
|
2021-11-09 18:59:06 +00:00
|
|
|
populateSubstitutions(emitHelper, verifyCtx);
|
2021-11-11 22:08:54 +00:00
|
|
|
genAttributeVerifier(emitHelper, verifyCtx, body, staticVerifierEmitter);
|
[mlir] Add verify method to adaptor
This allows verifying op-indepent attributes (e.g., attributes that do not require the op to have been created) before constructing an operation. These include checking whether required attributes are defined or constraints on attributes (such as I32 attribute). This is not perfect (e.g., if one had a disjunctive constraint where one part relied on the op and the other doesn't, then this would not try and extract the op independent from the op dependent).
The next step is to move these out to a trait that could be verified earlier than in the generated method. The first use case is for inferring the return type while constructing the op. At that point you don't have an Operation yet and that ends up in one having to duplicate the same checks, e.g., verify that attribute A is defined before querying A in shape function which requires that duplication. Instead this allows one to invoke a method to verify all the traits and, if this is checked first during verification, then all other traits could use attributes knowing they have been verified.
It is a little bit funny to have these on the adaptor, but I see the adaptor as a place to collect information about the op before the op is constructed (e.g., avoiding stringly typed accessors, verifying what is possible to verify before the op is constructed) while being cheap to use even with constructed op (so layer of indirection between the op constructed/being constructed). And from that point of view it made sense to me.
Differential Revision: https://reviews.llvm.org/D80842
2020-06-05 09:47:37 -07:00
|
|
|
|
2020-09-14 20:01:07 +00:00
|
|
|
body << " return ::mlir::success();";
|
2019-06-03 08:03:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-11 22:08:54 +00:00
|
|
|
void OpOperandAdaptorEmitter::emitDecl(
|
2022-04-08 16:41:31 +00:00
|
|
|
const Operator &op,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter,
|
2021-11-11 22:08:54 +00:00
|
|
|
raw_ostream &os) {
|
|
|
|
OpOperandAdaptorEmitter(op, staticVerifierEmitter).adaptor.writeDeclTo(os);
|
2019-06-03 08:03:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-11 22:08:54 +00:00
|
|
|
void OpOperandAdaptorEmitter::emitDef(
|
2022-04-08 16:41:31 +00:00
|
|
|
const Operator &op,
|
|
|
|
const StaticVerifierFunctionEmitter &staticVerifierEmitter,
|
2021-11-11 22:08:54 +00:00
|
|
|
raw_ostream &os) {
|
|
|
|
OpOperandAdaptorEmitter(op, staticVerifierEmitter).adaptor.writeDefTo(os);
|
2019-06-03 08:03:20 -07:00
|
|
|
}
|
|
|
|
|
2018-10-15 08:54:37 -07:00
|
|
|
// Emits the opcode enum and op classes.
|
2020-12-14 14:14:22 -08:00
|
|
|
static void emitOpClasses(const RecordKeeper &recordKeeper,
|
|
|
|
const std::vector<Record *> &defs, raw_ostream &os,
|
2019-03-20 17:25:34 -07:00
|
|
|
bool emitDecl) {
|
2019-07-29 10:44:33 -07:00
|
|
|
// First emit forward declaration for each class, this allows them to refer
|
|
|
|
// to each others in traits for example.
|
|
|
|
if (emitDecl) {
|
2020-07-14 06:52:32 -07:00
|
|
|
os << "#if defined(GET_OP_CLASSES) || defined(GET_OP_FWD_DEFINES)\n";
|
|
|
|
os << "#undef GET_OP_FWD_DEFINES\n";
|
2019-07-29 10:44:33 -07:00
|
|
|
for (auto *def : defs) {
|
|
|
|
Operator op(*def);
|
2021-05-10 14:30:22 -07:00
|
|
|
NamespaceEmitter emitter(os, op.getCppNamespace());
|
2019-07-29 10:44:33 -07:00
|
|
|
os << "class " << op.getCppClassName() << ";\n";
|
|
|
|
}
|
2020-07-14 06:52:32 -07:00
|
|
|
os << "#endif\n\n";
|
2019-07-29 10:44:33 -07:00
|
|
|
}
|
2020-07-14 06:52:32 -07:00
|
|
|
|
|
|
|
IfDefScope scope("GET_OP_CLASSES", os);
|
2020-12-14 14:14:22 -08:00
|
|
|
if (defs.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Generate all of the locally instantiated methods first.
|
2021-11-11 22:08:54 +00:00
|
|
|
StaticVerifierFunctionEmitter staticVerifierEmitter(os, recordKeeper);
|
2021-08-12 17:35:00 +00:00
|
|
|
os << formatv(opCommentHeader, "Local Utility Method", "Definitions");
|
2021-11-11 22:08:54 +00:00
|
|
|
staticVerifierEmitter.emitOpConstraints(defs, emitDecl);
|
2021-08-12 17:35:00 +00:00
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
for (auto *def : defs) {
|
2019-06-03 08:03:20 -07:00
|
|
|
Operator op(*def);
|
2019-03-20 17:25:34 -07:00
|
|
|
if (emitDecl) {
|
2021-07-28 05:22:45 +00:00
|
|
|
{
|
|
|
|
NamespaceEmitter emitter(os, op.getCppNamespace());
|
|
|
|
os << formatv(opCommentHeader, op.getQualCppClassName(),
|
|
|
|
"declarations");
|
2021-11-11 22:08:54 +00:00
|
|
|
OpOperandAdaptorEmitter::emitDecl(op, staticVerifierEmitter, os);
|
2021-07-28 05:22:45 +00:00
|
|
|
OpEmitter::emitDecl(op, os, staticVerifierEmitter);
|
|
|
|
}
|
|
|
|
// Emit the TypeID explicit specialization to have a single definition.
|
|
|
|
if (!op.getCppNamespace().empty())
|
2022-03-30 17:00:37 -07:00
|
|
|
os << "MLIR_DECLARE_EXPLICIT_TYPE_ID(" << op.getCppNamespace()
|
2021-07-28 05:22:45 +00:00
|
|
|
<< "::" << op.getCppClassName() << ")\n\n";
|
2019-03-20 17:25:34 -07:00
|
|
|
} else {
|
2021-07-28 05:22:45 +00:00
|
|
|
{
|
|
|
|
NamespaceEmitter emitter(os, op.getCppNamespace());
|
|
|
|
os << formatv(opCommentHeader, op.getQualCppClassName(), "definitions");
|
2021-11-11 22:08:54 +00:00
|
|
|
OpOperandAdaptorEmitter::emitDef(op, staticVerifierEmitter, os);
|
2021-07-28 05:22:45 +00:00
|
|
|
OpEmitter::emitDef(op, os, staticVerifierEmitter);
|
|
|
|
}
|
|
|
|
// Emit the TypeID explicit specialization to have a single definition.
|
|
|
|
if (!op.getCppNamespace().empty())
|
2022-03-30 17:00:37 -07:00
|
|
|
os << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" << op.getCppNamespace()
|
2021-07-28 05:22:45 +00:00
|
|
|
<< "::" << op.getCppClassName() << ")\n\n";
|
2019-03-20 17:25:34 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emits a comma-separated list of the ops.
|
|
|
|
static void emitOpList(const std::vector<Record *> &defs, raw_ostream &os) {
|
|
|
|
IfDefScope scope("GET_OP_LIST", os);
|
|
|
|
|
2020-04-16 08:05:21 -07:00
|
|
|
interleave(
|
2019-05-20 09:33:10 -07:00
|
|
|
// TODO: We are constructing the Operator wrapper instance just for
|
|
|
|
// getting it's qualified class name here. Reduce the overhead by having a
|
|
|
|
// lightweight version of Operator class just for that purpose.
|
|
|
|
defs, [&os](Record *def) { os << Operator(def).getQualCppClassName(); },
|
2019-03-20 17:25:34 -07:00
|
|
|
[&os]() { os << ",\n"; });
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool emitOpDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
|
|
|
|
emitSourceFileHeader("Op Declarations", os);
|
|
|
|
|
2021-04-06 12:53:27 -07:00
|
|
|
std::vector<Record *> defs = getRequestedOpDefinitions(recordKeeper);
|
2020-12-14 14:14:22 -08:00
|
|
|
emitOpClasses(recordKeeper, defs, os, /*emitDecl=*/true);
|
2019-03-20 17:25:34 -07:00
|
|
|
|
|
|
|
return false;
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:25:34 -07:00
|
|
|
static bool emitOpDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
|
|
|
|
emitSourceFileHeader("Op Definitions", os);
|
2018-10-15 08:54:37 -07:00
|
|
|
|
2021-04-06 12:53:27 -07:00
|
|
|
std::vector<Record *> defs = getRequestedOpDefinitions(recordKeeper);
|
2018-10-15 08:54:37 -07:00
|
|
|
emitOpList(defs, os);
|
2020-12-14 14:14:22 -08:00
|
|
|
emitOpClasses(recordKeeper, defs, os, /*emitDecl=*/false);
|
2019-03-20 17:25:34 -07:00
|
|
|
|
|
|
|
return false;
|
2018-10-15 08:54:37 -07:00
|
|
|
}
|
|
|
|
|
Start doc generation pass.
Start doc generation pass that generates simple markdown output. The output is formatted simply[1] in markdown, but this allows seeing what info we have, where we can refine the op description (e.g., the inputs is probably redundant), what info is missing (e.g., the attributes could probably have a description).
The formatting of the description is still left up to whatever was in the op definition (which luckily, due to the uniformity in the .td file, turned out well but relying on the indentation there is fragile). The mechanism to autogenerate these post changes has not been added yet either. The output file could be run through a markdown formatter too to remove extra spaces.
[1]. This is not proposal for final style :) There could also be a discussion around single doc vs multiple (per dialect, per op), whether we want a TOC, whether operands/attributes should be headings or just formatted differently ...
PiperOrigin-RevId: 230354538
2019-01-22 09:31:04 -08:00
|
|
|
static mlir::GenRegistration
|
2019-03-20 17:25:34 -07:00
|
|
|
genOpDecls("gen-op-decls", "Generate op declarations",
|
|
|
|
[](const RecordKeeper &records, raw_ostream &os) {
|
|
|
|
return emitOpDecls(records, os);
|
|
|
|
});
|
|
|
|
|
|
|
|
static mlir::GenRegistration genOpDefs("gen-op-defs", "Generate op definitions",
|
|
|
|
[](const RecordKeeper &records,
|
|
|
|
raw_ostream &os) {
|
|
|
|
return emitOpDefs(records, os);
|
|
|
|
});
|