Skip to content

[IRGen] Mark inout ptrs noalias. #42503

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

Merged
merged 1 commit into from
Apr 21, 2022

Conversation

nate-chandler
Copy link
Contributor

Thanks to exclusivity checking, inout arguments can be marked noalias.
In the fullness of time, this should be applied in general.

As an incremental step towards marking all such arguments noalias, start
by marking inout arguments of pointer type noalias. Take this
conservative step rather than always applying the attribute to ward
against issues with LLVM's alias analysis.

rdar://76540030

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

This should be applied to all indirect arguments except @inout_aliasable.

What's the purpose of doing this for "pointer types" only? What LLVM alias analysis issues need to be avoided, how does this check avoid them, and when can we stop hiding info from LLVM?

Comment on lines 273 to 280
// Thanks to exclusivity checking, it is not possible to alias inouts.
if (paramSILType.getASTType()->getAnyPointerElementType()) {
// To ward against issues with LLVM's alias analysis, for now, only add the
// attribute if it's a pointer being passed inout.
b.addAttribute(llvm::Attribute::NoAlias);
}
// Aliasing inouts can't be captured without doing unsafe stuff.
Copy link
Contributor

Choose a reason for hiding this comment

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

Indirect_InoutAliasable arguments do alias. So I think the code should check aliasable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Fixed.

Thanks to exclusivity checking, inout arguments can be marked noalias.
In the fullness of time, this should be applied in general.

As an incremental step towards marking all such arguments noalias, start
by marking inout arguments of pointer type noalias.  Take this
conservative step rather than always applying the attribute to ward
against issues with LLVM's alias analysis.

rdar://76540030
@nate-chandler
Copy link
Contributor Author

Deferring to @aschwaighofer to answer the questions about staging this in like this and starting with pointer types.

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@aschwaighofer
Copy link
Contributor

aschwaighofer commented Apr 21, 2022

What's the purpose of doing this for "pointer types" only? What LLVM alias analysis issues need to be avoided, how does this check avoid them, and when can we stop hiding info from LLVM?

There are reoccuring bugs in LLVM's alias analysis (as it is continuously "improved"). They cause hard to debug memory bugs and would impose a cost on those who have to debug them.

This cost can be weight against (performance) benefits that we suspect to be getting.

My suggestion was to introduce the use of noalias on at limited set of types to see whether we trigger bugs. The set of types chosen is UnsafePointer like types with the assumption you would (sometimes) use them in performance critical code.

At some later point we can remove the constraint and see where we end up with then.

@aschwaighofer
Copy link
Contributor

aschwaighofer commented Apr 21, 2022

when can we stop hiding info from LLVM

...

@aschwaighofer
Copy link
Contributor

When we have rewritten the code generation backend in Swift.

@nate-chandler nate-chandler merged commit f045f14 into swiftlang:main Apr 21, 2022
@nate-chandler nate-chandler deleted the rdar76540030 branch April 21, 2022 14:37
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.

3 participants