@@ -3509,9 +3509,9 @@ IRGenModule::getAddrOfLLVMVariable(LinkEntity entity,
3509
3509
? overrideDeclType
3510
3510
: entity.getDefaultDeclarationType (*this );
3511
3511
3512
- auto &entry = GlobalVars[entity];
3513
- if (entry ) {
3514
- auto existing = cast<llvm::GlobalValue>(entry );
3512
+ auto existingGlobal = GlobalVars[entity];
3513
+ if (existingGlobal ) {
3514
+ auto existing = cast<llvm::GlobalValue>(existingGlobal );
3515
3515
3516
3516
// If we're looking to define something, we may need to replace a
3517
3517
// forward declaration.
@@ -3531,12 +3531,12 @@ IRGenModule::getAddrOfLLVMVariable(LinkEntity entity,
3531
3531
3532
3532
// Fall out to the case below, clearing the name so that
3533
3533
// createVariable doesn't detect a collision.
3534
- entry ->setName (" " );
3534
+ existingGlobal ->setName (" " );
3535
3535
3536
3536
// Otherwise, we have a previous declaration or definition which
3537
3537
// we need to ensure has the right type.
3538
3538
} else {
3539
- return getElementBitCast (entry , defaultType);
3539
+ return getElementBitCast (existingGlobal , defaultType);
3540
3540
}
3541
3541
}
3542
3542
@@ -3580,11 +3580,17 @@ IRGenModule::getAddrOfLLVMVariable(LinkEntity entity,
3580
3580
lazyInitializer->Create (var);
3581
3581
}
3582
3582
3583
+ if (lazyInitializer) {
3584
+ // Protect against self-references that might've been created during
3585
+ // the lazy emission.
3586
+ existingGlobal = GlobalVars[entity];
3587
+ }
3588
+
3583
3589
// If we have an existing entry, destroy it, replacing it with the
3584
- // new variable.
3585
- if (entry ) {
3586
- auto existing = cast<llvm::GlobalValue>(entry );
3587
- auto castVar = llvm::ConstantExpr::getBitCast (var, entry ->getType ());
3590
+ // new variable. We only really have to do
3591
+ if (existingGlobal ) {
3592
+ auto existing = cast<llvm::GlobalValue>(existingGlobal );
3593
+ auto castVar = llvm::ConstantExpr::getBitCast (var, existing ->getType ());
3588
3594
existing->replaceAllUsesWith (castVar);
3589
3595
existing->eraseFromParent ();
3590
3596
}
@@ -3607,7 +3613,7 @@ IRGenModule::getAddrOfLLVMVariable(LinkEntity entity,
3607
3613
}
3608
3614
3609
3615
// Cache and return.
3610
- entry = var;
3616
+ GlobalVars[entity] = var;
3611
3617
return var;
3612
3618
}
3613
3619
0 commit comments