[llvm-bugs] [Bug 50190] New: Possible Issue with Whole Program Devirtualization

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Apr 30 16:17:16 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50190

            Bug ID: 50190
           Summary: Possible Issue with Whole Program Devirtualization
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: steplong at quicinc.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

class A {
public:
  virtual int foo() const = 0;
};

class B : public A {
public:
  virtual int foo() const { return 2; }
};

class C : public A {
public:
  virtual int foo() const { return 3; }
};

A *get_A(int num) {
  if (num > 1) {
    printf("returning new B\n");
    return new B; 
  }
  printf("returning new C\n");
  return new C;
}

__attribute__((noinline))
void call_foo(int argc) {
  C *c = (C *)get_A(argc);
  printf("%d\n", c->foo());
}

int main(int argc, char *argv[]) {
  call_foo(argc);
}

trunk clang with -O3 -flto -whole-program-vtables -fvisibility=hidden outputs
./a.out 1
returning new B
3

gcc 10.2 with -O3 -flto -fwhole-program -fvisibility=hidden outputs
./a.out 1
returning new B
2

It looks like the c->foo is being devirtualized to a direct C::foo() call.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210430/8c42a074/attachment.html>


More information about the llvm-bugs mailing list