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 @@ -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 { 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 @@ -4493,7 +4493,10 @@ M, Ty, /*IsConstant=*/false, GlobalValue::CommonLinkage, Constant::getNullValue(Ty), Elem.first(), /*InsertBefore=*/nullptr, GlobalValue::NotThreadLocal, AddressSpace); - GV->setAlignment(M.getDataLayout().getABITypeAlign(Ty)); + const DataLayout &DL = M.getDataLayout(); + const llvm::Align TypeAlign = DL.getABITypeAlign(Ty); + const llvm::Align PtrAlign = DL.getPointerABIAlignment(AddressSpace); + GV->setAlignment(std::max(TypeAlign, PtrAlign)); Elem.second = GV; } diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp --- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp +++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp @@ -2744,7 +2744,15 @@ 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); + const DataLayout &DL = M->getDataLayout(); + const llvm::Align TypeAlign = DL.getABITypeAlign(CriticalNamePtrTy); + const llvm::Align PtrAlign = DL.getPointerABIAlignment(GV->getAddressSpace()); + if (const llvm::MaybeAlign Alignment = GV->getAlign()) + EXPECT_EQ(*Alignment, std::max(TypeAlign, PtrAlign)); } TEST_F(OpenMPIRBuilderTest, OrderedDirectiveDependSource) {