Skip to content

Commit a1d417d

Browse files
authored
Merge pull request #14385 from alexet/ir-debug-perf
CPP: Improve performance of IR debugging
2 parents 0258dd4 + c79ec8c commit a1d417d

File tree

11 files changed

+166
-96
lines changed

11 files changed

+166
-96
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/IRBlock.qll

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ private import internal.IRBlockImports as Imports
88
import Imports::EdgeKind
99
private import Cached
1010

11+
/**
12+
* Holds if `block` is a block in `func` and `sortOverride`, `sortKey1`, and `sortKey2` are the
13+
* sort keys of the block (derived from its first instruction)
14+
*/
15+
pragma[nomagic]
16+
private predicate blockSortKeys(
17+
IRFunction func, IRBlockBase block, int sortOverride, int sortKey1, int sortKey2
18+
) {
19+
block.getEnclosingIRFunction() = func and
20+
block.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
21+
// Ensure that the block containing `EnterFunction` always comes first.
22+
if block.getFirstInstruction() instanceof EnterFunctionInstruction
23+
then sortOverride = 0
24+
else sortOverride = 1
25+
}
26+
1127
/**
1228
* A basic block in the IR. A basic block consists of a sequence of `Instructions` with the only
1329
* incoming edges at the beginning of the sequence and the only outgoing edges at the end of the
@@ -37,17 +53,14 @@ class IRBlockBase extends TIRBlock {
3753
exists(IRConfiguration::IRConfiguration config |
3854
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
3955
) and
40-
this =
41-
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
42-
funcBlock.getEnclosingFunction() = this.getEnclosingFunction() and
43-
funcBlock.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
44-
// Ensure that the block containing `EnterFunction` always comes first.
45-
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
46-
then sortOverride = 0
47-
else sortOverride = 1
48-
|
49-
funcBlock order by sortOverride, sortKey1, sortKey2
50-
)
56+
exists(IRFunction func |
57+
this =
58+
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
59+
blockSortKeys(func, funcBlock, sortOverride, sortKey1, sortKey2)
60+
|
61+
funcBlock order by sortOverride, sortKey1, sortKey2
62+
)
63+
)
5164
}
5265

5366
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ class Instruction extends Construction::TStageInstruction {
116116

117117
private int getLineRank() {
118118
this.shouldGenerateDumpStrings() and
119-
this =
120-
rank[result](Instruction instr |
121-
instr =
122-
getAnInstructionAtLine(this.getEnclosingIRFunction(), this.getLocation().getFile(),
123-
this.getLocation().getStartLine())
124-
|
125-
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
126-
)
119+
exists(IRFunction enclosing, Language::File file, int line |
120+
this =
121+
rank[result](Instruction instr |
122+
instr = getAnInstructionAtLine(enclosing, file, line)
123+
|
124+
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
125+
)
126+
)
127127
}
128128

129129
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/IRBlock.qll

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ private import internal.IRBlockImports as Imports
88
import Imports::EdgeKind
99
private import Cached
1010

11+
/**
12+
* Holds if `block` is a block in `func` and `sortOverride`, `sortKey1`, and `sortKey2` are the
13+
* sort keys of the block (derived from its first instruction)
14+
*/
15+
pragma[nomagic]
16+
private predicate blockSortKeys(
17+
IRFunction func, IRBlockBase block, int sortOverride, int sortKey1, int sortKey2
18+
) {
19+
block.getEnclosingIRFunction() = func and
20+
block.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
21+
// Ensure that the block containing `EnterFunction` always comes first.
22+
if block.getFirstInstruction() instanceof EnterFunctionInstruction
23+
then sortOverride = 0
24+
else sortOverride = 1
25+
}
26+
1127
/**
1228
* A basic block in the IR. A basic block consists of a sequence of `Instructions` with the only
1329
* incoming edges at the beginning of the sequence and the only outgoing edges at the end of the
@@ -37,17 +53,14 @@ class IRBlockBase extends TIRBlock {
3753
exists(IRConfiguration::IRConfiguration config |
3854
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
3955
) and
40-
this =
41-
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
42-
funcBlock.getEnclosingFunction() = this.getEnclosingFunction() and
43-
funcBlock.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
44-
// Ensure that the block containing `EnterFunction` always comes first.
45-
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
46-
then sortOverride = 0
47-
else sortOverride = 1
48-
|
49-
funcBlock order by sortOverride, sortKey1, sortKey2
50-
)
56+
exists(IRFunction func |
57+
this =
58+
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
59+
blockSortKeys(func, funcBlock, sortOverride, sortKey1, sortKey2)
60+
|
61+
funcBlock order by sortOverride, sortKey1, sortKey2
62+
)
63+
)
5164
}
5265

5366
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/Instruction.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ class Instruction extends Construction::TStageInstruction {
116116

117117
private int getLineRank() {
118118
this.shouldGenerateDumpStrings() and
119-
this =
120-
rank[result](Instruction instr |
121-
instr =
122-
getAnInstructionAtLine(this.getEnclosingIRFunction(), this.getLocation().getFile(),
123-
this.getLocation().getStartLine())
124-
|
125-
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
126-
)
119+
exists(IRFunction enclosing, Language::File file, int line |
120+
this =
121+
rank[result](Instruction instr |
122+
instr = getAnInstructionAtLine(enclosing, file, line)
123+
|
124+
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
125+
)
126+
)
127127
}
128128

129129
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,12 @@ private module CachedForDebugging {
423423
cached
424424
predicate instructionHasSortKeys(Instruction instruction, int key1, int key2) {
425425
key1 = getInstructionTranslatedElement(instruction).getId() and
426-
getInstructionTag(instruction) =
426+
getInstructionTag(instruction) = tagByRank(key2)
427+
}
428+
429+
pragma[nomagic]
430+
private InstructionTag tagByRank(int key2) {
431+
result =
427432
rank[key2](InstructionTag tag, string tagId |
428433
tagId = getInstructionTagId(tag)
429434
|

cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/IRBlock.qll

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ private import internal.IRBlockImports as Imports
88
import Imports::EdgeKind
99
private import Cached
1010

11+
/**
12+
* Holds if `block` is a block in `func` and `sortOverride`, `sortKey1`, and `sortKey2` are the
13+
* sort keys of the block (derived from its first instruction)
14+
*/
15+
pragma[nomagic]
16+
private predicate blockSortKeys(
17+
IRFunction func, IRBlockBase block, int sortOverride, int sortKey1, int sortKey2
18+
) {
19+
block.getEnclosingIRFunction() = func and
20+
block.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
21+
// Ensure that the block containing `EnterFunction` always comes first.
22+
if block.getFirstInstruction() instanceof EnterFunctionInstruction
23+
then sortOverride = 0
24+
else sortOverride = 1
25+
}
26+
1127
/**
1228
* A basic block in the IR. A basic block consists of a sequence of `Instructions` with the only
1329
* incoming edges at the beginning of the sequence and the only outgoing edges at the end of the
@@ -37,17 +53,14 @@ class IRBlockBase extends TIRBlock {
3753
exists(IRConfiguration::IRConfiguration config |
3854
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
3955
) and
40-
this =
41-
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
42-
funcBlock.getEnclosingFunction() = this.getEnclosingFunction() and
43-
funcBlock.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
44-
// Ensure that the block containing `EnterFunction` always comes first.
45-
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
46-
then sortOverride = 0
47-
else sortOverride = 1
48-
|
49-
funcBlock order by sortOverride, sortKey1, sortKey2
50-
)
56+
exists(IRFunction func |
57+
this =
58+
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
59+
blockSortKeys(func, funcBlock, sortOverride, sortKey1, sortKey2)
60+
|
61+
funcBlock order by sortOverride, sortKey1, sortKey2
62+
)
63+
)
5164
}
5265

5366
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ class Instruction extends Construction::TStageInstruction {
116116

117117
private int getLineRank() {
118118
this.shouldGenerateDumpStrings() and
119-
this =
120-
rank[result](Instruction instr |
121-
instr =
122-
getAnInstructionAtLine(this.getEnclosingIRFunction(), this.getLocation().getFile(),
123-
this.getLocation().getStartLine())
124-
|
125-
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
126-
)
119+
exists(IRFunction enclosing, Language::File file, int line |
120+
this =
121+
rank[result](Instruction instr |
122+
instr = getAnInstructionAtLine(enclosing, file, line)
123+
|
124+
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
125+
)
126+
)
127127
}
128128

129129
/**

csharp/ql/src/experimental/ir/implementation/raw/IRBlock.qll

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ private import internal.IRBlockImports as Imports
88
import Imports::EdgeKind
99
private import Cached
1010

11+
/**
12+
* Holds if `block` is a block in `func` and `sortOverride`, `sortKey1`, and `sortKey2` are the
13+
* sort keys of the block (derived from its first instruction)
14+
*/
15+
pragma[nomagic]
16+
private predicate blockSortKeys(
17+
IRFunction func, IRBlockBase block, int sortOverride, int sortKey1, int sortKey2
18+
) {
19+
block.getEnclosingIRFunction() = func and
20+
block.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
21+
// Ensure that the block containing `EnterFunction` always comes first.
22+
if block.getFirstInstruction() instanceof EnterFunctionInstruction
23+
then sortOverride = 0
24+
else sortOverride = 1
25+
}
26+
1127
/**
1228
* A basic block in the IR. A basic block consists of a sequence of `Instructions` with the only
1329
* incoming edges at the beginning of the sequence and the only outgoing edges at the end of the
@@ -37,17 +53,14 @@ class IRBlockBase extends TIRBlock {
3753
exists(IRConfiguration::IRConfiguration config |
3854
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
3955
) and
40-
this =
41-
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
42-
funcBlock.getEnclosingFunction() = this.getEnclosingFunction() and
43-
funcBlock.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
44-
// Ensure that the block containing `EnterFunction` always comes first.
45-
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
46-
then sortOverride = 0
47-
else sortOverride = 1
48-
|
49-
funcBlock order by sortOverride, sortKey1, sortKey2
50-
)
56+
exists(IRFunction func |
57+
this =
58+
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
59+
blockSortKeys(func, funcBlock, sortOverride, sortKey1, sortKey2)
60+
|
61+
funcBlock order by sortOverride, sortKey1, sortKey2
62+
)
63+
)
5164
}
5265

5366
/**

csharp/ql/src/experimental/ir/implementation/raw/Instruction.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ class Instruction extends Construction::TStageInstruction {
116116

117117
private int getLineRank() {
118118
this.shouldGenerateDumpStrings() and
119-
this =
120-
rank[result](Instruction instr |
121-
instr =
122-
getAnInstructionAtLine(this.getEnclosingIRFunction(), this.getLocation().getFile(),
123-
this.getLocation().getStartLine())
124-
|
125-
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
126-
)
119+
exists(IRFunction enclosing, Language::File file, int line |
120+
this =
121+
rank[result](Instruction instr |
122+
instr = getAnInstructionAtLine(enclosing, file, line)
123+
|
124+
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
125+
)
126+
)
127127
}
128128

129129
/**

csharp/ql/src/experimental/ir/implementation/unaliased_ssa/IRBlock.qll

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ private import internal.IRBlockImports as Imports
88
import Imports::EdgeKind
99
private import Cached
1010

11+
/**
12+
* Holds if `block` is a block in `func` and `sortOverride`, `sortKey1`, and `sortKey2` are the
13+
* sort keys of the block (derived from its first instruction)
14+
*/
15+
pragma[nomagic]
16+
private predicate blockSortKeys(
17+
IRFunction func, IRBlockBase block, int sortOverride, int sortKey1, int sortKey2
18+
) {
19+
block.getEnclosingIRFunction() = func and
20+
block.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
21+
// Ensure that the block containing `EnterFunction` always comes first.
22+
if block.getFirstInstruction() instanceof EnterFunctionInstruction
23+
then sortOverride = 0
24+
else sortOverride = 1
25+
}
26+
1127
/**
1228
* A basic block in the IR. A basic block consists of a sequence of `Instructions` with the only
1329
* incoming edges at the beginning of the sequence and the only outgoing edges at the end of the
@@ -37,17 +53,14 @@ class IRBlockBase extends TIRBlock {
3753
exists(IRConfiguration::IRConfiguration config |
3854
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
3955
) and
40-
this =
41-
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
42-
funcBlock.getEnclosingFunction() = this.getEnclosingFunction() and
43-
funcBlock.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
44-
// Ensure that the block containing `EnterFunction` always comes first.
45-
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
46-
then sortOverride = 0
47-
else sortOverride = 1
48-
|
49-
funcBlock order by sortOverride, sortKey1, sortKey2
50-
)
56+
exists(IRFunction func |
57+
this =
58+
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
59+
blockSortKeys(func, funcBlock, sortOverride, sortKey1, sortKey2)
60+
|
61+
funcBlock order by sortOverride, sortKey1, sortKey2
62+
)
63+
)
5164
}
5265

5366
/**

0 commit comments

Comments
 (0)