llvm / llvm/llvm-project

[SPIRV] Add PreLegalizer pattern matching for `cross` GL extension

Open
#214,879 0 comments 0 reactions 0 assignees View on GitHub
backend:SPIR-V
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

> Note: the `cross` move deleted the SPIR-V intrinsic, so `Intrinsic::spv_cross`, its `selectExtInst` lowering, and the SPIR-V `cross.ll` test need to be restored first so the PreLegalizer combiner can emit the GL extension through the intrinsic.

Follow-up to #135425.

Following the cross implementation move, SPIR-V now lowers `cross` as a sequence of `extractelement`/`fmul`/`fsub`/`insertelement` instructions instead of the GL extension:
```asm
cross(x, y) ->
x0 = extractelement x, 0
x1 = extractelement x, 1
x2 = extractelement x, 2
y0 = extractelement y, 0
y1 = extractelement y, 1
y2 = extractelement y, 2
sub0 = fsub(fmul(x1, y2), fmul(y1, x2))
vec0 = insertelement(poison, sub0, 0)
sub1 = fsub(fmul(x2, y0), fmul(y2, x0))
vec1 = insertelement(vec0, sub1, 1)
sub2 = fsub(fmul(x0, y1), fmul(y0, x1))
vec2 = insertelement(vec1, sub2, 2)
```
We can pattern match this in `SPIRVCombine.td` and `SPIRVCombinerHelper.cpp` to
```llvm
insertelement(
insertelement(
insertelement(
poison,
fsub(
fmul(extractelement(x, 1), extractelement(y, 2)),
fmul(extractelement(y, 1), extractelement(x, 2))),
0),
fsub(
fmul(extractelement(x, 2), extractelement(y, 0)),
fmul(extractelement(y, 2), extractelement(x, 0))),
1),
fsub(
fmul(extractelement(x, 0), extractelement(y, 1)),
fmul(extractelement(y, 0), extractelement(x, 1))),
2)
```
which would allow us to change this sequence back into `cross(x, y)` so that SPIR-V emits the GL extension again.

Contributor guide

Open the contributing guide

Research direction

Start with the deleted SPIR-V intrinsic, its selectExtInst lowering, and the SPIR-V cross.ll test, then read SPIRVCombine.td and SPIRVCombinerHelper.cpp. Verify the PreLegalizer combiner recognizes the shown extractelement/fmul/fsub/insertelement sequence, restores cross(x, y), and makes the SPIR-V test cover GL-extension emission.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.