llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Shreesh Adiga (tantei3)

<details>
<summary>Changes</summary>

Move the reflected CRC32 calculation used by the x86 CRC builtins into 
llvm::calculateReflectedCRC32() and reuse it from both the AST interpreter and 
constant expression evaluator.

This removes duplicated CRC32 implementation logic from Clang. This is planned 
to be used for implementing const folding CRC instructions for x86 and AArch64 
in the future in LLVM.

Based on suggestion in 
https://github.com/llvm/llvm-project/pull/219452#discussion_r4070846498

---
Full diff: https://github.com/llvm/llvm-project/pull/225774.diff


3 Files Affected:

- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+3-9) 
- (modified) clang/lib/AST/ExprConstant.cpp (+3-9) 
- (modified) llvm/include/llvm/Support/CRC.h (+20) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 843850af334a2..58e407f154e8d 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -21,6 +21,7 @@
 #include "clang/Basic/TargetInfo.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/AllocToken.h"
+#include "llvm/Support/CRC.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/SipHash.h"
 
@@ -819,15 +820,8 @@ static bool interp__builtin_ia32_crc32(InterpState &S, 
CodePtr OpPC,
   // CRC32C polynomial (iSCSI polynomial, bit-reversed)
   static const uint32_t CRC32C_POLY = 0x82F63B78;
 
-  // Process each byte
-  uint32_t Result = static_cast<uint32_t>(CRCVal);
-  for (unsigned I = 0; I != DataBytes; ++I) {
-    uint8_t Byte = static_cast<uint8_t>((DataVal >> (I * 8)) & 0xFF);
-    Result ^= Byte;
-    for (int J = 0; J != 8; ++J) {
-      Result = (Result >> 1) ^ ((Result & 1) ? CRC32C_POLY : 0);
-    }
-  }
+  uint32_t Result = llvm::calculateReflectedCRC32(
+      static_cast<uint32_t>(CRCVal), DataVal, DataBytes, CRC32C_POLY);
 
   pushInteger(S, Result, Call->getType());
   return true;
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index bb7721df5a28b..978f2b2954414 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -60,6 +60,7 @@
 #include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/SmallBitVector.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/CRC.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/SaveAndRestore.h"
@@ -17010,15 +17011,8 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
     // CRC32C polynomial (iSCSI polynomial, bit-reversed)
     static const uint32_t CRC32C_POLY = 0x82F63B78;
 
-    // Process each byte
-    uint32_t Result = static_cast<uint32_t>(CRCVal);
-    for (unsigned I = 0; I != DataBytes; ++I) {
-      uint8_t Byte = static_cast<uint8_t>((DataVal >> (I * 8)) & 0xFF);
-      Result ^= Byte;
-      for (int J = 0; J != 8; ++J) {
-        Result = (Result >> 1) ^ ((Result & 1) ? CRC32C_POLY : 0);
-      }
-    }
+    uint32_t Result = llvm::calculateReflectedCRC32(
+        static_cast<uint32_t>(CRCVal), DataVal, DataBytes, CRC32C_POLY);
 
     return Success(Result, E);
   };
diff --git a/llvm/include/llvm/Support/CRC.h b/llvm/include/llvm/Support/CRC.h
index a07b017d23519..39c03e2e69c03 100644
--- a/llvm/include/llvm/Support/CRC.h
+++ b/llvm/include/llvm/Support/CRC.h
@@ -26,6 +26,26 @@ LLVM_ABI uint32_t crc32(ArrayRef<uint8_t> Data);
 // checksum.
 LLVM_ABI uint32_t crc32(uint32_t CRC, ArrayRef<uint8_t> Data);
 
+// Calculate bit-reflected CRC for given initial CRC, Data and Polynomial.
+// It processes the lower DataBytes of Data from LSB onwards.
+// DataBytes must be 1/2/4/8.
+// Poly must already be in the bit-reflected form.
+constexpr inline uint32_t calculateReflectedCRC32(uint32_t Crc, uint64_t Data,
+                                                  unsigned DataBytes,
+                                                  uint32_t Poly) {
+  uint32_t Result = Crc;
+  // Process each byte
+  for (unsigned I = 0; I != DataBytes; ++I) {
+    uint8_t Byte = static_cast<uint8_t>((Data >> (I * 8)) & 0xFF);
+    Result ^= Byte;
+    for (int J = 0; J != 8; ++J) {
+      Result = (Result >> 1) ^ ((Result & 1) ? Poly : 0);
+    }
+  }
+
+  return Result;
+}
+
 // Class for computing the JamCRC.
 //
 // We will use the "Rocksoft^tm Model CRC Algorithm" to describe the properties

``````````

</details>


https://github.com/llvm/llvm-project/pull/225774
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to