21 #include "llvm/ADT/STLExtras.h"
24 #define GEN_PASS_DEF_MATHLEGALIZETOF32
25 #include "mlir/Dialect/Math/Transforms/Passes.h.inc"
38 struct LegalizeToF32Pass final
39 : mlir::math::impl::MathLegalizeToF32Base<LegalizeToF32Pass> {
40 void runOnOperation()
override;
47 [](
Type type) -> std::optional<Type> {
return type; });
49 if (type.getWidth() < 32)
53 typeConverter.
addConversion([](ShapedType type) -> std::optional<Type> {
54 if (
auto elemTy = dyn_cast<FloatType>(type.getElementType()))
60 return b.
create<arith::ExtFOp>(loc, target, input);
68 return typeConverter.
isLegal(op);
71 target.
addLegalOp<arith::ExtFOp, arith::TruncFOp>();
85 for (
auto [result, newType, origType] : llvm::zip_equal(
87 if (newType != origType)
88 result = rewriter.
create<arith::TruncFOp>(loc, origType, result);
96 patterns.
add<LegalizeToF32RewritePattern>(typeConverter,
100 void LegalizeToF32Pass::runOnOperation() {
111 return signalPassFailure();
static MLIRContext * getContext(OpFoldResult val)
This class implements a pattern rewriter for use with ConversionPatterns.
void replaceOp(Operation *op, ValueRange newValues) override
PatternRewriter hook for replacing an operation.
Base class for the conversion patterns.
This class describes a specific conversion target.
void addLegalOp(OperationName op)
Register the given operations as legal.
void addDynamicallyLegalDialect(const DynamicLegalityCallbackFn &callback, StringRef name, Names... names)
Register the operations of the given dialects as dynamically legal, i.e.
This class provides support for representing a failure result, or a valid value of type T.
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 * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Operation is the basic unit of execution within MLIR.
Location getLoc()
The source location the operation was defined or derived from.
result_type_range getResultTypes()
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.
void addConversion(FnT &&callback)
Register a conversion function.
bool isLegal(Type type) const
Return true if the given type is legal for this type converter, i.e.
void addTargetMaterialization(FnT &&callback)
This method registers a materialization that will be called when converting type from an illegal,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
This class provides an abstraction over the different types of ranges over Values.
void populateLegalizeToF32Patterns(RewritePatternSet &patterns, TypeConverter &typeConverter)
void populateLegalizeToF32TypeConverter(TypeConverter &typeConverter)
void populateLegalizeToF32ConversionTarget(ConversionTarget &target, TypeConverter &typeConverter)
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
FailureOr< Operation * > convertOpResultTypes(Operation *op, ValueRange operands, const TypeConverter &converter, ConversionPatternRewriter &rewriter)
Generic utility to convert op result types according to type converter without knowing exact op type.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.