Skip to content

Commit 293f4ea

Browse files
aykevldeadprogram
authored andcommitted
compiler: add tests for pragmas
These pragmas weren't really tested anywhere, except that some code might break if they are not properly applied. These tests make it easy to see they work correctly and also provide a logical place to add new pragma tests. I've also made a slight change to how functions and globals are created: with the change they're also created in the IR even if they're not referenced. This makes testing easier.
1 parent c303266 commit 293f4ea

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

compiler/compiler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,11 +715,11 @@ func (c *compilerContext) createPackage(irbuilder llvm.Builder, pkg *ssa.Package
715715
member := pkg.Members[name]
716716
switch member := member.(type) {
717717
case *ssa.Function:
718+
// Create the function definition.
719+
b := newBuilder(c, irbuilder, member)
718720
if member.Blocks == nil {
719721
continue // external function
720722
}
721-
// Create the function definition.
722-
b := newBuilder(c, irbuilder, member)
723723
b.createFunction()
724724
case *ssa.Type:
725725
if types.IsInterface(member.Type()) {
@@ -758,8 +758,8 @@ func (c *compilerContext) createPackage(irbuilder llvm.Builder, pkg *ssa.Package
758758
case *ssa.Global:
759759
// Global variable.
760760
info := c.getGlobalInfo(member)
761+
global := c.getGlobal(member)
761762
if !info.extern {
762-
global := c.getGlobal(member)
763763
global.SetInitializer(llvm.ConstNull(global.Type().ElementType()))
764764
global.SetVisibility(llvm.HiddenVisibility)
765765
}

compiler/compiler_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestCompiler(t *testing.T) {
4545
{"float.go", ""},
4646
{"interface.go", ""},
4747
{"func.go", ""},
48+
{"pragma.go", ""},
4849
{"goroutine.go", "wasm"},
4950
{"goroutine.go", "cortex-m-qemu"},
5051
}

compiler/testdata/pragma.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import _ "unsafe"
4+
5+
// Creates an external global with name extern_global.
6+
//go:extern extern_global
7+
var externGlobal [0]byte
8+
9+
// Creates a
10+
//go:align 32
11+
var alignedGlobal [4]uint32
12+
13+
// Test conflicting pragmas (the last one counts).
14+
//go:align 64
15+
//go:align 16
16+
var alignedGlobal16 [4]uint32
17+
18+
// Test exported functions.
19+
//export extern_func
20+
func externFunc() {
21+
}
22+
23+
// Define a function in a different package using go:linkname.
24+
//go:linkname withLinkageName1 somepkg.someFunction1
25+
func withLinkageName1() {
26+
}
27+
28+
// Import a function from a different package using go:linkname.
29+
//go:linkname withLinkageName2 somepkg.someFunction2
30+
func withLinkageName2()
31+
32+
// Function has an 'inline hint', similar to the inline keyword in C.
33+
//go:inline
34+
func inlineFunc() {
35+
}
36+
37+
// Function should never be inlined, equivalent to GCC
38+
// __attribute__((noinline)).
39+
//go:noinline
40+
func noinlineFunc() {
41+
}

compiler/testdata/pragma.ll

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; ModuleID = 'pragma.go'
2+
source_filename = "pragma.go"
3+
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
4+
target triple = "wasm32--wasi"
5+
6+
@extern_global = external global [0 x i8], align 1
7+
@main.alignedGlobal = hidden global [4 x i32] zeroinitializer, align 32
8+
@main.alignedGlobal16 = hidden global [4 x i32] zeroinitializer, align 16
9+
10+
declare noalias nonnull i8* @runtime.alloc(i32, i8*, i8*)
11+
12+
define hidden void @main.init(i8* %context, i8* %parentHandle) unnamed_addr {
13+
entry:
14+
ret void
15+
}
16+
17+
define void @extern_func() #0 {
18+
entry:
19+
ret void
20+
}
21+
22+
define hidden void @somepkg.someFunction1(i8* %context, i8* %parentHandle) unnamed_addr {
23+
entry:
24+
ret void
25+
}
26+
27+
declare void @somepkg.someFunction2(i8*, i8*)
28+
29+
; Function Attrs: inlinehint
30+
define hidden void @main.inlineFunc(i8* %context, i8* %parentHandle) unnamed_addr #1 {
31+
entry:
32+
ret void
33+
}
34+
35+
; Function Attrs: noinline
36+
define hidden void @main.noinlineFunc(i8* %context, i8* %parentHandle) unnamed_addr #2 {
37+
entry:
38+
ret void
39+
}
40+
41+
attributes #0 = { "wasm-export-name"="extern_func" }
42+
attributes #1 = { inlinehint }
43+
attributes #2 = { noinline }

0 commit comments

Comments
 (0)