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

Codegen for typeid is incompatible with MS ABI

Open ZequanWu opened this issue 6 years ago • 3 comments

Bugzilla Link 47467
Version trunk
OS Windows NT
CC @dwblaikie,@DougGregor,@zmodem,@zygoloid

Extended Description

Clang codegen for typeid generates calls to __RTtypeid sometimes when MSVC doesn't generates such call, https://godbolt.org/z/vzdn9Y

ZequanWu avatar Sep 09 '20 00:09 ZequanWu

Yes. I'll give it a try.

ZequanWu avatar Sep 09 '20 16:09 ZequanWu

Pasting the code here so we don't lose it:

namespace std {
struct type_info {};
} // namespace std
class B {
public:
  virtual ~B() = default;
};

class D1 : public B {
public:
  ~D1() = default;
};

const std::type_info& f1(B& b) {
    return typeid(b);
}
const std::type_info& f2(B* b) {
    return typeid(*b);
}
const std::type_info& f3(B b) {
    return typeid(b);
}
const std::type_info& f4(B b) {
    return typeid(*&b);
}
const std::type_info& f5(B b) {
    return typeid((B&)b);
}

Looking at your example, the difference seems to be for f3 and f5.

I don't think it's correct to say that the codegen is incompatible: I assume the code works, but it's a little bit inefficient.

If you look at the same code targeting Linux, the code for f3, f4, and f5 also seems inefficient since the type is known at compile-time. GCC handles this better and just returns the typeinfo directly.

It seems there is room for improvement here, both for Windows and non-Windows targets. I think it would be good to at least get better code for f3(). Want to give that a try?

zmodem avatar Sep 09 '20 08:09 zmodem

@llvm/issue-subscribers-clang-codegen

Author: Zequan Wu (ZequanWu)

| | | | --- | --- | | Bugzilla Link | [47467](https://llvm.org/bz47467) | | Version | trunk | | OS | Windows NT | | CC | @dwblaikie,@DougGregor,@zmodem,@zygoloid |

Extended Description

Clang codegen for typeid generates calls to __RTtypeid sometimes when MSVC doesn't generates such call, https://godbolt.org/z/vzdn9Y

llvmbot avatar Feb 08 '25 11:02 llvmbot