Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
10 changes: 10 additions & 0 deletions flang/include/flang/Optimizer/Builder/FIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
return createRealConstant(loc, realType, 0u);
}

/// Create a real constant of type \p realType with value one.
mlir::Value createRealOneConstant(mlir::Location loc, mlir::Type realType) {
return createRealConstant(loc, realType, 1u);
}

/// Create a slot for a local on the stack. Besides the variable's type and
/// shape, it may be given name, pinned, or target attributes.
mlir::Value allocateLocal(mlir::Location loc, mlir::Type ty,
Expand Down Expand Up @@ -856,6 +861,11 @@ mlir::Value genLenOfCharacter(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Value createZeroValue(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Type type);

/// Create a one value of a given numerical or logical \p type (`true`
/// for logical types).
mlir::Value createOneValue(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Type type);

/// Get the integer constants of triplet and compute the extent.
std::optional<std::int64_t> getExtentFromTriplet(mlir::Value lb, mlir::Value ub,
mlir::Value stride);
Expand Down
19 changes: 19 additions & 0 deletions flang/lib/Optimizer/Builder/FIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,25 @@ mlir::Value fir::factory::createZeroValue(fir::FirOpBuilder &builder,
"numeric or logical type");
}

mlir::Value fir::factory::createOneValue(fir::FirOpBuilder &builder,
mlir::Location loc, mlir::Type type) {
mlir::Type i1 = builder.getIntegerType(1);
if (mlir::isa<fir::LogicalType>(type) || type == i1)
return builder.createConvert(loc, type, builder.createBool(loc, true));
if (fir::isa_integer(type))
return builder.createIntegerConstant(loc, type, 0);
if (fir::isa_real(type))
return builder.createRealOneConstant(loc, type);
if (fir::isa_complex(type)) {
fir::factory::Complex complexHelper(builder, loc);
mlir::Type partType = complexHelper.getComplexPartType(type);
mlir::Value onePart = builder.createRealOneConstant(loc, partType);
return complexHelper.createComplex(type, onePart, onePart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imaginary part must be 0.

}
fir::emitFatalError(loc, "internal: trying to generate one value of non "
"numeric or logical type");
}

std::optional<std::int64_t>
fir::factory::getExtentFromTriplet(mlir::Value lb, mlir::Value ub,
mlir::Value stride) {
Expand Down
50 changes: 50 additions & 0 deletions flang/lib/Optimizer/HLFIR/Transforms/SimplifyHLFIRIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "mlir/IR/Location.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include <type_traits>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?


namespace hlfir {
#define GEN_PASS_DEF_SIMPLIFYHLFIRINTRINSICS
Expand Down Expand Up @@ -931,6 +932,37 @@ class SumAsElementalConverter
mlir::Value genScalarAdd(mlir::Value value1, mlir::Value value2);
};

/// Reduction converter for Product.
class ProductAsElementalConverter
: public NumericReductionAsElementalConverterBase<hlfir::ProductOp> {
using Base = NumericReductionAsElementalConverterBase;

public:
ProductAsElementalConverter(hlfir::ProductOp op,
mlir::PatternRewriter &rewriter)
: Base{op, rewriter} {}

private:
virtual llvm::SmallVector<mlir::Value> genReductionInitValues(
[[maybe_unused]] mlir::ValueRange oneBasedIndices,
[[maybe_unused]] const llvm::SmallVectorImpl<mlir::Value> &extents)
final {
return {fir::factory::createOneValue(builder, loc, getResultElementType())};
}
virtual llvm::SmallVector<mlir::Value>
reduceOneElement(const llvm::SmallVectorImpl<mlir::Value> &currentValue,
hlfir::Entity array,
mlir::ValueRange oneBasedIndices) final {
checkReductions(currentValue);
hlfir::Entity elementValue =
hlfir::loadElementAt(loc, builder, array, oneBasedIndices);
return {genScalarMult(currentValue[0], elementValue)};
}

// Generate scalar multiplication of the two values (of the same data type).
mlir::Value genScalarMult(mlir::Value value1, mlir::Value value2);
};

/// Base class for logical reductions like ALL, ANY, COUNT.
/// They do not have MASK and FastMathFlags.
template <typename OpT>
Expand Down Expand Up @@ -1194,6 +1226,20 @@ mlir::Value SumAsElementalConverter::genScalarAdd(mlir::Value value1,
llvm_unreachable("unsupported SUM reduction type");
}

mlir::Value ProductAsElementalConverter::genScalarMult(mlir::Value value1,
mlir::Value value2) {
mlir::Type ty = value1.getType();
assert(ty == value2.getType() && "reduction values' types do not match");
if (mlir::isa<mlir::FloatType>(ty))
return mlir::arith::MulFOp::create(builder, loc, value1, value2);
else if (mlir::isa<mlir::ComplexType>(ty))
return fir::MulcOp::create(builder, loc, value1, value2);
else if (mlir::isa<mlir::IntegerType>(ty))
return mlir::arith::MulIOp::create(builder, loc, value1, value2);

llvm_unreachable("unsupported MUL reduction type");
}

mlir::Value ReductionAsElementalConverter::genMaskValue(
mlir::Value mask, mlir::Value isPresentPred, mlir::ValueRange indices) {
mlir::OpBuilder::InsertionGuard guard(builder);
Expand Down Expand Up @@ -1265,6 +1311,9 @@ class ReductionConversion : public mlir::OpRewritePattern<Op> {
} else if constexpr (std::is_same_v<Op, hlfir::SumOp>) {
SumAsElementalConverter converter{op, rewriter};
return converter.convert();
} else if constexpr (std::is_same_v<Op, hlfir::ProductOp>) {
ProductAsElementalConverter converter{op, rewriter};
return converter.convert();
}
return rewriter.notifyMatchFailure(op, "unexpected reduction operation");
}
Expand Down Expand Up @@ -3158,6 +3207,7 @@ class SimplifyHLFIRIntrinsics
mlir::RewritePatternSet patterns(context);
patterns.insert<TransposeAsElementalConversion>(context);
patterns.insert<ReductionConversion<hlfir::SumOp>>(context);
patterns.insert<ReductionConversion<hlfir::ProductOp>>(context);
patterns.insert<ArrayShiftConversion<hlfir::CShiftOp>>(context);
patterns.insert<ArrayShiftConversion<hlfir::EOShiftOp>>(context);
patterns.insert<CmpCharOpConversion>(context);
Expand Down
Loading