Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp =================================================================== --- clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -2161,11 +2161,7 @@ llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { std::string Prefix = Twine("gomp_critical_user_", CriticalName).str(); std::string Name = getName({Prefix, "var"}); - llvm::GlobalVariable *G = OMPBuilder.getOrCreateInternalVariable(KmpCriticalNameTy, Name); - llvm::Align PtrAlign = OMPBuilder.M.getDataLayout().getPointerABIAlignment(G->getAddressSpace()); - if (PtrAlign > llvm::Align(G->getAlignment())) - G->setAlignment(PtrAlign); - return G; + return OMPBuilder.getOrCreateInternalVariable(KmpCriticalNameTy, Name); } namespace { Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp =================================================================== --- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -4493,7 +4493,11 @@ M, Ty, /*IsConstant=*/false, GlobalValue::CommonLinkage, Constant::getNullValue(Ty), Elem.first(), /*InsertBefore=*/nullptr, GlobalValue::NotThreadLocal, AddressSpace); - GV->setAlignment(M.getDataLayout().getABITypeAlign(Ty)); + llvm::Align TypeAlign = M.getDataLayout().getABITypeAlign(Ty); + llvm::Align PtrAlign = + M.getDataLayout().getPointerABIAlignment(GV->getAddressSpace()); + TypeAlign = PtrAlign > TypeAlign ? PtrAlign : TypeAlign; + GV->setAlignment(TypeAlign); Elem.second = GV; } Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp =================================================================== --- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp +++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp @@ -2744,7 +2744,14 @@ PointerType *CriticalNamePtrTy = PointerType::getUnqual(ArrayType::get(Type::getInt32Ty(Ctx), 8)); EXPECT_EQ(CriticalEndCI->getArgOperand(2), CriticalEntryCI->getArgOperand(2)); - EXPECT_EQ(CriticalEndCI->getArgOperand(2)->getType(), CriticalNamePtrTy); + GlobalVariable *GV = + dyn_cast(CriticalEndCI->getArgOperand(2)); + ASSERT_NE(GV, nullptr); + EXPECT_EQ(GV->getType(), CriticalNamePtrTy); + llvm::Align PtrAlign = + M->getDataLayout().getPointerABIAlignment(GV->getAddressSpace()); + if (PtrAlign.value() > GV->getAlignment()) + EXPECT_EQ(GV->getAlignment(), PtrAlign.value()); } TEST_F(OpenMPIRBuilderTest, OrderedDirectiveDependSource) {