[前][次][番号順一覧][スレッド一覧]

ruby-changes:42624

From: usa <ko1@a...>
Date: Fri, 22 Apr 2016 17:45:45 +0900 (JST)
Subject: [ruby-changes:42624] usa:r54698 (ruby_2_2): merge revision(s) 54542, 54548: [Backport #12082]

usa	2016-04-22 18:42:21 +0900 (Fri, 22 Apr 2016)

  New Revision: 54698

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54698

  Log:
    merge revision(s) 54542,54548: [Backport #12082]
    
    * compile.c (iseq_optimize): disable tail call optimization in
      rescued, rescue, and ensure blocks.
      [ruby-core:73871] [Bug #12082]
    
    * compile.c (new_label_body): initialize bit fields, since
      compile_data_alloc does not clear the memory.  [Bug #12082]

  Modified directories:
    branches/ruby_2_2/
  Modified files:
    branches/ruby_2_2/ChangeLog
    branches/ruby_2_2/compile.c
    branches/ruby_2_2/test/ruby/test_optimization.rb
    branches/ruby_2_2/version.h
Index: ruby_2_2/ChangeLog
===================================================================
--- ruby_2_2/ChangeLog	(revision 54697)
+++ ruby_2_2/ChangeLog	(revision 54698)
@@ -1,3 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/ChangeLog#L1
+Fri Apr 22 18:36:15 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* compile.c (new_label_body): initialize bit fields, since
+	  compile_data_alloc does not clear the memory.  [Bug #12082]
+
+Fri Apr 22 18:36:15 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* compile.c (iseq_optimize): disable tail call optimization in
+	  rescued, rescue, and ensure blocks.
+	  [ruby-core:73871] [Bug #12082]
+
 Fri Apr 22 18:34:31 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* doc/regexp.rdoc (comments): [DOC] terminators cannot appear in
Index: ruby_2_2/compile.c
===================================================================
--- ruby_2_2/compile.c	(revision 54697)
+++ ruby_2_2/compile.c	(revision 54698)
@@ -41,6 +41,13 @@ typedef struct iseq_link_anchor { https://github.com/ruby/ruby/blob/trunk/ruby_2_2/compile.c#L41
     LINK_ELEMENT *last;
 } LINK_ANCHOR;
 
+typedef enum {
+    LABEL_RESCUE_NONE,
+    LABEL_RESCUE_BEG,
+    LABEL_RESCUE_END,
+    LABEL_RESCUE_TYPE_MAX
+} LABEL_RESCUE_TYPE;
+
 typedef struct iseq_label_data {
     LINK_ELEMENT link;
     int label_no;
@@ -48,6 +55,7 @@ typedef struct iseq_label_data { https://github.com/ruby/ruby/blob/trunk/ruby_2_2/compile.c#L55
     int sc_state;
     int set;
     int sp;
+    unsigned int rescued: 2;
 } LABEL;
 
 typedef struct iseq_insn_data {
@@ -497,6 +505,9 @@ rb_iseq_compile_node(VALUE self, NODE *n https://github.com/ruby/ruby/blob/trunk/ruby_2_2/compile.c#L505
 		LABEL *start = iseq->compile_data->start_label = NEW_LABEL(0);
 		LABEL *end = iseq->compile_data->end_label = NEW_LABEL(0);
 
+		start->rescued = LABEL_RESCUE_BEG;
+		end->rescued = LABEL_RESCUE_END;
+
 		ADD_TRACE(ret, FIX2INT(iseq->location.first_lineno), RUBY_EVENT_B_CALL);
 		ADD_LABEL(ret, start);
 		COMPILE(ret, "block body", node->nd_body);
@@ -2038,20 +2049,37 @@ iseq_specialized_instruction(rb_iseq_t * https://github.com/ruby/ruby/blob/trunk/ruby_2_2/compile.c#L2049
     return COMPILE_OK;
 }
 
+static inline int
+tailcallable_p(rb_iseq_t *iseq)
+{
+    switch (iseq->type) {
+      case ISEQ_TYPE_RESCUE:
+      case ISEQ_TYPE_ENSURE:
+	/* rescue block can't tail call because of errinfo */
+	return FALSE;
+      default:
+	return TRUE;
+    }
+}
+
 static int
 iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *anchor)
 {
     LINK_ELEMENT *list;
     const int do_peepholeopt = iseq->compile_data->option->peephole_optimization;
-    const int do_tailcallopt = iseq->compile_data->option->tailcall_optimization;
+    const int do_tailcallopt = tailcallable_p(iseq) &&
+	iseq->compile_data->option->tailcall_optimization;
     const int do_si = iseq->compile_data->option->specialized_instruction;
     const int do_ou = iseq->compile_data->option->operands_unification;
+    int rescue_level = 0;
+    int tailcallopt = do_tailcallopt;
+
     list = FIRST_ELEMENT(anchor);
 
     while (list) {
 	if (list->type == ISEQ_ELEMENT_INSN) {
 	    if (do_peepholeopt) {
-		iseq_peephole_optimize(iseq, list, do_tailcallopt);
+		iseq_peephole_optimize(iseq, list, tailcallopt);
 	    }
 	    if (do_si) {
 		iseq_specialized_instruction(iseq, (INSN *)list);
@@ -2060,6 +2088,17 @@ iseq_optimize(rb_iseq_t *iseq, LINK_ANCH https://github.com/ruby/ruby/blob/trunk/ruby_2_2/compile.c#L2088
 		insn_operands_unification((INSN *)list);
 	    }
 	}
+	if (list->type == ISEQ_ELEMENT_LABEL) {
+	    switch (((LABEL *)list)->rescued) {
+	      case LABEL_RESCUE_BEG:
+		rescue_level++;
+		tailcallopt = FALSE;
+		break;
+	      case LABEL_RESCUE_END:
+		if (!--rescue_level) tailcallopt = do_tailcallopt;
+		break;
+	    }
+	}
 	list = list->next;
     }
     return COMPILE_OK;
@@ -3108,6 +3147,8 @@ defined_expr(rb_iseq_t *iseq, LINK_ANCHO https://github.com/ruby/ruby/blob/trunk/ruby_2_2/compile.c#L3147
 						       ("defined guard in "),
 						       iseq->location.label),
 					 ISEQ_TYPE_DEFINED_GUARD, 0);
+	lstart->rescued = LABEL_RESCUE_BEG;
+	lend->rescued = LABEL_RESCUE_END;
 	APPEND_LABEL(ret, lcur, lstart);
 	ADD_LABEL(ret, lend);
 	ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
@@ -3878,6 +3919,8 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/compile.c#L3919
 	    rb_str_concat(rb_str_new2("rescue in "), iseq->location.label),
 	    ISEQ_TYPE_RESCUE, line);
 
+	lstart->rescued = LABEL_RESCUE_BEG;
+	lend->rescued = LABEL_RESCUE_END;
 	ADD_LABEL(ret, lstart);
 	COMPILE(ret, "rescue head", node->nd_head);
 	ADD_LABEL(ret, lend);
Index: ruby_2_2/test/ruby/test_optimization.rb
===================================================================
--- ruby_2_2/test/ruby/test_optimization.rb	(revision 54697)
+++ ruby_2_2/test/ruby/test_optimization.rb	(revision 54698)
@@ -210,6 +210,23 @@ class TestRubyOptimization < Test::Unit: https://github.com/ruby/ruby/blob/trunk/ruby_2_2/test/ruby/test_optimization.rb#L210
     assert_equal true, MyObj.new == nil
   end
 
+  def self.tailcall(klass, src, file = nil, path = nil, line = nil)
+    unless file
+      loc, = caller_locations(1, 1)
+      file = loc.path
+      line ||= loc.lineno
+    end
+    RubyVM::InstructionSequence.new("proc {|_|_.class_eval {#{src}}}",
+                                    file, (path || file), line,
+                                    tailcall_optimization: true,
+                                    trace_instruction: false)
+      .eval[klass]
+  end
+
+  def tailcall(*args)
+    self.class.tailcall(singleton_class, *args)
+  end
+
   def test_tailcall
     bug4082 = '[ruby-core:33289]'
 
@@ -255,6 +272,30 @@ class TestRubyOptimization < Test::Unit: https://github.com/ruby/ruby/blob/trunk/ruby_2_2/test/ruby/test_optimization.rb#L272
     assert_equal(123, delay { 123 }.call, bug6901)
   end
 
+  def do_raise
+    raise "should be rescued"
+  end
+
+  def errinfo
+    $!
+  end
+
+  def test_tailcall_inhibited_by_rescue
+    bug12082 = '[ruby-core:73871] [Bug #12082]'
+
+    tailcall(<<-'end;')
+      def to_be_rescued
+        return do_raise
+        1 + 2
+      rescue
+        errinfo
+      end
+    end;
+    result = to_be_rescued
+    assert_instance_of(RuntimeError, result, bug12082)
+    assert_equal("should be rescued", result.message, bug12082)
+  end
+
   class Bug10557
     def [](_)
       block_given?
Index: ruby_2_2/version.h
===================================================================
--- ruby_2_2/version.h	(revision 54697)
+++ ruby_2_2/version.h	(revision 54698)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/version.h#L1
 #define RUBY_VERSION "2.2.5"
 #define RUBY_RELEASE_DATE "2016-04-22"
-#define RUBY_PATCHLEVEL 312
+#define RUBY_PATCHLEVEL 313
 
 #define RUBY_RELEASE_YEAR 2016
 #define RUBY_RELEASE_MONTH 4

Property changes on: ruby_2_2
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r54542


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]