From patchwork Fri Jan 19 09:55:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 125140 Delivered-To: patch@linaro.org Received: by 10.46.66.141 with SMTP id h13csp215208ljf; Fri, 19 Jan 2018 01:55:33 -0800 (PST) X-Google-Smtp-Source: ACJfBotm8fRTIdLdYgQ4SZqGB0C7S8HKGQyNn5NP6re2Y/PcZXT7F2GTBWqpG3kak5fMUdrn4I/d X-Received: by 10.98.130.5 with SMTP id w5mr33727783pfd.117.1516355733229; Fri, 19 Jan 2018 01:55:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516355733; cv=none; d=google.com; s=arc-20160816; b=KCFy8PJ8OvFOdvyevbqgadB8P4xCkrzKDy7lBeAZEnIW4EZgxv4H+pA775WURKt8ZK 5C4iM5AXXZ1UJKQ1H8wWeKBYbkbnHo2goMA0xNlbGvmVgII8goppKu0holeoBRYuPI69 BABygCZ3y3DdN/00tq915mke2ekrD/r7X/qIadGiZ4oofO93gWrBjgcOuPS5TVslCHTk hrB05wzybk2Tt9DAQ/fF9jrQ/8ywWb1uivvyzaf9sXQp22YWKOSfaD9HgruKKoa+jbIt onp7K5Bw2yVUM0HNhXOnBGa3CwH1cK2e+dH2ekY2ZxIzLVoa4Jpa0VvTmNCnSj9un5KC ItIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:message-id:date:subject:mail-followup-to:to :from:delivered-to:sender:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=8QDV0yPtzav0wRLiz7JMCGHm2ko5Rq8nYzI+h6bt1Bc=; b=Fa/oFwdKDsf8ZvEETTMwPU33Z1U8KRXeS8vXpBFwqXVq/HLBKbmKaGwI1loezRJN51 mi9q7Owr+rdlU8ItUJ3vW0ZsObfGbTqXxt7dwIQmsV2XAF41DguUrKEQIwRiuXQ+gURD B+72pd/69NrlGMXX2GbzaAyC3B3mRtGA1j1Ol94d2fLY0GloBeyt7zND3O+XEXXplorm OhcByGaifHiCa4zGE/3PRbri2cHjWMLlq1t7SyDV5GUj3SWKADs8MM4Xg+IBS1x/Syvl U9oeOewJF6L7qyD3CSLsqsxy5bhC4ypuITPmJwl3QleU3NnmSY8YG7VqO3pSDLcZQEDR B+0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=J0qWePl0; spf=pass (google.com: domain of gcc-patches-return-471643-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-471643-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id e23si9012789pfb.11.2018.01.19.01.55.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Jan 2018 01:55:33 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-471643-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=J0qWePl0; spf=pass (google.com: domain of gcc-patches-return-471643-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-471643-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; q=dns; s= default; b=c6wdYF3SRttWhjYVxbmb/90HrK8JOAtgyyn/KDw44lttipymny5q1 603Q4Q97yk0mH382Ejev8xGAl5ZIeplAteb/THHjva7/AO/3Jt19IyUrtd1rnDqc 29KZIfYA2/eKOTNtXur61z7sfuI9Kh1ZVUW+D0cSA6Dd5eg5tapGB0= 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:from :to:subject:date:message-id:mime-version:content-type; s= default; bh=P/5zIi0BdtgqaZ1/Fl6TNuN2f0g=; b=J0qWePl0rhsQkr7POCnv MqPH4uTgDuCDYEPW45lnxRqHVWPbj2g1DZO7gHXDG7tLHCNIg1gBNES89prWFouq dxSFxh5x5/Qr9PHoGYxUU3u6zIWDVbGt/uapJMxvGYOiR4nLwNdw+dM1KPM+g934 5hJHPBRL0eeYoRArrkEdnCY= Received: (qmail 78724 invoked by alias); 19 Jan 2018 09:55:22 -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 78714 invoked by uid 89); 19 Jan 2018 09:55:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-9.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_WEB, SPF_PASS autolearn=ham version=3.3.2 spammy=forbid, presumed X-HELO: mail-wm0-f45.google.com Received: from mail-wm0-f45.google.com (HELO mail-wm0-f45.google.com) (74.125.82.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 19 Jan 2018 09:55:19 +0000 Received: by mail-wm0-f45.google.com with SMTP id b21so2291927wme.4 for ; Fri, 19 Jan 2018 01:55:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:mail-followup-to:subject:date:message-id :user-agent:mime-version; bh=8QDV0yPtzav0wRLiz7JMCGHm2ko5Rq8nYzI+h6bt1Bc=; b=gDDVbS1ka8XCZw46nFlQnpeMw3hfFIkZi4WNMXyd5j34NOd7b39A87O7FxOv/9tK8E ad/MZdcZLo3yobN3ycLNE7ZSHL0kdoMdAexcCMhDxbdOev3fmfAsK5OgS5valYYV4v+K TF0gEVf/JpBtGzSd5OyJQUacB4XknZwTwQQ7zZSeWynLZ+xxqxWLP2TBwkDeR4uUVwPQ mGfS4GCqc3eRA7yIBycY8lPnLxV9SkFZ9ztDpqyeLhS4Q9j3mMCgITVTe5TL4abRP3hT PVA9EJG7QTf4LkSPJNhamFYTqcAKjM20MlhHNtpThAcKfXSc865YLSMHL4LidIyiytWg XTbA== X-Gm-Message-State: AKwxytesbuPq/tn05vD6wa70zu5CMtPVK9yV7t2gSWANJ56MM46jdhET mNCbHn09ePyaiCINr/5nUYEGSfciZUM= X-Received: by 10.28.113.21 with SMTP id m21mr7236732wmc.29.1516355717248; Fri, 19 Jan 2018 01:55:17 -0800 (PST) Received: from localhost (188.29.165.192.threembb.co.uk. [188.29.165.192]) by smtp.gmail.com with ESMTPSA id z39sm7633583wrz.20.2018.01.19.01.55.16 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 19 Jan 2018 01:55:16 -0800 (PST) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@linaro.org Subject: Check whether any statements need masking (PR 83922) Date: Fri, 19 Jan 2018 09:55:14 +0000 Message-ID: <87lggu41h9.fsf@linaro.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) MIME-Version: 1.0 This PR is an odd case in which, due to the low optimisation level, we enter vectorisation with: outer1: x_1 = PHI ; ... inner: x_2 = 0; ... outer2: x_3 = PHI ; These statements are tentatively treated as a double reduction by vect_force_simple_reduction, but in the end only x_3 and x_2 are marked as relevant. vect_analyze_loop_operations skips over x_3, leaving the vectorizable_reduction check to a presumed future test of x_1, which in this case never happens. We therefore end up vectorising x_2 only (complete with peeling for niters!) and leave the scalar x_3 in place. This caused a segfault in the support for fully-masked loops, since there were no statements that needed masking. Fixed by checking for that. But I think this is also a flaw in vect_analyze_loop_operations. Outer loop vectorisation reduces the number of times that the inner loop is executed, so it wouldn't necessarily be valid to leave the scalar x_3 in place for all vectorisable x_2. There's already code to forbid that when x_1 isn't present: /* FORNOW: we currently don't support the case that these phis are not used in the outerloop (unless it is double reduction, i.e., this phi is vect_reduction_def), cause this case requires to actually do something here. */ I think we need to do the same if x_1 is present but not relevant. Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu. OK to install? Richard 2018-01-19 Richard Sandiford gcc/ PR tree-optimization/83922 * tree-vect-loop.c (vect_verify_full_masking): Return false if there are no statements that need masking. (vect_active_double_reduction_p): New function. (vect_analyze_loop_operations): Use it when handling phis that are not in the loop header. gcc/testsuite/ PR tree-optimization/83922 * gcc.dg/pr83922.c: New test. Index: gcc/tree-vect-loop.c =================================================================== --- gcc/tree-vect-loop.c 2018-01-19 09:36:33.409191362 +0000 +++ gcc/tree-vect-loop.c 2018-01-19 09:52:00.681330865 +0000 @@ -1294,6 +1294,12 @@ vect_verify_full_masking (loop_vec_info struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo); unsigned int min_ni_width; + /* Use a normal loop if there are no statements that need masking. + This only happens in rare degenerate cases: it means that the loop + has no loads, no stores, and no live-out values. */ + if (LOOP_VINFO_MASKS (loop_vinfo).is_empty ()) + return false; + /* Get the maximum number of iterations that is representable in the counter type. */ tree ni_type = TREE_TYPE (LOOP_VINFO_NITERSM1 (loop_vinfo)); @@ -1739,6 +1745,33 @@ vect_update_vf_for_slp (loop_vec_info lo } } +/* Return true if STMT_INFO describes a double reduction phi and if + the other phi in the reduction is also relevant for vectorization. + This rejects cases such as: + + outer1: + x_1 = PHI ; + ... + + inner: + x_2 = ...; + ... + + outer2: + x_3 = PHI ; + + if nothing in x_2 or elsewhere makes x_1 relevant. */ + +static bool +vect_active_double_reduction_p (stmt_vec_info stmt_info) +{ + if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_double_reduction_def) + return false; + + gimple *other_phi = STMT_VINFO_REDUC_DEF (stmt_info); + return STMT_VINFO_RELEVANT_P (vinfo_for_stmt (other_phi)); +} + /* Function vect_analyze_loop_operations. Scan the loop stmts and make sure they are all vectorizable. */ @@ -1786,8 +1819,7 @@ vect_analyze_loop_operations (loop_vec_i i.e., this phi is vect_reduction_def), cause this case requires to actually do something here. */ if (STMT_VINFO_LIVE_P (stmt_info) - && STMT_VINFO_DEF_TYPE (stmt_info) - != vect_double_reduction_def) + && !vect_active_double_reduction_p (stmt_info)) { if (dump_enabled_p ()) dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, Index: gcc/testsuite/gcc.dg/pr83922.c =================================================================== --- /dev/null 2018-01-19 09:30:49.543814408 +0000 +++ gcc/testsuite/gcc.dg/pr83922.c 2018-01-19 09:52:00.680331041 +0000 @@ -0,0 +1,21 @@ +/* { dg-options "-O -ftree-vectorize" } */ + +int j4; + +void +k1 (int ak) +{ + while (ak < 1) + { + int ur; + + for (ur = 0; ur < 2; ++ur) + { + ++j4; + if (j4 != 0) + j4 = 0; + } + + ++ak; + } +}