MLIR  20.0.0git
Public Types | Public Member Functions | Protected Member Functions | List of all members
mlir::detail::AttrTypeReplacerBase< Concrete > Class Template Reference

This class provides a base utility for replacing attributes/types, and their sub elements. More...

#include "mlir/IR/AttrTypeSubElements.h"

+ Inheritance diagram for mlir::detail::AttrTypeReplacerBase< Concrete >:

Public Types

template<typename T >
using ReplaceFnResult = std::optional< std::pair< T, WalkResult > >
 A replacement mapping function, which returns either std::nullopt (to signal the element wasn't handled), or a pair of the replacement element and a WalkResult. More...
 
template<typename T >
using ReplaceFn = std::function< ReplaceFnResult< T >(T)>
 

Public Member Functions

void replaceElementsIn (Operation *op, bool replaceAttrs=true, bool replaceLocs=false, bool replaceTypes=false)
 Replace the elements within the given operation. More...
 
void recursivelyReplaceElementsIn (Operation *op, bool replaceAttrs=true, bool replaceLocs=false, bool replaceTypes=false)
 Replace the elements within the given operation, and all nested operations. More...
 
void addReplacement (ReplaceFn< Attribute > fn)
 Register a replacement function for mapping a given attribute or type. More...
 
void addReplacement (ReplaceFn< Type > fn)
 
template<typename FnT , typename T = typename llvm::function_traits< std::decay_t<FnT>>::template arg_t<0>, typename BaseT = std::conditional_t<std::is_base_of_v<Attribute, T>, Attribute, Type>, typename ResultT = std::invoke_result_t<FnT, T>>
std::enable_if_t<!std::is_same_v< T, BaseT >||!std::is_convertible_v< ResultT, ReplaceFnResult< BaseT > > > addReplacement (FnT &&callback)
 Register a replacement function that doesn't match the default signature, either because it uses a derived parameter type, or it uses a simplified result type. More...
 

Protected Member Functions

Attribute replaceBase (Attribute attr)
 Invokes the registered replacement functions from most recently registered to least recently registered until a successful replacement is returned. More...
 
Type replaceBase (Type type)
 

Detailed Description

template<typename Concrete>
class mlir::detail::AttrTypeReplacerBase< Concrete >

This class provides a base utility for replacing attributes/types, and their sub elements.

Multiple replacement functions may be registered.

This base utility is uncached. Users can choose between two cached versions of this replacer:

Concrete implementations implement the following replace entry functions:

Definition at line 134 of file AttrTypeSubElements.h.

Member Typedef Documentation

◆ ReplaceFn

template<typename Concrete >
template<typename T >
using mlir::detail::AttrTypeReplacerBase< Concrete >::ReplaceFn = std::function<ReplaceFnResult<T>(T)>

Definition at line 165 of file AttrTypeSubElements.h.

◆ ReplaceFnResult

template<typename Concrete >
template<typename T >
using mlir::detail::AttrTypeReplacerBase< Concrete >::ReplaceFnResult = std::optional<std::pair<T, WalkResult> >

A replacement mapping function, which returns either std::nullopt (to signal the element wasn't handled), or a pair of the replacement element and a WalkResult.

Definition at line 163 of file AttrTypeSubElements.h.

Member Function Documentation

◆ addReplacement() [1/3]

template<typename Concrete >
template<typename FnT , typename T = typename llvm::function_traits< std::decay_t<FnT>>::template arg_t<0>, typename BaseT = std::conditional_t<std::is_base_of_v<Attribute, T>, Attribute, Type>, typename ResultT = std::invoke_result_t<FnT, T>>
std::enable_if_t<!std::is_same_v<T, BaseT> || !std::is_convertible_v<ResultT, ReplaceFnResult<BaseT> > > mlir::detail::AttrTypeReplacerBase< Concrete >::addReplacement ( FnT &&  callback)
inline

Register a replacement function that doesn't match the default signature, either because it uses a derived parameter type, or it uses a simplified result type.

Definition at line 200 of file AttrTypeSubElements.h.

References mlir::detail::AttrTypeReplacerBase< Concrete >::addReplacement(), and mlir::WalkResult::advance().

◆ addReplacement() [2/3]

template<typename Concrete >
void detail::AttrTypeReplacerBase::addReplacement ( ReplaceFn< Attribute fn)

Register a replacement function for mapping a given attribute or type.

AttrTypeReplacerBase.

A replacement function must be convertible to any of the following forms(where T is a class derived from Type or Attribute, and BaseT is either Type or Attribute respectively):

  • std::optional<BaseT>(T)
    • This either returns a valid Attribute/Type in the case of success, nullptr in the case of failure, or std::nullopt to signify that additional replacement functions may be applied (i.e. this function doesn't handle that instance).
  • std::optional<std::pair<BaseT, WalkResult>>(T)
    • Similar to the above, but also allows specifying a WalkResult to control the replacement of sub elements of a given attribute or type. Returning a skip result, for example, will not recursively process the resultant attribute or type value.

Note: When replacing, the mostly recently added replacement functions will be invoked first.

Definition at line 74 of file AttrTypeSubElements.cpp.

Referenced by mlir::detail::AttrTypeReplacerBase< Concrete >::addReplacement(), mlir::spirv::convertMemRefTypesAndAttrs(), handleLoopAnnotations(), mlir::LLVM::legalizeDIExpressionsRecursively(), remapInlinedLocations(), mlir::Attribute::replace(), mlir::Type::replace(), and replaceAllSymbolUsesImpl().

◆ addReplacement() [3/3]

template<typename Concrete >
void detail::AttrTypeReplacerBase::addReplacement ( ReplaceFn< Type fn)

Definition at line 80 of file AttrTypeSubElements.cpp.

◆ recursivelyReplaceElementsIn()

template<typename Concrete >
void detail::AttrTypeReplacerBase::recursivelyReplaceElementsIn ( Operation op,
bool  replaceAttrs = true,
bool  replaceLocs = false,
bool  replaceTypes = false 
)

Replace the elements within the given operation, and all nested operations.

Definition at line 137 of file AttrTypeSubElements.cpp.

References mlir::Operation::walk().

Referenced by mlir::spirv::convertMemRefTypesAndAttrs(), handleLoopAnnotations(), mlir::LLVM::legalizeDIExpressionsRecursively(), and remapInlinedLocations().

◆ replaceBase() [1/2]

template<typename Concrete >
Attribute detail::AttrTypeReplacerBase::replaceBase ( Attribute  attr)
protected

Invokes the registered replacement functions from most recently registered to least recently registered until a successful replacement is returned.

Unless skipping is requested, invokes replace on sub-elements of the current attr/type.

Definition at line 221 of file AttrTypeSubElements.cpp.

◆ replaceBase() [2/2]

template<typename Concrete >
Type detail::AttrTypeReplacerBase::replaceBase ( Type  type)
protected

Definition at line 227 of file AttrTypeSubElements.cpp.

References replaceElementImpl().

◆ replaceElementsIn()

template<typename Concrete >
void detail::AttrTypeReplacerBase::replaceElementsIn ( Operation op,
bool  replaceAttrs = true,
bool  replaceLocs = false,
bool  replaceTypes = false 
)

Replace the elements within the given operation.

If replaceAttrs is true, this updates the attribute dictionary of the operation. If replaceLocs is true, this also updates its location, and the locations of any nested block arguments. If replaceTypes is true, this also updates the result types of the operation, and the types of any nested block arguments.

Definition at line 86 of file AttrTypeSubElements.cpp.

References mlir::Operation::getAttrDictionary(), mlir::Operation::getLoc(), mlir::Operation::getRegions(), mlir::Operation::getResults(), mlir::Operation::setAttrs(), and mlir::Operation::setLoc().

Referenced by replaceAllSymbolUsesImpl().


The documentation for this class was generated from the following files: