[llvm] Revert "[SPIR-V] Simplify and fix sign-extension bug in convertCharsToWord (#207769)" (PR #209162)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 05:34:05 PDT 2026


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/209162

This reverts commit 503f0ca19cfcc9c0c88d6918939a2c0038ad0498.

This is obviously incorrect on big endian architectures.

>From c1b93e71f8ddc73fdfa30431664daa2c343a1452 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 13 Jul 2026 14:33:14 +0200
Subject: [PATCH] Revert "[SPIR-V] Simplify and fix sign-extension bug in
 convertCharsToWord (#207769)"

This reverts commit 503f0ca19cfcc9c0c88d6918939a2c0038ad0498.

This is obviously incorrect on big endian architectures.
---
 llvm/lib/Target/SPIRV/SPIRVUtils.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index 748783490ef64..c6f53609a2b3d 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -27,7 +27,6 @@
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/IntrinsicsSPIRV.h"
 #include "llvm/Support/MathExtras.h"
-#include <cstring>
 #include <queue>
 #include <vector>
 
@@ -147,9 +146,15 @@ StringRef getOriginalAsmConstraints(const CallBase &CB) {
 // when making string comparisons in compiler passes.
 // SPIR-V requires null-terminated UTF-8 strings padded to 32-bit alignment.
 static uint32_t convertCharsToWord(StringRef Str, unsigned i) {
-  uint32_t Word = 0u; // Padding/null bytes are zero-initialized.
-  unsigned Count = std::min(static_cast<size_t>(4), Str.size() - i);
-  std::memcpy(&Word, Str.data() + i, Count);
+  uint32_t Word = 0u; // Build up this 32-bit word from 4 8-bit chars.
+  for (unsigned WordIndex = 0; WordIndex < 4; ++WordIndex) {
+    unsigned StrIndex = i + WordIndex;
+    uint8_t CharToAdd = 0;       // Initilize char as padding/null.
+    if (StrIndex < Str.size()) { // If it's within the string, get a real char.
+      CharToAdd = Str[StrIndex];
+    }
+    Word |= (CharToAdd << (WordIndex * 8));
+  }
   return Word;
 }
 



More information about the llvm-commits mailing list