[llvm] [Support] Add assert for DataBytes in calculateReflectedCRC32 (PR #228688)

Shreesh Adiga via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 03:40:50 PDT 2026


================
@@ -33,6 +34,9 @@ LLVM_ABI uint32_t crc32(uint32_t CRC, ArrayRef<uint8_t> Data);
 constexpr inline uint32_t calculateReflectedCRC32(uint32_t Crc, uint64_t Data,
                                                   unsigned DataBytes,
                                                   uint32_t Poly) {
+  assert(
----------------
tantei3 wrote:

Got a compiler error when using static_assert:
```
In file included from /home/shreesh/cpp/llvm-project/llvm/lib/Support/CRC.cpp:21:
/home/shreesh/cpp/llvm-project/llvm/include/llvm/Support/CRC.h: In function ‘constexpr uint32_t llvm::calculateReflectedCRC32(uint32_t, uint64_t, unsigned int, uint32_t)’:
/home/shreesh/cpp/llvm-project/llvm/include/llvm/Support/CRC.h:37:59: error: non-constant condition for static assertion
   37 |       (DataBytes == 1 || DataBytes == 2 || DataBytes == 4 || DataBytes == 8),
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/home/shreesh/cpp/llvm-project/llvm/include/llvm/Support/CRC.h:37:8: error: ‘DataBytes’ is not a constant expression
   37 |       (DataBytes == 1 || DataBytes == 2 || DataBytes == 4 || DataBytes == 8),
      |        ^~~~~~~~~
make[3]: *** [lib/Support/CMakeFiles/LLVMSupport.dir/build.make:527: lib/Support/CMakeFiles/LLVMSupport.dir/CRC.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:17725: lib/Support/CMakeFiles/LLVMSupport.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:16118: CMakeFiles/check-all.dir/rule] Error 2
make: *** [Makefile:215: check-all] Error 2
make: Leaving directory '/home/shreesh/cpp/llvm-project/build'
```

Usage in x86 const folding is passing non-constant expression as well. 

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


More information about the llvm-commits mailing list