Index: lib/AST/RecordLayoutBuilder.cpp
===================================================================
--- lib/AST/RecordLayoutBuilder.cpp	(revision 143335)
+++ lib/AST/RecordLayoutBuilder.cpp	(working copy)
@@ -800,8 +800,11 @@
     const CXXRecordDecl *Base =
       cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
 
-    if (Base->isDynamicClass()) {
+    if (Base->isPolymorphic() ||
+        (Base->getNumVBases() &&
+        Context.getTargetInfo().getCXXABI() != CXXABI_Microsoft)) {
       // We found it.
+      // In MS ABI only base with vfptr can be primary base.
       PrimaryBase = Base;
       PrimaryBaseIsVirtual = false;
       return;
@@ -1003,15 +1006,29 @@
     EnsureVTablePointerAlignment(PtrAlign);
     setSize(getSize() + PtrWidth);
     setDataSize(getSize());
+  } else if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft &&
+             !PrimaryBase && RD->isPolymorphic() && 
+             (RD->getNumVBases() == 0 || HasNewVirtualFunction(RD))) {
+
+    CharUnits PtrWidth = 
+       Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
+
+    VFPtrOffset = getSize();
+    setSize(getSize() + PtrWidth);
+    setDataSize(getSize());
   }
 
+  bool HasDirectVirtualBases = false;
+
   // Now lay out the non-virtual bases.
   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
          E = RD->bases_end(); I != E; ++I) {
 
     // Ignore virtual bases.
-    if (I->isVirtual())
+    if (I->isVirtual()) {
+      HasDirectVirtualBases = true;
       continue;
+    }
 
     const CXXRecordDecl *BaseDecl =
       cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
@@ -1039,14 +1056,7 @@
     CharUnits PtrAlign = 
       Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
     PtrAlign = std::max(PtrAlign, Alignment);
-    if (HasNewVirtualFunction(RD) &&
-        (!PrimaryBase || !BaseHasVFPtr(PrimaryBase))) {
-      EnsureVTablePointerAlignment(PtrAlign);
-      VFPtrOffset = getSize();
-      setSize(getSize() + PtrWidth);
-      setDataSize(getSize());
-    }
-    if (RD->getNumVBases() &&
+    if (HasDirectVirtualBases &&
         (!PrimaryBase || !PrimaryBase->getNumVBases())) {
       EnsureVTablePointerAlignment(PtrAlign);
       VBPtrOffset = getSize();
Index: lib/CodeGen/CGRecordLayoutBuilder.cpp
===================================================================
--- lib/CodeGen/CGRecordLayoutBuilder.cpp	(revision 143335)
+++ lib/CodeGen/CGRecordLayoutBuilder.cpp	(working copy)
@@ -685,6 +685,15 @@
              "VTable pointer must come first!");
       AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
       
+    } else if (Layout.getVFPtrOffset() != CharUnits::fromQuantity(-1)) {
+        llvm::Type *FunctionType =
+          llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
+          /*isVarArg=*/true);
+        llvm::Type *VTableTy = FunctionType->getPointerTo();
+
+        assert(NextFieldOffset.isZero() &&
+          "VTable pointer must come first!");
+        AppendField(Layout.getVFPtrOffset(), VTableTy->getPointerTo());
     }
   }
 
@@ -703,6 +712,15 @@
 
     LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
   }
+
+  if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) {
+    CharUnits VBPtrOffset = Layout.getVBPtrOffset();
+    if (NextFieldOffset < VBPtrOffset) {
+      AppendBytes(VBPtrOffset - NextFieldOffset);
+    }
+    llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
+    AppendField(VBPtrOffset, Vbptr);
+  }
 }
 
 bool
@@ -787,10 +805,13 @@
       return false;
     }
 
-    // And lay out the virtual bases.
-    RD->getIndirectPrimaryBases(IndirectPrimaryBases);
-    if (Layout.isPrimaryBaseVirtual())
-      IndirectPrimaryBases.insert(Layout.getPrimaryBase());
+    if (Types.getContext().getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
+      // And lay out the virtual bases.
+      // In MS ABI we don't have primary virtual bases.
+      RD->getIndirectPrimaryBases(IndirectPrimaryBases);
+      if (Layout.isPrimaryBaseVirtual())
+        IndirectPrimaryBases.insert(Layout.getPrimaryBase());
+    }
     LayoutVirtualBases(RD, Layout);
   }
   
@@ -837,7 +858,8 @@
   CharUnits alignedNextFieldOffset =
     NextFieldOffset.RoundUpToAlignment(fieldAlignment);
 
-  if (alignedNextFieldOffset < fieldOffset) {
+  if (alignedNextFieldOffset < fieldOffset ||
+      (!Packed && (fieldOffset != NextFieldOffset))) {
     // Even with alignment, the field offset is not at the right place,
     // insert padding.
     CharUnits padding = fieldOffset - NextFieldOffset;
Index: test/Sema/ms_class_layout.cpp
===================================================================
--- test/Sema/ms_class_layout.cpp	(revision 143335)
+++ test/Sema/ms_class_layout.cpp	(working copy)
@@ -67,6 +67,26 @@
   double q;
 };
 
+struct K
+{
+  int k;
+};
+
+struct L
+{
+  int l;
+};
+
+struct M : public virtual K
+{
+  int m;
+};
+
+struct N : public L, public M
+{
+  virtual void f(){}
+};
+
 #pragma pack(pop)
 
 // This needs only for building layouts. 
@@ -79,6 +99,7 @@
   H* g;
   BaseStruct* u;
   I* i;
+  N* n;
   return 0;
 }
 
@@ -89,7 +110,7 @@
 // CHECK-NEXT: sizeof=16, dsize=16, align=8
 // CHECK-NEXT: nvsize=16, nvalign=8
 
-// CHECK: %class.D = type { [8 x i8], double }
+// CHECK: %class.D = type { i32 (...)**, [4 x i8], double }
 
 // CHECK:       0 | class B
 // CHECK-NEXT:  0 |   (B vftable pointer)
@@ -98,7 +119,7 @@
 // CHECK-NEXT: sizeof=8, dsize=8, align=4
 // CHECK-NEXT: nvsize=8, nvalign=4
 
-// CHECK: %class.B = type { [4 x i8], i32 }
+// CHECK: %class.B = type { i32 (...)**, i32 }
 
 // CHECK:       0 | class A
 // CHECK-NEXT:  0 |   class B (primary base)
@@ -134,8 +155,8 @@
 
 // CHECK: %class.A = type { %class.B, i32, i8 }
 
-// CHECK: %class.C = type { %class.D, %class.B, [8 x i8], double, i32, double, i32, [4 x i8], %class.A }
-// CHECK: %class.C.base = type { %class.D, %class.B, [8 x i8], double, i32, double, i32 }
+// CHECK: %class.C = type { %class.D, %class.B, i32*, [4 x i8], double, i32, [4 x i8], double, i32, [4 x i8], %class.A }
+// CHECK: %class.C.base = type { %class.D, %class.B, i32*, [4 x i8], double, i32, [4 x i8], double, i32 }
 
 // CHECK:       0 | struct BaseStruct
 // CHECK-NEXT:  0 |   double v0
@@ -165,7 +186,7 @@
 // CHECK: sizeof=96, dsize=96, align=8
 // CHECK-NEXT: nvsize=96, nvalign=8
 
-// CHECK: %struct.BaseStruct = type { double, float, %class.C }
+// CHECK: %struct.BaseStruct = type { double, float, [4 x i8], %class.C }
 
 // CHECK:       0 | struct DerivedStruct
 // CHECK-NEXT:  0 |   struct BaseStruct (base)
@@ -213,7 +234,7 @@
 // CHECK-NEXT: sizeof=24, dsize=24, align=8
 // CHECK-NEXT: nvsize=8, nvalign=4
 
-// CHECK: %struct.H = type { %struct.G, [4 x i8], %class.D }
+// CHECK: %struct.H = type { %struct.G, i32*, %class.D }
 
 // CHECK:       0 | struct I
 // CHECK-NEXT:  0 |   (I vftable pointer)
@@ -225,5 +246,39 @@
 // CHECK-NEXT: sizeof=40, dsize=40, align=8
 // CHECK-NEXT: nvsize=24, nvalign=8
 
-// CHECK: %struct.I = type { [16 x i8], double, %class.D }
-// CHECK: %struct.I.base = type { [16 x i8], double }
+// CHECK: %struct.I = type { i32 (...)**, [4 x i8], i32*, [4 x i8], double, %class.D }
+// CHECK: %struct.I.base = type { i32 (...)**, [4 x i8], i32*, [4 x i8], double }
+
+// CHECK:       0 | struct L
+// CHECK-NEXT:  0 |   int l
+// CHECK-NEXT: sizeof=4, dsize=4, align=4
+// CHECK-NEXT: nvsize=4, nvalign=4
+
+// CHECK:       0 | struct K
+// CHECK-NEXT:  0 |   int k
+// CHECK-NEXT: sizeof=4, dsize=4, align=4
+// CHECK-NEXT: nvsize=4, nvalign=4
+
+// CHECK:       0 | struct M
+// CHECK-NEXT:  0 |   (M vbtable pointer)
+// CHECK-NEXT:  4 |   int m
+// CHECK-NEXT:  8 |   struct K (virtual base)
+// CHECK-NEXT:  8 |     int k
+// CHECK-NEXT: sizeof=12, dsize=12, align=4
+
+//CHECK: %struct.M = type { i32*, i32, %struct.K }
+//CHECK: %struct.M.base = type { i32*, i32 }
+
+// CHECK:       0 | struct N
+// CHECK-NEXT:  4 |   struct L (base)
+// CHECK-NEXT:  4 |     int l
+// CHECK-NEXT:  8 |   struct M (base)
+// CHECK-NEXT:  8 |     (M vbtable pointer)
+// CHECK-NEXT: 12 |     int m
+// CHECK-NEXT:  0 |   (N vftable pointer)
+// CHECK-NEXT: 16 |   struct K (virtual base)
+// CHECK-NEXT: 16 |     int k
+// CHECK-NEXT: sizeof=20, dsize=20, align=4
+// CHECK-NEXT: nvsize=16, nvalign=4
+
+//CHECK: %struct.N = type { i32 (...)**, %struct.L, %struct.M.base, %struct.K }
