[LLHD] Fix incorrect mux generation in remove-control-flow#10566
Open
vasyavasilevs wants to merge 1 commit into
Open
[LLHD] Fix incorrect mux generation in remove-control-flow#10566vasyavasilevs wants to merge 1 commit into
vasyavasilevs wants to merge 1 commit into
Conversation
1172323 to
0e4b7f1
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR fixes a logical error in the llhd-remove-control-flow pass where the generated mux logic for merging arguments does not correctly represent the original CFG. This leads to phantom execution paths where values from one branch are incorrectly propagated into other paths.
Analysis of existing test case
The flaw is evident in the current regression test: circt/test/Dialect/LLHD/Transforms/remove-control-flow.mlir.
Input MLIR in the test:
In this CFG, there are two paths leading to ^bb3:
Path A (from ^bb1): Triggered when !%0. The value passed is %2.
Path B (from ^bb2): Triggered when %0 AND %5. The value passed is %4.
The current output expected by the test:
The mux above implements the logic: if (!%0) then %2 else %4. It completely ignores the condition %5. If %0 is true but %5 is false, the control should flow to ^bb4 directly, bypassing ^bb3. However, the generated mux logic for the value %6 (represented by [[TMP6]]) assumes that if %0 is true, the value must be %4.
In a hardware context, this creates a phantom path where the logic from ^bb3 is executed with the value of %4 even when the condition %5 is false. This was discovered during formal verification of the PicoRV32 CPU, where such phantom paths led to false-positive assertion violations.
Root Cause
The mergeArgs function in RemoveControlFlow.cpp initializes the mergedArgs with the first predecessor it encounters:
If the first predecessor encountered is a conditional branch, its values are treated as the default for all other paths, without being guarded by the actual condition that makes that path reachable.
Proposed Solution
The fix involves sorting the predecessors of a block based on dominance before generating the mux chain. By ensuring that the Dominator Block is processed first, it becomes the base of the mux tree. All other conditional paths are then layered on top as refinements. In the case of the test above, %2 (from the dominator path) will be the base, and %4 will be applied only if the specific path condition (%0 & %5) is met.