It seems my previous changes uncovered an issue:
See build log here: http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast/builds/41191/steps/check-llvm%20msan/logs/stdio
And here: http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-bootstrap/builds/17119/steps/check-llvm%20asan/logs/stdio
What happens is that emitDebugInfoForGlobal calls getFullyQualifiedName, which in turn calls CodeViewDebug::collectParentScopeNames and then, DeferredCompleteTypes.push_back(Ty);
This leaves deferred types in the vector, but they are only 'applied' later, inadvertently by emitDebugInfoForUDTs while calling getCompleteTypeIndex which ends up destroying a TypeLoweringScope which applies the deferred types, which in turn creates new UDT, which resizes the vector being iterated in the first place in emitDebugInfoForUDTs.
I've added an extra TypeLoweringScope in getFullyQualifiedName() below, which should only fire if there are no other TypeLoweringScope in the stack (which is the case for emitDebugInfoForGlobal.
The actual change is this line.