p4c icon indicating copy to clipboard operation
p4c copied to clipboard

Local Copy Propogation issue

Open komaljai opened this issue 4 months ago • 4 comments

void ipip_push(inout headers_t hdr, in metadata_t meta)
{
   hdr.inner = hdr.outer;
   hdr.outer.srcAddr = meta.src;
   hdr.outer.dstAddr = meta.dst;
   hdr.outer.ttl = 64;
   hdr.outer.protocol = 4; /* IPIP */
   /* Assume MTU can accomodate +20 bytes */
   hdr.outer.totalLen = hdr.outer.totalLen + 20;
   hdr.outer.hdrChecksum = 0;
}

control Deparser(
    packet_out pkt,
    inout    headers_t hdr,
    in    metadata_t meta,
    in    pna_main_output_metadata_t ostd)
{
    apply {
        pkt.emit(hdr.ethernet);
	if (meta.push && hdr.outer.isValid()) {
               /* Push the ipip header */
               ipip_push(hdr, meta);
	}
    }
}

The IR representation at end of frontend looks like -

control Deparser(packet_out pkt, inout headers_t hdr, in metadata_t meta, in pna_main_output_metadata_t ostd) {
    @name("Deparser.hdr_0") headers_t hdr_1;
    @name("Deparser.meta_0") metadata_t meta_1;
    apply {
        pkt.emit<ethernet_t>(hdr.ethernet);
        if (meta.push && hdr.outer.isValid()) {
            hdr_1 = hdr;
            meta_1 = meta;
            hdr_1.inner = hdr_1.outer;
            hdr_1.outer.srcAddr = meta_1.src;
            hdr_1.outer.dstAddr = meta_1.dst;
            hdr_1.outer.ttl = 8w64;
            hdr_1.outer.protocol = 8w4;
            hdr_1.outer.totalLen = hdr_1.outer.totalLen + 16w20;
            hdr_1.outer.hdrChecksum = 16w0;
            hdr = hdr_1;
        }
    }
}

The issue is copy propogation, 'hdr' is assigned to 'hdr1' in beginning and 'hdr1' is assigned to 'hdr' back at the end, Shouldn't this be optimized?

komaljai avatar Sep 27 '24 05:09 komaljai