microsoft / microsoft/DirectXShaderCompiler

[SPIR-V] Intrinsics which are Termination Instructions fail Legalizations

Open
#7,279 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug spirv
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

Description

I added OpTerminateRayKHR with an inline SPIR-V intrinsic and put it in an if branch in place of AcceptHitAndEndSearch().

The shader fails to compile.

Steps to Reproduce

https://godbolt.org/z/sKWY1Kofa

[[vk::ext_capability(/*spv::CapabilityRayTracingKHR*/4479)]]
[[vk::ext_extension("SPV_KHR_ray_tracing")]]
[[vk::ext_instruction(/*spv::OpTerminateRayKHR*/4449)]]
void terminateRay();

struct [raypayload] OcclusionPayload
{
    float32_t attenuation : read(caller,anyhit,miss) : write(caller,anyhit,miss);
};

[shader("anyhit")]
void main(inout OcclusionPayload payload, in BuiltInTriangleIntersectionAttributes attribs)
{
    if (payload.attenuation<0.001)
        terminateRay();
    IgnoreHit();
}

Actual Behavior

Even with the -Vd option I see

fatal error: failed to optimize SPIR-V: terminator instruction outside basic block
note: please file a bug report on https://github.com/Microsoft/DirectXShaderCompiler/issues with source code if possible

Compiler returned: 5

Also -O0 -Vd doin't let me see the SPIR-V that DXC tries to emit in this case.

Looking at the regular codegen, I can see that DXC does something really weird to my payload, even though I access it only to read it, it OpStores it back at the end of each branch. This may be dislodging the termination instruction.

Environment

  • DXC version: Godbolt trunk
  • Host Operating System : Linux

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.

Research direction

Start with the Godbolt reproduction using the inline SPIR-V OpTerminateRayKHR intrinsic in the anyhit shader and compare behavior with -Vd and -O0. Trace the SPIR-V legalization and regular codegen around the termination instruction and payload stores; done means the shader compiles without the terminator-outside-basic-block failure and emits valid SPIR-V.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.