Skip to content

[MLIR] Allow constFoldBinaryOp to fold (T1, T1) -> T2 #151410

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 114 additions & 33 deletions mlir/include/mlir/Dialect/CommonFolders.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
#ifndef MLIR_DIALECT_COMMONFOLDERS_H
#define MLIR_DIALECT_COMMONFOLDERS_H

#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributeInterfaces.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/BuiltinTypeInterfaces.h"
#include "mlir/IR/Types.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"

#include <cassert>
#include <cstddef>
#include <optional>

namespace mlir {
Expand All @@ -30,11 +36,13 @@ class PoisonAttr;
/// Uses `resultType` for the type of the returned attribute.
/// Optional PoisonAttr template argument allows to specify 'poison' attribute
/// which will be directly propagated to result.
template <class AttrElementT,
template <class AttrElementT, //
class ElementValueT = typename AttrElementT::ValueType,
class PoisonAttr = ub::PoisonAttr,
class ResultAttrElementT = AttrElementT,
class ResultElementValueT = typename ResultAttrElementT::ValueType,
class CalculationT = function_ref<
std::optional<ElementValueT>(ElementValueT, ElementValueT)>>
std::optional<ResultElementValueT>(ElementValueT, ElementValueT)>>
Attribute constFoldBinaryOpConditional(ArrayRef<Attribute> operands,
Type resultType,
CalculationT &&calculate) {
Expand Down Expand Up @@ -65,7 +73,7 @@ Attribute constFoldBinaryOpConditional(ArrayRef<Attribute> operands,
if (!calRes)
return {};

return AttrElementT::get(resultType, *calRes);
return ResultAttrElementT::get(resultType, *calRes);
}

if (isa<SplatElementsAttr>(operands[0]) &&
Expand Down Expand Up @@ -99,7 +107,7 @@ Attribute constFoldBinaryOpConditional(ArrayRef<Attribute> operands,
return {};
auto lhsIt = *maybeLhsIt;
auto rhsIt = *maybeRhsIt;
SmallVector<ElementValueT, 4> elementResults;
SmallVector<ResultElementValueT, 4> elementResults;
elementResults.reserve(lhs.getNumElements());
for (size_t i = 0, e = lhs.getNumElements(); i < e; ++i, ++lhsIt, ++rhsIt) {
auto elementResult = calculate(*lhsIt, *rhsIt);
Expand All @@ -119,11 +127,13 @@ Attribute constFoldBinaryOpConditional(ArrayRef<Attribute> operands,
/// attribute.
/// Optional PoisonAttr template argument allows to specify 'poison' attribute
/// which will be directly propagated to result.
template <class AttrElementT,
template <class AttrElementT, //
class ElementValueT = typename AttrElementT::ValueType,
class PoisonAttr = ub::PoisonAttr,
class ResultAttrElementT = AttrElementT,
class ResultElementValueT = typename ResultAttrElementT::ValueType,
class CalculationT = function_ref<
std::optional<ElementValueT>(ElementValueT, ElementValueT)>>
std::optional<ResultElementValueT>(ElementValueT, ElementValueT)>>
Attribute constFoldBinaryOpConditional(ArrayRef<Attribute> operands,
CalculationT &&calculate) {
assert(operands.size() == 2 && "binary op takes two operands");
Expand All @@ -139,64 +149,73 @@ Attribute constFoldBinaryOpConditional(ArrayRef<Attribute> operands,
return operands[1];
}

auto getResultType = [](Attribute attr) -> Type {
auto getAttrType = [](Attribute attr) -> Type {
if (auto typed = dyn_cast_or_null<TypedAttr>(attr))
return typed.getType();
return {};
};

Type lhsType = getResultType(operands[0]);
Type rhsType = getResultType(operands[1]);
Type lhsType = getAttrType(operands[0]);
Type rhsType = getAttrType(operands[1]);
if (!lhsType || !rhsType)
return {};
if (lhsType != rhsType)
return {};

return constFoldBinaryOpConditional<AttrElementT, ElementValueT, PoisonAttr,
ResultAttrElementT, ResultElementValueT,
CalculationT>(
operands, lhsType, std::forward<CalculationT>(calculate));
}

template <class AttrElementT,
class ElementValueT = typename AttrElementT::ValueType,
class PoisonAttr = void,
class PoisonAttr = void, //
class ResultAttrElementT = AttrElementT,
class ResultElementValueT = typename ResultAttrElementT::ValueType,
class CalculationT =
function_ref<ElementValueT(ElementValueT, ElementValueT)>>
function_ref<ResultElementValueT(ElementValueT, ElementValueT)>>
Attribute constFoldBinaryOp(ArrayRef<Attribute> operands, Type resultType,
CalculationT &&calculate) {
return constFoldBinaryOpConditional<AttrElementT, ElementValueT, PoisonAttr>(
return constFoldBinaryOpConditional<AttrElementT, ElementValueT, PoisonAttr,
ResultAttrElementT>(
operands, resultType,
[&](ElementValueT a, ElementValueT b) -> std::optional<ElementValueT> {
return calculate(a, b);
});
[&](ElementValueT a, ElementValueT b)
-> std::optional<ResultElementValueT> { return calculate(a, b); });
}

template <class AttrElementT,
template <class AttrElementT, //
class ElementValueT = typename AttrElementT::ValueType,
class PoisonAttr = ub::PoisonAttr,
class ResultAttrElementT = AttrElementT,
class ResultElementValueT = typename ResultAttrElementT::ValueType,
class CalculationT =
function_ref<ElementValueT(ElementValueT, ElementValueT)>>
function_ref<ResultElementValueT(ElementValueT, ElementValueT)>>
Attribute constFoldBinaryOp(ArrayRef<Attribute> operands,
CalculationT &&calculate) {
return constFoldBinaryOpConditional<AttrElementT, ElementValueT, PoisonAttr>(
return constFoldBinaryOpConditional<AttrElementT, ElementValueT, PoisonAttr,
ResultAttrElementT>(
operands,
[&](ElementValueT a, ElementValueT b) -> std::optional<ElementValueT> {
return calculate(a, b);
});
[&](ElementValueT a, ElementValueT b)
-> std::optional<ResultElementValueT> { return calculate(a, b); });
}

/// Performs constant folding `calculate` with element-wise behavior on the one
/// attributes in `operands` and returns the result if possible.
/// Uses `resultType` for the type of the returned attribute.
/// Optional PoisonAttr template argument allows to specify 'poison' attribute
/// which will be directly propagated to result.
template <class AttrElementT,
template <class AttrElementT, //
class ElementValueT = typename AttrElementT::ValueType,
class PoisonAttr = ub::PoisonAttr,
class ResultAttrElementT = AttrElementT,
class ResultElementValueT = typename ResultAttrElementT::ValueType,
class CalculationT =
function_ref<std::optional<ElementValueT>(ElementValueT)>>
function_ref<std::optional<ResultElementValueT>(ElementValueT)>>
Attribute constFoldUnaryOpConditional(ArrayRef<Attribute> operands,
Type resultType,
CalculationT &&calculate) {
if (!llvm::getSingleElement(operands))
if (!resultType || !llvm::getSingleElement(operands))
return {};

static_assert(
Expand All @@ -214,7 +233,7 @@ Attribute constFoldUnaryOpConditional(ArrayRef<Attribute> operands,
auto res = calculate(op.getValue());
if (!res)
return {};
return AttrElementT::get(op.getType(), *res);
return ResultAttrElementT::get(resultType, *res);
}
if (isa<SplatElementsAttr>(operands[0])) {
// Both operands are splats so we can avoid expanding the values out and
Expand All @@ -224,7 +243,7 @@ Attribute constFoldUnaryOpConditional(ArrayRef<Attribute> operands,
auto elementResult = calculate(op.getSplatValue<ElementValueT>());
if (!elementResult)
return {};
return DenseElementsAttr::get(op.getType(), *elementResult);
return DenseElementsAttr::get(cast<ShapedType>(resultType), *elementResult);
} else if (isa<ElementsAttr>(operands[0])) {
// Operands are ElementsAttr-derived; perform an element-wise fold by
// expanding the values.
Expand All @@ -234,27 +253,89 @@ Attribute constFoldUnaryOpConditional(ArrayRef<Attribute> operands,
if (!maybeOpIt)
return {};
auto opIt = *maybeOpIt;
SmallVector<ElementValueT> elementResults;
SmallVector<ResultElementValueT> elementResults;
elementResults.reserve(op.getNumElements());
for (size_t i = 0, e = op.getNumElements(); i < e; ++i, ++opIt) {
auto elementResult = calculate(*opIt);
if (!elementResult)
return {};
elementResults.push_back(*elementResult);
}
return DenseElementsAttr::get(op.getShapedType(), elementResults);
return DenseElementsAttr::get(cast<ShapedType>(resultType), elementResults);
}
return {};
}

template <class AttrElementT,
/// Performs constant folding `calculate` with element-wise behavior on the one
/// attributes in `operands` and returns the result if possible.
/// Uses the operand element type for the element type of the returned
/// attribute.
/// Optional PoisonAttr template argument allows to specify 'poison' attribute
/// which will be directly propagated to result.
template <class AttrElementT, //
class ElementValueT = typename AttrElementT::ValueType,
class PoisonAttr = ub::PoisonAttr,
class ResultAttrElementT = AttrElementT,
class ResultElementValueT = typename ResultAttrElementT::ValueType,
class CalculationT =
function_ref<std::optional<ResultElementValueT>(ElementValueT)>>
Attribute constFoldUnaryOpConditional(ArrayRef<Attribute> operands,
CalculationT &&calculate) {
if (!llvm::getSingleElement(operands))
return {};

static_assert(
std::is_void_v<PoisonAttr> || !llvm::is_incomplete_v<PoisonAttr>,
"PoisonAttr is undefined, either add a dependency on UB dialect or pass "
"void as template argument to opt-out from poison semantics.");
if constexpr (!std::is_void_v<PoisonAttr>) {
if (isa<PoisonAttr>(operands[0]))
return operands[0];
}

auto getAttrType = [](Attribute attr) -> Type {
if (auto typed = dyn_cast_or_null<TypedAttr>(attr))
return typed.getType();
return {};
};

Type operandType = getAttrType(operands[0]);
if (!operandType)
return {};

return constFoldUnaryOpConditional<AttrElementT, ElementValueT, PoisonAttr,
ResultAttrElementT, ResultElementValueT,
CalculationT>(
operands, operandType, std::forward<CalculationT>(calculate));
}

template <class AttrElementT, //
class ElementValueT = typename AttrElementT::ValueType,
class PoisonAttr = ub::PoisonAttr,
class ResultAttrElementT = AttrElementT,
class ResultElementValueT = typename ResultAttrElementT::ValueType,
class CalculationT = function_ref<ResultElementValueT(ElementValueT)>>
Attribute constFoldUnaryOp(ArrayRef<Attribute> operands, Type resultType,
CalculationT &&calculate) {
return constFoldUnaryOpConditional<AttrElementT, ElementValueT, PoisonAttr,
ResultAttrElementT>(
operands, resultType,
[&](ElementValueT a) -> std::optional<ResultElementValueT> {
return calculate(a);
});
}

template <class AttrElementT, //
class ElementValueT = typename AttrElementT::ValueType,
class PoisonAttr = ub::PoisonAttr,
class CalculationT = function_ref<ElementValueT(ElementValueT)>>
class ResultAttrElementT = AttrElementT,
class ResultElementValueT = typename ResultAttrElementT::ValueType,
class CalculationT = function_ref<ResultElementValueT(ElementValueT)>>
Attribute constFoldUnaryOp(ArrayRef<Attribute> operands,
CalculationT &&calculate) {
return constFoldUnaryOpConditional<AttrElementT, ElementValueT, PoisonAttr>(
operands, [&](ElementValueT a) -> std::optional<ElementValueT> {
return constFoldUnaryOpConditional<AttrElementT, ElementValueT, PoisonAttr,
ResultAttrElementT>(
operands, [&](ElementValueT a) -> std::optional<ResultElementValueT> {
return calculate(a);
});
}
Expand Down
22 changes: 22 additions & 0 deletions mlir/test/Dialect/common_folders.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: mlir-opt %s --test-fold-type-converting-op --split-input-file | FileCheck %s

// CHECK-LABEL: @test_fold_unary_op_f32_to_si32(
func.func @test_fold_unary_op_f32_to_si32() -> tensor<4x2xsi32> {
// CHECK-NEXT: %[[POSITIVE_ONE:.*]] = arith.constant dense<1> : tensor<4x2xsi32>
// CHECK-NEXT: return %[[POSITIVE_ONE]] : tensor<4x2xsi32>
%operand = arith.constant dense<5.1> : tensor<4x2xf32>
%sign = test.sign %operand : (tensor<4x2xf32>) -> tensor<4x2xsi32>
return %sign : tensor<4x2xsi32>
}

// -----

// CHECK-LABEL: @test_fold_binary_op_f32_to_i1(
func.func @test_fold_binary_op_f32_to_i1() -> tensor<8xi1> {
// CHECK-NEXT: %[[FALSE:.*]] = arith.constant dense<false> : tensor<8xi1>
// CHECK-NEXT: return %[[FALSE]] : tensor<8xi1>
%lhs = arith.constant dense<5.1> : tensor<8xf32>
%rhs = arith.constant dense<4.2> : tensor<8xf32>
%less_than = test.less_than %lhs, %rhs : (tensor<8xf32>, tensor<8xf32>) -> tensor<8xi1>
return %less_than : tensor<8xi1>
}
20 changes: 20 additions & 0 deletions mlir/test/lib/Dialect/Test/TestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,26 @@ def OpP : TEST_Op<"op_p"> {
let results = (outs I32);
}

// Test constant-folding a pattern that maps `(F32) -> SI32`.
def SignOp : TEST_Op<"sign", [SameOperandsAndResultShape]> {
let arguments = (ins RankedTensorOf<[F32]>:$operand);
let results = (outs RankedTensorOf<[SI32]>:$result);

let assemblyFormat = [{
$operand attr-dict `:` functional-type(operands, results)
}];
}

// Test constant-folding a pattern that maps `(F32, F32) -> I1`.
def LessThanOp : TEST_Op<"less_than", [SameOperandsAndResultShape]> {
let arguments = (ins RankedTensorOf<[F32]>:$lhs, RankedTensorOf<[F32]>:$rhs);
let results = (outs RankedTensorOf<[I1]>:$result);

let assemblyFormat = [{
$lhs `,` $rhs attr-dict `:` functional-type(operands, results)
}];
}

// Test same operand name enforces equality condition check.
def TestEqualArgsPattern : Pat<(OpN $a, $a), (OpO $a)>;

Expand Down
Loading