[clang] [CIR] Upstream support for string literals (PR #140796)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Tue May 20 13:59:03 PDT 2025
================
@@ -26,6 +26,34 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
CIRGenBuilderTy(mlir::MLIRContext &mlirContext, const CIRGenTypeCache &tc)
: CIRBaseBuilderTy(mlirContext), typeCache(tc) {}
+ /// Get a cir::ConstArrayAttr for a string literal.
+ /// Note: This is different from what is returned by
+ /// mlir::Builder::getStringAttr() which is an mlir::StringAttr.
+ mlir::Attribute getString(llvm::StringRef str, mlir::Type eltTy,
+ unsigned size) {
+ unsigned finalSize = size ? size : str.size();
+
+ size_t lastNonZeroPos = str.find_last_not_of('\0');
+ // If the string is full of null bytes, emit a #cir.zero rather than
+ // a #cir.const_array.
+ if (lastNonZeroPos == llvm::StringRef::npos) {
+ auto arrayTy = cir::ArrayType::get(eltTy, finalSize);
+ return cir::ZeroAttr::get(arrayTy);
+ }
+ // We emit trailing zeros only if there are multiple trailing zeros.
+ int trailingZerosNum = 0;
----------------
erichkeane wrote:
This could probably end up being size_t in length with a big enough set of zeros, right? It doesn't look like you try tomake it negative anywhere, so perhaps it should too be size_t?
https://github.com/llvm/llvm-project/pull/140796
More information about the cfe-commits
mailing list