diff options
author | Vladimir Palevich <palevichva@gmail.com> | 2023-08-09 01:34:05 +0300 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2023-09-01 16:01:23 +0100 |
commit | 419c423d3aeca754e47e1ce1bf707735603a90a3 (patch) | |
tree | 85566082042c25882b51e6b26feacac150da81e4 /gcc | |
parent | 283994cba687e0746f9a1018bae2c41291e362ff (diff) | |
download | gcc-419c423d3aeca754e47e1ce1bf707735603a90a3.zip gcc-419c423d3aeca754e47e1ce1bf707735603a90a3.tar.gz gcc-419c423d3aeca754e47e1ce1bf707735603a90a3.tar.bz2 |
libstdc++: fix memory clobbering in std::vector [PR110879]
Fix ordering to prevent clobbering of class members by a call to deallocate
in _M_realloc_insert and _M_default_append.
Because of recent changes in _M_realloc_insert and _M_default_append,
calls to deallocate were ordered after assignment to class members of
std::vector (in the guard destructor), which is causing said members to
be call-clobbered. This is preventing further optimization, the
compiler is unable to move memory read out of a hot loop in this case.
This patch reorders the call to before assignments by putting guard in
its own block. Plus a new testsuite for this case. I'm not very happy
with the new testsuite, but I don't know how to properly test this.
PR libstdc++/110879
libstdc++-v3/ChangeLog:
* include/bits/vector.tcc (_M_realloc_insert): End guard
lifetime just before assignment to class members.
(_M_default_append): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/pr110879.C: New test.
Signed-off-by: Vladimir Palevich <palevichva@gmail.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/testsuite/g++.dg/pr110879.C | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/gcc/testsuite/g++.dg/pr110879.C b/gcc/testsuite/g++.dg/pr110879.C new file mode 100644 index 0000000..7f0a0a8 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr110879.C @@ -0,0 +1,16 @@ +// { dg-do compile } +// { dg-options "-O3 -fdump-tree-optimized" } + +#include <vector> + +std::vector<int> f(std::size_t n) { + std::vector<int> res; + for (std::size_t i = 0; i < n; ++i) { + res.push_back(i); + } + return res; +} + +// Reads of _M_finish should be optimized out. +// This regex matches all reads from res variable except for _M_end_of_storage field. +// { dg-final { scan-tree-dump-not "=\\s*\\S*res_(?!\\S*_M_end_of_storage;)" "optimized" } } |