Skip to content

Commit ec6dc13

Browse files
authored
Improve derived class casting support. (#272)
1 parent 62337aa commit ec6dc13

File tree

2 files changed

+85
-7
lines changed

2 files changed

+85
-7
lines changed

tools/cgeist/Lib/clang-mlir.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3297,8 +3297,8 @@ mlir::Value MLIRScanner::GetAddressOfDerivedClass(
32973297

32983298
// Add the offset.
32993299

3300-
mlir::Type nt = getMLIRType(
3301-
Glob.CGM.getContext().getLValueReferenceType(Base->getType()));
3300+
mlir::Type nt = getMLIRType(Glob.CGM.getContext().getLValueReferenceType(
3301+
QualType(RD->getTypeForDecl(), 0)));
33023302

33033303
mlir::Value Offset = nullptr;
33043304
if (isLLVMStructABI(RD, /*ST*/ nullptr)) {
@@ -3326,16 +3326,18 @@ mlir::Value MLIRScanner::GetAddressOfDerivedClass(
33263326
}
33273327

33283328
mlir::Value ptr = value;
3329-
if (auto PT = ptr.getType().dyn_cast<LLVM::LLVMPointerType>())
3329+
if (auto PT = ptr.getType().dyn_cast<LLVM::LLVMPointerType>()) {
33303330
ptr = builder.create<LLVM::BitcastOp>(
33313331
loc,
33323332
LLVM::LLVMPointerType::get(builder.getI8Type(), PT.getAddressSpace()),
33333333
ptr);
3334-
else
3334+
} else {
3335+
unsigned memorySpace =
3336+
ptr.getType().cast<mlir::MemRefType>().getMemorySpaceAsInt();
33353337
ptr = builder.create<polygeist::Memref2PointerOp>(
3336-
loc,
3337-
LLVM::LLVMPointerType::get(builder.getI8Type(), PT.getAddressSpace()),
3338+
loc, LLVM::LLVMPointerType::get(builder.getI8Type(), memorySpace),
33383339
ptr);
3340+
}
33393341

33403342
mlir::Value idx[] = {Offset};
33413343
ptr = builder.create<LLVM::GEPOp>(loc, ptr.getType(), ptr, idx);
@@ -3511,7 +3513,7 @@ ValueCategory MLIRScanner::VisitCastExpr(CastExpr *E) {
35113513
if (auto MT = ptr.getType().dyn_cast<MemRefType>())
35123514
nullptr_llvm =
35133515
builder.create<polygeist::Pointer2MemrefOp>(loc, MT, nullptr_llvm);
3514-
val = builder.create<arith::SelectOp>(loc, ne, val, nullptr_llvm);
3516+
val = builder.create<arith::SelectOp>(loc, ne, ptr, nullptr_llvm);
35153517
}
35163518
return ValueCategory(val, se.isReference);
35173519
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// RUN: cgeist %s --function=* -S | FileCheck %s
2+
// RUN: cgeist %s --function=* --struct-abi=0 -memref-abi=0 -S | FileCheck %s --check-prefix CHECK-STR
3+
4+
5+
struct A {
6+
int val1;
7+
int val2;
8+
};
9+
10+
struct B {
11+
bool bool1;
12+
};
13+
14+
struct C : B, A {
15+
int val3;
16+
};
17+
18+
struct D : C {
19+
int val3;
20+
bool bool2;
21+
};
22+
23+
C* castAtoC(A *a) {
24+
// A -> C
25+
return static_cast<C *>(a);
26+
};
27+
28+
D* castBtoD(B *b) {
29+
// B -> C -> D
30+
return static_cast<D *>(b);
31+
}
32+
33+
D* castAtoD(A *b) {
34+
// A -> C -> D
35+
return static_cast<D *>(b);
36+
}
37+
38+
int main() {
39+
C c;
40+
D d;
41+
c.val3 = 2;
42+
d.val3 = 2;
43+
return
44+
castAtoC(&c)->val3 + // expect nonzero offset due to A -> C
45+
castBtoD(&d)->val3 +
46+
castAtoD(&d)->val3; // expect nonzero offset due to A -> C
47+
}
48+
49+
// CHECK: func.func @_Z8castAtoCP1A(
50+
// CHECK-NEXT: polygeist.memref2pointer
51+
// CHECK-NEXT: llvm.getelementptr {{.*}}[-1]
52+
// CHECK-NEXT: llvm.bitcast
53+
// CHECK-NEXT: return
54+
// CHECK: func.func @_Z8castBtoDP1B(
55+
// CHECK-NEXT: polygeist.memref2pointer
56+
// CHECK-NEXT: llvm.bitcast
57+
// CHECK-NEXT: return
58+
// CHECK: func.func @_Z8castAtoDP1A(
59+
// CHECK-NEXT: polygeist.memref2pointer
60+
// CHECK-NEXT: llvm.getelementptr {{.*}}[-1]
61+
// CHECK-NEXT: llvm.bitcast
62+
// CHECK-NEXT: return
63+
// CHECK: func.func @main()
64+
// CHECK: call @_Z8castAtoCP1A(
65+
// CHECK: call @_Z8castBtoDP1B(
66+
// CHECK: call @_Z8castAtoDP1A(
67+
68+
// CHECK-STR: func.func @_Z8castAtoCP1A(
69+
// CHECK-STR-NEXT: llvm.bitcast
70+
// CHECK-STR-NEXT: llvm.getelementptr {{.*}}[-4]
71+
// CHECK-STR-NEXT: llvm.bitcast
72+
// CHECK-STR-NEXT: return
73+
// CHECK-STR: func.func @_Z8castBtoDP1B(
74+
// CHECK-STR-NEXT: llvm.bitcast
75+
// CHECK-STR-NEXT: llvm.bitcast
76+
// CHECK-STR-NEXT: return

0 commit comments

Comments
 (0)