docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
sdfg::passes::SwitchExpander Class Reference

An expander that bundles several sub-expanders and switches to the correct one per node. More...

#include <sdfg/passes/expansion/switch_expander.h>

Inheritance diagram for sdfg::passes::SwitchExpander:
[legend]

Public Member Functions

template<typename... Expanders>
 SwitchExpander (Expanders... expanders)
 Construct from a variadic list of expanders.
 SwitchExpander (std::vector< std::shared_ptr< const LibNodeExpander > > expanders)
 Construct from an already assembled list of expanders.
void register_expander (std::shared_ptr< const LibNodeExpander > expander)
 Register expander.
void register_for_code (const data_flow::LibraryNodeCode &code, std::shared_ptr< const LibNodeExpander > expander)
 Register expander as the handler for library nodes with the given code.
const LibNodeExpander * for_lib_node (const data_flow::LibraryNode &node) const override
 Find the expander that applies to node via an O(1) code lookup, with a linear fallback.
ExpandOutcome handle_expand (ExpandContext &context, structured_control_flow::Block &block, data_flow::LibraryNode &node) const override
 Delegate expansion to the matching sub-expander.
Public Member Functions inherited from sdfg::passes::LibNodeExpander
virtual ~LibNodeExpander ()=default
virtual const data_flow::LibraryNodeCode * matched_code () const
 If this expander dispatches on a single, fixed library node code, expose it here.

Additional Inherited Members

Public Types inherited from sdfg::passes::LibNodeExpander
enum class  InputUse {
  Skip = 0 , Scalar , IndirectRead , IndirectWrite ,
  IndirectReadWrite
}

Detailed Description

An expander that bundles several sub-expanders and switches to the correct one per node.

The LibNodeExpander interface already supports "tested" expanders: every expander exposes LibNodeExpander::for_lib_node which returns the concrete expander to use for a given node, or nullptr if the expander does not apply.

Most expanders dispatch on a node's data_flow::LibraryNodeCode (a string match). This class exploits that: code-based children advertise their code via LibNodeExpander::matched_code, so the switch builds a code -> expander hashmap once and then jumps straight to the right expander in O(1) per node, instead of testing every child linearly.

  • for_lib_node looks the node's code up in the map and returns the matching child (giving the child a final say via its own LibNodeExpander::for_lib_node so it can refine further). Any child that does not dispatch purely by code is kept in a small linear fallback list.
  • If nothing applies, nullptr is returned and the node is left untouched by the pass.

Expanders that handle an entire subclass of nodes (and therefore several codes) can be wired into the fast path explicitly via register_for_code, registering the same expander under each code it is responsible for.

Because it is itself a LibNodeExpander, a SwitchExpander plugs straight into the existing SingleExpanderHolder / LibraryNodeExpansionPass machinery without any changes:

auto expander = std::make_shared<SwitchExpander>(
std::make_shared<omp::OMPConvExpander>(),
std::make_shared<math::tensor::MatMulBlasExpander>()
);
// optionally wire an expander that covers a whole node subclass under specific codes:
expander->register_for_code(SomeLibraryNodeCode, std::make_shared<SomeSubclassExpander>());
LibraryNodeExpansionPass pass(expander);
pass.run(builder, analysis_manager);
Looks for and expands library nodes in a single pass, potentially recursively.
Definition library_node_expansion_pass.h:261
Definition function_builder.h:10

Constructor & Destructor Documentation

◆ SwitchExpander() [1/2]

template<typename... Expanders>
sdfg::passes::SwitchExpander::SwitchExpander ( Expanders... expanders)
inlineexplicit

Construct from a variadic list of expanders.

Parameters
expandersone or more shared_ptr to LibNodeExpander. Code-dispatching expanders (e.g. CodeLibNodeExpander subclasses) are indexed by their code for O(1) lookup; any others are consulted linearly as a fallback.

◆ SwitchExpander() [2/2]

sdfg::passes::SwitchExpander::SwitchExpander ( std::vector< std::shared_ptr< const LibNodeExpander > > expanders)
inlineexplicit

Construct from an already assembled list of expanders.

Member Function Documentation

◆ for_lib_node()

const LibNodeExpander * sdfg::passes::SwitchExpander::for_lib_node ( const data_flow::LibraryNode & node) const
inlineoverridevirtual

Find the expander that applies to node via an O(1) code lookup, with a linear fallback.

Returns
the matching concrete expander, or nullptr if none applies.

Reimplemented from sdfg::passes::LibNodeExpander.

◆ handle_expand()

ExpandOutcome sdfg::passes::SwitchExpander::handle_expand ( ExpandContext & context,
structured_control_flow::Block & block,
data_flow::LibraryNode & node ) const
inlineoverridevirtual

Delegate expansion to the matching sub-expander.

In the normal pass flow this is not reached, because for_lib_node already hands back the concrete child expander and the pass calls handle_expand on that child directly. It is implemented for completeness and for callers that drive this expander manually.

Implements sdfg::passes::LibNodeExpander.

◆ register_expander()

void sdfg::passes::SwitchExpander::register_expander ( std::shared_ptr< const LibNodeExpander > expander)
inline

Register expander.

If it dispatches on a single code, it is added to the fast lookup map via SwitchExpander::register_for_code otherwise it will be matched less efficiently

◆ register_for_code()

void sdfg::passes::SwitchExpander::register_for_code ( const data_flow::LibraryNodeCode & code,
std::shared_ptr< const LibNodeExpander > expander )
inline

Register expander as the handler for library nodes with the given code.

Parameters
codethe library node code to route to expander.
expanderthe expander to invoke for nodes with that code.
Exceptions
std::invalid_argumentif another expander is already registered for code.

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