Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Aug 20, 2024
1 parent ce5db1e commit b4ad3c6
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 37 deletions.
6 changes: 5 additions & 1 deletion llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,16 @@ Changes to the C API

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

* ``LLVMGetSyncScopeID`` to map a synchronization scope name to an ID
* ``LLVMGetSyncScopeID`` to map a synchronization scope name to an ID.
* ``LLVMBuildFenceSyncScope``, ``LLVMBuildAtomicRMWSyncScope`` and
``LLVMBuildAtomicCmpXchgSyncScope`` versions of the existing builder functions
with an additional synchronization scope ID parameter.
* ``LLVMGetAtomicSyncScopeID`` and ``LLVMSetAtomicSyncScopeID`` to get and set the
synchronization scope of any atomic instruction.
* ``LLVMIsAtomic`` to check if an instruction is atomic, for use with the above functions.
Because of backwards compatibility, ``LLVMIsAtomicSingleThread`` and
``LLVMSetAtomicSingleThread`` continue to work with any instruction type.


Changes to the CodeGen infrastructure
-------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4377,7 +4377,7 @@ LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
Instruction *I = unwrap<Instruction>(AtomicInst);
if (!I->isAtomic())
return 0;
return getAtomicSyncScopeID(I).value() == SyncScope::SingleThread;
return *getAtomicSyncScopeID(I) == SyncScope::SingleThread;
}

void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
Expand All @@ -4388,7 +4388,7 @@ void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
unsigned LLVMGetAtomicSyncScopeID(LLVMValueRef AtomicInst) {
Instruction *I = unwrap<Instruction>(AtomicInst);
assert(I->isAtomic() && "Expected an atomic instruction");
return getAtomicSyncScopeID(I).value();
return *getAtomicSyncScopeID(I);
}

void LLVMSetAtomicSyncScopeID(LLVMValueRef AtomicInst, unsigned SSID) {
Expand Down
1 change: 1 addition & 0 deletions llvm/tools/llvm-c-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ endif ()

add_llvm_tool(llvm-c-test
attributes.c
atomic.c
calc.c
debuginfo.c
diagnostic.c
Expand Down
64 changes: 64 additions & 0 deletions llvm/tools/llvm-c-test/atomic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*===-- atomic.c - tool for testing libLLVM and llvm-c API ----------------===*\
|* *|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
|* Exceptions. *|
|* See https://llvm.org/LICENSE.txt for license information. *|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
|* *|
|*===----------------------------------------------------------------------===*|
|* *|
|* This file implements the --atomic-* commands in llvm-c-test. *|
|* *|
\*===----------------------------------------------------------------------===*/

#include "llvm-c-test.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int llvm_atomic_syncscope(void) {
LLVMBuilderRef Builder = LLVMCreateBuilder();

LLVMModuleRef M = LLVMModuleCreateWithName("Mod");
LLVMTypeRef FT = LLVMFunctionType(LLVMVoidType(), NULL, 0, 0);
LLVMValueRef F = LLVMAddFunction(M, "Fun", FT);
LLVMBasicBlockRef BB = LLVMAppendBasicBlock(F, "Entry");
LLVMPositionBuilderAtEnd(Builder, BB);

// echo.cpp already tests the new SyncScope APIs, also test the old ones here

// fence
LLVMValueRef Fence =
LLVMBuildFence(Builder, LLVMAtomicOrderingSequentiallyConsistent, 0, "");
assert(!LLVMIsAtomicSingleThread(Fence));
Fence =
LLVMBuildFence(Builder, LLVMAtomicOrderingSequentiallyConsistent, 1, "");
assert(LLVMIsAtomicSingleThread(Fence));

// atomicrmw
LLVMValueRef Ptr = LLVMConstPointerNull(LLVMPointerType(LLVMInt32Type(), 0));
LLVMValueRef Val = LLVMConstInt(LLVMInt32Type(), 0, 0);
LLVMValueRef AtomicRMW =
LLVMBuildAtomicRMW(Builder, LLVMAtomicRMWBinOpXchg, Ptr, Val,
LLVMAtomicOrderingSequentiallyConsistent, 0);
assert(!LLVMIsAtomicSingleThread(AtomicRMW));
AtomicRMW = LLVMBuildAtomicRMW(Builder, LLVMAtomicRMWBinOpXchg, Ptr, Val,
LLVMAtomicOrderingSequentiallyConsistent, 1);
assert(LLVMIsAtomicSingleThread(AtomicRMW));

// cmpxchg
LLVMValueRef CmpXchg = LLVMBuildAtomicCmpXchg(
Builder, Ptr, Val, Val, LLVMAtomicOrderingSequentiallyConsistent,
LLVMAtomicOrderingSequentiallyConsistent, 0);
assert(!LLVMIsAtomicSingleThread(CmpXchg));
CmpXchg = LLVMBuildAtomicCmpXchg(Builder, Ptr, Val, Val,
LLVMAtomicOrderingSequentiallyConsistent,
LLVMAtomicOrderingSequentiallyConsistent, 1);
assert(LLVMIsAtomicSingleThread(CmpXchg));

LLVMDisposeBuilder(Builder);
LLVMDisposeModule(M);

return 0;
}
44 changes: 10 additions & 34 deletions llvm/tools/llvm-c-test/echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,13 +755,8 @@ struct FunCloner {
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
if (LLVMIsAtomic(Src)) {
LLVMBool IsAtomicSingleThread = LLVMIsAtomicSingleThread(Src);
if (IsAtomicSingleThread)
LLVMSetAtomicSingleThread(Dst, IsAtomicSingleThread);
else
LLVMSetAtomicSyncScopeID(Dst, LLVMGetAtomicSyncScopeID(Src));
}
if (LLVMIsAtomic(Src))
LLVMSetAtomicSyncScopeID(Dst, LLVMGetAtomicSyncScopeID(Src));
break;
}
case LLVMStore: {
Expand All @@ -771,13 +766,8 @@ struct FunCloner {
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
if (LLVMIsAtomic(Src)) {
LLVMBool IsAtomicSingleThread = LLVMIsAtomicSingleThread(Src);
if (IsAtomicSingleThread)
LLVMSetAtomicSingleThread(Dst, IsAtomicSingleThread);
else
LLVMSetAtomicSyncScopeID(Dst, LLVMGetAtomicSyncScopeID(Src));
}
if (LLVMIsAtomic(Src))
LLVMSetAtomicSyncScopeID(Dst, LLVMGetAtomicSyncScopeID(Src));
break;
}
case LLVMGetElementPtr: {
Expand All @@ -798,12 +788,8 @@ struct FunCloner {
LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 1));
LLVMAtomicRMWBinOp BinOp = LLVMGetAtomicRMWBinOp(Src);
LLVMAtomicOrdering Ord = LLVMGetOrdering(Src);
LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
if (SingleThread)
Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread);
else
Dst = LLVMBuildAtomicRMWSyncScope(Builder, BinOp, Ptr, Val, Ord,
LLVMGetAtomicSyncScopeID(Src));
Dst = LLVMBuildAtomicRMWSyncScope(Builder, BinOp, Ptr, Val, Ord,
LLVMGetAtomicSyncScopeID(Src));
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
LLVMSetValueName2(Dst, Name, NameLen);
Expand All @@ -815,14 +801,8 @@ struct FunCloner {
LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
if (SingleThread)
Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
SingleThread);
else
Dst = LLVMBuildAtomicCmpXchgSyncScope(Builder, Ptr, Cmp, New, Succ,
Fail,
LLVMGetAtomicSyncScopeID(Src));
Dst = LLVMBuildAtomicCmpXchgSyncScope(
Builder, Ptr, Cmp, New, Succ, Fail, LLVMGetAtomicSyncScopeID(Src));
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
LLVMSetWeak(Dst, LLVMGetWeak(Src));
Expand Down Expand Up @@ -1013,12 +993,8 @@ struct FunCloner {
}
case LLVMFence: {
LLVMAtomicOrdering Ordering = LLVMGetOrdering(Src);
LLVMBool IsSingleThreaded = LLVMIsAtomicSingleThread(Src);
if (IsSingleThreaded)
Dst = LLVMBuildFence(Builder, Ordering, IsSingleThreaded, Name);
else
Dst = LLVMBuildFenceSyncScope(Builder, Ordering,
LLVMGetAtomicSyncScopeID(Src), Name);
Dst = LLVMBuildFenceSyncScope(Builder, Ordering,
LLVMGetAtomicSyncScopeID(Src), Name);
break;
}
case LLVMZExt: {
Expand Down
3 changes: 3 additions & 0 deletions llvm/tools/llvm-c-test/llvm-c-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ int llvm_test_diagnostic_handler(void);
int llvm_test_function_attributes(void);
int llvm_test_callsite_attributes(void);

// atomic.c
int llvm_atomic_syncscope(void);

#ifdef __cplusplus
}
#endif /* !defined(__cplusplus) */
Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/llvm-c-test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ int main(int argc, char **argv) {
} else if (argc == 2 &&
!strcmp(argv[1], "--test-dibuilder-debuginfo-format")) {
return llvm_test_dibuilder();
} else if (argc == 2 && !strcmp(argv[1], "--atomic-syncscope")) {
return llvm_atomic_syncscope();
} else {
print_usage();
}
Expand Down

0 comments on commit b4ad3c6

Please sign in to comment.