Skip to content

Commit 0028d86

Browse files
committed
Address review comments.
1 parent 0b9f075 commit 0028d86

File tree

4 files changed

+28
-70
lines changed

4 files changed

+28
-70
lines changed

llvm/docs/ReleaseNotes.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ Changes to the C API
165165

166166
* Support for creating instructions with custom synchronization scopes has been added:
167167

168-
* ``LLVMGetSyncScopeID`` and ``LLVMGetSyncScopeIDInContext`` to map a synchronization
169-
scope name to an ID, and ``LLVMGetSyncScopeName`` to map an ID back to a name.
168+
* ``LLVMGetSyncScopeID`` to map a synchronization scope name to an ID
170169
* ``LLVMBuildFenceSyncScope``, ``LLVMBuildAtomicRMWSyncScope`` and
171170
``LLVMBuildAtomicCmpXchgSyncScope`` versions of the existing builder functions
172171
with an additional synchronization scope ID parameter.

llvm/include/llvm-c/Core.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -649,22 +649,7 @@ unsigned LLVMGetMDKindID(const char *Name, unsigned SLen);
649649
/**
650650
* Maps a synchronization scope name to a ID unique within this context.
651651
*/
652-
unsigned LLVMGetSyncScopeIDInContext(LLVMContextRef C, const char *Name,
653-
unsigned SLen);
654-
655-
/**
656-
* Maps a synchronization scope name to a unique ID.
657-
*
658-
* This is equivalent to calling LLVMGetSyncScopeIDInContext with
659-
* LLVMGetGlobalContext() as the context parameter.
660-
*/
661-
unsigned LLVMGetSyncScopeID(const char *Name, unsigned SLen);
662-
663-
/**
664-
* Maps a synchronization scope ID to its name.
665-
*/
666-
const char *LLVMGetSyncScopeName(LLVMContextRef C, unsigned ID,
667-
unsigned *Length);
652+
unsigned LLVMGetSyncScopeID(LLVMContextRef C, const char *Name, size_t SLen);
668653

669654
/**
670655
* Return an unique id given the name of a enum attribute,

llvm/include/llvm/IR/Instructions.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,6 +4942,25 @@ inline std::optional<SyncScope::ID> getAtomicSyncScopeID(const Instruction *I) {
49424942
llvm_unreachable("unhandled atomic operation");
49434943
}
49444944

4945+
/// A helper function that sets an atomic operation's sync scope.
4946+
/// Does nothing if it is not an atomic operation.
4947+
inline void setAtomicSyncScopeID(Instruction *I, SyncScope::ID SSID) {
4948+
if (!I->isAtomic())
4949+
return;
4950+
if (auto *AI = dyn_cast<LoadInst>(I))
4951+
AI->setSyncScopeID(SSID);
4952+
else if (auto *AI = dyn_cast<StoreInst>(I))
4953+
AI->setSyncScopeID(SSID);
4954+
else if (auto *AI = dyn_cast<FenceInst>(I))
4955+
AI->setSyncScopeID(SSID);
4956+
else if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I))
4957+
AI->setSyncScopeID(SSID);
4958+
else if (auto *AI = dyn_cast<AtomicRMWInst>(I))
4959+
AI->setSyncScopeID(SSID);
4960+
else
4961+
llvm_unreachable("unhandled atomic operation");
4962+
}
4963+
49454964
//===----------------------------------------------------------------------===//
49464965
// FreezeInst Class
49474966
//===----------------------------------------------------------------------===//

llvm/lib/IR/Core.cpp

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/IR/GlobalVariable.h"
2525
#include "llvm/IR/IRBuilder.h"
2626
#include "llvm/IR/InlineAsm.h"
27+
#include "llvm/IR/Instructions.h"
2728
#include "llvm/IR/IntrinsicInst.h"
2829
#include "llvm/IR/LLVMContext.h"
2930
#include "llvm/IR/LegacyPassManager.h"
@@ -146,24 +147,10 @@ unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
146147
return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
147148
}
148149

149-
unsigned LLVMGetSyncScopeIDInContext(LLVMContextRef C, const char *Name,
150-
unsigned SLen) {
150+
unsigned LLVMGetSyncScopeID(LLVMContextRef C, const char *Name, size_t SLen) {
151151
return unwrap(C)->getOrInsertSyncScopeID(StringRef(Name, SLen));
152152
}
153153

154-
unsigned LLVMGetSyncScopeID(const char *Name, unsigned SLen) {
155-
return LLVMGetSyncScopeIDInContext(LLVMGetGlobalContext(), Name, SLen);
156-
}
157-
158-
const char *LLVMGetSyncScopeName(LLVMContextRef C, unsigned ID,
159-
unsigned *Length) {
160-
SmallVector<StringRef> SSNs;
161-
unwrap(C)->getSyncScopeNames(SSNs);
162-
StringRef Name = SSNs[ID].empty() ? "system" : SSNs[ID];
163-
*Length = Name.size();
164-
return Name.data();
165-
}
166-
167154
unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
168155
return Attribute::getAttrKindFromName(StringRef(Name, SLen));
169156
}
@@ -4338,11 +4325,6 @@ LLVMValueRef LLVMBuildAtomicRMWSyncScope(LLVMBuilderRef B,
43384325
LLVMAtomicOrdering ordering,
43394326
unsigned SSID) {
43404327
AtomicRMWInst::BinOp intop = mapFromLLVMRMWBinOp(op);
4341-
4342-
SmallVector<StringRef> SSNs;
4343-
unwrap(B)->getContext().getSyncScopeNames(SSNs);
4344-
assert(SSID < SSNs.size() && "Invalid SyncScopeID");
4345-
43464328
return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
43474329
MaybeAlign(),
43484330
mapFromLLVMOrdering(ordering), SSID));
@@ -4386,49 +4368,22 @@ int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) {
43864368

43874369
int LLVMGetUndefMaskElem(void) { return PoisonMaskElem; }
43884370

4389-
static unsigned getAtomicSyncScopeID(Value *P) {
4390-
if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
4391-
return I->getSyncScopeID();
4392-
else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4393-
return FI->getSyncScopeID();
4394-
else if (StoreInst *SI = dyn_cast<StoreInst>(P))
4395-
return SI->getSyncScopeID();
4396-
else if (LoadInst *LI = dyn_cast<LoadInst>(P))
4397-
return LI->getSyncScopeID();
4398-
return cast<AtomicCmpXchgInst>(P)->getSyncScopeID();
4399-
}
4400-
44014371
LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
4402-
Value *P = unwrap(AtomicInst);
4403-
return getAtomicSyncScopeID(P) == SyncScope::SingleThread;
4372+
return getAtomicSyncScopeID(unwrap<Instruction>(AtomicInst)).value() ==
4373+
SyncScope::SingleThread;
44044374
}
44054375

44064376
unsigned LLVMGetAtomicSyncScopeID(LLVMValueRef AtomicInst) {
4407-
Value *P = unwrap(AtomicInst);
4408-
return getAtomicSyncScopeID(P);
4409-
}
4410-
4411-
static void setAtomicSyncScopeID(Value *P, unsigned SSID) {
4412-
if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P))
4413-
return I->setSyncScopeID(SSID);
4414-
else if (FenceInst *FI = dyn_cast<FenceInst>(P))
4415-
return FI->setSyncScopeID(SSID);
4416-
else if (StoreInst *SI = dyn_cast<StoreInst>(P))
4417-
return SI->setSyncScopeID(SSID);
4418-
else if (LoadInst *LI = dyn_cast<LoadInst>(P))
4419-
return LI->setSyncScopeID(SSID);
4420-
return cast<AtomicCmpXchgInst>(P)->setSyncScopeID(SSID);
4377+
return getAtomicSyncScopeID(unwrap<Instruction>(AtomicInst)).value();
44214378
}
44224379

44234380
void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
4424-
Value *P = unwrap(AtomicInst);
44254381
SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
4426-
setAtomicSyncScopeID(P, SSID);
4382+
setAtomicSyncScopeID(unwrap<Instruction>(AtomicInst), SSID);
44274383
}
44284384

44294385
void LLVMSetAtomicSyncScopeID(LLVMValueRef AtomicInst, unsigned SSID) {
4430-
Value *P = unwrap(AtomicInst);
4431-
setAtomicSyncScopeID(P, SSID);
4386+
setAtomicSyncScopeID(unwrap<Instruction>(AtomicInst), SSID);
44324387
}
44334388

44344389
LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) {

0 commit comments

Comments
 (0)