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: 863441 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-471643-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="FPK3awEC"; 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 3zNGR73s3gz9s7M for ; Fri, 19 Jan 2018 20:55:29 +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:from :to:subject:date:message-id:mime-version:content-type; q=dns; s= default; b=m5seYaxqV5AJN8IwHCA9l7hx/dic/ul2B51QcX2XazabqKrY+zXxh 9O3Pi0GW90+3KJ4d63lkgltjGuXYC4yXaCmtVQLU+kVX1aGv/rGvPLuaFdMAqe13 WSzFMIW84b+wtG3kOKrW5Olg2oRupVuQErHtQXAL30g/nLpMt84ivw= 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=FPK3awECitrRB6aV1/fk IsWhBlbh0LrDiekEjmwKv9IPOJyoVl72Dmfru5HzzFB0Fvo8ym4s2t3on8jMAw/U wSOXbHdEq7HJE8pQQTRM7zkV6iUXKLdnex7mmbVhK8Fr0FqP5nFUz+jqMRH+jZwW UPIIiy+KvVQmUNtyn3xWTSw= 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-Google-Smtp-Source: ACJfBouloYb1ngtJhBCaHoRLjnVdJhHw8Icxi0FMjLb3p4izW3Npj+3KCgdQwHEXmIFETN+B7+h/Ug== 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; + } +}