From 79f964dce953f441f983772539b321cc71514eb3 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Tue, 29 Aug 2023 12:27:30 +0200 Subject: [PATCH] Fix of atomic checks. Renamed MONOTONIC -> RELAXED. --- CMakeLists.txt | 7 +++++++ lib/std/core/mem.c3 | 2 +- src/compiler/llvm_codegen_builtins.c | 14 +++++++------- src/compiler/sema_builtins.c | 17 +++++++++++++---- .../concurrency/atomic_load_store.c3t | 4 ++-- .../concurrency/atomic_load_store_debug.c3t | 4 ++-- 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 43397cc67..3374b4dc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,6 +119,13 @@ endif() message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") message(STATUS "Libraries located in: ${LLVM_LIBRARY_DIRS}") + +if(LLVM_ENABLE_RTTI) + message(STATUS "LLVM was built with RTTI") +else() + message(STATUS "LLVM was not built with RTTI") +endif() + include_directories(${LLVM_INCLUDE_DIRS}) link_directories(${LLVM_LIBRARY_DIRS}) add_definitions(${LLVM_DEFINITIONS}) diff --git a/lib/std/core/mem.c3 b/lib/std/core/mem.c3 index 9079b8c5a..766c9d802 100644 --- a/lib/std/core/mem.c3 +++ b/lib/std/core/mem.c3 @@ -24,7 +24,7 @@ enum AtomicOrdering : int { NOT_ATOMIC, // Not atomic UNORDERED, // No lock - MONOTONIC, // Consistent ordering + RELAXED, // Consistent ordering ACQUIRE, // Barrier locking load/store RELEASE, // Barrier releasing load/store ACQUIRE_RELEASE, // Barrier fence to load/store diff --git a/src/compiler/llvm_codegen_builtins.c b/src/compiler/llvm_codegen_builtins.c index fd84e06b1..2ad1c8b02 100644 --- a/src/compiler/llvm_codegen_builtins.c +++ b/src/compiler/llvm_codegen_builtins.c @@ -69,13 +69,13 @@ static LLVMAtomicOrdering ordering_to_llvm(int value) { switch (value) { - case 0: return LLVMAtomicOrderingNotAtomic; - case 1: return LLVMAtomicOrderingUnordered; - case 2: return LLVMAtomicOrderingMonotonic; - case 3: return LLVMAtomicOrderingAcquire; - case 4: return LLVMAtomicOrderingRelease; - case 5: return LLVMAtomicOrderingAcquireRelease; - case 6: return LLVMAtomicOrderingSequentiallyConsistent; + case ATOMIC_NONE: return LLVMAtomicOrderingNotAtomic; + case ATOMIC_UNORDERED: return LLVMAtomicOrderingUnordered; + case ATOMIC_RELAXED: return LLVMAtomicOrderingMonotonic; + case ATOMIC_ACQUIRE: return LLVMAtomicOrderingAcquire; + case ATOMIC_RELEASE: return LLVMAtomicOrderingRelease; + case ATOMIC_ACQUIRE_RELEASE: return LLVMAtomicOrderingAcquireRelease; + case ATOMIC_SEQ_CONSISTENT: return LLVMAtomicOrderingSequentiallyConsistent; default: UNREACHABLE; } } diff --git a/src/compiler/sema_builtins.c b/src/compiler/sema_builtins.c index bed5bd075..184c7821c 100644 --- a/src/compiler/sema_builtins.c +++ b/src/compiler/sema_builtins.c @@ -3,6 +3,7 @@ // a copy of which can be found in the LICENSE file. #include "sema_internal.h" + typedef enum { BA_POINTER, @@ -164,7 +165,7 @@ static inline bool is_valid_atomicity(Expr* expr) { if (!expr_is_const_int(expr) || !int_fits(expr->const_expr.ixx, TYPE_U8) || expr->const_expr.ixx.i.low > 6) { - RETURN_SEMA_ERROR(expr, "Expected a constant integer value < 8."); + RETURN_SEMA_ERROR(expr, "Expected a constant integer value < 7."); } return true; } @@ -200,6 +201,14 @@ static bool sema_expr_analyse_compare_exchange(SemaContext *context, Expr *expr) if (!sema_analyse_expr_rhs(context, type_char, args[i], false)) return false; if (!is_valid_atomicity(args[i])) return false; } + unsigned success = args[5]->const_expr.ixx.i.low; + unsigned failure = args[6]->const_expr.ixx.i.low; + if (success < ATOMIC_RELAXED) RETURN_SEMA_ERROR(args[5], "Success ordering must be at least RELAXED."); + if (failure < ATOMIC_RELAXED) RETURN_SEMA_ERROR(args[6], "Failure ordering must be at least RELAXED."); + if (failure == ATOMIC_ACQUIRE_RELEASE || failure == ATOMIC_RELEASE) + { + RETURN_SEMA_ERROR(args[6], "Failure ordering may not be RELEASE / ACQUIRE_RELEASE."); + } Expr *align = args[7]; if (!sema_analyse_expr_rhs(context, type_usz, align, false)) return false; if (!expr_is_const_int(align) @@ -563,7 +572,7 @@ bool sema_expr_analyse_builtin_call(SemaContext *context, Expr *expr) if (!expr_is_const(args[1])) RETURN_SEMA_ERROR(args[1], "'is_volatile' must be a compile time constant."); if (!expr_is_const(args[2])) RETURN_SEMA_ERROR(args[2], "Ordering must be a compile time constant."); if (!is_valid_atomicity(args[2])) return false; - switch (expr->const_expr.ixx.i.low) + switch (args[2]->const_expr.ixx.i.low) { case ATOMIC_ACQUIRE_RELEASE: case ATOMIC_RELEASE: @@ -606,11 +615,11 @@ bool sema_expr_analyse_builtin_call(SemaContext *context, Expr *expr) if (!expr_is_const(args[2])) RETURN_SEMA_ERROR(args[2], "'is_volatile' must be a compile time constant."); if (!expr_is_const(args[3])) RETURN_SEMA_ERROR(args[3], "Ordering must be a compile time constant."); if (!is_valid_atomicity(args[3])) return false; - switch (expr->const_expr.ixx.i.low) + switch (args[3]->const_expr.ixx.i.low) { case ATOMIC_ACQUIRE_RELEASE: case ATOMIC_ACQUIRE: - RETURN_SEMA_ERROR(args[2], "'acquire' and 'acquire release' are not valid for atomic stores."); + RETURN_SEMA_ERROR(args[3], "'acquire' and 'acquire release' are not valid for atomic stores."); } rtype = args[1]->type; break; diff --git a/test/test_suite/concurrency/atomic_load_store.c3t b/test/test_suite/concurrency/atomic_load_store.c3t index 8f684099c..3eec6d2e0 100644 --- a/test/test_suite/concurrency/atomic_load_store.c3t +++ b/test/test_suite/concurrency/atomic_load_store.c3t @@ -12,9 +12,9 @@ fn void main() { int a = 111; int x = @atomic_load(a); - int y = @atomic_load(a, MONOTONIC, true); + int y = @atomic_load(a, RELAXED, true); @atomic_store(a, 123 + x); - @atomic_store(a, 33 + y, MONOTONIC, true); + @atomic_store(a, 33 + y, RELAXED, true); io::printfn("%d", a); } diff --git a/test/test_suite/concurrency/atomic_load_store_debug.c3t b/test/test_suite/concurrency/atomic_load_store_debug.c3t index 7355d97b6..4c03c66d8 100644 --- a/test/test_suite/concurrency/atomic_load_store_debug.c3t +++ b/test/test_suite/concurrency/atomic_load_store_debug.c3t @@ -13,9 +13,9 @@ fn void main() { int a = 111; int x = @atomic_load(a); - int y = @atomic_load(a, MONOTONIC, true); + int y = @atomic_load(a, RELAXED, true); @atomic_store(a, 123 + x); - @atomic_store(a, 33 + y, MONOTONIC, true); + @atomic_store(a, 33 + y, RELAXED, true); io::printfn("%d", a); }