@@ -471,7 +471,8 @@ mlir::Value CIRGenModule::getGlobalValue(const Decl *D) {
471
471
mlir::cir::GlobalOp CIRGenModule::createGlobalOp (CIRGenModule &CGM,
472
472
mlir::Location loc,
473
473
StringRef name, mlir::Type t,
474
- bool isCst) {
474
+ bool isCst,
475
+ mlir::Operation *insertPoint) {
475
476
mlir::cir::GlobalOp g;
476
477
auto &builder = CGM.getBuilder ();
477
478
{
@@ -486,8 +487,12 @@ mlir::cir::GlobalOp CIRGenModule::createGlobalOp(CIRGenModule &CGM,
486
487
builder.setInsertionPoint (curCGF->CurFn );
487
488
488
489
g = builder.create <mlir::cir::GlobalOp>(loc, name, t, isCst);
489
- if (!curCGF)
490
- CGM.getModule ().push_back (g);
490
+ if (!curCGF) {
491
+ if (insertPoint)
492
+ CGM.getModule ().insert (insertPoint, g);
493
+ else
494
+ CGM.getModule ().push_back (g);
495
+ }
491
496
492
497
// Default to private until we can judge based on the initializer,
493
498
// since MLIR doesn't allow public declarations.
@@ -501,6 +506,31 @@ void CIRGenModule::setCommonAttributes(GlobalDecl GD, mlir::Operation *GV) {
501
506
assert (!UnimplementedFeature::setCommonAttributes ());
502
507
}
503
508
509
+ void CIRGenModule::replaceGlobal (mlir::cir::GlobalOp Old,
510
+ mlir::cir::GlobalOp New) {
511
+ assert (Old.getSymName () == New.getSymName () && " symbol names must match" );
512
+
513
+ // If the types does not match, update all references to Old to the new type.
514
+ auto OldTy = Old.getSymType ();
515
+ auto NewTy = New.getSymType ();
516
+ if (OldTy != NewTy) {
517
+ auto OldSymUses = Old.getSymbolUses (theModule.getOperation ());
518
+ if (OldSymUses.has_value ()) {
519
+ for (auto Use : *OldSymUses) {
520
+ auto UseOp = dyn_cast<mlir::cir::GetGlobalOp>(Use.getUser ());
521
+ assert (UseOp && " GlobalOp symbol user is not a GetGlobalOp" );
522
+
523
+ auto UseOpResultValue = UseOp.getAddr ();
524
+ UseOpResultValue.setType (
525
+ mlir::cir::PointerType::get (builder.getContext (), NewTy));
526
+ }
527
+ }
528
+ }
529
+
530
+ // Remove old global from the module.
531
+ Old.erase ();
532
+ }
533
+
504
534
// / If the specified mangled name is not in the module,
505
535
// / create and return an mlir GlobalOp with the specified type (TODO(cir):
506
536
// / address space).
@@ -592,11 +622,14 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
592
622
// mlir::SymbolTable::Visibility::Public is the default, no need to explicitly
593
623
// mark it as such.
594
624
auto GV = CIRGenModule::createGlobalOp (*this , loc, MangledName, Ty,
595
- /* isConstant=*/ false );
625
+ /* isConstant=*/ false ,
626
+ /* insertPoint=*/ Entry.getOperation ());
596
627
597
628
// If we already created a global with the same mangled name (but different
598
- // type) before, take its name and remove it from its parent.
599
- assert (!Entry && " not implemented" );
629
+ // type) before, replace it with the new global.
630
+ if (Entry) {
631
+ replaceGlobal (Entry, GV);
632
+ }
600
633
601
634
// This is the first use or definition of a mangled name. If there is a
602
635
// deferred decl with this name, remember that we need to emit it at the end
0 commit comments