|
| 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 |
| 11 | +generated in the dxil sourced from hlsl. |
| 12 | + |
| 13 | +## Motivation |
| 14 | + |
| 15 | +As the new shader models, i.e. RayTracing, RayQuery, Workgraph, and more complex |
| 16 | +algorithms and structure emerges in recent time, we meet many more requirements of |
| 17 | +debugging capabilities of hlsl coding in game, application and driver development. |
| 18 | + |
| 19 | +The dxil counterpart spirv has a similar feature, |
| 20 | +[NonSemantic.DebugPrintf extention](https://github.com/KhronosGroup/SPIRV-Registry/blob/main/nonsemantic/NonSemantic.DebugPrintf.asciidoc) |
| 21 | +to generate DebugPrintf spirvOp souced from hlsl/glsl. Based on the spirvOp |
| 22 | +extention, some vendor drivers and Valve lunarG has |
| 23 | +[debug printf layer](https://github.com/KhronosGroup/Vulkan-ValidationLayers/blob/main/docs/debug_printf.md) |
| 24 | +to dump printf expression, "hlsl/glsl variables" into stdio or file. |
| 25 | + |
| 26 | +## Proposed solution |
| 27 | + |
| 28 | +The printf expression in hlsl mostly like this example. |
| 29 | +```c++ hlsl: |
| 30 | + |
| 31 | +const string str0= "str0"; |
| 32 | +string str1 = "str1"; |
| 33 | + |
| 34 | +void main() { |
| 35 | + printf(str0); |
| 36 | + printf(str1); |
| 37 | + printf("Variables are: %d %d %.2f", 1u, 2u, 1.5f); |
| 38 | +} |
| 39 | + |
| 40 | +``` |
| 41 | + |
| 42 | +The format specifier generally follow the c/c++ format specifiers as here, https://www.geeksforgeeks.org/format-specifiers-in-c/, |
| 43 | +Though how the final string representation after printf output is implementation dependent. |
| 44 | + |
| 45 | + |
| 46 | +DirectXCompiler at present can parse "printf" statement in hlsl as dx.hl.op |
| 47 | +instructions, |
| 48 | +``` |
| 49 | +dx.hl.op..void (i32, i8*, ...); |
| 50 | +``` |
| 51 | +The printf format string, the second argument of the printf instruction of |
| 52 | +dx.hl.op is a global *constant* variable or a GEP constant expression. |
| 53 | + |
| 54 | +The parsing is called from ast frontend into the |
| 55 | +HandleTranslationUnit-->TranslatePrintf.The function *TranslatePrintf* itself |
| 56 | + is empty implementation. |
| 57 | + |
| 58 | +The implementation of debug printf dxil op will be |
| 59 | + |
| 60 | +1) assign new dxil op code to the debug printf. |
| 61 | +2) Finished the TranslatePrintf implementation, create dxil op instruction with |
| 62 | +a proper dxil op code, and replace dx.hl.op. the dxil op instruction function |
| 63 | +will be a variable arguments function. |
| 64 | + |
| 65 | + |
| 66 | +## Detailed design |
| 67 | + |
| 68 | +1. The printf dxil op will be non-semantic, it does not affect final hlsl code/algorithm. |
| 69 | +Non-semantic dxil op code can be counted down from 0xffff, or 0xffffffff, it will give a hint to the client api |
| 70 | +to remove the non semantic dxil safely |
| 71 | +2. Add a option to enable printf dxil op generation to dxc, try to separate hlsl code for debugging |
| 72 | +and for production, if printf option is disabled, the printf in hlsl will be report a error |
| 73 | +3. We should not support dynamic string variable, a string variable content. |
| 74 | +retrieved from buffer. The string variable should be explicited defined and can |
| 75 | +be retrieved directly/indirectly from global constant variable. |
| 76 | +4. The format string input to the dx.hl.op..void, could be llvm constant |
| 77 | +expression, we need to retrieve global variable from the constant expression. |
| 78 | +5. The validation for the dxil overloading checking should be ignored. Because |
| 79 | +of printf variable arguments, there is no definite function type can be validated. |
| 80 | +6. dxc does not valiate format specifier to the c/c++ format speicifer standard, or the matching relation between |
| 81 | +format specifier and argument. If the number and type don't match, they will produce undefined result from |
| 82 | +client api, e.g. driver. |
0 commit comments