Skip to content

Commit c1ef1cb

Browse files
authored
Merge branch 'main' into merge-ifdef-same-func
2 parents 85d57c3 + c0fa432 commit c1ef1cb

File tree

39 files changed

+2101
-163
lines changed

39 files changed

+2101
-163
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ def CIR_ConstantOp : CIR_Op<"const", [
291291
return ptrAttr.isNullValue();
292292
return false;
293293
}
294+
295+
template <typename T>
296+
T getValueAttr() { return mlir::dyn_cast<T>(getValue()); }
294297
}];
295298

296299
let hasFolder = 1;

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,35 @@ def CIR_PointerType : CIR_Type<"Pointer", "ptr", [
281281
}];
282282
}
283283

284+
//===----------------------------------------------------------------------===//
285+
// CIR_VPtrType
286+
//===----------------------------------------------------------------------===//
287+
288+
def CIR_VPtrType : CIR_Type<"VPtr", "vptr", [
289+
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>
290+
]> {
291+
let summary = "CIR type that is used for the vptr member of C++ objects";
292+
let description = [{
293+
`cir.vptr` is a special type used as the type for the vptr member of a C++
294+
object. This avoids using arbitrary pointer types to declare vptr values
295+
and allows stronger type-based checking for operations that use or provide
296+
access to the vptr.
297+
298+
This type will be the element type of the 'vptr' member of structures that
299+
require a vtable pointer. A pointer to this type is returned by the
300+
`cir.vtable.address_point` and `cir.vtable.get_vptr` operations, and this
301+
pointer may be passed to the `cir.vtable.get_virtual_fn_addr` operation to
302+
get the address of a virtual function pointer.
303+
304+
The pointer may also be cast to other pointer types in order to perform
305+
pointer arithmetic based on information encoded in the AST layout to get
306+
the offset from a pointer to a dynamic object to the base object pointer,
307+
the base object offset value from the vtable, or the type information
308+
entry for an object.
309+
TODO: We should have special operations to do that too.
310+
}];
311+
}
312+
284313
//===----------------------------------------------------------------------===//
285314
// BoolType
286315
//===----------------------------------------------------------------------===//
@@ -635,7 +664,7 @@ def CIRRecordType : Type<
635664
def CIR_AnyType : AnyTypeOf<[
636665
CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_VectorType, CIR_IntType,
637666
CIR_AnyFloatType, CIR_PointerType, CIR_FuncType, CIR_RecordType,
638-
CIR_ComplexType
667+
CIR_ComplexType, CIR_VPtrType
639668
]>;
640669

641670
#endif // CLANG_CIR_DIALECT_IR_CIRTYPES_TD

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,16 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
349349
// doesn't happen, but it's not clear that it's worth it.
350350

351351
// Optimize for a constant count.
352-
auto constantCount = dyn_cast<cir::ConstantOp>(numElements.getDefiningOp());
353-
if (constantCount) {
354-
auto constIntAttr = mlir::dyn_cast<cir::IntAttr>(constantCount.getValue());
355-
// Just skip out if the constant count is zero.
356-
if (constIntAttr && constIntAttr.getUInt() == 0)
357-
return;
352+
if (auto constantCount = numElements.getDefiningOp<cir::ConstantOp>()) {
353+
if (auto constIntAttr = constantCount.getValueAttr<cir::IntAttr>()) {
354+
// Just skip out if the constant count is zero.
355+
if (constIntAttr.getUInt() == 0)
356+
return;
357+
// Otherwise, emit the check.
358+
}
359+
360+
if (constantCount.use_empty())
361+
constantCount.erase();
358362
} else {
359363
// Otherwise, emit the check.
360364
cgm.errorNYI(e->getSourceRange(), "dynamic-length array expression");
@@ -417,9 +421,6 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
417421
builder.create<cir::YieldOp>(loc);
418422
});
419423
}
420-
421-
if (constantCount.use_empty())
422-
constantCount.erase();
423424
}
424425

425426
void CIRGenFunction::emitDelegateCXXConstructorCall(

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -721,17 +721,16 @@ static const Expr *getSimpleArrayDecayOperand(const Expr *e) {
721721

722722
static cir::IntAttr getConstantIndexOrNull(mlir::Value idx) {
723723
// TODO(cir): should we consider using MLIRs IndexType instead of IntegerAttr?
724-
if (auto constantOp = dyn_cast<cir::ConstantOp>(idx.getDefiningOp()))
725-
return mlir::dyn_cast<cir::IntAttr>(constantOp.getValue());
724+
if (auto constantOp = idx.getDefiningOp<cir::ConstantOp>())
725+
return constantOp.getValueAttr<cir::IntAttr>();
726726
return {};
727727
}
728728

729729
static CharUnits getArrayElementAlign(CharUnits arrayAlign, mlir::Value idx,
730730
CharUnits eltSize) {
731731
// If we have a constant index, we can use the exact offset of the
732732
// element we're accessing.
733-
const cir::IntAttr constantIdx = getConstantIndexOrNull(idx);
734-
if (constantIdx) {
733+
if (const cir::IntAttr constantIdx = getConstantIndexOrNull(idx)) {
735734
const CharUnits offset = constantIdx.getValue().getZExtValue() * eltSize;
736735
return arrayAlign.alignmentAtOffset(offset);
737736
}

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ struct BinOpInfo {
4848
/// Check if the binop can result in integer overflow.
4949
bool mayHaveIntegerOverflow() const {
5050
// Without constant input, we can't rule out overflow.
51-
auto lhsci = dyn_cast<cir::ConstantOp>(lhs.getDefiningOp());
52-
auto rhsci = dyn_cast<cir::ConstantOp>(rhs.getDefiningOp());
51+
auto lhsci = lhs.getDefiningOp<cir::ConstantOp>();
52+
auto rhsci = rhs.getDefiningOp<cir::ConstantOp>();
5353
if (!lhsci || !rhsci)
5454
return true;
5555

clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct CIRRecordLowering final {
4141
// member type that ensures correct rounding.
4242
struct MemberInfo final {
4343
CharUnits offset;
44-
enum class InfoKind { Field, Base } kind;
44+
enum class InfoKind { VFPtr, Field, Base } kind;
4545
mlir::Type data;
4646
union {
4747
const FieldDecl *fieldDecl;
@@ -87,6 +87,8 @@ struct CIRRecordLowering final {
8787
accumulateBitFields(RecordDecl::field_iterator field,
8888
RecordDecl::field_iterator fieldEnd);
8989

90+
mlir::Type getVFPtrType();
91+
9092
bool isAAPCS() const {
9193
return astContext.getTargetInfo().getABI().starts_with("aapcs");
9294
}
@@ -902,9 +904,14 @@ void CIRRecordLowering::accumulateBases(const CXXRecordDecl *cxxRecordDecl) {
902904

903905
void CIRRecordLowering::accumulateVPtrs() {
904906
if (astRecordLayout.hasOwnVFPtr())
905-
cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(),
906-
"accumulateVPtrs: hasOwnVFPtr");
907+
members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::InfoKind::VFPtr,
908+
getVFPtrType()));
909+
907910
if (astRecordLayout.hasOwnVBPtr())
908911
cirGenTypes.getCGModule().errorNYI(recordDecl->getSourceRange(),
909912
"accumulateVPtrs: hasOwnVBPtr");
910913
}
914+
915+
mlir::Type CIRRecordLowering::getVFPtrType() {
916+
return cir::VPtrType::get(builder.getContext());
917+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,7 @@ LogicalResult cir::GetMemberOp::verify() {
18331833

18341834
OpFoldResult cir::VecCreateOp::fold(FoldAdaptor adaptor) {
18351835
if (llvm::any_of(getElements(), [](mlir::Value value) {
1836-
return !mlir::isa<cir::ConstantOp>(value.getDefiningOp());
1836+
return !value.getDefiningOp<cir::ConstantOp>();
18371837
}))
18381838
return {};
18391839

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,23 @@ BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
697697
return 1;
698698
}
699699

700+
//===----------------------------------------------------------------------===//
701+
// VPtrType Definitions
702+
//===----------------------------------------------------------------------===//
703+
704+
llvm::TypeSize
705+
VPtrType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
706+
mlir::DataLayoutEntryListRef params) const {
707+
// FIXME: consider size differences under different ABIs
708+
return llvm::TypeSize::getFixed(64);
709+
}
710+
711+
uint64_t VPtrType::getABIAlignment(const mlir::DataLayout &dataLayout,
712+
mlir::DataLayoutEntryListRef params) const {
713+
// FIXME: consider alignment differences under different ABIs
714+
return 8;
715+
}
716+
700717
//===----------------------------------------------------------------------===//
701718
// ArrayType Definitions
702719
//===----------------------------------------------------------------------===//

clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ struct SimplifyTernary final : public OpRewritePattern<TernaryOp> {
9797
// Check whether the region/block contains a cir.const followed by a
9898
// cir.yield that yields the value.
9999
auto yieldOp = mlir::cast<cir::YieldOp>(onlyBlock.getTerminator());
100-
auto yieldValueDefOp = mlir::dyn_cast_if_present<cir::ConstantOp>(
101-
yieldOp.getArgs()[0].getDefiningOp());
100+
auto yieldValueDefOp =
101+
yieldOp.getArgs()[0].getDefiningOp<cir::ConstantOp>();
102102
return yieldValueDefOp && yieldValueDefOp->getBlock() == &onlyBlock;
103103
}
104104
};
@@ -126,18 +126,13 @@ struct SimplifySelect : public OpRewritePattern<SelectOp> {
126126

127127
LogicalResult matchAndRewrite(SelectOp op,
128128
PatternRewriter &rewriter) const final {
129-
mlir::Operation *trueValueOp = op.getTrueValue().getDefiningOp();
130-
mlir::Operation *falseValueOp = op.getFalseValue().getDefiningOp();
131-
auto trueValueConstOp =
132-
mlir::dyn_cast_if_present<cir::ConstantOp>(trueValueOp);
133-
auto falseValueConstOp =
134-
mlir::dyn_cast_if_present<cir::ConstantOp>(falseValueOp);
135-
if (!trueValueConstOp || !falseValueConstOp)
129+
auto trueValueOp = op.getTrueValue().getDefiningOp<cir::ConstantOp>();
130+
auto falseValueOp = op.getFalseValue().getDefiningOp<cir::ConstantOp>();
131+
if (!trueValueOp || !falseValueOp)
136132
return mlir::failure();
137133

138-
auto trueValue = mlir::dyn_cast<cir::BoolAttr>(trueValueConstOp.getValue());
139-
auto falseValue =
140-
mlir::dyn_cast<cir::BoolAttr>(falseValueConstOp.getValue());
134+
auto trueValue = trueValueOp.getValueAttr<cir::BoolAttr>();
135+
auto falseValue = falseValueOp.getValueAttr<cir::BoolAttr>();
141136
if (!trueValue || !falseValue)
142137
return mlir::failure();
143138

@@ -265,8 +260,7 @@ struct SimplifyVecSplat : public OpRewritePattern<VecSplatOp> {
265260
LogicalResult matchAndRewrite(VecSplatOp op,
266261
PatternRewriter &rewriter) const override {
267262
mlir::Value splatValue = op.getValue();
268-
auto constant =
269-
mlir::dyn_cast_if_present<cir::ConstantOp>(splatValue.getDefiningOp());
263+
auto constant = splatValue.getDefiningOp<cir::ConstantOp>();
270264
if (!constant)
271265
return mlir::failure();
272266

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,12 +1935,11 @@ mlir::LogicalResult CIRToLLVMSelectOpLowering::matchAndRewrite(
19351935
cir::SelectOp op, OpAdaptor adaptor,
19361936
mlir::ConversionPatternRewriter &rewriter) const {
19371937
auto getConstantBool = [](mlir::Value value) -> cir::BoolAttr {
1938-
auto definingOp =
1939-
mlir::dyn_cast_if_present<cir::ConstantOp>(value.getDefiningOp());
1938+
auto definingOp = value.getDefiningOp<cir::ConstantOp>();
19401939
if (!definingOp)
19411940
return {};
19421941

1943-
auto constValue = mlir::dyn_cast<cir::BoolAttr>(definingOp.getValue());
1942+
auto constValue = definingOp.getValueAttr<cir::BoolAttr>();
19441943
if (!constValue)
19451944
return {};
19461945

0 commit comments

Comments
 (0)