Exemplo n.º 1
0
def BuildProgramGraph(
    ir: str,
    options: program_graph_options_pb2.ProgramGraphOptions = DefaultOptions,
    timeout: int = 60,
) -> program_graph_pb2.ProgramGraph:
    """Construct a program graph from an LLVM-IR.

    Args:
      ir: The text of an LLVM-IR Module.
      options: The graph construction options.
      timeout: The number of seconds to permit before timing out.

    Returns:
      A ProgramGraph instance.

    Raises:
      ValueError: In case graph construction fails.
      TimeoutError: If timeout is reached.
      OsError: In case of other error.
    """
    # Write the ProgramGraphOptions to a temporary file and pass it to a
    # worker subprocess which generates the graph and produces a ProgramGraph
    # message on stdout.
    with tempfile.NamedTemporaryFile("w") as f:
        f.write(ir)
        f.flush()
        options.ir_path = f.name
        process = subprocess.Popen(
            ["timeout", "-s9",
             str(timeout),
             str(GRAPH_BUILDER_BIN)],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            stdin=subprocess.PIPE,
        )
        stdout, stderr = process.communicate(options.SerializeToString())

    proto = program_graph_pb2.ProgramGraph()
    if process.returncode == 2:
        raise ValueError(stderr.decode("utf-8").rstrip())
    elif process.returncode == 9 or process.returncode == -9:
        raise TimeoutError(
            f"Program graph construction exceeded {timeout} seconds")
    elif process.returncode:
        raise OSError(stderr.decode("utf-8").rstrip())
    proto.ParseFromString(stdout)
    return proto
Exemplo n.º 2
0
def test_graph_with_unconnected_node_strict():
    builder = ProgramGraphBuilder(ProgramGraphOptions(strict=True))
    mod = builder.AddModule("x")
    fn = builder.AddFunction("x", mod)
    builder.AddInstruction("x", fn)
    with test.Raises(ValueError) as e_ctx:
        builder.Build()
    assert "INSTRUCTION has no connections: " in str(e_ctx.value)
Exemplo n.º 3
0
def test_add_empty_module_strict():
    builder = ProgramGraphBuilder(ProgramGraphOptions(strict=True))
    foo = builder.AddModule("foo")

    assert foo == 0
    with test.Raises(ValueError) as e_ctx:
        builder.Build()
    assert str(e_ctx.value) == "Module `foo` is empty"
Exemplo n.º 4
0
def test_add_empty_function_strict():
    builder = ProgramGraphBuilder(ProgramGraphOptions(strict=True))
    mod = builder.AddModule("foo")
    foo = builder.AddFunction("bar", mod)

    assert foo == 0
    with test.Raises(ValueError) as e_ctx:
        builder.Build()
    assert str(e_ctx.value) == "Function `bar` is empty"
Exemplo n.º 5
0
 def __init__(self, options: Optional[ProgramGraphOptions] = None):
   options = options or ProgramGraphOptions()
   super().__init__(options.SerializeToString())
Exemplo n.º 6
0
def test_empty_proto_strict():
    builder = ProgramGraphBuilder(ProgramGraphOptions(strict=True))
    with test.Raises(ValueError) as e_ctx:
        builder.Build()
    assert "INSTRUCTION has no connections: `[external]`" == str(e_ctx.value)