[clang] [llvm] [llvm:ir] Add support for constant data exceeding 4GiB (PR #126481)

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 10 18:02:42 PST 2025


================
@@ -1583,7 +1583,7 @@ static void printConstant(const Constant *COp, unsigned BitWidth,
     bool IsInteger = EltTy->isIntegerTy();
     bool IsFP = EltTy->isHalfTy() || EltTy->isFloatTy() || EltTy->isDoubleTy();
     unsigned EltBits = EltTy->getPrimitiveSizeInBits();
-    unsigned E = std::min(BitWidth / EltBits, CDS->getNumElements());
+    unsigned E = std::min(BitWidth / EltBits, (unsigned)CDS->getNumElements());
----------------
pzzp wrote:

We tested, actually it work well on our llvm-15 based TVM based for over half a year...
The function `printConstant` is only invoked for constants retrieved from the constant pool, which  is unlikely to be a large constant.
The purpose of our patch is to enable emitting constant data exceeding 4GB, hence we avoid to do extensive modifications like replace unsigned`  with `uint64_t` across the codebase.

This script can generate a test.
```python
print(
'''; ModuleID = 'large_string.c'
source_filename = "large_string.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
'''
)


print('@.str = private unnamed_addr constant [4294968320 x i8] c"', end='')
for i in range(4294968320//1024):
    print('abcdefgh' * 128, end='')
print('", align 1')

print(
'''@s = dso_local local_unnamed_addr global ptr @.str, align 8

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 21.0.0git (https://github.com/llvm/llvm-project.git 0395fd9680a348c6afc52a165453222c02a3cd48)"}
''')
```

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


More information about the cfe-commits mailing list