KhronosGroup / KhronosGroup/SPIRV-Tools
Opt: Need empty control flow removal
- Dominant language
- C++
- Stars
- 1.4k
- Forks
- 709
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 28
Description
The current aggressive dead code elimination does not get rid of empty control flow ie control flow headers and structures which are "empty" ie do not contain code which contributes to the externally facing results of the function. The most simple example of this is an OpBranchConditional where both true and false branches branch to blocks which are empty and branch directly to the merge block.
Elimination of such control flow can lead to elimination of a potentially significant amount of dead code which is used to compute the useless control flow. This dead code can cause a shader to reflect a false use of texture or shader object. Elimination of such false uses is essential in the porting of HLSL shaders.
The highest priority is empty "if" structures, followed by "loops" and "switches".
My first thought is to make this part of aggressive dce. The following is a description of how ADCE would be modified.
ADCE currently marks all branches as live. This would be changed to not mark OpBranchCondtionals which are associated with a SelectionMerge.
When an instruction is marked live, its block will be marked live. When a block is first marked live, if its immediately containing control structure is an "if", the corresponding OpBranchConditional will be marked live.
It is possible for the BranchConditional of an "if" to be live even when both branches are empty. This is when there are live phis in the merge block of the "if". So if such a phi is marked live, the OpBranchConditional in the merge's corresponding header block is marked live.
After closure is performed on all live instructions, if an OpBranchConditional is dead, it is replaced with an unconditional branch to its corresponding merge block.
Any control flow so processed will become a candidate for Unreachable Block Elimination.
I am claiming this issue and am beginning active implementation of empty "if" removal.
Contributor guide
Research direction
Start with the aggressive dead code elimination implementation, focusing on ADCE handling of OpBranchConditional and SelectionMerge, then inspect Unreachable Block Elimination. Done means empty if control flow is removed while preserving live phis and enabling unreachable-block cleanup; loops and switches are identified as follow-up priorities.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100