[llvm] 503f0ca - [SPIR-V] Simplify and fix sign-extension bug in convertCharsToWord (#207769)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 7 01:54:23 PDT 2026


Author: Arseniy Obolenskiy
Date: 2026-07-07T10:54:19+02:00
New Revision: 503f0ca19cfcc9c0c88d6918939a2c0038ad0498

URL: https://github.com/llvm/llvm-project/commit/503f0ca19cfcc9c0c88d6918939a2c0038ad0498
DIFF: https://github.com/llvm/llvm-project/commit/503f0ca19cfcc9c0c88d6918939a2c0038ad0498.diff

LOG: [SPIR-V] Simplify and fix sign-extension bug in convertCharsToWord (#207769)

Added: 
    

Modified: 
    llvm/lib/Target/SPIRV/SPIRVUtils.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index ecb095b5f09cf..6fcab5db1e745 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -27,6 +27,7 @@
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/IntrinsicsSPIRV.h"
 #include "llvm/Support/MathExtras.h"
+#include <cstring>
 #include <queue>
 #include <vector>
 
@@ -154,15 +155,9 @@ 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(const StringRef &Str, unsigned i) {
-  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));
-  }
+  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);
   return Word;
 }
 


        


More information about the llvm-commits mailing list