@@ -3437,9 +3437,9 @@ IRGenModule::getAddrOfLLVMVariable(LinkEntity entity,
3437
3437
? overrideDeclType
3438
3438
: entity.getDefaultDeclarationType (*this );
3439
3439
3440
- auto &entry = GlobalVars[entity];
3441
- if (entry ) {
3442
- auto existing = cast<llvm::GlobalValue>(entry );
3440
+ auto existingGlobal = GlobalVars[entity];
3441
+ if (existingGlobal ) {
3442
+ auto existing = cast<llvm::GlobalValue>(existingGlobal );
3443
3443
3444
3444
// If we're looking to define something, we may need to replace a
3445
3445
// forward declaration.
@@ -3459,12 +3459,12 @@ IRGenModule::getAddrOfLLVMVariable(LinkEntity entity,
3459
3459
3460
3460
// Fall out to the case below, clearing the name so that
3461
3461
// createVariable doesn't detect a collision.
3462
- entry ->setName (" " );
3462
+ existingGlobal ->setName (" " );
3463
3463
3464
3464
// Otherwise, we have a previous declaration or definition which
3465
3465
// we need to ensure has the right type.
3466
3466
} else {
3467
- return getElementBitCast (entry , defaultType);
3467
+ return getElementBitCast (existingGlobal , defaultType);
3468
3468
}
3469
3469
}
3470
3470
@@ -3508,11 +3508,17 @@ IRGenModule::getAddrOfLLVMVariable(LinkEntity entity,
3508
3508
lazyInitializer->Create (var);
3509
3509
}
3510
3510
3511
+ if (lazyInitializer) {
3512
+ // Protect against self-references that might've been created during
3513
+ // the lazy emission.
3514
+ existingGlobal = GlobalVars[entity];
3515
+ }
3516
+
3511
3517
// If we have an existing entry, destroy it, replacing it with the
3512
- // new variable.
3513
- if (entry ) {
3514
- auto existing = cast<llvm::GlobalValue>(entry );
3515
- auto castVar = llvm::ConstantExpr::getBitCast (var, entry ->getType ());
3518
+ // new variable. We only really have to do
3519
+ if (existingGlobal ) {
3520
+ auto existing = cast<llvm::GlobalValue>(existingGlobal );
3521
+ auto castVar = llvm::ConstantExpr::getBitCast (var, existing ->getType ());
3516
3522
existing->replaceAllUsesWith (castVar);
3517
3523
existing->eraseFromParent ();
3518
3524
}
@@ -3535,7 +3541,7 @@ IRGenModule::getAddrOfLLVMVariable(LinkEntity entity,
3535
3541
}
3536
3542
3537
3543
// Cache and return.
3538
- entry = var;
3544
+ GlobalVars[entity] = var;
3539
3545
return var;
3540
3546
}
3541
3547
0 commit comments