From: "Hongbo Liu" <lhbf@qq.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] c++ bugfix: inconsistent runtime beahavior when have -O2
Date: Mon, 6 Jun 2022 17:11:48 +0800 [thread overview]
Message-ID: <tencent_18F6B45554A14F3A8BF722D8C04B1FD70407@qq.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1152 bytes --]
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.
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?
[-- Attachment #2: test.cc --]
[-- Type: application/octet-stream, Size: 806 bytes --]
#include <cstdint>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void parse_varint_u64(void **buf, uint64_t *val) {
size_t of = 0;
uint64_t num = 0;
do {
num |= (uint64_t)(((char *)(*buf))[(int)of] & 0x7f);
} while (((char *)(*buf))[(int)of++] & 0x80);
*val = num;
}
void parse_varint_i64(void **buf, int64_t *val) {
uint64_t n = 123;
parse_varint_u64(buf, &n);
// printf("n: %d\n", n);
*val = (int64_t)(n >> 1) ^ -(int64_t)(n & 1);
}
int main(int argc, char *argv[]) {
size_t size = 16;
void* data = malloc(size);
for (int i = 0; i < size; ++i) {
((char*)data)[i] = i + 10;
}
long long offset_delta = 0;
parse_varint_i64(&data, (int64_t *)&offset_delta);
std::cout << "offset delta: " << offset_delta << std::endl;
return 0;
}
[-- Attachment #3: 0001-PATCH-c-bugfix-inconsistent-logic-when-have-O2.patch --]
[-- Type: application/octet-stream, Size: 698 bytes --]
From 2cfe523be931f26aa9988fa4d0b8ca57be548517 Mon Sep 17 00:00:00 2001
From: HongboLiu <lhbf@qq.com>
Date: Mon, 6 Jun 2022 16:49:32 +0800
Subject: [PATCH] [PATCH] c++ bugfix: inconsistent logic when have -O2
---
gcc/gimple.cc | 3 +++
1 file changed, 3 insertions(+)
diff --git a/gcc/gimple.cc b/gcc/gimple.cc
index b70ab4d..d9593aa 100644
--- a/gcc/gimple.cc
+++ b/gcc/gimple.cc
@@ -1566,6 +1566,9 @@ gimple_call_arg_flags (const gcall *stmt, unsigned arg)
attr_fnspec fnspec = gimple_call_fnspec (stmt);
int flags = 0;
+ if (!fnspec.known_p ())
+ return 0;
+
if (fnspec.known_p ())
flags = fnspec.arg_eaf_flags (arg);
tree callee = gimple_call_fndecl (stmt);
--
2.7.4
next reply other threads:[~2022-06-06 9:11 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-06 9:11 Hongbo Liu [this message]
2022-06-06 9:51 ` Andrew Pinski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_18F6B45554A14F3A8BF722D8C04B1FD70407@qq.com \
--to=lhbf@qq.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).