diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -2189,7 +2189,12 @@ llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); std::string Name = getName({Prefix, "var"}); - return getOrCreateInternalVariable(KmpCriticalNameTy, Name); + llvm::GlobalVariable *GV = cast( + getOrCreateInternalVariable(KmpCriticalNameTy, Name)); + bool IsPIE = GV->getParent()->getPIELevel() != llvm::PIELevel::Default; + bool IsPIC = GV->getParent()->getPICLevel() != llvm::PICLevel::NotPIC; + GV->setDSOLocal(!IsPIC || IsPIE); + return cast(GV); } namespace { diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -208,6 +208,13 @@ ModuleNameHash = (Twine(".__uniq.") + Twine(toString(IntHash, /* Radix = */ 10, /* Signed = */false))).str(); } + + if (uint32_t PLevel = Context.getLangOpts().PICLevel) { + assert(PLevel < 3 && "Invalid PIC Level"); + getModule().setPICLevel(static_cast(PLevel)); + if (Context.getLangOpts().PIE) + getModule().setPIELevel(static_cast(PLevel)); + } } CodeGenModule::~CodeGenModule() {} @@ -745,13 +752,6 @@ } } - if (uint32_t PLevel = Context.getLangOpts().PICLevel) { - assert(PLevel < 3 && "Invalid PIC Level"); - getModule().setPICLevel(static_cast(PLevel)); - if (Context.getLangOpts().PIE) - getModule().setPIELevel(static_cast(PLevel)); - } - if (getCodeGenOpts().CodeModel.size() > 0) { unsigned CM = llvm::StringSwitch(getCodeGenOpts().CodeModel) .Case("tiny", llvm::CodeModel::Tiny) diff --git a/clang/test/OpenMP/critical_codegen.cpp b/clang/test/OpenMP/critical_codegen.cpp --- a/clang/test/OpenMP/critical_codegen.cpp +++ b/clang/test/OpenMP/critical_codegen.cpp @@ -16,9 +16,9 @@ #define HEADER // ALL: [[IDENT_T_TY:%.+]] = type { i32, i32, i32, i32, i8* } -// ALL: [[UNNAMED_LOCK:@.+]] = common global [8 x i32] zeroinitializer -// ALL: [[THE_NAME_LOCK:@.+]] = common global [8 x i32] zeroinitializer -// ALL: [[THE_NAME_LOCK1:@.+]] = common global [8 x i32] zeroinitializer +// ALL: [[UNNAMED_LOCK:@.+]] = common dso_local global [8 x i32] zeroinitializer +// ALL: [[THE_NAME_LOCK:@.+]] = common dso_local global [8 x i32] zeroinitializer +// ALL: [[THE_NAME_LOCK1:@.+]] = common dso_local global [8 x i32] zeroinitializer // ALL: define {{.*}}void [[FOO:@.+]]() diff --git a/clang/test/OpenMP/critical_codegen_attr.cpp b/clang/test/OpenMP/critical_codegen_attr.cpp --- a/clang/test/OpenMP/critical_codegen_attr.cpp +++ b/clang/test/OpenMP/critical_codegen_attr.cpp @@ -16,9 +16,9 @@ #define HEADER // ALL: [[IDENT_T_TY:%.+]] = type { i32, i32, i32, i32, i8* } -// ALL: [[UNNAMED_LOCK:@.+]] = common global [8 x i32] zeroinitializer -// ALL: [[THE_NAME_LOCK:@.+]] = common global [8 x i32] zeroinitializer -// ALL: [[THE_NAME_LOCK1:@.+]] = common global [8 x i32] zeroinitializer +// ALL: [[UNNAMED_LOCK:@.+]] = common dso_local global [8 x i32] zeroinitializer +// ALL: [[THE_NAME_LOCK:@.+]] = common dso_local global [8 x i32] zeroinitializer +// ALL: [[THE_NAME_LOCK1:@.+]] = common dso_local global [8 x i32] zeroinitializer // ALL: define {{.*}}void [[FOO:@.+]]() diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -2487,7 +2487,13 @@ Value *OpenMPIRBuilder::getOMPCriticalRegionLock(StringRef CriticalName) { std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); std::string Name = getNameWithSeparators({Prefix, "var"}, ".", "."); - return getOrCreateOMPInternalVariable(KmpCriticalNameTy, Name); + llvm::GlobalVariable *GV = cast( + getOrCreateOMPInternalVariable(KmpCriticalNameTy, Name)); + bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default; + bool IsPIC = GV->getParent()->getPICLevel() != PICLevel::NotPIC; + GV->setDSOLocal(!IsPIC || IsPIE); + + return cast(GV); } GlobalVariable *