[Clang][CodeGen] typeid needs special care when type_info is not in the default AS
After https://reviews.llvm.org/D153092, for targets that use a non-default AS for globals, an "interesting" situation arises around typeid and its paired type, type_info: - on the AST level, the type_info interface is defined with default / generic addresses, be it for function arguments, or for this; - in IR, type_info values are globals, and thus pointers to type_info values are pointers to global This leads to a mismatch between the function signature / formal type of the argument, and its actual type. Currently we try to handle such mismatches via `bitcast`, but that is wrong in this case, since an `ascast` is required. This patch ensures that iff the pointer to `type_info` points to a non-default AS, an ascast is inserted so as to match the `typeid` interface / return value type. Reviewed by: yaxunl Differential Revision: https://reviews.llvm.org/D157452
This commit is contained in:
parent
dd3aa26fc8
commit
9c760ca8ec
@ -2195,11 +2195,19 @@ static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E,
|
||||
|
||||
llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
|
||||
llvm::Type *PtrTy = llvm::PointerType::getUnqual(getLLVMContext());
|
||||
LangAS GlobAS = CGM.GetGlobalVarAddressSpace(nullptr);
|
||||
|
||||
auto MaybeASCast = [=](auto &&TypeInfo) {
|
||||
if (GlobAS == LangAS::Default)
|
||||
return TypeInfo;
|
||||
return getTargetHooks().performAddrSpaceCast(CGM,TypeInfo, GlobAS,
|
||||
LangAS::Default, PtrTy);
|
||||
};
|
||||
|
||||
if (E->isTypeOperand()) {
|
||||
llvm::Constant *TypeInfo =
|
||||
CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext()));
|
||||
return TypeInfo;
|
||||
return MaybeASCast(TypeInfo);
|
||||
}
|
||||
|
||||
// C++ [expr.typeid]p2:
|
||||
@ -2212,7 +2220,7 @@ llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
|
||||
return EmitTypeidFromVTable(*this, E->getExprOperand(), PtrTy);
|
||||
|
||||
QualType OperandTy = E->getExprOperand()->getType();
|
||||
return CGM.GetAddrOfRTTIDescriptor(OperandTy);
|
||||
return MaybeASCast(CGM.GetAddrOfRTTIDescriptor(OperandTy));
|
||||
}
|
||||
|
||||
static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF,
|
||||
|
||||
@ -1444,8 +1444,8 @@ llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF,
|
||||
llvm::Type *StdTypeInfoPtrTy) {
|
||||
auto *ClassDecl =
|
||||
cast<CXXRecordDecl>(SrcRecordTy->castAs<RecordType>()->getDecl());
|
||||
llvm::Value *Value = CGF.GetVTablePtr(
|
||||
ThisPtr, llvm::PointerType::getUnqual(CGF.getLLVMContext()), ClassDecl);
|
||||
llvm::Value *Value = CGF.GetVTablePtr(ThisPtr, CGM.GlobalsInt8PtrTy,
|
||||
ClassDecl);
|
||||
|
||||
if (CGM.getItaniumVTableContext().isRelativeLayout()) {
|
||||
// Load the type info.
|
||||
|
||||
32
clang/test/CodeGenCXX/typeid-cxx11-with-address-space.cpp
Normal file
32
clang/test/CodeGenCXX/typeid-cxx11-with-address-space.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
// RUN: %clang_cc1 -I%S %s -triple amdgcn-amd-amdhsa -emit-llvm -std=c++11 -o - | FileCheck %s
|
||||
#include <typeinfo>
|
||||
|
||||
namespace Test1 {
|
||||
|
||||
struct Item {
|
||||
const std::type_info &ti;
|
||||
const char *name;
|
||||
void *(*make)();
|
||||
};
|
||||
|
||||
template<typename T> void *make_impl() { return new T; }
|
||||
template<typename T> constexpr Item item(const char *name) {
|
||||
return { typeid(T), name, make_impl<T> };
|
||||
}
|
||||
|
||||
struct A { virtual ~A(); };
|
||||
struct B : virtual A {};
|
||||
struct C { int n; };
|
||||
|
||||
// CHECK: @_ZN5Test15itemsE ={{.*}} constant [4 x {{.*}}] [{{.*}} ptr addrspacecast (ptr addrspace(1) @_ZTIN5Test11AE to ptr), {{.*}} @_ZN5Test19make_implINS_1AEEEPvv {{.*}} ptr addrspacecast (ptr addrspace(1) @_ZTIN5Test11BE to ptr), {{.*}} @_ZN5Test19make_implINS_1BEEEPvv {{.*}} ptr addrspacecast (ptr addrspace(1) @_ZTIN5Test11CE to ptr), {{.*}} @_ZN5Test19make_implINS_1CEEEPvv {{.*}} ptr addrspacecast (ptr addrspace(1) @_ZTIi to ptr), {{.*}} @_ZN5Test19make_implIiEEPvv }]
|
||||
extern constexpr Item items[] = {
|
||||
item<A>("A"), item<B>("B"), item<C>("C"), item<int>("int")
|
||||
};
|
||||
|
||||
// CHECK: @_ZN5Test11xE ={{.*}} constant ptr addrspacecast (ptr addrspace(1) @_ZTIN5Test11AE to ptr), align 8
|
||||
constexpr auto &x = items[0].ti;
|
||||
|
||||
// CHECK: @_ZN5Test11yE ={{.*}} constant ptr addrspacecast (ptr addrspace(1) @_ZTIN5Test11BE to ptr), align 8
|
||||
constexpr auto &y = typeid(B{});
|
||||
|
||||
}
|
||||
50
clang/test/CodeGenCXX/typeid-with-address-space.cpp
Normal file
50
clang/test/CodeGenCXX/typeid-with-address-space.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
// RUN: %clang_cc1 -I%S %s -triple amdgcn-amd-amdhsa -emit-llvm -fcxx-exceptions -fexceptions -o - | FileCheck %s
|
||||
#include <typeinfo>
|
||||
|
||||
namespace Test1 {
|
||||
|
||||
// PR7400
|
||||
struct A { virtual void f(); };
|
||||
|
||||
// CHECK: @_ZN5Test16int_tiE ={{.*}} constant ptr addrspacecast (ptr addrspace(1) @_ZTIi to ptr), align 8
|
||||
const std::type_info &int_ti = typeid(int);
|
||||
|
||||
// CHECK: @_ZN5Test14A_tiE ={{.*}} constant ptr addrspacecast (ptr addrspace(1) @_ZTIN5Test11AE to ptr), align 8
|
||||
const std::type_info &A_ti = typeid(const volatile A &);
|
||||
|
||||
volatile char c;
|
||||
|
||||
// CHECK: @_ZN5Test14c_tiE ={{.*}} constant ptr addrspacecast (ptr addrspace(1) @_ZTIc to ptr), align 8
|
||||
const std::type_info &c_ti = typeid(c);
|
||||
|
||||
extern const double &d;
|
||||
|
||||
// CHECK: @_ZN5Test14d_tiE ={{.*}} constant ptr addrspacecast (ptr addrspace(1) @_ZTId to ptr), align 8
|
||||
const std::type_info &d_ti = typeid(d);
|
||||
|
||||
extern A &a;
|
||||
|
||||
// CHECK: @_ZN5Test14a_tiE ={{.*}} global
|
||||
const std::type_info &a_ti = typeid(a);
|
||||
|
||||
// CHECK: @_ZN5Test18A10_c_tiE ={{.*}} constant ptr addrspacecast (ptr addrspace(1) @_ZTIA10_c to ptr), align 8
|
||||
const std::type_info &A10_c_ti = typeid(char const[10]);
|
||||
|
||||
// CHECK-LABEL: define{{.*}} ptr @_ZN5Test11fEv
|
||||
// CHECK-SAME: personality ptr @__gxx_personality_v0
|
||||
const char *f() {
|
||||
try {
|
||||
// CHECK: br i1
|
||||
// CHECK: invoke void @__cxa_bad_typeid() [[NR:#[0-9]+]]
|
||||
return typeid(*static_cast<A *>(0)).name();
|
||||
} catch (...) {
|
||||
// CHECK: landingpad { ptr, i32 }
|
||||
// CHECK-NEXT: catch ptr null
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// CHECK: attributes [[NR]] = { noreturn }
|
||||
@ -10,6 +10,14 @@ namespace std {
|
||||
bool operator!=(const type_info& __arg) const {
|
||||
return !operator==(__arg);
|
||||
}
|
||||
|
||||
bool before(const type_info& __arg) const {
|
||||
return __name < __arg.__name;
|
||||
}
|
||||
|
||||
unsigned long hash_code() const {
|
||||
return reinterpret_cast<unsigned long long>(__name);
|
||||
}
|
||||
protected:
|
||||
const char *__name;
|
||||
};
|
||||
|
||||
48
clang/test/CodeGenCXX/typeinfo-with-address-space.cpp
Normal file
48
clang/test/CodeGenCXX/typeinfo-with-address-space.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
// RUN: %clang_cc1 -I%S %s -triple amdgcn-amd-amdhsa -emit-llvm -o - | FileCheck %s -check-prefix=AS
|
||||
// RUN: %clang_cc1 -I%S %s -triple x86_64-linux-gnu -emit-llvm -o - | FileCheck %s -check-prefix=NO-AS
|
||||
#include <typeinfo>
|
||||
|
||||
class A {
|
||||
virtual void f() = 0;
|
||||
};
|
||||
|
||||
class B : A {
|
||||
void f() override;
|
||||
};
|
||||
|
||||
// AS: @_ZTISt9type_info = external addrspace(1) constant ptr addrspace(1)
|
||||
// NO-AS: @_ZTISt9type_info = external constant ptr
|
||||
// AS: @_ZTIi = external addrspace(1) constant ptr addrspace(1)
|
||||
// NO-AS: @_ZTIi = external constant ptr
|
||||
// AS: @_ZTVN10__cxxabiv117__class_type_infoE = external addrspace(1) global ptr addrspace(1)
|
||||
// NO-AS: @_ZTVN10__cxxabiv117__class_type_infoE = external global ptr
|
||||
// AS: @_ZTS1A = linkonce_odr addrspace(1) constant [3 x i8] c"1A\00", comdat, align 1
|
||||
// NO-AS: @_ZTS1A = linkonce_odr constant [3 x i8] c"1A\00", comdat, align 1
|
||||
// AS: @_ZTI1A = linkonce_odr addrspace(1) constant { ptr addrspace(1), ptr addrspace(1) } { ptr addrspace(1) getelementptr inbounds (ptr addrspace(1), ptr addrspace(1) @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr addrspace(1) @_ZTS1A }, comdat, align 8
|
||||
// NO-AS: @_ZTI1A = linkonce_odr constant { ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS1A }, comdat, align 8
|
||||
// AS: @_ZTIf = external addrspace(1) constant ptr addrspace(1)
|
||||
// NO-AS: @_ZTIf = external constant ptr
|
||||
|
||||
unsigned long Fn(B& b) {
|
||||
// AS: %call = call noundef zeroext i1 @_ZNKSt9type_infoeqERKS_(ptr {{.*}} addrspacecast (ptr addrspace(1) @_ZTISt9type_info to ptr), ptr {{.*}} %2)
|
||||
// NO-AS: %call = call noundef zeroext i1 @_ZNKSt9type_infoeqERKS_(ptr {{.*}} @_ZTISt9type_info, ptr {{.*}} %2)
|
||||
if (typeid(std::type_info) == typeid(b))
|
||||
return 42;
|
||||
// AS: %call2 = call noundef zeroext i1 @_ZNKSt9type_infoneERKS_(ptr {{.*}} addrspacecast (ptr addrspace(1) @_ZTIi to ptr), ptr {{.*}} %5)
|
||||
// NO-AS: %call2 = call noundef zeroext i1 @_ZNKSt9type_infoneERKS_(ptr {{.*}} @_ZTIi, ptr {{.*}} %5)
|
||||
if (typeid(int) != typeid(b))
|
||||
return 1712;
|
||||
// AS: %call5 = call noundef ptr @_ZNKSt9type_info4nameEv(ptr {{.*}} addrspacecast (ptr addrspace(1) @_ZTI1A to ptr))
|
||||
// NO-AS: %call5 = call noundef ptr @_ZNKSt9type_info4nameEv(ptr {{.*}} @_ZTI1A)
|
||||
// AS: %call7 = call noundef ptr @_ZNKSt9type_info4nameEv(ptr {{.*}} %8)
|
||||
// NO-AS: %call7 = call noundef ptr @_ZNKSt9type_info4nameEv(ptr {{.*}} %8)
|
||||
if (typeid(A).name() == typeid(b).name())
|
||||
return 0;
|
||||
// AS: %call11 = call noundef zeroext i1 @_ZNKSt9type_info6beforeERKS_(ptr {{.*}} %11, ptr {{.*}} addrspacecast (ptr addrspace(1) @_ZTIf to ptr))
|
||||
// NO-AS: %call11 = call noundef zeroext i1 @_ZNKSt9type_info6beforeERKS_(ptr {{.*}} %11, ptr {{.*}} @_ZTIf)
|
||||
if (typeid(b).before(typeid(float)))
|
||||
return 1;
|
||||
// AS: %call15 = call noundef i64 @_ZNKSt9type_info9hash_codeEv(ptr {{.*}} %14)
|
||||
// NO-AS: %call15 = call noundef i64 @_ZNKSt9type_info9hash_codeEv(ptr {{.*}} %14)
|
||||
return typeid(b).hash_code();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user