llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

missed optimization for struct initialization

Open KernelAddress opened this issue 7 years ago • 4 comments

Bugzilla Link 40456
Version trunk
OS Linux
CC @efriedma-quic,@eugenis,@ramosian-glider,@kcc,@jfbastien,@zygoloid

Extended Description

During structure initialization on stack/heap clang avoids touching uninitialized fields and padding. Initialization of uninit fields and padding is allowed and I think in lots of cases will lead to smaller/faster code. For example consider:

Source:

struct A { int a = 0; char b; short c = 0; char d = 0; short e; short f; char g = 0; };

struct B { int a = 0; char b = 0; short c = 0; char d = 0; short e = 0; short f = 0; char g = 0; };

struct C { int a = 0; short b = 0; short c = 0; short d = 0; short e = 0; short f = 0; short g = 0; };

A* newA() { return new A; } B* newB() { return new B; } C* newC() { return new C; }

Generated code with -O3:

0000000000000000 <_Z4newAv>: 0: 50 push %rax 1: bf 10 00 00 00 mov $0x10,%edi 6: e8 00 00 00 00 callq b <_Z4newAv+0xb> b: c7 00 00 00 00 00 movl $0x0,(%rax) 11: 66 c7 40 06 00 00 movw $0x0,0x6(%rax) 17: c6 40 08 00 movb $0x0,0x8(%rax) 1b: c6 40 0e 00 movb $0x0,0xe(%rax) 1f: 59 pop %rcx 20: c3 retq
21: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 28: 00 00 00 2b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)

0000000000000030 <_Z4newBv>: 30: 50 push %rax 31: bf 10 00 00 00 mov $0x10,%edi 36: e8 00 00 00 00 callq 3b <_Z4newBv+0xb> 3b: c7 00 00 00 00 00 movl $0x0,(%rax) 41: c6 40 04 00 movb $0x0,0x4(%rax) 45: 66 c7 40 06 00 00 movw $0x0,0x6(%rax) 4b: c6 40 08 00 movb $0x0,0x8(%rax) 4f: c7 40 0a 00 00 00 00 movl $0x0,0xa(%rax) 56: c6 40 0e 00 movb $0x0,0xe(%rax) 5a: 59 pop %rcx 5b: c3 retq
5c: 0f 1f 40 00 nopl 0x0(%rax)

0000000000000060 <_Z4newCv>: 60: 50 push %rax 61: bf 10 00 00 00 mov $0x10,%edi 66: e8 00 00 00 00 callq 6b <_Z4newCv+0xb> 6b: 0f 57 c0 xorps %xmm0,%xmm0 6e: 0f 11 00 movups %xmm0,(%rax) 71: 59 pop %rcx 72: c3 retq

Clang could emit 1 movups in all of these cases. That's 2 instructions/6 bytes vs 6 instructions/31 bytes.

clang version 8.0.0 (trunk 350842)

KernelAddress avatar Jan 25 '19 08:01 KernelAddress

In general I'd like us to get to a state where:

  1. By default, clang doesn't touch undef / padding:
  • It either memcpy's from a private global (which doesn't touch padding, and can contain under);
  • Or it generates a store of an immediate struct, with same restrictions as above (note: this seems to optimize poorly, and we can't have huge structs because IR limits certain things to 16 bits).
  1. When asked to initialize stack / heap / padding, clang explicitly stores to any padding or undef. It already does so in limited places by doing some bit cast.
  • IIRC it synthesizes the padding as a vector of i8
  • I added the following for undef: https://github.com/llvm/llvm-project/blob/76c9877b56b15f4aee1b45643db89db7cf178827/clang/lib/CodeGen/CGDecl.cpp#L1633

I'll be doing the rest of 2. pretty soon. This bug, AFAICT, isn't about 2., but you want LLVM to do something similar: knowing there's undef / padding in there, figure out whether a bunch of stores is better than something like memset / memcpy. Today clang does some of this with a ridiculous heuristic which should move out of clang and into LLVM: https://github.com/llvm/llvm-project/blob/76c9877b56b15f4aee1b45643db89db7cf178827/clang/lib/CodeGen/CGDecl.cpp#L856

  1. is kinda shared / duplicated between clang and middle-end / backend right now. We should move some stuff out of clang as we teach it to be smarter. If you look at patches I submitted to CGDecl.cpp in the last few months, I've started some of this work.

jfbastien avatar Jan 25 '19 18:01 jfbastien

There are two ways to reason about this sort of transform: one, we're allowed to store to uninitialized data, and two, the C standard allows stores to structures and structure members to modify padding for that struct. (The C++ standard doesn't explicitly allow the latter, as far as I can tell, but it's probably reasonable enough.)

The first is straightforward to implement: if memdep/MemorySSA finds an allocation call, the data is uninitialized. (See e.g. MemCpyOptPass::performCallSlotOptzn.) Maybe we could extend memcpyopt itself for this sort of code.

For the second, it's not clear how we would model assignments to struct members. I guess in some cases, the frontend could handle it (e.g. if we can constant-evaluate a constructor, we can transform it into a memcpy). But in general, we currently have no way to represent padding associated with a store in IR.

efriedma-quic avatar Jan 25 '19 17:01 efriedma-quic

Another related example:

struct A { int a; char b; short c; char d; short e; short f; char g; };

struct B { int a; short b; short c; short d; short e; short f; short g; };

void initA(A* a) { a->a = 0; a->b = 0; a->c = 0; a->d = 0; a->f = 0; a->e = 0; a->g = 0; }

void initB(B* a) { a->a = 0; a->b = 0; a->c = 0; a->d = 0; a->f = 0; a->e = 0; a->g = 0; }

0000000000000000 <_Z5initAP1A>: 0: c7 07 00 00 00 00 movl $0x0,(%rdi) 6: c6 47 04 00 movb $0x0,0x4(%rdi) a: 66 c7 47 06 00 00 movw $0x0,0x6(%rdi) 10: c6 47 08 00 movb $0x0,0x8(%rdi) 14: c7 47 0a 00 00 00 00 movl $0x0,0xa(%rdi) 1b: c6 47 0e 00 movb $0x0,0xe(%rdi) 1f: c3 retq

0000000000000020 <_Z5initBP1B>: 20: 0f 57 c0 xorps %xmm0,%xmm0 23: 0f 11 07 movups %xmm0,(%rdi) 26: c3 retq

Could clang emit the same short code for initA?

KernelAddress avatar Jan 25 '19 08:01 KernelAddress

Kostya, do you think fixing this can improve our results with full stack/heap initialization?

KernelAddress avatar Jan 25 '19 08:01 KernelAddress