From be313103884a11f32f3c95ed9d5834647fef2807 Mon Sep 17 00:00:00 2001 From: Lujin Wang <143145775+lujinwangnv@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:41:27 -0700 Subject: Add DebugLine before IfElse (#7368) Missing DebugLine in some basic blocks that include OpBranchConditional causes invalid line number '0' presented in the line table of '.debug_line' section. Emiting Debugline before IfElse fixes the issue. Modified maybeEmitDebugLine() to handle the case without Stmt. --- source/slang/slang-lower-to-ir.cpp | 45 ++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'source') diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index 5400ab166..596a091d4 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -3094,9 +3094,10 @@ struct StmtLoweringVisitor; void maybeEmitDebugLine( IRGenContext* context, - StmtLoweringVisitor& visitor, + StmtLoweringVisitor* visitor, Stmt* stmt, - SourceLoc loc = SourceLoc()); + SourceLoc loc = SourceLoc(), + bool allowNullStmt = false); // When lowering something callable (most commonly a function declaration), // we need to construct an appropriate parameter list for the IR function @@ -5221,6 +5222,8 @@ struct ExprLoweringVisitorBase : public ExprVisitor auto afterBlock = builder->createBlock(); auto irCond = getSimpleVal(context, lowerRValueExpr(context, expr->arguments[0])); + maybeEmitDebugLine(context, nullptr, nullptr, irCond->sourceLoc, true); + // ifElse(, %true-block, %false-block, %after-block) builder->emitIfElse(irCond, thenBlock, elseBlock, afterBlock); @@ -6211,6 +6214,8 @@ struct StmtLoweringVisitor : StmtVisitor auto irCond = getSimpleVal(context, lowerRValueExpr(context, condExpr)); + maybeEmitDebugLine(context, this, stmt, condExpr->loc); + IRInst* ifInst = nullptr; if (elseStmt) @@ -6323,7 +6328,7 @@ struct StmtLoweringVisitor : StmtVisitor // want to emit the expression for the loop condition: if (const auto condExpr = stmt->predicateExpression) { - maybeEmitDebugLine(context, *this, stmt, condExpr->loc); + maybeEmitDebugLine(context, this, stmt, condExpr->loc); auto irCondition = getSimpleVal(context, lowerRValueExpr(context, stmt->predicateExpression)); @@ -6383,7 +6388,7 @@ struct StmtLoweringVisitor : StmtVisitor insertBlock(continueLabel); if (auto incrExpr = stmt->sideEffectExpression) { - maybeEmitDebugLine(context, *this, stmt, incrExpr->loc); + maybeEmitDebugLine(context, this, stmt, incrExpr->loc); lowerRValueExpr(context, incrExpr); } @@ -6432,7 +6437,7 @@ struct StmtLoweringVisitor : StmtVisitor // want to emit the expression for the loop condition: if (auto condExpr = stmt->predicate) { - maybeEmitDebugLine(context, *this, stmt, condExpr->loc); + maybeEmitDebugLine(context, this, stmt, condExpr->loc); auto irCondition = getSimpleVal(context, lowerRValueExpr(context, condExpr)); @@ -6498,7 +6503,7 @@ struct StmtLoweringVisitor : StmtVisitor // want to emit the expression for the loop condition: if (auto condExpr = stmt->predicate) { - maybeEmitDebugLine(context, *this, stmt, stmt->predicate->loc); + maybeEmitDebugLine(context, this, stmt, stmt->predicate->loc); auto irCondition = getSimpleVal(context, lowerRValueExpr(context, condExpr)); @@ -7398,24 +7403,29 @@ IRInst* getOrEmitDebugSource(IRGenContext* context, PathInfo path) void maybeEmitDebugLine( IRGenContext* context, - StmtLoweringVisitor& visitor, + StmtLoweringVisitor* visitor, Stmt* stmt, - SourceLoc loc) + SourceLoc loc, + bool allowNullStmt) { if (!context->includeDebugInfo) return; - if (as(stmt)) - return; - if (!loc.isValid()) - loc = stmt->loc; + + if (!allowNullStmt) + { + if (as(stmt)) + return; + if (!loc.isValid()) + loc = stmt->loc; + } + auto sourceManager = context->getLinkage()->getSourceManager(); - auto sourceView = context->getLinkage()->getSourceManager()->findSourceView(loc); + auto sourceView = sourceManager->findSourceView(loc); if (!sourceView) return; IRInst* debugSourceInst = nullptr; - auto humaneLoc = - context->getLinkage()->getSourceManager()->getHumaneLoc(loc, SourceLocType::Emit); + auto humaneLoc = sourceManager->getHumaneLoc(loc, SourceLocType::Emit); // Do a best-effort attempt to retrieve the nominal source file. auto pathInfo = sourceView->getPathInfo(loc, SourceLocType::Emit); @@ -7434,7 +7444,8 @@ void maybeEmitDebugLine( { debugSourceInst = getOrEmitDebugSource(context, pathInfo); } - visitor.startBlockIfNeeded(stmt); + if (visitor) + visitor->startBlockIfNeeded(stmt); context->irBuilder->emitDebugLine( debugSourceInst, humaneLoc.line, @@ -7471,7 +7482,7 @@ void lowerStmt(IRGenContext* context, Stmt* stmt) try { - maybeEmitDebugLine(context, visitor, stmt, stmt->loc); + maybeEmitDebugLine(context, &visitor, stmt, stmt->loc); visitor.dispatch(stmt); } // Don't emit any context message for an explicit `AbortCompilationException` -- cgit v1.2.3