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 did the Axis intend to hold the Caucasus?
Should I accept an invitation to give a talk from someone who might review my proposal?
Dual-national, returning to US the day the US Passport expires; can he check in with airline on Dutch passport but reenter with expiring US passport?
Does dual boot harm a laptop battery or reduce its life?
ECDSA: Why is SigningKey shorter than VerifyingKey
Is it safe if the neutral lead is exposed and disconnected?
Is this photo showing a woman standing in the nude before teenagers real?
Is it okay for me to decline a project on ethical grounds?
What container to use to store developer concentrate?
Exploiting the delay when a festival ticket is scanned
How does one get an animal off of the Altar surreptitiously?
Name These Animals
Desktop app status bar: Notification vs error message
Why did I lose on time with 3 pawns vs Knight. Shouldn't it be a draw?
Why isn't there any 9.5 digit multimeter or higher?
Irreducible factors of primitive permutation group representation
Telling manager project isn't worth the effort?
Copying an existing HTML page and use it, is that against any copyright law?
What steps would an amateur scientist have to take in order to get a scientific breakthrough published?
What happens when a flying sword is killed?
reconstruction filter - How does it actually work?
Polyhedra, Polyhedron, Polytopes and Polygon
If Trump gets impeached, how long would Pence be president?
(3 of 11: Akari) What is Pyramid Cult's Favorite Car?
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 9 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