llvm / llvm/llvm-project

x86 backend issue in AVX-512 code when generating constant -1 in zmm reg

Open
#202,086 5 comments 0 reactions 0 assignees View on GitHub
backend:X86
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Observed in Clang 22.1.0, but I can reproduce this as far back as Clang 4.0, so it's been there forever.

Small repro on Compiler Explorer: https://godbolt.org/z/anceexcT7

The `set1_epi32(-1)` turns into `vpternlogd zmm1, zmm1, zmm1, 255`, presumably adapted from the older "ones' idiom" using `pcmpeq* reg, reg`. The issue being that the `pcmpeq` version is a dependency-breaking idiom on Intel/AMD cores, but the `vpternlogd` version is not, at least not on the Zen 4 machine I tested this on.

The original case I encountered had this `vpternlogd` inside a hot loop where the iterations are supposed to be independent, and the `vpternlogd` introduced a loop-carried dependency that decreased throughput by about 34%.

This seems iffy to do in general and should probably just use a constant load or similar on AVX-512 targets.

Contributor guide

Open the contributing guide

Research direction

Start by running the linked Compiler Explorer repro and comparing the AVX-512 code generated for set1_epi32(-1), especially the vpternlogd instruction. Trace the x86 backend's constant-generation decision and verify that the completed change removes the unwanted dependency without regressing the generated code.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.