Skip to content

Commit 6b68a48

Browse files
authored
[CIR][CIRGen][TBAA] Add support for vtable pointer (#1463)
1 parent cb98b99 commit 6b68a48

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

clang/include/clang/CIR/Dialect/IR/CIRTBAAAttrs.td

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ def CIR_TBAAOmnipotentChar
1515
let summary = "Describes a special scalar type, the omnipotent char type.";
1616
}
1717

18+
//===----------------------------------------------------------------------===//
19+
// TBAAVTablePointerAttr
20+
//===----------------------------------------------------------------------===//
21+
22+
def CIR_TBAAVTablePointerAttr
23+
: CIR_Attr<"TBAAVTablePointer", "tbaa_vptr", [], "TBAAAttr"> {
24+
let summary = "Describes a special scalar type, the vtable pointer type.";
25+
let parameters = (ins CIR_AnyType:$type);
26+
let assemblyFormat = "`<` struct(params) `>`";
27+
}
28+
1829
//===----------------------------------------------------------------------===//
1930
// TBAAScalarAttr
2031
//===----------------------------------------------------------------------===//
@@ -140,7 +151,8 @@ def CIR_TBAAStructAttr : CIR_Attr<"TBAAStruct",
140151

141152
def CIR_AnyTBAAAttr : AnyAttrOf<[
142153
CIR_TBAAAttr,
143-
CIR_TBAAOmnipotentChar,
154+
CIR_TBAAOmnipotentChar,
155+
CIR_TBAAVTablePointerAttr,
144156
CIR_TBAAScalarAttr,
145157
CIR_TBAAStructAttr,
146158
CIR_TBAATagAttr

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ struct MissingFeatures {
6060
static bool tbaa() { return false; }
6161
static bool tbaaStruct() { return false; }
6262
static bool tbaaTagForStruct() { return false; }
63-
static bool tbaaVTablePtr() { return false; }
6463
static bool tbaaIncompleteType() { return false; }
6564
static bool tbaaMergeTBAAInfo() { return false; }
6665
static bool tbaaMayAlias() { return false; }

clang/lib/CIR/CodeGen/CIRGenTBAA.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,10 @@ TBAAAccessInfo CIRGenTBAA::getAccessInfo(clang::QualType accessType) {
324324
}
325325

326326
TBAAAccessInfo CIRGenTBAA::getVTablePtrAccessInfo(mlir::Type vtablePtrType) {
327-
assert(!cir::MissingFeatures::tbaaVTablePtr());
328-
return TBAAAccessInfo();
327+
const mlir::DataLayout dataLayout(moduleOp);
328+
auto size = dataLayout.getTypeSize(vtablePtrType);
329+
return TBAAAccessInfo(
330+
cir::TBAAVTablePointerAttr::get(mlirContext, vtablePtrType), size);
329331
}
330332

331333
mlir::ArrayAttr CIRGenTBAA::getTBAAStructInfo(clang::QualType qty) {

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ struct CIROpAsmDialectInterface : public OpAsmDialectInterface {
108108
return AliasResult::FinalAlias;
109109
}
110110
return TypeSwitch<Attribute, AliasResult>(attr)
111-
.Case<cir::TBAAAttr, cir::TBAAOmnipotentCharAttr, cir::TBAAScalarAttr,
111+
.Case<cir::TBAAAttr, cir::TBAAOmnipotentCharAttr,
112+
cir::TBAAVTablePointerAttr, cir::TBAAScalarAttr,
112113
cir::TBAAStructAttr, cir::TBAATagAttr>([&](auto attr) {
113114
os << decltype(attr)::getMnemonic();
114115
return AliasResult::OverridableAlias;

clang/lib/CIR/Lowering/DirectToLLVM/LowerTBAAToLLVM.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "mlir/IR/BuiltinAttributes.h"
55
#include "mlir/IR/MLIRContext.h"
66
#include "mlir/Interfaces/DataLayoutInterfaces.h"
7+
#include "mlir/Support/LLVM.h"
78
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
89
#include "llvm/ADT/SmallVector.h"
910
#include "llvm/Support/ErrorHandling.h"
@@ -22,6 +23,11 @@ class CIRToLLVMTBAAAttrLowering {
2223
if (auto charAttr = mlir::dyn_cast<cir::TBAAOmnipotentCharAttr>(tbaa)) {
2324
return getChar();
2425
}
26+
if (auto vptrAttr = mlir::dyn_cast<cir::TBAAVTablePointerAttr>(tbaa)) {
27+
mlir::DataLayout layout;
28+
return createScalarTypeNode("vtable pointer", getRoot(),
29+
layout.getTypeSize(vptrAttr.getType()));
30+
}
2531
if (auto scalarAttr = mlir::dyn_cast<cir::TBAAScalarAttr>(tbaa)) {
2632
mlir::DataLayout layout;
2733
auto size = layout.getTypeSize(scalarAttr.getType());

clang/test/CIR/CodeGen/tbaa-vptr.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir -O1
22
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll -O1
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll -O1 -relaxed-aliasing
6+
// RUN: FileCheck --check-prefix=NO-TBAA --input-file=%t.ll %s
7+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll -O0
8+
// RUN: FileCheck --check-prefix=NO-TBAA --input-file=%t.ll %s
39

4-
// CIR-NOT: #tbaa
10+
// NO-TBAA-NOT: !tbaa
11+
12+
// CIR: #tbaa[[VPTR:.*]] = #cir.tbaa_vptr<type = !cir.ptr<!cir.ptr<!cir.func<() -> !u32i>>>>
513

614
struct Member {
715
~Member();
@@ -16,3 +24,11 @@ struct B : A {
1624
virtual ~B();
1725
};
1826
B::~B() { }
27+
28+
// CIR-LABEL: _ZN1BD2Ev
29+
// CIR: cir.store %{{.*}}, %{{.*}} : !cir.ptr<!cir.ptr<!cir.func<() -> !u32i>>>, !cir.ptr<!cir.ptr<!cir.ptr<!cir.func<() -> !u32i>>>> tbaa(#tbaa[[VPTR]])
30+
31+
// LLVM-LABEL: _ZN1BD2Ev
32+
// LLVM: store ptr getelementptr inbounds nuw (i8, ptr @_ZTV1B, i64 16), ptr %{{.*}}, align 8, !tbaa ![[TBAA_VPTR:.*]]
33+
// LLVM: ![[TBAA_VPTR]] = !{![[TBAA_VPTR_PARENT:.*]], ![[TBAA_VPTR_PARENT]], i64 0}
34+
// LLVM: ![[TBAA_VPTR_PARENT]] = !{!"vtable pointer", !

0 commit comments

Comments
 (0)