From patchwork Wed Nov 22 11:14:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Kuvyrkov X-Patchwork-Id: 746012 Delivered-To: patch@linaro.org Received: by 2002:a5d:6706:0:b0:32d:baff:b0ca with SMTP id o6csp239354wru; Wed, 22 Nov 2023 03:14:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IHDyHJubopNZmfU/qKEZPbwmGZmQRvssbyVA5WYALt1Bf0VgcvwZW4YfrDizG5Y0kCYAWmQ X-Received: by 2002:ac8:7e83:0:b0:421:c58e:f9b6 with SMTP id w3-20020ac87e83000000b00421c58ef9b6mr2206664qtj.64.1700651698471; Wed, 22 Nov 2023 03:14:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700651698; cv=pass; d=google.com; s=arc-20160816; b=KH25fLoyaGFKcaAH2fbcK02qnvpdWAwE8tPE9ss6l4KdwQ3Jex69SOzfAwOliC2fL1 hEVrfzkCUMB5/tO/wx/uFoB/jqHciX5knPc0u5kVGOVwq3/zuviADrafxkfEZcaUJLhL THEuv06fvVnZWkYLuGuTCNK6f3gaRsO+8h+AsYZ23lYqIM1vFSHY2iPqyRyYdR5FcW6o 0sI2WQzxH76cwUQnEEPdHsa5uh9q70NAlwjZTQNyfPZWasWBPnAKWQ7qP7u0q37NQJmy rhWHSa2SsOxs72593z9luhGtac2wDclIj8faaSbPopd+oKRV8k2mlwLF3uvZAlit2jo4 QiGw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=PJKUM/1g4CycxjvAoy+05D0Yi7GbiQ+fDfY0kXzVx08=; fh=WFm8hI3He6vfed9bNVx/Tt8MS1NEGxq9c34GiEnOLuU=; b=xQrgu5G1XW1uXyT40epB81fNHMI2UhP3Em8c0mFqbgmlDab3rxTlqy+W39IeDu7xLe 8fvWaaHoxUrqqFYxm7BtxxNWgOXYBNpx27KaReHF2to6DlC9yeR6DukLMyl/SYmxaKrX 8+isAaQkeFRd28YR1m7WPrlqE7n191vgXeQAHR/Z65ZGeoUfyP1WK3bdImtiHQEOfNBJ 3aoE+7Prht3ot5UZ6frXhJUQFxdcxRT4PgZIePmzXyp8nFV19ZIe4bSH9IquiblH+kGl qBhE5bx11+KlIxnNQM37vFwN2HZNuRpOq7CBBf5xNZIMYgFgcHgEEis0endu8CSVaW1D 2CWA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FYW3FNdl; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+patch=linaro.org@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+patch=linaro.org@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id c9-20020ac85a89000000b00417b8ceb616si11256400qtc.403.2023.11.22.03.14.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 03:14:58 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+patch=linaro.org@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FYW3FNdl; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+patch=linaro.org@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+patch=linaro.org@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 206CE385828D for ; Wed, 22 Nov 2023 11:14:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id 2AC823858410 for ; Wed, 22 Nov 2023 11:14:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2AC823858410 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2AC823858410 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::132 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700651693; cv=none; b=Wqjuyi4aNE6LHGFfWWfnVZlIwEqckwLAS5YdNOFsGoyNrM7dHS6V1lu6G3THFMRWPc+8Sf8XpiQeSiKeFqz6ZBw1TIP4r1qZ3oLkvZqa+5YdK8wFeKmaf4mkz2TlhKJhXkEeRDHDyOsr0eXDzaCRWheKl/RnnaLeTLXs7+l4rmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700651693; c=relaxed/simple; bh=TSndNLt4wid1s0PKDqQWPpoDktQC3cld8NwGlZo2ELw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=tz8GlF/tUHOCP0pr7FZC72kYdmUinGFHdTpd51lRmD1Tbr4fxoLf0IMW2G5AY0TWUDENJNE2aITDs22RyjtzKZP5J7I4keXI88hgQeKHrxDigkD9k00MndK7dDvplNJE810KlpMshvVoU5X3JooBt6HeaIOdpDSTjuZBCNyYJhc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-50aafe59afaso708349e87.0 for ; Wed, 22 Nov 2023 03:14:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1700651689; x=1701256489; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PJKUM/1g4CycxjvAoy+05D0Yi7GbiQ+fDfY0kXzVx08=; b=FYW3FNdl0vGLiI5JeqxjfBumud5akforfGHBYgCnUMiKTkZXTmZ1ef7Rmd6PWflRHc w0vg2XOzWa80SNOmxM36wxbW22Eu3a22+tVqzY7O+SPkNrpZtym+eVeOqH4R31s/rPRK /5iIMJQxTRHSI9WIoVtIqhGwK86RnEhafM8k1bB6uvnFl0w9TPOFn3MiJPLQJ1SrKC2N HRmTnJTxQsAjWiVlGZlPhUZn8pbcFUOoSSWAszQYzphS8O4TD0SvtJQTzY35WSVLrw29 L9TDjRNpDB+WE2GXrtkp8KHMylAcTy76tKlQPIJ3lUZtz4Pa3DX1EIIsuRYzVPBPeUux uYpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700651689; x=1701256489; 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=PJKUM/1g4CycxjvAoy+05D0Yi7GbiQ+fDfY0kXzVx08=; b=bszX+8i363WSdYML+eorlnNu/tr6i7lSPoYDbJs1+QXtzr/U3VEXRMEl1H6d4OWZxv vGFPhTBFzq3ySL0B2OfSPFlj7kzrCkfYPEeOumpq3s29eFnaMOw7x+X9Syn1W7L3VUbO ARwjkuMB5zzS2KfLwrUwwfFQTYf71yprH7/KYn9GMIXuFO6QGfLsAspXBRViybtZXxaH IuzR/ISyEc+SFkxmoa6FXi+SpjsuJMRtKy8u94PRgN3oCLQqjitD7G/Z1CRCTJbg4VPf Y7kLAlC7HdAD17ugWExq37hsAXWPpQB8ESqSJ+r2Rh/CdTUN245hsS/fQRj/Y7CEzIdP ONVg== X-Gm-Message-State: AOJu0YzRxobOxCbyMGB60X7gsoC2gggPSw7wIPAFIALfAOQkCazi137Q 6qaM+wGeor7/7fkskvKBzqQOrgUgGLZ0BxygviNYKyI= X-Received: by 2002:a05:6512:158b:b0:502:9b86:7112 with SMTP id bp11-20020a056512158b00b005029b867112mr1598011lfb.2.1700651688913; Wed, 22 Nov 2023 03:14:48 -0800 (PST) Received: from localhost.localdomain (static.225.72.216.95.clients.your-server.de. [95.216.72.225]) by smtp.gmail.com with ESMTPSA id t13-20020a195f0d000000b0050919538b00sm1848553lfb.79.2023.11.22.03.14.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 03:14:47 -0800 (PST) From: Maxim Kuvyrkov To: gcc-patches@gcc.gnu.org Cc: Maxim Kuvyrkov , Bernd Schmidt , Vladimir Makarov , Jeff Law , Alexander Monakov , Richard Guenther Subject: [PATCH v3 1/8] sched-deps.cc (find_modifiable_mems): Avoid exponential behavior Date: Wed, 22 Nov 2023 11:14:08 +0000 Message-Id: <20231122111415.815147-2-maxim.kuvyrkov@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231120120649.672893-1-maxim.kuvyrkov@linaro.org> References: <20231120120649.672893-1-maxim.kuvyrkov@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patch=linaro.org@gcc.gnu.org This patch avoids sched-deps.cc:find_inc() creating exponential number of dependencies, which become memory and compilation time hogs. Consider example (simplified from PR96388) ... === sp=sp-4 // sp_insnA mem_insnA1[sp+A1] ... mem_insnAN[sp+AN] sp=sp-4 // sp_insnB mem_insnB1[sp+B1] ... mem_insnBM[sp+BM] === [For simplicity, let's assume find_inc(backwards==true)]. In this example find_modifiable_mems() will arrange for mem_insnA* to be able to pass sp_insnA, and, while doing this, will create dependencies between all mem_insnA*s and sp_insnB -- because sp_insnB is a consumer of sp_insnA. After this sp_insnB will have N new backward dependencies. Then find_modifiable_mems() gets to mem_insnB*s and starts to create N new dependencies for _every_ mem_insnB*. This gets us N*M new dependencies. In PR96833's testcase N and M are 10k-15k, which causes RAM usage of 30GB and compilation time of 30 minutes, with sched2 accounting for 95% of both metrics. After this patch the RAM usage is down to 1GB and compilation time is down to 3-4 minutes, with sched2 no longer standing out on -ftime-report or memory usage. gcc/ChangeLog: PR rtl-optimization/96388 PR rtl-optimization/111554 * sched-deps.cc (find_inc): Avoid exponential behavior. --- gcc/sched-deps.cc | 48 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/gcc/sched-deps.cc b/gcc/sched-deps.cc index c23218890f3..005fc0f567e 100644 --- a/gcc/sched-deps.cc +++ b/gcc/sched-deps.cc @@ -4779,24 +4779,59 @@ parse_add_or_inc (struct mem_inc_info *mii, rtx_insn *insn, bool before_mem) /* Once a suitable mem reference has been found and the corresponding data in MII has been filled in, this function is called to find a suitable add or inc insn involving the register we found in the memory - reference. */ + reference. + If successful, this function will create additional dependencies between + - mii->inc_insn's producers and mii->mem_insn as a consumer (if backwards) + - mii->inc_insn's consumers and mii->mem_insn as a producer (if !backwards). +*/ static bool find_inc (struct mem_inc_info *mii, bool backwards) { sd_iterator_def sd_it; dep_t dep; + sd_list_types_def mem_deps = backwards ? SD_LIST_HARD_BACK : SD_LIST_FORW; + int n_mem_deps = sd_lists_size (mii->mem_insn, mem_deps); - sd_it = sd_iterator_start (mii->mem_insn, - backwards ? SD_LIST_HARD_BACK : SD_LIST_FORW); + sd_it = sd_iterator_start (mii->mem_insn, mem_deps); while (sd_iterator_cond (&sd_it, &dep)) { dep_node_t node = DEP_LINK_NODE (*sd_it.linkp); rtx_insn *pro = DEP_PRO (dep); rtx_insn *con = DEP_CON (dep); - rtx_insn *inc_cand = backwards ? pro : con; + rtx_insn *inc_cand; + int n_inc_deps; + if (DEP_NONREG (dep) || DEP_MULTIPLE (dep)) goto next; + + if (backwards) + { + inc_cand = pro; + n_inc_deps = sd_lists_size (inc_cand, SD_LIST_BACK); + } + else + { + inc_cand = con; + n_inc_deps = sd_lists_size (inc_cand, SD_LIST_FORW); + } + + /* In the FOR_EACH_DEP loop below we will create additional n_inc_deps + for mem_insn. This by itself is not a problem, since each mem_insn + will have only a few inc_insns associated with it. However, if + we consider that a single inc_insn may have a lot of mem_insns, AND, + on top of that, a few other inc_insns associated with it -- + those _other inc_insns_ will get (n_mem_deps * number of MEM insns) + dependencies created for them. This may cause an exponential + growth of memory usage and scheduling time. + See PR96388 for details. + We [heuristically] use n_inc_deps as a proxy for the number of MEM + insns, and drop opportunities for breaking modifiable_mem dependencies + when dependency lists grow beyond reasonable size. */ + if (n_mem_deps * n_inc_deps + >= param_max_pending_list_length * param_max_pending_list_length) + goto next; + if (parse_add_or_inc (mii, inc_cand, backwards)) { struct dep_replacement *desc; @@ -4838,6 +4873,11 @@ find_inc (struct mem_inc_info *mii, bool backwards) desc->insn = mii->mem_insn; move_dep_link (DEP_NODE_BACK (node), INSN_HARD_BACK_DEPS (con), INSN_SPEC_BACK_DEPS (con)); + + /* Make sure that n_inc_deps above is consistent with dependencies + we create. */ + gcc_assert (mii->inc_insn == inc_cand); + if (backwards) { FOR_EACH_DEP (mii->inc_insn, SD_LIST_BACK, sd_it, dep)