From patchwork Mon Nov 20 14:48:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Kuvyrkov X-Patchwork-Id: 745358 Delivered-To: patch@linaro.org Received: by 2002:a5d:5052:0:b0:32d:baff:b0ca with SMTP id h18csp1327636wrt; Mon, 20 Nov 2023 06:49:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNdg1N86OmxQn45LCGbUc3Akh1K8YshGpJdHAFoNJFCnce1XouEvqzXedTMAX7TFT5Mmtn X-Received: by 2002:a05:6358:5e19:b0:16b:fcd3:a3ed with SMTP id q25-20020a0563585e1900b0016bfcd3a3edmr6949563rwn.24.1700491780291; Mon, 20 Nov 2023 06:49:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700491780; cv=pass; d=google.com; s=arc-20160816; b=0Vpd0bRTGn/pkhd8vja4qaKfNrDaClbiz8It7hu8xgLFqo9dEerhKbdPj9VccSJRWh VHYh6SQtSsErLHsTv+l7VBDR1n0wemWX3z6Gy3TJQ6h8zi1tnbMxB268tpQxflj11F9f bmq0+2Rpuh+4g+sDOBZiM//us88E8vawgzCaxAUwcrsifG+ZsqBcd+jvD7VVthys60ZO Jyt0vyxUe+EL1Bmp6wsZ+/vksoNw8egycUyT9jZrmLRApfj2ivDOYNAl5DGm+3IgxvB7 GHSKvYKQBHJJ4Oq+UYIVOJQOCCbMPpXIxkfVsnCTQKdlLdRunptuLYG8wibyW7CObgEG FJlw== 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=tn3oPXdz0xlBGWVnFcD4qpo0MdDEcLxbwLA+pSXz9sE=; fh=Y11P5AUnxuDO5edr5Vb8FQKqVASn8kjRlUMiUi8j4sI=; b=WRy1j65mtkdLNPoqclnSY5tghWXk4GTCcaNJCMwNgwBgwX4VyLKFViYLh1OTFPfIJV ypbGdTGrStnFFDCjaEZSp5lexTX3i5aSiDkIUqbOuMlWpZhFYA2YQ0gp/4ACKg7dZ7vs JcKXJKIx0b0CKjRzTvWQLgufk7D7ANYmtFWGwcuJltsNsTenrmk43J6tTfg1qBd6kCbe XAQ2/ePsZ64qJ9cCV77bIBXkBDr9k5NSKs74tVnUvYGW9PaCHTJ34zFrOOTZhW8ipBVe 4aaJg+jJIqXKPGVT/Gb01m3jcaafh8FqSydyBvgXx20LtT9RA+x4ssuLWlB9NciP6cGb /mOA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YvdipVKj; 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 w16-20020a05622a191000b004181244b348si7165205qtc.432.2023.11.20.06.49.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 06:49:40 -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=YvdipVKj; 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 EFD4038432CC for ; Mon, 20 Nov 2023 14:49:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id F0AF4385C6F7 for ; Mon, 20 Nov 2023 14:49:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F0AF4385C6F7 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 F0AF4385C6F7 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700491774; cv=none; b=JBD6upRXMIaC7KivTs8+jMosHGeFx0XXIcM5ZAfQa6vLYKrkv1NWoSO7OFaIMsqFyPsOj4ij6Fvyt2DGGCI1R51heKU8ErLE1phf5EvY25R8W9GbvtytkClUhy4xaYqLX4AAVPR3pOGsGFK88G4Qp6LYtJ3QKMG5i+LFV6SqSws= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700491774; c=relaxed/simple; bh=EKG999h0+v3mdhwWQb/Kr0XQVl/uSvzSGZqaIOwy4Bo=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=qfw3DJtf7m2tLRmK9fndaOZt+6npkAm1ZyavVu/XM3YScxrqpBVJfehNPTBGFA5oNqy5Fp+v9xoRnkq/kdDb8F53LSihN+YIOMpM3N2yP/Q5yunKpJ3iFDpXkj72DxJDh+eBRV+eH8udR5O7aoZrWdpEIO4TLwJncRAO8PnUqG0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-507f63fcbf6so987651e87.1 for ; Mon, 20 Nov 2023 06:49:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1700491769; x=1701096569; 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=tn3oPXdz0xlBGWVnFcD4qpo0MdDEcLxbwLA+pSXz9sE=; b=YvdipVKjd6OaVeHjGjanb44p7VSjhmuFuZuMTn/uyOkC68wr41gc8oUq5iOaFaewGE K/yDaOKzKk/TKpngMqfWxMTjzPQIk2mxDi/3i56f0vLyIfEO/q7BNRKNw37HrFFofDRI Mgp97/cWs1QZXcPAGOwfDigv/IsU5oft0hvTk+2pdo0YS9yPM75JHvujU0xYs2hut9A5 11BPKv38MdA8zS81NmEOTnhYTGXUOg6LGFAsB9flKkg/iISRmLZ45UwFddTq+dxCE3Kn Tsd2N4TgL/cu7QxYkAdbDFHZNwomJARzatjEB//4PtUqPYBKTNoLa3octPUF11uNTL1D JfGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700491769; x=1701096569; 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=tn3oPXdz0xlBGWVnFcD4qpo0MdDEcLxbwLA+pSXz9sE=; b=FIvCeBBxRGl+AzHtZwHUzq0QtO+H82kx1oGf2qDb1jOhWLTNTYfVBr/mp5LrXQ97al lJQlv19ISJNu9RCuZW0o4P+URw0dj+eSWhKw0vILGcOn8dGK3Co36dKVV/oCb0xmGkHP 25XvrMyzFnWtdpZU3EF3uWWPJwA0UTulnhx686/EFyNutMEhqSDvCj+5Pk7SUNuZjJHs 0L3pKx15Sl9VpnNN9s5YIv7XdMo+/YKv/MOrEPTUM3K+YeiCa9h7V5c1droeIjj5RtFe 9LhQBY5HBrEWwXCS8EU+TYqcgKN39t30zAIY32BnrBt/ofWTT+cuBnL64JmykwkEsrC9 NITA== X-Gm-Message-State: AOJu0YzHU5vz0aj7IqB/fj/QdxvcFR+4yOZU2Rfe5Rw6XGHJrWtj4QXH J5nNqgJGtE6/odj73B08t9kMUi4LRIzQjpFyoQsIT0U= X-Received: by 2002:a19:ee17:0:b0:507:9861:2be9 with SMTP id g23-20020a19ee17000000b0050798612be9mr4175667lfb.6.1700491769461; Mon, 20 Nov 2023 06:49:29 -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 e22-20020ac24e16000000b0050aa5f2c496sm877998lfr.137.2023.11.20.06.49.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 06:49:28 -0800 (PST) From: Maxim Kuvyrkov To: gcc-patches@gcc.gnu.org, Richard Guenther Cc: Maxim Kuvyrkov , Bernd Schmidt , Vladimir Makarov , Jeff Law Subject: [PATCH v2] sched-deps.cc (find_modifiable_mems): Avoid exponential behavior Date: Mon, 20 Nov 2023 14:48:54 +0000 Message-Id: <20231120144854.676590-1-maxim.kuvyrkov@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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, 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] === ... 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 | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/gcc/sched-deps.cc b/gcc/sched-deps.cc index c23218890f3..a286ff319e2 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,8 @@ 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)); + + gcc_assert (mii->inc_insn == inc_cand); if (backwards) { FOR_EACH_DEP (mii->inc_insn, SD_LIST_BACK, sd_it, dep)