diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h
index 76c05852f721344ce3891ee917d4159687a450ba..76fda1c09156b8c25a4a361bd7e45eeccf343de4 100644
--- a/include/clang/AST/Decl.h
+++ b/include/clang/AST/Decl.h
@@ -2499,7 +2499,8 @@ public:
 /// IndirectFieldDecl - An instance of this class is created to represent a
 /// field injected from an anonymous union/struct into the parent scope.
 /// IndirectFieldDecl are always implicit.
-class IndirectFieldDecl : public ValueDecl {
+class IndirectFieldDecl : public ValueDecl,
+                          public Mergeable<IndirectFieldDecl> {
   void anchor() override;
   NamedDecl **Chaining;
   unsigned ChainingSize;
@@ -2537,6 +2538,9 @@ public:
     return dyn_cast<VarDecl>(*chain_begin());
   }
 
+  IndirectFieldDecl *getCanonicalDecl() override { return getFirstDecl(); }
+  const IndirectFieldDecl *getCanonicalDecl() const { return getFirstDecl(); }
+
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   static bool classofKind(Kind K) { return K == IndirectField; }
diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp
index 45a3e45bbcf7e8b2fbe4388af328d67519230901..0242e7eed07f5824e11074d91874847a1d099a9c 100644
--- a/lib/Serialization/ASTReaderDecl.cpp
+++ b/lib/Serialization/ASTReaderDecl.cpp
@@ -1178,6 +1178,8 @@ void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
 
   for (unsigned I = 0; I != FD->ChainingSize; ++I)
     FD->Chaining[I] = ReadDeclAs<NamedDecl>(Record, Idx);
+
+  mergeMergeable(FD);
 }
 
 ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
@@ -2638,6 +2640,13 @@ static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
     return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
   }
 
+  // Indirect fields with the same target field match.
+  if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
+    auto *IFDY = cast<IndirectFieldDecl>(Y);
+    return IFDX->getAnonField()->getCanonicalDecl() ==
+           IFDY->getAnonField()->getCanonicalDecl();
+  }
+
   // Enumerators with the same name match.
   if (isa<EnumConstantDecl>(X))
     // FIXME: Also check the value is odr-equivalent.
diff --git a/test/Modules/Inputs/submodules-merge-defs/defs.h b/test/Modules/Inputs/submodules-merge-defs/defs.h
index e4c86d80ba6fe466be5ac5f0c8a1fa1754176583..670c400ae3b266b63abc9ac3ca9f2663fe903056 100644
--- a/test/Modules/Inputs/submodules-merge-defs/defs.h
+++ b/test/Modules/Inputs/submodules-merge-defs/defs.h
@@ -103,3 +103,11 @@ namespace RedeclDifferentDeclKind {
   typedef X X;
   using RedeclDifferentDeclKind::X;
 }
+
+namespace Anon {
+  struct X {
+    union {
+      int n;
+    };
+  };
+}
diff --git a/test/Modules/submodules-merge-defs.cpp b/test/Modules/submodules-merge-defs.cpp
index 016b8a8f47a71cdbc8313c4ad437559a64456894..62cbbd51098696c65a9b717fa9d8b1cbc32bf668 100644
--- a/test/Modules/submodules-merge-defs.cpp
+++ b/test/Modules/submodules-merge-defs.cpp
@@ -104,6 +104,7 @@ template<typename T, int N, template<typename> class K> struct J;
 J<> post_j2;
 FriendDefArg::Y<int> friend_def_arg;
 FriendDefArg::D<> friend_def_arg_d;
+int post_anon_x_n = Anon::X().n;
 
 MergeFunctionTemplateSpecializations::X<int>::Q<char> xiqc;