MLIR  22.0.0git
Classes | Namespaces | Macros | Typedefs | Functions | Variables
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>

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

 mlir
 Include the generated interface declarations.
 
 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. More...
 

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. More...
 
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. More...
 
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. More...
 
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. More...
 
static OperationgetCommonDefiningOp (const ValueVector &values)
 Return the operation that defines all values in the vector. More...
 
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. More...
 
static void performReplaceBlockArg (RewriterBase &rewriter, BlockArgument arg, Value 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. More...
 
template<typename T >
static T moveAndReset (T &obj)
 Helper function that moves and returns the given object. More...
 
static void reportNewIrLegalizationFatalError (const Pattern &pattern, const SetVector< Operation * > &newOps, const SetVector< Operation * > &modifiedOps, const SetVector< Block * > &insertedBlocks)
 Report a fatal error indicating that newly produced or modified IR could not be legalized. More...
 
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 FailureOr< SmallVector< Value > > pdllConvertValues (ConversionPatternRewriter &rewriter, ValueRange values)
 Remap the given value using the rewriter and the type converter in the provided config. More...
 
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. More...
 

Variables

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

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "dialect-conversion"

Definition at line 32 of file DialectConversion.cpp.

Typedef Documentation

◆ ValueVector

using ValueVector = SmallVector<Value, 1>

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

Definition at line 101 of file DialectConversion.cpp.

Function Documentation

◆ applyConversion()

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

◆ composeLegalityCallbacks()

◆ computeInsertPoint() [1/2]

static 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 70 of file DialectConversion.cpp.

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

◆ computeInsertPoint() [2/2]

static 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 60 of file DialectConversion.cpp.

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

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

◆ convertFuncOpTypes()

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

◆ findCommonAncestor()

static 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 4037 of file DialectConversion.cpp.

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

Referenced by mlir::applyAnalysisConversion().

◆ getCommonDefiningOp()

static 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 187 of file DialectConversion.cpp.

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

◆ getReplacementValues()

static 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 1765 of file DialectConversion.cpp.

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

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

◆ isPureTypeConversion()

static 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 200 of file DialectConversion.cpp.

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

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

◆ legalizeUnresolvedMaterialization()

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

◆ logFailure()

template<typename... Args>
static 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 49 of file DialectConversion.cpp.

◆ logSuccess()

template<typename... Args>
static 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 36 of file DialectConversion.cpp.

◆ moveAndReset()

template<typename T >
static 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 2526 of file DialectConversion.cpp.

◆ pdllConvertValues()

static FailureOr<SmallVector<Value> > pdllConvertValues ( ConversionPatternRewriter rewriter,
ValueRange  values 
)
static

Remap the given value using the rewriter and the type converter in the provided config.

Definition at line 3906 of file DialectConversion.cpp.

References mlir::remark::failed(), and mlir::ConversionPatternRewriter::getRemappedValues().

Referenced by mlir::registerConversionPDLFunctions().

◆ performReplaceBlockArg()

static void performReplaceBlockArg ( RewriterBase rewriter,
BlockArgument  arg,
Value  repl 
)
static

◆ reportNewIrLegalizationFatalError()

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

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

Definition at line 2596 of file DialectConversion.cpp.

References mlir::Pattern::getDebugName(), mlir::Operation::getName(), mlir::Block::getParentOp(), 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 183 of file DialectConversion.cpp.

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