C++ constructors not completely eliminated from function-level static constants
| Bugzilla Link | 6551 |
| Resolution | FIXED |
| Resolved on | Jun 02, 2020 02:04 |
| Version | trunk |
| OS | All |
| CC | @asl,@lattner,@ekatz,@nlewycky |
Extended Description
llvm-g++ compiles this into a nice constant table:
#include
typedef std::pair<int,int> P;
static const P table[] = { P(1,2), P(3,4), P(5,6) };
P f(int n) { return table[n]; }
Produces the table:
.section __TEXT,__const
.align 4
__ZL5table: .long 1 .long 2 .long 3 .long 4 .long 5 .long 6
If the static const table is moved into the function
P f(int n) { static const P table[] = { P(1,2), P(3,4), P(5,6) }; return table[n]; }
We get horrible dynamic initialization:
Llabel3: movl %edi, %ebx cmpb $0, __ZGVZ1fiE5table(%rip) jne LBB1_3 leaq __ZGVZ1fiE5table(%rip), %rdi callq ___cxa_guard_acquire testl %eax, %eax je LBB1_3 leaq __ZGVZ1fiE5table(%rip), %rdi movl $1, __ZZ1fiE5table(%rip) movl $2, __ZZ1fiE5table+4(%rip) movl $3, __ZZ1fiE5table+8(%rip) movl $4, __ZZ1fiE5table+12(%rip) movl $5, __ZZ1fiE5table+16(%rip) movl $6, __ZZ1fiE5table+20(%rip) callq ___cxa_guard_release
Fixed in clang 6.0.
Nick is working on this.
P is marked in the IR as zero initialized. This makes life hard for the optimizers: they have to deduce that the stores to P come before any reads, and that can't be done without teaching them about the semantics of cxa_guard_acquire. Here's a plan that would improve the situation:
(1) have llvm-gcc output P to the IR with "undef" as initializer, for example like this:
--- llvm-backend.cpp (revision 98351) +++ llvm-backend.cpp (working copy) @@ -975,7 +975,13 @@ // This global should be zero initialized. Reconvert the type in case the // forward def of the global and the real def differ in type (e.g. declared // as 'int A[]', and defined as 'int A[100]').
- Init = Constant::getNullValue(ConvertType(TREE_TYPE(decl)));
- if (TYPE_NEEDS_CONSTRUCTING(TREE_TYPE(decl))) {
-
// The global will be initialized by a code sequence, so no need to start -
// with zero. -
Init = UndefValue::get(ConvertType(TREE_TYPE(decl))); - } else {
-
Init = Constant::getNullValue(ConvertType(TREE_TYPE(decl))); - } } else { assert((TREE_CONSTANT(DECL_INITIAL(decl)) || TREE_CODE(DECL_INITIAL(decl)) == STRING_CST) &&
(2) then the optimizers can correctly push the stores into the initializer of P. That's because any reads from P will either get 'undef' or the stored value, and since 'undef' may be anything we may suppose it is the stored value. Indeed GlobalOpt already does this optimization, but only for scalar values. So the second part of the plan is to generalize this existing optimization to aggregates.
At this point all the stores to P will have been removed. That still leaves something like this in the IR:
"3": %2 = tail call i32 @__cxa_guard_acquire(i64* @_ZGVZ1fiE5table) nounwind %3 = icmp eq i32 %2, 0 br i1 %3, label %"5", label %"4"
"4": tail call void @__cxa_guard_release(i64* @_ZGVZ1fiE5table) nounwind br label %"5"
"5": ...
Well, the optimizers can't do anything about that without knowing more about cxa_guard_acquire/cxa_guard_release. So to progress further either the optimizers need to be taught a little bit about cxa_guard stuff, or the front-end needs to help out further somehow.
After commit 98178, dragonegg is also producing good code in the first case. I know probably no-one cares but me :)
This optimization is done by GlobalOpt, which has special logic for global ctors. The reason it is not done for dragonegg is that dragonegg produces too good IR! It has constant expressions like this: i32* getelementptr (i32* getelementptr inbounds ([3 x { i32, i32 }]* @_ZL5table, i64 0, i64 1, i32 0), i64 1) which GlobalOpt considers too complicated. The IR coming out of llvm-gcc computes the inner GEP, stores it to an alloca, then loads from the alloca, does the outer GEP on that etc. GlobalOpt knows how to handle this...
Yes, the optimizers seem to be missing a trick here. It is the LLVM optimizers producing the nice code in the first case (the IR coming out of llvm-gcc has explicit stores before the optimizers run), and it's not clear why they don't in the second case. Amusingly (?), dragonegg produces IR which seems essentially the same as that of llvm-gcc for the first case, but the optimizers don't simplify it for some reason: it ends up with explicit stores being done from _GLOBAL__I__Z1fi.