MLIR  20.0.0git
Classes | Public Types | Public Member Functions | List of all members
mlir::CyclicReplacerCache< InT, OutT > Class Template Reference

A cache for replacer-like functions that map values between two domains. More...

#include "mlir/Support/CyclicReplacerCache.h"

+ Inheritance diagram for mlir::CyclicReplacerCache< InT, OutT >:

Classes

struct  CacheEntry
 A possibly unresolved cache entry. More...
 

Public Types

using CycleBreakerFn = std::function< std::optional< OutT >(InT)>
 User-provided replacement function & cycle-breaking functions. More...
 

Public Member Functions

 CyclicReplacerCache ()=delete
 
 CyclicReplacerCache (CycleBreakerFn cycleBreaker)
 
CacheEntry lookupOrInit (InT element)
 Lookup the cache for a pre-calculated replacement for element. More...
 

Detailed Description

template<typename InT, typename OutT>
class mlir::CyclicReplacerCache< InT, OutT >

A cache for replacer-like functions that map values between two domains.

The difference compared to just using a map to cache in-out pairs is that this class is able to handle replacer logic that is self-recursive (and thus may cause infinite recursion in the naive case).

This class provides a hook for the user to perform cycle pruning when a cycle is identified, and is able to perform context-sensitive caching so that the replacement result for an input that is part of a pruned cycle can be distinct from the replacement result for the same input when it is not part of a cycle.

In addition, this class allows deferring cycle pruning until specific inputs are repeated. This is useful for cases where not all elements in a cycle can perform pruning. The user still must guarantee that at least one element in any given cycle can perform pruning. Even if not, an assertion will eventually be tripped instead of infinite recursion (the run-time is linearly bounded by the maximum cycle length of its input).

WARNING: This class works best with InT & OutT that are trivial scalar types. The input/output elements will be frequently copied and hashed.

Definition at line 52 of file CyclicReplacerCache.h.

Member Typedef Documentation

◆ CycleBreakerFn

template<typename InT , typename OutT >
using mlir::CyclicReplacerCache< InT, OutT >::CycleBreakerFn = std::function<std::optional<OutT>(InT)>

User-provided replacement function & cycle-breaking functions.

The cycle-breaking function must not make any more recursive invocations to this cached replacer.

Definition at line 57 of file CyclicReplacerCache.h.

Constructor & Destructor Documentation

◆ CyclicReplacerCache() [1/2]

template<typename InT , typename OutT >
mlir::CyclicReplacerCache< InT, OutT >::CyclicReplacerCache ( )
delete

◆ CyclicReplacerCache() [2/2]

template<typename InT , typename OutT >
mlir::CyclicReplacerCache< InT, OutT >::CyclicReplacerCache ( CycleBreakerFn  cycleBreaker)
inline

Definition at line 60 of file CyclicReplacerCache.h.

Member Function Documentation

◆ lookupOrInit()

template<typename InT , typename OutT >
CyclicReplacerCache< InT, OutT >::CacheEntry mlir::CyclicReplacerCache< InT, OutT >::lookupOrInit ( InT  element)

Lookup the cache for a pre-calculated replacement for element.

If one exists, a resolved CacheEntry will be returned. Otherwise, an unresolved CacheEntry will be returned, and the caller must resolve it with the calculated replacement so it can be registered in the cache for future use. Multiple unresolved CacheEntries may be retrieved. However, any unresolved CacheEntries that are returned must be resolved in reverse order of retrieval, i.e. the last retrieved CacheEntry must be resolved first, and the first retrieved CacheEntry must be resolved last. This should be natural when used as a stack / inside recursion.

Definition at line 153 of file CyclicReplacerCache.h.


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