From patchwork Mon Jun 24 17:54:32 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 253941 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 0286C2C007C for ; Tue, 25 Jun 2013 03:54:52 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=q7kRjiz/lS/qe+DuI G1elhOjBCGHQOSMyHLyPRpRR0QOoQ62yDQlwzmH+PAMdSMVy5AGnkuSs2nkFqjXR bvsupxOXPI79RjzIkgVuXbpmN5gnlshIQLx8NYpjt5v432AR8GqKM4EOJdN1LrwJ VmAMFaAUY1kO5zsbZDt8F1qIWk= 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:date :from:to:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; s=default; bh=4V+7Bl/hvmv11dKO5J/uP6l c4hA=; b=sLzHOeM32PhkxL9yD/RwZtnP24WPl6FQ6XZS3W55aTtgEREFbCZeeRt kWl+Nat1QmMyQFDwq1MYmPg1JMo062PFxM2KvYycMU98nTQZz8KP6XGQeuGKGm4+ 7Edp3yGMbJ+KjVaGQvE/9sXZZy7QFXh5TqojINHTxnZcKAlUpL28= Received: (qmail 32226 invoked by alias); 24 Jun 2013 17:54:43 -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 32210 invoked by uid 89); 24 Jun 2013 17:54:42 -0000 X-Spam-SWARE-Status: No, score=-6.5 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS, TW_AV, TW_CF, TW_FN, TW_QI, TW_TM, TW_VF, TW_VQ autolearn=ham version=3.3.1 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Mon, 24 Jun 2013 17:54:38 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r5OHsaa6011971 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 24 Jun 2013 13:54:36 -0400 Received: from zalov.cz (vpn1-6-77.ams2.redhat.com [10.36.6.77]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r5OHsY6g024343 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 24 Jun 2013 13:54:35 -0400 Received: from zalov.cz (localhost [127.0.0.1]) by zalov.cz (8.14.5/8.14.5) with ESMTP id r5OHsXai012045; Mon, 24 Jun 2013 19:54:33 +0200 Received: (from jakub@localhost) by zalov.cz (8.14.5/8.14.5/Submit) id r5OHsWPJ012044; Mon, 24 Jun 2013 19:54:32 +0200 Date: Mon, 24 Jun 2013 19:54:32 +0200 From: Jakub Jelinek To: Richard Biener , Richard Henderson , Aldy Hernandez Cc: "Iyer, Balaji V" , "gcc-patches@gcc.gnu.org" Subject: Re: [gomp4] Some progress on #pragma omp simd Message-ID: <20130624175432.GL2336@tucnak.redhat.com> Reply-To: Jakub Jelinek References: <20130427181734.GX28963@tucnak.redhat.com> <51B8AE31.7070808@redhat.com> <20130612173055.GA2336@tucnak.redhat.com> <51B8B1F8.6040408@redhat.com> <20130612213617.GB2336@tucnak.redhat.com> <51BF43C0.9080901@redhat.com> <51BF4613.6000304@redhat.com> <51BFB0E3.4060502@redhat.com> <20130618070900.GD2336@tucnak.redhat.com> <51C0B1EB.5060203@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <51C0B1EB.5060203@redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) On Tue, Jun 18, 2013 at 02:15:55PM -0500, Aldy Hernandez wrote: > >Please move simduid after force_vect, so that it is better packed. > > Fixed. > > I also rewrote the builtins to use the internal function doo-dah as > previously suggested. > > Let me know if this is fine with y'all and you (Jakub) can keep this > patch and apply it on top of your pending patchset for clauses. Thanks. Here is the latest version of the patch, including your fixes. I've started to add runtime testcases, but already the first one fails due to issue 1) below. The lastprivate case is hopefully resolved now and we already seem to generate correct (and vectorized) code for say: __attribute__((noinline, noclone)) int foo2 (int *p) { int i, s = 0; #pragma omp simd aligned(a, p : 32) reduction(+:s) lastprivate(l) for (i = 0; i < 1024; i++) { a[i] *= p[i]; s += p[i] + k + (i + 1) * (m + 1); l = a[i] + p[i] + i; } return s; } Issues I'm struggling with are: 1) The linear clause handling is broken, when each SIMD lane has its own private variable, the user code increments that variable by whatever the loop body does (which must be equal to the declared linear_step), but that is only correct if vectorization factor is 1, because otherwise we need to still increment it somewhere by the remaining linear_step * (vf - 1). The array approach is still needed at least for the case where the address of the linear privatized var is taken, but also because of vectorizer deficiencies. For the linear handling, one approach would be to assign the magic array element at the start of the loop body to loop iteration # times linear step (roughly matching foo function below), this one is vectorized, but suboptimal. Another approach is just have another IV that contains the right value, hoping the vectorizer would do the expected thing and add vf * (m + 1) to a new vector IV, but the vectorizer just gives up on this. And the last approach would be to keep the original user's increment in the loop and add another GOMP_SIMD_VF () * linear_step increment somewhere (beginning of the loop body (with corresponding adjustment of the initial vals) or at the end of the loop body (with a corresponding correction after the loop for the final value). But, as baz function below (everything -O3 -mavx2) shows, we don't even merge the two additions and end up with two increments even in the vectorized loop (even for signed types, I believe if neither x += k nor x += 3 * k overflows, then x += 4 * k shouldn't overflow either. So any preferences what code would be best for vectorization? int a[1024] __attribute__((aligned (32))); void foo (int k, int m) { int i; for (i = 0; i < 1024; i++) a[i] = k + i * (m + 1); } void bar (int k, int m) { int i, k2 = k; for (i = 0; i < 1024; i++) { a[i] = k2; k2 += m + 1; } } void baz (int k, int m) { int i; for (i = 0; i < 1024; i++) { a[i] += k; a[i] += 3 * k; } } 2) the magic array approach still creates too many variables in memory which aren't promoted to registers only. The patch has code to shrink the arrays to the actual vectorization factor, but still say int[1] is not a GIMPLE type. Any suggestions how to improve this? Already for the shrinking the code looks for all uses of the magic GOMP_SIMD_{VF,LANE,LAST_LANE} internal builtins and I need to walk the whole IL to find those, perhaps another alternative would be just to walk all stmts in the function before vectorization, looking say using walk_gimple_op for all stmts mentioning the magic "omp simd array" temporary arrays, and note whether all of them are convertible from the array representation to something else, note those in a hash table and then just after vectorization transform all that can be safely transformed (if they have vf 1 to just scalar vars with tweaking all uses, for vf > 1 into vectors, and just keep what has been addressable as arrays). 3) for better vectorization results, at least for now I think we'll want to use the array approach even for private iteration vars in collapse>1 loops, as they don't seem to be vectorizable right now otherwise (vectorizer not liking the PHIs). Jakub --- gcc/internal-fn.def.jj 2013-03-20 10:08:13.024245282 +0100 +++ gcc/internal-fn.def 2013-06-21 11:23:00.616680668 +0200 @@ -40,3 +40,6 @@ along with GCC; see the file COPYING3. DEF_INTERNAL_FN (LOAD_LANES, ECF_CONST | ECF_LEAF) DEF_INTERNAL_FN (STORE_LANES, ECF_CONST | ECF_LEAF) +DEF_INTERNAL_FN (GOMP_SIMD_LANE, ECF_NOVOPS | ECF_LEAF | ECF_NOTHROW) +DEF_INTERNAL_FN (GOMP_SIMD_VF, ECF_CONST | ECF_LEAF | ECF_NOTHROW) +DEF_INTERNAL_FN (GOMP_SIMD_LAST_LANE, ECF_CONST | ECF_LEAF | ECF_NOTHROW) --- gcc/omp-low.c.jj 2013-06-21 08:38:19.085761648 +0200 +++ gcc/omp-low.c 2013-06-24 17:57:57.758930059 +0200 @@ -2508,6 +2508,73 @@ omp_clause_aligned_alignment (tree claus return build_int_cst (integer_type_node, al); } +/* Return maximum possible vectorization factor for the target. */ + +static int +omp_max_vf (void) +{ + if (!optimize + || optimize_debug + || (!flag_tree_vectorize + && global_options_set.x_flag_tree_vectorize)) + return 1; + + int vs = targetm.vectorize.autovectorize_vector_sizes (); + if (vs) + { + vs = 1 << floor_log2 (vs); + return vs; + } + enum machine_mode vqimode = targetm.vectorize.preferred_simd_mode (QImode); + if (GET_MODE_CLASS (vqimode) == MODE_VECTOR_INT) + return GET_MODE_NUNITS (vqimode); + return 1; +} + +/* Helper function of lower_rec_input_clauses, used for #pragma omp simd + privatization. */ + +static bool +lower_rec_simd_input_clauses (tree new_var, omp_context *ctx, int &max_vf, + tree &idx, tree &lane, tree &ivar, tree &lvar) +{ + if (max_vf == 0) + { + max_vf = omp_max_vf (); + if (max_vf > 1) + { + tree c = find_omp_clause (gimple_omp_for_clauses (ctx->stmt), + OMP_CLAUSE_SAFELEN); + if (c + && compare_tree_int (OMP_CLAUSE_SAFELEN_EXPR (c), max_vf) == -1) + max_vf = tree_low_cst (OMP_CLAUSE_SAFELEN_EXPR (c), 0); + } + if (max_vf > 1) + { + idx = create_tmp_var (unsigned_type_node, NULL); + lane = create_tmp_var (unsigned_type_node, NULL); + } + } + if (max_vf == 1) + return false; + + tree atype = build_array_type_nelts (TREE_TYPE (new_var), max_vf); + tree avar = create_tmp_var_raw (atype, NULL); + if (TREE_ADDRESSABLE (new_var)) + TREE_ADDRESSABLE (avar) = 1; + DECL_ATTRIBUTES (avar) + = tree_cons (get_identifier ("omp simd array"), NULL, + DECL_ATTRIBUTES (avar)); + gimple_add_tmp_var (avar); + ivar = build4 (ARRAY_REF, TREE_TYPE (new_var), avar, idx, + NULL_TREE, NULL_TREE); + lvar = build4 (ARRAY_REF, TREE_TYPE (new_var), avar, lane, + NULL_TREE, NULL_TREE); + SET_DECL_VALUE_EXPR (new_var, lvar); + DECL_HAS_VALUE_EXPR_P (new_var) = 1; + return true; +} + /* Generate code to implement the input clauses, FIRSTPRIVATE and COPYIN, from the receiver (aka child) side and initializers for REFERENCE_TYPE private variables. Initialization statements go in ILIST, while calls @@ -2515,15 +2582,43 @@ omp_clause_aligned_alignment (tree claus static void lower_rec_input_clauses (tree clauses, gimple_seq *ilist, gimple_seq *dlist, - omp_context *ctx) + omp_context *ctx, struct omp_for_data *fd) { tree c, dtor, copyin_seq, x, ptr; bool copyin_by_ref = false; bool lastprivate_firstprivate = false; int pass; + bool is_simd = (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR + && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD); + int max_vf = 0; + tree lane = NULL_TREE, idx = NULL_TREE; + tree ivar = NULL_TREE, lvar = NULL_TREE; + gimple_seq llist[2] = { NULL, NULL }; copyin_seq = NULL; + /* Enforce simdlen 1 in simd loops with data sharing clauses referencing + variable sized vars. That is unnecessarily hard to support and very + unlikely to result in vectorized code anyway. */ + if (is_simd) + for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) + switch (OMP_CLAUSE_CODE (c)) + { + case OMP_CLAUSE_REDUCTION: + if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)) + max_vf = 1; + /* FALLTHRU */ + case OMP_CLAUSE_PRIVATE: + case OMP_CLAUSE_FIRSTPRIVATE: + case OMP_CLAUSE_LASTPRIVATE: + case OMP_CLAUSE_LINEAR: + if (is_variable_sized (OMP_CLAUSE_DECL (c))) + max_vf = 1; + break; + default: + continue; + } + /* Do all the fixed sized types in the first pass, and the variable sized types in the second pass. This makes sure that the scalar arguments to the variable sized types are processed before we use them in the @@ -2553,7 +2648,11 @@ lower_rec_input_clauses (tree clauses, g case OMP_CLAUSE_COPYIN: case OMP_CLAUSE_REDUCTION: case OMP_CLAUSE_LINEAR: + break; case OMP_CLAUSE__LOOPTEMP_: + /* Handle _looptemp_ clauses only on parallel. */ + if (fd) + continue; break; case OMP_CLAUSE_LASTPRIVATE: if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)) @@ -2737,6 +2836,34 @@ lower_rec_input_clauses (tree clauses, g x = NULL; do_private: x = lang_hooks.decls.omp_clause_default_ctor (c, new_var, x); + if (is_simd) + { + tree y = lang_hooks.decls.omp_clause_dtor (c, new_var); + if ((TREE_ADDRESSABLE (new_var) || x || y + || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) + && lower_rec_simd_input_clauses (new_var, ctx, max_vf, + idx, lane, ivar, lvar)) + { + if (x) + x = lang_hooks.decls.omp_clause_default_ctor (c, ivar, + x); + if (x) + gimplify_and_add (x, &llist[0]); + if (y) + { + y = lang_hooks.decls.omp_clause_dtor (c, ivar); + if (y) + { + gimple_seq tseq = NULL; + + dtor = y; + gimplify_stmt (&dtor, &tseq); + gimple_seq_add_seq (&llist[1], tseq); + } + } + break; + } + } if (x) gimplify_and_add (x, ilist); /* FALLTHRU */ @@ -2779,10 +2906,74 @@ lower_rec_input_clauses (tree clauses, g } do_firstprivate: x = build_outer_var_ref (var, ctx); + if (is_simd) + { + if (lower_rec_simd_input_clauses (new_var, ctx, max_vf, idx, + lane, ivar, lvar)) + { + if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR) + { + tree stept = POINTER_TYPE_P (TREE_TYPE (x)) + ? sizetype : TREE_TYPE (x); + tree t = fold_convert (stept, + OMP_CLAUSE_LINEAR_STEP (c)); + tree this_idx = idx; + if (gimple_omp_for_combined_into_p (ctx->stmt)) + { + tree c = find_omp_clause (clauses, + OMP_CLAUSE__LOOPTEMP_); + gcc_assert (c); + tree l = OMP_CLAUSE_DECL (c); + if (fd->collapse == 1) + { + tree n1 = fd->loop.n1; + tree step = fd->loop.step; + tree itype = TREE_TYPE (l); + if (POINTER_TYPE_P (itype)) + itype = signed_type_for (itype); + l = fold_build2 (MINUS_EXPR, itype, l, n1); + if (TYPE_UNSIGNED (itype) + && fd->loop.cond_code == GT_EXPR) + l = fold_build2 (TRUNC_DIV_EXPR, itype, + fold_build1 (NEGATE_EXPR, + itype, l), + fold_build1 (NEGATE_EXPR, + itype, + step)); + else + l = fold_build2 (TRUNC_DIV_EXPR, itype, + l, step); + } + this_idx + = fold_build2 (PLUS_EXPR, TREE_TYPE (l), l, + fold_convert (TREE_TYPE (l), + idx)); + } + t = fold_build2 (MULT_EXPR, stept, + fold_convert (stept, this_idx), t); + if (POINTER_TYPE_P (TREE_TYPE (x))) + x = fold_build2 (POINTER_PLUS_EXPR, + TREE_TYPE (x), x, t); + else + x = fold_build2 (PLUS_EXPR, TREE_TYPE (x), x, t); + } + x = lang_hooks.decls.omp_clause_copy_ctor (c, ivar, x); + gimplify_and_add (x, &llist[0]); + x = lang_hooks.decls.omp_clause_dtor (c, ivar); + if (x) + { + gimple_seq tseq = NULL; + + dtor = x; + gimplify_stmt (&dtor, &tseq); + gimple_seq_add_seq (&llist[1], tseq); + } + break; + } + } x = lang_hooks.decls.omp_clause_copy_ctor (c, new_var, x); gimplify_and_add (x, ilist); goto do_dtor; - break; case OMP_CLAUSE__LOOPTEMP_: gcc_assert (is_parallel_ctx (ctx)); @@ -2805,6 +2996,8 @@ lower_rec_input_clauses (tree clauses, g tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c); x = build_outer_var_ref (var, ctx); + /* FIXME: Not handled yet. */ + gcc_assert (!is_simd); if (is_reference (var)) x = build_fold_addr_expr_loc (clause_loc, x); SET_DECL_VALUE_EXPR (placeholder, x); @@ -2819,7 +3012,31 @@ lower_rec_input_clauses (tree clauses, g { x = omp_reduction_init (c, TREE_TYPE (new_var)); gcc_assert (TREE_CODE (TREE_TYPE (new_var)) != ARRAY_TYPE); - gimplify_assign (new_var, x, ilist); + if (is_simd + && lower_rec_simd_input_clauses (new_var, ctx, max_vf, + idx, lane, ivar, lvar)) + { + enum tree_code code = OMP_CLAUSE_REDUCTION_CODE (c); + tree ref = build_outer_var_ref (var, ctx); + + gimplify_assign (ivar, x, &llist[0]); + + /* reduction(-:var) sums up the partial results, so it + acts identically to reduction(+:var). */ + if (code == MINUS_EXPR) + code = PLUS_EXPR; + + x = build2 (code, TREE_TYPE (ref), ref, ivar); + ref = build_outer_var_ref (var, ctx); + gimplify_assign (ref, x, &llist[1]); + } + else + { + gimplify_assign (new_var, x, ilist); + if (is_simd) + gimplify_assign (build_outer_var_ref (var, ctx), + new_var, dlist); + } } break; @@ -2829,6 +3046,49 @@ lower_rec_input_clauses (tree clauses, g } } + if (lane) + { + tree uid = create_tmp_var (ptr_type_node, "simduid"); + gimple g + = gimple_build_call_internal (IFN_GOMP_SIMD_LANE, 1, uid); + gimple_call_set_lhs (g, lane); + gimple_stmt_iterator gsi = gsi_start_1 (gimple_omp_body_ptr (ctx->stmt)); + gsi_insert_before_without_update (&gsi, g, GSI_SAME_STMT); + c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__SIMDUID_); + OMP_CLAUSE__SIMDUID__DECL (c) = uid; + OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (ctx->stmt); + gimple_omp_for_set_clauses (ctx->stmt, c); + g = gimple_build_assign_with_ops (INTEGER_CST, lane, + build_int_cst (unsigned_type_node, 0), + NULL_TREE); + gimple_seq_add_stmt (ilist, g); + for (int i = 0; i < 2; i++) + if (llist[i]) + { + tree vf = create_tmp_var (unsigned_type_node, NULL); + g = gimple_build_call_internal (IFN_GOMP_SIMD_VF, 1, uid); + gimple_call_set_lhs (g, vf); + gimple_seq *seq = i == 0 ? ilist : dlist; + gimple_seq_add_stmt (seq, g); + tree t = build_int_cst (unsigned_type_node, 0); + g = gimple_build_assign_with_ops (INTEGER_CST, idx, t, NULL_TREE); + gimple_seq_add_stmt (seq, g); + tree body = create_artificial_label (UNKNOWN_LOCATION); + tree header = create_artificial_label (UNKNOWN_LOCATION); + tree end = create_artificial_label (UNKNOWN_LOCATION); + gimple_seq_add_stmt (seq, gimple_build_goto (header)); + gimple_seq_add_stmt (seq, gimple_build_label (body)); + gimple_seq_add_seq (seq, llist[i]); + t = build_int_cst (unsigned_type_node, 1); + g = gimple_build_assign_with_ops (PLUS_EXPR, idx, idx, t); + gimple_seq_add_stmt (seq, g); + gimple_seq_add_stmt (seq, gimple_build_label (header)); + g = gimple_build_cond (LT_EXPR, idx, vf, body, end); + gimple_seq_add_stmt (seq, g); + gimple_seq_add_stmt (seq, gimple_build_label (end)); + } + } + /* The copyin sequence is not to be executed by the main thread, since that would result in self-copies. Perhaps not visible to scalars, but it certainly is to C++ operator=. */ @@ -2849,11 +3109,30 @@ lower_rec_input_clauses (tree clauses, g happens after firstprivate copying in all threads. */ if (copyin_by_ref || lastprivate_firstprivate) { - /* Don't add any barrier for #pragma omp simd. */ + /* Don't add any barrier for #pragma omp simd or + #pragma omp distribute. */ if (gimple_code (ctx->stmt) != GIMPLE_OMP_FOR - || gimple_omp_for_kind (ctx->stmt) != GF_OMP_FOR_KIND_SIMD) + || gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_FOR) gimplify_and_add (build_omp_barrier (), ilist); } + + /* If max_vf is non-NULL, then we can use only vectorization factor + up to the max_vf we chose. So stick it into safelen clause. */ + if (max_vf) + { + tree c = find_omp_clause (gimple_omp_for_clauses (ctx->stmt), + OMP_CLAUSE_SAFELEN); + if (c == NULL_TREE + || compare_tree_int (OMP_CLAUSE_SAFELEN_EXPR (c), + max_vf) == 1) + { + c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN); + OMP_CLAUSE_SAFELEN_EXPR (c) = build_int_cst (integer_type_node, + max_vf); + OMP_CLAUSE_CHAIN (c) = gimple_omp_for_clauses (ctx->stmt); + gimple_omp_for_set_clauses (ctx->stmt, c); + } + } } @@ -2865,8 +3144,9 @@ static void lower_lastprivate_clauses (tree clauses, tree predicate, gimple_seq *stmt_list, omp_context *ctx) { - tree x, c, label = NULL; + tree x, c, label = NULL, orig_clauses = clauses; bool par_clauses = false; + tree simduid = NULL, lastlane = NULL; /* Early exit if there are no lastprivate or linear clauses. */ for (; clauses ; clauses = OMP_CLAUSE_CHAIN (clauses)) @@ -2910,6 +3190,14 @@ lower_lastprivate_clauses (tree clauses, gimple_seq_add_stmt (stmt_list, gimple_build_label (label_true)); } + if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR + && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD) + { + simduid = find_omp_clause (orig_clauses, OMP_CLAUSE__SIMDUID_); + if (simduid) + simduid = OMP_CLAUSE__SIMDUID__DECL (simduid); + } + for (c = clauses; c ;) { tree var, new_var; @@ -2922,6 +3210,31 @@ lower_lastprivate_clauses (tree clauses, var = OMP_CLAUSE_DECL (c); new_var = lookup_decl (var, ctx); + if (simduid && DECL_HAS_VALUE_EXPR_P (new_var)) + { + tree val = DECL_VALUE_EXPR (new_var); + if (TREE_CODE (val) == ARRAY_REF + && VAR_P (TREE_OPERAND (val, 0)) + && lookup_attribute ("omp simd array", + DECL_ATTRIBUTES (TREE_OPERAND (val, + 0)))) + { + if (lastlane == NULL) + { + lastlane = create_tmp_var (unsigned_type_node, NULL); + gimple g + = gimple_build_call_internal (IFN_GOMP_SIMD_LAST_LANE, + 2, simduid, + TREE_OPERAND (val, 1)); + gimple_call_set_lhs (g, lastlane); + gimple_seq_add_stmt (stmt_list, g); + } + new_var = build4 (ARRAY_REF, TREE_TYPE (val), + TREE_OPERAND (val, 0), lastlane, + NULL_TREE, NULL_TREE); + } + } + if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)) { @@ -2971,6 +3284,11 @@ lower_reduction_clauses (tree clauses, g tree x, c; int count = 0; + /* SIMD reductions are handled in lower_rec_input_clauses. */ + if (gimple_code (ctx->stmt) == GIMPLE_OMP_FOR + && gimple_omp_for_kind (ctx->stmt) == GF_OMP_FOR_KIND_SIMD) + return; + /* First see if there is exactly one reduction clause. Use OMP_ATOMIC update in that case, otherwise use a lock. */ for (c = clauses; c && count < 2; c = OMP_CLAUSE_CHAIN (c)) @@ -5691,6 +6009,8 @@ expand_omp_simd (struct omp_region *regi int i; tree safelen = find_omp_clause (gimple_omp_for_clauses (fd->for_stmt), OMP_CLAUSE_SAFELEN); + tree simduid = find_omp_clause (gimple_omp_for_clauses (fd->for_stmt), + OMP_CLAUSE__SIMDUID_); tree n1, n2; type = TREE_TYPE (fd->loop.v); @@ -5902,11 +6222,19 @@ expand_omp_simd (struct omp_region *regi loop->safelen = INT_MAX; else loop->safelen = tree_low_cst (safelen, 1); + if (loop->safelen == 1) + loop->safelen = 0; + } + if (simduid) + { + loop->simduid = OMP_CLAUSE__SIMDUID__DECL (simduid); + cfun->has_simduid_loops = true; } /* If not -fno-tree-vectorize, hint that we want to vectorize the loop. */ - if (flag_tree_vectorize - || !global_options_set.x_flag_tree_vectorize) + if ((flag_tree_vectorize + || !global_options_set.x_flag_tree_vectorize) + && loop->safelen > 1) { loop->force_vect = true; cfun->has_force_vect_loops = true; @@ -7107,7 +7435,7 @@ lower_omp_sections (gimple_stmt_iterator dlist = NULL; ilist = NULL; lower_rec_input_clauses (gimple_omp_sections_clauses (stmt), - &ilist, &dlist, ctx); + &ilist, &dlist, ctx, NULL); new_body = gimple_omp_body (stmt); gimple_omp_set_body (stmt, NULL); @@ -7315,7 +7643,7 @@ lower_omp_single (gimple_stmt_iterator * bind_body = NULL; dlist = NULL; lower_rec_input_clauses (gimple_omp_single_clauses (single_stmt), - &bind_body, &dlist, ctx); + &bind_body, &dlist, ctx, NULL); lower_omp (gimple_omp_body_ptr (single_stmt), ctx); gimple_seq_add_stmt (&bind_body, single_stmt); @@ -7578,7 +7906,7 @@ static void lower_omp_for (gimple_stmt_iterator *gsi_p, omp_context *ctx) { tree *rhs_p, block; - struct omp_for_data fd; + struct omp_for_data fd, *fdp = NULL; gimple stmt = gsi_stmt (*gsi_p), new_stmt; gimple_seq omp_for_body, body, dlist; size_t i; @@ -7605,41 +7933,11 @@ lower_omp_for (gimple_stmt_iterator *gsi gimple_bind_append_vars (new_stmt, vars); } - /* The pre-body and input clauses go before the lowered GIMPLE_OMP_FOR. */ - dlist = NULL; - body = NULL; - lower_rec_input_clauses (gimple_omp_for_clauses (stmt), &body, &dlist, ctx); - gimple_seq_add_seq (&body, gimple_omp_for_pre_body (stmt)); - - lower_omp (gimple_omp_body_ptr (stmt), ctx); - - /* Lower the header expressions. At this point, we can assume that - the header is of the form: - - #pragma omp for (V = VAL1; V {<|>|<=|>=} VAL2; V = V [+-] VAL3) - - We just need to make sure that VAL1, VAL2 and VAL3 are lowered - using the .omp_data_s mapping, if needed. */ - for (i = 0; i < gimple_omp_for_collapse (stmt); i++) - { - rhs_p = gimple_omp_for_initial_ptr (stmt, i); - if (!is_gimple_min_invariant (*rhs_p)) - *rhs_p = get_formal_tmp_var (*rhs_p, &body); - - rhs_p = gimple_omp_for_final_ptr (stmt, i); - if (!is_gimple_min_invariant (*rhs_p)) - *rhs_p = get_formal_tmp_var (*rhs_p, &body); - - rhs_p = &TREE_OPERAND (gimple_omp_for_incr (stmt, i), 1); - if (!is_gimple_min_invariant (*rhs_p)) - *rhs_p = get_formal_tmp_var (*rhs_p, &body); - } - - /* Once lowered, extract the bounds and clauses. */ - extract_omp_for_data (stmt, &fd, NULL); - if (gimple_omp_for_combined_into_p (stmt)) { + extract_omp_for_data (stmt, &fd, NULL); + fdp = &fd; + /* We need two temporaries with fd.loop.v type (istart/iend) and then (fd.collapse - 1) temporaries with the same type for count2 ... countN-1 vars if not constant. */ @@ -7674,8 +7972,41 @@ lower_omp_for (gimple_stmt_iterator *gsi *pc = clauses; } - if (gimple_omp_for_kind (fd.for_stmt) != GF_OMP_FOR_KIND_SIMD) - lower_omp_for_lastprivate (&fd, &body, &dlist, ctx); + /* The pre-body and input clauses go before the lowered GIMPLE_OMP_FOR. */ + dlist = NULL; + body = NULL; + lower_rec_input_clauses (gimple_omp_for_clauses (stmt), &body, &dlist, ctx, + fdp); + gimple_seq_add_seq (&body, gimple_omp_for_pre_body (stmt)); + + lower_omp (gimple_omp_body_ptr (stmt), ctx); + + /* Lower the header expressions. At this point, we can assume that + the header is of the form: + + #pragma omp for (V = VAL1; V {<|>|<=|>=} VAL2; V = V [+-] VAL3) + + We just need to make sure that VAL1, VAL2 and VAL3 are lowered + using the .omp_data_s mapping, if needed. */ + for (i = 0; i < gimple_omp_for_collapse (stmt); i++) + { + rhs_p = gimple_omp_for_initial_ptr (stmt, i); + if (!is_gimple_min_invariant (*rhs_p)) + *rhs_p = get_formal_tmp_var (*rhs_p, &body); + + rhs_p = gimple_omp_for_final_ptr (stmt, i); + if (!is_gimple_min_invariant (*rhs_p)) + *rhs_p = get_formal_tmp_var (*rhs_p, &body); + + rhs_p = &TREE_OPERAND (gimple_omp_for_incr (stmt, i), 1); + if (!is_gimple_min_invariant (*rhs_p)) + *rhs_p = get_formal_tmp_var (*rhs_p, &body); + } + + /* Once lowered, extract the bounds and clauses. */ + extract_omp_for_data (stmt, &fd, NULL); + + lower_omp_for_lastprivate (&fd, &body, &dlist, ctx); gimple_seq_add_stmt (&body, stmt); gimple_seq_add_seq (&body, gimple_omp_body (stmt)); @@ -7685,20 +8016,13 @@ lower_omp_for (gimple_stmt_iterator *gsi /* After the loop, add exit clauses. */ lower_reduction_clauses (gimple_omp_for_clauses (stmt), &body, ctx); + gimple_seq_add_seq (&body, dlist); body = maybe_catch_exception (body); /* Region exit marker goes at the end of the loop body. */ gimple_seq_add_stmt (&body, gimple_build_omp_return (fd.have_nowait)); - if (gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_SIMD) - { - dlist = NULL; - lower_lastprivate_clauses (gimple_omp_for_clauses (fd.for_stmt), - NULL_TREE, &dlist, ctx); - gimple_seq_add_seq (&body, dlist); - } - pop_gimplify_context (new_stmt); gimple_bind_append_vars (new_stmt, ctx->block_vars); @@ -8057,7 +8381,7 @@ lower_omp_taskreg (gimple_stmt_iterator par_olist = NULL; par_ilist = NULL; - lower_rec_input_clauses (clauses, &par_ilist, &par_olist, ctx); + lower_rec_input_clauses (clauses, &par_ilist, &par_olist, ctx, NULL); lower_omp (&par_body, ctx); if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL) lower_reduction_clauses (clauses, &par_olist, ctx); --- gcc/tree.h.jj 2013-06-21 08:38:19.083761515 +0200 +++ gcc/tree.h 2013-06-21 09:15:14.351431926 +0200 @@ -456,7 +456,10 @@ enum omp_clause_code OMP_CLAUSE_SECTIONS, /* OpenMP clause: taskgroup. */ - OMP_CLAUSE_TASKGROUP + OMP_CLAUSE_TASKGROUP, + + /* Internally used only clause, holding SIMD uid. */ + OMP_CLAUSE__SIMDUID_ }; /* The definition of tree nodes fills the next several pages. */ @@ -2001,6 +2004,9 @@ extern void protected_set_expr_location #define OMP_CLAUSE_SIMDLEN_EXPR(NODE) \ OMP_CLAUSE_OPERAND (OMP_CLAUSE_SUBCODE_CHECK (NODE, OMP_CLAUSE_SIMDLEN), 0) +#define OMP_CLAUSE__SIMDUID__DECL(NODE) \ + OMP_CLAUSE_OPERAND (OMP_CLAUSE_SUBCODE_CHECK (NODE, OMP_CLAUSE__SIMDUID_), 0) + enum omp_clause_schedule_kind { OMP_CLAUSE_SCHEDULE_STATIC, --- gcc/tree-vectorizer.c.jj 2013-06-12 11:53:15.166216030 +0200 +++ gcc/tree-vectorizer.c 2013-06-24 15:56:37.325925385 +0200 @@ -66,13 +66,209 @@ along with GCC; see the file COPYING3. #include "cfgloop.h" #include "tree-vectorizer.h" #include "tree-pass.h" +#include "hash-table.h" +#include "tree-ssa-propagate.h" /* Loop or bb location. */ LOC vect_location; /* Vector mapping GIMPLE stmt to stmt_vec_info. */ vec stmt_vec_info_vec; + +/* For mapping simduid to vectorization factor. */ + +struct simduid_to_vf : typed_free_remove +{ + unsigned int simduid; + int vf; + + /* hash_table support. */ + typedef simduid_to_vf value_type; + typedef simduid_to_vf compare_type; + static inline hashval_t hash (const value_type *); + static inline int equal (const value_type *, const compare_type *); +}; + +inline hashval_t +simduid_to_vf::hash (const value_type *p) +{ + return p->simduid; +} + +inline int +simduid_to_vf::equal (const value_type *p1, const value_type *p2) +{ + return p1->simduid == p2->simduid; +} + +/* For mapping decl to simduid. */ + +struct decl_to_simduid : typed_free_remove +{ + tree decl; + unsigned int simduid; + + /* hash_table support. */ + typedef decl_to_simduid value_type; + typedef decl_to_simduid compare_type; + static inline hashval_t hash (const value_type *); + static inline int equal (const value_type *, const compare_type *); +}; + +inline hashval_t +decl_to_simduid::hash (const value_type *p) +{ + return DECL_UID (p->decl); +} + +inline int +decl_to_simduid::equal (const value_type *p1, const value_type *p2) +{ + return p1->decl == p2->decl; +} + +/* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF and IFN_GOMP_SIMD_LAST_LANE + into their corresponding constants. */ + +static void +adjust_simduid_builtins (hash_table &htab) +{ + basic_block bb; + + FOR_EACH_BB (bb) + { + gimple_stmt_iterator i; + + for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i)) + { + unsigned int vf = 1; + enum internal_fn ifn; + gimple stmt = gsi_stmt (i); + tree t; + if (!is_gimple_call (stmt) + || !gimple_call_internal_p (stmt)) + continue; + ifn = gimple_call_internal_fn (stmt); + switch (ifn) + { + case IFN_GOMP_SIMD_LANE: + case IFN_GOMP_SIMD_VF: + case IFN_GOMP_SIMD_LAST_LANE: + break; + default: + continue; + } + tree arg = gimple_call_arg (stmt, 0); + gcc_assert (arg != NULL_TREE); + gcc_assert (TREE_CODE (arg) == SSA_NAME); + simduid_to_vf *p = NULL, data; + data.simduid = DECL_UID (SSA_NAME_VAR (arg)); + if (htab.is_created ()) + p = htab.find (&data); + if (p) + vf = p->vf; + switch (ifn) + { + case IFN_GOMP_SIMD_VF: + t = build_int_cst (unsigned_type_node, vf); + break; + case IFN_GOMP_SIMD_LANE: + t = build_int_cst (unsigned_type_node, 0); + break; + case IFN_GOMP_SIMD_LAST_LANE: + t = gimple_call_arg (stmt, 1); + break; + default: + gcc_unreachable (); + } + update_call_from_tree (&i, t); + } + } +} +/* Helper structure for note_simd_array_uses. */ + +struct note_simd_array_uses_struct +{ + hash_table *htab; + unsigned int simduid; +}; + +/* Callback for note_simd_array_uses, called through walk_gimple_op. */ + +static tree +note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data) +{ + struct walk_stmt_info *wi = (struct walk_stmt_info *) data; + struct note_simd_array_uses_struct *ns + = (struct note_simd_array_uses_struct *) wi->info; + + if (TYPE_P (*tp)) + *walk_subtrees = 0; + else if (VAR_P (*tp) + && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp)) + && DECL_CONTEXT (*tp) == current_function_decl) + { + decl_to_simduid data; + if (!ns->htab->is_created ()) + ns->htab->create (15); + data.decl = *tp; + data.simduid = ns->simduid; + decl_to_simduid **slot = ns->htab->find_slot (&data, INSERT); + if (*slot == NULL) + { + decl_to_simduid *p = XNEW (decl_to_simduid); + *p = data; + *slot = p; + } + else if ((*slot)->simduid != ns->simduid) + (*slot)->simduid = -1U; + *walk_subtrees = 0; + } + return NULL_TREE; +} + +/* Find "omp simd array" temporaries and map them to corresponding + simduid. */ + +static void +note_simd_array_uses (hash_table *htab) +{ + basic_block bb; + gimple_stmt_iterator gsi; + struct walk_stmt_info wi; + struct note_simd_array_uses_struct ns; + + memset (&wi, 0, sizeof (wi)); + wi.info = &ns; + ns.htab = htab; + + FOR_EACH_BB (bb) + for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) + { + gimple stmt = gsi_stmt (gsi); + if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt)) + continue; + switch (gimple_call_internal_fn (stmt)) + { + case IFN_GOMP_SIMD_LANE: + case IFN_GOMP_SIMD_VF: + case IFN_GOMP_SIMD_LAST_LANE: + break; + default: + continue; + } + tree lhs = gimple_call_lhs (stmt); + if (lhs == NULL_TREE) + continue; + imm_use_iterator use_iter; + gimple use_stmt; + ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0))); + FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs) + if (!is_gimple_debug (use_stmt)) + walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi); + } +} /* Function vectorize_loops. @@ -86,12 +282,21 @@ vectorize_loops (void) unsigned int vect_loops_num; loop_iterator li; struct loop *loop; + hash_table simduid_to_vf_htab; + hash_table decl_to_simduid_htab; vect_loops_num = number_of_loops (cfun); /* Bail out if there are no loops. */ if (vect_loops_num <= 1) - return 0; + { + if (cfun->has_simduid_loops) + adjust_simduid_builtins (simduid_to_vf_htab); + return 0; + } + + if (cfun->has_simduid_loops) + note_simd_array_uses (&decl_to_simduid_htab); init_stmt_vec_info_vec (); @@ -126,6 +331,17 @@ vectorize_loops (void) /* Now that the loop has been vectorized, allow it to be unrolled etc. */ loop->force_vect = false; + + if (loop->simduid) + { + simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf); + if (!simduid_to_vf_htab.is_created ()) + simduid_to_vf_htab.create (15); + simduid_to_vf_data->simduid = DECL_UID (loop->simduid); + simduid_to_vf_data->vf = loop_vinfo->vectorization_factor; + *simduid_to_vf_htab.find_slot (simduid_to_vf_data, INSERT) + = simduid_to_vf_data; + } } vect_location = UNKNOWN_LOC; @@ -153,6 +369,40 @@ vectorize_loops (void) free_stmt_vec_info_vec (); + /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE} builtins. */ + if (cfun->has_simduid_loops) + adjust_simduid_builtins (simduid_to_vf_htab); + + /* Shrink any "omp array simd" temporary arrays to the + actual vectorization factors. */ + if (decl_to_simduid_htab.is_created ()) + { + for (hash_table ::iterator iter + = decl_to_simduid_htab.begin (); + iter != decl_to_simduid_htab.end (); ++iter) + if ((*iter).simduid != -1U) + { + tree decl = (*iter).decl; + int vf = 1; + if (simduid_to_vf_htab.is_created ()) + { + simduid_to_vf *p = NULL, data; + data.simduid = (*iter).simduid; + p = simduid_to_vf_htab.find (&data); + if (p) + vf = p->vf; + } + tree atype + = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf); + TREE_TYPE (decl) = atype; + relayout_decl (decl); + } + + decl_to_simduid_htab.dispose (); + } + if (simduid_to_vf_htab.is_created ()) + simduid_to_vf_htab.dispose (); + if (num_vectorized_loops > 0) { /* If we vectorized any loop only virtual SSA form needs to be updated. --- gcc/tree-vectorizer.h.jj 2013-06-12 11:53:15.212215271 +0200 +++ gcc/tree-vectorizer.h 2013-06-21 09:10:38.662575194 +0200 @@ -576,6 +576,9 @@ typedef struct _stmt_vec_info { /* For loads only, true if this is a gather load. */ bool gather_p; bool stride_load_p; + + /* For both loads and stores. */ + bool simd_lane_access_p; } *stmt_vec_info; /* Access Functions. */ @@ -591,6 +594,7 @@ typedef struct _stmt_vec_info { #define STMT_VINFO_DATA_REF(S) (S)->data_ref_info #define STMT_VINFO_GATHER_P(S) (S)->gather_p #define STMT_VINFO_STRIDE_LOAD_P(S) (S)->stride_load_p +#define STMT_VINFO_SIMD_LANE_ACCESS_P(S) (S)->simd_lane_access_p #define STMT_VINFO_DR_BASE_ADDRESS(S) (S)->dr_base_address #define STMT_VINFO_DR_INIT(S) (S)->dr_init --- gcc/tree-data-ref.c.jj 2013-06-12 11:53:15.265214394 +0200 +++ gcc/tree-data-ref.c 2013-06-21 09:40:51.912195754 +0200 @@ -4331,10 +4331,25 @@ get_references_in_stmt (gimple stmt, vec /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects. As we cannot model data-references to not spelled out accesses give up if they may occur. */ - if ((stmt_code == GIMPLE_CALL - && !(gimple_call_flags (stmt) & ECF_CONST)) - || (stmt_code == GIMPLE_ASM - && (gimple_asm_volatile_p (stmt) || gimple_vuse (stmt)))) + if (stmt_code == GIMPLE_CALL + && !(gimple_call_flags (stmt) & ECF_CONST)) + { + /* Allow IFN_GOMP_SIMD_LANE in their own loops. */ + if (gimple_call_internal_p (stmt) + && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE) + { + struct loop *loop = gimple_bb (stmt)->loop_father; + tree uid = gimple_call_arg (stmt, 0); + gcc_assert (TREE_CODE (uid) == SSA_NAME); + if (loop == NULL + || loop->simduid != SSA_NAME_VAR (uid)) + clobbers_memory = true; + } + else + clobbers_memory = true; + } + else if (stmt_code == GIMPLE_ASM + && (gimple_asm_volatile_p (stmt) || gimple_vuse (stmt))) clobbers_memory = true; if (!gimple_vuse (stmt)) --- gcc/tree-inline.c.jj 2013-05-27 09:22:21.000000000 +0200 +++ gcc/tree-inline.c 2013-06-21 17:58:15.232373759 +0200 @@ -2345,6 +2345,8 @@ copy_cfg_body (copy_body_data * id, gcov get_loop (src_cfun, 0)); /* Defer to cfgcleanup to update loop-father fields of basic-blocks. */ loops_state_set (LOOPS_NEED_FIXUP); + cfun->has_force_vect_loops |= src_cfun->has_force_vect_loops; + cfun->has_simduid_loops |= src_cfun->has_simduid_loops; } /* If the loop tree in the source function needed fixup, mark the --- gcc/fortran/f95-lang.c.jj 2013-06-12 11:53:15.148216324 +0200 +++ gcc/fortran/f95-lang.c 2013-06-21 09:10:38.674576091 +0200 @@ -538,6 +538,7 @@ gfc_builtin_function (tree decl) #define ATTR_CONST_NOTHROW_LEAF_LIST (ECF_NOTHROW | ECF_LEAF | ECF_CONST) #define ATTR_NOTHROW_LIST (ECF_NOTHROW) #define ATTR_CONST_NOTHROW_LIST (ECF_NOTHROW | ECF_CONST) +#define ATTR_NOVOPS_NOTHROW_LEAF_LIST (ECF_NOTHROW | ECF_LEAF | ECF_NOVOPS) static void gfc_define_builtin (const char *name, tree type, enum built_in_function code, --- gcc/function.h.jj 2013-06-12 11:53:15.198215502 +0200 +++ gcc/function.h 2013-06-21 09:10:38.682576482 +0200 @@ -654,6 +654,10 @@ struct GTY(()) function { /* Nonzero if the current function contains any loops with loop->force_vect set. */ unsigned int has_force_vect_loops : 1; + + /* Nonzero if the current function contains any loops with + nonzero value in loop->simduid. */ + unsigned int has_simduid_loops : 1; }; /* Add the decl D to the local_decls list of FUN. */ --- gcc/tree-ssa-ccp.c.jj 2013-05-13 16:46:18.000000000 +0200 +++ gcc/tree-ssa-ccp.c 2013-06-24 14:04:04.673006408 +0200 @@ -626,6 +626,22 @@ likely_value (gimple stmt) if (has_constant_operand) all_undefined_operands = false; + if (has_undefined_operand + && code == GIMPLE_CALL + && gimple_call_internal_p (stmt)) + switch (gimple_call_internal_fn (stmt)) + { + /* These 3 builtins use the first argument just as a magic + way how to find out a decl uid. */ + case IFN_GOMP_SIMD_LANE: + case IFN_GOMP_SIMD_VF: + case IFN_GOMP_SIMD_LAST_LANE: + has_undefined_operand = false; + break; + default: + break; + } + /* If the operation combines operands like COMPLEX_EXPR make sure to not mark the result UNDEFINED if only one part of the result is undefined. */ --- gcc/tree-pretty-print.c.jj 2013-06-21 08:38:19.082761445 +0200 +++ gcc/tree-pretty-print.c 2013-06-21 09:15:14.347431350 +0200 @@ -595,6 +595,13 @@ dump_omp_clause (pretty_printer *buffer, pp_character (buffer, ')'); break; + case OMP_CLAUSE__SIMDUID_: + pp_string (buffer, "_simduid_("); + dump_generic_node (buffer, OMP_CLAUSE__SIMDUID__DECL (clause), + spc, flags, false); + pp_character (buffer, ')'); + break; + case OMP_CLAUSE_INBRANCH: pp_string (buffer, "inbranch"); break; --- gcc/cfgloop.h.jj 2013-06-12 11:53:15.383212443 +0200 +++ gcc/cfgloop.h 2013-06-21 09:58:39.000000000 +0200 @@ -177,6 +177,11 @@ struct GTY ((chain_next ("%h.next"))) lo /* True if we should try harder to vectorize this loop. */ bool force_vect; + /* For SIMD loops, this is a unique identifier of the loop, referenced + by IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LANE and IFN_GOMP_SIMD_LAST_LANE + builtins. */ + tree simduid; + /* Upper bound on number of iterations of a loop. */ struct nb_iter_bound *bounds; --- gcc/Makefile.in.jj 2013-06-14 18:46:39.742908279 +0200 +++ gcc/Makefile.in 2013-06-21 09:10:38.664575375 +0200 @@ -2633,7 +2633,7 @@ tree-vect-data-refs.o: tree-vect-data-re tree-vectorizer.o: tree-vectorizer.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \ $(DUMPFILE_H) $(TM_H) $(GGC_H) $(TREE_H) $(TREE_FLOW_H) \ $(CFGLOOP_H) $(TREE_PASS_H) $(TREE_VECTORIZER_H) \ - $(TREE_PRETTY_PRINT_H) + $(TREE_PRETTY_PRINT_H) $(HASH_TABLE_H) tree-ssa-propagate.h tree-loop-distribution.o: tree-loop-distribution.c $(CONFIG_H) $(SYSTEM_H) \ coretypes.h $(TREE_FLOW_H) $(CFGLOOP_H) $(TREE_DATA_REF_H) $(TREE_PASS_H) tree-parloops.o: tree-parloops.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \ --- gcc/tree-vect-data-refs.c.jj 2013-06-12 11:53:15.290213981 +0200 +++ gcc/tree-vect-data-refs.c 2013-06-21 09:15:14.348431496 +0200 @@ -2877,6 +2877,7 @@ vect_analyze_data_refs (loop_vec_info lo stmt_vec_info stmt_info; tree base, offset, init; bool gather = false; + bool simd_lane_access = false; int vf; if (!dr || !DR_REF (dr)) @@ -2894,12 +2895,17 @@ vect_analyze_data_refs (loop_vec_info lo if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr) || !DR_STEP (dr)) { - /* If target supports vector gather loads, see if they can't - be used. */ - if (loop_vinfo - && DR_IS_READ (dr) + bool maybe_gather + = DR_IS_READ (dr) && !TREE_THIS_VOLATILE (DR_REF (dr)) - && targetm.vectorize.builtin_gather != NULL + && targetm.vectorize.builtin_gather != NULL; + bool maybe_simd_lane_access + = loop_vinfo && loop->simduid; + + /* If target supports vector gather loads, or if this might be + a SIMD lane access, see if they can't be used. */ + if (loop_vinfo + && (maybe_gather || maybe_simd_lane_access) && !nested_in_vect_loop_p (loop, stmt)) { struct data_reference *newdr @@ -2912,14 +2918,59 @@ vect_analyze_data_refs (loop_vec_info lo && DR_STEP (newdr) && integer_zerop (DR_STEP (newdr))) { - dr = newdr; - gather = true; + if (maybe_simd_lane_access) + { + tree off = DR_OFFSET (newdr); + STRIP_NOPS (off); + if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST + && TREE_CODE (off) == MULT_EXPR + && host_integerp (TREE_OPERAND (off, 1), 1)) + { + tree step = TREE_OPERAND (off, 1); + off = TREE_OPERAND (off, 0); + STRIP_NOPS (off); + if (CONVERT_EXPR_P (off) + && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off, + 0))) + < TYPE_PRECISION (TREE_TYPE (off))) + off = TREE_OPERAND (off, 0); + if (TREE_CODE (off) == SSA_NAME) + { + gimple def = SSA_NAME_DEF_STMT (off); + tree reft = TREE_TYPE (DR_REF (newdr)); + if (gimple_call_internal_p (def) + && gimple_call_internal_fn (def) + == IFN_GOMP_SIMD_LANE) + { + tree arg = gimple_call_arg (def, 0); + gcc_assert (TREE_CODE (arg) == SSA_NAME); + arg = SSA_NAME_VAR (arg); + if (arg == loop->simduid + /* For now. */ + && tree_int_cst_equal + (TYPE_SIZE_UNIT (reft), + step)) + { + DR_OFFSET (newdr) = ssize_int (0); + DR_STEP (newdr) = step; + dr = newdr; + simd_lane_access = true; + } + } + } + } + } + if (!simd_lane_access && maybe_gather) + { + dr = newdr; + gather = true; + } } - else + if (!gather && !simd_lane_access) free_data_ref (newdr); } - if (!gather) + if (!gather && !simd_lane_access) { if (dump_enabled_p ()) { @@ -2946,7 +2997,7 @@ vect_analyze_data_refs (loop_vec_info lo if (bb_vinfo) break; - if (gather) + if (gather || simd_lane_access) free_data_ref (dr); return false; } @@ -2979,7 +3030,7 @@ vect_analyze_data_refs (loop_vec_info lo if (bb_vinfo) break; - if (gather) + if (gather || simd_lane_access) free_data_ref (dr); return false; } @@ -2998,7 +3049,7 @@ vect_analyze_data_refs (loop_vec_info lo if (bb_vinfo) break; - if (gather) + if (gather || simd_lane_access) free_data_ref (dr); return false; } @@ -3019,7 +3070,7 @@ vect_analyze_data_refs (loop_vec_info lo if (bb_vinfo) break; - if (gather) + if (gather || simd_lane_access) free_data_ref (dr); return false; } @@ -3154,12 +3205,17 @@ vect_analyze_data_refs (loop_vec_info lo if (bb_vinfo) break; - if (gather) + if (gather || simd_lane_access) free_data_ref (dr); return false; } STMT_VINFO_DATA_REF (stmt_info) = dr; + if (simd_lane_access) + { + STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true; + datarefs[i] = dr; + } /* Set vectype for STMT. */ scalar_type = TREE_TYPE (DR_REF (dr)); @@ -3180,7 +3236,7 @@ vect_analyze_data_refs (loop_vec_info lo if (bb_vinfo) break; - if (gather) + if (gather || simd_lane_access) { STMT_VINFO_DATA_REF (stmt_info) = NULL; free_data_ref (dr); --- gcc/gimplify.c.jj 2013-06-21 08:38:19.067760217 +0200 +++ gcc/gimplify.c 2013-06-21 21:13:38.518787295 +0200 @@ -5814,7 +5814,7 @@ omp_add_variable (struct gimplify_omp_ct flags |= GOVD_SEEN; n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl); - if (n != NULL) + if (n != NULL && n->value != GOVD_ALIGNED) { /* We shouldn't be re-adding the decl with the same data sharing class. */ @@ -5823,7 +5823,8 @@ omp_add_variable (struct gimplify_omp_ct FIRSTPRIVATE and LASTPRIVATE. */ nflags = n->value | flags; gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS) - == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)); + == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE) + || (flags & GOVD_DATA_SHARE_CLASS) == 0); n->value = nflags; return; } @@ -5893,7 +5894,10 @@ omp_add_variable (struct gimplify_omp_ct } } - splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags); + if (n != NULL) + n->value |= flags; + else + splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags); } /* Notice a threadprivate variable DECL used in OpenMP context CTX. @@ -6935,7 +6939,7 @@ gimplify_omp_for (tree *expr_p, gimple_s omp_is_private (gimplify_omp_ctxp, decl, simd); if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0) omp_notice_variable (gimplify_omp_ctxp, decl, true); - else + else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1) { c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR); OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1; @@ -6948,6 +6952,21 @@ gimplify_omp_for (tree *expr_p, gimple_s omp_add_variable (gimplify_omp_ctxp, decl, GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN); } + else + { + bool lastprivate + = (!has_decl_expr + || !bitmap_bit_p (has_decl_expr, DECL_UID (decl))); + c = build_omp_clause (input_location, + lastprivate ? OMP_CLAUSE_LASTPRIVATE + : OMP_CLAUSE_PRIVATE); + OMP_CLAUSE_DECL (c) = decl; + OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt); + omp_add_variable (gimplify_omp_ctxp, decl, + (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE) + | GOVD_SEEN); + c = NULL_TREE; + } } else if (omp_is_private (gimplify_omp_ctxp, decl, simd)) omp_notice_variable (gimplify_omp_ctxp, decl, true); --- gcc/tree.c.jj 2013-06-21 08:38:19.072760564 +0200 +++ gcc/tree.c 2013-06-21 09:12:19.361938147 +0200 @@ -265,7 +265,8 @@ unsigned const char omp_clause_num_ops[] 0, /* OMP_CLAUSE_FOR */ 0, /* OMP_CLAUSE_PARALLEL */ 0, /* OMP_CLAUSE_SECTIONS */ - 0 /* OMP_CLAUSE_TASKGROUP */ + 0, /* OMP_CLAUSE_TASKGROUP */ + 1, /* OMP_CLAUSE__SIMDUID_ */ }; const char * const omp_clause_code_name[] = @@ -308,7 +309,8 @@ const char * const omp_clause_code_name[ "for", "parallel", "sections", - "taskgroup" + "taskgroup", + "_simduid_" }; @@ -11024,6 +11026,7 @@ walk_tree_1 (tree *tp, walk_tree_fn func case OMP_CLAUSE_SAFELEN: case OMP_CLAUSE_SIMDLEN: case OMP_CLAUSE__LOOPTEMP_: + case OMP_CLAUSE__SIMDUID_: WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0)); /* FALLTHRU */ --- gcc/tree-vect-loop.c.jj 2013-06-24 12:42:03.000000000 +0200 +++ gcc/tree-vect-loop.c 2013-06-24 15:31:10.544187794 +0200 @@ -5345,7 +5345,7 @@ vectorizable_induction (gimple phi, gimp bool vectorizable_live_operation (gimple stmt, gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED, - gimple *vec_stmt ATTRIBUTE_UNUSED) + gimple *vec_stmt) { stmt_vec_info stmt_info = vinfo_for_stmt (stmt); loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info); @@ -5365,7 +5365,41 @@ vectorizable_live_operation (gimple stmt return false; if (!is_gimple_assign (stmt)) - return false; + { + if (gimple_call_internal_p (stmt) + && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE + && gimple_call_lhs (stmt) + && loop->simduid + && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME + && loop->simduid + == SSA_NAME_VAR (gimple_call_arg (stmt, 0))) + { + edge e = single_exit (loop); + basic_block merge_bb = e->dest; + imm_use_iterator imm_iter; + use_operand_p use_p; + tree lhs = gimple_call_lhs (stmt); + + FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs) + { + gimple use_stmt = USE_STMT (use_p); + if (gimple_code (use_stmt) == GIMPLE_PHI + || gimple_bb (use_stmt) == merge_bb) + { + if (vec_stmt) + { + tree vfm1 + = build_int_cst (unsigned_type_node, + loop_vinfo->vectorization_factor - 1); + SET_PHI_ARG_DEF (use_stmt, e->dest_idx, vfm1); + } + return true; + } + } + } + + return false; + } if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME) return false; --- gcc/tree-vect-stmts.c.jj 2013-06-12 11:53:15.379212510 +0200 +++ gcc/tree-vect-stmts.c 2013-06-21 16:36:11.206297096 +0200 @@ -1755,6 +1755,14 @@ vectorizable_call (gimple stmt, gimple_s if (nargs == 0 || nargs > 3) return false; + /* Ignore the argument of IFN_GOMP_SIMD_LANE, it is magic. */ + if (gimple_call_internal_p (stmt) + && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE) + { + nargs = 0; + rhs_type = unsigned_type_node; + } + for (i = 0; i < nargs; i++) { tree opvectype; @@ -1830,11 +1838,26 @@ vectorizable_call (gimple stmt, gimple_s fndecl = vectorizable_function (stmt, vectype_out, vectype_in); if (fndecl == NULL_TREE) { - if (dump_enabled_p ()) - dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, - "function is not vectorizable."); - - return false; + if (gimple_call_internal_p (stmt) + && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE + && !slp_node + && loop_vinfo + && LOOP_VINFO_LOOP (loop_vinfo)->simduid + && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME + && LOOP_VINFO_LOOP (loop_vinfo)->simduid + == SSA_NAME_VAR (gimple_call_arg (stmt, 0))) + { + /* We can handle IFN_GOMP_SIMD_LANE by returning a + { 0, 1, 2, ... vf - 1 } vector. */ + gcc_assert (nargs == 0); + } + else + { + if (dump_enabled_p ()) + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, + "function is not vectorizable."); + return false; + } } gcc_assert (!gimple_vuse (stmt)); @@ -1932,9 +1955,30 @@ vectorizable_call (gimple stmt, gimple_s vargs.quick_push (vec_oprnd0); } - new_stmt = gimple_build_call_vec (fndecl, vargs); - new_temp = make_ssa_name (vec_dest, new_stmt); - gimple_call_set_lhs (new_stmt, new_temp); + if (gimple_call_internal_p (stmt) + && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE) + { + tree *v = XALLOCAVEC (tree, nunits_out); + int k; + for (k = 0; k < nunits_out; ++k) + v[k] = build_int_cst (unsigned_type_node, j * nunits_out + k); + tree cst = build_vector (vectype_out, v); + tree new_var + = vect_get_new_vect_var (vectype_out, vect_simple_var, "cst_"); + gimple init_stmt = gimple_build_assign (new_var, cst); + new_temp = make_ssa_name (new_var, init_stmt); + gimple_assign_set_lhs (init_stmt, new_temp); + vect_init_vector_1 (stmt, init_stmt, NULL); + new_temp = make_ssa_name (vec_dest, NULL); + new_stmt = gimple_build_assign (new_temp, + gimple_assign_lhs (init_stmt)); + } + else + { + new_stmt = gimple_build_call_vec (fndecl, vargs); + new_temp = make_ssa_name (vec_dest, new_stmt); + gimple_call_set_lhs (new_stmt, new_temp); + } vect_finish_stmt_generation (stmt, new_stmt, gsi); if (j == 0) @@ -4041,7 +4085,7 @@ vectorizable_store (gimple stmt, gimple_ for (j = 0; j < ncopies; j++) { gimple new_stmt; - gimple ptr_incr; + gimple ptr_incr = NULL; if (j == 0) { @@ -4085,9 +4129,13 @@ vectorizable_store (gimple stmt, gimple_ /* We should have catched mismatched types earlier. */ gcc_assert (useless_type_conversion_p (vectype, TREE_TYPE (vec_oprnd))); - dataref_ptr = vect_create_data_ref_ptr (first_stmt, aggr_type, NULL, - NULL_TREE, &dummy, gsi, - &ptr_incr, false, &inv_p); + bool simd_lane_access_p + = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info); + dataref_ptr + = vect_create_data_ref_ptr (first_stmt, aggr_type, + simd_lane_access_p ? loop : NULL, + NULL_TREE, &dummy, gsi, &ptr_incr, + simd_lane_access_p, &inv_p); gcc_assert (bb_vinfo || !inv_p); } else @@ -4314,7 +4362,7 @@ vectorizable_load (gimple stmt, gimple_s tree dummy; enum dr_alignment_support alignment_support_scheme; tree dataref_ptr = NULL_TREE; - gimple ptr_incr; + gimple ptr_incr = NULL; int nunits = TYPE_VECTOR_SUBPARTS (vectype); int ncopies; int i, j, group_size, group_gap; @@ -4947,9 +4995,14 @@ vectorizable_load (gimple stmt, gimple_s { /* 1. Create the vector or array pointer update chain. */ if (j == 0) - dataref_ptr = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop, - offset, &dummy, gsi, - &ptr_incr, false, &inv_p); + { + bool simd_lane_access_p + = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info); + dataref_ptr + = vect_create_data_ref_ptr (first_stmt, aggr_type, at_loop, + offset, &dummy, gsi, &ptr_incr, + simd_lane_access_p, &inv_p); + } else dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr, gsi, stmt, TYPE_SIZE_UNIT (aggr_type)); --- gcc/internal-fn.c.jj 2013-03-20 10:05:01.624313535 +0100 +++ gcc/internal-fn.c 2013-06-21 11:24:00.572706791 +0200 @@ -109,6 +109,30 @@ expand_STORE_LANES (gimple stmt) expand_insn (get_multi_vector_move (type, vec_store_lanes_optab), 2, ops); } +/* This should get expanded in adjust_simduid_builtins. */ + +static void +expand_GOMP_SIMD_LANE (gimple stmt ATTRIBUTE_UNUSED) +{ + gcc_unreachable (); +} + +/* This should get expanded in adjust_simduid_builtins. */ + +static void +expand_GOMP_SIMD_VF (gimple stmt ATTRIBUTE_UNUSED) +{ + gcc_unreachable (); +} + +/* This should get expanded in adjust_simduid_builtins. */ + +static void +expand_GOMP_SIMD_LAST_LANE (gimple stmt ATTRIBUTE_UNUSED) +{ + gcc_unreachable (); +} + /* Routines to expand each internal function, indexed by function number. Each routine has the prototype: --- libgomp/testsuite/libgomp.c++/simd-1.C.jj 2013-06-24 16:49:32.968179359 +0200 +++ libgomp/testsuite/libgomp.c++/simd-1.C 2013-06-24 17:21:04.648231293 +0200 @@ -0,0 +1,48 @@ +// { dg-do run } +// { dg-options "" } +// { dg-additional-options "-msse2" { target sse2_runtime } } +// { dg-additional-options "-mavx" { target avx_runtime } } + +extern "C" void abort (); +int a[1024] __attribute__((aligned (32))) = { 1 }; +int b[1024] __attribute__((aligned (32))) = { 1 }; +int k, m; + +__attribute__((noinline, noclone)) int +foo1 (int *p) +{ + int i, s = 0; + #pragma omp simd aligned(a, p : 32) linear(k: m + 1) reduction(+:s) + for (i = 0; i < 1024; i++) + { + a[i] *= p[i]; + k += m + 1; + s += p[i] + k; + } + return s; +} + +int +main () +{ +#if __SIZEOF_INT__ >= 4 + int i; + k = 4; + m = 2; + for (i = 0; i < 1024; i++) + { + a[i] = i - 512; + b[i] = (i - 51) % 39; + } + int s = foo1 (b); + for (i = 0; i < 1024; i++) + { + if (b[i] != (i - 51) % 39 + || a[i] != (i - 512) * b[i]) + abort (); + } + if (k != 4 + 3 * 1024 || s != 1596127) + abort (); +#endif + return 0; +}