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

[X86] tryCompressVPMOVPattern folds to VEX instruction using extended registers (ymm16-31)

Open abadams opened this issue 7 months ago • 4 comments

PR #175219 added tryCompressVPMovPattern, which replaces EVEX vpmovb2m+kmovd with VEX vpmovmskb, but VEX can't access ymm16-31. No register class check is performed, so if the original used an extended register, bad things happen.

This is causes miscompilations (incorrect output) in a complex test in a Halide PR (https://github.com/halide/Halide/pull/8925). It only causes failures in complex tests because simple ones are unlikely to ever use ymm16-31. Claude provided me with the MIR repro below, which is hopefully useful. I don't speak MIR myself so I can't vouch for it. I can provide the much longer piece of breaking .ll if necessary.

  # RUN: llc -mtriple=x86_64-- -mcpu=skylake-avx512 -run-pass=x86-compress-evex %s -o /dev/null -verify-machineinstrs                                                                        
  --- |                                                                                                   
    define void @test(ptr %p) { ret void }                  
  ...                                                                                                     
  ---
  name: test                                                                                              
  tracksRegLiveness: true                                   
  body: |                
    bb.0:
      liveins: $ymm23, $rdi
      $k3 = VPMOVB2MZ256kr killed $ymm23
      $edx = KMOVDrk killed $k3         
      MOV32mr $rdi, 1, $noreg, 0, $noreg, killed $edx                                                     
      RET 0                                          
  ...                                                    

abadams avatar Apr 09 '26 21:04 abadams

@llvm/issue-subscribers-backend-x86

Author: Andrew Adams (abadams)

PR #175219 added tryCompressVPMovPattern, which replaces EVEX vpmovb2m+kmovd with VEX vpmovmskb, but VEX can't access ymm16-31. No register class check is performed, so if the original used an extended register, bad things happen.

This is causes miscompilations (incorrect output) in a complex test in a Halide PR (https://github.com/halide/Halide/pull/8925). It only causes failures in complex tests because simple ones are unlikely to ever use ymm16-31. Claude provided me with the MIR repro below, which is hopefully useful. I don't speak MIR myself so I can't vouch for it. I can provide the much longer piece of breaking .ll if necessary.

  # RUN: llc -mtriple=x86_64-- -mcpu=skylake-avx512 -run-pass=x86-compress-evex %s -o /dev/null -verify-machineinstrs                                                                        
  --- |                                                                                                   
    define void @<!-- -->test(ptr %p) { ret void }                  
  ...                                                                                                     
  ---
  name: test                                                                                              
  tracksRegLiveness: true                                   
  body: |                
    bb.0:
      liveins: $ymm23, $rdi
      $k3 = VPMOVB2MZ256kr killed $ymm23
      $edx = KMOVDrk killed $k3         
      MOV32mr $rdi, 1, $noreg, 0, $noreg, killed $edx                                                     
      RET 0                                          
  ...                                                    

llvmbot avatar Apr 09 '26 22:04 llvmbot

CC @realqhc

RKSimon avatar Apr 12 '26 12:04 RKSimon

I'll take a look, thanks for the report.

realqhc avatar Apr 13 '26 05:04 realqhc

I have created a pull request to fix this miscompilation, thanks again for catching this.

realqhc avatar Apr 13 '26 05:04 realqhc