[PATCH] D157040: [OpenMP][IR] Set correct alignment for critical region lock

Hao Jin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 3 14:37:56 PDT 2023


erjin created this revision.
erjin added reviewers: jlpeyton, tstellar, jdoerfert, gchatelet, bryanpkc, tianshilei1992.
erjin added projects: All, OpenMP, LLVM.
Herald added subscribers: guansong, hiraditya, yaxunl.
erjin requested review of this revision.
Herald added subscribers: llvm-commits, jplehr, sstefan1.

OpenMP runtime functions assume the pointers are aligned to sizeof(pointer), but it is being aligned incorrectly. Fix with the proper alignment in the IR builder.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157040

Files:
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp


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<GlobalVariable>(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) {
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===================================================================
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -4503,7 +4503,13 @@
 Value *OpenMPIRBuilder::getOMPCriticalRegionLock(StringRef CriticalName) {
   std::string Prefix = Twine("gomp_critical_user_", CriticalName).str();
   std::string Name = getNameWithSeparators({Prefix, "var"}, ".", ".");
-  return getOrCreateInternalVariable(KmpCriticalNameTy, Name);
+  llvm::GlobalVariable *G =
+      getOrCreateInternalVariable(KmpCriticalNameTy, Name);
+  llvm::Align PtrAlign =
+      M.getDataLayout().getPointerABIAlignment(G->getAddressSpace());
+  if (PtrAlign > llvm::Align(G->getAlignment()))
+    G->setAlignment(PtrAlign);
+  return G;
 }
 
 Value *OpenMPIRBuilder::getSizeInBytes(Value *BasePtr) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157040.547006.patch
Type: text/x-patch
Size: 1811 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230803/be7a99b1/attachment.bin>


More information about the llvm-commits mailing list