From 73a61edda8893901acad05bb4e7d3110db5041a8 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 28 Feb 2024 22:57:07 -0800 Subject: [SPIRV] Add NonSemanticDebugInfo for step-through debugging. (#3644) * [SPIRV] Add NonSemanticDebugInfo for step-through debugging. * Fix. * Fix. --- source/slang/slang-ir-insert-debug-value-store.cpp | 116 +++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 source/slang/slang-ir-insert-debug-value-store.cpp (limited to 'source/slang/slang-ir-insert-debug-value-store.cpp') diff --git a/source/slang/slang-ir-insert-debug-value-store.cpp b/source/slang/slang-ir-insert-debug-value-store.cpp new file mode 100644 index 000000000..2fec61f55 --- /dev/null +++ b/source/slang/slang-ir-insert-debug-value-store.cpp @@ -0,0 +1,116 @@ +#include "slang-ir-insert-debug-value-store.h" + +#include "slang-ir.h" +#include "slang-ir-insts.h" +#include "slang-ir-util.h" + +namespace Slang +{ + void insertDebugValueStore(IRFunc* func) + { + IRBuilder builder(func); + Dictionary mapVarToDebugVar; + auto firstBlock = func->getFirstBlock(); + if (!firstBlock) + return; + auto funcDebugLoc = func->findDecoration(); + + List params; + for (auto param : firstBlock->getParams()) + { + params.add(param); + } + + for (auto param : params) + { + builder.setInsertBefore(firstBlock->getFirstOrdinaryInst()); + auto paramType = param->getDataType(); + bool isRefParam = false; + if (auto outType = as(paramType)) + { + isRefParam = true; + paramType = outType->getValueType(); + } + auto debugVar = builder.emitDebugVar( + paramType, + funcDebugLoc->getSource(), + funcDebugLoc->getLine(), + funcDebugLoc->getCol()); + copyNameHintAndDebugDecorations(debugVar, param); + + mapVarToDebugVar[param] = debugVar; + + // Store the initial value of the parameter into the debug var. + IRInst* paramVal = nullptr; + if (!isRefParam) + paramVal = param; + else if (as(param->getDataType())) + paramVal = builder.emitLoad(param); + if (paramVal) + { + ArrayView accessChain; + builder.emitDebugValue(debugVar, paramVal, accessChain); + } + } + + for (auto block : func->getBlocks()) + { + IRInst* nextInst = nullptr; + for (auto inst = block->getFirstInst(); inst; inst = nextInst) + { + nextInst = inst->getNextInst(); + if (auto varInst = as(inst)) + { + if (auto debugLoc = varInst->findDecoration()) + { + builder.setInsertBefore(varInst); + auto debugVar = builder.emitDebugVar( + tryGetPointedToType(&builder, varInst->getDataType()), + debugLoc->getSource(), + debugLoc->getLine(), + debugLoc->getCol()); + copyNameHintAndDebugDecorations(debugVar, varInst); + mapVarToDebugVar[varInst] = debugVar; + } + } + } + } + + // Collect all stores and insert debug value insts to update debug vars. + for (auto block : func->getBlocks()) + { + for (auto inst = block->getFirstInst(); inst; inst = inst->getNextInst()) + { + if (auto storeInst = as(inst)) + { + List accessChain; + auto varInst = getRootAddr(storeInst->getPtr(), accessChain); + IRInst* debugVar = nullptr; + if (mapVarToDebugVar.tryGetValue(varInst, debugVar)) + { + builder.setInsertBefore(storeInst); + builder.emitDebugValue(debugVar, storeInst->getVal(), accessChain.getArrayView()); + } + } + } + } + } + + void insertDebugValueStore(IRModule* module) + { + for (auto globalInst : module->getGlobalInsts()) + { + if (auto genericInst = as(globalInst)) + { + if (auto func = as(findGenericReturnVal(genericInst))) + { + insertDebugValueStore(func); + } + } + else if (auto func = as(globalInst)) + { + insertDebugValueStore(func); + } + } + } +} -- cgit v1.2.3