-
Notifications
You must be signed in to change notification settings - Fork 2k
C++: Split new range analysis into constant and relative stages #11928
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
Merged
MathiasVP
merged 10 commits into
github:main
from
rdmarsh2:rdmarsh2/stageify-range-analysis
Mar 18, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
50fac30
C++: split RA into constant and relative phases
b941d54
C++ Move RangeAnalysis to work around shadowing
d4e3f7f
C++: fix missing bounds in new range analysis
b4b7507
C++: autoformat
267c341
C++: exclude `ZeroBound`s in relative stage
623e390
C++: fix ModulusAnalysis test
45fdf69
C++: add SemLocation so SemBound is copy-shareable
1aecc64
C++: Autoformat.
MathiasVP 726f999
C++: remove direct IR dependency in range analysis
1e8404c
C++: Remove fixed TODO
rdmarsh2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
cpp/ql/lib/experimental/semmle/code/cpp/semantic/SemanticLocation.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| private import semmle.code.cpp.Location | ||
|
|
||
| class SemLocation instanceof Location { | ||
| /** | ||
| * Gets a textual representation of this element. | ||
| * | ||
| * The format is "file://filePath:startLine:startColumn:endLine:endColumn". | ||
| */ | ||
| string toString() { result = super.toString() } | ||
|
|
||
| /** | ||
| * Holds if this element is at the specified location. | ||
| * The location spans column `startcolumn` of line `startline` to | ||
| * column `endcolumn` of line `endline` in file `filepath`. | ||
| * For more information, see | ||
| * [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). | ||
| */ | ||
| predicate hasLocationInfo( | ||
| string filepath, int startline, int startcolumn, int endline, int endcolumn | ||
| ) { | ||
| super.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
cpp/ql/lib/experimental/semmle/code/cpp/semantic/analysis/IntDelta.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| private import RangeAnalysisStage | ||
|
|
||
| module IntDelta implements DeltaSig { | ||
| class Delta = int; | ||
|
|
||
| bindingset[d] | ||
| bindingset[result] | ||
| float toFloat(Delta d) { result = d } | ||
|
|
||
| bindingset[d] | ||
| bindingset[result] | ||
| int toInt(Delta d) { result = d } | ||
|
|
||
| bindingset[n] | ||
| bindingset[result] | ||
| Delta fromInt(int n) { result = n } | ||
|
|
||
| bindingset[f] | ||
| Delta fromFloat(float f) { | ||
| result = | ||
| min(float diff, float res | | ||
| diff = (res - f) and res = f.ceil() | ||
| or | ||
| diff = (f - res) and res = f.floor() | ||
| | | ||
| res order by diff | ||
| ) | ||
| } | ||
| } |
26 changes: 2 additions & 24 deletions
26
cpp/ql/lib/experimental/semmle/code/cpp/semantic/analysis/RangeAnalysis.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,2 @@ | ||
| private import RangeAnalysisStage | ||
| private import RangeAnalysisSpecific | ||
| private import experimental.semmle.code.cpp.semantic.analysis.FloatDelta | ||
| private import RangeUtils | ||
| private import experimental.semmle.code.cpp.semantic.SemanticBound as SemanticBound | ||
|
|
||
| module Bounds implements BoundSig<FloatDelta> { | ||
| class SemBound instanceof SemanticBound::SemBound { | ||
| string toString() { result = super.toString() } | ||
|
|
||
| SemExpr getExpr(float delta) { result = super.getExpr(delta) } | ||
| } | ||
|
|
||
| class SemZeroBound extends SemBound instanceof SemanticBound::SemZeroBound { } | ||
|
|
||
| class SemSsaBound extends SemBound instanceof SemanticBound::SemSsaBound { | ||
| SemSsaVariable getAVariable() { result = this.(SemanticBound::SemSsaBound).getAVariable() } | ||
| } | ||
| } | ||
|
|
||
| private module CppRangeAnalysis = | ||
| RangeStage<FloatDelta, Bounds, CppLangImpl, RangeUtil<FloatDelta, CppLangImpl>>; | ||
|
|
||
| import CppRangeAnalysis | ||
| import RangeAnalysisImpl | ||
| import experimental.semmle.code.cpp.semantic.SemanticBound |
107 changes: 107 additions & 0 deletions
107
cpp/ql/lib/experimental/semmle/code/cpp/semantic/analysis/RangeAnalysisImpl.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| private import RangeAnalysisStage | ||
| private import RangeAnalysisSpecific | ||
| private import experimental.semmle.code.cpp.semantic.analysis.FloatDelta | ||
| private import RangeUtils | ||
| private import experimental.semmle.code.cpp.semantic.SemanticBound as SemanticBound | ||
| private import experimental.semmle.code.cpp.semantic.SemanticLocation | ||
| private import experimental.semmle.code.cpp.semantic.SemanticSSA | ||
|
|
||
| module ConstantBounds implements BoundSig<FloatDelta> { | ||
| class SemBound instanceof SemanticBound::SemBound { | ||
| SemBound() { | ||
| this instanceof SemanticBound::SemZeroBound | ||
| or | ||
| this.(SemanticBound::SemSsaBound).getAVariable() instanceof SemSsaPhiNode | ||
| } | ||
|
|
||
| string toString() { result = super.toString() } | ||
|
|
||
| SemLocation getLocation() { result = super.getLocation() } | ||
|
|
||
| SemExpr getExpr(float delta) { result = super.getExpr(delta) } | ||
| } | ||
|
|
||
| class SemZeroBound extends SemBound instanceof SemanticBound::SemZeroBound { } | ||
|
|
||
| class SemSsaBound extends SemBound instanceof SemanticBound::SemSsaBound { | ||
| SemSsaVariable getAVariable() { result = this.(SemanticBound::SemSsaBound).getAVariable() } | ||
| } | ||
| } | ||
|
|
||
| private module RelativeBounds implements BoundSig<FloatDelta> { | ||
| class SemBound instanceof SemanticBound::SemBound { | ||
| SemBound() { not this instanceof SemanticBound::SemZeroBound } | ||
|
|
||
| string toString() { result = super.toString() } | ||
|
|
||
| SemLocation getLocation() { result = super.getLocation() } | ||
|
|
||
| SemExpr getExpr(float delta) { result = super.getExpr(delta) } | ||
| } | ||
|
|
||
| class SemZeroBound extends SemBound instanceof SemanticBound::SemZeroBound { } | ||
|
|
||
| class SemSsaBound extends SemBound instanceof SemanticBound::SemSsaBound { | ||
| SemSsaVariable getAVariable() { result = this.(SemanticBound::SemSsaBound).getAVariable() } | ||
| } | ||
| } | ||
|
|
||
| private module ConstantStage = | ||
| RangeStage<FloatDelta, ConstantBounds, CppLangImpl, RangeUtil<FloatDelta, CppLangImpl>>; | ||
|
|
||
| private module RelativeStage = | ||
| RangeStage<FloatDelta, RelativeBounds, CppLangImpl, RangeUtil<FloatDelta, CppLangImpl>>; | ||
|
|
||
| private newtype TSemReason = | ||
| TSemNoReason() or | ||
| TSemCondReason(SemGuard guard) { | ||
| guard = any(ConstantStage::SemCondReason reason).getCond() | ||
| or | ||
| guard = any(RelativeStage::SemCondReason reason).getCond() | ||
| } | ||
|
|
||
| /** | ||
| * A reason for an inferred bound. This can either be `CondReason` if the bound | ||
| * is due to a specific condition, or `NoReason` if the bound is inferred | ||
| * without going through a bounding condition. | ||
| */ | ||
| abstract class SemReason extends TSemReason { | ||
| /** Gets a textual representation of this reason. */ | ||
| abstract string toString(); | ||
| } | ||
|
|
||
| /** | ||
| * A reason for an inferred bound that indicates that the bound is inferred | ||
| * without going through a bounding condition. | ||
| */ | ||
| class SemNoReason extends SemReason, TSemNoReason { | ||
| override string toString() { result = "NoReason" } | ||
| } | ||
|
|
||
| /** A reason for an inferred bound pointing to a condition. */ | ||
| class SemCondReason extends SemReason, TSemCondReason { | ||
| /** Gets the condition that is the reason for the bound. */ | ||
| SemGuard getCond() { this = TSemCondReason(result) } | ||
|
|
||
| override string toString() { result = getCond().toString() } | ||
| } | ||
|
|
||
| private ConstantStage::SemReason constantReason(SemReason reason) { | ||
| result instanceof ConstantStage::SemNoReason and reason instanceof SemNoReason | ||
| or | ||
| result.(ConstantStage::SemCondReason).getCond() = reason.(SemCondReason).getCond() | ||
| } | ||
|
|
||
| private RelativeStage::SemReason relativeReason(SemReason reason) { | ||
| result instanceof RelativeStage::SemNoReason and reason instanceof SemNoReason | ||
| or | ||
| result.(RelativeStage::SemCondReason).getCond() = reason.(SemCondReason).getCond() | ||
| } | ||
|
|
||
| predicate semBounded( | ||
| SemExpr e, SemanticBound::SemBound b, float delta, boolean upper, SemReason reason | ||
| ) { | ||
| ConstantStage::semBounded(e, b, delta, upper, constantReason(reason)) | ||
| or | ||
| RelativeStage::semBounded(e, b, delta, upper, relativeReason(reason)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to check the performance impact of instantiating
RangeStagetwice. You can do that in one of two ways, I'd say:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the liberty of starting option two here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DCA looks fine 🎉