[clang] [CIR] Upstream global initialization for ArrayType (PR #131657)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 19 10:26:59 PDT 2025
================
@@ -158,13 +158,56 @@ class ConstExprEmitter
// TODO(cir): this can be shared with LLVM's codegen
static QualType getNonMemoryType(CIRGenModule &cgm, QualType type) {
- if (auto at = type->getAs<AtomicType>()) {
+ if (const auto *at = type->getAs<AtomicType>()) {
return cgm.getASTContext().getQualifiedType(at->getValueType(),
type.getQualifiers());
}
return type;
}
+static mlir::Attribute
+emitArrayConstant(CIRGenModule &cgm, mlir::Type desiredType,
+ mlir::Type commonElementType, unsigned arrayBound,
+ SmallVectorImpl<mlir::TypedAttr> &elements,
+ mlir::TypedAttr filter) {
+ const auto &builder = cgm.getBuilder();
+
+ unsigned nonzeroLength = arrayBound;
+ if (elements.size() < nonzeroLength && builder.isNullValue(filter))
+ nonzeroLength = elements.size();
+
+ if (nonzeroLength == elements.size()) {
+ while (nonzeroLength > 0 &&
+ builder.isNullValue(elements[nonzeroLength - 1]))
+ --nonzeroLength;
+ }
+
+ if (nonzeroLength == 0)
+ return cir::ZeroAttr::get(builder.getContext(), desiredType);
+
+ const unsigned trailingZeroes = arrayBound - nonzeroLength;
+ if (trailingZeroes >= 8) {
+ if (elements.size() < nonzeroLength)
+ cgm.errorNYI("missing initializer for non-zero element");
+ } else if (elements.size() != arrayBound) {
+ elements.resize(arrayBound, filter);
+
+ if (filter.getType() != commonElementType)
+ cgm.errorNYI(
----------------
andykaylor wrote:
I see. It confused me because you put the errorNYI here rather than at the bottom of the function where the incubator code creates the struct. I suppose this way would work, but it will be easier to update the code later if you maintain the structure of the incubator code and put the errorNYI code at the end.
https://github.com/llvm/llvm-project/pull/131657
More information about the cfe-commits
mailing list