From patchwork Mon Nov 11 21:55:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Thomas_K=C3=B6nig?= X-Patchwork-Id: 1193163 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-513004-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=tkoenig.net Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="PmoTgZFw"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=tkoenig.net header.i=@tkoenig.net header.b="qG5Qz5oL"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47Bl7Y5S96z9sPF for ; Tue, 12 Nov 2019 08:56:08 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=O1q8BV0PgW4Ie4/t3cjUo9TP3CB5T19d0gDrpCyFcH7jNA1z1O m78MEtp36qRqoh374dfVBiHHgYXYYjD9tvccQHe7RFswy3UeKbk8TjLHloTzG3YB C56vpGLlNaem3d6VOGfXpXvimF4NQ8vASbBj4zoDE6Me/5UvEVNc2kISs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=APmWMN3iggqHUfJzstdbtctzwk0=; b=PmoTgZFw1n9FsWvtZGsX 6ftf2q+yMxRK6CbIKttux3c2xnh2J342F1uMTDYImXN8cDDrD1cQwPEVJojn1QYR 0fAlB64lj53Pzr2DjnPhTIbk1TE5oyKQBLGoIJjb1R4YajSDg5Drzj1iSEMGavBk XQHQ3uisew10jgWtglWFxOU= Received: (qmail 92762 invoked by alias); 11 Nov 2019 21:56:00 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 92736 invoked by uid 89); 11 Nov 2019 21:56:00 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=attrflavor, attr.flavor, **c, arbeitskopie X-HELO: mo4-p00-ob.smtp.rzone.de Received: from mo4-p00-ob.smtp.rzone.de (HELO mo4-p00-ob.smtp.rzone.de) (81.169.146.217) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 11 Nov 2019 21:55:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1573509355; s=strato-dkim-0002; d=tkoenig.net; h=Date:Message-ID:Subject:From:To:X-RZG-CLASS-ID:X-RZG-AUTH:From: Subject:Sender; bh=RjTBMxaAJZHeKB6YUHmlvjDY3YM79jhja/PClgmQ9Fs=; b=qG5Qz5oLrn1jhentb+CFmWJXdoan2uziqPHGnf4U7iXwaHp24Ab6tSLynQHL6aIZiL VQ6h+G6ok9kMJkkCTXJkzub5WoW+u56AuP7GCsWYlnWrH647VPV/uRyB0HWxybOJxgyq AJALd2CyjJGLqEsdUEowkT2fMA5hRQ4jvawe/IdUfYN+2GVWE6d7XiJPtOTv5HiiuiZ8 fmmvDa51zww5d0oC5V2uLnq5XNw503IbEwg8kV1gSSar3iv7Aw2PnfDtR0+Dzkigu/tX kqYBrLr2fZ7bOGBR9k+FmOvN+hhd+K7PhVBd5emEKTS+n0ijIG/HTO8PNCWdp4571Oy/ et6Q== Received: from [IPv6:2001:4dd6:2b98:0:7285:c2ff:fe6c:992d] by smtp.strato.de (RZmta 44.29.0 AUTH) with ESMTPSA id q00c85vABLtspdc (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Mon, 11 Nov 2019 22:55:54 +0100 (CET) To: "fortran@gcc.gnu.org" , gcc-patches From: =?utf-8?q?Thomas_K=C3=B6nig?= Subject: [patch, fortran] Load scalar intent-in variables at the beginning of procedures Message-ID: <48286910-ebbb-10e4-488b-8c96e505375c@tkoenig.net> Date: Mon, 11 Nov 2019 22:55:54 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 Hello world, the attached patch loads scalar INTENT(IN) variables to a local variable at the start of a procedure, as suggested in PR 67202, in order to aid optimization. This is controlled by front-end optimization so it is easier to catch if any bugs should turn up :-) This is done to make optimization by the middle-end easier. I left in the parts for debugging that I added for this patch. Seeing the difference between EXEC_INIT_ASSIGN and EXEC_ASSIGN was particularly instructive. Regression-tested. OK for trunk? Regards Thomas Index: dump-parse-tree.c =================================================================== --- dump-parse-tree.c (Revision 278025) +++ dump-parse-tree.c (Arbeitskopie) @@ -57,6 +57,15 @@ static void show_attr (symbol_attribute *, const c /* Allow dumping of an expression in the debugger. */ void gfc_debug_expr (gfc_expr *); +void debug (gfc_namespace *ns) +{ + FILE *tmp = dumpfile; + dumpfile = stderr; + show_namespace (ns); + fputc ('\n', dumpfile); + dumpfile = tmp; +} + void debug (symbol_attribute *attr) { FILE *tmp = dumpfile; @@ -1889,6 +1898,9 @@ show_code_node (int level, gfc_code *c) break; case EXEC_INIT_ASSIGN: + fputs ("INIT_", dumpfile); + /* Fallthrough */ + case EXEC_ASSIGN: fputs ("ASSIGN ", dumpfile); show_expr (c->expr1); Index: frontend-passes.c =================================================================== --- frontend-passes.c (Revision 278025) +++ frontend-passes.c (Arbeitskopie) @@ -57,6 +57,7 @@ static int call_external_blas (gfc_code **, int *, static int matmul_temp_args (gfc_code **, int *,void *data); static int index_interchange (gfc_code **, int*, void *); static bool is_fe_temp (gfc_expr *e); +static void replace_intent_in (gfc_namespace *); #ifdef CHECKING_P static void check_locus (gfc_namespace *); @@ -1467,6 +1468,7 @@ optimize_namespace (gfc_namespace *ns) if (flag_frontend_optimize) { + replace_intent_in (ns); gfc_code_walker (&ns->code, simplify_io_impl_do, dummy_expr_callback, NULL); gfc_code_walker (&ns->code, convert_do_while, dummy_expr_callback, NULL); gfc_code_walker (&ns->code, convert_elseif, dummy_expr_callback, NULL); @@ -5503,3 +5505,132 @@ gfc_check_externals (gfc_namespace *ns) gfc_errors_to_warnings (false); } + +/* For scalar INTENT(IN) variables or for variables where we know + their value is not changed, we can replace them by an auxiliary + variable whose value is set on procedure entry. */ + +typedef struct sym_replacement +{ + gfc_symbol *original; + gfc_symtree *replacement_symtree; + bool referenced; + +} sym_replace; + +/* Callback function - replace expression if possible, and set + sr->referenced if this was done (so we know we need to generate + the assignment statement). */ + +static int +replace_symbol_in_expr (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, + void *data) +{ + gfc_expr *expr = *e; + sym_replacement *sr; + + if (expr->expr_type != EXPR_VARIABLE || expr->symtree == NULL) + return 0; + + sr = (sym_replacement *) data; + + if (expr->symtree->n.sym == sr->original) + { + expr->symtree = sr->replacement_symtree; + sr->referenced = true; + } + + return 0; +} + +/* Replace INTENT(IN) scalar variables by assigning their values to + temporary variables. We really only want to use this for the + simplest cases, all the fancy stuff is excluded. */ + +static void +replace_intent_in (gfc_namespace *ns) +{ + gfc_formal_arglist *f; + gfc_namespace *ns_c; + + if (ns == NULL || ns->proc_name == NULL || gfc_elemental (ns->proc_name) + || ns->proc_name->attr.entry_master) + return; + + for (f = ns->proc_name->formal; f; f = f->next) + { + if (f->sym == NULL || f->sym->attr.dimension || f->sym->attr.allocatable + || f->sym->attr.optional || f->sym->attr.pointer + || f->sym->attr.codimension || f->sym->attr.value + || f->sym->attr.proc_pointer || f->sym->attr.target + || f->sym->attr.asynchronous + || f->sym->ts.type == BT_CHARACTER || f->sym->ts.type == BT_DERIVED + || f->sym->ts.type == BT_CLASS) + continue; + + /* TODO: It could also be possible to check if the variable can + actually not be changed by appearing in a variable + definition context or by being passed as an argument to a + procedure where it could be changed. */ + + if (f->sym->attr.intent == INTENT_IN) + { + gfc_symtree *symtree; + gfc_symbol *replacement; + sym_replace sr; + + char name[GFC_MAX_SYMBOL_LEN + 1]; + snprintf (name, GFC_MAX_SYMBOL_LEN, "__dummy_%d_%s", var_num++, + f->sym->name); + + if (gfc_get_sym_tree (name, ns, &symtree, false) != 0) + gcc_unreachable (); + + replacement = symtree->n.sym; + replacement->ts = f->sym->ts; + replacement->attr.flavor = FL_VARIABLE; + replacement->attr.fe_temp = 1; + replacement->attr.referenced = 1; + replacement->declared_at = f->sym->declared_at; + gfc_commit_symbol (replacement); + + sr.original = f->sym; + sr.replacement_symtree = symtree; + sr.referenced = false; + + gfc_code_walker (&ns->code, gfc_dummy_code_callback, + replace_symbol_in_expr, (void *) &sr); + + for (ns_c = ns->contained; ns_c != NULL; ns_c = ns_c->sibling) + gfc_code_walker (&ns_c->code, gfc_dummy_code_callback, + replace_symbol_in_expr, (void *) &sr); + + if (sr.referenced) + { + gfc_code *n; + gfc_symtree *formal_symtree; + gfc_code **c; + + /* Generate statement __tmp_42_foo = foo . */ + n = XCNEW (gfc_code); + n->op = EXEC_ASSIGN; + n->expr1 = gfc_lval_expr_from_sym (replacement); + n->expr1->where = f->sym->declared_at; + formal_symtree = gfc_find_symtree (ns->sym_root, f->sym->name); + n->expr2 = gfc_get_variable_expr (formal_symtree); + n->expr2->where = f->sym->declared_at; + n->loc = f->sym->declared_at; + + /* Put this statement after the initialization + assignment statements. */ + + for (c = &ns->code; *c != NULL && (*c)->op == EXEC_INIT_ASSIGN; + c = &(*c)->next) + ; + + n->next = (*c); + (*c) = n; + } + } + } +}