[P4-dev] p4test and IR in JSON format question

hemant at mnkcg.com hemant at mnkcg.com
Fri Aug 4 19:10:13 EDT 2017


Got it - thanks much!

 

Hemant

 

From: Mihai Budiu [mailto:mbudiu at vmware.com] 
Sent: Friday, August 04, 2017 7:06 PM
To: hemant at mnkcg.com; p4-dev at lists.p4.org
Subject: RE: [P4-dev] p4test and IR in JSON format question

 

There is no tool that I am aware of to dump the IR graph in dot format.
However, these graphs are fairly large, it is not clear that a dot printout
will be very legible.

 

I don't expect it will be too difficult to write a piece of code which
generates such graphs, though. This could be done either in C++, or in
Python, by ingesting the JSON and dumping dot. In JSON anything that has a
"NodeID" is a node, and fields in JSON indicate the edges. If you want to do
it in C++ take a look at the DumpIR class in the top4.cpp file; it is very
small, but it could be even shorter if it didn't try to be so smart. In C++
you will also have to find a place to hook up your method to be called.

 

Mihai

 

From: hemant at mnkcg.com <mailto:hemant at mnkcg.com>  [mailto:hemant at mnkcg.com] 
Sent: Friday, August 4, 2017 3:58 PM
To: Mihai Budiu <mbudiu at vmware.com <mailto:mbudiu at vmware.com> >;
p4-dev at lists.p4.org <mailto:p4-dev at lists.p4.org> 
Subject: RE: [P4-dev] p4test and IR in JSON format question

 

Sorry, my bad.  I am saying a DAG graph that I could use a tool such as
"dot" (graphviz) to draw a visual of the graph.   I am aware of the Visitor
C++ class for a backend to use.   I should be able to use the Visitor class
to dump the graph and figure out plotting it using dot.

 

Thanks,

 

Hemant

 

From: Mihai Budiu [mailto:mbudiu at vmware.com] 
Sent: Friday, August 04, 2017 6:17 PM
To: hemant at mnkcg.com <mailto:hemant at mnkcg.com> ; p4-dev at lists.p4.org
<mailto:p4-dev at lists.p4.org> 
Subject: RE: [P4-dev] p4test and IR in JSON format question

 

What do you mean by "DAG format"?

The JSON text describes a DAG, which can be used to recover the internal
representation of the program used by the compiler.

The IR C++ data structures in the compiler also represent the program as a
DAG of C++ objects.

 

Mihai

 

From: hemant at mnkcg.com <mailto:hemant at mnkcg.com>  [mailto:hemant at mnkcg.com] 
Sent: Friday, August 4, 2017 2:59 PM
To: Mihai Budiu <mbudiu at vmware.com <mailto:mbudiu at vmware.com> >; 'Antonin
Bas' <antonin at barefootnetworks.com <mailto:antonin at barefootnetworks.com> >;
p4-dev at lists.p4.org <mailto:p4-dev at lists.p4.org> 
Subject: RE: [P4-dev] p4test and IR in JSON format question

 

My question was indeed, why I don't see a "size" : 16 in ir.json around the
snipped output in my email.   I didn't know the IR was in serialized form.
I looked for the IR node with number 456 and found the "size" : 16 elsewhere
in ir.json.   I am interested in using p4test to dump the Midend IR that a
backend will receive.  My preference is to receive this IR in a DAG format.
Is that possible with p4test?

 

Thanks,

 

Hemant

 

 

From: Mihai Budiu [mailto:mbudiu at vmware.com] 
Sent: Friday, August 04, 2017 4:07 PM
To: hemant at mnkcg.com <mailto:hemant at mnkcg.com> ; 'Antonin Bas'
<antonin at barefootnetworks.com <mailto:antonin at barefootnetworks.com> >;
p4-dev at lists.p4.org <mailto:p4-dev at lists.p4.org> 
Subject: RE: [P4-dev] p4test and IR in JSON format question

 

I am not sure what is the actual question you are asking.

The JSON you are dumping is actually the internal representation of the
program inside the compiler. There is a separate JSON representation of the
program, which is consumed by the behavioral model; that representation is
produced by the p4c-bm2-ss compiler using the -o flag.

 

Assuming that what you care about is the internal IR, then the type of the
'metadata' field is in the IR node with number 456, which should be present
somewhere in the JSON file, and should look like this:

 

                        "type" : {

                          "Node_ID" : 456,

                          "Node_Type" : "Type_Bits",

                          "size" : 16,

                          "isSigned" : false

                        },

 

What you see are just references to this node. The IR is a DAG, and in the
serialized form each node appears only once; subsequent appearances of a
node are replaced with a reference to the first appearance, which has always
a unique nodeId.

 

Mihai

 

From: P4-dev [mailto:p4-dev-bounces at lists.p4.org] On Behalf Of
hemant at mnkcg.com <mailto:hemant at mnkcg.com> 
Sent: Thursday, August 03, 2017 11:40 AM
To: 'p4-dev' <p4-dev at lists.p4.org <mailto:p4-dev at lists.p4.org> >
Subject: Re: [P4-dev] p4test and IR in JSON format question

 

Sorry, I forgot to add the annotation in the mystruct  example below.   I
have added it now.

 

From: P4-dev [mailto:p4-dev-bounces at lists.p4.org] On Behalf Of
hemant at mnkcg.com <mailto:hemant at mnkcg.com> 
Sent: Thursday, August 03, 2017 10:46 AM
To: 'p4-dev' <p4-dev at lists.p4.org <mailto:p4-dev at lists.p4.org> >
Subject: [P4-dev] p4test and IR in JSON format question

 

I have used this command to generate IR to various output files.

 

./p4test --p4v 16 ~/p4-xpc/mySwitch.p4 --top4 FrontEndLast,MidEndLast
--toJSON ~/ir.json --dump ~ -v

 

I get the following output files.

 

mySwitch-FrontEnd_31_FrontEndLast.p4

 

mySwitch-MidEnd_40_MidEndLast.p4

 

ir.json

 

My P4-16 program has this struct defined.

 

struct mystruct_t {

@MY_annotation("(FOO)")

bit<16>   metadata;

 

}

 

In the mySwitch-MidEnd_40_MidEndLast.p4 file, I see my data struct in the P4
program show up with its bit fields.  

 

/* 
  <Type_Struct>(978058) */
struct mystruct_t {
/* 
    <StructField>(978060)
      <Annotations>(978061)
      <Type_Bits>(456) */
        @MY_annotation("(FOO)") 
    bit<16> metadata;
}

 

However, the ir.json file  does not show any bits with the metadata field.
An operative question is which IR does the -toJSON dump, especially, in
comparison to the MidEndLast pass?  Then, the next question is, why the 16
bits do not show up in the JSON output below?  How does a backend know the
metadata fields comprises of 16 bits?  Did I miss anything in the JSON IR
below?  

 

{
        "Node_ID" : 978058,
        "Node_Type" : "Type_Struct",
        "name" : "mystruct_t",
        "declid" : 369,
        "annotations" : {
          "Node_ID" : 3
        },
        "fields" : {
          "Node_ID" : 978059,
          "Node_Type" : "IndexedVector<StructField>",
          "vec" : [
            {
              "Node_ID" : 978060,
              "Node_Type" : "StructField",
              "name" : "metadata",
              "declid" : 5560,
              "annotations" : {
                "Node_ID" : 978061,
                "Node_Type" : "Annotations",
                "annotations" : {
                  "Node_ID" : 978062,
                  "Node_Type" : "Vector<Annotation>",
                  "vec" : [
                    {
                      "Node_ID" : 978063,
                      "Node_Type" : "Annotation",
                      "name" : "MY_annotation",
                      "expr" : {
                        "Node_ID" : 978064,
                        "Node_Type" : "Vector<Expression>",
                        "vec" : [
                          {
                            "Node_ID" : 978065,
                            "Node_Type" : "StringLiteral",
                            "type" : {
                              "Node_ID" : 55225
                            },
                            "value" : "(FOO)",
                            "Source_Info" : {
                              "filename" : "/home/hemant/mySwitch.p4",
                              "line" : 71,
                              "column" : 25,
                              "source_fragment" : """
                            }
                          }
                        ]
                      },
                      "Source_Info" : {
                        "filename" : "/home/hemant/mySwitch.p4",
                        "line" : 71,
                        "column" : 0,
                        "source_fragment" : "@"
                      }
                    }
                  ]
                },
                "Source_Info" : {
                  "filename" : "/home/hemant/mySwitch.p4",
                  "line" : 71,
                  "column" : 0,
                  "source_fragment" : "@MY_annotation("(FOO)")"
                }
              },
              "type" : {
                "Node_ID" : 456
              },
              "Source_Info" : {
                "filename" : "/home/hemant/mySwitch.p4",
                "line" : 72,
                "column" : 12,
                "source_fragment" : "metadata"
              }
            }
          ],
          "declarations" : {
            "metadata" : {
              "Node_ID" : 978060,
              "Node_Type" : "StructField",
              "name" : "metadata",
              "declid" : 5560,
              "annotations" : {
                "Node_ID" : 978061
              },
              "type" : {
                "Node_ID" : 456
              }
            }
          }
        },

 

Thanks,

 

Hemant

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.p4.org/pipermail/p4-dev_lists.p4.org/attachments/20170804/b5bf88ad/attachment-0002.html>


More information about the P4-dev mailing list