compiler: discard global sink variables with static initializers
authorIan Lance Taylor <iant@golang.org>
Tue, 15 Dec 2020 04:41:30 +0000 (20:41 -0800)
committerIan Lance Taylor <iant@golang.org>
Tue, 15 Dec 2020 20:56:50 +0000 (12:56 -0800)
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.

Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/278172

gcc/go/gofrontend/MERGE
gcc/go/gofrontend/gogo.cc
gcc/go/gofrontend/gogo.h
gcc/go/gofrontend/parse.cc

index 1bada25300dbf486250ac2923f8c2b483bdc1241..a596b241a4e8d02f76696ea2a5eb7e1d46a90010 100644 (file)
@@ -1,4 +1,4 @@
-8b913a1865e36d4bd105f29aa0b12264a4e03515
+85c390ec75c6c3f3fbfe08f6dac58585588c6211
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
index e31a038dbd3ebb60d1e861d0d92bec2e1b38c0aa..fbf8935bb06a4b3333f591f45887c7cf63307a02 100644 (file)
@@ -1558,6 +1558,17 @@ Gogo::write_globals()
          || (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 @@ Variable::Variable(Type* type, Expression* init, bool is_global,
   : 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),
index 49d7bd9b98ad6b7dc83ab24c663210872b8402f1..bdb3166006d068f654faed725ada4698b79d0341 100644 (file)
@@ -2113,6 +2113,20 @@ class Variable
   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 @@ class Variable
   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
index a4740cfad0e302a961498e8066739cfdd3c5bc12..3b2e5a7aa86c7a7043ffbf495b179f8b76c64b43 100644 (file)
@@ -2075,6 +2075,7 @@ Parse::create_dummy_global(Type* type, Expression* init,
   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);