@@ -618,70 +618,75 @@ Address CIRGenModule::createUnnamedGlobalFrom(const VarDecl &D,
618618// / Add the initializer for 'D' to the global variable that has already been
619619// / created for it. If the initializer has a different type than GV does, this
620620// / may free GV and return a different one. Otherwise it just returns GV.
621- cir::GlobalOp CIRGenFunction::addInitializerToStaticVarDecl (
622- const VarDecl &D, cir::GlobalOp GV, cir::GetGlobalOp GVAddr) {
621+ cir::GlobalOp
622+ CIRGenFunction::addInitializerToStaticVarDecl (const VarDecl &varDecl,
623+ cir::GlobalOp globalOp,
624+ cir::GetGlobalOp getGlobalOp) {
623625 ConstantEmitter emitter (*this );
624- mlir::TypedAttr Init =
625- mlir::dyn_cast_or_null<mlir::TypedAttr>( emitter.tryEmitForInitializer (D ));
626+ mlir::TypedAttr typedInit = mlir::dyn_cast_or_null<mlir::TypedAttr>(
627+ emitter.tryEmitForInitializer (varDecl ));
626628
627629 // If constant emission failed, then this should be a C++ static
628630 // initializer.
629- if (!Init ) {
631+ if (!typedInit ) {
630632 if (!getLangOpts ().CPlusPlus )
631- CGM.ErrorUnsupported (D .getInit (), " constant l-value expression" );
632- else if (D .hasFlexibleArrayInit (getContext ()))
633- CGM.ErrorUnsupported (D .getInit (), " flexible array initializer" );
633+ CGM.ErrorUnsupported (varDecl .getInit (), " constant l-value expression" );
634+ else if (varDecl .hasFlexibleArrayInit (getContext ()))
635+ CGM.ErrorUnsupported (varDecl .getInit (), " flexible array initializer" );
634636 else {
635637 // Since we have a static initializer, this global variable can't
636638 // be constant.
637- GV.setConstant (false );
638639 llvm_unreachable (" C++ guarded init it NYI" );
640+ globalOp.setConstant (false );
639641 }
640- return GV ;
642+ return globalOp ;
641643 }
642644
643645#ifndef NDEBUG
644- CharUnits VarSize = CGM.getASTContext ().getTypeSizeInChars (D.getType ()) +
645- D.getFlexibleArrayInitChars (getContext ());
646- CharUnits CstSize = CharUnits::fromQuantity (
647- CGM.getDataLayout ().getTypeAllocSize (Init.getType ()));
648- assert (VarSize == CstSize && " Emitted constant has unexpected size" );
646+ CharUnits varSize =
647+ CGM.getASTContext ().getTypeSizeInChars (varDecl.getType ()) +
648+ varDecl.getFlexibleArrayInitChars (getContext ());
649+ CharUnits cstSize = CharUnits::fromQuantity (
650+ CGM.getDataLayout ().getTypeAllocSize (typedInit.getType ()));
651+ assert (varSize == cstSize && " Emitted constant has unexpected size" );
649652#endif
650653
651654 // The initializer may differ in type from the global. Rewrite
652655 // the global to match the initializer. (We have to do this
653656 // because some types, like unions, can't be completely represented
654657 // in the LLVM type system.)
655- if (GV.getSymType () != Init.getType ()) {
656- GV.setSymType (Init.getType ());
658+ // NOTE(CIR): This was removed in OG since opaque pointers made it trivial. We
659+ // need it since we still have typed pointers.
660+ if (globalOp.getSymType () != typedInit.getType ()) {
661+ globalOp.setSymType (typedInit.getType ());
657662
658663 // Normally this should be done with a call to CGM.replaceGlobal(OldGV, GV),
659664 // but since at this point the current block hasn't been really attached,
660665 // there's no visibility into the GetGlobalOp corresponding to this Global.
661666 // Given those constraints, thread in the GetGlobalOp and update it
662667 // directly.
663- GVAddr .getAddr ().setType (
664- getBuilder (). getPointerTo (Init. getType (), GV .getAddrSpaceAttr ()));
668+ getGlobalOp .getAddr ().setType ( getBuilder (). getPointerTo (
669+ typedInit. getType (), globalOp .getAddrSpaceAttr ()));
665670 }
666671
667- bool NeedsDtor =
668- D .needsDestruction (getContext ()) == QualType::DK_cxx_destructor;
672+ bool needsDtor =
673+ varDecl .needsDestruction (getContext ()) == QualType::DK_cxx_destructor;
669674
670- GV .setConstant (D .getType ().isConstantStorage (
671- getContext (), /* ExcludeCtor=*/ true , !NeedsDtor ));
675+ globalOp .setConstant (varDecl .getType ().isConstantStorage (
676+ getContext (), /* ExcludeCtor=*/ true , !needsDtor ));
672677
673- GV .setInitialValueAttr (Init );
678+ globalOp .setInitialValueAttr (typedInit );
674679
675- emitter.finalize (GV );
680+ emitter.finalize (globalOp );
676681
677- if (NeedsDtor ) {
682+ if (needsDtor ) {
678683 // We have a constant initializer, but a nontrivial destructor. We still
679684 // need to perform a guarded "initialization" in order to register the
680685 // destructor.
681686 llvm_unreachable (" C++ guarded init is NYI" );
682687 }
683688
684- return GV ;
689+ return globalOp ;
685690}
686691
687692void CIRGenFunction::emitStaticVarDecl (const VarDecl &D,
0 commit comments