Skip to content

Commit

Permalink
cmd/gc: add way to specify 'noescape' for extern funcs
Browse files Browse the repository at this point in the history
A new comment directive //go:noescape instructs the compiler
that the following external (no body) func declaration should be
treated as if none of its arguments escape to the heap.

Fixes #4099.

R=golang-dev, dave, minux.ma, daniel.morsing, remyoudompheng, adg, agl, iant
CC=golang-dev
https://golang.org/cl/7289048
  • Loading branch information
rsc committed Feb 5, 2013
1 parent f1c409b commit fd178d6
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 145 deletions.
27 changes: 26 additions & 1 deletion src/cmd/gc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ other packages. It is therefore not necessary when compiling client C of
package P to read the files of P's dependencies, only the compiled output
of P.
Command Line
Usage:
go tool 6g [flags] file...
The specified files must be Go source files and all part of the same package.
Expand All @@ -48,7 +50,7 @@ Flags:
disable optimizations
-S
write assembly language text to standard output (code only)
-SS
-S -S
write assembly language text to standard output (code and data)
-u
disallow importing packages not marked as safe
Expand All @@ -60,5 +62,28 @@ Flags:
There are also a number of debugging flags; run the command with no arguments
to get a usage message.
Compiler Directives
The compiler accepts two compiler directives in the form of // comments at the
beginning of a line. To distinguish them from non-directive comments, the directives
require no space between the slashes and the name of the directive. However, since
they are comments, tools unaware of the directive convention or of a particular
directive can skip over a directive like any other comment.
//line path/to/file:linenumber
The //line directive specifies that the source line that follows should be recorded
as having come from the given file path and line number. Successive lines are
recorded using increasing line numbers, until the next directive. This directive
typically appears in machine-generated code, so that compilers and debuggers
will show lines in the original input to the generator.
//go:noescape
The //go:noescape directive specifies that the next declaration in the file, which
must be a func without a body (meaning that it has an implementation not written
in Go) does not allow any of the pointers passed as arguments to escape into the
heap or into the values returned from the function. This information can be used as
during the compiler's escape analysis of Go code calling the function.
*/
package documentation
19 changes: 15 additions & 4 deletions src/cmd/gc/esc.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ escfunc(EscState *e, Node *func)
case PPARAM:
if(ll->n->type && !haspointers(ll->n->type))
break;
ll->n->esc = EscNone; // prime for escflood later
if(curfn->nbody == nil && !curfn->noescape)
ll->n->esc = EscHeap;
else
ll->n->esc = EscNone; // prime for escflood later
e->noesc = list(e->noesc, ll->n);
ll->n->escloopdepth = 1;
break;
Expand Down Expand Up @@ -1109,13 +1112,21 @@ esctag(EscState *e, Node *func)
{
Node *savefn;
NodeList *ll;

Type *t;

USED(e);
func->esc = EscFuncTagged;

// External functions must be assumed unsafe.
if(func->nbody == nil)
// External functions are assumed unsafe,
// unless //go:noescape is given before the declaration.
if(func->nbody == nil) {
if(func->noescape) {
for(t=getinargx(func->type)->type; t; t=t->down)
if(haspointers(t->type))
t->note = mktag(EscNone);
}
return;
}

savefn = curfn;
curfn = func;
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/gc/go.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ struct Node
uchar colas; // OAS resulting from :=
uchar diag; // already printed error about this
uchar esc; // EscXXX
uchar noescape; // func arguments do not escape
uchar funcdepth;
uchar builtin; // built-in name, like len or close
uchar walkdef;
Expand Down Expand Up @@ -943,6 +944,7 @@ EXTERN int compiling_wrappers;
EXTERN int pure_go;
EXTERN int flag_race;
EXTERN int flag_largemodel;
EXTERN int noescape;

EXTERN int nointerface;
EXTERN int fieldtrack_enabled;
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/gc/go.y
Original file line number Diff line number Diff line change
Expand Up @@ -1277,8 +1277,11 @@ xfndcl:
$$ = $2;
if($$ == N)
break;
if(noescape && $3 != nil)
yyerror("can only use //go:noescape with external func implementations");
$$->nbody = $3;
$$->endlineno = lineno;
$$->noescape = noescape;
funcbody($$);
}

Expand Down Expand Up @@ -1462,6 +1465,7 @@ xdcl_list:
if(nsyntaxerrors == 0)
testdclstack();
nointerface = 0;
noescape = 0;
}

vardcl_list:
Expand Down
30 changes: 22 additions & 8 deletions src/cmd/gc/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ getlinepragma(void)
Hist *h;

c = getr();
if(c == 'g' && fieldtrack_enabled)
if(c == 'g')
goto go;
if(c != 'l')
goto out;
Expand Down Expand Up @@ -1491,18 +1491,32 @@ getlinepragma(void)
goto out;

go:
for(i=1; i<11; i++) {
c = getr();
if(c != "go:nointerface"[i])
goto out;
}
nointerface = 1;
cp = lexbuf;
ep = lexbuf+sizeof(lexbuf)-5;
*cp++ = 'g'; // already read
for(;;) {
c = getr();
if(c == EOF || c == '\n')
if(c == EOF || c >= Runeself)
goto out;
if(c == '\n')
break;
if(cp < ep)
*cp++ = c;
}
*cp = 0;
ep = strchr(lexbuf, ' ');
if(ep != nil)
*ep = 0;

if(strcmp(lexbuf, "go:nointerface") == 0 && fieldtrack_enabled) {
nointerface = 1;
goto out;
}
if(strcmp(lexbuf, "go:noescape") == 0) {
noescape = 1;
goto out;
}

out:
return c;
}
Expand Down
Loading

0 comments on commit fd178d6

Please sign in to comment.