Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MLGO] Add ability to extract IR from bazel using aquery #96964

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def parse_args_and_run():
parser.add_argument(
"--input_type",
type=str,
help="Input file type - JSON, LLD params, or directory.",
choices=["json", "params", "directory"],
help="Input file type - JSON, LLD params, directory, or bazel aquery.",
choices=["json", "params", "directory", "bazel_aquery"],
default="json",
nargs="?",
)
Expand Down Expand Up @@ -149,6 +149,11 @@ def main(args):
"structured compilation database, use that instead"
)
objs = extract_ir_lib.load_from_directory(args.input, args.output_dir)
elif args.input_type == "bazel_aquery":
with open(args.input, encoding="utf-8") as aquery_json_handle:
objs = extract_ir_lib.load_bazel_aquery(
json.load(aquery_json_handle), args.obj_base_dir, args.output_dir
)
else:
logging.error("Unknown input type: %s", args.input_type)

Expand Down
23 changes: 23 additions & 0 deletions llvm/utils/mlgo-utils/mlgo/corpus/extract_ir_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,29 @@ def make_spec(obj_file: str):
return [make_spec(path) for path in paths]


def load_bazel_aquery(aquery_json, obj_base_dir: str, output_dir: str):
"""Creates an object file array by looking at the JSON output of bazel aquery.

Args:
aquery_json: The JSON-formatted output of the bazel aquery command for
boomanaiden154 marked this conversation as resolved.
Show resolved Hide resolved
the target of interest. The bazel aquery JSON should be a JSON
serialized version of the analysis.ActionGraphContainer proto.
https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/analysis_v2.proto
obj_base_dir: The base build directory that all object files will be
written out as arelative to.
output_dir: The output directory where extracted .bc and .cmd files should
be placed.
"""
linker_params = []

for action_info in aquery_json["actions"]:
if action_info["mnemonic"] != "CppLink":
continue
linker_params = action_info["arguments"]

return load_from_lld_params(linker_params, obj_base_dir, output_dir)


def run_extraction(
objs: List[TrainingIRExtractor],
num_workers: int,
Expand Down
37 changes: 37 additions & 0 deletions llvm/utils/mlgo-utils/tests/corpus/extract_ir_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,43 @@ def test_lld_thinlto_extraction(outer, outdir):
# CHECK-LLD-THINLTO-EXTRACTION-PY: 3


## Test that we can load a bazel query JSON as expected.

# RUN: %python %s test_load_bazel_aquery | FileCheck %s --check-prefix CHECK-TEST-LOAD-BAZEL-AQUERY


def test_load_bazel_aquery():
obj = extract_ir_lib.load_bazel_aquery(
{
"actions": [
{"mnemonic": "not-link", "arguments": []},
{
"mnemonic": "CppLink",
"arguments": ["clang", "-o", "output_binary", "test1.o", "test2.o"],
},
]
},
"/some/path",
"/tmp/out",
)
print(obj[0].input_obj())
# CHECK-TEST-LOAD-BAZEL-AQUERY: /some/path/test1.o
print(obj[0].relative_output_path())
# CHECK-TEST-LOAD-BAZEL-AQUERY: test1.o
print(obj[0].cmd_file())
# CHECK-TEST-LOAD-BAZEL-AQUERY: /tmp/out/test1.o.cmd
print(obj[0].bc_file())
# CHECK-TEST-LOAD-BAZEL-AQUERY: /tmp/out/test1.o.bc
print(obj[1].input_obj())
# CHECK-TEST-LOAD-BAZEL-AQUERY: /some/path/test2.o
print(obj[1].relative_output_path())
# CHECK-TEST-LOAD-BAZEL-AQUERY: test2.o
print(obj[1].cmd_file())
# CHECK-TEST-LOAD-BAZEL-AQUERY: /tmp/out/test2.o.cmd
print(obj[1].bc_file())
# CHECK-TEST-LOAD-BAZEL-AQUERY: /tmp/out/test2.o.bc


## Test that filtering works correctly

# RUN: %python %s test_filtering | FileCheck %s --check-prefix CHECK-TEST-FILTERING
Expand Down
Loading