Skip to content

Implement more descriptive pretty stack printing for generated derivatives. - #1402

Merged
vgvassilev merged 2 commits into
masterfrom
pretty-print-stack-trace
Jul 5, 2025
Merged

Implement more descriptive pretty stack printing for generated derivatives.#1402
vgvassilev merged 2 commits into
masterfrom
pretty-print-stack-trace

Conversation

@vgvassilev

Copy link
Copy Markdown
Owner

This prints:

1.	<eof> parser at end of file
2.	Building code for '<double wrapper_function(double x, double (*func)(double))>[name=wrapper_function, order=1, mode=pushforward, args='', tbr]'
 #0 0x0000000104d4c680 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/Users/vvassilev/workspace/builds/llvm-project-clad/bin/clang-18+0x102c14680)
 #1 0x0000000104d4cc64 PrintStackTraceSignalHandler(void*) (/Users/vvassilev/workspace/builds/llvm-project-clad/bin/clang-18+0x102c14c64)
 #2 0x0000000104d4a91c llvm::sys::RunSignalHandlers() (/Users/vvassilev/workspace/builds/llvm-project-clad/bin/clang-18+0x102c1291c)
 #3 0x0000000104d4dd80 SignalHandler(int) (/Users/vvassilev/workspace/builds/llvm-project-clad/bin/clang-18+0x102c15d80)
 #4 0x0000000185424624 (/usr/lib/system/libsystem_platform.dylib+0x1804ac624)
...

@PetroZarytskyi, can you help me put what would help us make more sense out of a stack trace? I thought it might be a good idea to print also the last statement of the forward and reverse passes. Would that help?

@vgvassilev
vgvassilev requested a review from PetroZarytskyi June 9, 2025 15:00

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/VisitorBase.h
Comment thread include/clad/Differentiator/VisitorBase.h
Comment thread lib/Differentiator/BaseForwardModeVisitor.cpp Outdated
Comment thread lib/Differentiator/ReverseModeVisitor.cpp Outdated
@codecov

codecov Bot commented Jun 9, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 34.28571% with 23 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
lib/Differentiator/VisitorBase.cpp 0.00% 21 Missing ⚠️
...clude/clad/Differentiator/BaseForwardModeVisitor.h 80.00% 1 Missing ⚠️
include/clad/Differentiator/ReverseModeVisitor.h 66.66% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@PetroZarytskyi

Copy link
Copy Markdown
Collaborator

Probably not for this PR, but I think we need to include DVI in the DiffRequest dump. I did it locally while working on static graph PRs as it's crucial for matching requests and caused the most trouble.

@PetroZarytskyi

Copy link
Copy Markdown
Collaborator

This functionality looks very useful. At first, I thought it would be amazing to see the last original statement we started visiting (ideally, with location). This would be enough info for 80% of the cases when I need to run debuggers. But perhaps this would be hard to implement. Dumping the last generated stmts seems like a great idea as it does pretty much the same job.

@vgvassilev

Copy link
Copy Markdown
Owner Author

This functionality looks very useful. At first, I thought it would be amazing to see the last original statement we started visiting (ideally, with location). This would be enough info for 80% of the cases when I need to run debuggers. But perhaps this would be hard to implement. Dumping the last generated stmts seems like a great idea as it does pretty much the same job.

Did you mean:

1.	<eof> parser at end of file
2.	Building code for '<double wrapper_function(double x, double (*func)(double))>[name=wrapper_function, order=1, mode=pushforward, args='', tbr]'
While visiting <IntegerLiteral> [ '/Users/vvassilev/workspace/sources/clad/test/Misc/Test.cpp:11:22', '/Users/vvassilev/workspace/sources/clad/test/Misc/Test.cpp:11:22']

Begin Stmt Dump ---
2
End Stmt Dump ---

@vgvassilev
vgvassilev force-pushed the pretty-print-stack-trace branch from 5918828 to e31fe6c Compare June 10, 2025 08:14

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

clang::Expr* m_ThisExprDerivative = nullptr;

/// The currently visited statement. Useful for crash pretty-printing.
const clang::Stmt* m_CurVisitedStmt = nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: member variable 'm_CurVisitedStmt' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

    const clang::Stmt* m_CurVisitedStmt = nullptr;
                       ^

Comment thread lib/Differentiator/BaseForwardModeVisitor.cpp
Comment thread lib/Differentiator/VisitorBase.cpp Outdated
Comment thread lib/Differentiator/VisitorBase.cpp
@vgvassilev
vgvassilev force-pushed the pretty-print-stack-trace branch from e31fe6c to d454e8e Compare June 10, 2025 08:44
Comment thread include/clad/Differentiator/BaseForwardModeVisitor.h
@vgvassilev
vgvassilev force-pushed the pretty-print-stack-trace branch from d454e8e to fa0387b Compare July 4, 2025 19:43
#ifndef NDEBUG
// Enable testing of the pretty printing of the state when clad crashes.
if (const char* Env = std::getenv("CLAD_FORCE_CRASH"))
std::terminate();

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tested but only run in debug mode. Our codecov infrastructure does not do run in debug mode that's why it complains.

@vgvassilev
vgvassilev force-pushed the pretty-print-stack-trace branch from fa0387b to 41c8785 Compare July 4, 2025 20:03
@vgvassilev
vgvassilev force-pushed the pretty-print-stack-trace branch from 41c8785 to 5b032bf Compare July 4, 2025 20:04
@vgvassilev
vgvassilev merged commit a40fa2c into master Jul 5, 2025
149 of 170 checks passed
@vgvassilev
vgvassilev deleted the pretty-print-stack-trace branch July 5, 2025 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants