[cfe-dev] Different class layouts between clang and MSVC

Don Williamson don.williamson at yahoo.com
Mon Sep 5 08:50:47 PDT 2011


One other failure has turned up in my testing:

struct G
{
virtual ~G() { }
int a;
// double b;
};

As it stands, the offset of a is 4 with both compilers. When any double-parameters are introduced (uncomment b above), MSVC places a at 8 and clang keeps it at 4 - MSVC is padding up from the vtable ptr to a. It obviously then has to shift b up and results in an object that's 8 bytes larger.

Is there any use/interest in having clang and MSVC match up? Obviously it's of interest to me, but it'd be nice to know the bigger picture :)

Thanks,
- Don


----- Original Message -----
From: Don Williamson <don.williamson at yahoo.com>
To: "cfe-dev at cs.uiuc.edu" <cfe-dev at cs.uiuc.edu>
Cc: 
Sent: Monday, September 5, 2011 1:24 PM
Subject: Re: [cfe-dev] Different class layouts between clang and MSVC

Some further points:

* LangOptions::RTTI = 0
* LangOptions::Microsoft = 1
* TargetOptions::CXXABI = "microsoft"

After stepping it appears the problem is related to the NonVirtualSize field of CXXRecordLayoutInfo in ASTRecordLayout:

Without constructor: 16
With constructor: 12

This is calculated in ASTContext::getASTRecordLayout with the following lines:

    // FIXME: This should be done in FinalizeLayout.
    CharUnits DataSize =
      IsPODForThePurposeOfLayout ? Builder->getSize() : Builder->getDataSize();
    CharUnits NonVirtualSize = 
      IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize;

The inputs are:

Without constructor, DataSize: 16, Builder->NonVirtualSize: 12, IsPODForThePurposeOfLayout: true
With constructor, DataSize: 12, Builder->NonVirtualSize: 12, IsPODForThePurposeOfLayout: false

In both cases, Builder Size and DataSize are 128 and 96, respectively, so it looks like the difference is triggered by IsPODForThePurposeOfLayout.

The change that modified the code is: http://llvm.org/viewvc/llvm-project?view=rev&revision=77352


>From my simple understanding of this, Size is different to DataSize (in this limited test) due to being aligned upwards to the alignment of the double field in FinishLayout. What's the reasoning behind making POD types use an aligned value vs. an unaligned value for NonVirtualSize? Can this behaviour be changed based on ABI?

Thanks,
- Don

----- Original Message -----
From: Don Williamson <don.williamson at yahoo.com>
To: "cfe-dev at cs.uiuc.edu" <cfe-dev at cs.uiuc.edu>
Cc: 
Sent: Monday, September 5, 2011 11:23 AM
Subject: [cfe-dev] Different class layouts between clang and MSVC

Hi,

I am synced to 134398 as of 2011-07-05, 00:09:02+0100.

I'm using the clang frontend to calculate class layouts which I'm hoping are identical to MSVC. Before I go any further, I suppose the first questions are:

* Is the goal of MSRecordLayoutBuilder to be as identical to MSVC layout as is knowable?
* Has that goal been achieved or are their known defects?

My test case is the following code:


   #pragma pack(push, 8)
   struct BaseStruct
   {
      // BaseStruct() { }
      double v0;
      float v1;
   };

   struct DerivedStruct : public BaseStruct
   {
      int x;
   };
   #pragma pack(pop)

With the constructor above commented out, I get the following:

sizeof(BaseStruct):         16
clang size:                 16
sizeof(DerivedStruct):      24
clang size:                 24
offsetof(DerivedStruct::x): 16
clang offset:               16

Which all matches up. However, with the constructor restored, I get:

sizeof(BaseStruct):         16
clang size:                 16
sizeof(DerivedStruct):      24
clang size:                 16
offsetof(DerivedStruct::x): 16
clang offset:               12

clang seems to be changing its behaviour based on the addition of the constructor. I'm currently tracing through the code trying to figure out how it all works in the hope of diagnosing the issue. Does anybody have any clues as to what's going on that could help me?

Thanks,
- Don

_______________________________________________
cfe-dev mailing list
cfe-dev at cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
cfe-dev at cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev





More information about the cfe-dev mailing list