Skip to content

Fix assert due to unreachable discard - #7289

Merged
Antonio Maiorano (amaiorano) merged 2 commits into
microsoft:mainfrom
amaiorano:fix-assert-unreachable-discard
Apr 2, 2025
Merged

Antonio Maiorano (amaiorano) merged 2 commits into
microsoft:mainfrom
amaiorano:fix-assert-unreachable-discard

Conversation

@amaiorano

Copy link
Copy Markdown
Collaborator

When emitting discard in an unreachable code context (e.g. after an infinite loop), DXC would assert (if asserts enabled), or trigger a UBSAN failure because the discard instruction would have no parent. When an infinite loop is emitted during CodeGen, the InsertPt is cleared, thus subsequent discard instructions would be created, but no parent set. We skip emitting discard in this case, which follows the same pattern as is done for EmitIfStmt, and EmitSwitchStmt.

When emitting discard in an unreachable code context (e.g. after an
infinite loop), DXC would assert (if asserts enabled), or trigger a
UBSAN failure because the discard instruction would have no parent. When
an infinite loop is emitted during CodeGen, the InsertPt is cleared,
thus subsequent discard instructions would be created, but no parent
set. We skip emitting discard in this case, which follows the same
pattern as is done for EmitIfStmt, and EmitSwitchStmt.
@amaiorano

Copy link
Copy Markdown
Collaborator Author

An example of the problem can be seen on compiler explorer: https://godbolt.org/z/c4fq7eaTz

Because CE uses a release build without asserts and without ubsan, the extra discard is needed before the while loop to showcase the failure. I didn't dig into why exactly, but without it, DXC behaves "correctly". But with asserts or UBSAN enabled, the fact that the second discard has a nullptr parent results in a crash.

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

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants