microsoft / DirectXShaderCompiler

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Other
3.11k stars 696 forks source link

HLSL loop optimization results in an unsupported i33 type #5059

Open sooknarine opened 1 year ago

sooknarine commented 1 year ago

Compiling i33.hlsl.txt with -T lib_6_3 i33.hlsl -Fc i33.dxil.txt shows a zext of i32 to i33. The DXIL spec only mentions support for integer types “i1, i8, i16, i32, i64”.

Relevant snippet from i33.dxil.txt:

  %8 = zext i32 %7 to i33
  %9 = add i32 %4, -2
  %10 = zext i32 %9 to i33
  %11 = mul i33 %8, %10
  %12 = lshr i33 %11, 1
  %13 = trunc i33 %12 to i32

Tested on December 2022 and October 2020 releases.

The i33 type is added by the Scalar Evolution pass - this is a standard LLVM pass that is used to optimize loop control expressions. Where possible it replaces the loop control expression with a constant or a simpler expression which replaces the loop, or at least reduces the number of iterations. The expression used is ((input - 1) * (input - 2)) / 2 and the extension to i33 is to allow for the possibility of the multiplication resulting in overflow - the division by 2 guarantees the final result fits in i32.

pow2clk commented 1 year ago

Here's a compiler explorer link for the described behavior: https://godbolt.org/z/x5aP3b41T

damyanp commented 2 months ago

This no longer repros on recent compilers.

damyanp commented 2 months ago

Actually the previous godbolt link was bad. This one shows a repro: https://godbolt.org/z/x8er4WYeE