From patchwork Mon Aug 7 09:59:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Turko X-Patchwork-Id: 1817738 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=n62bAl3f; dkim-atps=neutral Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RKBkz19Pcz1yYC for ; Mon, 7 Aug 2023 20:04:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 28EF5385734B for ; Mon, 7 Aug 2023 10:04:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 28EF5385734B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691402697; bh=H7LJPb9v10UvTF/fKVJElHK2/J+vCc9iXs5aEhS1cd8=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=n62bAl3favjFwzCDoubuoUNTRhVQ+aR9WKmk7aPI9O0v7H5x8fllpJpMhHbqlfdbz suvBOJbvg+BP98Gc6XoWmNVaCs+UCjtTrLOh6GeQ3lCEejeE3p11H9V21ohfI7hWlU kaW+AIFLXwjhvJkuYeZurCWtSFs0vki3yhCFoKIU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 911173858417 for ; Mon, 7 Aug 2023 10:04:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 911173858417 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-99bcf2de59cso634910866b.0 for ; Mon, 07 Aug 2023 03:04:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691402645; x=1692007445; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H7LJPb9v10UvTF/fKVJElHK2/J+vCc9iXs5aEhS1cd8=; b=fZ6ry8gx3/IY3/9IChAXsDKgzck0Tt+UGSVGdYQdkU7Yc0fHnWQ68yNnkVb18LXymx yF0yDbkHCzDvLzNqw5yy+pS+z8X1PLWAtI39uYTh6hE0yxZ6aQ66oYYl4b3evi4bQvh6 elSqjFmowgGSIQ56jRIRCW4c7z0+QrGqrY55+fbyPU3sqG+QpuyaiSc7KvV9kTMKIAY4 tZ326FO8yGfe2zzH58O7EB7HUKhedHeaFxaYNzldQkSy/7jcZPtHPPkOBduasoJ4UcEu w2VC74zbks/n/fBGOUc3x1v2brDU3CkpDOFzr1DDViTqvUTzBEoEilfQSPSGjyT0ejeZ DKlA== X-Gm-Message-State: AOJu0YyfM6supe1mAACijwp3RDmW+zpXq2lrfQ+/WjlvO7qCFV68lKPU SZx4o1GnwEE0uB1DCO5bMfGHZUdsidVl+1hmDZU= X-Google-Smtp-Source: AGHT+IEW6De4De1P2LHliP1eL52agpqEs2X2R6ExvF0+BVpRsAkgEJwN4thJzEG6Me7Xfww4gEVnGw== X-Received: by 2002:a17:907:7857:b0:99c:7551:933e with SMTP id lb23-20020a170907785700b0099c7551933emr7883284ejc.64.1691402644681; Mon, 07 Aug 2023 03:04:04 -0700 (PDT) Received: from amwld-aturko1.us.drwholdings.com ([149.14.21.6]) by smtp.gmail.com with ESMTPSA id la4-20020a170906ad8400b0099bd682f317sm4881305ejb.206.2023.08.07.03.04.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 03:04:04 -0700 (PDT) To: gcc-patches@gcc.gnu.org, Richard Biener Cc: Andrzej Turko Subject: [PATCH 3/3 v4] genmatch: Log line numbers indirectly Date: Mon, 7 Aug 2023 11:59:01 +0200 Message-Id: <20230807095901.267099-4-andrzej.turko@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807095901.267099-1-andrzej.turko@gmail.com> References: <20230807095901.267099-1-andrzej.turko@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrzej Turko via Gcc-patches From: Andrzej Turko Reply-To: Andrzej Turko Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Currently fprintf calls logging to a dump file take line numbers in the match.pd file directly as arguments. When match.pd is edited, referenced code changes line numbers, which causes changes to many fprintf calls and, thus, to many (usually all) .cc files generated by genmatch. This forces make to (unnecessarily) rebuild many .o files. This change replaces those logging fprintf calls with calls to a dedicated logging function. Because it reads the line numbers from the lookup table, it is enough to pass a corresponding index. Thanks to this, when match.pd changes, it is enough to rebuild the file containing the lookup table and, of course, those actually affected by the change. Signed-off-by: Andrzej Turko gcc/ChangeLog: * genmatch.cc: Log line numbers indirectly. --- gcc/genmatch.cc | 95 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 15 deletions(-) diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc index 1deca505603..f46d2e1520d 100644 --- a/gcc/genmatch.cc +++ b/gcc/genmatch.cc @@ -217,10 +217,56 @@ fp_decl_done (FILE *f, const char *trailer) fprintf (header_file, "%s;", trailer); } +/* Line numbers for use by indirect line directives. */ +static vec dbg_line_numbers; + +static void +write_header_declarations (bool gimple, FILE *f) +{ + fprintf (f, "\nextern void\n%s_dump_logs (const char *file1, int line1_id, " + "const char *file2, int line2, bool simplify);\n", + gimple ? "gimple" : "generic"); +} + +static void +define_dump_logs (bool gimple, FILE *f) +{ + if (dbg_line_numbers.is_empty ()) + return; + + fprintf (f , "void\n%s_dump_logs (const char *file1, int line1_id, " + "const char *file2, int line2, bool simplify)\n{\n", + gimple ? "gimple" : "generic"); + + fprintf_indent (f, 2, "static int dbg_line_numbers[%d] = {", + dbg_line_numbers.length ()); + + for (unsigned i = 0; i < dbg_line_numbers.length () - 1; i++) + { + if (i % 20 == 0) + fprintf (f, "\n\t"); + + fprintf (f, "%d, ", dbg_line_numbers[i]); + } + fprintf (f, "%d\n };\n\n", dbg_line_numbers.last ()); + + + fprintf_indent (f, 2, "fprintf (dump_file, \"%%s " + "%%s:%%d, %%s:%%d\\n\",\n"); + fprintf_indent (f, 10, "simplify ? \"Applying pattern\" : " + "\"Matching expression\", file1, " + "dbg_line_numbers[line1_id], file2, line2);"); + + fprintf (f, "\n}\n\n"); +} + static void output_line_directive (FILE *f, location_t location, - bool dumpfile = false, bool fnargs = false) + bool dumpfile = false, bool fnargs = false, + bool indirect_line_numbers = false) { + typedef pair_hash> location_hash; + static hash_map loc_id_map; const line_map_ordinary *map; linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map); expanded_location loc = linemap_expand_location (line_table, map, location); @@ -239,7 +285,23 @@ output_line_directive (FILE *f, location_t location, ++file; if (fnargs) - fprintf (f, "\"%s\", %d", file, loc.line); + { + if (indirect_line_numbers) + { + bool existed; + int &loc_id = loc_id_map.get_or_insert ( + std::make_pair (file, loc.line), &existed); + if (!existed) + { + loc_id = dbg_line_numbers.length (); + dbg_line_numbers.safe_push (loc.line); + } + + fprintf (f, "\"%s\", %d", file, loc_id); + } + else + fprintf (f, "\"%s\", %d", file, loc.line); + } else fprintf (f, "%s:%d", file, loc.line); } @@ -3375,20 +3437,19 @@ dt_operand::gen (FILE *f, int indent, bool gimple, int depth) } } -/* Emit a fprintf to the debug file to the file F, with the INDENT from +/* Emit a logging call to the debug file to the file F, with the INDENT from either the RESULT location or the S's match location if RESULT is null. */ static void -emit_debug_printf (FILE *f, int indent, class simplify *s, operand *result) +emit_logging_call (FILE *f, int indent, class simplify *s, operand *result, + bool gimple) { fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) " - "fprintf (dump_file, \"%s ", - s->kind == simplify::SIMPLIFY - ? "Applying pattern" : "Matching expression"); - fprintf (f, "%%s:%%d, %%s:%%d\\n\", "); + "%s_dump_logs (", gimple ? "gimple" : "generic"); output_line_directive (f, - result ? result->location : s->match->location, true, - true); - fprintf (f, ", __FILE__, __LINE__);\n"); + result ? result->location : s->match->location, + true, true, true); + fprintf (f, ", __FILE__, __LINE__, %s);\n", + s->kind == simplify::SIMPLIFY ? "true" : "false"); } /* Generate code for the '(if ...)', '(with ..)' and actual transform @@ -3524,7 +3585,7 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result) if (!result) { /* If there is no result then this is a predicate implementation. */ - emit_debug_printf (f, indent, s, result); + emit_logging_call (f, indent, s, result, gimple); fprintf_indent (f, indent, "return true;\n"); } else if (gimple) @@ -3615,7 +3676,7 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result) } else gcc_unreachable (); - emit_debug_printf (f, indent, s, result); + emit_logging_call (f, indent, s, result, gimple); fprintf_indent (f, indent, "return true;\n"); } else /* GENERIC */ @@ -3670,7 +3731,7 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result) } if (is_predicate) { - emit_debug_printf (f, indent, s, result); + emit_logging_call (f, indent, s, result, gimple); fprintf_indent (f, indent, "return true;\n"); } else @@ -3738,7 +3799,7 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result) i); } } - emit_debug_printf (f, indent, s, result); + emit_logging_call (f, indent, s, result, gimple); fprintf_indent (f, indent, "return _r;\n"); } } @@ -5447,6 +5508,7 @@ main (int argc, char **argv) parts.quick_push (stdout); write_header (stdout, s_include_file); write_header_includes (gimple, stdout); + write_header_declarations (gimple, stdout); } else { @@ -5460,6 +5522,7 @@ main (int argc, char **argv) fprintf (header_file, "#ifndef GCC_GIMPLE_MATCH_AUTO_H\n" "#define GCC_GIMPLE_MATCH_AUTO_H\n"); write_header_includes (gimple, header_file); + write_header_declarations (gimple, header_file); } /* Go over all predicates defined with patterns and perform @@ -5502,6 +5565,8 @@ main (int argc, char **argv) dt.gen (parts, gimple); + define_dump_logs (gimple, choose_output (parts)); + for (FILE *f : parts) { fprintf (f, "#pragma GCC diagnostic pop\n");