Skip to content

[SimplifyCFG] Fix illegal-width bitmap from switch lookup table#8444

Open
damyanp wants to merge 5 commits into
microsoft:mainfrom
damyanp:8421
Open

[SimplifyCFG] Fix illegal-width bitmap from switch lookup table#8444
damyanp wants to merge 5 commits into
microsoft:mainfrom
damyanp:8421

Conversation

@damyanp
Copy link
Copy Markdown
Member

@damyanp damyanp commented May 12, 2026

SwitchLookupTable built bitmap APInts of width TableSize * ResultBitWidth, producing non-standard integer widths (i9, i17, i26, i33, i40, ...) that DXIL validation rejects - DXIL only allows i1/i8/i16/i32/i64.

Round the bitmap width up to a legal width, and cap the optimization at 32 bits to avoid silently promoting to i64 (which would set the Int64Ops shader flag and add a 64-bit-integer capability requirement the source shader did not have). Wider tables fall back to the array path, or - for i1 results - preserve the original switch.

Closes #8421

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

…osoft#8421)

SwitchLookupTable built bitmap APInts of width `TableSize * ResultBitWidth`,
producing non-standard integer widths (i9, i17, i26, i33, i40, ...) that
DXIL validation rejects - DXIL only allows i1/i8/i16/i32/i64.

Round the bitmap width up to a legal width, and cap the optimization at
32 bits to avoid silently promoting to i64 (which would set the Int64Ops
shader flag and add a 64-bit-integer capability requirement the source
shader did not have). Wider tables fall back to the array path, or - for
i1 results - preserve the original switch.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Collaborator

@llvm-beanz llvm-beanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments, but I think I'm mostly okay with this.

Comment thread lib/Transforms/Utils/SimplifyCFG.cpp
Comment thread lib/Transforms/Utils/SimplifyCFG.cpp Outdated
Comment thread lib/Transforms/Utils/SimplifyCFG.cpp Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 12, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

@damyanp damyanp requested a review from llvm-beanz May 12, 2026 21:38
Copy link
Copy Markdown
Collaborator

@bob80905 bob80905 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions with @alsepkow:

// or i32)
uint64_t RawBitMapWidth = TableSize * IT->getBitWidth();
uint64_t BitMapWidth =
NextPowerOf2(std::max(UINT64_C(15), RawBitMapWidth - 1));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: @alsepkow recommended this alternative:
uint64_t BitMapWidth = std::max(UINT64_C(16), PowerOf2Ceil(RawBitMapWidth));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that's...different...I'm not sure why it's worth changing?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our opinion is it's functionally equivalent but a bit simpler / more readable.

;
; CHECK-LABEL: @switch_i8_5_cases
; CHECK-NOT: i40
; CHECK: ret i8
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is an array being made here, can we add a check for the array?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also noticed that this case uses i8's that we've just taken steps to avoid in other places, so fixed that as well.


end:
; Non-linear values prevent the LinearMap fast path so the bitmap path is
; the one that would have been chosen.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to suggest a bitmap is being created, but such a bitmap would be > 32 bits, right? Is this comment saying that thanks to non-linear values, no bitmap is produced?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified comment

@damyanp damyanp requested a review from bob80905 May 12, 2026 23:12
Copy link
Copy Markdown
Collaborator

@bob80905 bob80905 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

Compilation failure in DXC 1.10.2605.2 about invalid width i26 Integers

3 participants