-
Notifications
You must be signed in to change notification settings - Fork 861
[SimplifyCFG] Fix illegal-width bitmap from switch lookup table #8444
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
damyanp
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
damyanp:8421
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.
+257
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1e04e45
[SimplifyCFG] Fix illegal-width bitmap from switch lookup table (#8421)
damyanp 84cd513
Tidy up comments; use i16 as smallest integer size
damyanp a1276d7
chore: autopublish 2026-05-12T21:39:28Z
github-actions[bot] 6c8a138
Avoid i8's in tests, explicitly test that bitmaps aren't used when >i…
damyanp b9bb75a
Clarify comment
damyanp 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
69 changes: 69 additions & 0 deletions
69
tools/clang/test/HLSLFileCheck/passes/llvm/simplifycfg/dxil_switch_bitmap_avoids_int64.ll
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,69 @@ | ||
| ; RUN: %opt %s -simplifycfg -S | FileCheck %s | ||
| ; | ||
| ; Companion test for https://github.com/microsoft/DirectXShaderCompiler/issues/8421 | ||
| ; | ||
| ; A switch with 33 boolean cases would naively build an i33 bitmap. Rounding up | ||
| ; to i64 would emit i64 ops and silently set the Int64Ops shader flag, so the | ||
| ; bitmap optimization is capped at 32 bits and the switch is preserved instead. | ||
|
|
||
| target datalayout = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f64:64-n8:16:32:64" | ||
| target triple = "dxil-ms-dx" | ||
|
|
||
| ; CHECK-LABEL: @switch_bool_33_cases | ||
| ; CHECK-NOT: i33 | ||
| ; CHECK-NOT: lshr i64 | ||
| ; CHECK-NOT: trunc i64 | ||
| ; CHECK: switch i32 | ||
| ; CHECK: ret i1 | ||
|
|
||
| define i1 @switch_bool_33_cases(i32 %x) { | ||
| entry: | ||
| switch i32 %x, label %default [ | ||
| i32 1, label %case_true | ||
| i32 2, label %case_true | ||
| i32 3, label %case_false | ||
| i32 4, label %case_true | ||
| i32 5, label %case_false | ||
| i32 6, label %case_true | ||
| i32 7, label %case_true | ||
| i32 8, label %case_false | ||
| i32 9, label %case_false | ||
| i32 10, label %case_true | ||
| i32 11, label %case_true | ||
| i32 12, label %case_false | ||
| i32 13, label %case_true | ||
| i32 14, label %case_false | ||
| i32 15, label %case_true | ||
| i32 16, label %case_true | ||
| i32 17, label %case_false | ||
| i32 18, label %case_true | ||
| i32 19, label %case_false | ||
| i32 20, label %case_false | ||
| i32 21, label %case_true | ||
| i32 22, label %case_true | ||
| i32 23, label %case_false | ||
| i32 24, label %case_true | ||
| i32 25, label %case_false | ||
| i32 26, label %case_true | ||
| i32 27, label %case_false | ||
| i32 28, label %case_true | ||
| i32 29, label %case_true | ||
| i32 30, label %case_false | ||
| i32 31, label %case_true | ||
| i32 32, label %case_false | ||
| i32 33, label %case_true | ||
| ] | ||
|
|
||
| case_true: | ||
| br label %end | ||
|
|
||
| case_false: | ||
| br label %end | ||
|
|
||
| default: | ||
| br label %end | ||
|
|
||
| end: | ||
| %result = phi i1 [ true, %case_true ], [ false, %case_false ], [ false, %default ] | ||
| ret i1 %result | ||
| } |
176 changes: 176 additions & 0 deletions
176
tools/clang/test/HLSLFileCheck/passes/llvm/simplifycfg/dxil_switch_bitmap_legal_types.ll
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,176 @@ | ||
| ; RUN: %opt %s -simplifycfg -S | FileCheck %s | ||
| ; | ||
| ; Regression test for https://github.com/microsoft/DirectXShaderCompiler/issues/8421 | ||
| ; | ||
| ; SimplifyCFG's switch-to-lookup-table built bitmaps of width | ||
| ; "TableSize * ValueBitWidth", producing illegal widths like i9, i17, i26 or | ||
| ; i48. The fix rounds the bitmap up to i16 or i32, and skips the lookup table | ||
| ; entirely if the bitmap would exceed 32 bits. | ||
|
|
||
| target datalayout = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f64:64-n8:16:32:64" | ||
| target triple = "dxil-ms-dx" | ||
|
|
||
| ; 5 boolean cases with mixed values would naively build an i5 bitmap; rounded | ||
| ; to i16 by the fix. | ||
| ; | ||
| ; CHECK-LABEL: @switch_bool_5_cases | ||
| ; CHECK: switch.lookup: | ||
| ; CHECK: lshr i16 | ||
| ; CHECK-NOT: i5 | ||
| ; CHECK: ret i1 | ||
|
|
||
| define i1 @switch_bool_5_cases(i32 %x) { | ||
| entry: | ||
| switch i32 %x, label %default [ | ||
| i32 0, label %case_t | ||
| i32 1, label %case_t | ||
| i32 2, label %case_f | ||
| i32 3, label %case_t | ||
| i32 4, label %case_f | ||
| ] | ||
|
|
||
| case_t: | ||
| br label %end | ||
|
|
||
| case_f: | ||
| br label %end | ||
|
|
||
| default: | ||
| br label %end | ||
|
|
||
| end: | ||
| %result = phi i1 [ true, %case_t ], [ false, %case_f ], [ false, %default ] | ||
| ret i1 %result | ||
| } | ||
|
|
||
| ; 9 boolean cases would naively build an i9 bitmap; rounded to i16 by the fix. | ||
| ; | ||
| ; CHECK-LABEL: @switch_bool_9_cases | ||
| ; CHECK: switch.lookup: | ||
| ; CHECK: lshr i16 | ||
| ; CHECK-NOT: i9 | ||
| ; CHECK: ret i1 | ||
|
|
||
| define i1 @switch_bool_9_cases(i32 %x) { | ||
| entry: | ||
| switch i32 %x, label %default [ | ||
| i32 0, label %case_true | ||
| i32 1, label %case_true | ||
| i32 3, label %case_true | ||
| i32 4, label %case_true | ||
| i32 5, label %case_true | ||
| i32 6, label %case_true | ||
| i32 7, label %case_true | ||
| i32 8, label %case_true | ||
| ] | ||
|
|
||
| case_true: | ||
| br label %end | ||
|
|
||
| default: | ||
| br label %end | ||
|
|
||
| end: | ||
| %result = phi i1 [ true, %case_true ], [ false, %default ] | ||
| ret i1 %result | ||
| } | ||
|
|
||
| ; 17 boolean cases would naively build an i17 bitmap; rounded to i32 by the | ||
| ; fix. | ||
| ; | ||
| ; CHECK-LABEL: @switch_bool_17_cases | ||
| ; CHECK: switch.lookup: | ||
| ; CHECK: lshr i32 | ||
| ; CHECK-NOT: i17 | ||
| ; CHECK: ret i1 | ||
|
|
||
| define i1 @switch_bool_17_cases(i32 %x) { | ||
| entry: | ||
| switch i32 %x, label %default [ | ||
| i32 0, label %case_true | ||
| i32 1, label %case_true | ||
| i32 3, label %case_true | ||
| i32 4, label %case_true | ||
| i32 5, label %case_true | ||
| i32 7, label %case_true | ||
| i32 8, label %case_true | ||
| i32 10, label %case_true | ||
| i32 11, label %case_true | ||
| i32 12, label %case_true | ||
| i32 14, label %case_true | ||
| i32 15, label %case_true | ||
| i32 16, label %case_true | ||
| ] | ||
|
|
||
| case_true: | ||
| br label %end | ||
|
|
||
| default: | ||
| br label %end | ||
|
|
||
| end: | ||
| %result = phi i1 [ true, %case_true ], [ false, %default ] | ||
| ret i1 %result | ||
| } | ||
|
|
||
| ; 26 boolean cases (the original #8421 repro) would naively build an i26 | ||
| ; bitmap; rounded to i32 by the fix. | ||
| ; | ||
| ; CHECK-LABEL: @switch_bool_26_cases | ||
| ; CHECK: switch.lookup: | ||
| ; CHECK: lshr i32 | ||
| ; CHECK-NOT: i26 | ||
| ; CHECK: ret i1 | ||
|
|
||
| define i1 @switch_bool_26_cases(i32 %x) { | ||
| entry: | ||
| switch i32 %x, label %default [ | ||
| i32 1, label %case_true | ||
| i32 6, label %case_true | ||
| i32 11, label %case_true | ||
| i32 16, label %case_true | ||
| i32 21, label %case_true | ||
| i32 26, label %case_true | ||
| ] | ||
|
|
||
| case_true: | ||
| br label %end | ||
|
|
||
| default: | ||
| br label %end | ||
|
|
||
| end: | ||
| %result = phi i1 [ true, %case_true ], [ false, %default ] | ||
| ret i1 %result | ||
| } | ||
|
|
||
| ; Non-i1 result: 3 i16 cases would naively build an i48 bitmap. The fix makes | ||
| ; bitmaps wider than 32 bits skip the lookup table; the original switch is | ||
| ; preserved. | ||
| ; | ||
| ; CHECK-LABEL: @switch_i16_3_cases | ||
| ; CHECK: switch i32 | ||
| ; CHECK-NOT: switch.lookup | ||
| ; CHECK-NOT: i48 | ||
| ; CHECK: ret i16 | ||
|
|
||
| define i16 @switch_i16_3_cases(i32 %x) { | ||
| entry: | ||
| switch i32 %x, label %default [ | ||
| i32 0, label %c0 | ||
| i32 1, label %c1 | ||
| i32 2, label %c2 | ||
| ] | ||
|
|
||
| c0: br label %end | ||
| c1: br label %end | ||
| c2: br label %end | ||
| default: br label %end | ||
|
|
||
| end: | ||
| ; Non-linear values prevent the LinearMap fast path so the bitmap path is | ||
| ; the one that would have been chosen, but since this would want to use an i48 | ||
| ; (which isn't valid in DXIL) the switch is preserved. | ||
| %result = phi i16 [ 73, %c0 ], [ 42, %c1 ], [ 88, %c2 ], [ 0, %default ] | ||
| ret i16 %result | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.