From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 87BF838485AF for ; Mon, 6 Jun 2022 09:51:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 87BF838485AF Received: by mail-lf1-x131.google.com with SMTP id i29so5661390lfp.3 for ; Mon, 06 Jun 2022 02:51:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=T6y9OX48e2r1x9srtroFnk3kfE0hgaUUfAekICpCEmU=; b=Kyx81gx7lCXwtodR34whgj2K7FqGe1f0q+Ie/ISAkUAYSCXH4vc45ivB022aE5WxHS 7tBWl3u7Qt60hsAkuqkY2MQ0SxZzsDstRQWYhIBjxKk5cBfiOs513JeBuJfZkikxn9Z8 Fe/H8x+LNswmQ4cIzK8vMEmGnjXmdk3vUTRJaE8FQDvgLWeywiV01OMshr5Edo1Fd84T VOghdedaenKvhYR4qVPRjy90kgl+kwlhanZrwVlpMmvRkiksCKQDJVTFSFZYy7PBd21k hNjEt/8M9GEzZvX92YUsoiFgCGhMrbGRFHb45vdkb0a97C5lAdZxjmCUO8U+6No10Cxl erdA== X-Gm-Message-State: AOAM531MgXMtk0FqFAtAs7EjxCr6O2pK+oEW0t7+zkKxvc5O9UFea3gb +jKhcc1ClNChLkcYn/sx4fmJUFebXpu0+CjYbno= X-Google-Smtp-Source: ABdhPJyrgSBXfmRe5fkyrbXO9so6NtbqxgSJqcAhyHtFqz5vnF88MrdY0zFekhv3ymlcmO72+SCEtBCC5gFSS3XUMsM= X-Received: by 2002:a05:6512:c23:b0:479:3233:e377 with SMTP id z35-20020a0565120c2300b004793233e377mr5464987lfu.684.1654509088787; Mon, 06 Jun 2022 02:51:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Andrew Pinski Date: Mon, 6 Jun 2022 02:51:17 -0700 Message-ID: Subject: Re: [PATCH] c++ bugfix: inconsistent runtime beahavior when have -O2 To: Hongbo Liu Cc: gcc-patches Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no 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-List-Received-Date: Mon, 06 Jun 2022 09:51:32 -0000 On Mon, Jun 6, 2022 at 2:12 AM Hongbo Liu via Gcc-patches wrote: > > Hi, > > This is my first time using email patch, please correct me if there is any error. Thanks! > > > I found a c++ optimization bug first introduced via commit 520d5ad337eaa15860a5a964daf7ca46cf31c029, which will make program compiled with -O2 have unexpected behavior. Except your testcase violates C/C++ aliasing rules. You store to offset_delta via int64_t (which is long on x86_64) but then do a load via long long. If you fix the testcase, it works correctly at -O2 and above. If you don't want strict aliasing to be turned on you can use -fno-strict-aliasing. Your patch is wrong as now none of the modref data will be used which is exposing the bug in the testcase and it is a bug in the source code you are compiling. As for how to add the testcase, https://gcc.gnu.org/wiki/HowToPrepareATestcase does have some information. So does https://gcc.gnu.org/onlinedocs/gccint/Testsuites.html#Testsuites . Thanks, Andrew Pinski > > > How to produce > > > 1. Checkout to latest master commit: c4d702fb3c1e2f6e1bc8711da81bff59543b1b19 > 2. Build and install the gcc; > 3. Compile the attached file `test.cc` with command: `latest-g++ -std=c++20 -g0 -O1 test.cc -o test_o1`; > 4. The output of `./test_o1` will be `offset delta: 5`; > 5.  Compile the attached file `test.cc` with command: `latest-g++ -std=c++20 -g0 -O2 test.cc -o test_o2`; > 6. The output of `./test_o2` will be `offset delta: 0`; > > > The program behavior with `-O2` is inconsistent with `-O1` and unexpected. > > > How to fix > > > The bug was introduced via commit 520d5ad337eaa15860a5a964daf7ca46cf31c029. The attached patch file could fix the problem and make `-O2` and `-O1` have same behavior. > > > Request for suggestion > > > I could not find and document about how to add this kind of test, could anyone give me some suggestions?