P4C
The P4 Compiler
Loading...
Searching...
No Matches
P4::InlineSummary::PerCaller Struct Reference

Various substitutions that must be applied for each instance. More...

#include <inlining.h>

Classes

struct  key_equal
 
struct  key_hash
 

Public Types

typedef std::pair< const IR::MethodCallStatement *, const IR::PathExpression * > InlinedInvocationInfo
 

Public Member Functions

const IR::MethodCallStatement * uniqueCaller (const IR::Declaration_Instance *instance) const
 

Public Attributes

std::map< const IR::MethodCallStatement *, const IR::Declaration_Instance * > callToInstance
 For each invocation (key) call the instance that is invoked.
 
std::map< const IR::Declaration_Instance *, const IR::IContainer * > declToCallee
 For each instance (key) the container that is intantiated.
 
std::unordered_map< const InlinedInvocationInfo, const IR::ID, key_hash, key_equalinvocationToState
 
std::map< const IR::Declaration_Instance *, PerInstanceSubstitutions * > substitutions
 For each instance (key) we must apply a bunch of substitutions.
 

Detailed Description

Various substitutions that must be applied for each instance.

Member Typedef Documentation

◆ InlinedInvocationInfo

std::pair<const IR::MethodCallStatement *, const IR::PathExpression *> P4::InlineSummary::PerCaller::InlinedInvocationInfo

Pair identifying all the invocations of the subparser which can use the same inlined states because the path after returning from the subparser is identical for all such invocations.

Invocations are characterized by:

  • Pointer to the apply method call statement (comparing apply method call statement ensures that the same instance is invoked and same arguments are passed to the call)
  • Pointer to the transition statement expression which has to be the name of the state (select expression is not allowed)

Additional conditions which need to be met:

  • there is no other statement between the invocation of the subparser and the transition statement
  • transition statement has to be the name of the state (select expression is not allowed)
Attention
Note that local variables declared in states calling subparser and passed to the subparser as arguments need to be eliminated before Inline pass. Currently this condition is met as passes UniqueNames and MoveDeclarations are called before Inline pass in FrontEnd. Otherwise (if this condition was not met) there could be different variables with the same names passed as arguments to the apply method and additional checks would have to be introduced to avoid optimization in such case.
See also
field invocationToState

Member Function Documentation

◆ uniqueCaller()

const IR::MethodCallStatement * P4::InlineSummary::PerCaller::uniqueCaller ( const IR::Declaration_Instance * instance) const
inline
Returns
nullptr if there isn't exactly one caller, otherwise the single caller of this instance.

Member Data Documentation

◆ invocationToState

std::unordered_map<const InlinedInvocationInfo, const IR::ID, key_hash, key_equal> P4::InlineSummary::PerCaller::invocationToState

For each distinct invocation of the subparser identified by InlinedInvocationInfo we store the ID of the next caller parser state which is a new state replacing the start state of the callee parser (subparser). Transition to this state is used in case there is another subparser invocation which has the equivalent InlinedInvocationInfo.

Subparser invocations are considered to be equivalent when following conditions are met (which means that the path in the parse graph is the same after returning from subparser call):

  • apply method call statements of the invocations are equivalent (which means that the same subparser instance is invoked and the same arguments are passed to the apply method)
  • there is no statement between apply method call statement and transition statement
  • transition statement is a name of the state (not a select expression)
  • name of the state in transition statement is the same

Example of the optimization

Parser and subparser before Inline pass:

parser Subparser(packet_in packet, inout data_t hdr) {
state start {
hdr.f = 8w42;
transition accept;
}
}
parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta,
inout standard_metadata_t standard_metadata) {
@name("p") Subparser() p_0;
state start {
transition select(standard_metadata.ingress_port) {
9w0: p0;
default: p1;
}
}
state p0 {
p_0.apply(packet, hdr.h1);
transition accept;
}
state p1 {
p_0.apply(packet, hdr.h1);
transition accept;
}
}

Parser after Inline pass without optimization:

parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout
standard_metadata_t standard_metadata) { state start { transition
select(standard_metadata.ingress_port) { 9w0: p0; default: p1;
}
}
state p0 {
transition Subparser_start;
}
state Subparser_start {
hdr.h1.f = 8w42;
transition p0_0;
}
state p0_0 {
transition accept;
}
state p1 {
transition Subparser_start_0;
}
state Subparser_start_0 {
hdr.h1.f = 8w42;
transition p1_0;
}
state p1_0 {
transition accept;
}
}

Parser after Inline pass with optimization:

parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout
standard_metadata_t standard_metadata) { state start { transition
select(standard_metadata.ingress_port) { 9w0: p0; default: p1;
}
}
state p0 {
transition Subparser_start;
}
state Subparser_start {
hdr.h1.f = 8w42;
transition p0_0;
}
state p0_0 {
transition accept;
}
state p1 {
transition Subparser_start;
}
}
Attention
This field is used only when the optimization of parser inlining is enabled, otherwise it is not used. The optimization is disabled by default. The optimization can be enabled using command line option –parser-inline-opt.