23#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/SetVector.h"
27#define GEN_PASS_DEF_MATHEXTENDTOSUPPORTEDTYPES
28#include "mlir/Dialect/Math/Transforms/Passes.h.inc"
35 ExtendToSupportedTypesRewritePattern(
const TypeConverter &converter,
37 : ConversionPattern(converter, MatchAnyOpTypeTag{}, 1, context) {}
39 matchAndRewrite(Operation *op, ArrayRef<Value> operands,
40 ConversionPatternRewriter &rewriter)
const override;
43struct ExtendToSupportedTypesPass
45 ExtendToSupportedTypesPass> {
46 using math::impl::MathExtendToSupportedTypesBase<
47 ExtendToSupportedTypesPass>::MathExtendToSupportedTypesBase;
49 void runOnOperation()
override;
57 typeConverter.addConversion(
58 [](
Type type) -> std::optional<Type> {
return type; });
59 typeConverter.addConversion(
60 [&sourceTypes, targetType](FloatType type) -> std::optional<Type> {
61 if (!sourceTypes.contains(type))
66 typeConverter.addConversion(
67 [&sourceTypes, targetType](ShapedType type) -> std::optional<Type> {
68 if (
auto elemTy = dyn_cast<FloatType>(type.getElementType()))
69 if (!sourceTypes.contains(elemTy))
70 return type.clone(targetType);
74 typeConverter.addTargetMaterialization(
78 arith::ExtFOp::Properties{});
79 extFOp.setFastmath(arith::FastMathFlags::contract);
86 target.markUnknownOpDynamicallyLegal([&typeConverter](
Operation *op) ->
bool {
88 return typeConverter.isLegal(op);
91 target.addLegalOp<FmaOp>();
92 target.addLegalOp<arith::ExtFOp, arith::TruncFOp>();
95LogicalResult ExtendToSupportedTypesRewritePattern::matchAndRewrite(
97 ConversionPatternRewriter &rewriter)
const {
100 FailureOr<Operation *> legalized =
101 convertOpResultTypes(op, operands, *converter, rewriter);
102 if (failed(legalized))
106 for (
auto [
result, newType, origType] : llvm::zip_equal(
108 if (newType != origType) {
109 auto truncFOp = arith::TruncFOp::create(rewriter, loc, origType,
result);
110 truncFOp.setFastmath(arith::FastMathFlags::contract);
111 result = truncFOp.getResult();
114 rewriter.replaceOp(op, results);
120 patterns.
add<ExtendToSupportedTypesRewritePattern>(typeConverter,
124void ExtendToSupportedTypesPass::runOnOperation() {
131 emitError(UnknownLoc::get(ctx),
"could not map target type '" +
133 "' to a known floating-point type");
142 emitError(UnknownLoc::get(ctx),
"could not map source type '" +
144 "' to a known floating-point type");
147 sourceTypes.insert(extraType);
151 sourceTypes.insert(
b.getF64Type());
152 sourceTypes.insert(
b.getF32Type());
161 if (failed(applyPartialConversion(op,
target, std::move(patterns))))
This class is a general helper class for creating context-global objects like types,...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
Location getLoc()
The source location the operation was defined or derived from.
result_type_range getResultTypes()
void signalPassFailure()
Signal that some invariant was broken when running.
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
::mlir::Pass::ListOption< std::string > extraTypeStrs
FloatType parseFloatType(MLIRContext *ctx, StringRef name)
void populateExtendToSupportedTypesPatterns(RewritePatternSet &patterns, const TypeConverter &typeConverter)
void populateExtendToSupportedTypesConversionTarget(ConversionTarget &target, TypeConverter &typeConverter)
void populateExtendToSupportedTypesTypeConverter(TypeConverter &typeConverter, const SetVector< Type > &sourceTypes, Type targetType)
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::SetVector< T, Vector, Set, N > SetVector