Skip to content

DebugPrintf prevents panics' infinite loops from being (unsoundly) optimized away. #1048

Closed
@charles-r-earp

Description

@charles-r-earp

#768

See https://github.com/charles-r-earp/rust-gpu/tree/debug_printf_panic.

A minimal example that prints and panics.

#![no_std]
#![feature(asm_experimental_arch)]

use spirv_std::macros::spirv;

#[spirv(compute(threads(1)))]
pub fn main() {
    unsafe {
        spirv_std::macros::debug_printfln!();
    }
    // panic becomes infinite loop, normally optimized away
    // but with debug_printf it is not.
    panic!();
}
cargo run -p debug-printf-panic
// output
; SPIR-V
; Version: 1.5
; Generator: Google rspirv; 0
; Bound: 22
; Schema: 0
               OpCapability Shader
               OpCapability VulkanMemoryModel
               OpExtension "SPV_KHR_non_semantic_info"
          %1 = OpExtInstImport "NonSemantic.DebugPrintf"
               OpMemoryModel Logical Vulkan
               OpEntryPoint GLCompute %2 "main"
               OpExecutionMode %2 LocalSize 1 1 1

               ; Debug Information
          %3 = OpString "
"

               ; Types, variables and constants
       %void = OpTypeVoid
          %7 = OpTypeFunction %void

               ; Function 2
          %2 = OpFunction %void None %7
          %8 = OpLabel
         %17 = OpExtInst %void %1 1 %3
               OpBranch %18
         %18 = OpLabel
               OpLoopMerge %21 %18 None
               OpBranch %18
         %21 = OpLabel
               OpUnreachable
               OpFunctionEnd

#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

void main()
{
    // unimplemented ext op 12
    for (;;)
    {
    }
}

This causes a crash or hang when running the shader.

Metadata

Metadata

Assignees

No one assigned

    Labels

    t: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions