Skip to content

Commit

Permalink
Address more review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Aug 20, 2024
1 parent b4ad3c6 commit 41aa204
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 77 deletions.
4 changes: 1 addition & 3 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4943,10 +4943,8 @@ inline std::optional<SyncScope::ID> getAtomicSyncScopeID(const Instruction *I) {
}

/// A helper function that sets an atomic operation's sync scope.
/// Does nothing if it is not an atomic operation.
inline void setAtomicSyncScopeID(Instruction *I, SyncScope::ID SSID) {
if (!I->isAtomic())
return;
assert(I->isAtomic());
if (auto *AI = dyn_cast<LoadInst>(I))
AI->setSyncScopeID(SSID);
else if (auto *AI = dyn_cast<StoreInst>(I))
Expand Down
8 changes: 7 additions & 1 deletion llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4377,12 +4377,18 @@ LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
Instruction *I = unwrap<Instruction>(AtomicInst);
if (!I->isAtomic())
return 0;

return *getAtomicSyncScopeID(I) == SyncScope::SingleThread;
}

void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) {
// Backwards compatibility: ignore non-atomic instructions
Instruction *I = unwrap<Instruction>(AtomicInst);
if (!I->isAtomic())
return;

SyncScope::ID SSID = NewValue ? SyncScope::SingleThread : SyncScope::System;
setAtomicSyncScopeID(unwrap<Instruction>(AtomicInst), SSID);
setAtomicSyncScopeID(I, SSID);
}

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

add_llvm_tool(llvm-c-test
attributes.c
atomic.c
calc.c
debuginfo.c
diagnostic.c
Expand Down
64 changes: 0 additions & 64 deletions llvm/tools/llvm-c-test/atomic.c

This file was deleted.

3 changes: 0 additions & 3 deletions llvm/tools/llvm-c-test/llvm-c-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ 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: 0 additions & 2 deletions llvm/tools/llvm-c-test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ 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
52 changes: 49 additions & 3 deletions llvm/unittests/IR/InstructionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,8 @@ TEST(InstructionsTest, ShuffleMaskQueries) {
EXPECT_TRUE(
ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3}), 2));

// Nothing special about the values here - just re-using inputs to reduce code.
// Nothing special about the values here - just re-using inputs to reduce
// code.
Constant *V0 = ConstantVector::get({C0, C1, C2, C3});
Constant *V1 = ConstantVector::get({C3, C2, C1, C0});

Expand Down Expand Up @@ -1216,7 +1217,7 @@ TEST(InstructionsTest, ShuffleMaskQueries) {
EXPECT_FALSE(Id6->isIdentityWithExtract());
EXPECT_FALSE(Id6->isConcat());
delete Id6;

// Result has more elements than operands, but extra elements are not undef.
ShuffleVectorInst *Id7 = new ShuffleVectorInst(V0, V1,
ConstantVector::get({C0, C1, C2, C3, CU, C1}));
Expand All @@ -1225,7 +1226,7 @@ TEST(InstructionsTest, ShuffleMaskQueries) {
EXPECT_FALSE(Id7->isIdentityWithExtract());
EXPECT_FALSE(Id7->isConcat());
delete Id7;

// Result has more elements than operands; choose from Op0 and Op1 is not identity.
ShuffleVectorInst *Id8 = new ShuffleVectorInst(V0, V1,
ConstantVector::get({C4, CU, C2, C3, CU, CU}));
Expand Down Expand Up @@ -1814,5 +1815,50 @@ TEST(InstructionsTest, InsertAtEnd) {
EXPECT_EQ(Ret->getNextNode(), I);
}

TEST(InstructionsTest, AtomicSyncscope) {
LLVMContext Ctx;

Module M("Mod", Ctx);
FunctionType *FT = FunctionType::get(Type::getVoidTy(Ctx), {}, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, "Fun", M);
BasicBlock *BB = BasicBlock::Create(Ctx, "Entry", F);
IRBuilder<> Builder(BB);

// SyncScope-variants of LLVM C IRBuilder APIs are tested by llvm-c-test,
// so cover the old versions (with a SingleThreaded argument) here.
Value *Ptr = ConstantPointerNull::get(Builder.getPtrTy());
Value *Val = ConstantInt::get(Type::getInt32Ty(Ctx), 0);

// fence
LLVMValueRef Fence = LLVMBuildFence(
wrap(&Builder), LLVMAtomicOrderingSequentiallyConsistent, 0, "");
EXPECT_FALSE(LLVMIsAtomicSingleThread(Fence));
Fence = LLVMBuildFence(wrap(&Builder),
LLVMAtomicOrderingSequentiallyConsistent, 1, "");
EXPECT_TRUE(LLVMIsAtomicSingleThread(Fence));

// atomicrmw
LLVMValueRef AtomicRMW = LLVMBuildAtomicRMW(
wrap(&Builder), LLVMAtomicRMWBinOpXchg, wrap(Ptr), wrap(Val),
LLVMAtomicOrderingSequentiallyConsistent, 0);
EXPECT_FALSE(LLVMIsAtomicSingleThread(AtomicRMW));
AtomicRMW = LLVMBuildAtomicRMW(wrap(&Builder), LLVMAtomicRMWBinOpXchg,
wrap(Ptr), wrap(Val),
LLVMAtomicOrderingSequentiallyConsistent, 1);
EXPECT_TRUE(LLVMIsAtomicSingleThread(AtomicRMW));

// cmpxchg
LLVMValueRef CmpXchg =
LLVMBuildAtomicCmpXchg(wrap(&Builder), wrap(Ptr), wrap(Val), wrap(Val),
LLVMAtomicOrderingSequentiallyConsistent,
LLVMAtomicOrderingSequentiallyConsistent, 0);
EXPECT_FALSE(LLVMIsAtomicSingleThread(CmpXchg));
CmpXchg =
LLVMBuildAtomicCmpXchg(wrap(&Builder), wrap(Ptr), wrap(Val), wrap(Val),
LLVMAtomicOrderingSequentiallyConsistent,
LLVMAtomicOrderingSequentiallyConsistent, 1);
EXPECT_TRUE(LLVMIsAtomicSingleThread(CmpXchg));
}

} // end anonymous namespace
} // end namespace llvm

0 comments on commit 41aa204

Please sign in to comment.