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

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

Open kmpeng opened this issue 3 months ago • 1 comments

Follow-up to #128043.

Following the normalize implementation move, SPIR-V now lowers normalize as a sequence of dot/rsqrt/insertelement/shufflevector/fmul instructions instead of the GL extension:

normalize(x) ->
  d     = dot x, x
  r     = rsqrt d
  splat = shufflevector(insertelement(poison, r, 0), poison, zeroinitializer)
  fmul x, splat

We can pattern match this in SPIRVCombine.td and SPIRVPreLegalizerCombiner.cpp to

fmul(
    x,
    shufflevector(
        insertelement(poison,
                      rsqrt(dot(x, x)),
                      0),
        poison,
        zeroinitializer))

which would allow us to change this sequence back into normalize(x) so that SPIR-V emits the GL extension again.

kmpeng avatar Aug 07 '26 23:08 kmpeng

Going to take a different approach to emit the GL extension since pattern matching normalize is quite complicated.

Update: still going to go with pattern matching, but it will be done in #128043 and not as a follow-up.

kmpeng avatar Aug 12 '26 02:08 kmpeng