Skip to content

Commit 408855d

Browse files
aykevldeadprogram
authored andcommitted
compiler: add generics test case
This test case shows a few bugs, that I will fix in subsequent patches.
1 parent 58072a5 commit 408855d

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

compiler/compiler_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ func TestCompiler(t *testing.T) {
6565
if goMinor >= 17 {
6666
tests = append(tests, testCase{"go1.17.go", "", ""})
6767
}
68+
if goMinor >= 18 {
69+
tests = append(tests, testCase{"generics.go", "", ""})
70+
}
6871

6972
for _, tc := range tests {
7073
name := tc.file

compiler/testdata/generics.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
type Coord interface {
4+
int | float32
5+
}
6+
7+
type Point[T Coord] struct {
8+
X, Y T
9+
}
10+
11+
func Add[T Coord](a, b Point[T]) Point[T] {
12+
return Point[T]{
13+
X: a.X + b.X,
14+
Y: a.Y + b.Y,
15+
}
16+
}
17+
18+
func main() {
19+
var af, bf Point[float32]
20+
Add(af, bf)
21+
}

compiler/testdata/generics.ll

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
; ModuleID = 'generics.go'
2+
source_filename = "generics.go"
3+
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
4+
target triple = "wasm32-unknown-wasi"
5+
6+
%main.Point = type { %runtime._interface, %runtime._interface }
7+
%runtime._interface = type { i32, i8* }
8+
%main.Point.0 = type { float, float }
9+
10+
declare noalias nonnull i8* @runtime.alloc(i32, i8*, i8*) #0
11+
12+
declare void @runtime.trackPointer(i8* nocapture readonly, i8*) #0
13+
14+
; Function Attrs: nounwind
15+
define hidden void @main.init(i8* %context) unnamed_addr #1 {
16+
entry:
17+
ret void
18+
}
19+
20+
declare %main.Point @main.Add(%main.Point, %main.Point, i8*) #0
21+
22+
; Function Attrs: nounwind
23+
define hidden void @main.main(i8* %context) unnamed_addr #1 {
24+
entry:
25+
%bf = alloca %main.Point.0, align 8
26+
%af = alloca %main.Point.0, align 8
27+
%af.repack = getelementptr inbounds %main.Point.0, %main.Point.0* %af, i32 0, i32 0
28+
store float 0.000000e+00, float* %af.repack, align 8
29+
%af.repack1 = getelementptr inbounds %main.Point.0, %main.Point.0* %af, i32 0, i32 1
30+
store float 0.000000e+00, float* %af.repack1, align 4
31+
%0 = bitcast %main.Point.0* %af to i8*
32+
call void @runtime.trackPointer(i8* nonnull %0, i8* undef) #2
33+
%bf.repack = getelementptr inbounds %main.Point.0, %main.Point.0* %bf, i32 0, i32 0
34+
store float 0.000000e+00, float* %bf.repack, align 8
35+
%bf.repack2 = getelementptr inbounds %main.Point.0, %main.Point.0* %bf, i32 0, i32 1
36+
store float 0.000000e+00, float* %bf.repack2, align 4
37+
%1 = bitcast %main.Point.0* %bf to i8*
38+
call void @runtime.trackPointer(i8* nonnull %1, i8* undef) #2
39+
%.elt = getelementptr inbounds %main.Point.0, %main.Point.0* %af, i32 0, i32 0
40+
%.unpack = load float, float* %.elt, align 8
41+
%.elt3 = getelementptr inbounds %main.Point.0, %main.Point.0* %af, i32 0, i32 1
42+
%.unpack4 = load float, float* %.elt3, align 4
43+
%.elt5 = getelementptr inbounds %main.Point.0, %main.Point.0* %bf, i32 0, i32 0
44+
%.unpack6 = load float, float* %.elt5, align 8
45+
%.elt7 = getelementptr inbounds %main.Point.0, %main.Point.0* %bf, i32 0, i32 1
46+
%.unpack8 = load float, float* %.elt7, align 4
47+
%2 = call %main.Point.0 @"main.Add[float32]"(float %.unpack, float %.unpack4, float %.unpack6, float %.unpack8, i8* undef)
48+
ret void
49+
}
50+
51+
; Function Attrs: nounwind
52+
define linkonce_odr hidden %main.Point.0 @"main.Add[float32]"(float %a.X, float %a.Y, float %b.X, float %b.Y, i8* %context) unnamed_addr #1 {
53+
entry:
54+
%complit = alloca %main.Point.0, align 8
55+
%b = alloca %main.Point.0, align 8
56+
%a = alloca %main.Point.0, align 8
57+
%a.repack = getelementptr inbounds %main.Point.0, %main.Point.0* %a, i32 0, i32 0
58+
store float 0.000000e+00, float* %a.repack, align 8
59+
%a.repack9 = getelementptr inbounds %main.Point.0, %main.Point.0* %a, i32 0, i32 1
60+
store float 0.000000e+00, float* %a.repack9, align 4
61+
%0 = bitcast %main.Point.0* %a to i8*
62+
call void @runtime.trackPointer(i8* nonnull %0, i8* undef) #2
63+
%a.repack10 = getelementptr inbounds %main.Point.0, %main.Point.0* %a, i32 0, i32 0
64+
store float %a.X, float* %a.repack10, align 8
65+
%a.repack11 = getelementptr inbounds %main.Point.0, %main.Point.0* %a, i32 0, i32 1
66+
store float %a.Y, float* %a.repack11, align 4
67+
%b.repack = getelementptr inbounds %main.Point.0, %main.Point.0* %b, i32 0, i32 0
68+
store float 0.000000e+00, float* %b.repack, align 8
69+
%b.repack13 = getelementptr inbounds %main.Point.0, %main.Point.0* %b, i32 0, i32 1
70+
store float 0.000000e+00, float* %b.repack13, align 4
71+
%1 = bitcast %main.Point.0* %b to i8*
72+
call void @runtime.trackPointer(i8* nonnull %1, i8* undef) #2
73+
%b.repack14 = getelementptr inbounds %main.Point.0, %main.Point.0* %b, i32 0, i32 0
74+
store float %b.X, float* %b.repack14, align 8
75+
%b.repack15 = getelementptr inbounds %main.Point.0, %main.Point.0* %b, i32 0, i32 1
76+
store float %b.Y, float* %b.repack15, align 4
77+
%complit.repack = getelementptr inbounds %main.Point.0, %main.Point.0* %complit, i32 0, i32 0
78+
store float 0.000000e+00, float* %complit.repack, align 8
79+
%complit.repack17 = getelementptr inbounds %main.Point.0, %main.Point.0* %complit, i32 0, i32 1
80+
store float 0.000000e+00, float* %complit.repack17, align 4
81+
%2 = bitcast %main.Point.0* %complit to i8*
82+
call void @runtime.trackPointer(i8* nonnull %2, i8* undef) #2
83+
%3 = getelementptr inbounds %main.Point.0, %main.Point.0* %complit, i32 0, i32 0
84+
br i1 false, label %deref.throw, label %deref.next
85+
86+
deref.next: ; preds = %entry
87+
br i1 false, label %deref.throw1, label %deref.next2
88+
89+
deref.next2: ; preds = %deref.next
90+
%4 = getelementptr inbounds %main.Point.0, %main.Point.0* %b, i32 0, i32 0
91+
%5 = getelementptr inbounds %main.Point.0, %main.Point.0* %a, i32 0, i32 0
92+
%6 = load float, float* %5, align 8
93+
%7 = load float, float* %4, align 8
94+
%8 = fadd float %6, %7
95+
br i1 false, label %deref.throw3, label %deref.next4
96+
97+
deref.next4: ; preds = %deref.next2
98+
br i1 false, label %deref.throw5, label %deref.next6
99+
100+
deref.next6: ; preds = %deref.next4
101+
%9 = getelementptr inbounds %main.Point.0, %main.Point.0* %b, i32 0, i32 1
102+
%10 = getelementptr inbounds %main.Point.0, %main.Point.0* %a, i32 0, i32 1
103+
%11 = load float, float* %10, align 4
104+
%12 = load float, float* %9, align 4
105+
br i1 false, label %store.throw, label %store.next
106+
107+
store.next: ; preds = %deref.next6
108+
store float %8, float* %3, align 8
109+
br i1 false, label %store.throw7, label %store.next8
110+
111+
store.next8: ; preds = %store.next
112+
%13 = getelementptr inbounds %main.Point.0, %main.Point.0* %complit, i32 0, i32 1
113+
%14 = fadd float %11, %12
114+
store float %14, float* %13, align 4
115+
%.elt = getelementptr inbounds %main.Point.0, %main.Point.0* %complit, i32 0, i32 0
116+
%.unpack = load float, float* %.elt, align 8
117+
%15 = insertvalue %main.Point.0 undef, float %.unpack, 0
118+
%16 = insertvalue %main.Point.0 %15, float %14, 1
119+
ret %main.Point.0 %16
120+
121+
deref.throw: ; preds = %entry
122+
unreachable
123+
124+
deref.throw1: ; preds = %deref.next
125+
unreachable
126+
127+
deref.throw3: ; preds = %deref.next2
128+
unreachable
129+
130+
deref.throw5: ; preds = %deref.next4
131+
unreachable
132+
133+
store.throw: ; preds = %deref.next6
134+
unreachable
135+
136+
store.throw7: ; preds = %store.next
137+
unreachable
138+
}
139+
140+
declare void @runtime.nilPanic(i8*) #0
141+
142+
attributes #0 = { "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
143+
attributes #1 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
144+
attributes #2 = { nounwind }

0 commit comments

Comments
 (0)