[clang] [CIR] Fix handling of wide-string values >2^31. (PR #228101)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 1 08:03:52 PDT 2026
https://github.com/erichkeane created https://github.com/llvm/llvm-project/pull/228101
This showed up in Bruno's bug report, but is a pretty easy one. We were using the wrong overload of IntAttr::get and ended up having the value converted to a 64 bit, and it is obviously too big to fit back into the 32 bit value. This patch uses the APInt overload to make sure we keep the size correct.
>From ba5a14a812db85165ca4dc04659f31dff019477e Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Thu, 1 Oct 2026 08:01:56 -0700
Subject: [PATCH] [CIR] Fix handling of wide-string values >2^31.
This showed up in Bruno's bug report, but is a pretty easy one. We were
using the wrong overload of IntAttr::get and ended up having the value
converted to a 64 bit, and it is obviously too big to fit back into the
32 bit value. This patch uses the APInt overload to make sure we
keep the size correct.
---
clang/lib/CIR/CodeGen/CIRGenModule.cpp | 3 ++-
clang/test/CIR/CodeGen/wide-string.cpp | 8 ++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index cc0f36a0e7992..1bedd0d791855 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -1821,7 +1821,8 @@ CIRGenModule::getConstantArrayFromStringLiteral(const StringLiteral *e) {
SmallVector<mlir::Attribute> elements;
elements.reserve(arraySize);
for (unsigned i = 0; i < literalSize; ++i)
- elements.push_back(cir::IntAttr::get(arrayEltTy, e->getCodeUnit(i)));
+ elements.push_back(cir::IntAttr::get(
+ arrayEltTy, llvm::APInt(arrayEltTy.getWidth(), e->getCodeUnit(i))));
auto elementsAttr = mlir::ArrayAttr::get(&getMLIRContext(), elements);
return builder.getConstArray(elementsAttr, arrayTy);
diff --git a/clang/test/CIR/CodeGen/wide-string.cpp b/clang/test/CIR/CodeGen/wide-string.cpp
index b0c124150f20a..a7c920409fac7 100644
--- a/clang/test/CIR/CodeGen/wide-string.cpp
+++ b/clang/test/CIR/CodeGen/wide-string.cpp
@@ -66,3 +66,11 @@ const char16_t *test_char16_typedef() {
// CIR: cir.global "private" constant cir_private dso_local @{{.+}} = #cir.const_array<[#cir.int<116> : !u16i, #cir.int<101> : !u16i, #cir.int<115> : !u16i, #cir.int<116> : !u16i], trailing_zeros> : !cir.array<!u16i x 5>
// LLVM: @{{.+}} = private constant [5 x i16] [i16 116, i16 101, i16 115, i16 116, i16 0]
// OGCG: @{{.+}} = private unnamed_addr constant [5 x i16] [i16 116, i16 101, i16 115, i16 116, i16 0]
+
+const wchar_t *test_wchar_high_bit() {
+ return L"\x80000000";
+}
+
+// CIR: cir.global "private" constant cir_private dso_local @{{.+}} = #cir.const_array<[#cir.int<-2147483648> : !s32i], trailing_zeros> : !cir.array<!s32i x 2>
+// LLVM: @{{.+}} = private constant [2 x i32] [i32 -2147483648, i32 0]
+// OGCG: @{{.+}} = private unnamed_addr constant [2 x i32] [i32 -2147483648, i32 0]
More information about the cfe-commits
mailing list