diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp --- a/clang/lib/CodeGen/CGDeclCXX.cpp +++ b/clang/lib/CodeGen/CGDeclCXX.cpp @@ -578,7 +578,9 @@ // COMDAT group associated with the global, so the initializers get folded // too. I = DelayedCXXInitPosition.find(D); - unsigned LexOrder = I == DelayedCXXInitPosition.end() ? ~0U : I->second; + // CXXGlobalInits.size() is the lex order for non-deferred emission. + unsigned LexOrder = + I == DelayedCXXInitPosition.end() ? CXXGlobalInits.size() : I->second; AddGlobalCtor(Fn, 65535, LexOrder, COMDATKey); if (COMDATKey && (getTriple().isOSBinFormatELF() || getTarget().getCXXABI().isMicrosoft())) { diff --git a/clang/test/CodeGenCXX/static-init-inline-variable.cpp b/clang/test/CodeGenCXX/static-init-inline-variable.cpp --- a/clang/test/CodeGenCXX/static-init-inline-variable.cpp +++ b/clang/test/CodeGenCXX/static-init-inline-variable.cpp @@ -1,7 +1,14 @@ // RUN: %clang_cc1 -std=c++17 -S -emit-llvm -disable-llvm-passes -o - %s -triple x86_64-linux-gnu | FileCheck %s +struct A { + int x; + A(int x) : x(x) {} + ~A() {} +}; + inline int a = 1; -inline int b = a + 1; -inline int c = b + 1; +inline A b(a + 1); +inline int c = b.x + 1; int d = c; -// CHECK: @llvm.global_ctors = appending global [3 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.2, ptr @b }, { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.1, ptr @c }, { i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_static_init_inline_variable.cpp, ptr null }] + +// CHECK: @llvm.global_ctors = appending global [3 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @b }, { i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init.2, ptr @c }, { i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_static_init_inline_variable.cpp, ptr null }] \ No newline at end of file