Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class VarDecl;
class RecordDecl;
} // namespace clang

namespace cir {
class ArrayType;
} // namespace cir

#define GET_ATTRDEF_CLASSES
#include "clang/CIR/Dialect/IR/CIROpsAttributes.h.inc"

Expand Down
21 changes: 20 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ def CIR_BoolType :
}];
}

//===----------------------------------------------------------------------===//
// ArrayType
//===----------------------------------------------------------------------===//

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

let summary = "CIR array type";
let description = [{
`CIR.array` represents C/C++ constant arrays.
}];

let parameters = (ins "mlir::Type":$eltType, "uint64_t":$size);

let assemblyFormat = [{
`<` $eltType `x` $size `>`
}];
}

//===----------------------------------------------------------------------===//
// FuncType
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -386,7 +405,7 @@ def VoidPtr : Type<
//===----------------------------------------------------------------------===//

def CIR_AnyType : AnyTypeOf<[
CIR_VoidType, CIR_BoolType, CIR_IntType, CIR_AnyFloat, CIR_PointerType,
CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_IntType, CIR_AnyFloat, CIR_PointerType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line is over 80 characters. It should wrap to avoid that.

CIR_FuncType
]>;

Expand Down
8 changes: 8 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
llvm_unreachable("NYI: PPC double-double format for long double");
llvm_unreachable("Unsupported format for long double");
}

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

} // namespace clang::CIRGen
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
break;
}

case Type::ConstantArray: {
const ConstantArrayType *arrTy = cast<ConstantArrayType>(ty);
mlir::Type elemTy = convertTypeForMem(arrTy->getElementType());

// 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(elemTy) && "not implemented");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use the cgm.errorNYI, then just come up with a way to recover. That said, this is exactly why having isSized assert is awkward here.

Copy link
Member Author

@AmrDeveloper AmrDeveloper Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey, thats make sense and i will try to recover with SInt32Ty

resultType = cir::ArrayType::get(builder.getContext(), elemTy,
arrTy->getSize().getZExtValue());
break;
}

case Type::FunctionNoProto:
case Type::FunctionProto:
resultType = convertFunctionTypeInternal(type);
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
return success();
}

if (mlir::isa<cir::ArrayType>(opType))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this in the incubator implementation of this function. Why is this here?

return success();

assert(isa<TypedAttr>(attrType) && "What else could we be looking at here?");
return op->emitOpError("global with type ")
<< cast<TypedAttr>(attrType).getType() << " not yet supported";
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,22 @@ BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
return 1;
}

//===----------------------------------------------------------------------===//
// Definitions
//===----------------------------------------------------------------------===//

llvm::TypeSize
ArrayType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these functions being called with any of the test cases? I don't see any checks that would be related to this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of sizeof operator but it's not implemented yet, is there is another way to test the size in lit test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point was that if the code isn't being called, you should leave it out until it is needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it's part of the DataLayoutTypeInterface interface so we must implement it, I can test it after merging the sizeof PR #130847

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thanks for the clarification.

::mlir::DataLayoutEntryListRef params) const {
return getSize() * dataLayout.getTypeSizeInBits(getEltType());
}

uint64_t
ArrayType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return dataLayout.getTypeABIAlignment(getEltType());
}

//===----------------------------------------------------------------------===//
// PointerType Definitions
//===----------------------------------------------------------------------===//
Expand Down
12 changes: 12 additions & 0 deletions clang/test/CIR/CodeGen/array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will multidimensional arrays and array function arguments work at this point? If so, can you add tests for those?


int a[10];
// CHECK: cir.global external @a : !cir.array<!cir.int<s, 32> x 10>

extern int b[10];
// CHECK: cir.global external @b : !cir.array<!cir.int<s, 32> x 10>

void f() {
int c[10];
// CHECK: %[[ARR:.*]] = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["c"]
}
Loading