Skip to content
Merged
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
3 changes: 3 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ struct RecordTypeStorage;

bool isValidFundamentalIntWidth(unsigned width);

// Returns true if the type is a CIR sized type.
bool isSized(mlir::Type ty);

} // namespace cir

mlir::ParseResult parseAddrSpaceAttribute(mlir::AsmParser &p,
Expand Down
67 changes: 44 additions & 23 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ class CIR_Type<string name, string typeMnemonic, list<Trait> traits = [],
// IntType
//===----------------------------------------------------------------------===//

def CIR_IntType : CIR_Type<"Int", "int",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
def CIR_IntType : CIR_Type<"Int", "int", [
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
DeclareTypeInterfaceMethods<CIR_SizedTypeInterface>,
]> {
let summary = "Integer type with arbitrary precision up to a fixed limit";
let description = [{
CIR type that represents integer types with arbitrary precision.
Expand Down Expand Up @@ -81,8 +83,9 @@ def CIR_IntType : CIR_Type<"Int", "int",
//===----------------------------------------------------------------------===//

class CIR_FloatType<string name, string mnemonic> : CIR_Type<name, mnemonic, [
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
DeclareTypeInterfaceMethods<CIR_FPTypeInterface>
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
DeclareTypeInterfaceMethods<CIR_FPTypeInterface>,
DeclareTypeInterfaceMethods<CIR_SizedTypeInterface>
]>;

def CIR_Single : CIR_FloatType<"Single", "float"> {
Expand Down Expand Up @@ -151,9 +154,10 @@ def CIR_LongDouble : CIR_FloatType<"LongDouble", "long_double"> {
// ComplexType
//===----------------------------------------------------------------------===//

def CIR_ComplexType : CIR_Type<"Complex", "complex",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {

def CIR_ComplexType : CIR_Type<"Complex", "complex", [
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
DeclareTypeInterfaceMethods<CIR_SizedTypeInterface>
]> {
let summary = "CIR complex type";
let description = [{
CIR type that represents a C complex number. `cir.complex` models the C type
Expand Down Expand Up @@ -194,9 +198,10 @@ def CIR_ComplexType : CIR_Type<"Complex", "complex",
// PointerType
//===----------------------------------------------------------------------===//

def CIR_PointerType : CIR_Type<"Pointer", "ptr",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {

def CIR_PointerType : CIR_Type<"Pointer", "ptr", [
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
DeclareTypeInterfaceMethods<CIR_SizedTypeInterface>
]> {
let summary = "CIR pointer type";
let description = [{
`CIR.ptr` is a type returned by any op generating a pointer in C++.
Expand Down Expand Up @@ -295,9 +300,10 @@ def CIR_DataMemberType : CIR_Type<"DataMember", "data_member",
// BoolType
//===----------------------------------------------------------------------===//

def CIR_BoolType : CIR_Type<"Bool", "bool",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {

def CIR_BoolType : CIR_Type<"Bool", "bool", [
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
DeclareTypeInterfaceMethods<CIR_SizedTypeInterface>
]> {
let summary = "CIR bool type";
let description = [{
`cir.bool` represent's C++ bool type.
Expand All @@ -308,9 +314,10 @@ def CIR_BoolType : CIR_Type<"Bool", "bool",
// ArrayType
//===----------------------------------------------------------------------===//

def CIR_ArrayType : CIR_Type<"Array", "array",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {

def CIR_ArrayType : CIR_Type<"Array", "array", [
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
DeclareTypeInterfaceMethods<CIR_SizedTypeInterface, ["isSized"]>,
]> {
let summary = "CIR array type";
let description = [{
`CIR.array` represents C/C++ constant arrays.
Expand All @@ -329,14 +336,22 @@ def CIR_ArrayType : CIR_Type<"Array", "array",
let assemblyFormat = [{
`<` $elementType `x` $size `>`
}];

let extraClassDefinition = [{
bool $cppClass::isSized() const {
return ::cir::isSized(getElementType());
}
}];
}

//===----------------------------------------------------------------------===//
// VectorType (fixed size)
//===----------------------------------------------------------------------===//

def CIR_VectorType : CIR_Type<"Vector", "vector",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
def CIR_VectorType : CIR_Type<"Vector", "vector", [
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
DeclareTypeInterfaceMethods<CIR_SizedTypeInterface, ["isSized"]>,
]> {

let summary = "CIR vector type";
let description = [{
Expand Down Expand Up @@ -378,6 +393,12 @@ def CIR_VectorType : CIR_Type<"Vector", "vector",
`<` $elementType `x` $size `>`
}];

let extraClassDefinition = [{
bool $cppClass::isSized() const {
return ::cir::isSized(getElementType());
}
}];

let genVerifyDecl = 1;
}

Expand Down Expand Up @@ -524,11 +545,11 @@ def CIR_VoidType : CIR_Type<"Void", "void"> {
// The base type for all RecordDecls.
//===----------------------------------------------------------------------===//

def CIR_RecordType : CIR_Type<"Record", "record",
[
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
MutableType,
]> {
def CIR_RecordType : CIR_Type<"Record", "record", [
DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
DeclareTypeInterfaceMethods<CIR_SizedTypeInterface>,
MutableType,
]> {
let summary = "CIR record type";
let description = [{
Each unique clang::RecordDecl is mapped to a `cir.record` and any object in
Expand Down
28 changes: 28 additions & 0 deletions clang/include/clang/CIR/Interfaces/CIRTypeInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,32 @@ def CIR_FPTypeInterface : TypeInterface<"FPTypeInterface"> {
];
}

def CIR_SizedTypeInterface : TypeInterface<"SizedTypeInterface"> {
let description = [{
Annotates types that have known size. Types that don't have a size are
abstract types and void.
}];

let cppNamespace = "::cir";

let methods = [
InterfaceMethod<[{
Returns true if this is a sized type. This mirrors sizedness from the
clang AST, where a type is sized if it has a known size.

By default type defining this interface returns true,
but this can be overridden if sizedness depends on properties of the type.
For example, whether a struct is not sized if it is incomplete.
}],
/*retTy=*/"bool",
/*methodName=*/"isSized",
/*args=*/(ins),
/*methodBody=*/"",
/*defaultImplementation=*/[{
return true;
}]
>,
];
}

#endif // CLANG_CIR_INTERFACES_CIRTYPEINTERFACES_TD
12 changes: 0 additions & 12 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,18 +527,6 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
return getCompleteRecordTy(members, name, packed, padded, ast);
}

bool isSized(mlir::Type ty) {
if (mlir::isa<cir::PointerType, cir::RecordType, cir::ArrayType,
cir::BoolType, cir::IntType, cir::FPTypeInterface,
cir::ComplexType>(ty))
return true;
if (mlir::isa<cir::VectorType>(ty)) {
return isSized(mlir::cast<cir::VectorType>(ty).getElementType());
}
assert(0 && "Unimplemented size for type");
return false;
}

//
// Constant creation helpers
// -------------------------
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ mlir::Type CIRGenTypes::convertType(QualType T) {
// int X[] -> [0 x int], unless the element type is not sized. If it is
// unsized (e.g. an incomplete record) just use [0 x i8].
ResultType = convertTypeForMem(A->getElementType());
if (!Builder.isSized(ResultType)) {
if (!cir::isSized(ResultType)) {
SkippedLayout = true;
ResultType = Builder.getUInt8Ty();
}
Expand All @@ -659,7 +659,7 @@ mlir::Type CIRGenTypes::convertType(QualType T) {

// FIXME: In LLVM, "lower arrays of undefined struct type to arrays of
// i8 just to have a concrete type". Not sure this makes sense in CIR yet.
assert(Builder.isSized(EltTy) && "not implemented");
assert(cir::isSized(EltTy) && "not implemented");
ResultType = cir::ArrayType::get(EltTy, A->getSize().getZExtValue());
break;
}
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@

using cir::MissingFeatures;

//===----------------------------------------------------------------------===//
// CIR Helpers
//===----------------------------------------------------------------------===//

bool cir::isSized(mlir::Type ty) {
if (auto sizedTy = mlir::dyn_cast<cir::SizedTypeInterface>(ty))
return sizedTy.isSized();
// TODO: Remove this once all sized types are annotated.
assert(0 && "Unimplemented size for type");
return false;
}

//===----------------------------------------------------------------------===//
// CIR Custom Parser/Printer Signatures
//===----------------------------------------------------------------------===//
Expand Down
Loading