8000 revise example · sarvex/rust-memory-model@8a0f29e · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a0f29e

Browse files
committed
revise example
1 parent c24010d commit 8a0f29e

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

optimizations/up-propagation.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,47 @@
66
// pointer.
77
fn write_to_ret(condition: bool) -> Data {
88
let f = get_data();
9-
if condition { abort(); }
9+
XXX
1010
f
1111
}
1212

13-
fn write_to_ref1(condition: bool, result: &mut Data) {
13+
fn write_to_ref(condition: bool, result: &mut Data) {
1414
let f = get_data();
15-
if condition { abort(); }
16-
*result = f;
17-
}
18-
19-
fn write_to_ref2(condition: bool, result: &mut Data) {
20-
let f = get_data();
21-
if condition {
22-
/* something that does not abort but does not panic */
23-
}
15+
XXX
2416
*result = f;
2517
}
2618

2719
fn write_to_raw1(condition: bool, result: *mut Data) {
2820
let f = get_data();
29-
if condition { abort(); }
21+
XXX
3022
unsafe { *result = f; } // maybe not, did not observe a write
3123
}
3224

3325
fn write_to_raw1a(condition: bool, result: &mut Data) {
3426
let f = get_data();
35-
if condition { abort(); }
27+
XXX
3628
*result = f; // but not this
3729

3830
let f = get_data();
39-
if condition { abort(); }
31+
XXX
4032
*result = f; // can NRVO this one
4133
}
4234

4335
fn write_to_raw2(condition: bool, result: *mut Data) {
4436
let result = unsafe { &mut *result };
4537
let f = get_data();
46-
if condition { abort(); }
38+
XXX
4739
*result = f;
4840
}
4941

5042
fn write_to_raw3(condition: bool, result: *mut Data) {
5143
let f = get_data();
52-
if condition { abort(); }
44+
XXX
5345
unsafe { *result = f; }
5446

5547
let result = &mut * result; // this cannot "be moved"
5648
let f = get_data();
57-
if condition { abort(); }
49+
XXX
5850
*result = f; // can only be promoted as high as the `&mut *result`
5951
}
6052
```
@@ -68,13 +60,18 @@ eventual location. The danger typically arises because that write
6860
would be visible (potentially) earlier than it was "supposed" to
6961
happen -- when is that ok?
7062

71-
These examples mostly include a conditional abort in between: the
72-
assumption is that the compiler understands that abort *aborts* and
73-
does not return.
74-
75-
Some factors that are relevant:
76-
77-
- could other threads observe?
63+
These examples include some unknown behavior XXX in between. Typically
64+
speaking, whether the optimization can be performed will depend (in
65+
each case) on what this XXX is:
66+
67+
- For example, it may contain a *conditional abort*. In that case, if
68+
we optimize the write to occur early, we don't know that the write
69+
would even ever execute!
70+
- Similarly, if XXX might unwind, that could lead to potential reads
71+
of the ultimate destination, in some cases.
72+
- Finally, the XXX might read from the ultimate destination through an
73+
alias (in some cases, that read may be considered undefined
74+
behavior, however).
7875

7976

8077
### Source

0 commit comments

Comments
 (0)
0