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

Preserve qualifiers on array subscript #4159

Merged

Conversation

llvm-beanz
Copy link
Collaborator

This change prevents dropping const qualifiers on array subscript
operators. Without this change the array subscript operator converts
from an lvalue to an rvalue that drops the type qualifiers. Since
arrays are actually addresses to the start of the array, it results in
dropping the qualifiers on the underlying array, making the array
modifiable.

This resolves #2860

@AppVeyorBot
Copy link

Copy link
Contributor

@gracejennings gracejennings left a comment

Choose a reason for hiding this comment

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

LGTM!

@llvm-beanz llvm-beanz closed this Dec 22, 2021
@llvm-beanz llvm-beanz deleted the cbieneman/array-preserve-qualifiers branch December 22, 2021 00:47
@llvm-beanz llvm-beanz restored the cbieneman/array-preserve-qualifiers branch December 22, 2021 00:47
@llvm-beanz llvm-beanz reopened this Dec 22, 2021
This change prevents dropping const qualifiers on array subscript
operators. Without this change the array subscript operator converts
from an lvalue to an rvalue that drops the type qualifiers. Since
arrays are actually addresses to the start of the array, it results in
dropping the qualifiers on the underlying array, making the array
modifyable.
This test was added to verify the compiler doesn't crash in the HLSL
SROA pass when writing to InputPatch objects, but InputPatch objects
are defined to be constant and should not be writeable.
@llvm-beanz llvm-beanz force-pushed the cbieneman/array-preserve-qualifiers branch from 8fce2aa to 34bdfc3 Compare December 22, 2021 00:50
@AppVeyorBot
Copy link

@llvm-beanz llvm-beanz merged commit 36764f6 into microsoft:master Dec 30, 2021
points[0].norm[0] = 1;
points[0].norm[1] = 2;
points[0].norm[0] = 1; // expected-error {{read-only variable is not assignable}}
points[0].norm[1] = 2; // expected-error {{read-only variable is not assignable}}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think points should be considered const. Or is our subscript operator returning the wrong type here? I believe this is incompatible with fxc with this change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The docs for InputPatch’s operator[] say “ Gets a read-only resource variable.”, and our implementation returns const.

If that’s not correct we can fix our InputPatch implementation, but that should be orthogonal to this change.

See: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-inputpatch

Copy link
Contributor

Choose a reason for hiding this comment

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

I just checked and it turns out FXC doesn't like this code either. Strange as I thought I'd seen this pattern somewhere, and thought this came from some sample (since it's under a path with that implication). So it's all good!

@llvm-beanz llvm-beanz deleted the cbieneman/array-preserve-qualifiers branch July 19, 2024 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DXC allows assigning to members of a const struct variable
4 participants