Skip to content

Commit 698cf01

Browse files
authored
Fix i1 array global crash in NVPTXAsmPrinter. (#92506)
See the test file. At head, this crashes with ``` assertion failed at llvm/lib/Support/APInt.cpp:492 in uint64_t llvm::APInt::extractBitsAsZExtValue(unsigned int, unsigned int) const: bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth && "Illegal bit extraction" ```
1 parent d0e2808 commit 698cf01

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,9 +1847,13 @@ void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
18471847
auto AddIntToBuffer = [AggBuffer, Bytes](const APInt &Val) {
18481848
size_t NumBytes = (Val.getBitWidth() + 7) / 8;
18491849
SmallVector<unsigned char, 16> Buf(NumBytes);
1850-
for (unsigned I = 0; I < NumBytes; ++I) {
1850+
for (unsigned I = 0; I < NumBytes - 1; ++I) {
18511851
Buf[I] = Val.extractBitsAsZExtValue(8, I * 8);
18521852
}
1853+
size_t LastBytePosition = (NumBytes - 1) * 8;
1854+
size_t LastByteBits = Val.getBitWidth() - LastBytePosition;
1855+
Buf[NumBytes - 1] =
1856+
Val.extractBitsAsZExtValue(LastByteBits, LastBytePosition);
18531857
AggBuffer->addBytes(Buf.data(), NumBytes, Bytes);
18541858
};
18551859

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s
2+
; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_20 | %ptxas-verify %}
3+
4+
target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64"
5+
target triple = "nvptx-nvidia-cuda"
6+
7+
@global_cst = private constant [6 x i1] [i1 true, i1 false, i1 true, i1 false, i1 true, i1 false]
8+
9+
; CHECK: .global .align 1 .b8 global_cst[6] = {1, 0, 1, 0, 1}
10+
define void @kernel(i32 %i, ptr %out) {
11+
%5 = getelementptr inbounds i1, ptr @global_cst, i32 %i
12+
%6 = load i1, ptr %5, align 1
13+
store i1 %6, ptr %out, align 1
14+
ret void
15+
}
16+
17+
!nvvm.annotations = !{!0}
18+
!0 = !{ptr @kernel, !"kernel", i32 1}
19+

0 commit comments

Comments
 (0)