[llvm] 8bd8534 - LLVM-C: Allow LLVM{Get/Set}Alignment on an atomicrmw/cmpxchg instruction.

James Y Knight via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 12 15:32:26 PST 2021


Author: James Y Knight
Date: 2021-02-12T18:31:18-05:00
New Revision: 8bd8534aa3bdddf328683d79cfa46ee1d678f3d2

URL: https://github.com/llvm/llvm-project/commit/8bd8534aa3bdddf328683d79cfa46ee1d678f3d2
DIFF: https://github.com/llvm/llvm-project/commit/8bd8534aa3bdddf328683d79cfa46ee1d678f3d2.diff

LOG: LLVM-C: Allow LLVM{Get/Set}Alignment on an atomicrmw/cmpxchg instruction.

(Now that these can have alignment specified.)

Added: 
    

Modified: 
    llvm/include/llvm-c/Core.h
    llvm/lib/IR/Core.cpp
    llvm/test/Bindings/llvm-c/echo.ll
    llvm/tools/llvm-c-test/echo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index a78df16ca404..91c15323451f 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -2249,6 +2249,8 @@ void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr);
  * @see llvm::AllocaInst::getAlignment()
  * @see llvm::LoadInst::getAlignment()
  * @see llvm::StoreInst::getAlignment()
+ * @see llvm::AtomicRMWInst::setAlignment()
+ * @see llvm::AtomicCmpXchgInst::setAlignment()
  * @see llvm::GlobalValue::getAlignment()
  */
 unsigned LLVMGetAlignment(LLVMValueRef V);
@@ -2258,6 +2260,8 @@ unsigned LLVMGetAlignment(LLVMValueRef V);
  * @see llvm::AllocaInst::setAlignment()
  * @see llvm::LoadInst::setAlignment()
  * @see llvm::StoreInst::setAlignment()
+ * @see llvm::AtomicRMWInst::setAlignment()
+ * @see llvm::AtomicCmpXchgInst::setAlignment()
  * @see llvm::GlobalValue::setAlignment()
  */
 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes);

diff  --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 90ba69069bae..d38cd6fe19f3 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -2042,9 +2042,14 @@ unsigned LLVMGetAlignment(LLVMValueRef V) {
     return LI->getAlignment();
   if (StoreInst *SI = dyn_cast<StoreInst>(P))
     return SI->getAlignment();
+  if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
+    return RMWI->getAlign().value();
+  if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
+    return CXI->getAlign().value();
 
   llvm_unreachable(
-      "only GlobalObject, AllocaInst, LoadInst and StoreInst have alignment");
+      "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
+      "and AtomicCmpXchgInst have alignment");
 }
 
 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
@@ -2057,9 +2062,14 @@ void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
     LI->setAlignment(Align(Bytes));
   else if (StoreInst *SI = dyn_cast<StoreInst>(P))
     SI->setAlignment(Align(Bytes));
+  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
+    RMWI->setAlignment(Align(Bytes));
+  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
+    CXI->setAlignment(Align(Bytes));
   else
     llvm_unreachable(
-        "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
+        "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
+        "and AtomicCmpXchgInst have alignment");
 }
 
 LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,

diff  --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll
index a1e77f65ccda..64e516c1970f 100644
--- a/llvm/test/Bindings/llvm-c/echo.ll
+++ b/llvm/test/Bindings/llvm-c/echo.ll
@@ -148,11 +148,11 @@ define void @memops(i8* %ptr) {
   store volatile i8 0, i8* %ptr
   store i8 0, i8* %ptr, align 8
   store atomic i8 0, i8* %ptr release, align 32
-  %e = atomicrmw add i8* %ptr, i8 0 monotonic
-  %f = atomicrmw volatile xchg i8* %ptr, i8 0 acq_rel
-  %g = cmpxchg i8* %ptr, i8 1, i8 2 seq_cst acquire
-  %h = cmpxchg weak i8* %ptr, i8 1, i8 2 seq_cst acquire
-  %i = cmpxchg volatile i8* %ptr, i8 1, i8 2 monotonic monotonic
+  %e = atomicrmw add i8* %ptr, i8 0 monotonic, align 1
+  %f = atomicrmw volatile xchg i8* %ptr, i8 0 acq_rel, align 8
+  %g = cmpxchg i8* %ptr, i8 1, i8 2 seq_cst acquire, align 1
+  %h = cmpxchg weak i8* %ptr, i8 1, i8 2 seq_cst acquire, align 8
+  %i = cmpxchg volatile i8* %ptr, i8 1, i8 2 monotonic monotonic, align 16
   ret void
 }
 

diff  --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp
index a29f360d1fdf..539e87e866c6 100644
--- a/llvm/tools/llvm-c-test/echo.cpp
+++ b/llvm/tools/llvm-c-test/echo.cpp
@@ -653,6 +653,7 @@ struct FunCloner {
         LLVMAtomicOrdering Ord = LLVMGetOrdering(Src);
         LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
         Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread);
+        LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
         LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
         LLVMSetValueName2(Dst, Name, NameLen);
         break;
@@ -667,6 +668,7 @@ struct FunCloner {
 
         Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
                                      SingleThread);
+        LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
         LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
         LLVMSetWeak(Dst, LLVMGetWeak(Src));
         LLVMSetValueName2(Dst, Name, NameLen);


        


More information about the llvm-commits mailing list