MLIR 22.0.0git
DialectConversion.cpp File Reference
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Config/mlir-config.h"
#include "mlir/IR/Block.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Dominance.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/Iterators.h"
#include "mlir/IR/Operation.h"
#include "mlir/Interfaces/FunctionInterfaces.h"
#include "mlir/Rewrite/PatternApplicator.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLog.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/ScopedPrinter.h"
#include <optional>
#include <utility>

Go to the source code of this file.

Classes

struct  mlir::detail::ConversionPatternRewriterImpl
struct  mlir::detail::ConversionPatternRewriterImpl::SingleEraseRewriter
 A rewriter that keeps track of erased ops and blocks. More...
struct  mlir::OperationConverter
class  ApplyConversionAction
 This is the type of Action that is dispatched when a conversion is applied. More...

Namespaces

namespace  mlir
 Include the generated interface declarations.
namespace  mlir::detail
 AttrTypeReplacer.

Macros

#define DEBUG_TYPE   "dialect-conversion"

Typedefs

using ValueVector = SmallVector<Value, 1>
 A vector of SSA values, optimized for the most common case of a single value.

Functions

template<typename... Args>
static void logSuccess (llvm::ScopedPrinter &os, StringRef fmt, Args &&...args)
 A utility function to log a successful result for the given reason.
template<typename... Args>
static void logFailure (llvm::ScopedPrinter &os, StringRef fmt, Args &&...args)
 A utility function to log a failure result for the given reason.
static OpBuilder::InsertPoint computeInsertPoint (Value value)
 Helper function that computes an insertion point where the given value is defined and can be used without a dominance violation.
static OpBuilder::InsertPoint computeInsertPoint (ArrayRef< Value > vals)
 Helper function that computes an insertion point where the given values are defined and can be used without a dominance violation.
static OperationgetCommonDefiningOp (const ValueVector &values)
 Return the operation that defines all values in the vector.
static bool isPureTypeConversion (const ValueVector &values)
 A vector of values is a pure type conversion if all values are defined by the same operation and the operation has the kPureTypeConversionMarker attribute.
static void performReplaceValue (RewriterBase &rewriter, Value from, Value repl)
 Replace all uses of from with repl.
static SmallVector< ValuegetReplacementValues (ConversionPatternRewriterImpl &impl, ValueRange fromRange, const SmallVector< SmallVector< Value > > &toRange, const TypeConverter *converter)
 Given that fromRange is about to be replaced with toRange, compute replacement values with the types of fromRange.
template<typename T>
static T moveAndReset (T &obj)
 Helper function that moves and returns the given object.
static void reportNewIrLegalizationFatalError (const Pattern &pattern, const SetVector< Operation * > &newOps, const SetVector< Operation * > &modifiedOps)
 Report a fatal error indicating that newly produced or modified IR could not be legalized.
template<typename RangeT>
static void reconcileUnrealizedCastsImpl (RangeT castOps, function_ref< bool(UnrealizedConversionCastOp)> isCastOpOfInterestFn, SmallVectorImpl< UnrealizedConversionCastOp > *remainingCastOps)
 Try to reconcile all given UnrealizedConversionCastOps and store the left-over ops in remainingCastOps (if provided).
static void mlir::reconcileUnrealizedCasts (const DenseMap< UnrealizedConversionCastOp, UnresolvedMaterializationInfo > &castOps, SmallVectorImpl< UnrealizedConversionCastOp > *remainingCastOps)
static LogicalResult legalizeUnresolvedMaterialization (RewriterBase &rewriter, UnrealizedConversionCastOp op, const UnresolvedMaterializationInfo &info)
static LogicalResult convertFuncOpTypes (FunctionOpInterface funcOp, const TypeConverter &typeConverter, ConversionPatternRewriter &rewriter)
static ConversionTarget::DynamicLegalityCallbackFn composeLegalityCallbacks (ConversionTarget::DynamicLegalityCallbackFn oldCallback, ConversionTarget::DynamicLegalityCallbackFn newCallback)
static LogicalResult applyConversion (ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config, OpConversionMode mode)
static OperationfindCommonAncestor (ArrayRef< Operation * > ops)
 Find a common IsolatedFromAbove ancestor of the given ops.

Variables

static const StringRef kPureTypeConversionMarker = "__pure_type_conversion__"
 Marker attribute for pure type conversions.

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "dialect-conversion"

Definition at line 33 of file DialectConversion.cpp.

Typedef Documentation

◆ ValueVector

A vector of SSA values, optimized for the most common case of a single value.

Definition at line 118 of file DialectConversion.cpp.

Function Documentation

◆ applyConversion()

LogicalResult applyConversion ( ArrayRef< Operation * > ops,
const ConversionTarget & target,
const FrozenRewritePatternSet & patterns,
ConversionConfig config,
OpConversionMode mode )
static

◆ composeLegalityCallbacks()

ConversionTarget::DynamicLegalityCallbackFn composeLegalityCallbacks ( ConversionTarget::DynamicLegalityCallbackFn oldCallback,
ConversionTarget::DynamicLegalityCallbackFn newCallback )
static

Definition at line 3936 of file DialectConversion.cpp.

References result.

◆ computeInsertPoint() [1/2]

OpBuilder::InsertPoint computeInsertPoint ( ArrayRef< Value > vals)
static

Helper function that computes an insertion point where the given values are defined and can be used without a dominance violation.

Definition at line 71 of file DialectConversion.cpp.

References computeInsertPoint(), mlir::DominanceInfo::dominates(), mlir::OpBuilder::InsertPoint::getBlock(), and mlir::OpBuilder::InsertPoint::getPoint().

◆ computeInsertPoint() [2/2]

OpBuilder::InsertPoint computeInsertPoint ( Value value)
static

Helper function that computes an insertion point where the given value is defined and can be used without a dominance violation.

Definition at line 61 of file DialectConversion.cpp.

References mlir::Block::begin(), and mlir::Value::getParentBlock().

Referenced by computeInsertPoint(), mlir::detail::ConversionPatternRewriterImpl::findOrBuildReplacementValue(), getReplacementValues(), and mlir::detail::ConversionPatternRewriterImpl::remapValues().

◆ convertFuncOpTypes()

LogicalResult convertFuncOpTypes ( FunctionOpInterface funcOp,
const TypeConverter & typeConverter,
ConversionPatternRewriter & rewriter )
static

Definition at line 3772 of file DialectConversion.cpp.

References result, and success().

◆ findCommonAncestor()

Operation * findCommonAncestor ( ArrayRef< Operation * > ops)
static

Find a common IsolatedFromAbove ancestor of the given ops.

If at least one op is a top-level module op (which is expected to be isolated from above), return that op.

Definition at line 4165 of file DialectConversion.cpp.

References mlir::Operation::getParentOp(), mlir::Operation::getParentWithTrait(), mlir::Operation::hasTrait(), mlir::Operation::isAncestor(), and mlir::Operation::isProperAncestor().

◆ getCommonDefiningOp()

Operation * getCommonDefiningOp ( const ValueVector & values)
static

Return the operation that defines all values in the vector.

Return nullptr if the values are not defined by the same operation.

Definition at line 204 of file DialectConversion.cpp.

Referenced by mlir::transform::TrackingListener::findReplacementOp(), isPureTypeConversion(), and mlir::detail::ConversionPatternRewriterImpl::lookupOrDefault().

◆ getReplacementValues()

SmallVector< Value > getReplacementValues ( ConversionPatternRewriterImpl & impl,
ValueRange fromRange,
const SmallVector< SmallVector< Value > > & toRange,
const TypeConverter * converter )
static

Given that fromRange is about to be replaced with toRange, compute replacement values with the types of fromRange.

Definition at line 1826 of file DialectConversion.cpp.

References computeInsertPoint(), mlir::Value::getLoc(), mlir::Value::getType(), TypeRange, mlir::Value::use_empty(), and ValueRange.

Referenced by mlir::detail::ConversionPatternRewriterImpl::replaceAllUsesWith(), and mlir::detail::ConversionPatternRewriterImpl::replaceOp().

◆ isPureTypeConversion()

bool isPureTypeConversion ( const ValueVector & values)
static

A vector of values is a pure type conversion if all values are defined by the same operation and the operation has the kPureTypeConversionMarker attribute.

Definition at line 217 of file DialectConversion.cpp.

References getCommonDefiningOp(), mlir::Operation::hasAttr(), and kPureTypeConversionMarker.

Referenced by mlir::detail::ConversionPatternRewriterImpl::buildUnresolvedMaterialization(), and mlir::detail::ConversionPatternRewriterImpl::lookupOrDefault().

◆ legalizeUnresolvedMaterialization()

LogicalResult legalizeUnresolvedMaterialization ( RewriterBase & rewriter,
UnrealizedConversionCastOp op,
const UnresolvedMaterializationInfo & info )
static

◆ logFailure()

template<typename... Args>
void logFailure ( llvm::ScopedPrinter & os,
StringRef fmt,
Args &&... args )
static

A utility function to log a failure result for the given reason.

Definition at line 50 of file DialectConversion.cpp.

◆ logSuccess()

template<typename... Args>
void logSuccess ( llvm::ScopedPrinter & os,
StringRef fmt,
Args &&... args )
static

A utility function to log a successful result for the given reason.

Definition at line 37 of file DialectConversion.cpp.

◆ moveAndReset()

template<typename T>
T moveAndReset ( T & obj)
static

Helper function that moves and returns the given object.

Also resets the original object, so that it is in a valid, empty state again.

Definition at line 2635 of file DialectConversion.cpp.

References result.

◆ performReplaceValue()

◆ reconcileUnrealizedCastsImpl()

template<typename RangeT>
void reconcileUnrealizedCastsImpl ( RangeT castOps,
function_ref< bool(UnrealizedConversionCastOp)> isCastOpOfInterestFn,
SmallVectorImpl< UnrealizedConversionCastOp > * remainingCastOps )
static

Try to reconcile all given UnrealizedConversionCastOps and store the left-over ops in remainingCastOps (if provided).

See documentation in DialectConversion.h for more details. The isCastOpOfInterestFn is used to filter the cast ops to proceed: the algorithm may visit an operand (or user) which is a cast op, but will not try to reconcile it if not in the filtered set.

Definition at line 3112 of file DialectConversion.cpp.

References mlir::Value::getDefiningOp(), and mlir::Operation::getOperands().

Referenced by mlir::reconcileUnrealizedCasts().

◆ reportNewIrLegalizationFatalError()

void reportNewIrLegalizationFatalError ( const Pattern & pattern,
const SetVector< Operation * > & newOps,
const SetVector< Operation * > & modifiedOps )
static

Report a fatal error indicating that newly produced or modified IR could not be legalized.

Definition at line 2704 of file DialectConversion.cpp.

References mlir::Pattern::getDebugName(), mlir::Operation::getName(), and mlir::OperationName::getStringRef().

Variable Documentation

◆ kPureTypeConversionMarker

const StringRef kPureTypeConversionMarker = "__pure_type_conversion__"
static

Marker attribute for pure type conversions.

I.e., mappings whose only purpose is to resolve a type mismatch. (In contrast, mappings that point to the replacement values of a "replaceOp" call, etc., are not pure type conversions.)

Definition at line 200 of file DialectConversion.cpp.

Referenced by mlir::detail::ConversionPatternRewriterImpl::buildUnresolvedMaterialization(), mlir::OperationConverter::convertOperations(), and isPureTypeConversion().