[llvm] [X86] Implement CRC32 const folding (PR #219452)
Shreesh Adiga via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 22 04:03:48 PDT 2026
================
@@ -2260,6 +2260,29 @@ X86TTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
}
break;
+ case Intrinsic::x86_sse42_crc32_32_8:
+ case Intrinsic::x86_sse42_crc32_32_16:
+ case Intrinsic::x86_sse42_crc32_32_32:
+ case Intrinsic::x86_sse42_crc32_64_64: {
+ auto *CrcArg = dyn_cast<ConstantInt>(II.getArgOperand(0));
+ auto *DataArg = dyn_cast<ConstantInt>(II.getArgOperand(1));
+ if (!CrcArg || !DataArg)
+ break;
+
+ // If both operands are constant, we can completely constant fold this.
+ uint64_t Crc = CrcArg->getZExtValue() & 0xffffffff;
+ uint64_t Data = DataArg->getZExtValue();
+ unsigned BitWidth = DataArg->getBitWidth();
+ Crc ^= Data;
+ // CRC32C polynomial (iSCSI polynomial, bit-reversed)
+ const uint32_t Poly = 0x82F63B78;
+ for (unsigned Bit = 0; Bit != BitWidth; ++Bit) {
+ Crc = (Crc >> 1) ^ ((Crc & 1) ? Poly : 0);
+ }
----------------
tantei3 wrote:
Sure if you'd like to deduplicate the calculation and have a single library function for the constexpr and const folding that sounds good, I can attempt the refactoring with your guidance.
For GCC, I had submitted https://patchwork.sourceware.org/project/gcc/patch/20260901055821.370641-1-16567adigashreesh@gmail.com/ which is the library function to compute CRC and then submitted x86 const folding: https://patchwork.sourceware.org/project/gcc/patch/20260915135300.1733418-1-16567adigashreesh@gmail.com/ and aarch64 https://patchwork.sourceware.org/project/gcc/patch/20260915135300.1733418-2-16567adigashreesh@gmail.com/
Could you share references around which file to place the function in LLVM and whether the GCC approach would be good?
https://github.com/llvm/llvm-project/pull/219452
More information about the llvm-commits
mailing list