Skip to content

Commit 8bd8534

Browse files
committed
LLVM-C: Allow LLVM{Get/Set}Alignment on an atomicrmw/cmpxchg instruction.
(Now that these can have alignment specified.)
1 parent 8fc219d commit 8bd8534

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

llvm/include/llvm-c/Core.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,8 @@ void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr);
22492249
* @see llvm::AllocaInst::getAlignment()
22502250
* @see llvm::LoadInst::getAlignment()
22512251
* @see llvm::StoreInst::getAlignment()
2252+
* @see llvm::AtomicRMWInst::setAlignment()
2253+
* @see llvm::AtomicCmpXchgInst::setAlignment()
22522254
* @see llvm::GlobalValue::getAlignment()
22532255
*/
22542256
unsigned LLVMGetAlignment(LLVMValueRef V);
@@ -2258,6 +2260,8 @@ unsigned LLVMGetAlignment(LLVMValueRef V);
22582260
* @see llvm::AllocaInst::setAlignment()
22592261
* @see llvm::LoadInst::setAlignment()
22602262
* @see llvm::StoreInst::setAlignment()
2263+
* @see llvm::AtomicRMWInst::setAlignment()
2264+
* @see llvm::AtomicCmpXchgInst::setAlignment()
22612265
* @see llvm::GlobalValue::setAlignment()
22622266
*/
22632267
void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes);

llvm/lib/IR/Core.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,9 +2042,14 @@ unsigned LLVMGetAlignment(LLVMValueRef V) {
20422042
return LI->getAlignment();
20432043
if (StoreInst *SI = dyn_cast<StoreInst>(P))
20442044
return SI->getAlignment();
2045+
if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2046+
return RMWI->getAlign().value();
2047+
if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2048+
return CXI->getAlign().value();
20452049

20462050
llvm_unreachable(
2047-
"only GlobalObject, AllocaInst, LoadInst and StoreInst have alignment");
2051+
"only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2052+
"and AtomicCmpXchgInst have alignment");
20482053
}
20492054

20502055
void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
@@ -2057,9 +2062,14 @@ void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
20572062
LI->setAlignment(Align(Bytes));
20582063
else if (StoreInst *SI = dyn_cast<StoreInst>(P))
20592064
SI->setAlignment(Align(Bytes));
2065+
else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2066+
RMWI->setAlignment(Align(Bytes));
2067+
else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2068+
CXI->setAlignment(Align(Bytes));
20602069
else
20612070
llvm_unreachable(
2062-
"only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
2071+
"only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2072+
"and AtomicCmpXchgInst have alignment");
20632073
}
20642074

20652075
LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value,

llvm/test/Bindings/llvm-c/echo.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ define void @memops(i8* %ptr) {
148148
store volatile i8 0, i8* %ptr
149149
store i8 0, i8* %ptr, align 8
150150
store atomic i8 0, i8* %ptr release, align 32
151-
%e = atomicrmw add i8* %ptr, i8 0 monotonic
152-
%f = atomicrmw volatile xchg i8* %ptr, i8 0 acq_rel
153-
%g = cmpxchg i8* %ptr, i8 1, i8 2 seq_cst acquire
154-
%h = cmpxchg weak i8* %ptr, i8 1, i8 2 seq_cst acquire
155-
%i = cmpxchg volatile i8* %ptr, i8 1, i8 2 monotonic monotonic
151+
%e = atomicrmw add i8* %ptr, i8 0 monotonic, align 1
152+
%f = atomicrmw volatile xchg i8* %ptr, i8 0 acq_rel, align 8
153+
%g = cmpxchg i8* %ptr, i8 1, i8 2 seq_cst acquire, align 1
154+
%h = cmpxchg weak i8* %ptr, i8 1, i8 2 seq_cst acquire, align 8
155+
%i = cmpxchg volatile i8* %ptr, i8 1, i8 2 monotonic monotonic, align 16
156156
ret void
157157
}
158158

llvm/tools/llvm-c-test/echo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ struct FunCloner {
653653
LLVMAtomicOrdering Ord = LLVMGetOrdering(Src);
654654
LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
655655
Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread);
656+
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
656657
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
657658
LLVMSetValueName2(Dst, Name, NameLen);
658659
break;
@@ -667,6 +668,7 @@ struct FunCloner {
667668

668669
Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
669670
SingleThread);
671+
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
670672
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
671673
LLVMSetWeak(Dst, LLVMGetWeak(Src));
672674
LLVMSetValueName2(Dst, Name, NameLen);

0 commit comments

Comments
 (0)