runtime: extend internal atomics to comply with sync/atomic

This is the gofrontend version of https://golang.org/cl/289152.

Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/339690
This commit is contained in:
Ian Lance Taylor 2021-08-03 16:22:48 -07:00
parent c8b024fa4b
commit 582c24e9fe
4 changed files with 129 additions and 26 deletions

View File

@ -1,4 +1,4 @@
b47bcf942daa9a0c252db9b57b8f138adbfcdaa2
32590102c464679f845667b5554e1dcce2549ad2
The first line of this file holds the git revision number of the last
merge done from the gofrontend repository.

View File

@ -11590,12 +11590,10 @@ Call_expression::intrinsify(Gogo* gogo,
// sync/atomic functions and runtime/internal/atomic functions
// are very similar. In order not to duplicate code, we just
// redirect to the latter and let the code below to handle them.
// In case there is no equivalent functions (slight variance
// in types), we just make an artificial name (begin with '$').
// Note: no StorePointer, SwapPointer, and CompareAndSwapPointer,
// as they need write barriers.
if (name == "LoadInt32")
name = "$Loadint32";
name = "Loadint32";
else if (name == "LoadInt64")
name = "Loadint64";
else if (name == "LoadUint32")
@ -11607,9 +11605,9 @@ Call_expression::intrinsify(Gogo* gogo,
else if (name == "LoadPointer")
name = "Loadp";
else if (name == "StoreInt32")
name = "$Storeint32";
name = "Storeint32";
else if (name == "StoreInt64")
name = "$Storeint64";
name = "Storeint64";
else if (name == "StoreUint32")
name = "Store";
else if (name == "StoreUint64")
@ -11617,7 +11615,7 @@ Call_expression::intrinsify(Gogo* gogo,
else if (name == "StoreUintptr")
name = "Storeuintptr";
else if (name == "AddInt32")
name = "$Xaddint32";
name = "Xaddint32";
else if (name == "AddInt64")
name = "Xaddint64";
else if (name == "AddUint32")
@ -11627,9 +11625,9 @@ Call_expression::intrinsify(Gogo* gogo,
else if (name == "AddUintptr")
name = "Xadduintptr";
else if (name == "SwapInt32")
name = "$Xchgint32";
name = "Xchgint32";
else if (name == "SwapInt64")
name = "$Xchgint64";
name = "Xchgint64";
else if (name == "SwapUint32")
name = "Xchg";
else if (name == "SwapUint64")
@ -11637,9 +11635,9 @@ Call_expression::intrinsify(Gogo* gogo,
else if (name == "SwapUintptr")
name = "Xchguintptr";
else if (name == "CompareAndSwapInt32")
name = "$Casint32";
name = "Casint32";
else if (name == "CompareAndSwapInt64")
name = "$Casint64";
name = "Casint64";
else if (name == "CompareAndSwapUint32")
name = "Cas";
else if (name == "CompareAndSwapUint64")
@ -11875,7 +11873,7 @@ Call_expression::intrinsify(Gogo* gogo,
if ((name == "Load" || name == "Load64" || name == "Loadint64" || name == "Loadp"
|| name == "Loaduint" || name == "Loaduintptr" || name == "LoadAcq"
|| name == "$Loadint32")
|| name == "Loadint32")
&& this->args_ != NULL && this->args_->size() == 1)
{
if (int_size < 8 && (name == "Load64" || name == "Loadint64"))
@ -11895,7 +11893,7 @@ Call_expression::intrinsify(Gogo* gogo,
code = Runtime::ATOMIC_LOAD_8;
res_type = uint64_type;
}
else if (name == "$Loadint32")
else if (name == "Loadint32")
{
code = Runtime::ATOMIC_LOAD_4;
res_type = int32_type;
@ -11942,10 +11940,10 @@ Call_expression::intrinsify(Gogo* gogo,
if ((name == "Store" || name == "Store64" || name == "StorepNoWB"
|| name == "Storeuintptr" || name == "StoreRel"
|| name == "$Storeint32" || name == "$Storeint64")
|| name == "Storeint32" || name == "Storeint64")
&& this->args_ != NULL && this->args_->size() == 2)
{
if (int_size < 8 && (name == "Store64" || name == "$Storeint64"))
if (int_size < 8 && (name == "Store64" || name == "Storeint64"))
return NULL;
Runtime::Function code;
@ -11955,9 +11953,9 @@ Call_expression::intrinsify(Gogo* gogo,
code = Runtime::ATOMIC_STORE_4;
else if (name == "Store64")
code = Runtime::ATOMIC_STORE_8;
else if (name == "$Storeint32")
else if (name == "Storeint32")
code = Runtime::ATOMIC_STORE_4;
else if (name == "$Storeint64")
else if (name == "Storeint64")
code = Runtime::ATOMIC_STORE_8;
else if (name == "Storeuintptr")
code = (ptr_size == 8 ? Runtime::ATOMIC_STORE_8 : Runtime::ATOMIC_STORE_4);
@ -11979,7 +11977,7 @@ Call_expression::intrinsify(Gogo* gogo,
}
if ((name == "Xchg" || name == "Xchg64" || name == "Xchguintptr"
|| name == "$Xchgint32" || name == "$Xchgint64")
|| name == "Xchgint32" || name == "Xchgint64")
&& this->args_ != NULL && this->args_->size() == 2)
{
if (int_size < 8 && (name == "Xchg64" || name == "Xchgint64"))
@ -11997,12 +11995,12 @@ Call_expression::intrinsify(Gogo* gogo,
code = Runtime::ATOMIC_EXCHANGE_8;
res_type = uint64_type;
}
else if (name == "$Xchgint32")
else if (name == "Xchgint32")
{
code = Runtime::ATOMIC_EXCHANGE_4;
res_type = int32_type;
}
else if (name == "$Xchgint64")
else if (name == "Xchgint64")
{
code = Runtime::ATOMIC_EXCHANGE_8;
res_type = int64_type;
@ -12025,10 +12023,10 @@ Call_expression::intrinsify(Gogo* gogo,
if ((name == "Cas" || name == "Cas64" || name == "Casuintptr"
|| name == "Casp1" || name == "CasRel"
|| name == "$Casint32" || name == "$Casint64")
|| name == "Casint32" || name == "Casint64")
&& this->args_ != NULL && this->args_->size() == 3)
{
if (int_size < 8 && (name == "Cas64" || name == "$Casint64"))
if (int_size < 8 && (name == "Cas64" || name == "Casint64"))
return NULL;
Runtime::Function code;
@ -12047,9 +12045,9 @@ Call_expression::intrinsify(Gogo* gogo,
code = Runtime::ATOMIC_COMPARE_EXCHANGE_4;
else if (name == "Cas64")
code = Runtime::ATOMIC_COMPARE_EXCHANGE_8;
else if (name == "$Casint32")
else if (name == "Casint32")
code = Runtime::ATOMIC_COMPARE_EXCHANGE_4;
else if (name == "$Casint64")
else if (name == "Casint64")
code = Runtime::ATOMIC_COMPARE_EXCHANGE_8;
else if (name == "Casuintptr")
code = (ptr_size == 8
@ -12077,7 +12075,7 @@ Call_expression::intrinsify(Gogo* gogo,
}
if ((name == "Xadd" || name == "Xadd64" || name == "Xaddint64"
|| name == "Xadduintptr" || name == "$Xaddint32")
|| name == "Xadduintptr" || name == "Xaddint32")
&& this->args_ != NULL && this->args_->size() == 2)
{
if (int_size < 8 && (name == "Xadd64" || name == "Xaddint64"))
@ -12095,7 +12093,7 @@ Call_expression::intrinsify(Gogo* gogo,
code = Runtime::ATOMIC_ADD_FETCH_8;
res_type = uint64_type;
}
else if (name == "$Xaddint32")
else if (name == "Xaddint32")
{
code = Runtime::ATOMIC_ADD_FETCH_4;
res_type = int32_type;

View File

@ -104,6 +104,16 @@ Loaduint (uintgo *ptr)
return __atomic_load_n (ptr, __ATOMIC_SEQ_CST);
}
int32_t Loadint32 (int32_t *ptr)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Loadint32")
__attribute__ ((no_split_stack));
int32_t
Loadint32 (int32_t *ptr)
{
return __atomic_load_n (ptr, __ATOMIC_SEQ_CST);
}
int64_t Loadint64 (int64_t *ptr)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Loadint64")
__attribute__ ((no_split_stack));
@ -126,6 +136,16 @@ Xadd (uint32_t *ptr, int32_t delta)
return __atomic_add_fetch (ptr, (uint32_t) delta, __ATOMIC_SEQ_CST);
}
int32_t Xaddint32 (int32_t *ptr, int32_t delta)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xaddint32")
__attribute__ ((no_split_stack));
int32_t
Xaddint32 (int32_t *ptr, int32_t delta)
{
return __atomic_add_fetch (ptr, delta, __ATOMIC_SEQ_CST);
}
uint64_t Xadd64 (uint64_t *ptr, int64_t delta)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xadd64")
__attribute__ ((no_split_stack));
@ -170,6 +190,16 @@ Xchg (uint32_t *ptr, uint32_t new)
return __atomic_exchange_n (ptr, new, __ATOMIC_SEQ_CST);
}
int32_t Xchgint32 (int32_t *ptr, int32_t new)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xchgint32")
__attribute__ ((no_split_stack));
int32_t
Xchgint32 (int32_t *ptr, int32_t new)
{
return __atomic_exchange_n (ptr, new, __ATOMIC_SEQ_CST);
}
uint64_t Xchg64 (uint64_t *ptr, uint64_t new)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xchg64")
__attribute__ ((no_split_stack));
@ -182,6 +212,16 @@ Xchg64 (uint64_t *ptr, uint64_t new)
return __atomic_exchange_n (ptr, new, __ATOMIC_SEQ_CST);
}
int64_t Xchgint64 (int64_t *ptr, int64_t new)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xchgint64")
__attribute__ ((no_split_stack));
int64_t
Xchgint64 (int64_t *ptr, int64_t new)
{
return __atomic_exchange_n (ptr, new, __ATOMIC_SEQ_CST);
}
uintptr_t Xchguintptr (uintptr_t *ptr, uintptr_t new)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Xchguintptr")
__attribute__ ((no_split_stack));
@ -264,6 +304,26 @@ CasRel (uint32_t *ptr, uint32_t old, uint32_t new)
return __atomic_compare_exchange_n (ptr, &old, new, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
}
_Bool Casint32 (int32_t *ptr, int32_t old, int32_t new)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Casint32")
__attribute__ ((no_split_stack));
_Bool
Casint32 (int32_t *ptr, int32_t old, int32_t new)
{
return __atomic_compare_exchange_n (ptr, &old, new, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
}
_Bool Casint64 (int64_t *ptr, int64_t old, int64_t new)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Casint64")
__attribute__ ((no_split_stack));
_Bool
Casint64 (int64_t *ptr, int64_t old, int64_t new)
{
return __atomic_compare_exchange_n (ptr, &old, new, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
}
_Bool Casp1 (void **ptr, void *old, void *new)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Casp1")
__attribute__ ((no_split_stack));
@ -304,6 +364,16 @@ Store8 (uint8_t *ptr, uint8_t val)
__atomic_store_n (ptr, val, __ATOMIC_SEQ_CST);
}
void Storeint32 (int32_t *ptr, int32_t val)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Storeint32")
__attribute__ ((no_split_stack));
void
Storeint32 (int32_t *ptr, int32_t val)
{
__atomic_store_n (ptr, val, __ATOMIC_SEQ_CST);
}
void Store64 (uint64_t *ptr, uint64_t val)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Store64")
__attribute__ ((no_split_stack));
@ -338,6 +408,16 @@ StoreRel64 (uint64_t *ptr, uint64_t val)
__atomic_store_n (ptr, val, __ATOMIC_RELEASE);
}
void Storeint64 (int64_t *ptr, int64_t val)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.Storeint64")
__attribute__ ((no_split_stack));
void
Storeint64 (int64_t *ptr, int64_t val)
{
__atomic_store_n (ptr, val, __ATOMIC_SEQ_CST);
}
void StoreReluintptr (uintptr_t *ptr, uintptr_t val)
__asm__ (GOSYM_PREFIX "runtime_1internal_1atomic.StoreReluintptr")
__attribute__ ((no_split_stack));

View File

@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !wasm
// +build !wasm
package atomic
@ -14,9 +15,21 @@ func Cas(ptr *uint32, old, new uint32) bool
// NO go:noescape annotation; see atomic_pointer.go.
func Casp1(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool
//go:noescape
func Casint32(ptr *int32, old, new int32) bool
//go:noescape
func Casint64(ptr *int64, old, new int64) bool
//go:noescape
func Casuintptr(ptr *uintptr, old, new uintptr) bool
//go:noescape
func Storeint32(ptr *int32, new int32)
//go:noescape
func Storeint64(ptr *int64, new int64)
//go:noescape
func Storeuintptr(ptr *uintptr, new uintptr)
@ -28,8 +41,20 @@ func Loaduint(ptr *uint) uint
// TODO(matloob): Should these functions have the go:noescape annotation?
//go:noescape
func Loadint32(ptr *int32) int32
//go:noescape
func Loadint64(ptr *int64) int64
//go:noescape
func Xaddint32(ptr *int32, delta int32) int32
//go:noescape
func Xaddint64(ptr *int64, delta int64) int64
//go:noescape
func Xchgint32(ptr *int32, new int32) int32
//go:noescape
func Xchgint64(ptr *int64, new int64) int64