@@ -317,6 +317,36 @@ cir::GlobalOp CIRGenModule::createGlobalOp(CIRGenModule &cgm,
317317 return g;
318318}
319319
320+ void CIRGenModule::setCommonAttributes (GlobalDecl gd, mlir::Operation *gv) {
321+ const Decl *d = gd.getDecl ();
322+ if (isa_and_nonnull<NamedDecl>(d))
323+ setGVProperties (gv, dyn_cast<NamedDecl>(d));
324+ assert (!cir::MissingFeatures::defaultVisibility ());
325+ assert (!cir::MissingFeatures::opGlobalUsedOrCompilerUsed ());
326+ }
327+
328+ void CIRGenModule::setNonAliasAttributes (GlobalDecl gd, mlir::Operation *op) {
329+ setCommonAttributes (gd, op);
330+
331+ assert (!cir::MissingFeatures::opGlobalUsedOrCompilerUsed ());
332+ assert (!cir::MissingFeatures::opGlobalSection ());
333+ assert (!cir::MissingFeatures::opFuncCPUAndFeaturesAttributes ());
334+ assert (!cir::MissingFeatures::opFuncSection ());
335+
336+ assert (!cir::MissingFeatures::setTargetAttributes ());
337+ }
338+
339+ static void setLinkageForGV (cir::GlobalOp &gv, const NamedDecl *nd) {
340+ // Set linkage and visibility in case we never see a definition.
341+ LinkageInfo lv = nd->getLinkageAndVisibility ();
342+ // Don't set internal linkage on declarations.
343+ // "extern_weak" is overloaded in LLVM; we probably should have
344+ // separate linkage types for this.
345+ if (isExternallyVisible (lv.getLinkage ()) &&
346+ (nd->hasAttr <WeakAttr>() || nd->isWeakImported ()))
347+ gv.setLinkage (cir::GlobalLinkageKind::ExternalWeakLinkage);
348+ }
349+
320350// / If the specified mangled name is not in the module,
321351// / create and return an mlir GlobalOp with the specified type (TODO(cir):
322352// / address space).
@@ -387,7 +417,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
387417
388418 gv.setAlignmentAttr (getSize (astContext.getDeclAlign (d)));
389419 assert (!cir::MissingFeatures::opGlobalConstant ());
390- assert (!cir::MissingFeatures::opGlobalLinkage ());
420+
421+ setLinkageForGV (gv, d);
391422
392423 if (d->getTLSKind ())
393424 errorNYI (d->getSourceRange (), " thread local global variable" );
@@ -555,8 +586,6 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
555586 errorNYI (vd->getSourceRange (), " annotate global variable" );
556587 }
557588
558- assert (!cir::MissingFeatures::opGlobalLinkage ());
559-
560589 if (langOpts.CUDA ) {
561590 errorNYI (vd->getSourceRange (), " CUDA global variable" );
562591 }
@@ -577,6 +606,12 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
577606 assert (!cir::MissingFeatures::opGlobalDLLImportExport ());
578607 if (linkage == cir::GlobalLinkageKind::CommonLinkage)
579608 errorNYI (initExpr->getSourceRange (), " common linkage" );
609+
610+ setNonAliasAttributes (vd, gv);
611+
612+ assert (!cir::MissingFeatures::opGlobalThreadLocal ());
613+
614+ maybeSetTrivialComdat (*vd, gv);
580615}
581616
582617void CIRGenModule::emitGlobalDefinition (clang::GlobalDecl gd,
@@ -668,6 +703,15 @@ static bool shouldBeInCOMDAT(CIRGenModule &cgm, const Decl &d) {
668703 llvm_unreachable (" No such linkage" );
669704}
670705
706+ void CIRGenModule::maybeSetTrivialComdat (const Decl &d, mlir::Operation *op) {
707+ if (!shouldBeInCOMDAT (*this , d))
708+ return ;
709+ if (auto globalOp = dyn_cast_or_null<cir::GlobalOp>(op))
710+ globalOp.setComdat (true );
711+
712+ assert (!cir::MissingFeatures::opFuncSetComdat ());
713+ }
714+
671715// TODO(CIR): this could be a common method between LLVM codegen.
672716static bool isVarDeclStrongDefinition (const ASTContext &astContext,
673717 CIRGenModule &cgm, const VarDecl *vd,
@@ -830,10 +874,10 @@ CIRGenModule::getCIRLinkageVarDefinition(const VarDecl *vd, bool isConstant) {
830874 return getCIRLinkageForDeclarator (vd, linkage, isConstant);
831875}
832876
833- static cir::GlobalOp generateStringLiteral (mlir::Location loc,
834- mlir::TypedAttr c, CIRGenModule &cgm ,
835- StringRef globalName ,
836- CharUnits alignment) {
877+ static cir::GlobalOp
878+ generateStringLiteral (mlir::Location loc, mlir::TypedAttr c,
879+ cir::GlobalLinkageKind lt, CIRGenModule &cgm ,
880+ StringRef globalName, CharUnits alignment) {
837881 assert (!cir::MissingFeatures::addressSpace ());
838882
839883 // Create a global variable for this string
@@ -843,7 +887,8 @@ static cir::GlobalOp generateStringLiteral(mlir::Location loc,
843887
844888 // Set up extra information and add to the module
845889 gv.setAlignmentAttr (cgm.getSize (alignment));
846- assert (!cir::MissingFeatures::opGlobalLinkage ());
890+ gv.setLinkageAttr (
891+ cir::GlobalLinkageKindAttr::get (cgm.getBuilder ().getContext (), lt));
847892 assert (!cir::MissingFeatures::opGlobalThreadLocal ());
848893 assert (!cir::MissingFeatures::opGlobalUnnamedAddr ());
849894 CIRGenModule::setInitializer (gv, c);
@@ -907,7 +952,8 @@ cir::GlobalOp CIRGenModule::getGlobalForStringLiteral(const StringLiteral *s,
907952 mlir::Location loc = getLoc (s->getSourceRange ());
908953 auto typedC = llvm::cast<mlir::TypedAttr>(c);
909954 cir::GlobalOp gv =
910- generateStringLiteral (loc, typedC, *this , uniqueName, alignment);
955+ generateStringLiteral (loc, typedC, cir::GlobalLinkageKind::PrivateLinkage,
956+ *this , uniqueName, alignment);
911957 setDSOLocal (static_cast <mlir::Operation *>(gv));
912958
913959 assert (!cir::MissingFeatures::sanitizers ());
0 commit comments