Skip to content

Commit 05cdde1

Browse files
pkedydeadprogram
authored andcommitted
Set internal linkage and keeping default visibility for anonymous functions
1 parent 25c8d3e commit 05cdde1

File tree

10 files changed

+123
-9
lines changed

10 files changed

+123
-9
lines changed

compiler/compiler.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,9 +1039,17 @@ func (b *builder) createFunctionStart() {
10391039
b.addError(b.fn.Pos(), errValue)
10401040
return
10411041
}
1042+
10421043
b.addStandardDefinedAttributes(b.llvmFn)
10431044
if !b.info.exported {
1044-
b.llvmFn.SetVisibility(llvm.HiddenVisibility)
1045+
// Do not set visibility for local linkage (internal or private).
1046+
// Otherwise a "local linkage requires default visibility"
1047+
// assertion error in llvm-project/llvm/include/llvm/IR/GlobalValue.h:236
1048+
// is thrown.
1049+
if b.llvmFn.Linkage() != llvm.InternalLinkage &&
1050+
b.llvmFn.Linkage() != llvm.PrivateLinkage {
1051+
b.llvmFn.SetVisibility(llvm.HiddenVisibility)
1052+
}
10451053
b.llvmFn.SetUnnamedAddr(true)
10461054
}
10471055
if b.info.section != "" {
@@ -1265,6 +1273,7 @@ func (b *builder) createFunction() {
12651273
// Create anonymous functions (closures etc.).
12661274
for _, sub := range b.fn.AnonFuncs {
12671275
b := newBuilder(b.compilerContext, b.Builder, sub)
1276+
b.llvmFn.SetLinkage(llvm.InternalLinkage)
12681277
b.createFunction()
12691278
}
12701279
}

compiler/testdata/basic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ entry:
196196
}
197197

198198
; Function Attrs: nounwind
199-
define hidden void @"main.foo$1"(%main.kv.0* dereferenceable_or_null(1) %b, i8* %context) unnamed_addr #1 {
199+
define internal void @"main.foo$1"(%main.kv.0* dereferenceable_or_null(1) %b, i8* %context) unnamed_addr #1 {
200200
entry:
201201
ret void
202202
}

compiler/testdata/defer-cortex-m-qemu.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ declare i8* @llvm.stacksave() #2
115115
declare void @runtime.setupDeferFrame(%runtime.deferFrame* dereferenceable_or_null(24), i8*, i8*) #0
116116

117117
; Function Attrs: nounwind
118-
define hidden void @"main.deferSimple$1"(i8* %context) unnamed_addr #1 {
118+
define internal void @"main.deferSimple$1"(i8* %context) unnamed_addr #1 {
119119
entry:
120120
call void @runtime.printint32(i32 3, i8* undef) #3
121121
ret void
@@ -246,14 +246,14 @@ rundefers.end9: ; preds = %rundefers.loophead1
246246
}
247247

248248
; Function Attrs: nounwind
249-
define hidden void @"main.deferMultiple$1"(i8* %context) unnamed_addr #1 {
249+
define internal void @"main.deferMultiple$1"(i8* %context) unnamed_addr #1 {
250250
entry:
251251
call void @runtime.printint32(i32 3, i8* undef) #3
252252
ret void
253253
}
254254

255255
; Function Attrs: nounwind
256-
define hidden void @"main.deferMultiple$2"(i8* %context) unnamed_addr #1 {
256+
define internal void @"main.deferMultiple$2"(i8* %context) unnamed_addr #1 {
257257
entry:
258258
call void @runtime.printint32(i32 5, i8* undef) #3
259259
ret void

compiler/testdata/goroutine-cortex-m-qemu-tasks.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ entry:
5151
}
5252

5353
; Function Attrs: nounwind
54-
define hidden void @"main.inlineFunctionGoroutine$1"(i32 %x, i8* %context) unnamed_addr #1 {
54+
define internal void @"main.inlineFunctionGoroutine$1"(i32 %x, i8* %context) unnamed_addr #1 {
5555
entry:
5656
ret void
5757
}
@@ -84,7 +84,7 @@ entry:
8484
}
8585

8686
; Function Attrs: nounwind
87-
define hidden void @"main.closureFunctionGoroutine$1"(i32 %x, i8* %context) unnamed_addr #1 {
87+
define internal void @"main.closureFunctionGoroutine$1"(i32 %x, i8* %context) unnamed_addr #1 {
8888
entry:
8989
%unpack.ptr = bitcast i8* %context to i32*
9090
store i32 7, i32* %unpack.ptr, align 4

compiler/testdata/goroutine-wasm-asyncify.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ entry:
5353
}
5454

5555
; Function Attrs: nounwind
56-
define hidden void @"main.inlineFunctionGoroutine$1"(i32 %x, i8* %context) unnamed_addr #1 {
56+
define internal void @"main.inlineFunctionGoroutine$1"(i32 %x, i8* %context) unnamed_addr #1 {
5757
entry:
5858
ret void
5959
}
@@ -90,7 +90,7 @@ entry:
9090
}
9191

9292
; Function Attrs: nounwind
93-
define hidden void @"main.closureFunctionGoroutine$1"(i32 %x, i8* %context) unnamed_addr #1 {
93+
define internal void @"main.closureFunctionGoroutine$1"(i32 %x, i8* %context) unnamed_addr #1 {
9494
entry:
9595
%unpack.ptr = bitcast i8* %context to i32*
9696
store i32 7, i32* %unpack.ptr, align 4

testdata/generics.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package main
22

3+
import (
4+
"github.com/tinygo-org/tinygo/testdata/generics/testa"
5+
"github.com/tinygo-org/tinygo/testdata/generics/testb"
6+
)
7+
38
func main() {
49
println("add:", Add(3, 5))
510
println("add:", Add(int8(3), 5))
611

712
var c C[int]
813
c.F() // issue 2951
14+
15+
testa.Test()
16+
testb.Test()
917
}
1018

1119
type Integer interface {

testdata/generics.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
add: 8
22
add: 8
3+
value: 101
4+
value: 101
5+
value: 501
6+
value: 501

testdata/generics/testa/testa.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package testa
2+
3+
import (
4+
"github.com/tinygo-org/tinygo/testdata/generics/value"
5+
)
6+
7+
func Test() {
8+
v := value.New(1)
9+
vm := value.Map(v, Plus100)
10+
vm.Get(callback, callback)
11+
}
12+
13+
func callback(v int) {
14+
println("value:", v)
15+
}
16+
17+
// Plus100 is a `Transform` that adds 100 to `value`.
18+
func Plus100(value int) int {
19+
return value + 100
20+
}

testdata/generics/testb/testb.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package testb
2+
3+
import (
4+
"github.com/tinygo-org/tinygo/testdata/generics/value"
5+
)
6+
7+
func Test() {
8+
v := value.New(1)
9+
vm := value.Map(v, Plus500)
10+
vm.Get(callback, callback)
11+
}
12+
13+
func callback(v int) {
14+
println("value:", v)
15+
}
16+
17+
// Plus500 is a `Transform` that adds 500 to `value`.
18+
func Plus500(value int) int {
19+
return value + 500
20+
}

testdata/generics/value/value.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package value
2+
3+
type (
4+
Value[T any] interface {
5+
Get(Callback[T], Callback[T])
6+
}
7+
8+
Callback[T any] func(T)
9+
10+
Transform[S any, D any] func(S) D
11+
)
12+
13+
func New[T any](v T) Value[T] {
14+
return &value[T]{
15+
v: v,
16+
}
17+
}
18+
19+
type value[T any] struct {
20+
v T
21+
}
22+
23+
func (v *value[T]) Get(fn1, fn2 Callback[T]) {
24+
// For example purposes.
25+
// Normally would be asynchronous callback.
26+
fn1(v.v)
27+
fn2(v.v)
28+
}
29+
30+
func Map[S, D any](v Value[S], tx Transform[S, D]) Value[D] {
31+
return &mapper[S, D]{
32+
v: v,
33+
tx: tx,
34+
}
35+
}
36+
37+
type mapper[S, D any] struct {
38+
v Value[S]
39+
tx Transform[S, D]
40+
}
41+
42+
func (m *mapper[S, D]) Get(fn1, fn2 Callback[D]) {
43+
// two callbacks are passed to generate more than
44+
// one anonymous function symbol name.
45+
m.v.Get(func(v S) {
46+
// anonymous function inside of anonymous function.
47+
func() {
48+
fn1(m.tx(v))
49+
}()
50+
}, func(v S) {
51+
fn2(m.tx(v))
52+
})
53+
}

0 commit comments

Comments
 (0)