def test_fuzz_proto_networkx_equivalence( node_x_dimensionality: int, node_y_dimensionality: int, graph_x_dimensionality: int, graph_y_dimensionality: int, node_count: int, ): """Fuzz proto -> networkx -> proto on random generated graphs.""" proto_in = random_programl_generator.CreateRandomProto( node_x_dimensionality=node_x_dimensionality, node_y_dimensionality=node_y_dimensionality, graph_x_dimensionality=graph_x_dimensionality, graph_y_dimensionality=graph_y_dimensionality, node_count=node_count, ) # proto -> networkx g = programl.ProgramGraphToNetworkX(proto_in) assert g.number_of_nodes() == len(proto_in.node) assert g.number_of_edges() == len(proto_in.edge) # Check that the functions match up. functions_in_graph = set([ function for _, function in g.nodes(data="function") if function is not None ]) functions_in_proto = [function.name for function in proto_in.function] assert sorted(functions_in_proto) == sorted(functions_in_graph) # networkx -> proto proto_out = programl.NetworkXToProgramGraph(g) assert proto_out.function == proto_in.function assert proto_out.node == proto_in.node # Randomly generated graphs don't have a stable edge order. assert len(proto_out.edge) == len(proto_in.edge)
def Main(): """Main entry point.""" proto = programl.ReadStdin() g = programl.ProgramGraphToNetworkX(proto) encoder = GraphNodeEncoder() ir = fs.Read(FLAGS.ir) if FLAGS.ir else None encoder.EncodeNodes(g, ir=ir) programl.WriteStdout(programl.NetworkXToProgramGraph(g))
def test_proto_networkx_equivalence( random_100_proto: programl_pb2.ProgramGraph, ): """Test proto -> networkx -> proto on 100 "real" graphs.""" # proto -> networkx g = programl.ProgramGraphToNetworkX(random_100_proto) assert g.number_of_nodes() == len(random_100_proto.node) assert g.number_of_edges() == len(random_100_proto.edge) # networkx -> proto proto_out = programl.NetworkXToProgramGraph(g) assert proto_out.function == random_100_proto.function assert proto_out.node == random_100_proto.node assert proto_out.edge == random_100_proto.edge
def Main(): irs = [fs.Read(path) for path in LLVM_IR.iterdir()] ir_count = len(irs) with prof.ProfileToStdout(lambda t: ( f"STAGE 1: Construct unlabelled graphs (llvm2graph) " f"({humanize.Duration(t / ir_count)} / IR)")): graphs = [llvm2graph.BuildProgramGraphNetworkX(ir) for ir in irs] encoder = node_encoder.GraphNodeEncoder() with prof.ProfileToStdout(lambda t: ( f"STAGE 2: Encode graphs (inst2vec) " f"({humanize.Duration(t / ir_count)} / IR)")): for graph, ir in zip(graphs, irs): encoder.EncodeNodes(graph, ir) features_count = 0 features_lists = [] with prof.ProfileToStdout(lambda t: ( f"STAGE 3: Produce labelled graphs (reachability analysis) " f"({humanize.Duration(t / features_count)} / graph)")): for graph in graphs: analysis = reachability.ReachabilityAnnotator( programl.NetworkXToProgramGraph(graph)) features_list = analysis.MakeAnnotated(n=10).graphs features_count += len(features_list) features_lists.append(features_list) def iter(): for features_list in features_lists: for graph in features_list: yield graph_tuple.GraphTuple.CreateFromNetworkX(graph) with prof.ProfileToStdout(lambda t: ( f"STAGE 4: Construct graph tuples " f"({humanize.Duration(t / features_count)} / graph)")): batcher = graph_batcher.GraphBatcher(iter(), max_node_count=10000) graph_tuples = list(batcher) print("=================================") print(f"Unlabelled graphs count: {ir_count}") print(f" Labelled graphs count: {features_count}") print(f" Graph tuples count: {len(graph_tuples)}") print( f" Total node count: {sum(gt.node_count for gt in graph_tuples)}" ) print( f" Total edge count: {sum(gt.edge_count for gt in graph_tuples)}" )
def test_proto_networkx_equivalence_with_preallocated_proto( random_100_proto: programl_pb2.ProgramGraph, ): """Test proto -> networkx -> proto on 100 "real" graphs using the same proto instance.""" # proto -> networkx g = programl.ProgramGraphToNetworkX(random_100_proto) assert g.number_of_nodes() == len(random_100_proto.node) assert g.number_of_edges() == len(random_100_proto.edge) # networkx -> proto # Allocate the proto ahead of time: proto_out = programl_pb2.ProgramGraph() programl.NetworkXToProgramGraph(g, proto=proto_out) assert proto_out.function == random_100_proto.function assert proto_out.node == random_100_proto.node assert proto_out.edge == random_100_proto.edge
def test_proto_networkx_equivalence( llvm_program_graph: programl_pb2.ProgramGraph, ): """Test proto -> networkx -> proto equivalence.""" # proto -> networkx g = programl.ProgramGraphToNetworkX(llvm_program_graph) assert g.number_of_nodes() == len(llvm_program_graph.node) assert g.number_of_edges() == len(llvm_program_graph.edge) # networkx -> proto proto_out = programl.NetworkXToProgramGraph(g) assert set(fn.name for fn in proto_out.function) == set( fn.name for fn in llvm_program_graph.function ) assert len(llvm_program_graph.node) == len(proto_out.node) assert len(llvm_program_graph.edge) == len(proto_out.edge)
def test_proto_networkx_equivalence_with_preallocated_proto( llvm_program_graph: programl_pb2.ProgramGraph, ): """Test proto -> networkx -> proto equivalent using pre-allocated protos.""" # proto -> networkx g = programl.ProgramGraphToNetworkX(llvm_program_graph) assert g.number_of_nodes() == len(llvm_program_graph.node) assert g.number_of_edges() == len(llvm_program_graph.edge) # networkx -> proto # Allocate the proto ahead of time: proto_out = programl_pb2.ProgramGraph() programl.NetworkXToProgramGraph(g, proto=proto_out) assert set(fn.name for fn in proto_out.function) == set( fn.name for fn in llvm_program_graph.function ) assert len(llvm_program_graph.node) == len(proto_out.node) assert len(llvm_program_graph.edge) == len(proto_out.edge)
def test_profiled_ep_A(): """Graph with profiling information.""" proto_in = llvm2graph.BuildProgramGraphProto( fs.Read(REGRESSION_TESTS / "profiled_ep.A.ll")) # Check that profiling data has been lifted. assert any(f.HasField("llvm_entry_count") for f in proto_in.function) assert any(n.HasField("llvm_profile_true_weight") for n in proto_in.node) assert any(n.HasField("llvm_profile_false_weight") for n in proto_in.node) assert any(n.HasField("llvm_profile_total_weight") for n in proto_in.node) # Check that profiling data is preserved after conversion to networkx. g = programl.ProgramGraphToNetworkX(proto_in) assert g.graph["llvm_function_entry_count"] assert any("llvm_profile_true_weight" in d for _, d in g.nodes(data=True)) assert any("llvm_profile_false_weight" in d for _, d in g.nodes(data=True)) assert any("llvm_profile_total_weight" in d for _, d in g.nodes(data=True)) # Check that profiling data is preserved after conversion back to proto. proto_out = programl.NetworkXToProgramGraph(g) assert any(f.HasField("llvm_entry_count") for f in proto_out.function) assert any(n.HasField("llvm_profile_true_weight") for n in proto_out.node) assert any(n.HasField("llvm_profile_false_weight") for n in proto_out.node) assert any(n.HasField("llvm_profile_total_weight") for n in proto_out.node)
def protos(self) -> List[programl_pb2.ProgramGraph]: """Convert the networkx graphs to program graph protos.""" return [programl.NetworkXToProgramGraph(g) for g in self.graphs]
def test_53(builder: graph_builder.ProGraMLGraphBuilder): """github.com/ChrisCummins/ProGraML/issues/53""" proto = builder.Build(fs.Read(REGRESSION_TESTS / "53.ll")) programl.NetworkXToProgramGraph(proto)
def Main(): """Main entry point.""" bytecode = sys.stdin.read() builder = graph_builder.ProGraMLGraphBuilder() g = builder.Build(bytecode, FLAGS.opt) print(programl.NetworkXToProgramGraph(g))