Skip to content

Commit 1ceb63d

Browse files
aykevldeadprogram
authored andcommitted
compiler: really define runtime/volatile.* functions
This makes them available to deferred calls, among others.
1 parent e1052f9 commit 1ceb63d

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

compiler/compiler.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,10 +1642,6 @@ func (b *builder) createFunctionCall(instr *ssa.CallCommon) (llvm.Value, error)
16421642
return b.createSyscall(instr)
16431643
case strings.HasPrefix(name, "syscall.rawSyscallNoError"):
16441644
return b.createRawSyscallNoError(instr)
1645-
case strings.HasPrefix(name, "runtime/volatile.Load"):
1646-
return b.createVolatileLoad(instr)
1647-
case strings.HasPrefix(name, "runtime/volatile.Store"):
1648-
return b.createVolatileStore(instr)
16491645
case name == "runtime.supportsRecover":
16501646
supportsRecover := uint64(0)
16511647
if b.supportsRecover() {

compiler/intrinsics.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
func (b *builder) defineIntrinsicFunction() {
2121
name := b.fn.RelString(nil)
2222
switch {
23+
case strings.HasPrefix(name, "runtime/volatile.Load"):
24+
b.createVolatileLoad()
25+
case strings.HasPrefix(name, "runtime/volatile.Store"):
26+
b.createVolatileStore()
2327
case strings.HasPrefix(name, "sync/atomic.") && token.IsExported(b.fn.Name()):
2428
b.createFunctionStart()
2529
returnValue := b.createAtomicOp(b.fn.Name())

compiler/volatile.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,25 @@ package compiler
33
// This file implements volatile loads/stores in runtime/volatile.LoadT and
44
// runtime/volatile.StoreT as compiler builtins.
55

6-
import (
7-
"golang.org/x/tools/go/ssa"
8-
"tinygo.org/x/go-llvm"
9-
)
10-
116
// createVolatileLoad is the implementation of the intrinsic function
127
// runtime/volatile.LoadT().
13-
func (b *builder) createVolatileLoad(instr *ssa.CallCommon) (llvm.Value, error) {
14-
addr := b.getValue(instr.Args[0])
15-
b.createNilCheck(instr.Args[0], addr, "deref")
8+
func (b *builder) createVolatileLoad() {
9+
b.createFunctionStart()
10+
addr := b.getValue(b.fn.Params[0])
11+
b.createNilCheck(b.fn.Params[0], addr, "deref")
1612
val := b.CreateLoad(addr, "")
1713
val.SetVolatile(true)
18-
return val, nil
14+
b.CreateRet(val)
1915
}
2016

2117
// createVolatileStore is the implementation of the intrinsic function
2218
// runtime/volatile.StoreT().
23-
func (b *builder) createVolatileStore(instr *ssa.CallCommon) (llvm.Value, error) {
24-
addr := b.getValue(instr.Args[0])
25-
val := b.getValue(instr.Args[1])
26-
b.createNilCheck(instr.Args[0], addr, "deref")
19+
func (b *builder) createVolatileStore() {
20+
b.createFunctionStart()
21+
addr := b.getValue(b.fn.Params[0])
22+
val := b.getValue(b.fn.Params[1])
23+
b.createNilCheck(b.fn.Params[0], addr, "deref")
2724
store := b.CreateStore(val, addr)
2825
store.SetVolatile(true)
29-
return llvm.Value{}, nil
26+
b.CreateRetVoid()
3027
}

testdata/atomic.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"sync/atomic"
55
"unsafe"
6+
7+
"runtime/volatile"
68
)
79

810
func main() {
@@ -82,7 +84,7 @@ func main() {
8284
testValue(int(3), int(-2))
8385
testValue("", "foobar", "baz")
8486

85-
// Test atomic operations as deferred values.
87+
// Test atomic and volatile operations as deferred values.
8688
testDefer()
8789
}
8890

@@ -99,8 +101,11 @@ func testValue(values ...interface{}) {
99101

100102
func testDefer() {
101103
n1 := int32(5)
104+
n2 := uint32(6)
102105
defer func() {
103106
println("deferred atomic add:", n1)
107+
println("deferred volatile store:", n2)
104108
}()
105109
defer atomic.AddInt32(&n1, 3)
110+
defer volatile.StoreUint32(&n2, 22)
106111
}

testdata/atomic.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ StoreUint64: 20
3434
StoreUintptr: 20
3535
StorePointer: true
3636
deferred atomic add: 8
37+
deferred volatile store: 22

0 commit comments

Comments
 (0)