//===-- OneToNTypeConversion.cpp - Utils for 1:N type conversion-*- C++ -*-===// // // Licensed under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "mlir/Transforms/OneToNTypeConversion.h" #include "mlir/Interfaces/FunctionInterfaces.h" #include "mlir/Transforms/GreedyPatternRewriteDriver.h" #include "llvm/ADT/SmallSet.h" #include <unordered_map> usingnamespacellvm; usingnamespacemlir; std::optional<SmallVector<Value>> OneToNTypeConverter::materializeTargetConversion(OpBuilder &builder, Location loc, TypeRange resultTypes, Value input) const { … } TypeRange OneToNTypeMapping::getConvertedTypes(unsigned originalTypeNo) const { … } ValueRange OneToNTypeMapping::getConvertedValues(ValueRange convertedValues, unsigned originalValueNo) const { … } void OneToNTypeMapping::convertLocation( Value originalValue, unsigned originalValueNo, llvm::SmallVectorImpl<Location> &result) const { … } void OneToNTypeMapping::convertLocations( ValueRange originalValues, llvm::SmallVectorImpl<Location> &result) const { … } static bool isIdentityConversion(Type originalType, TypeRange convertedTypes) { … } bool OneToNTypeMapping::hasNonIdentityConversion() const { … } namespace { enum class CastKind { … }; } // namespace /// Mapping of enum values to string values. StringRef getCastKindName(CastKind kind) { … } /// Attribute name that is used to annotate inserted unrealized casts with their /// kind (source, argument, or target). static const char *const castKindAttrName = …; /// Builds an `UnrealizedConversionCastOp` from the given inputs to the given /// result types. Returns the result values of the cast. static ValueRange buildUnrealizedCast(OpBuilder &builder, TypeRange resultTypes, ValueRange inputs, CastKind kind) { … } /// Builds one `UnrealizedConversionCastOp` for each of the given original /// values using the respective target types given in the provided conversion /// mapping and returns the results of these casts. If the conversion mapping of /// a value maps a type to itself (i.e., is an identity conversion), then no /// cast is inserted and the original value is returned instead. /// Note that these unrealized casts are different from target materializations /// in that they are *always* inserted, even if they immediately fold away, such /// that patterns always see valid intermediate IR, whereas materializations are /// only used in the places where the unrealized casts *don't* fold away. static SmallVector<Value> buildUnrealizedForwardCasts(ValueRange originalValues, OneToNTypeMapping &conversion, RewriterBase &rewriter, CastKind kind) { … } /// Builds one `UnrealizedConversionCastOp` for each sequence of the given /// original values to one value of the type they originated from, i.e., a /// "reverse" conversion from N converted values back to one value of the /// original type, using the given (forward) type conversion. If a given value /// was mapped to a value of the same type (i.e., the conversion in the mapping /// is an identity conversion), then the "converted" value is returned without /// cast. /// Note that these unrealized casts are different from source materializations /// in that they are *always* inserted, even if they immediately fold away, such /// that patterns always see valid intermediate IR, whereas materializations are /// only used in the places where the unrealized casts *don't* fold away. static SmallVector<Value> buildUnrealizedBackwardsCasts(ValueRange convertedValues, const OneToNTypeMapping &typeConversion, RewriterBase &rewriter) { … } void OneToNPatternRewriter::replaceOp(Operation *op, ValueRange newValues, const OneToNTypeMapping &resultMapping) { … } Block *OneToNPatternRewriter::applySignatureConversion( Block *block, OneToNTypeMapping &argumentConversion) { … } LogicalResult OneToNConversionPattern::matchAndRewrite(Operation *op, PatternRewriter &rewriter) const { … } namespace mlir { // This function applies the provided patterns using // `applyPatternsAndFoldGreedily` and then replaces all newly inserted // `UnrealizedConversionCastOps` that haven't folded away. ("Backward" casts // from target to source types inserted by a `OneToNConversionPattern` normally // fold away with the "forward" casts from source to target types inserted by // the next pattern.) To understand which casts are "newly inserted", all casts // inserted by this pass are annotated with a string attribute that also // documents which kind of the cast (source, argument, or target). LogicalResult applyPartialOneToNConversion(Operation *op, OneToNTypeConverter &typeConverter, const FrozenRewritePatternSet &patterns) { … } namespace { class FunctionOpInterfaceSignatureConversion : public OneToNConversionPattern { … }; } // namespace void populateOneToNFunctionOpInterfaceTypeConversionPattern( StringRef functionLikeOpName, TypeConverter &converter, RewritePatternSet &patterns) { … } } // namespace mlir