Skip to content

Conversation

keith
Copy link
Member

@keith keith commented Sep 12, 2025

Path mapping is a feature of bazel that allows actions to be
deduplicated across bazel transitions if they are otherwise identical.
This is helpful if you build a binary in N transitions, where the
settings differences are irrelevant to this action. In our case we build
multiple python native extensions transitioning on the python version
they target, and before this change would run each of these actions once
per python version even though the outputs would be identical.

This is a no-op unless --experimental_output_paths=strip is passed.

The changes here are just enough to make bazel automatically remap the
paths, which is done by how you use the args object. The core change is
that instead of carrying around paths that have ctx.bin_dir hardcoded
in the strings. This is done by mapping them with the output file
object's root path when adding them to the args.

As a side effect this drops the genfiles_dir, but that has been the same
as bin_dir for a long time so hopefully that's a no-op for folks.

Path mapping is a feature of bazel that allows actions to be
deduplicated across bazel transitions if they are otherwise identical.
This is helpful if you build a binary in N transitions, where the
settings differences are irrelevant to this action. In our case we build
multiple python native extensions transitioning on the python version
they target, and before this change would run each of these actions once
per python version even though the outputs would be identical.

This is a no-op unless `--experimental_output_paths=strip` is passed.

The changes here are just enough to make bazel automatically remap the
paths, which is done by how you use the args object. The core change is
that instead of carrying around paths that have `ctx.bin_dir` hardcoded
in the strings. This is done by mapping them with the output file
object's root path when adding them to the args.

As a side effect this drops the genfiles_dir, but that has been the same
as bin_dir for a long time so hopefully that's a no-op for folks.
@llvmbot llvmbot added the bazel "Peripheral" support tier build system: utils/bazel label Sep 12, 2025
@keith
Copy link
Member Author

keith commented Sep 12, 2025

we have quite a few internal instances of these rules so i'm pretty confident in this change, but it would be great if some other folks could try in their projects if you have your own tblgen rules

Copy link
Member

@aaronmondal aaronmondal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

ctx.attr.deps,
)

test_args = [ctx.executable.tblgen.short_path]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sidenode: I don't immediately see how, but surely this can be expressed via ctx.actions.args(). Not relevant to this PR though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea shouldn't be a big deal either since that would only affect things if this test itself was under a transition

@rupprecht
Copy link
Collaborator

As a side effect this drops the genfiles_dir, but that has been the same
as bin_dir for a long time so hopefully that's a no-op for folks.

It is, but the old behavior can be restored w/ some --incompatible flags. It's a trivial fix though: add output_to_bindir = 1 to any genrule which generates a .td file. See #158669 for the one fix needed in LLVM for this.

@keith
Copy link
Member Author

keith commented Sep 15, 2025

It is, but the old behavior can be restored w/ some --incompatible flags. It's a trivial fix though: add output_to_bindir = 1 to any genrule which generates a .td file. See #158669 for the one fix needed in LLVM for this.

I don't think that should matter for this case then, since all the files are collected from other ctx.actions.run calls, so I think it would only hit if a td input file was produced by a filegroup, which I don't see any cases of today

@keith
Copy link
Member Author

keith commented Sep 15, 2025

if that ends up breaking cases we can conditionalize this behavior on ctx.bin_dir != ctx.genfiles_dir

@keith keith merged commit 6626e6a into llvm:main Sep 15, 2025
10 checks passed
@keith keith deleted the ks/bazel-update-tblgen-rules-to-support-path-mapping branch September 15, 2025 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants