P4C
The P4 Compiler
Loading...
Searching...
No Matches
ArithCompareAdjustment Class Reference

#include <instruction_selection.h>

Inheritance diagram for ArithCompareAdjustment:
[legend]

Public Member Functions

 ArithCompareAdjustment (const PhvInfo &p)
 
- Public Member Functions inherited from P4::PassManager
 PassManager (const PassManager &)=default
 
 PassManager (const std::initializer_list< VisitorRef > &init)
 
 PassManager (PassManager &&)=default
 
void addDebugHook (DebugHook h, bool recursive=false)
 
void addDebugHooks (std::vector< DebugHook > hooks, bool recursive=false)
 
void addPasses (const std::initializer_list< VisitorRef > &init)
 
const IR::Nodeapply_visitor (const IR::Node *, const char *=0) override
 
bool backtrack (trigger &trig) override
 
PassManagerclone () const override
 
void early_exit ()
 
void listPasses (std::ostream &, cstring sep) const
 
bool never_backtracks () override
 
void removePasses (const std::vector< cstring > &exclude)
 
void setStopOnError (bool stop)
 
- Public Member Functions inherited from P4::Visitor
virtual bool check_clone (const Visitor *a)
 
virtual bool check_global (cstring)
 
virtual void clear_globals ()
 
virtual ControlFlowVisitorcontrolFlowVisitor ()
 
virtual void end_apply ()
 
virtual void end_apply (const IR::Node *root)
 
virtual void erase_global (cstring)
 
template<class T >
const T * findContext () const
 
template<class T >
const T * findContext (const Context *&c) const
 
template<class T >
const T * findOrigCtxt () const
 
template<class T >
const T * findOrigCtxt (const Context *&c) const
 
virtual Visitorflow_clone ()
 
virtual void flow_merge (Visitor &)
 
virtual bool flow_merge_closure (Visitor &)
 
virtual void flow_merge_global_from (cstring)
 
virtual void flow_merge_global_to (cstring)
 
const ContextgetChildContext () const
 
int getChildrenVisited () const
 
const ContextgetContext () const
 
int getContextDepth () const
 
const IR::NodegetCurrentNode () const
 
template<class T >
const T * getCurrentNode () const
 
const IR::NodegetOriginal () const
 
template<class T >
const T * getOriginal () const
 
template<class T >
const T * getParent () const
 
virtual bool has_flow_joins () const
 
profile_t init_apply (const IR::Node *root, const Context *parent_context)
 
bool isInContext (const IR::Node *n) const
 
virtual const char * name () const
 
template<class T >
void parallel_visit (const IR::Vector< T > &v, const char *name, int cidx)
 
template<class T >
void parallel_visit (const IR::Vector< T > &v, const char *name=0)
 
template<class T >
void parallel_visit (IR::Vector< T > &v, const char *name, int cidx)
 
template<class T >
void parallel_visit (IR::Vector< T > &v, const char *name=0)
 
void print_context () const
 
const VisitorsetCalledBy (const Visitor *visitor)
 
void setName (const char *name)
 
void visit (const IR::Node &n, const char *name, int cidx)
 
void visit (const IR::Node &n, const char *name=0)
 
void visit (const IR::Node *&n, const char *name, int cidx)
 
void visit (const IR::Node *&n, const char *name=0)
 
void visit (const IR::Node *const &n, const char *name, int cidx)
 
void visit (const IR::Node *const &n, const char *name=0)
 
void visit (IR::Node &n, const char *name, int cidx)
 
void visit (IR::Node &n, const char *name=0)
 
void visit (IR::Node *&, const char *=0, int=0)
 
template<class T , typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>, class... Args>
void warn (const int kind, const char *format, const T &node, Args &&...args)
 The const ref variant of the above.
 
template<class T , typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
void warn (const int kind, const char *format, const T *node, Args &&...args)
 
bool warning_enabled (int warning_kind) const
 
- Public Member Functions inherited from P4::Backtrack

Public Attributes

std::map< const PHV::Field *, int > comp_adj_field_width_map
 
std::map< const IR::MAU::Action *, std::set< const PHV::Field * > > comp_adj_fields_per_action_map
 
std::map< const cstring, int > comp_adj_name_width_map
 
- Public Attributes inherited from P4::Visitor
const Visitorcalled_by = nullptr
 
cstring internalName
 
SplitFlowVisit_base *& split_link
 
SplitFlowVisit_basesplit_link_mem = nullptr
 

Additional Inherited Members

- Public Types inherited from P4::Visitor
typedef Visitor_Context Context
 
- Static Public Member Functions inherited from P4::Visitor
static cstring demangle (const char *)
 
static bool warning_enabled (const Visitor *visitor, int warning_kind)
 
- Protected Member Functions inherited from P4::PassManager
profile_t init_apply (const IR::Node *root) override
 
void runDebugHooks (const char *visitorName, const IR::Node *node)
 
- Protected Member Functions inherited from P4::Visitor
virtual void init_join_flows (const IR::Node *)
 
virtual bool join_flows (const IR::Node *)
 
virtual void post_join_flows (const IR::Node *, const IR::Node *)
 
void visit_children (const IR::Node *, std::function< void()> fn)
 
virtual void visitAgain () const
 
virtual void visitOnce () const
 
- Protected Attributes inherited from P4::PassManager
safe_vector< DebugHookdebugHooks
 
safe_vector< Visitor * > passes
 
bool running = false
 
unsigned seqNo = 0
 
bool stop_on_error = true
 
- Protected Attributes inherited from P4::Visitor
bool dontForwardChildrenBeforePreorder = false
 
bool joinFlows = false
 
bool visitDagOnce = true
 

Detailed Description

Applies two transformations to arithmetic compare operations:

  1. Narrow write destinations are expanded to match the read field widths. Currently done by expanding the write field width. This should be changed in the future to add and write to padding.
  2. A compare + set 0 that writes across all bits in a wide write target are merged into a single compare. (The compare must write to bit 0, and the set must write to bits width-1 .. 1 .)

These transformations are necessary because of the mismatch between P4 comparison operators and MAU arithmetic compare instructions. P4 comparison operators always produce a 1b boolean value. MAU compare instructions write to the entire PHV container: the LSB is set to the comparison result and all other bits are zeroed.

E.g.: header data_t { bit<8> f1; bit<8> f2; bit<8> f3; }

struct metadata { bool is_gt; };

action gt_action() { meta.is_gt = hdr.data.f1 > hdr.data.f2; hdr.data.f3 = (bit<8>)(bit<1>)(hdr.data.f1 > data.data.f2); }

This pass will:

  • Change the type of 'meta.is_gt' from bool to bit<8>. All references to 'meta.is_gt' other than the comparions would be replaced with 'meta.is_gt[0:0]'
  • The hdr.data.f3 action is translated to two IR::MAU:Instruction objects: set(ingress::hdr.data.f3[7:1], 0); gtu(ingress::hdr.data.f3[0:0], ingress::hdr.data.f1, ingress::hdr.data.f2); This is changed to: gtu(ingress::hdr.data.f3, ingress::hdr.data.f1, ingress::hdr.data.f2);

Details: This pass has two sub-passes: the first scans the design to find comparisons to transform, and the second applies the transforms.

Usage: This pass should be run after ElimAllButLastWrite. This ensures that only the last write to a location is used when verifying whether the comparison transforms can be safely applied.

Limitations:

  • Currently only works on metadata.

FIXME(glen): The first transformation currently expands the width of the write field to match the width of the read fields. This should be changed to add a pad field and then write to the pad + original field. Unfortunately the compiler doesn't currently support writing to more than one field with a single instruction.

MultiOperand seems an appropriate candidate to allow writes to multiple fields at the same time, but this require updates to the PHV allocation location logic and the dead code elimination logic.