@@ -870,7 +870,15 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
870
870
}
871
871
872
872
GV->setInitializer (Init);
873
- GV->setConstant (D->getType ().isConstant (Context));
873
+
874
+ // If it is safe to mark the global 'constant', do so now.
875
+ GV->setConstant (false );
876
+ if (D->getType ().isConstant (Context)) {
877
+ // FIXME: In C++, if the variable has a non-trivial ctor/dtor or any mutable
878
+ // members, it cannot be declared "LLVM const".
879
+ GV->setConstant (true );
880
+ }
881
+
874
882
GV->setAlignment (getContext ().getDeclAlignInBytes (D));
875
883
876
884
// Set the llvm linkage type as appropriate.
@@ -880,13 +888,18 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
880
888
GV->setLinkage (llvm::Function::DLLImportLinkage);
881
889
else if (D->hasAttr <DLLExportAttr>())
882
890
GV->setLinkage (llvm::Function::DLLExportLinkage);
883
- else if (D->hasAttr <WeakAttr>())
884
- GV->setLinkage (llvm::GlobalVariable::WeakAnyLinkage);
885
- else if (!CompileOpts.NoCommon &&
891
+ else if (D->hasAttr <WeakAttr>()) {
892
+ if (GV->isConstant ())
893
+ GV->setLinkage (llvm::GlobalVariable::WeakODRLinkage);
894
+ else
895
+ GV->setLinkage (llvm::GlobalVariable::WeakAnyLinkage);
896
+ } else if (!CompileOpts.NoCommon &&
886
897
!D->hasExternalStorage () && !D->getInit () &&
887
- !D->getAttr <SectionAttr>())
898
+ !D->getAttr <SectionAttr>()) {
888
899
GV->setLinkage (llvm::GlobalVariable::CommonLinkage);
889
- else
900
+ // common vars aren't constant even if declared const.
901
+ GV->setConstant (false );
902
+ } else
890
903
GV->setLinkage (llvm::GlobalVariable::ExternalLinkage);
891
904
892
905
SetCommonAttributes (D, GV);
0 commit comments