From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id EC08B385C423 for ; Thu, 2 Dec 2021 17:21:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EC08B385C423 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-550-MWrXCdw9OR6e6Vo53Cmb4A-1; Thu, 02 Dec 2021 12:21:10 -0500 X-MC-Unique: MWrXCdw9OR6e6Vo53Cmb4A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 73D2E19253CE; Thu, 2 Dec 2021 17:21:09 +0000 (UTC) Received: from [10.22.9.136] (unknown [10.22.9.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id DF4B960C13; Thu, 2 Dec 2021 17:21:08 +0000 (UTC) Message-ID: <6995f136-f658-2f81-47e7-c869cd1b554b@redhat.com> Date: Thu, 2 Dec 2021 12:21:08 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [PR103437] [committed] IRA: Process multiplication overflow in priority calculation for allocno assignments From: Vladimir Makarov To: Jakub Jelinek Cc: Christophe Lyon , "gcc-patches@gcc.gnu.org" References: <116e765c-ea89-47f4-600f-af115dd561c3@redhat.com> <20211202140021.GH2646553@tucnak> <3bb6317f-b049-13f1-1bac-6ffd753e9685@redhat.com> <20211202142937.GI2646553@tucnak> <68638247-ddb8-b165-d24a-89a0f8155e63@redhat.com> <20211202161348.GJ2646553@tucnak> In-Reply-To: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, BODY_8BITS, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Thu, 02 Dec 2021 17:21:13 -0000 On 2021-12-02 12:06, Vladimir Makarov wrote: > > On 2021-12-02 11:13, Jakub Jelinek wrote: >> On Thu, Dec 02, 2021 at 11:03:46AM -0500, Vladimir Makarov wrote: >>> --- a/gcc/ira-color.c >>> +++ b/gcc/ira-color.c >>> @@ -2797,6 +2797,7 @@ static void >>>   setup_allocno_priorities (ira_allocno_t *consideration_allocnos, >>> int n) >>>   { >>>     int i, length, nrefs, priority, max_priority, mult, diff; >>> +  bool overflow_backup_p = true; >>>     ira_allocno_t a; >>>       max_priority = 0; >>> @@ -2811,9 +2812,25 @@ setup_allocno_priorities (ira_allocno_t >>> *consideration_allocnos, int n) >>>         diff = ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a); >>>         /* Multiplication can overflow for very large functions. >>>        Check the overflow and constrain the result if necessary: */ >>> +#ifdef __has_builtin >>> +#if __has_builtin(__builtin_smul_overflow) >>> +      overflow_backup_p = false; >>>         if (__builtin_smul_overflow (mult, diff, &priority) >>>         || priority <= -INT_MAX) >>>       priority = diff >= 0 ? INT_MAX : -INT_MAX; >>> +#endif >>> +#endif >>> +      if (overflow_backup_p) >>> +    { >>> +      static_assert >>> +        (sizeof (long long) >= 2 * sizeof (int), >>> +         "overflow code does not work for such int and long long >>> sizes"); >>> +      long long priorityll = (long long) mult * diff; >>> +      if (priorityll < -INT_MAX || priorityll > INT_MAX) >>> +        priority = diff >= 0 ? INT_MAX : -INT_MAX; >>> +      else >>> +        priority = priorityll; >>> +    } > So simple problem and so many details :) >> This will require that long long is at least twice as large as int >> everywhere, I thought you wanted to do that only when >> __builtin_smul_overflow isn't available. > > That is not critical as GCC and probably all others C++ compiler > support only targets with this assertion.  I guess it is better to > find this problem earlier on targets (if any) where it is not true > *independently* on used compiler. > > So it is difficult for me to know what is better.  Probably for > GCC/Clang oriented world, your variant is better as it permits to > compile the code by GCC even on targets where the assertion is false. > > After some more considerations, I think you are right and the backup code should be conditional.  Because otherwise, there is no sense to use code with __builtin_smul_overflow.  I'll do the changes.