Skip to content
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

Fix assertion when compiling array of struct init #6814

Conversation

amaiorano
Copy link
Collaborator

In CGHLSLMSFinishCodeGen's BuildImmInit, when initializing an array, if the init value type doesn't match the array element type, we must bail and instead, have it inject a call to the global ctor. Without this, builds with asserts enabled would assert later with "Wrong type in array element initializer". In non-assert builds, this invalid IR would be removed, and valid code emitted.

See #5294

@amaiorano amaiorano requested a review from a team as a code owner July 23, 2024 17:36
@amaiorano amaiorano requested review from sudonatalie and dneto0 July 23, 2024 18:24
In CGHLSLMSFinishCodeGen's BuildImmInit, when initializing an array, if
the init value type doesn't match the array element type, we must bail
and instead, have it inject a call to the global ctor. Without this,
builds with asserts enabled would assert later with "Wrong type in array
element initializer". In non-assert builds, this invalid IR would be
removed, and valid code emitted.

See microsoft#5294
@amaiorano amaiorano force-pushed the fix-assert-on-contruct-global-array-of-struct branch from 0d9106f to 355059f Compare July 23, 2024 19:43
@amaiorano amaiorano enabled auto-merge (squash) July 23, 2024 20:31
Copy link
Collaborator

@dneto0 dneto0 left a comment

Choose a reason for hiding this comment

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

LGTM.

I had to read the surrounding flow to understand what was going on. The example in the linked bug was very helpful.
Bailing here means the fallback path will take effect, i.e. the constructor function will be called from the initialization code

@amaiorano amaiorano merged commit 4a5253d into microsoft:main Jul 23, 2024
12 checks passed
dmpots added a commit to dmpots/DirectXShaderCompiler that referenced this pull request Jul 25, 2024
In microsoft#6814, we modified the compiler to avoid generating bad code in some
cases for array initializers. However, this caused a crash in the case
where the initializer does not use a GEP expression for addressing
because the `GV` will be null.

I considered setting `GV` to the value in the `store` pointer operand,
but it looked like `GV` was also checked elsewhere for null and
did not want to modify the behavior of the code in other places.

The fix is to check if we found a global variable before validating the
array case.
dmpots added a commit that referenced this pull request Jul 25, 2024
In #6814, we modified the compiler to avoid generating bad code in some
cases for array initializers. However, this caused a crash in the case
where the initializer does not use a GEP expression for addressing
because the `GV` will be null.

I considered setting `GV` to the value in the `store` pointer operand,
but it looked like `GV` was also checked elsewhere for null and did not
want to modify the behavior of the code in other places.

The fix is to check if we found a global variable before validating the
array case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants