<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/112556>112556</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang++] Assertion `DesiredOffset >= SizeSoFar && "elements out of order"' failed.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          yijan4845
      </td>
    </tr>
</table>

<pre>
    **This testcase is generated by a fuzzer.**

Compiler Explorer: [https://godbolt.org/z/dEnndzno1](https://godbolt.org/z/dEnndzno1)

This valid code will crash on Clang Assertion Trunk, it seems that this goes back to clang-17:
```cpp
struct __attribute__((packed, aligned(2))) PackedAndPadded {
  ~PackedAndPadded();
  char c;
  int n;
};
struct WithPackedAndPadded {
  [[no_unique_address]] PackedAndPadded pap;
  PackedAndPadded pap_1;  char d;
};
WithPackedAndPadded wpap = {1, 2, 3};
```

Stack dump:
```
clang++: /root/llvm-project/clang/lib/CodeGen/CGExprConstant.cpp:494: static llvm::Constant* {anonymous}::ConstantAggregateBuilder::buildFrom(clang::CodeGen::CodeGenModule&, llvm::ArrayRef<llvm::Constant*>, llvm::ArrayRef<clang::CharUnits>, clang::CharUnits, clang::CharUnits, bool, llvm::Type*, bool): Assertion `DesiredOffset >= SizeSoFar && "elements out of order"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics <source>
1.      <eof> parser at end of file
2.      <source>:10:21: LLVM IR generation of declaration 'wpap'
3.      <source>:10:21: Generating code for declaration 'wpap'
 #0 0x0000000003bb1d58 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bb1d58)
 #1 0x0000000003bafa1c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bafa1c)
 #2 0x0000000003afcd68 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x000072e83c842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x000072e83c8969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x000072e83c842476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x000072e83c8287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x000072e83c82871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x000072e83c839e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x0000000003eddc85 (anonymous namespace)::ConstantAggregateBuilder::buildFrom(clang::CodeGen::CodeGenModule&, llvm::ArrayRef<llvm::Constant*>, llvm::ArrayRef<clang::CharUnits>, clang::CharUnits, clang::CharUnits, bool, llvm::Type*, bool) CGExprConstant.cpp:0:0
#10 0x0000000003ede31f (anonymous namespace)::ConstStructBuilder::Finalize(clang::QualType) (.isra.0) CGExprConstant.cpp:0:0
#11 0x0000000003ee73df (anonymous namespace)::ConstStructBuilder::BuildStruct(clang::CodeGen::ConstantEmitter&, clang::InitListExpr const*, clang::QualType) CGExprConstant.cpp:0:0
#12 0x0000000003ee5a6b clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::ConstExprEmitter, llvm::Constant*, clang::QualType>::Visit(clang::Stmt const*, clang::QualType) CGExprConstant.cpp:0:0
#13 0x0000000003eea889 clang::CodeGen::ConstantEmitter::tryEmitPrivateForVarInit(clang::VarDecl const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3eea889)
#14 0x0000000003eeaae2 clang::CodeGen::ConstantEmitter::tryEmitForInitializer(clang::VarDecl const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3eeaae2)
#15 0x0000000003fd67fe clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3fd67fe)
#16 0x0000000003ffa039 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3ffa039)
#17 0x0000000003ffa67b clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3ffa67b)
#18 0x0000000004004a6b clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#19 0x0000000004528896 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#20 0x0000000004519478 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4519478)
#21 0x000000000666aed4 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x666aed4)
#22 0x0000000004526408 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4526408)
#23 0x00000000047e0629 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47e0629)
#24 0x000000000475f1ae clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x475f1ae)
#25 0x00000000048c5dce clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48c5dce)
#26 0x0000000000cdce6f cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcdce6f)
#27 0x0000000000cd4b7a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#28 0x000000000456a119 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#29 0x0000000003afd214 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3afd214)
#30 0x000000000456a70f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#31 0x000000000453000d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x453000d)
#32 0x00000000045310fd clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x45310fd)
#33 0x0000000004538a25 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4538a25)
#34 0x0000000000cd9d3f clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcd9d3f)
#35 0x0000000000ba9264 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xba9264)
#36 0x000072e83c829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#37 0x000072e83c829e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#38 0x0000000000cd462e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcd462e)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
ASM generation compiler returned: 134
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWk9z2zjy_TTwBSUVCVL8c_BBlqz8MpWpyS_yZo8qkGhKGFMAFwAdOYf97FsAKZugJa2dsmcPu1OelAgC3e89NBoNklRrvhUA12h2g2bLK9qanVTXj_xPKuIsnl0Vkj1eIzJHZH634xob0KakGjDXeAsCFDXAcPGIKa7anz9BTbvOKFiioP93IfcNr0Hh20NTSwUKRXOMZjc7YxqNojkiK0RWW8kKWZupVFtEVj8RWbFbIdhPIUM0WyKSvb47yYfuHe4HWnOGS8kA_-B1jUtF9Q5LgRc1FVs81xqU4VLgO9WKe0QWmBusAfYamx012FgjWwkaF7S8x0bi0g6chKlF1HlLgu6vbJquRRvVlgZvNtQYxYvWwGaDSIZI1tDyHph1Q2s7AQyRjFjc7g9_dbfngn2ljAHDKL3pLGL8z9E9Zy9H0VOHckcVLgcNXBgsnq5Runz63eP7Oze7Cx5daNwIuWkF_0cLG8qYAq3tpMyWL6A2tBn4PnF3E6LopofJTsI6hedHQxuMoqUFFlrdiP0nGg570n84-Wtjp4u1--blPHWXbh4RubF_Ni7JSklpEFnV9cN-0ij5J5T2su-3qnmByGohGXwCYX99uj00aiGFNlSYqZ38aB7nsTWmDTW8xNaSdR_Nj90QmVsmVEjxuJetdjyGHebbrYItNXDT8pq5JYOieWEvVkruEck6PP2gDszw4nfJ2hoQSaxOzwDmStHHb1ChaHESFYpuzw4YetxR9TfBje77n7x1ob2Qsvb93D024FLH8WZuBXxelygJlqC5AvZHVWkw2HqOlnjNf8JarqjCjmuCESFQwx6E0Vi2BssKS2UVJASRFFeU18Cm3dx__XI7X99i3RZ7bjDFRbvFChqpjF3ho4TDza4tpqUV38XGyxDhWregEVlhKhjmoqxbBtjsoE83NnUYRUuwLBsFjZIlaA0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