<feed xmlns='http://www.w3.org/2005/Atom'>
<title>rust/src/test/codegen/function-arguments.rs, branch stable</title>
<subtitle>https://github.com/rust-lang/rust
</subtitle>
<id>http://git.dreamy.place/mirrors/rust/atom?h=stable</id>
<link rel='self' href='http://git.dreamy.place/mirrors/rust/atom?h=stable'/>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/'/>
<updated>2023-01-11T09:32:08+00:00</updated>
<entry>
<title>Move /src/test to /tests</title>
<updated>2023-01-11T09:32:08+00:00</updated>
<author>
<name>Albert Larsan</name>
<email>74931857+albertlarsan68@users.noreply.github.com</email>
</author>
<published>2023-01-05T08:13:28+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=cf2dff2b1e3fa55fa5415d524200070d0d7aacfe'/>
<id>urn:sha1:cf2dff2b1e3fa55fa5415d524200070d0d7aacfe</id>
<content type='text'>
</content>
</entry>
<entry>
<title>do not add noalias in return position</title>
<updated>2023-01-02T14:11:19+00:00</updated>
<author>
<name>Ralf Jung</name>
<email>post@ralfj.de</email>
</author>
<published>2023-01-02T14:11:19+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=e7cad62257e5b0d59ca18515a522e90924177330'/>
<id>urn:sha1:e7cad62257e5b0d59ca18515a522e90924177330</id>
<content type='text'>
</content>
</entry>
<entry>
<title>Introduce deduced parameter attributes, and use them for deducing `readonly` on</title>
<updated>2022-10-21T09:33:15+00:00</updated>
<author>
<name>Patrick Walton</name>
<email>pcwalton@fb.com</email>
</author>
<published>2022-10-18T02:42:15+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=da630ac79d03d12b12b73843e9fb92528db79eb3'/>
<id>urn:sha1:da630ac79d03d12b12b73843e9fb92528db79eb3</id>
<content type='text'>
indirect immutable freeze by-value function parameters.

Right now, `rustc` only examines function signatures and the platform ABI when
determining the LLVM attributes to apply to parameters. This results in missed
optimizations, because there are some attributes that can be determined via
analysis of the MIR making up the function body. In particular, `readonly`
could be applied to most indirectly-passed by-value function arguments
(specifically, those that are freeze and are observed not to be mutated), but
it currently is not.

This patch introduces the machinery that allows `rustc` to determine those
attributes. It consists of a query, `deduced_param_attrs`, that, when
evaluated, analyzes the MIR of the function to determine supplementary
attributes. The results of this query for each function are written into the
crate metadata so that the deduced parameter attributes can be applied to
cross-crate functions. In this patch, we simply check the parameter for
mutations to determine whether the `readonly` attribute should be applied to
parameters that are indirect immutable freeze by-value.  More attributes could
conceivably be deduced in the future: `nocapture` and `noalias` come to mind.

Adding `readonly` to indirect function parameters where applicable enables some
potential optimizations in LLVM that are discussed in [issue 103103] and [PR
103070] around avoiding stack-to-stack memory copies that appear in functions
like `core::fmt::Write::write_fmt` and `core::panicking::assert_failed`. These
functions pass a large structure unchanged by value to a subfunction that also
doesn't mutate it. Since the structure in this case is passed as an indirect
parameter, it's a pointer from LLVM's perspective. As a result, the
intermediate copy of the structure that our codegen emits could be optimized
away by LLVM's MemCpyOptimizer if it knew that the pointer is `readonly
nocapture noalias` in both the caller and callee. We already pass `nocapture
noalias`, but we're missing `readonly`, as we can't determine whether a
by-value parameter is mutated by examining the signature in Rust. I didn't have
much success with having LLVM infer the `readonly` attribute, even with fat
LTO; it seems that deducing it at the MIR level is necessary.

No large benefits should be expected from this optimization *now*; LLVM needs
some changes (discussed in [PR 103070]) to more aggressively use the `noalias
nocapture readonly` combination in its alias analysis. I have some LLVM patches
for these optimizations and have had them looked over. With all the patches
applied locally, I enabled LLVM to remove all the `memcpy`s from the following
code:

```rust
fn main() {
    println!("Hello {}", 3);
}
```

which is a significant codegen improvement over the status quo. I expect that
if this optimization kicks in in multiple places even for such a simple
program, then it will apply to Rust code all over the place.

[issue 103103]: https://github.com/rust-lang/rust/issues/103103

[PR 103070]: https://github.com/rust-lang/rust/pull/103070
</content>
</entry>
<entry>
<title>Remove outdated rustc_allocator test</title>
<updated>2022-07-27T14:19:07+00:00</updated>
<author>
<name>Nikita Popov</name>
<email>npopov@redhat.com</email>
</author>
<published>2022-07-27T13:04:20+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=e6f0e358d6fc811b02b9162d275217b5ac389ab6'/>
<id>urn:sha1:e6f0e358d6fc811b02b9162d275217b5ac389ab6</id>
<content type='text'>
This attribute now does more than just place noalias on the return,
and has specific requirements for the signature.

Drop the test entirely, as we already check __rust_alloc attributes
in other codegen tests.
</content>
</entry>
<entry>
<title>do not mark interior mutable shared refs as dereferenceable</title>
<updated>2022-07-22T18:25:41+00:00</updated>
<author>
<name>Ralf Jung</name>
<email>post@ralfj.de</email>
</author>
<published>2022-06-21T03:51:15+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=5b7197af7fb379a84895084625b1eed47aa5c74f'/>
<id>urn:sha1:5b7197af7fb379a84895084625b1eed47aa5c74f</id>
<content type='text'>
</content>
</entry>
<entry>
<title>Update some codegen tests for opaque pointers</title>
<updated>2022-05-25T15:29:37+00:00</updated>
<author>
<name>Nikita Popov</name>
<email>npopov@redhat.com</email>
</author>
<published>2022-02-21T10:21:23+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=4d7ff4e5096625b56f154fa485a1af9351c41b5c'/>
<id>urn:sha1:4d7ff4e5096625b56f154fa485a1af9351c41b5c</id>
<content type='text'>
</content>
</entry>
<entry>
<title>Apply noundef attribute to all scalar types which do not permit raw init</title>
<updated>2022-02-26T21:42:33+00:00</updated>
<author>
<name>Erik Desjardins</name>
<email>erikdesjardins@users.noreply.github.com</email>
</author>
<published>2022-02-12T06:38:24+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=5979b681e6f7cfd760f45440624f8bc1759d2071'/>
<id>urn:sha1:5979b681e6f7cfd760f45440624f8bc1759d2071</id>
<content type='text'>
Beyond `&amp;`/`&amp;mut`/`Box`, this covers `char`, discriminants, `NonZero*`, etc.
All such types currently cause a Miri error if left uninitialized,
and an `invalid_value` lint in cases like `mem::uninitialized::&lt;char&gt;()`

Note that this _does not_ change whether or not it is UB for `u64` (or
other integer types with no invalid values) to be undef.
</content>
</entry>
<entry>
<title>apply noundef explicitly in all cases instead of relying on dereferenceable implying it</title>
<updated>2022-02-07T02:11:11+00:00</updated>
<author>
<name>Erik Desjardins</name>
<email>erikdesjardins@users.noreply.github.com</email>
</author>
<published>2022-02-07T02:11:11+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=75ed7def5d31845f5672461c706609bd76f93d08'/>
<id>urn:sha1:75ed7def5d31845f5672461c706609bd76f93d08</id>
<content type='text'>
</content>
</entry>
<entry>
<title>test that MaybeUninit&lt;bool&gt; is not noundef</title>
<updated>2022-02-07T02:09:21+00:00</updated>
<author>
<name>Erik Desjardins</name>
<email>erikdesjardins@users.noreply.github.com</email>
</author>
<published>2022-02-07T02:09:21+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=ced947fc124197bf87e2bdc6741d50db92d296a8'/>
<id>urn:sha1:ced947fc124197bf87e2bdc6741d50db92d296a8</id>
<content type='text'>
</content>
</entry>
<entry>
<title>Apply noundef attribute to &amp;T, &amp;mut T, Box&lt;T&gt;, bool</title>
<updated>2022-02-05T06:09:52+00:00</updated>
<author>
<name>Erik Desjardins</name>
<email>erikdesjardins@users.noreply.github.com</email>
</author>
<published>2022-02-05T06:00:37+00:00</published>
<link rel='alternate' type='text/html' href='http://git.dreamy.place/mirrors/rust/commit/?id=8cb0b6ca5bf1321d38f4602113b0f41c837d0586'/>
<id>urn:sha1:8cb0b6ca5bf1321d38f4602113b0f41c837d0586</id>
<content type='text'>
This doesn't handle `char` because it's a bit awkward to distinguish it
from u32 at this point in codegen.

Note that for some types (like `&amp;Struct` and `&amp;mut Struct`),
we already apply `dereferenceable`, which implies `noundef`,
so the IR does not change.
</content>
</entry>
</feed>
