llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[X86] Avoid repeated Chain user scans in combineLoad

Open Michael-Chen-NJU opened this issue 2 weeks ago • 4 comments

X86TargetLowering::combineLoad scans Chain->users() for each eligible vector load to find a matching X86ISD::SUBV_BROADCAST_LOAD. When no matching broadcast load exists for a pointer, the scan cannot succeed, but the shared chain is still traversed.

This was observed while compiling the SDXL UNet CPU test from IREE.

The attached IR was reduced from the captured attention function using llvm-reduce. It triggers over 1M Chain-user visits with no successful matches under diagnostic instrumentation, while compiling successfully with llc.

reproducer.ll

llc -O3 -mcpu=znver4 -filetype=obj reproducer.ll -o /tmp/reproducer.o

On the full LLVM IR generated from the IREE SDXL UNet compilation, this reduced llc -O3 compilation time from 13.11s to 10.59s (19.5%) across seven alternating A/B pairs.This is an exploratory prototype, not a fully validated fix.

Michael-Chen-NJU avatar Nov 20 '26 09:11 Michael-Chen-NJU

Hi @RKSimon, could you take a look at this issue and advise whether the repeated Chain->users() traversal is worth addressing? I'm happy to prepare a patch and a focused regression test if the direction looks reasonable.

Michael-Chen-NJU avatar Nov 20 '26 10:11 Michael-Chen-NJU

@llvm/issue-subscribers-backend-x86

Author: 陈子昂 (Michael-Chen-NJU)

`X86TargetLowering::combineLoad` scans `Chain->users()` for each eligible vector load to find a matching `X86ISD::SUBV_BROADCAST_LOAD.` When no matching broadcast load exists for a pointer, the scan cannot succeed, but the shared chain is still traversed.

This was observed while compiling the SDXL UNet CPU test from IREE.

The attached IR was reduced from the captured attention function using llvm-reduce. It triggers over 1M Chain-user visits with no successful matches under diagnostic instrumentation, while compiling successfully with llc.

reproducer.ll

llc -O3 -mcpu=znver4 -filetype=obj reproducer.ll -o /tmp/reproducer.o

On the full LLVM IR generated from the IREE SDXL UNet compilation, this reduced llc -O3 compilation time from 13.11s to 10.59s (19.5%) across seven alternating A/B pairs.This is an exploratory prototype, not a fully validated fix.

@Michael-Chen-NJU Yes, its probably worth improving the order of the checks to increase the likelihood of cheap&early failure

RKSimon avatar Nov 20 '26 16:11 RKSimon

@Michael-Chen-NJU Yes, its probably worth improving the order of the checks to increase the likelihood of cheap&early failure

Thanks. I tried reordering the checks first. While this makes rejection cheaper per user, it does not reduce the number of users visited. In the reduced reproducer, the loop still visits all 1,001,009 Chain users when no matching node exists.

The current patch instead adds a conservative flag to X86MachineFunctionInfo, set at both SUBV_BROADCAST_LOAD creation sites and never cleared. This allows combineLoad to skip the search entirely when no such node has been created in the function. A stale flag can only cause an unnecessary search, not a missed match. The original matching logic, candidate order and memory ordering remain unchanged.

Michael-Chen-NJU avatar Nov 22 '26 07:11 Michael-Chen-NJU