compiler: discard global sink variables with static initializers
This is specifically for the test fixedbugs/issue23781.go, which
builds a large static array. The code does compile and work without
this change, but it takes a long time and generates a large object
file. Discarding the unnecessary static initializer makes this test
much faster.
Change-Id: Ic15b462bf049c0b19dce330e5925e6989775f369
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/278172
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/go/gogo.cc b/go/gogo.cc
index e31a038..fbf8935 100644
--- a/go/gogo.cc
+++ b/go/gogo.cc
@@ -1558,6 +1558,17 @@
|| (no->is_const() && no->const_value()->is_sink()))
continue;
+ // Skip global sink variables with static initializers. With
+ // non-static initializers we have to evaluate for side effects,
+ // and we wind up initializing a dummy variable. That is not
+ // ideal but it works and it's a rare case.
+ if (no->is_variable()
+ && no->var_value()->is_global_sink()
+ && !no->var_value()->has_pre_init()
+ && (no->var_value()->init() == NULL
+ || no->var_value()->init()->is_static_initializer()))
+ continue;
+
// There is nothing useful we can output for constants which
// have ideal or non-integral type.
if (no->is_const())
@@ -7447,7 +7458,7 @@
: type_(type), init_(init), preinit_(NULL), location_(location),
backend_(NULL), is_global_(is_global), is_parameter_(is_parameter),
is_closure_(false), is_receiver_(is_receiver),
- is_varargs_parameter_(false), is_used_(false),
+ is_varargs_parameter_(false), is_global_sink_(false), is_used_(false),
is_address_taken_(false), is_non_escaping_address_taken_(false),
seen_(false), init_is_lowered_(false), init_is_flattened_(false),
type_from_init_tuple_(false), type_from_range_index_(false),
diff --git a/go/gogo.h b/go/gogo.h
index 49d7bd9..bdb3166 100644
--- a/go/gogo.h
+++ b/go/gogo.h
@@ -2113,6 +2113,20 @@
is_varargs_parameter() const
{ return this->is_varargs_parameter_; }
+ // Return whether this is a global sink variable, created only to
+ // run an initializer.
+ bool
+ is_global_sink() const
+ { return this->is_global_sink_; }
+
+ // Record that this is a global sink variable.
+ void
+ set_is_global_sink()
+ {
+ go_assert(this->is_global_);
+ this->is_global_sink_ = true;
+ }
+
// Whether this variable's address is taken.
bool
is_address_taken() const
@@ -2340,6 +2354,9 @@
bool is_receiver_ : 1;
// Whether this is the varargs parameter of a function.
bool is_varargs_parameter_ : 1;
+ // Whether this is a global sink variable created to run an
+ // initializer.
+ bool is_global_sink_ : 1;
// Whether this variable is ever referenced.
bool is_used_ : 1;
// Whether something takes the address of this variable. For a
diff --git a/go/parse.cc b/go/parse.cc
index a4740cf..3b2e5a7 100644
--- a/go/parse.cc
+++ b/go/parse.cc
@@ -2075,6 +2075,7 @@
if (type == NULL && init == NULL)
type = Type::lookup_bool_type();
Variable* var = new Variable(type, init, true, false, false, location);
+ var->set_is_global_sink();
static int count;
char buf[30];
snprintf(buf, sizeof buf, "_.%d", count);