microsoft / microsoft/DirectXShaderCompiler

[DXIL] Illegal code motion for CalculateLevelOfDetailUnclamped

Open
#4,858 3 comments 0 reactions 1 assignee View on GitHub

@tex3d is already working on this.

Since Sep 26, 2024.

bug check-in-clang correctness
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

Given this code:

Texture2D<float> T;
RWTexture2D<uint> A;
SamplerState S;

float4 main(float2 uv : UV) : SV_Target
{
	float l = T.CalculateLevelOfDetailUnclamped(S, uv);
	if (all(uv < 0.5))
	{
		uint il = uint(max(l, 0.0) * 32);
		uint o;
		InterlockedMin(A[int2(uv)], il, o);
	}

	return T.Sample(S, uv);
}

with dxc test.frag -Tps_6_0 this compiles to:

define void @main() {
  %6 = fcmp fast olt float %4, 5.000000e-01
  %7 = fcmp fast olt float %5, 5.000000e-01
  %8 = and i1 %6, %7
  br i1 %8, label %9, label %17

; <label>:9                                       ; preds = %0
  %10 = call float @dx.op.calculateLOD.f32(i32 81, %dx.types.Handle %2, %dx.types.Handle %3, float %4, float %5, float undef, i1 false)  ; CalculateLOD(handle,sampler,coord0,coord1,coord2,clamped)
  %11 = call float @dx.op.binary.f32(i32 35, float %10, float 0.000000e+00)  ; FMax(a,b)
  %12 = fmul fast float %11, 3.200000e+01
  %13 = fptoui float %12 to i32
  %14 = fptosi float %4 to i32
  %15 = fptosi float %5 to i32
  %16 = call i32 @dx.op.atomicBinOp.i32(i32 78, %dx.types.Handle %1, i32 6, i32 %14, i32 %15, i32 undef, i32 %13)  ; AtomicBinOp(handle,atomicOp,offset0,offset1,offset2,newValue)
  br label %17
}

The calculateLOD is now sunk to non-uniform control flow, which is undefined.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.