Skip to content

Commit fd83822

Browse files
authored
Merge pull request #23011 from aschackmull/ssa/caching
Ssa: Refactor caching
2 parents fb80308 + 1e2e6cb commit fd83822

15 files changed

Lines changed: 124 additions & 145 deletions

File tree

‎cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,8 @@ module IteratorFlow {
21732173
predicate variableRead(IRCfg::BasicBlock bb, int i, SourceVariable v, boolean certain) {
21742174
Ssa::variableRead(bb, i, v, certain)
21752175
}
2176+
2177+
predicate ssaCachedStageRevRef() { DataFlowIntegrationImpl::ssaCachedStageRevRef2() }
21762178
}
21772179

21782180
private module IteratorSsa = SsaImpl::Make<Location, IRCfg, SsaInput>;

‎cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaImpl.qll‎

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,37 +1104,29 @@ private module SsaInput implements Ssa::InputSig<Location, IRCfg::BasicBlock> {
11041104
if use.isCertain() then certain = true else certain = false
11051105
)
11061106
}
1107+
1108+
predicate ssaCachedStageRevRef() { DataFlowIntegrationImpl::ssaCachedStageRevRef2() }
11071109
}
11081110

1109-
/**
1110-
* The final SSA predicates used for dataflow purposes.
1111-
*/
1112-
cached
1113-
module SsaCached {
1114-
cached
1115-
predicate ssaDefReachesRead(SourceVariable v, Definition def, IRBlock bb, int i) {
1116-
SsaImpl::ssaDefReachesRead(v, def, bb, i)
1117-
}
1111+
predicate ssaDefReachesRead(SourceVariable v, Definition def, IRBlock bb, int i) {
1112+
SsaImpl::ssaDefReachesRead(v, def, bb, i)
1113+
}
11181114

1119-
cached
1120-
predicate phiHasInputFromBlock(PhiNode phi, Definition inp, IRBlock bb) {
1121-
SsaImpl::phiHasInputFromBlock(phi, inp, bb)
1122-
}
1115+
predicate phiHasInputFromBlock(PhiNode phi, Definition inp, IRBlock bb) {
1116+
SsaImpl::phiHasInputFromBlock(phi, inp, bb)
1117+
}
11231118

1124-
cached
1125-
predicate uncertainWriteDefinitionInput(Definition uncertain, Definition inp) {
1126-
SsaImpl::uncertainWriteDefinitionInput(uncertain, inp)
1127-
}
1119+
predicate uncertainWriteDefinitionInput(Definition uncertain, Definition inp) {
1120+
SsaImpl::uncertainWriteDefinitionInput(uncertain, inp)
1121+
}
11281122

1129-
cached
1130-
predicate ssaDefReachesEndOfBlock(IRBlock bb, Definition def) {
1131-
SsaImpl::ssaDefReachesEndOfBlock(bb, def, _)
1132-
}
1123+
predicate ssaDefReachesEndOfBlock(IRBlock bb, Definition def) {
1124+
SsaImpl::ssaDefReachesEndOfBlock(bb, def, _)
1125+
}
11331126

1134-
predicate variableRead = SsaInput::variableRead/4;
1127+
predicate variableRead = SsaInput::variableRead/4;
11351128

1136-
predicate variableWrite = SsaInput::variableWrite/4;
1137-
}
1129+
predicate variableWrite = SsaInput::variableWrite/4;
11381130

11391131
/** Gets the `DefImpl` corresponding to `def`. */
11401132
pragma[nomagic]
@@ -1173,14 +1165,6 @@ private module DataFlowIntegrationInput implements SsaImpl::DataFlowIntegrationI
11731165
predicate hasCfgNode(IRCfg::BasicBlock bb, int i) { bb.getInstruction(i) = this }
11741166
}
11751167

1176-
Expr getARead(SsaImpl::Definition def) {
1177-
exists(SourceVariable v, IRBlock bb, int i |
1178-
ssaDefReachesRead(v, def, bb, i) and
1179-
variableRead(bb, i, v, true) and
1180-
result.hasCfgNode(bb, i)
1181-
)
1182-
}
1183-
11841168
predicate ssaDefHasSource(SsaImpl::WriteDefinition def) { none() }
11851169

11861170
predicate allowFlowIntoUncertainDef(SsaImpl::UncertainWriteDefinition def) { any() }
@@ -1654,5 +1638,3 @@ class IndirectExplicitDefinition extends ExplicitDefinition {
16541638
class DirectExplicitDefinition extends ExplicitDefinition {
16551639
DirectExplicitDefinition() { this.getIndirectionIndex() = 0 }
16561640
}
1657-
1658-
import SsaCached

‎csharp/ql/lib/semmle/code/csharp/dataflow/internal/BaseSSA.qll‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ private module BaseSsaImpl {
121121
certain = true
122122
)
123123
}
124+
125+
predicate ssaCachedStageRevRef() { BaseSsa::ssaCachedStageRevRef1() }
124126
}
125127

126128
module SsaImpl = SsaImplCommon::Make<Location, Cfg, SsaImplInput>;

‎csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImpl.qll‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ private module SsaImplInput implements SsaImplCommon::InputSig<Location, BasicBl
3939
variableReadPseudo(bb, i, v) and
4040
certain = false
4141
}
42+
43+
predicate ssaCachedStageRevRef() {
44+
Ssa_::ssaCachedStageRevRef1() and DataFlowIntegrationImpl::ssaCachedStageRevRef2()
45+
}
4246
}
4347

4448
import SsaImplCommon::Make<Location, Cfg, SsaImplInput> as Impl
@@ -1012,8 +1016,6 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
10121016
predicate hasCfgNode(BasicBlock bb, int i) { this = bb.getNode(i) }
10131017
}
10141018

1015-
Expr getARead(Definition def) { def.(SsaDefinition).getARead().getControlFlowNode() = result }
1016-
10171019
predicate ssaDefHasSource(Impl::WriteDefinition def) {
10181020
// exclude flow directly from RHS to SSA definition, as we instead want to
10191021
// go from RHS to matching assignable definition, and from there to SSA definition

‎go/ql/lib/semmle/go/dataflow/SsaImpl.qll‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,32 +140,23 @@ final class PhiNode = Impl::PhiNode;
140140
module Consistency = Impl::Consistency;
141141

142142
/**
143-
* NB: This predicate should be cached.
144-
*
145143
* Holds if the SSA definition of `v` at `def` reaches a read at index `i` in
146144
* basic block `bb`.
147145
*/
148-
cached
149146
predicate ssaDefReachesRead(SsaSourceVariable v, Definition def, BasicBlock bb, int i) {
150147
Impl::ssaDefReachesRead(v, def, bb, i)
151148
}
152149

153150
/**
154-
* NB: This predicate should be cached.
155-
*
156151
* Holds if the SSA definition of `v` at `def` reaches the end of basic block `bb`.
157152
*/
158-
cached
159153
predicate ssaDefReachesEndOfBlock(BasicBlock bb, Definition def, SsaSourceVariable v) {
160154
Impl::ssaDefReachesEndOfBlock(bb, def, v)
161155
}
162156

163157
/**
164-
* NB: This predicate should be cached.
165-
*
166158
* Holds if `inp` is an input to the phi node `phi` along the edge originating in `bb`.
167159
*/
168-
cached
169160
predicate phiHasInputFromBlock(PhiNode phi, Definition inp, BasicBlock bb) {
170161
Impl::phiHasInputFromBlock(phi, inp, bb)
171162
}

‎java/ql/lib/semmle/code/java/dataflow/internal/BaseSSA.qll‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ private module SsaImplInput implements SsaImplCommon::InputSig<Location, BasicBl
184184
variableCapture(v, _, bb, i) and
185185
certain = false
186186
}
187+
188+
predicate ssaCachedStageRevRef() { Ssa::ssaCachedStageRevRef1() }
187189
}
188190

189191
private module Impl = SsaImplCommon::Make<Location, Cfg, SsaImplInput>;

‎java/ql/lib/semmle/code/java/dataflow/internal/SsaImpl.qll‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ private module SsaImplInput implements SsaImplCommon::InputSig<Location, BasicBl
199199
variableCapture(v, _, bb, i) and
200200
certain = false
201201
}
202+
203+
predicate ssaCachedStageRevRef() {
204+
Ssa::ssaCachedStageRevRef1() and DataFlowIntegrationImpl::ssaCachedStageRevRef2()
205+
}
202206
}
203207

204208
import SsaImplCommon::Make<Location, Cfg, SsaImplInput> as Impl
@@ -642,8 +646,6 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
642646
}
643647
}
644648

645-
Expr getARead(Definition def) { result = def.(SsaDefinition).getARead() }
646-
647649
predicate ssaDefHasSource(WriteDefinition def) { def instanceof SsaExplicitWrite }
648650

649651
predicate allowFlowIntoUncertainDef(UncertainWriteDefinition def) {

‎javascript/ql/lib/semmle/javascript/dataflow/internal/sharedlib/Ssa.qll‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ module SsaConfig implements InputSig<js::Location, js::Cfg::BasicBlock> {
3838
certain = true and
3939
bb.getNode(i).(ThisUse).getBindingContainer() = v.asThisContainer()
4040
}
41+
42+
predicate ssaCachedStageRevRef() { DataFlowIntegrationImpl::ssaCachedStageRevRef2() }
4143
}
4244

4345
import Make<js::Location, js::Cfg, SsaConfig>
@@ -56,16 +58,6 @@ module SsaDataflowInput implements DataFlowIntegrationInputSig {
5658
none()
5759
}
5860

59-
cached
60-
Expr getARead(Definition def) {
61-
// Copied from implementation so we can cache it here
62-
exists(SsaConfig::SourceVariable v, js::BasicBlock bb, int i |
63-
ssaDefReachesRead(v, def, bb, i) and
64-
SsaConfig::variableRead(bb, i, v, true) and
65-
result.hasCfgNode(bb, i)
66-
)
67-
}
68-
6961
class GuardValue = Boolean;
7062

7163
class Guard extends js::ControlFlowNode {
@@ -107,4 +99,6 @@ module SsaDataflowInput implements DataFlowIntegrationInputSig {
10799
}
108100
}
109101

110-
import DataFlowIntegration<SsaDataflowInput>
102+
private module DataFlowIntegrationImpl = DataFlowIntegration<SsaDataflowInput>;
103+
104+
import DataFlowIntegrationImpl

‎ruby/ql/lib/codeql/ruby/dataflow/SSA.qll‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ module Ssa {
333333
* Gets the immediately preceding definition. Since this update is uncertain,
334334
* the value from the preceding definition might still be valid.
335335
*/
336-
final Definition getPriorDefinition() { result = SsaImpl::uncertainWriteDefinitionInput(this) }
336+
final Definition getPriorDefinition() {
337+
SsaImpl::Impl::uncertainWriteDefinitionInput(this, result)
338+
}
337339

338340
override string toString() { result = "<captured exit> " + this.getSourceVariable() }
339341
}
@@ -381,7 +383,7 @@ module Ssa {
381383

382384
/** Holds if `inp` is an input to this phi node along the edge originating in `bb`. */
383385
predicate hasInputFromBlock(Definition inp, BasicBlock bb) {
384-
inp = SsaImpl::phiHasInputFromBlock(this, bb)
386+
SsaImpl::Impl::phiHasInputFromBlock(this, inp, bb)
385387
}
386388

387389
override string toString() { result = "phi" }

‎ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImpl.qll‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ module SsaInput implements SsaImplCommon::InputSig<Location, BasicBlock> {
5656
namespaceSelfExitRead(bb, i, v) and
5757
certain = false
5858
}
59+
60+
predicate ssaCachedStageRevRef() { DataFlowIntegrationImpl::ssaCachedStageRevRef2() }
5961
}
6062

6163
import SsaImplCommon::Make<Location, Cfg::Cfg, SsaInput> as Impl
@@ -279,11 +281,6 @@ private module Cached {
279281

280282
private import codeql.ruby.dataflow.SSA
281283

282-
cached
283-
Definition phiHasInputFromBlock(PhiNode phi, Cfg::BasicBlock bb) {
284-
Impl::phiHasInputFromBlock(phi, result, bb)
285-
}
286-
287284
/**
288285
* Holds if the value defined at SSA definition `def` can reach a read at `read`,
289286
* without passing through any other non-pseudo read.
@@ -310,11 +307,6 @@ private module Cached {
310307
)
311308
}
312309

313-
cached
314-
Definition uncertainWriteDefinitionInput(UncertainWriteDefinition def) {
315-
Impl::uncertainWriteDefinitionInput(def, result)
316-
}
317-
318310
cached
319311
module DataFlowIntegration {
320312
import DataFlowIntegrationImpl
@@ -446,8 +438,6 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
446438
predicate hasCfgNode(BasicBlock bb, int i) { this = bb.getNode(i) }
447439
}
448440

449-
Expr getARead(Definition def) { result = Cached::getARead(def) }
450-
451441
predicate ssaDefHasSource(WriteDefinition def) {
452442
any(ParameterExt p).isInitializedBy(def) or def.(Ssa::WriteDefinition).assigns(_)
453443
}

0 commit comments

Comments
 (0)