Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
cir::BoolType getBoolTy() { return cir::BoolType::get(getContext()); }
cir::VoidType getVoidTy() { return cir::VoidType::get(getContext()); }

cir::IntType getUIntNTy(int n) {
return cir::IntType::get(getContext(), n, false);
}

cir::IntType getSIntNTy(int n) {
return cir::IntType::get(getContext(), n, true);
}

cir::PointerType getPointerTo(mlir::Type ty) {
return cir::PointerType::get(ty);
}
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,16 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
/*relative_layout=*/false);
}

mlir::Value createDynCastToVoid(mlir::Location loc, mlir::Value src,
bool vtableUseRelativeLayout) {
// TODO(cir): consider address space here.
assert(!cir::MissingFeatures::addressSpace());
cir::PointerType destTy = getVoidPtrTy();
return cir::DynamicCastOp::create(
*this, loc, destTy, cir::DynamicCastKind::Ptr, src,
cir::DynamicCastInfoAttr{}, vtableUseRelativeLayout);
}

Address createBaseClassAddr(mlir::Location loc, Address addr,
mlir::Type destType, unsigned offset,
bool assumeNotNull) {
Expand Down
15 changes: 11 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,15 @@ static cir::FuncOp getItaniumDynamicCastFn(CIRGenFunction &cgf) {
return cgf.cgm.createRuntimeFunction(FTy, "__dynamic_cast");
}

static Address emitDynamicCastToVoid(CIRGenFunction &cgf, mlir::Location loc,
QualType srcRecordTy, Address src) {
bool vtableUsesRelativeLayout =
cgf.cgm.getItaniumVTableContext().isRelativeLayout();
mlir::Value ptr = cgf.getBuilder().createDynCastToVoid(
loc, src.getPointer(), vtableUsesRelativeLayout);
return Address{ptr, src.getAlignment()};
}

static cir::DynamicCastInfoAttr emitDynamicCastInfo(CIRGenFunction &cgf,
mlir::Location loc,
QualType srcRecordTy,
Expand Down Expand Up @@ -1959,10 +1968,8 @@ mlir::Value CIRGenItaniumCXXABI::emitDynamicCast(CIRGenFunction &cgf,
bool isCastToVoid = destRecordTy.isNull();
assert((!isCastToVoid || !isRefCast) && "cannot cast to void reference");

if (isCastToVoid) {
cgm.errorNYI(loc, "emitDynamicCastToVoid");
return {};
}
if (isCastToVoid)
return emitDynamicCastToVoid(cgf, loc, srcRecordTy, src).getPointer();

// If the destination is effectively final, the cast succeeds if and only
// if the dynamic type of the pointer is exactly the destination type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,53 @@ static mlir::Value
buildDynamicCastToVoidAfterNullCheck(cir::CIRBaseBuilderTy &builder,
clang::ASTContext &astCtx,
cir::DynamicCastOp op) {
llvm_unreachable("dynamic cast to void is NYI");
mlir::Location loc = op.getLoc();
bool vtableUsesRelativeLayout = op.getRelativeLayout();

// TODO(cir): consider address space in this function.
assert(!cir::MissingFeatures::addressSpace());

mlir::Type vtableElemTy;
uint64_t vtableElemAlign;
if (vtableUsesRelativeLayout) {
vtableElemTy = builder.getSIntNTy(32);
vtableElemAlign = 4;
} else {
const auto &targetInfo = astCtx.getTargetInfo();
auto ptrdiffTy = targetInfo.getPtrDiffType(clang::LangAS::Default);
bool ptrdiffTyIsSigned = clang::TargetInfo::isTypeSigned(ptrdiffTy);
uint64_t ptrdiffTyWidth = targetInfo.getTypeWidth(ptrdiffTy);

vtableElemTy = cir::IntType::get(builder.getContext(), ptrdiffTyWidth,
ptrdiffTyIsSigned);
vtableElemAlign =
llvm::divideCeil(targetInfo.getPointerAlign(clang::LangAS::Default), 8);
}

// Access vtable to get the offset from the given object to its containing
// complete object.
// TODO: Add a specialized operation to get the object offset?
auto vptrTy = cir::VPtrType::get(builder.getContext());
cir::PointerType vptrPtrTy = builder.getPointerTo(vptrTy);
auto vptrPtr =
cir::VTableGetVPtrOp::create(builder, loc, vptrPtrTy, op.getSrc());
mlir::Value vptr = builder.createLoad(loc, vptrPtr);
mlir::Value elementPtr =
builder.createBitcast(vptr, builder.getPointerTo(vtableElemTy));
mlir::Value minusTwo = builder.getSignedInt(loc, -2, 64);
auto offsetToTopSlotPtr = cir::PtrStrideOp::create(
builder, loc, builder.getPointerTo(vtableElemTy), elementPtr, minusTwo);
mlir::Value offsetToTop =
builder.createAlignedLoad(loc, offsetToTopSlotPtr, vtableElemAlign);

// Add the offset to the given pointer to get the cast result.
// Cast the input pointer to a uint8_t* to allow pointer arithmetic.
cir::PointerType u8PtrTy = builder.getPointerTo(builder.getUIntNTy(8));
mlir::Value srcBytePtr = builder.createBitcast(op.getSrc(), u8PtrTy);
auto dstBytePtr =
cir::PtrStrideOp::create(builder, loc, u8PtrTy, srcBytePtr, offsetToTop);
// Cast the result to a void*.
return builder.createBitcast(dstBytePtr, builder.getVoidPtrTy());
}

mlir::Value
Expand Down
56 changes: 56 additions & 0 deletions clang/test/CIR/CodeGen/dynamic-cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,59 @@ Derived &ref_cast(Base &b) {
// OGCG: br i1 %[[IS_NULL]], label %[[BAD_CAST:.*]], label %[[DONE:.*]]
// OGCG: [[BAD_CAST]]:
// OGCG: call void @__cxa_bad_cast()

void *ptr_cast_to_complete(Base *ptr) {
return dynamic_cast<void *>(ptr);
}

// CIR-BEFORE: cir.func dso_local @_Z20ptr_cast_to_completeP4Base
// CIR-BEFORE: %{{.+}} = cir.dyn_cast ptr %{{.+}} : !cir.ptr<!rec_Base> -> !cir.ptr<!void>
// CIR-BEFORE: }

// CIR-AFTER: cir.func dso_local @_Z20ptr_cast_to_completeP4Base
// CIR-AFTER: %[[SRC:.*]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!cir.ptr<!rec_Base>>, !cir.ptr<!rec_Base>
// CIR-AFTER-NEXT: %[[SRC_IS_NOT_NULL:.*]] = cir.cast ptr_to_bool %[[SRC]] : !cir.ptr<!rec_Base> -> !cir.bool
// CIR-AFTER-NEXT: %{{.+}} = cir.ternary(%[[SRC_IS_NOT_NULL]], true {
// CIR-AFTER-NEXT: %[[VPTR_PTR:.*]] = cir.vtable.get_vptr %[[SRC]] : !cir.ptr<!rec_Base> -> !cir.ptr<!cir.vptr>
// CIR-AFTER-NEXT: %[[VPTR:.*]] = cir.load %[[VPTR_PTR]] : !cir.ptr<!cir.vptr>, !cir.vptr
// CIR-AFTER-NEXT: %[[ELEM_PTR:.*]] = cir.cast bitcast %[[VPTR]] : !cir.vptr -> !cir.ptr<!s64i>
// CIR-AFTER-NEXT: %[[MINUS_TWO:.*]] = cir.const #cir.int<-2> : !s64i
// CIR-AFTER-NEXT: %[[BASE_OFFSET_PTR:.*]] = cir.ptr_stride %[[ELEM_PTR]], %[[MINUS_TWO]] : (!cir.ptr<!s64i>, !s64i) -> !cir.ptr<!s64i>
// CIR-AFTER-NEXT: %[[BASE_OFFSET:.*]] = cir.load{{.*}} %[[BASE_OFFSET_PTR]] : !cir.ptr<!s64i>, !s64i
// CIR-AFTER-NEXT: %[[SRC_BYTES_PTR:.*]] = cir.cast bitcast %[[SRC]] : !cir.ptr<!rec_Base> -> !cir.ptr<!u8i>
// CIR-AFTER-NEXT: %[[DST_BYTES_PTR:.*]] = cir.ptr_stride %[[SRC_BYTES_PTR]], %[[BASE_OFFSET]] : (!cir.ptr<!u8i>, !s64i) -> !cir.ptr<!u8i>
// CIR-AFTER-NEXT: %[[CASTED_PTR:.*]] = cir.cast bitcast %[[DST_BYTES_PTR]] : !cir.ptr<!u8i> -> !cir.ptr<!void>
// CIR-AFTER-NEXT: cir.yield %[[CASTED_PTR]] : !cir.ptr<!void>
// CIR-AFTER-NEXT: }, false {
// CIR-AFTER-NEXT: %[[NULL_PTR:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!void>
// CIR-AFTER-NEXT: cir.yield %[[NULL_PTR]] : !cir.ptr<!void>
// CIR-AFTER-NEXT: }) : (!cir.bool) -> !cir.ptr<!void>
// CIR-AFTER: }

// LLVM: define {{.*}} @_Z20ptr_cast_to_completeP4Base
// LLVM: %[[IS_NOT_NULL:.*]] = icmp ne ptr %[[PTR:.*]], null
// LLVM: br i1 %[[IS_NOT_NULL]], label %[[NOT_NULL:.*]], label %[[NULL:.*]]
// LLVM: [[NOT_NULL]]:
// LLVM: %[[VPTR:.*]] = load ptr, ptr %[[PTR]]
// LLVM: %[[BASE_OFFSET_PTR:.*]] = getelementptr i64, ptr %7, i64 -2
// LLVM: %[[BASE_OFFSET:.*]] = load i64, ptr %[[BASE_OFFSET_PTR]]
// LLVM: %[[RESULT:.*]] = getelementptr i8, ptr %[[PTR]], i64 %[[BASE_OFFSET]]
// LLVM: br label %[[DONE:.*]]
// LLVM: [[NULL]]:
// LLVM: br label %[[DONE]]
// LLVM: [[DONE]]:
// LLVM: %[[RET:.*]] = phi ptr [ null, %[[NULL]] ], [ %[[RESULT]], %[[NOT_NULL]] ]

// OGCG: define {{.*}} @_Z20ptr_cast_to_completeP4Base
// OGCG: %[[IS_NULL:.*]] = icmp eq ptr %[[PTR:.*]], null
// OGCG: br i1 %[[IS_NULL]], label %[[NULL:.*]], label %[[NOT_NULL:.*]]
// OGCG: [[NOT_NULL]]:
// OGCG: %[[VPTR:.*]] = load ptr, ptr %[[PTR]]
// OGCG: %[[BASE_OFFSET_PTR:.*]] = getelementptr inbounds i64, ptr %[[VPTR]], i64 -2
// OGCG: %[[BASE_OFFSET:.*]] = load i64, ptr %[[BASE_OFFSET_PTR]]
// OGCG: %[[RESULT:.*]] = getelementptr inbounds i8, ptr %[[PTR]], i64 %[[BASE_OFFSET]]
// OGCG: br label %[[DONE:.*]]
// OGCG: [[NULL]]:
// OGCG: br label %[[DONE]]
// OGCG: [[DONE]]:
// OGCG: %[[RET:.*]] = phi ptr [ %[[RESULT]], %[[NOT_NULL]] ], [ null, %[[NULL]] ]