[clang] [CIR] Lower pointer const_array globals without insertvalue chains (PR #198427)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 10 11:51:47 PDT 2026


================
@@ -12,28 +12,45 @@
 
 #include "clang/CIR/LoweringHelpers.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
+#include "mlir/IR/SymbolTable.h"
 #include "clang/CIR/MissingFeatures.h"
 
+static unsigned getIntOrBoolBitWidth(mlir::Type ty) {
+  if (auto intTy = mlir::dyn_cast<cir::IntType>(ty))
+    return intTy.getWidth();
+  assert(mlir::isa<cir::BoolType>(ty) &&
+         "expected CIR integer or bool element type");
+  return 1;
+}
+
 mlir::DenseElementsAttr
 convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr,
                                      mlir::Type type) {
-  auto values = llvm::SmallVector<mlir::APInt, 8>{};
   const auto stringAttr = mlir::cast<mlir::StringAttr>(attr.getElts());
+  const auto arrayTy = mlir::cast<cir::ArrayType>(attr.getType());
+  const unsigned totalSize = arrayTy.getSize();
+  const unsigned trailingZeros = attr.getTrailingZerosNum();
+  assert(stringAttr.size() + trailingZeros == totalSize &&
+         "string const_array size must match explicit elements plus "
+         "trailing_zeros");
+
+  const unsigned bitWidth = getIntOrBoolBitWidth(arrayTy.getElementType());
+  llvm::SmallVector<mlir::APInt> values;
+  values.reserve(totalSize);
 
   for (const char element : stringAttr)
-    values.push_back({8, (uint64_t)element});
+    values.emplace_back(bitWidth, element);
 
-  const auto arrayTy = mlir::cast<cir::ArrayType>(attr.getType());
-  if (arrayTy.getSize() != stringAttr.size())
-    assert(!cir::MissingFeatures::stringTypeWithDifferentArraySize());
----------------
andykaylor wrote:

This can be removed from MissingFeatures.h now.

https://github.com/llvm/llvm-project/pull/198427


More information about the cfe-commits mailing list