atomic Load/Store

This commit is contained in:
xushiwei
2024-06-16 16:35:46 +08:00
parent fbd15a81b4
commit 340b5bd165
9 changed files with 236 additions and 156 deletions

View File

@@ -2,20 +2,24 @@ package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/sync"
"github.com/goplus/llgo/c/sync/atomic"
)
func main() {
var v int64 = 100
sync.FetchAndAdd(&v, 1)
c.Printf(c.Str("%ld\n"), v)
var v int64
sync.CompareAndXchg(&v, 100, 102)
c.Printf(c.Str("%ld\n"), v)
atomic.Store(&v, 100)
println("store:", atomic.Load(&v))
sync.CompareAndXchg(&v, 101, 102)
c.Printf(c.Str("%ld\n"), v)
atomic.Add(&v, 1)
c.Printf(c.Str("v: %ld\n"), v)
sync.FetchAndSub(&v, 1)
c.Printf(c.Str("%ld\n"), v)
atomic.CompareAndExchange(&v, 100, 102)
c.Printf(c.Str("v: %ld\n"), v)
atomic.CompareAndExchange(&v, 101, 102)
c.Printf(c.Str("v: %ld\n"), v)
atomic.Sub(&v, 1)
c.Printf(c.Str("v: %ld\n"), v)
}

View File

@@ -1,13 +1,16 @@
; ModuleID = 'main'
source_filename = "main"
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
@0 = private unnamed_addr constant [5 x i8] c"%ld\0A\00", align 1
@1 = private unnamed_addr constant [5 x i8] c"%ld\0A\00", align 1
@2 = private unnamed_addr constant [5 x i8] c"%ld\0A\00", align 1
@3 = private unnamed_addr constant [5 x i8] c"%ld\0A\00", align 1
@0 = private unnamed_addr constant [6 x i8] c"store:", align 1
@1 = private unnamed_addr constant [8 x i8] c"v: %ld\0A\00", align 1
@2 = private unnamed_addr constant [8 x i8] c"v: %ld\0A\00", align 1
@3 = private unnamed_addr constant [8 x i8] c"v: %ld\0A\00", align 1
@4 = private unnamed_addr constant [8 x i8] c"v: %ld\0A\00", align 1
define void @main.init() {
_llgo_0:
@@ -29,19 +32,30 @@ _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 8)
store i64 100, ptr %2, align 4
%3 = atomicrmw add ptr %2, i64 1 seq_cst, align 8
%4 = load i64, ptr %2, align 4
%5 = call i32 (ptr, ...) @printf(ptr @0, i64 %4)
%6 = cmpxchg ptr %2, i64 100, i64 102 seq_cst seq_cst, align 8
%7 = load i64, ptr %2, align 4
%8 = call i32 (ptr, ...) @printf(ptr @1, i64 %7)
%9 = cmpxchg ptr %2, i64 101, i64 102 seq_cst seq_cst, align 8
%10 = load i64, ptr %2, align 4
%11 = call i32 (ptr, ...) @printf(ptr @2, i64 %10)
%12 = atomicrmw sub ptr %2, i64 1 seq_cst, align 8
%13 = load i64, ptr %2, align 4
%14 = call i32 (ptr, ...) @printf(ptr @3, i64 %13)
store atomic i64 100, ptr %2 seq_cst, align 4
%3 = load atomic i64, ptr %2 seq_cst, align 4
%4 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %4, i32 0, i32 0
store ptr @0, ptr %5, align 8
%6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %4, i32 0, i32 1
store i64 6, ptr %6, align 4
%7 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %4, align 8
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %7)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %3)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
%8 = atomicrmw add ptr %2, i64 1 seq_cst, align 8
%9 = load i64, ptr %2, align 4
%10 = call i32 (ptr, ...) @printf(ptr @1, i64 %9)
%11 = cmpxchg ptr %2, i64 100, i64 102 seq_cst seq_cst, align 8
%12 = load i64, ptr %2, align 4
%13 = call i32 (ptr, ...) @printf(ptr @2, i64 %12)
%14 = cmpxchg ptr %2, i64 101, i64 102 seq_cst seq_cst, align 8
%15 = load i64, ptr %2, align 4
%16 = call i32 (ptr, ...) @printf(ptr @3, i64 %15)
%17 = atomicrmw sub ptr %2, i64 1 seq_cst, align 8
%18 = load i64, ptr %2, align 4
%19 = call i32 (ptr, ...) @printf(ptr @4, i64 %18)
ret i32 0
}
@@ -49,4 +63,10 @@ declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String")
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)
declare i32 @printf(ptr, ...)

View File

@@ -318,18 +318,21 @@ var llgoInstrs = map[string]int{
"deferData": llgoDeferData,
"unreachable": llgoUnreachable,
"atomicLoad": llgoAtomicLoad,
"atomicStore": llgoAtomicStore,
"atomicCmpXchg": llgoAtomicCmpXchg,
"atomicXchg": int(llgoAtomicXchg),
"atomicAdd": int(llgoAtomicAdd),
"atomicSub": int(llgoAtomicSub),
"atomicAnd": int(llgoAtomicAnd),
"atomicNand": int(llgoAtomicNand),
"atomicOr": int(llgoAtomicOr),
"atomicXor": int(llgoAtomicXor),
"atomicMax": int(llgoAtomicMax),
"atomicMin": int(llgoAtomicMin),
"atomicUMax": int(llgoAtomicUMax),
"atomicUMin": int(llgoAtomicUMin),
"atomicXchg": int(llgoAtomicXchg),
"atomicAdd": int(llgoAtomicAdd),
"atomicSub": int(llgoAtomicSub),
"atomicAnd": int(llgoAtomicAnd),
"atomicNand": int(llgoAtomicNand),
"atomicOr": int(llgoAtomicOr),
"atomicXor": int(llgoAtomicXor),
"atomicMax": int(llgoAtomicMax),
"atomicMin": int(llgoAtomicMin),
"atomicUMax": int(llgoAtomicUMax),
"atomicUMin": int(llgoAtomicUMin),
}
// funcOf returns a function by name and set ftype = goFunc, cFunc, etc.
@@ -564,6 +567,24 @@ func (p *context) atomic(b llssa.Builder, op llssa.AtomicOp, args []ssa.Value) (
panic("atomicOp(addr *T, val T) T: invalid arguments")
}
func (p *context) atomicLoad(b llssa.Builder, args []ssa.Value) llssa.Expr {
if len(args) == 1 {
addr := p.compileValue(b, args[0])
return b.Load(addr).SetOrdering(llssa.OrderingSeqConsistent)
}
panic("atomicLoad(addr *T) T: invalid arguments")
}
func (p *context) atomicStore(b llssa.Builder, args []ssa.Value) {
if len(args) == 2 {
addr := p.compileValue(b, args[0])
val := p.compileValue(b, args[1])
b.Store(addr, val).SetOrdering(llssa.OrderingSeqConsistent)
return
}
panic("atomicStore(addr *T, val T) T: invalid arguments")
}
func (p *context) atomicCmpXchg(b llssa.Builder, args []ssa.Value) llssa.Expr {
if len(args) == 3 {
addr := p.compileValue(b, args[0])
@@ -677,6 +698,10 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon
ret = p.allocaCStr(b, args)
case llgoStringData:
ret = p.stringData(b, args)
case llgoAtomicLoad:
ret = p.atomicLoad(b, args)
case llgoAtomicStore:
p.atomicStore(b, args)
case llgoAtomicCmpXchg:
ret = p.atomicCmpXchg(b, args)
case llgoSigsetjmp:

View File

@@ -343,14 +343,18 @@ const (
llgoAdvance = llgoInstrBase + 4
llgoIndex = llgoInstrBase + 5
llgoStringData = llgoInstrBase + 6
llgoPyList = llgoInstrBase + 7
llgoSigjmpbuf = llgoInstrBase + 0xa
llgoSigsetjmp = llgoInstrBase + 0xb
llgoSiglongjmp = llgoInstrBase + 0xc
llgoDeferData = llgoInstrBase + 0xd
llgoDeferData = llgoInstrBase + 7
llgoAtomicCmpXchg = llgoInstrBase + 0xf
llgoAtomicOpBase = llgoInstrBase + 0x10
llgoSigjmpbuf = llgoInstrBase + 0xa
llgoSigsetjmp = llgoInstrBase + 0xb
llgoSiglongjmp = llgoInstrBase + 0xc
llgoPyList = llgoInstrBase + 0x10
llgoAtomicLoad = llgoInstrBase + 0x1d
llgoAtomicStore = llgoInstrBase + 0x1e
llgoAtomicCmpXchg = llgoInstrBase + 0x1f
llgoAtomicOpBase = llgoInstrBase + 0x20
llgoAtomicXchg = llgoAtomicOpBase + llssa.OpXchg
llgoAtomicAdd = llgoAtomicOpBase + llssa.OpAdd