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

[CIR] Fix for missing side effects during null pointer initialization #1547

Merged
merged 1 commit into from
Apr 4, 2025

Conversation

andykaylor
Copy link
Collaborator

When a pointer variable is initialized with a null pointer, the AST contains a NullToPointer cast. If we just emit a null pointer of the correct type, we will miss any side effects that occur in the initializer. This change adds code to emit the initializer expression if it is not a simple constant.

This results in an extra null pointer constant being emitted when the expression has side effects, but the constant emitted while visiting the expression does not have the correct type, so the alternative would be to capture the emitted constant and bitcast it to the correct type. An extra constant seems less intrusive than an unnecessary bitcast.

When a pointer variable is initialized with a null pointer, the AST
contains a NullToPointer cast. If we just emit a null pointer of the
correct type, we will miss any side effects that occur in the initializer.
This change adds code to emit the initializer expression if it is not a
simple constant.

This results in an extra null pointer constant being emitted when the
expression has side effects, but the constant emitted while visiting
the expression does not have the correct type, so the alternative would
be to capture the emitted constant and bitcast it to the correct type.
An extra constant seems less intrusive than an unnecessary bitcast.
@andykaylor
Copy link
Collaborator Author

Thanks to @dkolsen-pgi for suggesting the test case to introduce side effects in the initializer!

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

Nice catch, LGTM

@bcardosolopes bcardosolopes merged commit 9f2da98 into llvm:main Apr 4, 2025
10 checks passed
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.

2 participants