-
Notifications
You must be signed in to change notification settings - Fork 0
Extract R8 DCE verification into featured-shrinker-tests module #165
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
Open
kirich1409
wants to merge
1
commit into
main
Choose a base branch
from
refactor/extract-shrinker-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| swarm-report/ | ||
| *.iml | ||
| .kotlin | ||
| .gradle | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # R8 Dead-Code Elimination Verification | ||
|
|
||
| ## Why it matters | ||
|
|
||
| Featured's core guarantee for local flags is that when a flag value is fixed at build time, | ||
| the code reachable only through the disabled branch is completely removed from the final APK. | ||
| This relies on R8 honouring the `-assumevalues` ProGuard rules generated by | ||
| `ProguardRulesGenerator`. | ||
|
|
||
| A rule that is syntactically correct but semantically wrong would silently fail to eliminate | ||
| dead code. The `featured-shrinker-tests` module gives automated, deterministic verification | ||
| that the exact rule format produced by the plugin is sufficient for R8 to perform DCE. | ||
|
|
||
| ## How it works | ||
|
|
||
| The tests use a three-step synthetic pipeline: | ||
|
|
||
| 1. **Bytecode generation (ASM)** — `SyntheticBytecodeFactory` builds `.class` files in | ||
| memory that mirror the structure the plugin generates at build time: a `ConfigValues` | ||
| holder, an extensions class that reads from it, branch-target classes (`IfBranchCode`, | ||
| `ElseBranchCode`, `PositiveCountCode`), and a caller entry point. | ||
|
|
||
| 2. **Rules generation** — `ProguardRulesWriter` writes `.pro` files in the exact format | ||
| `ProguardRulesGenerator` produces, optionally including the `-assumevalues` block. | ||
|
|
||
| 3. **R8 invocation** — `R8TestHarness.runR8()` calls R8 programmatically via | ||
| `R8Command.builder()`, producing an output JAR with DCE applied. | ||
|
|
||
| After each run, `JarAssertions` inspects the output JAR and asserts which classes are | ||
| present or absent, proving that the rule caused (or did not cause) elimination. | ||
|
|
||
| ## Test scenarios | ||
|
|
||
| ### Boolean flags (`R8BooleanFlagEliminationTest`) | ||
|
|
||
| | Test | Rule | Expected | | ||
| |------|------|----------| | ||
| | `if-branch class is eliminated when boolean flag returns false` | `-assumevalues … return false` | `IfBranchCode` absent; `ElseBranchCode` present | | ||
| | `else-branch class is eliminated when boolean flag returns true` | `-assumevalues … return true` | `ElseBranchCode` absent; `IfBranchCode` present | | ||
| | `both branch classes survive when no boolean assumevalues rule is present` | No `-assumevalues` | Both classes present | | ||
|
|
||
| The third test is a control: it proves that elimination is caused by the rule, not by R8's | ||
| own constant-folding. | ||
|
|
||
| ### Int flags (`R8IntFlagEliminationTest`) | ||
|
|
||
| | Test | Rule | Expected | | ||
| |------|------|----------| | ||
| | `guarded class is eliminated when int flag is assumed to return zero` | `-assumevalues … return 0` | `PositiveCountCode` absent; `IntCaller` present | | ||
| | `guarded class survives when int flag has no assumevalues rule` | No `-assumevalues` | Both classes present | | ||
|
|
||
| With `-assumevalues return 0`, R8 constant-folds `0 > 0` to `false` and eliminates the | ||
| guarded block entirely. | ||
|
|
||
| ## Running the tests | ||
|
|
||
| ```bash | ||
| ./gradlew :featured-shrinker-tests:test | ||
| ``` | ||
|
|
||
| To run only one test class: | ||
|
|
||
| ```bash | ||
| ./gradlew :featured-shrinker-tests:test --tests "dev.androidbroadcast.featured.shrinker.r8.R8BooleanFlagEliminationTest" | ||
| ``` | ||
|
|
||
| ## Adding new scenarios | ||
|
|
||
| 1. **New flag type** — add bytecode generators in `SyntheticBytecodeFactory.kt`, JAR | ||
| assembler functions in `JarAssembler.kt`, rule writers in `ProguardRulesWriter.kt`, and | ||
| a new test class in `r8/` that extends `R8TestHarness`. | ||
|
|
||
| 2. **New rule variant** — add a `write*Rules()` function in `ProguardRulesWriter.kt` and a | ||
| corresponding `@Test` method in the relevant test class. | ||
|
|
||
| 3. **Verifying a rule format change** — update the `write*Rules()` function to match the | ||
| new format produced by `ProguardRulesGenerator`, then run the tests to confirm DCE still | ||
| works. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This guide states that
ProguardRulesWriterwrites.profiles in the exact formatProguardRulesGeneratorproduces, but the test rules intentionally add extra directives (e.g.,-keep,-dontwarn) and don’t include the generator’s header comments. Consider clarifying that the-assumevaluesblock matches the plugin output while the rest is test scaffolding.