[llvm] Fix i1 array global crash in NVPTXAsmPrinter. (PR #92506)

Johannes Reifferscheid via llvm-commits llvm-commits at lists.llvm.org
Fri May 17 00:24:33 PDT 2024


https://github.com/jreiffers created https://github.com/llvm/llvm-project/pull/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"
```

>From b25de44cfd7a10984236e3d1e3769e6348b520bc Mon Sep 17 00:00:00 2001
From: Johannes Reifferscheid <jreiffers at google.com>
Date: Fri, 17 May 2024 09:21:15 +0200
Subject: [PATCH] Fix i1 array global crash in NVPTXAsmPrinter.

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"
```
---
 llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp  |  6 +++++-
 llvm/test/CodeGen/NVPTX/i1-array-global.ll | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/NVPTX/i1-array-global.ll

diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index 9f31b72bbceb1..f8b246a1a36cd 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -1847,9 +1847,13 @@ void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
   auto AddIntToBuffer = [AggBuffer, Bytes](const APInt &Val) {
     size_t NumBytes = (Val.getBitWidth() + 7) / 8;
     SmallVector<unsigned char, 16> Buf(NumBytes);
-    for (unsigned I = 0; I < NumBytes; ++I) {
+    for (unsigned I = 0; I < NumBytes - 1; ++I) {
       Buf[I] = Val.extractBitsAsZExtValue(8, I * 8);
     }
+    size_t LastBytePosition = (NumBytes - 1) * 8;
+    size_t LastByteBits = Val.getBitWidth() - LastBytePosition;
+    Buf[NumBytes - 1] =
+        Val.extractBitsAsZExtValue(LastByteBits, LastBytePosition);
     AggBuffer->addBytes(Buf.data(), NumBytes, Bytes);
   };
 
diff --git a/llvm/test/CodeGen/NVPTX/i1-array-global.ll b/llvm/test/CodeGen/NVPTX/i1-array-global.ll
new file mode 100644
index 0000000000000..a177f75a18dd6
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/i1-array-global.ll
@@ -0,0 +1,19 @@
+; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s
+; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_20 | %ptxas-verify %}
+
+target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64"
+target triple = "nvptx-nvidia-cuda"
+
+ at global_cst = private constant [6 x i1] [i1 true, i1 false, i1 true, i1 false, i1 true, i1 false]
+
+; CHECK: .global .align 1 .b8 global_cst[6] = {1, 0, 1, 0, 1}
+define void @kernel(i32 %i, ptr %out) {
+  %5 = getelementptr inbounds i1, ptr @global_cst, i32 %i
+  %6 = load i1, ptr %5, align 1
+  store i1 %6, ptr %out, align 1
+  ret void
+}
+
+!nvvm.annotations = !{!0}
+!0 = !{ptr @kernel, !"kernel", i32 1}
+



More information about the llvm-commits mailing list