2018-07-08 20:51:38 -07:00
|
|
|
//===- Builders.cpp - Helpers for constructing MLIR Classes ---------------===//
|
|
|
|
//
|
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-07-08 20:51:38 -07:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-08 20:51:38 -07:00
|
|
|
|
|
|
|
#include "mlir/IR/Builders.h"
|
2018-07-10 10:59:53 -07:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
2020-12-03 17:22:29 -08:00
|
|
|
#include "mlir/IR/BuiltinTypes.h"
|
2019-12-13 12:21:42 -08:00
|
|
|
#include "mlir/IR/Dialect.h"
|
2023-01-08 14:15:07 -08:00
|
|
|
#include "mlir/IR/IRMapping.h"
|
2018-08-07 14:24:38 -07:00
|
|
|
#include "mlir/IR/IntegerSet.h"
|
2019-12-13 12:21:42 -08:00
|
|
|
#include "mlir/IR/Matchers.h"
|
2020-12-03 17:22:57 -08:00
|
|
|
#include "mlir/IR/SymbolTable.h"
|
2020-12-03 17:22:29 -08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-07-08 20:51:38 -07:00
|
|
|
|
2020-12-03 15:46:23 -08:00
|
|
|
using namespace mlir;
|
2018-07-08 20:51:38 -07:00
|
|
|
|
2018-08-27 21:05:16 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Locations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-06-25 16:57:32 -07:00
|
|
|
Location Builder::getUnknownLoc() { return UnknownLoc::get(context); }
|
2018-08-27 21:05:16 -07:00
|
|
|
|
2018-11-09 11:27:28 -08:00
|
|
|
Location Builder::getFusedLoc(ArrayRef<Location> locs, Attribute metadata) {
|
|
|
|
return FusedLoc::get(locs, metadata, context);
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-08 20:51:38 -07:00
|
|
|
// Types.
|
2018-07-10 10:59:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
Add APFloat and MLIR type support for fp8 (e5m2).
(Re-Apply with fixes to clang MicrosoftMangle.cpp)
This is a first step towards high level representation for fp8 types
that have been built in to hardware with near term roadmaps. Like the
BFLOAT16 type, the family of fp8 types are inspired by IEEE-754 binary
floating point formats but, due to the size limits, have been tweaked in
various ways in order to maximally use the range/precision in various
scenarios. The list of variants is small/finite and bounded by real
hardware.
This patch introduces the E5M2 FP8 format as proposed by Nvidia, ARM,
and Intel in the paper: https://arxiv.org/pdf/2209.05433.pdf
As the more conformant of the two implemented datatypes, we are plumbing
it through LLVM's APFloat type and MLIR's type system first as a
template. It will be followed by the range optimized E4M3 FP8 format
described in the paper. Since that format deviates further from the
IEEE-754 norms, it may require more debate and implementation
complexity.
Given that we see two parts of the FP8 implementation space represented
by these cases, we are recommending naming of:
* `F8M<N>` : For FP8 types that can be conceived of as following the
same rules as FP16 but with a smaller number of mantissa/exponent
bits. Including the number of mantissa bits in the type name is enough
to fully specify the type. This naming scheme is used to represent
the E5M2 type described in the paper.
* `F8M<N>F` : For FP8 types such as E4M3 which only support finite
values.
The first of these (this patch) seems fairly non-controversial. The
second is previewed here to illustrate options for extending to the
other known variant (but can be discussed in detail in the patch
which implements it).
Many conversations about these types focus on the Machine-Learning
ecosystem where they are used to represent mixed-datatype computations
at a high level. At that level (which is why we also expose them in
MLIR), it is important to retain the actual type definition so that when
lowering to actual kernels or target specific code, the correct
promotions, casts and rescalings can be done as needed. We expect that
most LLVM backends will only experience these types as opaque `I8`
values that are applicable to some instructions.
MLIR does not make it particularly easy to add new floating point types
(i.e. the FloatType hierarchy is not open). Given the need to fully
model FloatTypes and make them interop with tooling, such types will
always be "heavy-weight" and it is not expected that a highly open type
system will be particularly helpful. There are also a bounded number of
floating point types in use for current and upcoming hardware, and we
can just implement them like this (perhaps looking for some cosmetic
ways to reduce the number of places that need to change). Creating a
more generic mechanism for extending floating point types seems like it
wouldn't be worth it and we should just deal with defining them one by
one on an as-needed basis when real hardware implements a new scheme.
Hopefully, with some additional production use and complete software
stacks, hardware makers will converge on a set of such types that is not
terribly divergent at the level that the compiler cares about.
(I cleaned up some old formatting and sorted some items for this case:
If we converge on landing this in some form, I will NFC commit format
only changes as a separate commit)
Differential Revision: https://reviews.llvm.org/D133823
2022-07-26 19:02:37 -07:00
|
|
|
FloatType Builder::getFloat8E5M2Type() {
|
|
|
|
return FloatType::getFloat8E5M2(context);
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:24:24 +01:00
|
|
|
FloatType Builder::getFloat8E4M3FNType() {
|
|
|
|
return FloatType::getFloat8E4M3FN(context);
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:10:20 +00:00
|
|
|
FloatType Builder::getFloat8E5M2FNUZType() {
|
|
|
|
return FloatType::getFloat8E5M2FNUZ(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
FloatType Builder::getFloat8E4M3FNUZType() {
|
|
|
|
return FloatType::getFloat8E4M3FNUZ(context);
|
|
|
|
}
|
|
|
|
|
2023-03-09 23:10:57 +00:00
|
|
|
FloatType Builder::getFloat8E4M3B11FNUZType() {
|
|
|
|
return FloatType::getFloat8E4M3B11FNUZ(context);
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:08:04 -08:00
|
|
|
FloatType Builder::getBF16Type() { return FloatType::getBF16(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
2019-02-12 11:08:04 -08:00
|
|
|
FloatType Builder::getF16Type() { return FloatType::getF16(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
2019-02-12 11:08:04 -08:00
|
|
|
FloatType Builder::getF32Type() { return FloatType::getF32(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
2019-02-12 11:08:04 -08:00
|
|
|
FloatType Builder::getF64Type() { return FloatType::getF64(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
2021-01-15 10:29:37 -05:00
|
|
|
FloatType Builder::getF80Type() { return FloatType::getF80(context); }
|
|
|
|
|
|
|
|
FloatType Builder::getF128Type() { return FloatType::getF128(context); }
|
|
|
|
|
2019-02-12 11:08:04 -08:00
|
|
|
IndexType Builder::getIndexType() { return IndexType::get(context); }
|
2018-07-08 20:51:38 -07:00
|
|
|
|
2020-12-17 12:24:45 -08:00
|
|
|
IntegerType Builder::getI1Type() { return IntegerType::get(context, 1); }
|
2018-11-28 11:49:26 -08:00
|
|
|
|
2022-09-12 23:54:16 +00:00
|
|
|
IntegerType Builder::getI2Type() { return IntegerType::get(context, 2); }
|
|
|
|
|
|
|
|
IntegerType Builder::getI4Type() { return IntegerType::get(context, 4); }
|
|
|
|
|
2021-08-03 11:34:04 +02:00
|
|
|
IntegerType Builder::getI8Type() { return IntegerType::get(context, 8); }
|
|
|
|
|
2022-09-12 23:54:16 +00:00
|
|
|
IntegerType Builder::getI16Type() { return IntegerType::get(context, 16); }
|
|
|
|
|
2020-12-17 12:24:45 -08:00
|
|
|
IntegerType Builder::getI32Type() { return IntegerType::get(context, 32); }
|
2020-05-18 08:56:22 +00:00
|
|
|
|
2020-12-17 12:24:45 -08:00
|
|
|
IntegerType Builder::getI64Type() { return IntegerType::get(context, 64); }
|
2020-05-18 08:56:22 +00:00
|
|
|
|
2018-10-30 14:59:22 -07:00
|
|
|
IntegerType Builder::getIntegerType(unsigned width) {
|
2020-12-17 12:24:45 -08:00
|
|
|
return IntegerType::get(context, width);
|
2018-07-08 20:51:38 -07:00
|
|
|
}
|
|
|
|
|
2020-01-10 14:48:24 -05:00
|
|
|
IntegerType Builder::getIntegerType(unsigned width, bool isSigned) {
|
|
|
|
return IntegerType::get(
|
2020-12-17 12:24:45 -08:00
|
|
|
context, width, isSigned ? IntegerType::Signed : IntegerType::Unsigned);
|
2020-01-10 14:48:24 -05:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:46:26 -07:00
|
|
|
FunctionType Builder::getFunctionType(TypeRange inputs, TypeRange results) {
|
2020-12-17 12:24:45 -08:00
|
|
|
return FunctionType::get(context, inputs, results);
|
2018-07-08 20:51:38 -07:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:46:26 -07:00
|
|
|
TupleType Builder::getTupleType(TypeRange elementTypes) {
|
2020-12-17 12:24:45 -08:00
|
|
|
return TupleType::get(context, elementTypes);
|
2019-03-19 10:59:02 -07:00
|
|
|
}
|
|
|
|
|
2019-04-27 18:35:04 -07:00
|
|
|
NoneType Builder::getNoneType() { return NoneType::get(context); }
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Attributes.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-02-28 16:45:30 -08:00
|
|
|
NamedAttribute Builder::getNamedAttr(StringRef name, Attribute val) {
|
2021-11-16 17:21:15 +00:00
|
|
|
return NamedAttribute(getStringAttr(name), val);
|
2019-02-28 16:45:30 -08:00
|
|
|
}
|
|
|
|
|
2019-04-25 09:56:09 -07:00
|
|
|
UnitAttr Builder::getUnitAttr() { return UnitAttr::get(context); }
|
|
|
|
|
2018-10-25 15:46:10 -07:00
|
|
|
BoolAttr Builder::getBoolAttr(bool value) {
|
2021-02-08 09:44:03 +01:00
|
|
|
return BoolAttr::get(context, value);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2019-05-31 09:24:48 -07:00
|
|
|
DictionaryAttr Builder::getDictionaryAttr(ArrayRef<NamedAttribute> value) {
|
2021-02-08 09:44:03 +01:00
|
|
|
return DictionaryAttr::get(context, value);
|
2019-05-31 09:24:48 -07:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:16:32 -04:00
|
|
|
IntegerAttr Builder::getIndexAttr(int64_t value) {
|
|
|
|
return IntegerAttr::get(getIndexType(), APInt(64, value));
|
|
|
|
}
|
|
|
|
|
2018-12-26 11:48:58 -08:00
|
|
|
IntegerAttr Builder::getI64IntegerAttr(int64_t value) {
|
2018-11-15 17:53:51 -08:00
|
|
|
return IntegerAttr::get(getIntegerType(64), APInt(64, value));
|
|
|
|
}
|
|
|
|
|
2020-06-19 10:40:03 -07:00
|
|
|
DenseIntElementsAttr Builder::getBoolVectorAttr(ArrayRef<bool> values) {
|
|
|
|
return DenseIntElementsAttr::get(
|
|
|
|
VectorType::get(static_cast<int64_t>(values.size()), getI1Type()),
|
|
|
|
values);
|
|
|
|
}
|
|
|
|
|
2019-12-02 07:51:27 -08:00
|
|
|
DenseIntElementsAttr Builder::getI32VectorAttr(ArrayRef<int32_t> values) {
|
2020-03-11 15:15:06 -07:00
|
|
|
return DenseIntElementsAttr::get(
|
|
|
|
VectorType::get(static_cast<int64_t>(values.size()), getIntegerType(32)),
|
|
|
|
values);
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseIntElementsAttr Builder::getI64VectorAttr(ArrayRef<int64_t> values) {
|
|
|
|
return DenseIntElementsAttr::get(
|
|
|
|
VectorType::get(static_cast<int64_t>(values.size()), getIntegerType(64)),
|
|
|
|
values);
|
2019-12-02 07:51:27 -08:00
|
|
|
}
|
|
|
|
|
2021-04-20 11:26:44 +00:00
|
|
|
DenseIntElementsAttr Builder::getIndexVectorAttr(ArrayRef<int64_t> values) {
|
|
|
|
return DenseIntElementsAttr::get(
|
|
|
|
VectorType::get(static_cast<int64_t>(values.size()), getIndexType()),
|
|
|
|
values);
|
|
|
|
}
|
|
|
|
|
2022-08-17 22:17:58 +02:00
|
|
|
DenseBoolArrayAttr Builder::getDenseBoolArrayAttr(ArrayRef<bool> values) {
|
|
|
|
return DenseBoolArrayAttr::get(context, values);
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:50:40 +02:00
|
|
|
DenseI8ArrayAttr Builder::getDenseI8ArrayAttr(ArrayRef<int8_t> values) {
|
|
|
|
return DenseI8ArrayAttr::get(context, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseI16ArrayAttr Builder::getDenseI16ArrayAttr(ArrayRef<int16_t> values) {
|
|
|
|
return DenseI16ArrayAttr::get(context, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseI32ArrayAttr Builder::getDenseI32ArrayAttr(ArrayRef<int32_t> values) {
|
|
|
|
return DenseI32ArrayAttr::get(context, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseI64ArrayAttr Builder::getDenseI64ArrayAttr(ArrayRef<int64_t> values) {
|
|
|
|
return DenseI64ArrayAttr::get(context, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseF32ArrayAttr Builder::getDenseF32ArrayAttr(ArrayRef<float> values) {
|
|
|
|
return DenseF32ArrayAttr::get(context, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseF64ArrayAttr Builder::getDenseF64ArrayAttr(ArrayRef<double> values) {
|
|
|
|
return DenseF64ArrayAttr::get(context, values);
|
|
|
|
}
|
|
|
|
|
2020-03-19 11:29:57 -07:00
|
|
|
DenseIntElementsAttr Builder::getI32TensorAttr(ArrayRef<int32_t> values) {
|
|
|
|
return DenseIntElementsAttr::get(
|
|
|
|
RankedTensorType::get(static_cast<int64_t>(values.size()),
|
|
|
|
getIntegerType(32)),
|
|
|
|
values);
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseIntElementsAttr Builder::getI64TensorAttr(ArrayRef<int64_t> values) {
|
|
|
|
return DenseIntElementsAttr::get(
|
|
|
|
RankedTensorType::get(static_cast<int64_t>(values.size()),
|
|
|
|
getIntegerType(64)),
|
|
|
|
values);
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:44:20 -07:00
|
|
|
DenseIntElementsAttr Builder::getIndexTensorAttr(ArrayRef<int64_t> values) {
|
|
|
|
return DenseIntElementsAttr::get(
|
|
|
|
RankedTensorType::get(static_cast<int64_t>(values.size()),
|
|
|
|
getIndexType()),
|
|
|
|
values);
|
|
|
|
}
|
|
|
|
|
2018-12-26 11:48:58 -08:00
|
|
|
IntegerAttr Builder::getI32IntegerAttr(int32_t value) {
|
|
|
|
return IntegerAttr::get(getIntegerType(32), APInt(32, value));
|
|
|
|
}
|
|
|
|
|
2020-01-10 14:48:24 -05:00
|
|
|
IntegerAttr Builder::getSI32IntegerAttr(int32_t value) {
|
|
|
|
return IntegerAttr::get(getIntegerType(32, /*isSigned=*/true),
|
|
|
|
APInt(32, value, /*isSigned=*/true));
|
|
|
|
}
|
|
|
|
|
|
|
|
IntegerAttr Builder::getUI32IntegerAttr(uint32_t value) {
|
|
|
|
return IntegerAttr::get(getIntegerType(32, /*isSigned=*/false),
|
2020-02-21 10:05:07 -05:00
|
|
|
APInt(32, (uint64_t)value, /*isSigned=*/false));
|
2020-01-10 14:48:24 -05:00
|
|
|
}
|
|
|
|
|
2019-09-14 17:02:06 -07:00
|
|
|
IntegerAttr Builder::getI16IntegerAttr(int16_t value) {
|
|
|
|
return IntegerAttr::get(getIntegerType(16), APInt(16, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
IntegerAttr Builder::getI8IntegerAttr(int8_t value) {
|
|
|
|
return IntegerAttr::get(getIntegerType(8), APInt(8, value));
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:53:51 -08:00
|
|
|
IntegerAttr Builder::getIntegerAttr(Type type, int64_t value) {
|
|
|
|
if (type.isIndex())
|
|
|
|
return IntegerAttr::get(type, APInt(64, value));
|
2020-01-10 14:48:24 -05:00
|
|
|
return IntegerAttr::get(
|
|
|
|
type, APInt(type.getIntOrFloatBitWidth(), value, type.isSignedInteger()));
|
2018-11-15 17:53:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
IntegerAttr Builder::getIntegerAttr(Type type, const APInt &value) {
|
|
|
|
return IntegerAttr::get(type, value);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-12-26 11:48:58 -08:00
|
|
|
FloatAttr Builder::getF64FloatAttr(double value) {
|
2018-12-17 07:19:53 -08:00
|
|
|
return FloatAttr::get(getF64Type(), APFloat(value));
|
|
|
|
}
|
|
|
|
|
2018-12-26 11:48:58 -08:00
|
|
|
FloatAttr Builder::getF32FloatAttr(float value) {
|
2018-11-15 17:53:51 -08:00
|
|
|
return FloatAttr::get(getF32Type(), APFloat(value));
|
|
|
|
}
|
|
|
|
|
2019-06-05 22:02:22 -07:00
|
|
|
FloatAttr Builder::getF16FloatAttr(float value) {
|
|
|
|
return FloatAttr::get(getF16Type(), value);
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:53:51 -08:00
|
|
|
FloatAttr Builder::getFloatAttr(Type type, double value) {
|
2018-12-17 07:19:53 -08:00
|
|
|
return FloatAttr::get(type, value);
|
2018-10-20 18:31:49 -07:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:53:51 -08:00
|
|
|
FloatAttr Builder::getFloatAttr(Type type, const APFloat &value) {
|
|
|
|
return FloatAttr::get(type, value);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2021-06-05 11:38:31 -07:00
|
|
|
StringAttr Builder::getStringAttr(const Twine &bytes) {
|
2021-02-08 09:44:03 +01:00
|
|
|
return StringAttr::get(context, bytes);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-10-25 15:46:10 -07:00
|
|
|
ArrayAttr Builder::getArrayAttr(ArrayRef<Attribute> value) {
|
2021-02-08 09:44:03 +01:00
|
|
|
return ArrayAttr::get(context, value);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2020-05-18 09:41:59 -04:00
|
|
|
ArrayAttr Builder::getBoolArrayAttr(ArrayRef<bool> values) {
|
|
|
|
auto attrs = llvm::to_vector<8>(llvm::map_range(
|
|
|
|
values, [this](bool v) -> Attribute { return getBoolAttr(v); }));
|
|
|
|
return getArrayAttr(attrs);
|
|
|
|
}
|
|
|
|
|
2019-04-05 12:19:22 -07:00
|
|
|
ArrayAttr Builder::getI32ArrayAttr(ArrayRef<int32_t> values) {
|
2020-04-13 14:07:38 -07:00
|
|
|
auto attrs = llvm::to_vector<8>(llvm::map_range(
|
|
|
|
values, [this](int32_t v) -> Attribute { return getI32IntegerAttr(v); }));
|
2019-04-05 12:19:22 -07:00
|
|
|
return getArrayAttr(attrs);
|
|
|
|
}
|
|
|
|
ArrayAttr Builder::getI64ArrayAttr(ArrayRef<int64_t> values) {
|
2020-04-13 14:07:38 -07:00
|
|
|
auto attrs = llvm::to_vector<8>(llvm::map_range(
|
|
|
|
values, [this](int64_t v) -> Attribute { return getI64IntegerAttr(v); }));
|
2019-04-05 12:19:22 -07:00
|
|
|
return getArrayAttr(attrs);
|
|
|
|
}
|
|
|
|
|
2019-08-20 01:59:58 -07:00
|
|
|
ArrayAttr Builder::getIndexArrayAttr(ArrayRef<int64_t> values) {
|
2020-04-13 14:07:38 -07:00
|
|
|
auto attrs = llvm::to_vector<8>(
|
|
|
|
llvm::map_range(values, [this](int64_t v) -> Attribute {
|
2019-08-20 01:59:58 -07:00
|
|
|
return getIntegerAttr(IndexType::get(getContext()), v);
|
2020-04-13 14:07:38 -07:00
|
|
|
}));
|
2019-08-20 01:59:58 -07:00
|
|
|
return getArrayAttr(attrs);
|
|
|
|
}
|
|
|
|
|
2019-04-05 12:19:22 -07:00
|
|
|
ArrayAttr Builder::getF32ArrayAttr(ArrayRef<float> values) {
|
2020-04-13 14:07:38 -07:00
|
|
|
auto attrs = llvm::to_vector<8>(llvm::map_range(
|
|
|
|
values, [this](float v) -> Attribute { return getF32FloatAttr(v); }));
|
2019-04-05 12:19:22 -07:00
|
|
|
return getArrayAttr(attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayAttr Builder::getF64ArrayAttr(ArrayRef<double> values) {
|
2020-04-13 14:07:38 -07:00
|
|
|
auto attrs = llvm::to_vector<8>(llvm::map_range(
|
|
|
|
values, [this](double v) -> Attribute { return getF64FloatAttr(v); }));
|
2019-04-05 12:19:22 -07:00
|
|
|
return getArrayAttr(attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayAttr Builder::getStrArrayAttr(ArrayRef<StringRef> values) {
|
2020-04-13 14:07:38 -07:00
|
|
|
auto attrs = llvm::to_vector<8>(llvm::map_range(
|
|
|
|
values, [this](StringRef v) -> Attribute { return getStringAttr(v); }));
|
2019-04-05 12:19:22 -07:00
|
|
|
return getArrayAttr(attrs);
|
|
|
|
}
|
|
|
|
|
[mlir][PDL] Add a PDL Interpreter Dialect
The PDL Interpreter dialect provides a lower level abstraction compared to the PDL dialect, and is targeted towards low level optimization and interpreter code generation. The dialect operations encapsulates low-level pattern match and rewrite "primitives", such as navigating the IR (Operation::getOperand), creating new operations (OpBuilder::create), etc. Many of the operations within this dialect also fuse branching control flow with some form of a predicate comparison operation. This type of fusion reduces the amount of work that an interpreter must do when executing.
An example of this representation is shown below:
```mlir
// The following high level PDL pattern:
pdl.pattern : benefit(1) {
%resultType = pdl.type
%inputOperand = pdl.input
%root, %results = pdl.operation "foo.op"(%inputOperand) -> %resultType
pdl.rewrite %root {
pdl.replace %root with (%inputOperand)
}
}
// May be represented in the interpreter dialect as follows:
module {
func @matcher(%arg0: !pdl.operation) {
pdl_interp.check_operation_name of %arg0 is "foo.op" -> ^bb2, ^bb1
^bb1:
pdl_interp.return
^bb2:
pdl_interp.check_operand_count of %arg0 is 1 -> ^bb3, ^bb1
^bb3:
pdl_interp.check_result_count of %arg0 is 1 -> ^bb4, ^bb1
^bb4:
%0 = pdl_interp.get_operand 0 of %arg0
pdl_interp.is_not_null %0 : !pdl.value -> ^bb5, ^bb1
^bb5:
%1 = pdl_interp.get_result 0 of %arg0
pdl_interp.is_not_null %1 : !pdl.value -> ^bb6, ^bb1
^bb6:
pdl_interp.record_match @rewriters::@rewriter(%0, %arg0 : !pdl.value, !pdl.operation) : benefit(1), loc([%arg0]), root("foo.op") -> ^bb1
}
module @rewriters {
func @rewriter(%arg0: !pdl.value, %arg1: !pdl.operation) {
pdl_interp.replace %arg1 with(%arg0)
pdl_interp.return
}
}
}
```
Differential Revision: https://reviews.llvm.org/D84579
2020-08-26 05:12:07 -07:00
|
|
|
ArrayAttr Builder::getTypeArrayAttr(TypeRange values) {
|
|
|
|
auto attrs = llvm::to_vector<8>(llvm::map_range(
|
|
|
|
values, [](Type v) -> Attribute { return TypeAttr::get(v); }));
|
|
|
|
return getArrayAttr(attrs);
|
|
|
|
}
|
|
|
|
|
Add a generic Linalg op
This CL introduces a linalg.generic op to represent generic tensor contraction operations on views.
A linalg.generic operation requires a numbers of attributes that are sufficient to emit the computation in scalar form as well as compute the appropriate subviews to enable tiling and fusion.
These attributes are very similar to the attributes for existing operations such as linalg.matmul etc and existing operations can be implemented with the generic form.
In the future, most existing operations can be implemented using the generic form.
This CL starts by splitting out most of the functionality of the linalg::NInputsAndOutputs trait into a ViewTrait that queries the per-instance properties of the op. This allows using the attribute informations.
This exposes an ordering of verifiers issue where ViewTrait::verify uses attributes but the verifiers for those attributes have not been run. The desired behavior would be for the verifiers of the attributes specified in the builder to execute first but it is not the case atm. As a consequence, to emit proper error messages and avoid crashing, some of the
linalg.generic methods are defensive as such:
```
unsigned getNumInputs() {
// This is redundant with the `n_views` attribute verifier but ordering of verifiers
// may exhibit cases where we crash instead of emitting an error message.
if (!getAttr("n_views") || n_views().getValue().size() != 2)
return 0;
```
In pretty-printed form, the specific attributes required for linalg.generic are factored out in an independent dictionary named "_". When parsing its content is flattened and the "_name" is dropped. This allows using aliasing for reducing boilerplate at each linalg.generic invocation while benefiting from the Tablegen'd verifier form for each named attribute in the dictionary.
For instance, implementing linalg.matmul in terms of linalg.generic resembles:
```
func @mac(%a: f32, %b: f32, %c: f32) -> f32 {
%d = mulf %a, %b: f32
%e = addf %c, %d: f32
return %e: f32
}
#matmul_accesses = [
(m, n, k) -> (m, k),
(m, n, k) -> (k, n),
(m, n, k) -> (m, n)
]
#matmul_trait = {
doc = "C(m, n) += A(m, k) * B(k, n)",
fun = @mac,
indexing_maps = #matmul_accesses,
library_call = "linalg_matmul",
n_views = [2, 1],
n_loop_types = [2, 1, 0]
}
```
And can be used in multiple places as:
```
linalg.generic #matmul_trait %A, %B, %C [other-attributes] :
!linalg.view<?x?xf32>, !linalg.view<?x?xf32>, !linalg.view<?x?xf32>
```
In the future it would be great to have a mechanism to alias / register a new
linalg.op as a pair of linalg.generic, #trait.
Also, note that with one could theoretically only specify the `doc` string and parse all the attributes from it.
PiperOrigin-RevId: 261338740
2019-08-02 09:53:08 -07:00
|
|
|
ArrayAttr Builder::getAffineMapArrayAttr(ArrayRef<AffineMap> values) {
|
2020-04-13 14:07:38 -07:00
|
|
|
auto attrs = llvm::to_vector<8>(llvm::map_range(
|
|
|
|
values, [](AffineMap v) -> Attribute { return AffineMapAttr::get(v); }));
|
Add a generic Linalg op
This CL introduces a linalg.generic op to represent generic tensor contraction operations on views.
A linalg.generic operation requires a numbers of attributes that are sufficient to emit the computation in scalar form as well as compute the appropriate subviews to enable tiling and fusion.
These attributes are very similar to the attributes for existing operations such as linalg.matmul etc and existing operations can be implemented with the generic form.
In the future, most existing operations can be implemented using the generic form.
This CL starts by splitting out most of the functionality of the linalg::NInputsAndOutputs trait into a ViewTrait that queries the per-instance properties of the op. This allows using the attribute informations.
This exposes an ordering of verifiers issue where ViewTrait::verify uses attributes but the verifiers for those attributes have not been run. The desired behavior would be for the verifiers of the attributes specified in the builder to execute first but it is not the case atm. As a consequence, to emit proper error messages and avoid crashing, some of the
linalg.generic methods are defensive as such:
```
unsigned getNumInputs() {
// This is redundant with the `n_views` attribute verifier but ordering of verifiers
// may exhibit cases where we crash instead of emitting an error message.
if (!getAttr("n_views") || n_views().getValue().size() != 2)
return 0;
```
In pretty-printed form, the specific attributes required for linalg.generic are factored out in an independent dictionary named "_". When parsing its content is flattened and the "_name" is dropped. This allows using aliasing for reducing boilerplate at each linalg.generic invocation while benefiting from the Tablegen'd verifier form for each named attribute in the dictionary.
For instance, implementing linalg.matmul in terms of linalg.generic resembles:
```
func @mac(%a: f32, %b: f32, %c: f32) -> f32 {
%d = mulf %a, %b: f32
%e = addf %c, %d: f32
return %e: f32
}
#matmul_accesses = [
(m, n, k) -> (m, k),
(m, n, k) -> (k, n),
(m, n, k) -> (m, n)
]
#matmul_trait = {
doc = "C(m, n) += A(m, k) * B(k, n)",
fun = @mac,
indexing_maps = #matmul_accesses,
library_call = "linalg_matmul",
n_views = [2, 1],
n_loop_types = [2, 1, 0]
}
```
And can be used in multiple places as:
```
linalg.generic #matmul_trait %A, %B, %C [other-attributes] :
!linalg.view<?x?xf32>, !linalg.view<?x?xf32>, !linalg.view<?x?xf32>
```
In the future it would be great to have a mechanism to alias / register a new
linalg.op as a pair of linalg.generic, #trait.
Also, note that with one could theoretically only specify the `doc` string and parse all the attributes from it.
PiperOrigin-RevId: 261338740
2019-08-02 09:53:08 -07:00
|
|
|
return getArrayAttr(attrs);
|
|
|
|
}
|
|
|
|
|
2023-04-16 17:28:48 -04:00
|
|
|
TypedAttr Builder::getZeroAttr(Type type) {
|
2023-05-11 11:10:46 +02:00
|
|
|
if (llvm::isa<FloatType>(type))
|
2019-09-02 23:43:36 -07:00
|
|
|
return getFloatAttr(type, 0.0);
|
2023-05-11 11:10:46 +02:00
|
|
|
if (llvm::isa<IndexType>(type))
|
2020-08-12 13:07:28 +00:00
|
|
|
return getIndexAttr(0);
|
2023-05-11 11:10:46 +02:00
|
|
|
if (auto integerType = llvm::dyn_cast<IntegerType>(type))
|
|
|
|
return getIntegerAttr(type,
|
|
|
|
APInt(llvm::cast<IntegerType>(type).getWidth(), 0));
|
|
|
|
if (llvm::isa<RankedTensorType, VectorType>(type)) {
|
|
|
|
auto vtType = llvm::cast<ShapedType>(type);
|
2019-01-11 09:12:11 -08:00
|
|
|
auto element = getZeroAttr(vtType.getElementType());
|
|
|
|
if (!element)
|
|
|
|
return {};
|
2019-10-17 20:08:01 -07:00
|
|
|
return DenseElementsAttr::get(vtType, element);
|
2019-01-11 09:12:11 -08:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-10-20 00:11:03 -07:00
|
|
|
// Affine Expressions, Affine Maps, and Integer Sets.
|
2018-07-10 10:59:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-10-08 13:47:18 -07:00
|
|
|
AffineExpr Builder::getAffineDimExpr(unsigned position) {
|
2018-10-08 10:20:25 -07:00
|
|
|
return mlir::getAffineDimExpr(position, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-10-08 13:47:18 -07:00
|
|
|
AffineExpr Builder::getAffineSymbolExpr(unsigned position) {
|
2018-10-08 10:20:25 -07:00
|
|
|
return mlir::getAffineSymbolExpr(position, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2018-10-08 13:47:18 -07:00
|
|
|
AffineExpr Builder::getAffineConstantExpr(int64_t constant) {
|
2018-10-08 10:20:25 -07:00
|
|
|
return mlir::getAffineConstantExpr(constant, context);
|
2018-07-10 10:59:53 -07:00
|
|
|
}
|
|
|
|
|
2019-08-07 10:31:14 -07:00
|
|
|
AffineMap Builder::getEmptyAffineMap() { return AffineMap::get(context); }
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getConstantAffineMap(int64_t val) {
|
2018-10-03 15:39:12 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/0,
|
[MLIR] Improve support for 0-dimensional Affine Maps.
Summary:
Modified AffineMap::get to remove support for the overload which allowed
an ArrayRef of AffineExpr but no context (and gathered the context from a
presumed first entry, resulting in bugs when there were 0 results).
Instead, we support only a ArrayRef and a context, and a version which
takes a single AffineExpr.
Additionally, removed some now needless case logic which previously
special cased which call to AffineMap::get to use.
Reviewers: flaub, bondhugula, rriddle!, nicolasvasilache, ftynse, ulysseB, mravishankar, antiagainst, aartbik
Subscribers: mehdi_amini, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, bader, grosul1, frgossen, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78226
2020-04-15 11:12:47 -07:00
|
|
|
getAffineConstantExpr(val));
|
2018-08-24 23:38:14 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getDimIdentityMap() {
|
[MLIR] Improve support for 0-dimensional Affine Maps.
Summary:
Modified AffineMap::get to remove support for the overload which allowed
an ArrayRef of AffineExpr but no context (and gathered the context from a
presumed first entry, resulting in bugs when there were 0 results).
Instead, we support only a ArrayRef and a context, and a version which
takes a single AffineExpr.
Additionally, removed some now needless case logic which previously
special cased which call to AffineMap::get to use.
Reviewers: flaub, bondhugula, rriddle!, nicolasvasilache, ftynse, ulysseB, mravishankar, antiagainst, aartbik
Subscribers: mehdi_amini, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, bader, grosul1, frgossen, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78226
2020-04-15 11:12:47 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, getAffineDimExpr(0));
|
2018-08-24 23:38:14 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getMultiDimIdentityMap(unsigned rank) {
|
2018-10-08 13:47:18 -07:00
|
|
|
SmallVector<AffineExpr, 4> dimExprs;
|
2018-10-08 11:10:11 -07:00
|
|
|
dimExprs.reserve(rank);
|
|
|
|
for (unsigned i = 0; i < rank; ++i)
|
|
|
|
dimExprs.push_back(getAffineDimExpr(i));
|
[MLIR] Improve support for 0-dimensional Affine Maps.
Summary:
Modified AffineMap::get to remove support for the overload which allowed
an ArrayRef of AffineExpr but no context (and gathered the context from a
presumed first entry, resulting in bugs when there were 0 results).
Instead, we support only a ArrayRef and a context, and a version which
takes a single AffineExpr.
Additionally, removed some now needless case logic which previously
special cased which call to AffineMap::get to use.
Reviewers: flaub, bondhugula, rriddle!, nicolasvasilache, ftynse, ulysseB, mravishankar, antiagainst, aartbik
Subscribers: mehdi_amini, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, bader, grosul1, frgossen, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78226
2020-04-15 11:12:47 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/rank, /*symbolCount=*/0, dimExprs,
|
|
|
|
context);
|
2018-10-08 11:10:11 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getSymbolIdentityMap() {
|
2018-10-08 10:20:25 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/1,
|
[MLIR] Improve support for 0-dimensional Affine Maps.
Summary:
Modified AffineMap::get to remove support for the overload which allowed
an ArrayRef of AffineExpr but no context (and gathered the context from a
presumed first entry, resulting in bugs when there were 0 results).
Instead, we support only a ArrayRef and a context, and a version which
takes a single AffineExpr.
Additionally, removed some now needless case logic which previously
special cased which call to AffineMap::get to use.
Reviewers: flaub, bondhugula, rriddle!, nicolasvasilache, ftynse, ulysseB, mravishankar, antiagainst, aartbik
Subscribers: mehdi_amini, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, bader, grosul1, frgossen, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78226
2020-04-15 11:12:47 -07:00
|
|
|
getAffineSymbolExpr(0));
|
2018-08-24 23:38:14 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getSingleDimShiftAffineMap(int64_t shift) {
|
2018-10-03 15:34:57 -07:00
|
|
|
// expr = d0 + shift.
|
2018-10-08 10:20:25 -07:00
|
|
|
auto expr = getAffineDimExpr(0) + shift;
|
[MLIR] Improve support for 0-dimensional Affine Maps.
Summary:
Modified AffineMap::get to remove support for the overload which allowed
an ArrayRef of AffineExpr but no context (and gathered the context from a
presumed first entry, resulting in bugs when there were 0 results).
Instead, we support only a ArrayRef and a context, and a version which
takes a single AffineExpr.
Additionally, removed some now needless case logic which previously
special cased which call to AffineMap::get to use.
Reviewers: flaub, bondhugula, rriddle!, nicolasvasilache, ftynse, ulysseB, mravishankar, antiagainst, aartbik
Subscribers: mehdi_amini, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, bader, grosul1, frgossen, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78226
2020-04-15 11:12:47 -07:00
|
|
|
return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, expr);
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:39:24 -07:00
|
|
|
AffineMap Builder::getShiftedAffineMap(AffineMap map, int64_t shift) {
|
2018-10-08 13:47:18 -07:00
|
|
|
SmallVector<AffineExpr, 4> shiftedResults;
|
2018-10-09 16:39:24 -07:00
|
|
|
shiftedResults.reserve(map.getNumResults());
|
2019-10-17 20:08:01 -07:00
|
|
|
for (auto resultExpr : map.getResults())
|
2018-10-09 10:59:27 -07:00
|
|
|
shiftedResults.push_back(resultExpr + shift);
|
[MLIR] Improve support for 0-dimensional Affine Maps.
Summary:
Modified AffineMap::get to remove support for the overload which allowed
an ArrayRef of AffineExpr but no context (and gathered the context from a
presumed first entry, resulting in bugs when there were 0 results).
Instead, we support only a ArrayRef and a context, and a version which
takes a single AffineExpr.
Additionally, removed some now needless case logic which previously
special cased which call to AffineMap::get to use.
Reviewers: flaub, bondhugula, rriddle!, nicolasvasilache, ftynse, ulysseB, mravishankar, antiagainst, aartbik
Subscribers: mehdi_amini, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, bader, grosul1, frgossen, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78226
2020-04-15 11:12:47 -07:00
|
|
|
return AffineMap::get(map.getNumDims(), map.getNumSymbols(), shiftedResults,
|
|
|
|
context);
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
|
|
|
|
2018-07-19 09:52:39 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-04-30 21:28:36 -07:00
|
|
|
// OpBuilder
|
2018-07-24 10:15:13 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-12-11 16:26:08 -08:00
|
|
|
/// Insert the given operation at the current insertion point and return it.
|
|
|
|
Operation *OpBuilder::insert(Operation *op) {
|
|
|
|
if (block)
|
|
|
|
block->getOperations().insert(insertPoint, op);
|
2020-04-30 21:28:36 -07:00
|
|
|
|
|
|
|
if (listener)
|
|
|
|
listener->notifyOperationInserted(op);
|
2019-12-11 16:26:08 -08:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2020-04-03 19:53:13 +02:00
|
|
|
Block *OpBuilder::createBlock(Region *parent, Region::iterator insertPt,
|
2021-05-23 14:08:31 -07:00
|
|
|
TypeRange argTypes, ArrayRef<Location> locs) {
|
2019-07-12 10:43:11 -07:00
|
|
|
assert(parent && "expected valid parent region");
|
2022-01-18 18:28:51 -08:00
|
|
|
assert(argTypes.size() == locs.size() && "argument location mismatch");
|
2019-07-12 10:43:11 -07:00
|
|
|
if (insertPt == Region::iterator())
|
|
|
|
insertPt = parent->end();
|
2018-08-24 21:13:19 -07:00
|
|
|
|
2019-07-12 10:43:11 -07:00
|
|
|
Block *b = new Block();
|
2021-05-23 14:08:31 -07:00
|
|
|
b->addArguments(argTypes, locs);
|
2019-07-12 10:43:11 -07:00
|
|
|
parent->getBlocks().insert(insertPt, b);
|
2018-12-27 15:06:22 -08:00
|
|
|
setInsertionPointToEnd(b);
|
2020-04-30 21:28:36 -07:00
|
|
|
|
|
|
|
if (listener)
|
|
|
|
listener->notifyBlockCreated(b);
|
2018-07-24 10:15:13 -07:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2020-04-03 19:53:13 +02:00
|
|
|
/// Add new block with 'argTypes' arguments and set the insertion point to the
|
|
|
|
/// end of it. The block is placed before 'insertBefore'.
|
2021-05-23 14:08:31 -07:00
|
|
|
Block *OpBuilder::createBlock(Block *insertBefore, TypeRange argTypes,
|
|
|
|
ArrayRef<Location> locs) {
|
2019-07-12 10:43:11 -07:00
|
|
|
assert(insertBefore && "expected valid insertion block");
|
2020-04-03 19:53:13 +02:00
|
|
|
return createBlock(insertBefore->getParent(), Region::iterator(insertBefore),
|
2021-05-23 14:08:31 -07:00
|
|
|
argTypes, locs);
|
2019-07-12 10:43:11 -07:00
|
|
|
}
|
|
|
|
|
2018-08-07 09:12:35 -07:00
|
|
|
/// Create an operation given the fields represented as an OperationState.
|
2022-03-23 21:37:26 +00:00
|
|
|
Operation *OpBuilder::create(const OperationState &state) {
|
2019-12-11 16:26:08 -08:00
|
|
|
return insert(Operation::create(state));
|
2018-08-07 09:12:35 -07:00
|
|
|
}
|
2019-06-05 10:50:10 -07:00
|
|
|
|
2022-03-23 21:37:26 +00:00
|
|
|
/// Creates an operation with the given fields.
|
|
|
|
Operation *OpBuilder::create(Location loc, StringAttr opName,
|
|
|
|
ValueRange operands, TypeRange types,
|
|
|
|
ArrayRef<NamedAttribute> attributes,
|
|
|
|
BlockRange successors,
|
|
|
|
MutableArrayRef<std::unique_ptr<Region>> regions) {
|
|
|
|
OperationState state(loc, opName, operands, types, attributes, successors,
|
|
|
|
regions);
|
|
|
|
return create(state);
|
|
|
|
}
|
|
|
|
|
2019-06-05 10:50:10 -07:00
|
|
|
/// Attempts to fold the given operation and places new results within
|
2019-12-13 12:21:42 -08:00
|
|
|
/// 'results'. Returns success if the operation was folded, failure otherwise.
|
|
|
|
/// Note: This function does not erase the operation on a successful fold.
|
|
|
|
LogicalResult OpBuilder::tryFold(Operation *op,
|
2019-12-23 14:45:01 -08:00
|
|
|
SmallVectorImpl<Value> &results) {
|
2021-11-03 19:57:36 +00:00
|
|
|
ResultRange opResults = op->getResults();
|
|
|
|
|
|
|
|
results.reserve(opResults.size());
|
2019-12-13 12:21:42 -08:00
|
|
|
auto cleanupFailure = [&] {
|
2021-11-03 19:57:36 +00:00
|
|
|
results.assign(opResults.begin(), opResults.end());
|
2019-12-13 12:21:42 -08:00
|
|
|
return failure();
|
2019-06-05 10:50:10 -07:00
|
|
|
};
|
|
|
|
|
2019-12-13 12:21:42 -08:00
|
|
|
// If this operation is already a constant, there is nothing to do.
|
2020-01-13 17:21:04 +01:00
|
|
|
if (matchPattern(op, m_Constant()))
|
2019-12-13 12:21:42 -08:00
|
|
|
return cleanupFailure();
|
|
|
|
|
|
|
|
// Check to see if any operands to the operation is constant and whether
|
|
|
|
// the operation knows how to constant fold itself.
|
2019-06-05 10:50:10 -07:00
|
|
|
SmallVector<Attribute, 4> constOperands(op->getNumOperands());
|
2021-11-03 19:57:36 +00:00
|
|
|
for (unsigned i = 0, e = constOperands.size(); i != e; ++i)
|
2019-12-13 12:21:42 -08:00
|
|
|
matchPattern(op->getOperand(i), m_Constant(&constOperands[i]));
|
|
|
|
|
|
|
|
// Try to fold the operation.
|
|
|
|
SmallVector<OpFoldResult, 4> foldResults;
|
|
|
|
if (failed(op->fold(constOperands, foldResults)) || foldResults.empty())
|
|
|
|
return cleanupFailure();
|
|
|
|
|
|
|
|
// A temporary builder used for creating constants during folding.
|
|
|
|
OpBuilder cstBuilder(context);
|
|
|
|
SmallVector<Operation *, 1> generatedConstants;
|
|
|
|
|
|
|
|
// Populate the results with the folded results.
|
|
|
|
Dialect *dialect = op->getDialect();
|
2021-11-03 19:57:36 +00:00
|
|
|
for (auto it : llvm::zip(foldResults, opResults.getTypes())) {
|
|
|
|
Type expectedType = std::get<1>(it);
|
|
|
|
|
2019-12-13 12:21:42 -08:00
|
|
|
// Normal values get pushed back directly.
|
2021-11-03 19:57:36 +00:00
|
|
|
if (auto value = std::get<0>(it).dyn_cast<Value>()) {
|
|
|
|
if (value.getType() != expectedType)
|
|
|
|
return cleanupFailure();
|
|
|
|
|
2019-12-13 12:21:42 -08:00
|
|
|
results.push_back(value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, try to materialize a constant operation.
|
|
|
|
if (!dialect)
|
|
|
|
return cleanupFailure();
|
|
|
|
|
|
|
|
// Ask the dialect to materialize a constant operation for this value.
|
2021-11-03 19:57:36 +00:00
|
|
|
Attribute attr = std::get<0>(it).get<Attribute>();
|
|
|
|
auto *constOp = dialect->materializeConstant(cstBuilder, attr, expectedType,
|
|
|
|
op->getLoc());
|
2019-12-13 12:21:42 -08:00
|
|
|
if (!constOp) {
|
|
|
|
// Erase any generated constants.
|
|
|
|
for (Operation *cst : generatedConstants)
|
|
|
|
cst->erase();
|
|
|
|
return cleanupFailure();
|
|
|
|
}
|
2020-03-12 14:06:14 -07:00
|
|
|
assert(matchPattern(constOp, m_Constant()));
|
2019-12-13 12:21:42 -08:00
|
|
|
|
|
|
|
generatedConstants.push_back(constOp);
|
|
|
|
results.push_back(constOp->getResult(0));
|
2019-06-05 10:50:10 -07:00
|
|
|
}
|
|
|
|
|
2019-12-13 12:21:42 -08:00
|
|
|
// If we were successful, insert any generated constants.
|
|
|
|
for (Operation *cst : generatedConstants)
|
|
|
|
insert(cst);
|
|
|
|
|
|
|
|
return success();
|
2019-06-05 10:50:10 -07:00
|
|
|
}
|
2020-10-18 21:10:55 -07:00
|
|
|
|
2023-01-08 14:15:07 -08:00
|
|
|
Operation *OpBuilder::clone(Operation &op, IRMapping &mapper) {
|
2020-10-18 21:10:55 -07:00
|
|
|
Operation *newOp = op.clone(mapper);
|
|
|
|
// The `insert` call below handles the notification for inserting `newOp`
|
|
|
|
// itself. But if `newOp` has any regions, we need to notify the listener
|
|
|
|
// about any ops that got inserted inside those regions as part of cloning.
|
|
|
|
if (listener) {
|
|
|
|
auto walkFn = [&](Operation *walkedOp) {
|
|
|
|
listener->notifyOperationInserted(walkedOp);
|
|
|
|
};
|
|
|
|
for (Region ®ion : newOp->getRegions())
|
|
|
|
region.walk(walkFn);
|
|
|
|
}
|
|
|
|
return insert(newOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation *OpBuilder::clone(Operation &op) {
|
2023-01-08 14:15:07 -08:00
|
|
|
IRMapping mapper;
|
2020-10-18 21:10:55 -07:00
|
|
|
return clone(op, mapper);
|
|
|
|
}
|