Skip to content

Commit 584e94c

Browse files
aykevldeadprogram
authored andcommitted
transform: allow updating tests with -update flag
This should make it much easier to update existing tests.
1 parent 5308e89 commit 584e94c

File tree

5 files changed

+73
-44
lines changed

5 files changed

+73
-44
lines changed

transform/testdata/allocs.out.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ define void @testNonEscapingLoop() {
5454
entry:
5555
%stackalloc.alloca = alloca [1 x i32]
5656
br label %loop
57-
loop:
57+
58+
loop: ; preds = %loop, %entry
5859
store [1 x i32] zeroinitializer, [1 x i32]* %stackalloc.alloca
5960
%stackalloc = bitcast [1 x i32]* %stackalloc.alloca to i32*
6061
%0 = call i32* @noescapeIntPtr(i32* %stackalloc)
6162
%1 = icmp eq i32* null, %0
6263
br i1 %1, label %loop, label %end
63-
end:
64+
65+
end: ; preds = %loop
6466
ret void
6567
}
6668

transform/testdata/coroutines.out.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,33 @@ target triple = "armv7m-none-eabi"
55
%"internal/task.state" = type { i8* }
66

77
declare void @"internal/task.start"(i32, i8*, i8*, i8*)
8+
89
declare void @"internal/task.Pause"(i8*, i8*)
910

1011
declare void @runtime.scheduler(i8*, i8*)
1112

1213
declare i8* @runtime.alloc(i32, i8*, i8*)
14+
1315
declare void @runtime.free(i8*, i8*, i8*)
1416

1517
declare %"internal/task.Task"* @"internal/task.Current"(i8*, i8*)
1618

1719
declare i8* @"(*internal/task.Task).setState"(%"internal/task.Task"*, i8*, i8*, i8*)
1820

1921
declare void @"(*internal/task.Task).setReturnPtr"(%"internal/task.Task"*, i8*, i8*, i8*)
22+
2023
declare i8* @"(*internal/task.Task).getReturnPtr"(%"internal/task.Task"*, i8*, i8*)
2124

2225
declare void @"(*internal/task.Task).returnTo"(%"internal/task.Task"*, i8*, i8*, i8*)
26+
2327
declare void @"(*internal/task.Task).returnCurrent"(%"internal/task.Task"*, i8*, i8*)
2428

2529
declare %"internal/task.Task"* @"internal/task.createTask"(i8*, i8*)
2630

2731
declare void @callMain(i8*, i8*)
2832

2933
declare void @enqueueTimer(%"internal/task.Task"*, i64, i8*, i8*)
34+
3035
define void @sleep(i64, i8*, i8* %parentHandle) {
3136
entry:
3237
%task.current = bitcast i8* %parentHandle to %"internal/task.Task"*
@@ -159,15 +164,31 @@ entry:
159164
ret void
160165
}
161166

167+
; Function Attrs: argmemonly nounwind readonly
162168
declare token @llvm.coro.id(i32, i8* readnone, i8* nocapture readonly, i8*) #0
169+
170+
; Function Attrs: nounwind readnone
163171
declare i32 @llvm.coro.size.i32() #1
172+
173+
; Function Attrs: nounwind
164174
declare i8* @llvm.coro.begin(token, i8* writeonly) #2
175+
176+
; Function Attrs: nounwind
165177
declare i8 @llvm.coro.suspend(token, i1) #2
178+
179+
; Function Attrs: nounwind
166180
declare i1 @llvm.coro.end(i8*, i1) #2
181+
182+
; Function Attrs: argmemonly nounwind readonly
167183
declare i8* @llvm.coro.free(token, i8* nocapture readonly) #0
184+
185+
; Function Attrs: nounwind
168186
declare token @llvm.coro.save(i8*) #2
169187

188+
; Function Attrs: argmemonly nounwind
170189
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #3
190+
191+
; Function Attrs: argmemonly nounwind
171192
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #3
172193

173194
attributes #0 = { argmemonly nounwind readonly }

transform/testdata/func-lowering.out.ll

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,63 +29,56 @@ declare void @func1Uint8(i8, i8*, i8*)
2929

3030
declare void @func2Uint8(i8, i8*, i8*)
3131

32-
; Call a function of which only one function with this signature is used as a
33-
; function value. This means that lowering it to IR is trivial: simply check
34-
; whether the func value is nil, and if not, call that one function directly.
3532
define void @runFunc1(i8*, i32, i8, i8* %context, i8* %parentHandle) {
3633
entry:
3734
%3 = icmp eq i32 %1, 0
3835
%4 = select i1 %3, void (i8, i8*, i8*)* null, void (i8, i8*, i8*)* @funcInt8
3936
%5 = icmp eq void (i8, i8*, i8*)* %4, null
4037
br i1 %5, label %fpcall.nil, label %fpcall.next
4138

42-
fpcall.nil:
39+
fpcall.nil: ; preds = %entry
4340
call void @runtime.nilPanic(i8* undef, i8* null)
4441
unreachable
4542

46-
fpcall.next:
43+
fpcall.next: ; preds = %entry
4744
call void %4(i8 %2, i8* %0, i8* undef)
4845
ret void
4946
}
5047

51-
; There are two functions with this signature used in a func value. That means
52-
; that we'll have to check at runtime which of the two it is (or whether the
53-
; func value is nil). This call will thus be lowered to a switch statement.
5448
define void @runFunc2(i8*, i32, i8, i8* %context, i8* %parentHandle) {
5549
entry:
5650
br i1 false, label %fpcall.nil, label %fpcall.next
5751

58-
fpcall.nil:
52+
fpcall.nil: ; preds = %entry
5953
call void @runtime.nilPanic(i8* undef, i8* null)
6054
unreachable
6155

62-
fpcall.next:
56+
fpcall.next: ; preds = %entry
6357
switch i32 %1, label %func.default [
6458
i32 0, label %func.nil
6559
i32 1, label %func.call1
6660
i32 2, label %func.call2
6761
]
6862

69-
func.nil:
63+
func.nil: ; preds = %fpcall.next
7064
call void @runtime.nilPanic(i8* undef, i8* null)
7165
unreachable
7266

73-
func.call1:
67+
func.call1: ; preds = %fpcall.next
7468
call void @func1Uint8(i8 %2, i8* %0, i8* undef)
7569
br label %func.next
7670

77-
func.call2:
71+
func.call2: ; preds = %fpcall.next
7872
call void @func2Uint8(i8 %2, i8* %0, i8* undef)
7973
br label %func.next
8074

81-
func.next:
75+
func.next: ; preds = %func.call2, %func.call1
8276
ret void
8377

84-
func.default:
78+
func.default: ; preds = %fpcall.next
8579
unreachable
8680
}
8781

88-
; Special case for runtime.makeGoroutine.
8982
define void @sleepFuncValue(i8*, i32, i8* nocapture readnone %context, i8* nocapture readnone %parentHandle) {
9083
entry:
9184
switch i32 %1, label %func.default [
@@ -94,21 +87,21 @@ entry:
9487
i32 2, label %func.call2
9588
]
9689

97-
func.nil:
90+
func.nil: ; preds = %entry
9891
call void @runtime.nilPanic(i8* undef, i8* null)
9992
unreachable
10093

101-
func.call1:
94+
func.call1: ; preds = %entry
10295
call void @"internal/task.start"(i32 ptrtoint (void (i32, i8*, i8*)* @"main$1" to i32), i8* null, i8* undef, i8* null)
10396
br label %func.next
10497

105-
func.call2:
98+
func.call2: ; preds = %entry
10699
call void @"internal/task.start"(i32 ptrtoint (void (i32, i8*, i8*)* @"main$2" to i32), i8* null, i8* undef, i8* null)
107100
br label %func.next
108101

109-
func.next:
102+
func.next: ; preds = %func.call2, %func.call1
110103
ret void
111104

112-
func.default:
105+
func.default: ; preds = %entry
113106
unreachable
114107
}

transform/testdata/interface.out.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,32 @@ define void @printInterface(i32 %typecode, i8* %value) {
3636
%typeassert.ok1 = call i1 @"Unmatched$typeassert"(i32 %typecode)
3737
br i1 %typeassert.ok1, label %typeswitch.Unmatched, label %typeswitch.notUnmatched
3838

39-
typeswitch.Unmatched:
39+
typeswitch.Unmatched: ; preds = %0
4040
%unmatched = ptrtoint i8* %value to i32
4141
call void @runtime.printptr(i32 %unmatched)
4242
call void @runtime.printnl()
4343
ret void
4444

45-
typeswitch.notUnmatched:
45+
typeswitch.notUnmatched: ; preds = %0
4646
%typeassert.ok = call i1 @"Doubler$typeassert"(i32 %typecode)
4747
br i1 %typeassert.ok, label %typeswitch.Doubler, label %typeswitch.notDoubler
4848

49-
typeswitch.Doubler:
49+
typeswitch.Doubler: ; preds = %typeswitch.notUnmatched
5050
%doubler.result = call i32 @"(Number).Double$invoke"(i8* %value, i8* null)
5151
call void @runtime.printint32(i32 %doubler.result)
5252
ret void
5353

54-
typeswitch.notDoubler:
54+
typeswitch.notDoubler: ; preds = %typeswitch.notUnmatched
5555
%typeassert.ok2 = icmp eq i32 16, %typecode
5656
br i1 %typeassert.ok2, label %typeswitch.byte, label %typeswitch.notByte
5757

58-
typeswitch.byte:
58+
typeswitch.byte: ; preds = %typeswitch.notDoubler
5959
%byte = ptrtoint i8* %value to i8
6060
call void @runtime.printuint8(i8 %byte)
6161
call void @runtime.printnl()
6262
ret void
6363

64-
typeswitch.notByte:
64+
typeswitch.notByte: ; preds = %typeswitch.notDoubler
6565
ret void
6666
}
6767

@@ -82,10 +82,10 @@ entry:
8282
i32 68, label %then
8383
]
8484

85-
then:
85+
then: ; preds = %entry
8686
ret i1 true
8787

88-
else:
88+
else: ; preds = %entry
8989
ret i1 false
9090
}
9191

@@ -94,9 +94,9 @@ entry:
9494
switch i32 %actualType, label %else [
9595
]
9696

97-
then:
97+
then: ; No predecessors!
9898
ret i1 true
9999

100-
else:
100+
else: ; preds = %entry
101101
ret i1 false
102102
}

transform/transform_test.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package transform
33
// This file defines some helper functions for testing transforms.
44

55
import (
6+
"flag"
67
"io/ioutil"
78
"os"
89
"strings"
@@ -11,6 +12,8 @@ import (
1112
"tinygo.org/x/go-llvm"
1213
)
1314

15+
var update = flag.Bool("update", false, "update transform package tests")
16+
1417
// testTransform runs a transformation pass on an input file (pathPrefix+".ll")
1518
// and checks whether it matches the expected output (pathPrefix+".out.ll"). The
1619
// output is compared with a fuzzy match that ignores some irrelevant lines such
@@ -31,18 +34,28 @@ func testTransform(t *testing.T, pathPrefix string, transform func(mod llvm.Modu
3134
// Perform the transform.
3235
transform(mod)
3336

34-
// Read the expected output IR.
35-
out, err := ioutil.ReadFile(pathPrefix + ".out.ll")
36-
if err != nil {
37-
t.Fatalf("could not read output file %s: %v", pathPrefix+".out.ll", err)
38-
}
39-
40-
// See whether the transform output matches with the expected output IR.
41-
expected := string(out)
37+
// Get the output from the test and filter some irrelevant lines.
4238
actual := mod.String()
43-
if !fuzzyEqualIR(expected, actual) {
44-
t.Logf("output does not match expected output:\n%s", actual)
45-
t.Fail()
39+
actual = actual[strings.Index(actual, "\ntarget datalayout = ")+1:]
40+
41+
if *update {
42+
err := ioutil.WriteFile(pathPrefix+".out.ll", []byte(actual), 0666)
43+
if err != nil {
44+
t.Error("failed to write out new output:", err)
45+
}
46+
} else {
47+
// Read the expected output IR.
48+
out, err := ioutil.ReadFile(pathPrefix + ".out.ll")
49+
if err != nil {
50+
t.Fatalf("could not read output file %s: %v", pathPrefix+".out.ll", err)
51+
}
52+
53+
// See whether the transform output matches with the expected output IR.
54+
expected := string(out)
55+
if !fuzzyEqualIR(expected, actual) {
56+
t.Logf("output does not match expected output:\n%s", actual)
57+
t.Fail()
58+
}
4659
}
4760
}
4861

0 commit comments

Comments
 (0)