Skip to content

AliasAnalysis: a complete overhaul of alias- and memory-behavior analysis #75505

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 20 commits into from
Jul 30, 2024

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented Jul 26, 2024

The main changes are:

  • Rewrite everything in swift. So far, parts of memory-behavior analysis were already implemented in swift. Now everything is done in swift and lives in AliasAnalysis.swift. This is a big code simplification.

  • Support many more instructions in the memory-behavior analysis - especially OSSA instructions, like begin_borrow, end_borrow, store_borrow, load_borrow. The computation of end_borrow effects is now much more precise. Also, partial_apply is now handled more precisely.

  • Simplify and reduce type-based alias analysis (TBAA). The complexity of the old TBAA comes from old days where the language and SIL didn't have strict aliasing and exclusivity rules (e.g. for inout arguments). Now TBAA is only needed for code using unsafe pointers and for classes. The new TBAA handles this - and not more.

  • Handle aliasing in begin_access [modify] scopes. We already supported truly immutable scopes like begin_access [read] or ref_element_addr [immutable]. For begin_access [modify] we know that there are no other reads or writes to the access-address within the scope.

  • Don't cache memory-behavior results. It turned out that the hit-miss rate was pretty bad (~ 1:7). The overhead of the cache lookup took as long as recomputing the memory behavior.

Also fix a wrong constant folding of the is_same_metatype builtin for function type. This was uncovered by a lit test due to the better alias analysis.

@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci benchmark

@eeckstein
Copy link
Contributor Author

@swift-ci test

eeckstein added 20 commits July 29, 2024 17:33
…eOfMetatype`

The same for SILType

It needs to be made clear that this is not the "original", but the lowered SIL type.

NFC
…e` builtin for function types

We need to compare the not lowered types, because function types may differ in their original version but are equal in the lowered version, e.g.
```
  ((Int, Int) -> ())
  (((Int, Int)) -> ())
```

Fixes a miscompile
Make each instruction to appear in a separate line
…irection` parameter

It simplifies the EscapeUtils API
NFC
…lure` function in case of a failure

This brings all the nice verifier features to the swift verifier, like printing the surrounding instructions in case of a failure, etc.
Indirect-in arguments are consumed by the caller and that not only counts as read but also as a write.
It was called for non-address operands of fix_lifetime.
Just exclude such fix_lifetime instructions from moving. It's not important anyway.
… value

Instead of just aborting if a `begin_apply` gets an address argument, follow the yielded values and see if they actually escape
It's dangerous to continue walking over an `unchecked_addr_cast` which casts between two different types.
We can only do this if the result is known to be the end of the walk, i.e. the cast result is not used in a relevant way.
…ysis tests

*) remove `// users: ...` comments
*) replace some `CHECK:` with `CHECK-NEXT:` lines. Otherwise the test could pass although the result is wrong
…istinct`

If the operand types (which are classes) of `ref_element_addr` or `ref_tail_addr` differ, then we know those can't be the same objects.
The result of a store_borrow can "escape" to other access bases, like a "pointer" access base.
Then a store-borrow access base is _not_ distinct from such another base.
…ysis

The main changes are:

*) Rewrite everything in swift. So far, parts of memory-behavior analysis were already implemented in swift. Now everything is done in swift and lives in `AliasAnalysis.swift`. This is a big code simplification.

*) Support many more instructions in the memory-behavior analysis - especially OSSA instructions, like `begin_borrow`, `end_borrow`, `store_borrow`, `load_borrow`. The computation of end_borrow effects is now much more precise. Also, partial_apply is now handled more precisely.

*) Simplify and reduce type-based alias analysis (TBAA). The complexity of the old TBAA comes from old days where the language and SIL didn't have strict aliasing and exclusivity rules (e.g. for inout arguments). Now TBAA is only needed for code using unsafe pointers. The new TBAA handles this - and not more. Note that TBAA for classes is already done in `AccessBase.isDistinct`.

*) Handle aliasing in `begin_access [modify]` scopes. We already supported truly immutable scopes like `begin_access [read]` or `ref_element_addr [immutable]`. For `begin_access [modify]` we know that there are no other reads or writes to the access-address within the scope.

*) Don't cache memory-behavior results. It turned out that the hit-miss rate was pretty bad (~ 1:7). The overhead of the cache lookup took as long as recomputing the memory behavior.
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein merged commit 8189840 into swiftlang:main Jul 30, 2024
5 checks passed
@eeckstein eeckstein deleted the alias-analysis branch July 30, 2024 05:32
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.

1 participant