From 375fd2f43a3300c73416b4490eb1b7b441b5d1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Jos=C3=A9=20G=C3=B3mez=20Hern=C3=A1ndez?= Date: Sat, 16 Sep 2023 18:04:33 +0200 Subject: [PATCH] Fix atomic_fetch_sub builtin + Updated atomic library --- lib/std/atomic.c3 | 130 ++++++--------------------- src/compiler/llvm_codegen_builtins.c | 2 +- 2 files changed, 29 insertions(+), 103 deletions(-) diff --git a/lib/std/atomic.c3 b/lib/std/atomic.c3 index 415567fdb..45963ef2f 100644 --- a/lib/std/atomic.c3 +++ b/lib/std/atomic.c3 @@ -85,31 +85,31 @@ macro Type Atomic.min(&self, Type value, AtomicOrdering ordering = SEQ_CONSISTEN return @atomic_exec(atomic::fetch_min, data, value, ordering); } -macro Type Atomic.or(&self, ulong value, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) +macro Type Atomic.or(&self, uint value, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) { Type* data = &self.data; return @atomic_exec(atomic::fetch_or, data, value, ordering); } -fn Type Atomic.xor(&self, ulong value, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) +fn Type Atomic.xor(&self, uint value, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) { Type* data = &self.data; return @atomic_exec(atomic::fetch_xor, data, value, ordering); } -macro Type Atomic.and(&self, ulong value, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) +macro Type Atomic.and(&self, uint value, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) { Type* data = &self.data; return @atomic_exec(atomic::fetch_and, data, value, ordering); } -macro Type Atomic.shift_right(&self, ulong amount, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) +macro Type Atomic.shift_right(&self, uint amount, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) { Type* data = &self.data; return @atomic_exec(atomic::fetch_shift_right, data, amount, ordering); } -macro Type Atomic.shift_left(&self, ulong amount, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) +macro Type Atomic.shift_left(&self, uint amount, AtomicOrdering ordering = SEQ_CONSISTENT) @if(!types::is_float(Type)) { Type* data = &self.data; return @atomic_exec(atomic::fetch_shift_left, data, amount, ordering); @@ -139,31 +139,9 @@ module std::atomic; * @require types::is_int($typeof(*ptr)) || types::is_float($typeof(*ptr)) "Only integer/float pointers may be used." * @require $ordering != AtomicOrdering.NOT_ATOMIC && $ordering != AtomicOrdering.UNORDERED "Acquire ordering is not valid." **/ -macro fetch_add(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) +macro fetch_add(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT, bool $volatile = false, usz $alignment = 0) { - var $load_ordering = $ordering; - $if $ordering == RELEASE || $ordering == ACQUIRE_RELEASE: - $load_ordering = AtomicOrdering.SEQ_CONSISTENT; - $endif - - var $StorageType = $typefrom(types::lower_to_atomic_compatible_type($typeof(*ptr))); - - $StorageType* storage_ptr = ($StorageType*)ptr; - - $typeof(*ptr) old_value; - $typeof(*ptr) new_value; - - $StorageType storage_old_value; - $StorageType storage_new_value; - - do { - storage_old_value = $$atomic_load(storage_ptr, false, $load_ordering.ordinal); - old_value = bitcast(storage_old_value, $typeof(*ptr)); - new_value = old_value + y; - storage_new_value = bitcast(new_value, $StorageType); - } while (mem::compare_exchange(storage_ptr, storage_old_value, storage_new_value, $ordering, $load_ordering) != storage_old_value); - - return old_value; + return $$atomic_fetch_add(ptr, y, $volatile, $ordering.ordinal, $alignment); } /** @@ -175,31 +153,9 @@ macro fetch_add(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) * @require types::is_int($typeof(*ptr)) || types::is_float($typeof(*ptr)) "Only integer/float pointers may be used." * @require $ordering != AtomicOrdering.NOT_ATOMIC && $ordering != AtomicOrdering.UNORDERED "Acquire ordering is not valid." **/ -macro fetch_sub(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) +macro fetch_sub(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT, bool $volatile = false, usz $alignment = 0) { - var $load_ordering = $ordering; - $if $ordering == RELEASE || $ordering == ACQUIRE_RELEASE: - $load_ordering = AtomicOrdering.SEQ_CONSISTENT; - $endif - - var $StorageType = $typefrom(types::lower_to_atomic_compatible_type($typeof(*ptr))); - - $StorageType* storage_ptr = ($StorageType*)ptr; - - $typeof(*ptr) old_value; - $typeof(*ptr) new_value; - - $StorageType storage_old_value; - $StorageType storage_new_value; - - do { - storage_old_value = $$atomic_load(storage_ptr, false, $load_ordering.ordinal); - old_value = bitcast(storage_old_value, $typeof(*ptr)); - new_value = old_value - y; - storage_new_value = bitcast(new_value, $StorageType); - } while (mem::compare_exchange(storage_ptr, storage_old_value, storage_new_value, $ordering, $load_ordering) != storage_old_value); - - return old_value; + return $$atomic_fetch_sub(ptr, y, $volatile, $ordering.ordinal, $alignment); } /** @@ -285,8 +241,12 @@ macro fetch_div(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) * @require types::is_int($typeof(y)) "The value for or must be an int" * @require $ordering != AtomicOrdering.NOT_ATOMIC && $ordering != AtomicOrdering.UNORDERED "Acquire ordering is not valid." **/ -macro fetch_or(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) +macro fetch_or(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT, bool $volatile = false, usz $alignment = 0) { + $if types::is_int($typeof(*ptr)): + return $$atomic_fetch_or(ptr, y, $volatile, $ordering.ordinal, $alignment); + $endif + var $load_ordering = $ordering; $if $ordering == RELEASE || $ordering == ACQUIRE_RELEASE: $load_ordering = AtomicOrdering.SEQ_CONSISTENT; @@ -323,8 +283,12 @@ macro fetch_or(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) * @require types::is_int($typeof(y)) "The value for or must be an int" * @require $ordering != AtomicOrdering.NOT_ATOMIC && $ordering != AtomicOrdering.UNORDERED "Acquire ordering is not valid." **/ -macro fetch_xor(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) +macro fetch_xor(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT, bool $volatile = false, usz $alignment = 0) { + $if types::is_int($typeof(*ptr)): + return $$atomic_fetch_xor(ptr, y, $volatile, $ordering.ordinal, $alignment); + $endif + var $load_ordering = $ordering; $if $ordering == RELEASE || $ordering == ACQUIRE_RELEASE: $load_ordering = AtomicOrdering.SEQ_CONSISTENT; @@ -361,8 +325,12 @@ macro fetch_xor(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) * @require types::is_int($typeof(y)) "The value for or must be an int" * @require $ordering != AtomicOrdering.NOT_ATOMIC && $ordering != AtomicOrdering.UNORDERED "Acquire ordering is not valid." **/ -macro fetch_and(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) +macro fetch_and(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT, bool $volatile = false, usz $alignment = 0) { + $if types::is_int($typeof(*ptr)): + return $$atomic_fetch_and(ptr, y, $volatile, $ordering.ordinal, $alignment); + $endif + var $load_ordering = $ordering; $if $ordering == RELEASE || $ordering == ACQUIRE_RELEASE: $load_ordering = AtomicOrdering.SEQ_CONSISTENT; @@ -514,30 +482,9 @@ macro flag_clear(ptr, AtomicOrdering $ordering = SEQ_CONSISTENT) * @require types::is_int($typeof(*ptr)) || types::is_float($typeof(*ptr)) "Only integer/float pointers may be used." * @require $ordering != AtomicOrdering.NOT_ATOMIC && $ordering != AtomicOrdering.UNORDERED "Acquire ordering is not valid." **/ -macro fetch_max(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) +macro fetch_max(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT, bool $volatile = false, usz $alignment = 0) { - var $load_ordering = $ordering; - $if $ordering == RELEASE || $ordering == ACQUIRE_RELEASE: - $load_ordering = AtomicOrdering.SEQ_CONSISTENT; - $endif - - var $StorageType = $typefrom(types::lower_to_atomic_compatible_type($typeof(*ptr))); - - $StorageType* storage_ptr = ($StorageType*)ptr; - - $typeof(*ptr) old_value; - $typeof(*ptr) new_value = y; - - $StorageType storage_old_value; - $StorageType storage_new_value = bitcast(new_value, $StorageType); - - do { - storage_old_value = $$atomic_load(storage_ptr, false, $load_ordering.ordinal); - old_value = bitcast(storage_old_value, $typeof(*ptr)); - if (old_value >= new_value) break; - } while (mem::compare_exchange(storage_ptr, storage_old_value, storage_new_value, $ordering, $load_ordering) != storage_old_value); - - return old_value; + return $$atomic_fetch_max(ptr, y, $volatile, $ordering.ordinal, $alignment); } /** @@ -549,28 +496,7 @@ macro fetch_max(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) * @require types::is_int($typeof(*ptr)) || types::is_float($typeof(*ptr)) "Only integer/float pointers may be used." * @require $ordering != AtomicOrdering.NOT_ATOMIC && $ordering != AtomicOrdering.UNORDERED "Acquire ordering is not valid." **/ -macro fetch_min(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT) +macro fetch_min(ptr, y, AtomicOrdering $ordering = SEQ_CONSISTENT, bool $volatile = false, usz $alignment = 0) { - var $load_ordering = $ordering; - $if $ordering == RELEASE || $ordering == ACQUIRE_RELEASE: - $load_ordering = AtomicOrdering.SEQ_CONSISTENT; - $endif - - var $StorageType = $typefrom(types::lower_to_atomic_compatible_type($typeof(*ptr))); - - $StorageType* storage_ptr = ($StorageType*)ptr; - - $typeof(*ptr) old_value; - $typeof(*ptr) new_value = y; - - $StorageType storage_old_value; - $StorageType storage_new_value = bitcast(new_value, $StorageType); - - do { - storage_old_value = $$atomic_load(storage_ptr, false, $load_ordering.ordinal); - old_value = bitcast(storage_old_value, $typeof(*ptr)); - if (old_value <= new_value) break; - } while (mem::compare_exchange(storage_ptr, storage_old_value, storage_new_value, $ordering, $load_ordering) != storage_old_value); - - return old_value; + return $$atomic_fetch_min(ptr, y, $volatile, $ordering.ordinal, $alignment); } diff --git a/src/compiler/llvm_codegen_builtins.c b/src/compiler/llvm_codegen_builtins.c index cf19fa831..70941962b 100644 --- a/src/compiler/llvm_codegen_builtins.c +++ b/src/compiler/llvm_codegen_builtins.c @@ -199,7 +199,7 @@ INLINE void llvm_emit_atomic_fetch(GenContext *c, BuiltinFunction func, BEValue op = is_float ? LLVMAtomicRMWBinOpFAdd : LLVMAtomicRMWBinOpAdd; break; case BUILTIN_ATOMIC_FETCH_SUB: - op = is_float ? LLVMAtomicRMWBinOpFAdd : LLVMAtomicRMWBinOpAdd; + op = is_float ? LLVMAtomicRMWBinOpFSub : LLVMAtomicRMWBinOpSub; break; case BUILTIN_ATOMIC_FETCH_MAX: op = is_float ? LLVMAtomicRMWBinOpFMax : (is_unsigned ? LLVMAtomicRMWBinOpUMax : LLVMAtomicRMWBinOpMax);