Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?Why can't (or doesn't) the compiler optimize a predictable addition loop into a multiplication?Does clang or gcc take advantage of referencing restrictions for alias analysisWhy can lambdas be better optimized by the compiler than plain functions?Why does the enhanced GCC 6 optimizer break practical C++ code?How to make a Rust mutable reference immutable?Why does creating a mutable reference to a dereferenced mutable reference work?Why can the Rust compiler not optimize Option::take and an “if let” if you print the value?Why can the Rust compiler not optimize away the Err arm of Box::downcast?Why is casting a const reference directly to a mutable reference invalid in Rust?Is it possible to unborrow a mutable reference in rust?
How do I access the checkbox field column value as a PHP array?
How can Paypal know my card is being used in another account?
Is it safe if the neutral lead is exposed and disconnected?
What steps would an amateur scientist have to take in order to get a scientific breakthrough published?
Can I change the license of a forked project to the MIT if the license of the parent project has changed from the GPL to the MIT?
Is it error of law to judge on less relevant case law when there is much more relevant one?
If Trump gets impeached, how long would Pence be president?
Finding the Maximum of a Continuous Function over a Closed Interval
8086 stack segment and avoiding overflow in interrupts
How did the SysRq key get onto modern keyboards if it's rarely used?
reconstruction filter - How does it actually work?
Is there a wealth gap in Boston where the median net worth of white households is $247,500 while the median net worth for black families was $8?
Composing fill in the blanks
Are the named pipe created by `mknod` and the FIFO created by `mkfifo` equivalent?
Why is it considered acid rain with pH <5.6?
Why is the number of local variables used in a Java bytecode method not the most economical?
(3 of 11: Akari) What is Pyramid Cult's Favorite Car?
How long until two planets become one?
Can a US President, after impeachment and removal, be re-elected or re-appointed?
Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?
How to store my pliers and wire cutters on my desk?
Why did Windows 95 crash the whole system but newer Windows only crashed programs?
How did the Sinclair compare on price with the C64 in the UK?
Can you place a support header in the ceiling?
Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?
Why can't (or doesn't) the compiler optimize a predictable addition loop into a multiplication?Does clang or gcc take advantage of referencing restrictions for alias analysisWhy can lambdas be better optimized by the compiler than plain functions?Why does the enhanced GCC 6 optimizer break practical C++ code?How to make a Rust mutable reference immutable?Why does creating a mutable reference to a dereferenced mutable reference work?Why can the Rust compiler not optimize Option::take and an “if let” if you print the value?Why can the Rust compiler not optimize away the Err arm of Box::downcast?Why is casting a const reference directly to a mutable reference invalid in Rust?Is it possible to unborrow a mutable reference in rust?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
As far as I know, reference/pointer aliasing can hinder the compiler's ability to generate optimized code, since they must ensure the generated binary behaves correctly in the case where the two references/pointers indeed alias. For instance, in the following C code,
void adds(int *a, int *b)
*a += *b;
*a += *b;
when compiled by clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
with the -O3
flag, it emits
0000000000000000 <adds>:
0: 8b 07 mov (%rdi),%eax
2: 03 06 add (%rsi),%eax
4: 89 07 mov %eax,(%rdi) # the first time
6: 03 06 add (%rsi),%eax
8: 89 07 mov %eax,(%rdi) # the second time
a: c3 retq
Here the code stores back to (%rdi)
twice in case int *a
and int *b
alias.
When we explicitly tell the compiler that these two pointers cannot alias with the restrict
keyword:
void adds(int * restrict a, int * restrict b)
*a += *b;
*a += *b;
Then clang will emit a more optimized version of binary code
0000000000000000 <adds>:
0: 8b 06 mov (%rsi),%eax
2: 01 c0 add %eax,%eax
4: 01 07 add %eax,(%rdi)
6: c3 retq
Since Rust makes sure (except in unsafe code) that two mutable references cannot alias, I would think that the compiler should be able to emit the more optimized version of the code.
When I test with the code below and compile it with rustc 1.35.0
with -C opt-level=3 --emit obj
#![crate_type = "staticlib"]
#[no_mangle]
fn adds(a: &mut i32, b: &mut i32)
*a += *b;
*a += *b;
it generates
0000000000000000 <adds>:
0: 8b 07 mov (%rdi),%eax
2: 03 06 add (%rsi),%eax
4: 89 07 mov %eax,(%rdi)
6: 03 06 add (%rsi),%eax
8: 89 07 mov %eax,(%rdi)
a: c3 retq
This does not take advantage of the guarantee that a
and b
cannot alias.
Is this because the current Rust compiler is still in development and has not yet incorporated alias analysis to do the optimization?
Is this because there is still a chance that a
and b
could alias, even in safe Rust?
rust compiler-optimization
add a comment |
As far as I know, reference/pointer aliasing can hinder the compiler's ability to generate optimized code, since they must ensure the generated binary behaves correctly in the case where the two references/pointers indeed alias. For instance, in the following C code,
void adds(int *a, int *b)
*a += *b;
*a += *b;
when compiled by clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
with the -O3
flag, it emits
0000000000000000 <adds>:
0: 8b 07 mov (%rdi),%eax
2: 03 06 add (%rsi),%eax
4: 89 07 mov %eax,(%rdi) # the first time
6: 03 06 add (%rsi),%eax
8: 89 07 mov %eax,(%rdi) # the second time
a: c3 retq
Here the code stores back to (%rdi)
twice in case int *a
and int *b
alias.
When we explicitly tell the compiler that these two pointers cannot alias with the restrict
keyword:
void adds(int * restrict a, int * restrict b)
*a += *b;
*a += *b;
Then clang will emit a more optimized version of binary code
0000000000000000 <adds>:
0: 8b 06 mov (%rsi),%eax
2: 01 c0 add %eax,%eax
4: 01 07 add %eax,(%rdi)
6: c3 retq
Since Rust makes sure (except in unsafe code) that two mutable references cannot alias, I would think that the compiler should be able to emit the more optimized version of the code.
When I test with the code below and compile it with rustc 1.35.0
with -C opt-level=3 --emit obj
#![crate_type = "staticlib"]
#[no_mangle]
fn adds(a: &mut i32, b: &mut i32)
*a += *b;
*a += *b;
it generates
0000000000000000 <adds>:
0: 8b 07 mov (%rdi),%eax
2: 03 06 add (%rsi),%eax
4: 89 07 mov %eax,(%rdi)
6: 03 06 add (%rsi),%eax
8: 89 07 mov %eax,(%rdi)
a: c3 retq
This does not take advantage of the guarantee that a
and b
cannot alias.
Is this because the current Rust compiler is still in development and has not yet incorporated alias analysis to do the optimization?
Is this because there is still a chance that a
and b
could alias, even in safe Rust?
rust compiler-optimization
godbolt.org/z/aEDINX, strange
– Stargateur
8 hours ago
3
Side remark: "Since Rust makes sure (except in unsafe code) that two mutable references cannot alias" -- it is worth mentioning that even inunsafe
code, aliasing mutable references are not allowed and result in undefined behavior. You can have aliasing raw pointers, butunsafe
code does not actually allow you to ignore Rust standard rules. It's just a common misconception and thus worth pointing out.
– Lukas Kalbertodt
7 hours ago
add a comment |
As far as I know, reference/pointer aliasing can hinder the compiler's ability to generate optimized code, since they must ensure the generated binary behaves correctly in the case where the two references/pointers indeed alias. For instance, in the following C code,
void adds(int *a, int *b)
*a += *b;
*a += *b;
when compiled by clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
with the -O3
flag, it emits
0000000000000000 <adds>:
0: 8b 07 mov (%rdi),%eax
2: 03 06 add (%rsi),%eax
4: 89 07 mov %eax,(%rdi) # the first time
6: 03 06 add (%rsi),%eax
8: 89 07 mov %eax,(%rdi) # the second time
a: c3 retq
Here the code stores back to (%rdi)
twice in case int *a
and int *b
alias.
When we explicitly tell the compiler that these two pointers cannot alias with the restrict
keyword:
void adds(int * restrict a, int * restrict b)
*a += *b;
*a += *b;
Then clang will emit a more optimized version of binary code
0000000000000000 <adds>:
0: 8b 06 mov (%rsi),%eax
2: 01 c0 add %eax,%eax
4: 01 07 add %eax,(%rdi)
6: c3 retq
Since Rust makes sure (except in unsafe code) that two mutable references cannot alias, I would think that the compiler should be able to emit the more optimized version of the code.
When I test with the code below and compile it with rustc 1.35.0
with -C opt-level=3 --emit obj
#![crate_type = "staticlib"]
#[no_mangle]
fn adds(a: &mut i32, b: &mut i32)
*a += *b;
*a += *b;
it generates
0000000000000000 <adds>:
0: 8b 07 mov (%rdi),%eax
2: 03 06 add (%rsi),%eax
4: 89 07 mov %eax,(%rdi)
6: 03 06 add (%rsi),%eax
8: 89 07 mov %eax,(%rdi)
a: c3 retq
This does not take advantage of the guarantee that a
and b
cannot alias.
Is this because the current Rust compiler is still in development and has not yet incorporated alias analysis to do the optimization?
Is this because there is still a chance that a
and b
could alias, even in safe Rust?
rust compiler-optimization
As far as I know, reference/pointer aliasing can hinder the compiler's ability to generate optimized code, since they must ensure the generated binary behaves correctly in the case where the two references/pointers indeed alias. For instance, in the following C code,
void adds(int *a, int *b)
*a += *b;
*a += *b;
when compiled by clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
with the -O3
flag, it emits
0000000000000000 <adds>:
0: 8b 07 mov (%rdi),%eax
2: 03 06 add (%rsi),%eax
4: 89 07 mov %eax,(%rdi) # the first time
6: 03 06 add (%rsi),%eax
8: 89 07 mov %eax,(%rdi) # the second time
a: c3 retq
Here the code stores back to (%rdi)
twice in case int *a
and int *b
alias.
When we explicitly tell the compiler that these two pointers cannot alias with the restrict
keyword:
void adds(int * restrict a, int * restrict b)
*a += *b;
*a += *b;
Then clang will emit a more optimized version of binary code
0000000000000000 <adds>:
0: 8b 06 mov (%rsi),%eax
2: 01 c0 add %eax,%eax
4: 01 07 add %eax,(%rdi)
6: c3 retq
Since Rust makes sure (except in unsafe code) that two mutable references cannot alias, I would think that the compiler should be able to emit the more optimized version of the code.
When I test with the code below and compile it with rustc 1.35.0
with -C opt-level=3 --emit obj
#![crate_type = "staticlib"]
#[no_mangle]
fn adds(a: &mut i32, b: &mut i32)
*a += *b;
*a += *b;
it generates
0000000000000000 <adds>:
0: 8b 07 mov (%rdi),%eax
2: 03 06 add (%rsi),%eax
4: 89 07 mov %eax,(%rdi)
6: 03 06 add (%rsi),%eax
8: 89 07 mov %eax,(%rdi)
a: c3 retq
This does not take advantage of the guarantee that a
and b
cannot alias.
Is this because the current Rust compiler is still in development and has not yet incorporated alias analysis to do the optimization?
Is this because there is still a chance that a
and b
could alias, even in safe Rust?
rust compiler-optimization
rust compiler-optimization
edited 8 hours ago
Shepmaster
174k20 gold badges383 silver badges535 bronze badges
174k20 gold badges383 silver badges535 bronze badges
asked 8 hours ago


AccienteAcciente
554 bronze badges
554 bronze badges
godbolt.org/z/aEDINX, strange
– Stargateur
8 hours ago
3
Side remark: "Since Rust makes sure (except in unsafe code) that two mutable references cannot alias" -- it is worth mentioning that even inunsafe
code, aliasing mutable references are not allowed and result in undefined behavior. You can have aliasing raw pointers, butunsafe
code does not actually allow you to ignore Rust standard rules. It's just a common misconception and thus worth pointing out.
– Lukas Kalbertodt
7 hours ago
add a comment |
godbolt.org/z/aEDINX, strange
– Stargateur
8 hours ago
3
Side remark: "Since Rust makes sure (except in unsafe code) that two mutable references cannot alias" -- it is worth mentioning that even inunsafe
code, aliasing mutable references are not allowed and result in undefined behavior. You can have aliasing raw pointers, butunsafe
code does not actually allow you to ignore Rust standard rules. It's just a common misconception and thus worth pointing out.
– Lukas Kalbertodt
7 hours ago
godbolt.org/z/aEDINX, strange
– Stargateur
8 hours ago
godbolt.org/z/aEDINX, strange
– Stargateur
8 hours ago
3
3
Side remark: "Since Rust makes sure (except in unsafe code) that two mutable references cannot alias" -- it is worth mentioning that even in
unsafe
code, aliasing mutable references are not allowed and result in undefined behavior. You can have aliasing raw pointers, but unsafe
code does not actually allow you to ignore Rust standard rules. It's just a common misconception and thus worth pointing out.– Lukas Kalbertodt
7 hours ago
Side remark: "Since Rust makes sure (except in unsafe code) that two mutable references cannot alias" -- it is worth mentioning that even in
unsafe
code, aliasing mutable references are not allowed and result in undefined behavior. You can have aliasing raw pointers, but unsafe
code does not actually allow you to ignore Rust standard rules. It's just a common misconception and thus worth pointing out.– Lukas Kalbertodt
7 hours ago
add a comment |
1 Answer
1
active
oldest
votes
Rust originally did enable LLVM's noalias
attribute, but this caused miscompiled code. When all supported LLVM versions no longer miscompile the code, it will be re-enabled.
If you add -Zmutable-noalias=yes
to the compiler options, you get the expected assembly:
adds:
mov eax, dword ptr [rsi]
add eax, eax
add dword ptr [rdi], eax
ret
Simply put, Rust put the equivalent of C's restrict
keyword everywhere, far more prevalent than any usual C program. This exercised corner cases of LLVM more than it was able to handle correctly. It turns out that C and C++ programmers simply don't use restrict
as frequently as &mut
is used in Rust.
This has happened multiple times.
Related Rust issues:
Current case
- Incorrect code generation for nalgebra's Matrix::swap_rows() #54462
- Re-enable noalias annotations by default once LLVM no longer miscompiles them #54878
Previous case
- Workaround LLVM optimizer bug by not marking &mut pointers as noalias #31545
- Mark &mut pointers as noalias once LLVM no longer miscompiles them #31681
Other
- make use of LLVM's scoped noalias metadata #16515
- Missed optimization: references from pointers aren't treated as noalias #38941
- noalias is not enough #53105
- mutable noalias: re-enable permanently, only for panic=abort, or stabilize flag? #45029
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57259126%2fwhy-does-the-rust-compiler-not-optimize-code-assuming-that-two-mutable-reference%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Rust originally did enable LLVM's noalias
attribute, but this caused miscompiled code. When all supported LLVM versions no longer miscompile the code, it will be re-enabled.
If you add -Zmutable-noalias=yes
to the compiler options, you get the expected assembly:
adds:
mov eax, dword ptr [rsi]
add eax, eax
add dword ptr [rdi], eax
ret
Simply put, Rust put the equivalent of C's restrict
keyword everywhere, far more prevalent than any usual C program. This exercised corner cases of LLVM more than it was able to handle correctly. It turns out that C and C++ programmers simply don't use restrict
as frequently as &mut
is used in Rust.
This has happened multiple times.
Related Rust issues:
Current case
- Incorrect code generation for nalgebra's Matrix::swap_rows() #54462
- Re-enable noalias annotations by default once LLVM no longer miscompiles them #54878
Previous case
- Workaround LLVM optimizer bug by not marking &mut pointers as noalias #31545
- Mark &mut pointers as noalias once LLVM no longer miscompiles them #31681
Other
- make use of LLVM's scoped noalias metadata #16515
- Missed optimization: references from pointers aren't treated as noalias #38941
- noalias is not enough #53105
- mutable noalias: re-enable permanently, only for panic=abort, or stabilize flag? #45029
add a comment |
Rust originally did enable LLVM's noalias
attribute, but this caused miscompiled code. When all supported LLVM versions no longer miscompile the code, it will be re-enabled.
If you add -Zmutable-noalias=yes
to the compiler options, you get the expected assembly:
adds:
mov eax, dword ptr [rsi]
add eax, eax
add dword ptr [rdi], eax
ret
Simply put, Rust put the equivalent of C's restrict
keyword everywhere, far more prevalent than any usual C program. This exercised corner cases of LLVM more than it was able to handle correctly. It turns out that C and C++ programmers simply don't use restrict
as frequently as &mut
is used in Rust.
This has happened multiple times.
Related Rust issues:
Current case
- Incorrect code generation for nalgebra's Matrix::swap_rows() #54462
- Re-enable noalias annotations by default once LLVM no longer miscompiles them #54878
Previous case
- Workaround LLVM optimizer bug by not marking &mut pointers as noalias #31545
- Mark &mut pointers as noalias once LLVM no longer miscompiles them #31681
Other
- make use of LLVM's scoped noalias metadata #16515
- Missed optimization: references from pointers aren't treated as noalias #38941
- noalias is not enough #53105
- mutable noalias: re-enable permanently, only for panic=abort, or stabilize flag? #45029
add a comment |
Rust originally did enable LLVM's noalias
attribute, but this caused miscompiled code. When all supported LLVM versions no longer miscompile the code, it will be re-enabled.
If you add -Zmutable-noalias=yes
to the compiler options, you get the expected assembly:
adds:
mov eax, dword ptr [rsi]
add eax, eax
add dword ptr [rdi], eax
ret
Simply put, Rust put the equivalent of C's restrict
keyword everywhere, far more prevalent than any usual C program. This exercised corner cases of LLVM more than it was able to handle correctly. It turns out that C and C++ programmers simply don't use restrict
as frequently as &mut
is used in Rust.
This has happened multiple times.
Related Rust issues:
Current case
- Incorrect code generation for nalgebra's Matrix::swap_rows() #54462
- Re-enable noalias annotations by default once LLVM no longer miscompiles them #54878
Previous case
- Workaround LLVM optimizer bug by not marking &mut pointers as noalias #31545
- Mark &mut pointers as noalias once LLVM no longer miscompiles them #31681
Other
- make use of LLVM's scoped noalias metadata #16515
- Missed optimization: references from pointers aren't treated as noalias #38941
- noalias is not enough #53105
- mutable noalias: re-enable permanently, only for panic=abort, or stabilize flag? #45029
Rust originally did enable LLVM's noalias
attribute, but this caused miscompiled code. When all supported LLVM versions no longer miscompile the code, it will be re-enabled.
If you add -Zmutable-noalias=yes
to the compiler options, you get the expected assembly:
adds:
mov eax, dword ptr [rsi]
add eax, eax
add dword ptr [rdi], eax
ret
Simply put, Rust put the equivalent of C's restrict
keyword everywhere, far more prevalent than any usual C program. This exercised corner cases of LLVM more than it was able to handle correctly. It turns out that C and C++ programmers simply don't use restrict
as frequently as &mut
is used in Rust.
This has happened multiple times.
Related Rust issues:
Current case
- Incorrect code generation for nalgebra's Matrix::swap_rows() #54462
- Re-enable noalias annotations by default once LLVM no longer miscompiles them #54878
Previous case
- Workaround LLVM optimizer bug by not marking &mut pointers as noalias #31545
- Mark &mut pointers as noalias once LLVM no longer miscompiles them #31681
Other
- make use of LLVM's scoped noalias metadata #16515
- Missed optimization: references from pointers aren't treated as noalias #38941
- noalias is not enough #53105
- mutable noalias: re-enable permanently, only for panic=abort, or stabilize flag? #45029
edited 8 hours ago
answered 8 hours ago
ShepmasterShepmaster
174k20 gold badges383 silver badges535 bronze badges
174k20 gold badges383 silver badges535 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57259126%2fwhy-does-the-rust-compiler-not-optimize-code-assuming-that-two-mutable-reference%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
godbolt.org/z/aEDINX, strange
– Stargateur
8 hours ago
3
Side remark: "Since Rust makes sure (except in unsafe code) that two mutable references cannot alias" -- it is worth mentioning that even in
unsafe
code, aliasing mutable references are not allowed and result in undefined behavior. You can have aliasing raw pointers, butunsafe
code does not actually allow you to ignore Rust standard rules. It's just a common misconception and thus worth pointing out.– Lukas Kalbertodt
7 hours ago