Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix#6784 - Add diagnostic for for-loop update expressions with no effect #6832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

szihs
Copy link
Contributor

@szihs szihs commented Apr 16, 2025

This commit introduces a new warning diagnostic (30506) for cases where the update expression in a for-loop does not modify any variables, potentially leading to infinite loops. The implementation includes checks in the SemanticsStmtVisitor to identify such expressions and provide appropriate warnings. Additionally, tests have been added to verify the new diagnostic behavior.

@szihs szihs requested a review from a team as a code owner April 16, 2025 15:59
@szihs szihs linked an issue Apr 16, 2025 that may be closed by this pull request
Copy link
Collaborator

@csyonghe csyonghe left a comment

Choose a reason for hiding this comment

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

The name based checking is a big code smell, and always should be avoided.

The right way to check is to implement a general side effect detection logic in post lowering validation phase next to where we are detecting missing returns. If we see a loop whose condition is a constant and the loop does not have any exit jumps, then there should be a warning.

@szihs
Copy link
Contributor Author

szihs commented Apr 22, 2025

The name based checking is a big code smell, and always should be avoided.

The right way to check is to implement a general side effect detection logic in post lowering validation phase next to where we are detecting missing returns. If we see a loop whose condition is a constant and the loop does not have any exit jumps, then there should be a warning.

@csyonghe by post lowering validation does that mean
slang-lower-to-ir.cpp checkForMissingReturns(module, compileRequest->getSink(), CodeGenTarget::None, true);

@csyonghe
Copy link
Collaborator

Correct, we should add another pass after the check missing return pass to validate this.

@szihs szihs self-assigned this Apr 24, 2025
@szihs szihs marked this pull request as draft April 24, 2025 04:58
@szihs
Copy link
Contributor Author

szihs commented Apr 24, 2025

Okay. Would require to redo the logic.

szihs added 3 commits April 25, 2025 01:38
This commit introduces a new warning diagnostic (30506) for cases where the update expression in a for-loop does not modify any variables, potentially leading to infinite loops. The implementation includes checks in the `SemanticsStmtVisitor` to identify such expressions and provide appropriate warnings. Additionally, tests have been added to verify the new diagnostic behavior.
@szihs szihs force-pushed the haaggarwal/fix-6784-forloop branch from 3d9367c to f400187 Compare April 24, 2025 20:16
@szihs szihs force-pushed the haaggarwal/fix-6784-forloop branch from 8bffd28 to 3e2bee9 Compare April 25, 2025 09:16
@szihs szihs marked this pull request as ready for review April 25, 2025 09:16
@@ -56,5 +58,9 @@ float doSomething(int x)
{
i--;
}
for (int i = 1; i < 10; i+1) // warn.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@csyonghe - I observed that Diagnostics::potentialInfiniteLoop is only emitted for first occurence in doSomething. In the code we are doing a graph search - so ideally this should be caught, but i am not able to wrap my head on this.

void checkForPotentialInfiniteLoops(IRModule* module, DiagnosticSink* sink, bool diagnoseWarning)
{
// Look for loops in the module
checkForPotentialInfiniteLoopsInInst(module->getModuleInst(), sink, diagnoseWarning);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be done only for any IRLoop in the module, instead of just starting off on the first inst?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We start procesing all IR instructions - The IRloop check is done inside this function at L105

@szihs szihs requested a review from expipiplus1 April 30, 2025 03:41
@szihs
Copy link
Contributor Author

szihs commented Apr 30, 2025

The original issue is NOT giving warning ->image (17)

However able to do on sample repro loop_issue1.slang Image-2
image (18)
adding @expipiplus1 to review as well.

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.

Windows: slangc generate no code for compute shader
3 participants