Closed
Description
Continue the discussion in #73780 (comment).
nbdd0121 said that:
let bar: [u32; 10] = [0,10,20,30,40,50,60,70,80,90];Semantically just says: put this on the stack. Rust is doing exactly what you ask it to do;
it cannot place foo inside rodata because it is a value.
Consider this code: https://rust.godbolt.org/z/6TBstc
pub fn foo(x: usize) -> i32 {
let base: [i32; 64] = [
67, 754, 860, 559, 368, 870, 548, 972,
141, 731, 351, 664, 32, 4, 996, 741,
203, 292, 237, 480, 151, 940, 777, 540,
143, 587, 747, 65, 152, 517, 882, 880,
712, 595, 370, 901, 237, 53, 789, 785,
912, 650, 896, 367, 316, 392, 62, 473,
675, 691, 281, 192, 445, 970, 225, 425,
628, 324, 322, 206, 912, 867, 462, 92
];
base[x % 64]
}
The Rust compiler puts base
on the stack in both opt-level 2 and 3:
The same code in C (I know that C array is mostly pointer): https://godbolt.org/z/WYbKs97E6
#include <stdlib.h>
#include <stdint.h>
uint32_t square(size_t x) {
uint32_t bases[64] = {
67, 754, 860, 559, 368, 870, 548, 972,
141, 731, 351, 664, 32, 4, 996, 741,
203, 292, 237, 480, 151, 940, 777, 540,
143, 587, 747, 65, 152, 517, 882, 880,
712, 595, 370, 901, 237, 53, 789, 785,
912, 650, 896, 367, 316, 392, 62, 473,
675, 691, 281, 192, 445, 970, 225, 425,
628, 324, 322, 206, 912, 867, 462, 92
};
return bases[x % 64];
}
clang (with -O1
) optimizes out the bases
array to .rodata
and use that static array directly.
gcc also fails to optimized. In -Oz
gcc optimizes it to a memcpy.
Could the same optimization be done in Rust?
Metadata
Metadata
Assignees
Labels
Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: MIR optimizationsCategory: An issue proposing an enhancement or a PR with one.Category: An issue highlighting optimization opportunities or PRs implementing suchIssue: Problems and improvements with respect to binary size of generated code.Issue: Problems and improvements with respect to performance of generated code.Relevant to the compiler team, which will review and decide on the PR/issue.