LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 31863 - Clang can't read back a PCH just produced: Assertion failed: (D && "Cannot get layout of forward declarations!"), function getASTRecordLayout
Summary: Clang can't read back a PCH just produced: Assertion failed: (D && "Cannot ge...
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: Modules (show other bugs)
Version: 4.0
Hardware: PC All
: P normal
Assignee: Richard Smith
URL:
Keywords:
: 32144 32186 33313 (view as bug list)
Depends on:
Blocks: release-4.0.1
  Show dependency tree
 
Reported: 2017-02-04 02:43 PST by Mehdi Amini
Modified: 2017-06-05 15:21 PDT (History)
12 users (show)

See Also:
Fixed By Commit(s):


Attachments
Repro reduced (272 bytes, application/octet-stream)
2017-02-04 02:43 PST, Mehdi Amini
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mehdi Amini 2017-02-04 02:43:35 PST
Created attachment 17936 [details]
Repro reduced

Clang-4.0 (and trunk) are crashing when building Unreal Engine, a PCH can't be read back we hit this assertion. I believe this is a regression from clang-3.9 introduced in r276159:

Author: Richard Smith <richard-llvm@metafoo.co.uk>
Date:   Wed Jul 20 14:10:16 2016

    [modules] Don't emit initializers for VarDecls within a module eagerly whenever
    we first touch any part of that module. Instead, defer them until the first
    time that module is (transitively) imported. The initializer step for a module
    then recursively initializes modules that its own headers imported.
    
    For example, this avoids running the <iostream> global initializer in programs
    that don't actually use iostreams, but do use other parts of the standard
    library.


Reduced repro attached. Run with:

clang -c -x objective-c++-header pre.h -std=c++14 -o pre.gch
clang -c -x objective-c++ /dev/null    -std=c++14 -include-pch pre.gch
Comment 1 Duncan 2017-02-04 08:50:21 PST
(In reply to comment #0)
> clang -c -x objective-c++-header pre.h -std=c++14 -o pre.gch
> clang -c -x objective-c++ /dev/null    -std=c++14 -include-pch pre.gch

Note: the same repro works with -x c++-header and -x c++.
Comment 2 Bruno Cardoso Lopes 2017-02-09 02:24:07 PST
@rsmith, I've attempted a fix, can you take a look? https://reviews.llvm.org/D29753
Comment 3 Bruno Cardoso Lopes 2017-03-01 11:36:57 PST
A stopgap fix for PCH was committed in r296656.

The problem still happens with modules though. Richard triggered the issue with:

  $ cat test/PCH/empty-def-fwd-struct.modulemap
  module M { header "empty-def-fwd-struct.h" }
  $ clang -cc1 -x c++ -std=c++14 -fmodules -emit-module -fmodule-name=M test/PCH/empty-def-fwd-struct.modulemap -o tmp.pcm
  $ cat use.cpp
  #include "test/PCH/empty-def-fwd-struct.h"
  $ clang -cc1 -x c++ -std=c++14 -fmodules -emit-llvm -fmodule-file=tmp.pcm use.cpp -o -
  clang: [...]/src/tools/clang/lib/AST/RecordLayoutBuilder.cpp:2933: const clang::ASTRecordLayout &clang::ASTContext::getASTRecordLayout(const clang::RecordDecl *) const: Assertion `D && "Cannot get layout of forward declarations!"' failed.

Leaving this open until we find a more complete solution.
Comment 4 Hans Wennborg 2017-03-01 13:48:51 PST
(In reply to Bruno Cardoso Lopes from comment #3)
> A stopgap fix for PCH was committed in r296656.

Thanks! I will merge it to the release branch once it's been in tree for a bit.
Comment 5 Hans Wennborg 2017-03-02 09:00:37 PST
(In reply to Hans Wennborg from comment #4)
> (In reply to Bruno Cardoso Lopes from comment #3)
> > A stopgap fix for PCH was committed in r296656.
> 
> Thanks! I will merge it to the release branch once it's been in tree for a
> bit.

Merged to 4.0 in r296762.

If we can fix this properly for 4.0.1 that would be great, so marking it a blocker of that.
Comment 6 Richard Smith 2017-03-09 14:30:52 PST
*** Bug 32186 has been marked as a duplicate of this bug. ***
Comment 7 Richard Smith 2017-03-09 14:31:42 PST
PR32186 has a totally unrelated symptom with the same root cause (the call to DeclMustBeEmitted in a context where AST invariants are not met during deserialization).
Comment 8 Gonzalo BG 2017-03-10 01:04:35 PST
*** Bug 32144 has been marked as a duplicate of this bug. ***
Comment 9 Richard Smith 2017-04-26 14:38:17 PDT
This was mostly addressed by https://reviews.llvm.org/D30793 / r300110.
A follow-up fix is under review in https://reviews.llvm.org/D32499.
Comment 10 Vassil Vassilev 2017-05-19 09:57:28 PDT
Second part of the fix landed in r303432.
Comment 11 Tom Stellard 2017-05-19 11:38:34 PDT
Richard, now that the fixes have been merged, are https://reviews.llvm.org/rL300110 and https://reviews.llvm.org/rL303432 OK to merge to the release_40 branch?
Comment 12 Richard Smith 2017-05-19 11:50:21 PDT
r303432 is very new; would you prefer to merge it now and revert it from the branch if people have problems with it on trunk, or wait a few days and merge it if there are no complications? (I'm not anticipating any problems, just cautious about merging a patch that's not really been field-tested.)

I'm fine with merging r300110 now.
Comment 13 Richard Smith 2017-06-05 15:21:50 PDT
*** Bug 33313 has been marked as a duplicate of this bug. ***