[llvm-bugs] [Bug 41403] New: Stack unnecessarily used when unrolling loops on statically-sized local arrays
via llvm-bugs
llvm-bugs at lists.llvm.org
Fri Apr 5 14:24:55 PDT 2019
https://bugs.llvm.org/show_bug.cgi?id=41403
Bug ID: 41403
Summary: Stack unnecessarily used when unrolling loops on
statically-sized local arrays
Product: libraries
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: Register Allocator
Assignee: unassignedbugs at nondot.org
Reporter: vgatherps at gmail.com
CC: llvm-bugs at lists.llvm.org, quentin.colombet at gmail.com
Hi,
The following code results in unnecessary stack spills when unrolling the outer
loop:
#define LEN 2
// determines to what extent iteration happens in the inner loop
// (do first operation horizontally, then second horizontally)
// this unrolling of the inner loop results in bad codegen
//
// as opposed to the outer loop
// (do operation 1 on first index, then 2, etc, then go to index 2)
#define INNER_LEN 2
static_assert(LEN % INNER_LEN == 0, "length not divisible by inner width");
void test_loop_width(int *in) {
int inner1[LEN];
for (int j = 0; j < (LEN / INNER_LEN); j++) {
for (int i = 0; i < INNER_LEN; i++) {
inner1[i + j*INNER_LEN] = in[i + j*INNER_LEN];
}
for (int i = 0; i < INNER_LEN; i++) {
in[i + j*INNER_LEN] *= inner1[i + j*INNER_LEN];
}
}
}
Results in the following code getting generated:
test_loop_width(int*):
movq (%rdi), %rax
movq %rax, -8(%rsp)
imull (%rdi), %eax
movl %eax, (%rdi)
movl 4(%rdi), %eax
imull -4(%rsp), %eax
movl %eax, 4(%rdi)
retq
Recompiling this with INNER_LEN set to 1 generates the expected code of
test_loop_width(int*):
movl (%rdi), %eax
movl 4(%rdi), %ecx
imull %eax, %eax
movl %eax, (%rdi)
imull %ecx, %ecx
movl %ecx, 4(%rdi)
retq
The second vertical pattern scales to arbitrary lengths, while the horizontal
pattern uses the stack aggressively.
Ideally, llvm would not be using the stack here and would be able to recognize
that the two are equivalent and code could be reordered to not use the stack.
I don't think this is an aliasing bug since the code vectorizes for larger
lengths in each case, just that the horizontal case still uses the stack.
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190405/fa482cb1/attachment.html>
More information about the llvm-bugs
mailing list