From 368ddbb7b99dfb939d20f53c35d05b2b4758bd64 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 21 Jul 2025 09:30:24 -0700 Subject: Add utility to trace creation of problematic IRInsts to assist LLM in debugging (#7820) * Initial plan * Add SLANG_DEBUG_IR_BREAK environment variable support Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Apply code formatting to SLANG_DEBUG_IR_BREAK implementation Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Improve stack trace debugging with -rdynamic flag and backtrace_symbols Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Address PR feedback: use PlatformUtil::getEnvironmentVariable, remove -rdynamic flag, and delete fallback branch Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Address PR feedback: simplify env var parsing, move backtrace to PlatformUtil, use #if for SLANG_LINUX_FAMILY Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Address PR feedback: remove unneeded include, make backtrace() more generic by removing uid parameter Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Fix and clone source tracking. * Add python script to dump traces. * Update instructions. * Batch calls to addr2line * Cleanup claude instructions. * update claude action. * Remove duplicated build instructions from claude.yml workflow Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * fix build error. * Fix build errors --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> Co-authored-by: Yong He Co-authored-by: Gangzheng Tong --- source/slang/slang-ir.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'source/slang/slang-ir.cpp') diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp index 3c2d5d2d1..63d3766ab 100644 --- a/source/slang/slang-ir.cpp +++ b/source/slang/slang-ir.cpp @@ -2,6 +2,7 @@ #include "slang-ir.h" #include "../core/slang-basic.h" +#include "../core/slang-platform.h" #include "../core/slang-writer.h" #include "slang-ir-dominators.h" #include "slang-ir-insts.h" @@ -1738,8 +1739,21 @@ void IRBuilder::_maybeSetSourceLoc(IRInst* inst) } #if SLANG_ENABLE_IR_BREAK_ALLOC -SLANG_API uint32_t _slangIRAllocBreak = 0xFFFFFFFF; +uint32_t _slangIRAllocBreak = 0xFFFFFFFF; +bool _slangIRPrintStackAtBreak = false; static bool _slangIRAllocBreakFirst = true; +static uint32_t _slangInstBeingCloned = 0xFFFFFFFF; + +void _debugSetInstBeingCloned(uint32_t uid) +{ + _slangInstBeingCloned = uid; +} + +void _debugResetInstBeingCloned() +{ + _slangInstBeingCloned = 0xFFFFFFFF; +} + uint32_t& _debugGetIRAllocCounter() { static uint32_t counter = 0; @@ -1758,6 +1772,20 @@ uint32_t _debugGetAndIncreaseInstCounter() #if _WIN32 && defined(_MSC_VER) __debugbreak(); #endif + if (_slangIRPrintStackAtBreak) + { + fprintf(stdout, "BEGIN IR Trace\nInstruction #%u created at:\n", _slangIRAllocBreak); + PlatformUtil::backtrace(); + if (_slangInstBeingCloned != 0xFFFFFFFF) + { + fprintf( + stdout, + "Inst #%u is a clone of Inst #%u.\n", + _slangIRAllocBreak, + _slangInstBeingCloned); + } + fprintf(stdout, "END IR Trace\n"); + } } return _debugGetIRAllocCounter()++; } -- cgit v1.2.3