llvm-project
llvm-project copied to clipboard
virtual function calls for functions marked `[[gnu::const]]` get optimized away
GCC calls the virtual destructor for
struct S {
[[gnu::const]] virtual ~S();
};
void destroy(S* s) {
s->~S();
}
(Godbolt) while clang optimizes it away. Since [[gnu::const]]
is a GCC extension clang should emulate the behaviour.
For function declarations GCC applies the const
and pure
attribute to the declarations themselves (rather than to their types) but when deciding whether to retain or remove a call to a function made through a pointer (such as through the virtual table) it only considers the type of the called function and so it doesn't see either of the attributes. So the fact that it retains a call is incidental rather than intentional, and is true not just for virtual calls but for any call made through a pointer. (It makes sense to keep such calls in general since the call is not necessarily to the declared function.)
This is a duplicate of #13130.
@llvm/issue-subscribers-clang-codegen
@philnik777: Please don't forget to add duplicate
labels to such issues.