|  | 
|  | 1 | +# Dxil Debug Printf | 
|  | 2 | + | 
|  | 3 | +* Proposal: [NNNN](NNNN-debug-printf.md) | 
|  | 4 | +* Author(s): [Jiao Lu](https://github.com/jiaolu) | 
|  | 5 | +* Sponsor: TBD | 
|  | 6 | +* Status: **Under Consideration** | 
|  | 7 | + | 
|  | 8 | +## Introduction | 
|  | 9 | + | 
|  | 10 | +Add new dxil op to allow c/c++ like *printf* intrinsic instructions can be generated in the dxil sourced from hlsl. | 
|  | 11 | + | 
|  | 12 | +## Motivation | 
|  | 13 | + | 
|  | 14 | +As the new shader models, i.e. RayTracing, RayQuery, Workgraph, and more complex algorithms and structure emerges in recent time, we meet many more requirements of debugging capabilities of hlsl coding in game, application and driver development. | 
|  | 15 | + | 
|  | 16 | +The dxil counterpart spirv has a similar feature, [NonSemantic.DebugPrintf extention](https://github.com/KhronosGroup/SPIRV-Registry/blob/main/nonsemantic/NonSemantic.DebugPrintf.asciidoc) to generate DebugPrintf spirvOp souced from hlsl/glsl. Based on the spirvOp extention, some vendor drivers and Valve lunarG has [debug printf layer](https://github.com/KhronosGroup/Vulkan-ValidationLayers/blob/main/docs/debug_printf.md) to dump printf expression, "hlsl/glsl variables" into stdio or file. | 
|  | 17 | + | 
|  | 18 | +## Proposed solution | 
|  | 19 | + | 
|  | 20 | +The printf expression in hlsl mostly like this example. | 
|  | 21 | +```c++ hlsl: | 
|  | 22 | + | 
|  | 23 | +const string str0= "str0"; | 
|  | 24 | +string str1 = "str1"; | 
|  | 25 | + | 
|  | 26 | +void main() { | 
|  | 27 | +    printf(str0); | 
|  | 28 | +    printf(str1); | 
|  | 29 | +    printf("Variables are: %d %d %.2f", 1u, 2u, 1.5f); | 
|  | 30 | +} | 
|  | 31 | + | 
|  | 32 | +``` | 
|  | 33 | + | 
|  | 34 | + | 
|  | 35 | +DirectXCompiler at present can parse "printf" statement in hlsl as dx.hl.op instructions, | 
|  | 36 | +``` | 
|  | 37 | +dx.hl.op..void (i32, i8*, ...); | 
|  | 38 | +``` | 
|  | 39 | +The printf format string, the second argument of the printf instruction of dx.hl.op is a global *constant* variable or a GEP constant expression. | 
|  | 40 | + | 
|  | 41 | + The parsing is called from ast frontend into the HandleTranslationUnit-->TranslatePrintf.The function *TranslatePrintf* itself is empty implementation. | 
|  | 42 | + | 
|  | 43 | +The implementation of debug printf dxil op will be | 
|  | 44 | + | 
|  | 45 | +1) assign new dxil op code to the debug printf. | 
|  | 46 | +2) Finished the TranslatePrintf implementation, create dxil op instruction with a proper dxil op code, and replace dx.hl.op. the dxil op instruction function will be a variable arguments function. | 
|  | 47 | + | 
|  | 48 | +## Detailed design | 
|  | 49 | + | 
|  | 50 | +1. We should not support dynamic string variable, a string variable content retrieved from buffer. The string variable should be explicited defined and can be retrieved directly/indirectly from global constant variable. | 
|  | 51 | + | 
|  | 52 | +2. The format string input to the dx.hl.op..void, could be llvm constant expression, we need to retrieve global variable from the constant expression. | 
|  | 53 | + | 
|  | 54 | +3. The validation for the dxil overloading should be handled properly. Because of printf variable arguments, there is no definite function type can be validated. | 
|  | 55 | + | 
|  | 56 | + | 
0 commit comments