llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

virtual function calls for functions marked `[[gnu::const]]` get optimized away

Open philnik777 opened this issue 2 years ago • 1 comments

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.

philnik777 avatar Aug 07 '22 22:08 philnik777

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.)

msebor avatar Aug 16 '22 17:08 msebor

This is a duplicate of #13130.

philnik777 avatar Aug 15 '23 06:08 philnik777

@llvm/issue-subscribers-clang-codegen

llvmbot avatar Aug 15 '23 06:08 llvmbot

@philnik777: Please don't forget to add duplicate labels to such issues.

EugeneZelenko avatar Aug 15 '23 14:08 EugeneZelenko