[clang] [CIR] Upstream global initialization for ArrayType (PR #131657)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 18 10:57:52 PDT 2025
================
@@ -271,16 +314,61 @@ mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &value,
cgm.getASTContext().getTargetInfo().useFP16ConversionIntrinsics()) {
cgm.errorNYI("ConstExprEmitter::tryEmitPrivate half");
return {};
- } else {
- mlir::Type ty = cgm.convertType(destType);
- assert(mlir::isa<cir::CIRFPTypeInterface>(ty) &&
- "expected floating-point type");
- return cgm.getBuilder().getAttr<cir::FPAttr>(ty, init);
}
+
+ mlir::Type ty = cgm.convertType(destType);
+ assert(mlir::isa<cir::CIRFPTypeInterface>(ty) &&
+ "expected floating-point type");
+ return cgm.getBuilder().getAttr<cir::FPAttr>(ty, init);
}
case APValue::Array: {
- cgm.errorNYI("ConstExprEmitter::tryEmitPrivate array");
- return {};
+ const ArrayType *arrayTy = cgm.getASTContext().getAsArrayType(destType);
+ const QualType arrayElementTy = arrayTy->getElementType();
+ const unsigned numElements = value.getArraySize();
+ const unsigned numInitElts = value.getArrayInitializedElts();
+
+ mlir::Attribute filter;
+ if (value.hasArrayFiller()) {
+ filter =
+ tryEmitPrivate(value.getArrayFiller(), arrayTy->getElementType());
+ if (!filter)
+ return {};
+ }
+
+ SmallVector<mlir::TypedAttr, 16> elements;
+ if (filter && builder.isNullValue(filter))
+ elements.reserve(numInitElts + 1);
+ else
+ elements.reserve(numInitElts);
+
+ mlir::Type commonElementType;
+ for (unsigned i = 0; i < numInitElts; ++i) {
+ const APValue &arrayElement = value.getArrayInitializedElt(i);
+ const mlir::Attribute element =
+ tryEmitPrivateForMemory(arrayElement, arrayElementTy);
+ if (!element)
+ return {};
+
+ const mlir::TypedAttr elementTyped = mlir::cast<mlir::TypedAttr>(element);
+ if (i == 0)
+ commonElementType = elementTyped.getType();
+ else if (elementTyped.getType() != commonElementType) {
+ cgm.errorNYI("ConstExprEmitter::tryEmitPrivate Array without common "
----------------
AmrDeveloper wrote:
My point is that it's will not be assert or error after we support StructType
https://github.com/llvm/llvm-project/pull/131657
More information about the cfe-commits
mailing list