def _optimize_ir(ctx, src):
    """Optimizes an IR file.

    The macro creates an action in the context that optimizes an IR file.

    Args:
      ctx: The current rule's context object.
      src: The source file.

    Returns:
      A File referencing the optimized IR file.
    """
    entry = ctx.attr.entry
    args = ("--entry=" + entry) if entry else ""
    opt_ir_file = ctx.actions.declare_file(src.basename[:-2] + "opt.ir")
    ctx.actions.run_shell(
        outputs = [opt_ir_file],
        # The IR optimization executable is a tool needed by the action.
        tools = [ctx.executable._ir_opt_tool],
        # The files required for optimizing the IR file also requires the IR
        # optimization executable.
        inputs = [src, ctx.executable._ir_opt_tool],
        command = "{} {} {} > {}".format(
            ctx.executable._ir_opt_tool.path,
            src.path,
            args,
            opt_ir_file.path,
        ),
        mnemonic = "OptimizeIR",
        progress_message = "Optimizing IR file: %s" % (src.path),
    )
    return opt_ir_file