centralize CPU feature detection

From: John Naylor <johncnaylorls(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: root <tenistarkim(at)gmail(dot)com>, Nathan Bossart <nathandbossart(at)gmail(dot)com>
Subject: centralize CPU feature detection
Date: 2026-02-15 13:04:01
Message-ID: CANWCAZbgEUFw7LuYSVeJ=Tj98R5HoOB1Ffeqk3aLvbw5rU5NTw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

We have accrued duplicate bits of hardware detection logic in
different places, and the the AVX2 page checksum patch is about to add
more. It seems like a good time to try again to centralize things,
before that happens. The attached only touches x86, but that's enough
to demonstrate, and there's no point in trying to do everything at
once. Arm should get the same treatment at some point.

0001 starts by renaming pg_crc32c_sse42_choose.c to something more
general and does just enough to fix the build. Without a separate
rename step, there's too much change for git to call it a rename.
Humans can still see some carryover, so it seems right to keep git
history continuous.

0002 adds an array of bool indexed by an enum of feature names, and
adjusts the CRC and popcount code to use it.

0003 refactors detection of support for ZMM registers in preparation
of doing same for YMM (needed by AVX2).

0004 rebases the latest page checksum patch on top of the above for
demonstration (review of that is happening in its own thread [1]). Not
counting autoconf/meson and the pointer juggling, the additional
feature detection is now only 2 lines of code, which is nice.

For PG20, we can build on this to simplify the rat's nest of #ifdefs
that a couple of src/include/port headers have. We should also be able
to arrange so that packagers that pass relevant flags to common
compilers will automatically get some branches/indirection eliminated
via the compiler's standard dead code elimination, in a simple way,
rather than our having to kluge it together in multiple places. That
will make irrelevant the question that occasionally come up about
moving hardware requirements. Anyway, 0001-3 is doable for PG19.

[1] https://postgr.es/m/CA%2BvA85_5GTu%2BHHniSbvvP%2B8k3%3DxZO%3DWE84NPwiKyxztqvpfZ3Q%40mail.gmail.com

--
John Naylor
Amazon Web Services

Attachment Content-Type Size
v1-0001-Rename-CRC-choose-files-for-future-general-purpos.patch application/x-patch 3.1 KB
v1-0003-Refactor-the-detection-of-ZMM-registers.patch application/x-patch 2.5 KB
v1-0002-Centralize-detection-of-CPU-features.patch application/x-patch 12.8 KB
v1-0004-Enable-autovectorizing-page-checksums-with-AVX2-w.patch application/x-patch 12.6 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Junwang Zhao 2026-02-15 13:38:42 Re: Small improvements to substring()
Previous Message Rahila Syed 2026-02-15 12:40:06 Re: Enhancing Memory Context Statistics Reporting