Merge remote-tracking branch 'origin/swift-4.1-branch' into stable
diff --git a/lib/CodeGen/CGObjCMac.cpp b/lib/CodeGen/CGObjCMac.cpp
index 32a7d6a..6f89b75 100644
--- a/lib/CodeGen/CGObjCMac.cpp
+++ b/lib/CodeGen/CGObjCMac.cpp
@@ -5089,6 +5089,11 @@
     fieldType = arrayType->getElementType();
   }
 
+  if (isa<IncompleteArrayType>(fieldType)) {
+    numElts = 0;
+    fieldType = fieldType->getAsArrayTypeUnsafe()->getElementType();
+  }
+
   assert(!fieldType->isArrayType() && "ivar of non-constant array type?");
 
   // If we ended up with a zero-sized array, we've done what we can do within
diff --git a/test/CodeGenObjC/ivar-layout-flexible-array.m b/test/CodeGenObjC/ivar-layout-flexible-array.m
new file mode 100644
index 0000000..2378de2
--- /dev/null
+++ b/test/CodeGenObjC/ivar-layout-flexible-array.m
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -Wno-objc-root-class -fobjc-arc -emit-llvm -o - %s | FileCheck %s
+
+// rdar://problem/21054495
+struct Packet {
+  int size;
+  char data[];
+};
+
+@interface VariableSizeIvar {
+  struct Packet flexible_struct;
+}
+@end
+@implementation VariableSizeIvar
+@end
+// CHECK: @OBJC_METH_VAR_NAME_{{.*}} = private unnamed_addr constant {{.*}} c"flexible_struct\00"
+// CHECK-NEXT: @OBJC_METH_VAR_TYPE_{{.*}} = private unnamed_addr constant {{.*}} c"{Packet=\22size\22i\22data\22[0c]}\00"