From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-1.7 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 434471F463 for ; Thu, 12 Sep 2019 09:43:48 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id A0A781209F8; Thu, 12 Sep 2019 18:43:39 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id B49C01208F8 for ; Thu, 12 Sep 2019 18:43:37 +0900 (JST) Received: by filter0161p3mdw1.sendgrid.net with SMTP id filter0161p3mdw1-8961-5D7A134B-5 2019-09-12 09:43:39.106898521 +0000 UTC m=+143617.094235021 Received: from herokuapp.com (unknown [54.242.45.138]) by ismtpd0036p1iad1.sendgrid.net (SG) with ESMTP id Rx3h7q8tQQ6kbECDfvZW0A for ; Thu, 12 Sep 2019 09:43:39.070 +0000 (UTC) Date: Thu, 12 Sep 2019 09:43:39 +0000 (UTC) From: s.wanabe@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70475 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16161 X-Redmine-Issue-Author: wanabe X-Redmine-Sender: wanabe X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?FsuGSN1PD4adq4aDFRSSBF8ffe=2F4plpeX9T+dOtlSNbuKAf83OLZVE17eGHPBm?= =?us-ascii?Q?zDXlodrGJR7seZfLURdthalZpfNCw5LzxRu7Xi0?= =?us-ascii?Q?1U2pDt6FFqHqhDpwJv6v6lLwV09G36b59ufoTyJ?= =?us-ascii?Q?Hma4Bftu8mAVP=2FxgD6z7+HEdZtf4PR2FSSdTJoC?= =?us-ascii?Q?yjGuuPA+gKI7P9uRQNOAJ08mKilIybTyOjw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94921 Subject: [ruby-core:94921] [Ruby master Bug#16161] tailcall_optimization may be disabled after r67315 X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16161 has been updated by wanabe (_ wanabe). How about adding `vm_call_iseq_setup_tailcall_opt_start` as same as `vm_call_iseq_setup_normal_opt_start`? (Copy-and-paste code, sorry!) ```patch diff --git a/vm_insnhelper.c b/vm_insnhelper.c index a83e3a952a..4abee2fadd 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1825,6 +1825,30 @@ vm_call_iseq_setup_normal_opt_start(rb_execution_context_t *ec, rb_control_frame return vm_call_iseq_setup_normal(ec, cfp, calling, cc->me, opt_pc, param - delta, local); } +static VALUE +vm_call_iseq_setup_tailcall_opt_start(rb_execution_context_t *ec, rb_control_frame_t *cfp, + struct rb_calling_info *calling, + const struct rb_call_info *ci, struct rb_call_cache *cc) +{ + const rb_iseq_t *iseq = def_iseq_ptr(cc->me->def); + const int lead_num = iseq->body->param.lead_num; + const int opt = calling->argc - lead_num; + const int opt_pc = (int)iseq->body->param.opt_table[opt]; + + RB_DEBUG_COUNTER_INC(ccf_iseq_opt); + +#if USE_OPT_HIST + if (opt_pc < OPT_HIST_MAX) { + opt_hist[opt]++; + } + else { + opt_hist[OPT_HIST_MAX]++; + } +#endif + + return vm_call_iseq_setup_tailcall(ec, cfp, calling, ci, cc, opt_pc); +} + static void args_setup_kw_parameters(rb_execution_context_t *const ec, const rb_iseq_t *const iseq, VALUE *const passed_values, const int passed_keyword_len, const VALUE *const passed_keywords, @@ -1909,9 +1933,16 @@ vm_callee_setup_arg(rb_execution_context_t *ec, struct rb_calling_info *calling, argument_arity_error(ec, iseq, argc, lead_num, lead_num + opt_num); } - CC_SET_FASTPATH(cc, vm_call_iseq_setup_normal_opt_start, - !IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) && - !(METHOD_ENTRY_VISI(cc->me) == METHOD_VISI_PROTECTED)); + if (LIKELY(!(ci->flag & VM_CALL_TAILCALL))) { + CC_SET_FASTPATH(cc, vm_call_iseq_setup_normal_opt_start, + !IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) && + !(METHOD_ENTRY_VISI(cc->me) == METHOD_VISI_PROTECTED)); + } + else { + CC_SET_FASTPATH(cc, vm_call_iseq_setup_tailcall_opt_start, + !IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) && + !(METHOD_ENTRY_VISI(cc->me) == METHOD_VISI_PROTECTED)); + } /* initialize opt vars for self-references */ VM_ASSERT((int)iseq->body->param.size == lead_num + opt_num); ``` ---------------------------------------- Bug #16161: tailcall_optimization may be disabled after r67315 https://bugs.ruby-lang.org/issues/16161#change-81535 * Author: wanabe (_ wanabe) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.7.0dev (2019-09-09T23:18:03Z master 3678c37119) [x86_64-linux] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- Before r67315: ``` $ ./miniruby -v -e 'iseq = RubyVM::InstructionSequence.compile("def foo(n, s = 0);return s if n < 1;foo(n - 1, n + s); end", tailcall_optimization: true); iseq.eval; p foo(900_000)' ruby 2.7.0dev (2019-03-20 trunk 67314) [x86_64-linux] 405000450000 ``` After r67315: ``` $ ./miniruby -v -e 'iseq = RubyVM::InstructionSequence.compile("def foo(n, s = 0);return s if n < 1;foo(n - 1, n + s); end", tailcall_optimization: true); iseq.eval; p foo(900_000)' ruby 2.7.0dev (2019-03-20 trunk 67315) [x86_64-linux] Traceback (most recent call last): 10080: from -e:1:in `
' 10079: from :1:in `foo' 10078: from :1:in `foo' 10077: from :1:in `foo' 10076: from :1:in `foo' 10075: from :1:in `foo' 10074: from :1:in `foo' 10073: from :1:in `foo' ... 10068 levels... 4: from :1:in `foo' 3: from :1:in `foo' 2: from :1:in `foo' 1: from :1:in `foo' :1:in `foo': stack level too deep (SystemStackError) ``` master: ``` $ ./miniruby -v -e 'iseq = RubyVM::InstructionSequence.compile("def foo(n, s = 0);return s if n < 1;foo(n - 1, n + s); end", tailcall_optimization: true); iseq.eval; p foo(900_000)' ruby 2.7.0dev (2019-09-09T23:18:03Z master 3678c37119) [x86_64-linux] Traceback (most recent call last): 10080: from -e:1:in `
' 10079: from :1:in `foo' 10078: from :1:in `foo' 10077: from :1:in `foo' 10076: from :1:in `foo' 10075: from :1:in `foo' 10074: from :1:in `foo' 10073: from :1:in `foo' ... 10068 levels... 4: from :1:in `foo' 3: from :1:in `foo' 2: from :1:in `foo' 1: from :1:in `foo' :1:in `foo': stack level too deep (SystemStackError) ``` I think ruby should not raise SystemStackError with tailcall_optimization, should it? -- https://bugs.ruby-lang.org/