Issue 237989
Summary [IR] Reduce constant-folding overhead for vector insertions
Labels new issue
Assignees
Reporter ParkHanbum
    `ConstantFoldInsertElementInstruction` rebuilds a constant vector by copying every lane other than the inserted one. Each copy currently obtains an index constant and enters the general extraction folder, even when the existing lane is directly accessible. Trying `getAggregateElement(unsigned)` first avoids that repeated work while retaining the original extraction fallback for symbolic vectors. Folding results are preserved; the O(N²) structure of a full insertion chain remains.

A real example occurs in ONNX Runtime’s [`sqnbitgemm_kernel_avx2.cpp`](https://github.com/microsoft/onnxruntime/blob/402979ac1bef89dcd55add41eaa7277c407599eb/onnxruntime/core/mlas/lib/sqnbitgemm_kernel_avx2.cpp#L138). Its scalar broadcasts lower through Clang’s intrinsic helpers into 16-insertion `<16 x i8>` and `<16 x i16>` chains, and a 32-insertion `<32 x i8>` chain. These already optimize to the correct constants or broadcasts; the opportunity is to reduce compiler work while reaching those results.

An untimed O3 profile of this actual translation unit recorded **2,360 insertion-folder calls and 57,168 copied lanes**. Every copied lane supported direct aggregate access. The baseline and candidate produced identical optimized IR. These are module-wide counts, not attribution of every call to one broadcast chain.

Local optimized, assertions-enabled arm64 measurements used 12 alternating before/after pairs:

| Measurement | Median paired CPU-time change |
|---|---:|
| Warmed 16-lane i32 insertion-chain API benchmark | **−68.4%** |
| Warmed 64-lane i32 insertion-chain API benchmark | **−76.3%** |
| InstSimplify on 32,768 constant insert instructions | **−36.4%** |
| O3 aggregate across 13 ONNX Runtime/LLVM/llama.cpp translation units | +0.13% (95% interval: −0.58% to +0.54%) |
| Fallback-only 64-lane vector-GEP microbenchmark | +3.5% |

The large improvements are from amplified path benchmarks. They do not establish an ONNX Runtime build-time improvement or a general O3 speedup. The fallback-only result shows the cost of an unsuccessful direct query before the existing extraction path.

Correctness checks found:

- **35,535 differential cases with identical results**, covering poison/undef, integer/FP/byte/pointer vectors, unusual and out-of-range indices, scalable vectors, vector GEPs and opaque bitcasts.
- **Identical Assembler and InstSimplify test statuses:** 814 pass, 17 unsupported and three existing failures with identical optimizer output.
- **Identical O3 output IR** for the actual sqnbitgemm module and all 13 general benchmark inputs.

The comparison used LLVM `5f47feeedda059f88c30ca6f11109faa5ce5428c` with and without the accessor change. Both variants were freshly compiled against the same existing libraries, with unchanged base InlineCost code. This controlled relink isolates the ConstantFold change; it was not a clean rebuild of the complete LLVM tree.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to