[platform_dependent] Ensure that platform_dependent only lowers for intended platforms #28607
+200
−84
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #28594
Currently
lax.platform_dependent
allows specifying code that behavesdifferently when lowered on different platforms. However, this function
operates in a confusing way, in that it will create a branch on the
platform, but will lower all branches for the current lowering platforms.
For example, in the following code:
If we lower for CPU, we lower both
for_cpu
andfor_tpu
for CPU (!), but only the branch corresponding to
for_cpu
will actually run.
This is a problem if, e.g.,
for_tpu
does not have a loweringfor CPU. We will get an error during lowering. Instead there should
be no error during lowering, because that branch is not actually needed.
We add a new test
test_platform_dependent_with_primitive_with_lowering_error
to demonstrate this.
The solution implememented here is the Solution A from #28594: we
add a
branches_platform
param to thecond
primitive, which ispropagated by all transformations. This param is used only for the
conditionals arising from
lax.platform_dependendet
.During lowering we drop the branches corresponding to the platforms
that are not interesting.