Skip to content

Commit 37460ad

Browse files
aykevldeadprogram
authored andcommitted
compiler: support pragmas on generic functions
1 parent dcca47f commit 37460ad

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

compiler/symbol.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,14 @@ func (c *compilerContext) getFunctionInfo(f *ssa.Function) functionInfo {
250250
// parsePragmas is used by getFunctionInfo to parse function pragmas such as
251251
// //export or //go:noinline.
252252
func (c *compilerContext) parsePragmas(info *functionInfo, f *ssa.Function) {
253-
if f.Syntax() == nil {
253+
syntax := f.Syntax()
254+
if f.Origin() != nil {
255+
syntax = f.Origin().Syntax()
256+
}
257+
if syntax == nil {
254258
return
255259
}
256-
if decl, ok := f.Syntax().(*ast.FuncDecl); ok && decl.Doc != nil {
260+
if decl, ok := syntax.(*ast.FuncDecl); ok && decl.Doc != nil {
257261
for _, comment := range decl.Doc.List {
258262
text := comment.Text
259263
if strings.HasPrefix(text, "//export ") {

compiler/testdata/pragma.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ func inlineFunc() {
4848
func noinlineFunc() {
4949
}
5050

51+
type Int interface {
52+
int8 | int16
53+
}
54+
55+
// Same for generic functions (but the compiler may miss the pragma due to it
56+
// being generic).
57+
//
58+
//go:noinline
59+
func noinlineGenericFunc[T Int]() {
60+
}
61+
62+
func useGeneric() {
63+
// Make sure the generic function above is instantiated.
64+
noinlineGenericFunc[int8]()
65+
}
66+
5167
// This function should have the specified section.
5268
//
5369
//go:section .special_function_section

compiler/testdata/pragma.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ entry:
4848
ret void
4949
}
5050

51+
; Function Attrs: nounwind
52+
define hidden void @main.useGeneric(ptr %context) unnamed_addr #2 {
53+
entry:
54+
call void @"main.noinlineGenericFunc[int8]"(ptr undef)
55+
ret void
56+
}
57+
58+
; Function Attrs: noinline nounwind
59+
define linkonce_odr hidden void @"main.noinlineGenericFunc[int8]"(ptr %context) unnamed_addr #5 {
60+
entry:
61+
ret void
62+
}
63+
5164
; Function Attrs: noinline nounwind
5265
define hidden void @main.functionInSection(ptr %context) unnamed_addr #5 section ".special_function_section" {
5366
entry:

0 commit comments

Comments
 (0)