Skip to content

Commit 2191088

Browse files
Refactor trace context (#55)
1 parent faf97b8 commit 2191088

18 files changed

+180
-98
lines changed

nautilus/src/nautilus/compiler/DumpHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "nautilus/compiler/DumpHandler.hpp"
2-
#include "nautilus/common/File.hpp"
32
#include "fmt/core.h"
3+
#include "nautilus/common/File.hpp"
44
#include <filesystem>
55

66
namespace nautilus::compiler {

nautilus/src/nautilus/tracing/ExecutionTrace.cpp

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ ExecutionTrace::ExecutionTrace() : currentBlockIndex(0), currentOperationIndex(0
1010
createBlock();
1111
}
1212

13+
Block& ExecutionTrace::getBlock(uint16_t blockIndex) {
14+
return blocks[blockIndex];
15+
}
16+
17+
uint16_t ExecutionTrace::getCurrentBlockIndex() const {
18+
return currentBlockIndex;
19+
}
20+
21+
Block& ExecutionTrace::getCurrentBlock() {
22+
return blocks[currentBlockIndex];
23+
}
24+
25+
void ExecutionTrace::setCurrentBlock(uint16_t index) {
26+
currentOperationIndex = 0;
27+
currentBlockIndex = index;
28+
}
29+
30+
std::vector<Block>& ExecutionTrace::getBlocks() {
31+
return blocks;
32+
}
33+
1334
bool ExecutionTrace::checkTag(Snapshot& snapshot) {
1435
// check if operation is in global map -> we have a repeating operation ->
1536
// this is a control-flow merge
@@ -20,11 +41,10 @@ bool ExecutionTrace::checkTag(Snapshot& snapshot) {
2041
return false;
2142
}
2243

44+
// check if we visited the same operation in this execution -> loop
2345
auto localTagIter = localTagMap.find(snapshot);
2446
if (localTagIter != localTagMap.end()) {
25-
// TODO #3500 Fix handling of repeated operations
2647
auto& ref = localTagIter->second;
27-
// add loop iteration to tag
2848
processControlFlowMerge(ref);
2949
return false;
3050
}
@@ -41,7 +61,6 @@ void ExecutionTrace::addReturn(Snapshot& snapshot, Type type, value_ref ref) {
4161
auto operationIdentifier = getNextOperationIdentifier();
4262
addTag(snapshot, operationIdentifier);
4363

44-
// returnRefs
4564
returnRefs.emplace_back(operationIdentifier);
4665
}
4766

@@ -88,11 +107,8 @@ void ExecutionTrace::addCmpOperation(Snapshot& snapshot, value_ref inputs) {
88107
getBlock(trueBlock).predecessors.emplace_back(getCurrentBlockIndex());
89108
auto falseBlock = createBlock();
90109
getBlock(falseBlock).predecessors.emplace_back(getCurrentBlockIndex());
91-
92-
auto operationInputs = std::vector<InputVariant> {inputs, BlockRef(trueBlock), BlockRef(falseBlock)};
93-
94110
auto& operations = blocks[currentBlockIndex].operations;
95-
operations.emplace_back(snapshot, CMP, Type::v, value_ref(getNextValueRef(), Type::v), std::forward<std::vector<InputVariant>>(operationInputs));
111+
operations.emplace_back(snapshot, CMP, Type::v, value_ref(getNextValueRef(), Type::v), std::vector<InputVariant> {inputs, BlockRef(trueBlock), BlockRef(falseBlock)});
96112
auto operationIdentifier = getNextOperationIdentifier();
97113
addTag(snapshot, operationIdentifier);
98114
}
@@ -107,25 +123,18 @@ void ExecutionTrace::nextOperation() {
107123
}
108124

109125
TraceOperation& ExecutionTrace::getCurrentOperation() {
110-
auto currentOp = getCurrentBlock().operations[currentOperationIndex];
126+
auto& currentOp = getCurrentBlock().operations[currentOperationIndex];
111127
while (currentOp.op == JMP) {
112128
auto& nextBlock = std::get<BlockRef>(currentOp.input[0]);
113129
setCurrentBlock(nextBlock.block);
114130
currentOp = getCurrentBlock().operations[currentOperationIndex];
115131
}
116-
return getCurrentBlock().operations[currentOperationIndex];
132+
return currentOp;
117133
}
118134

119135
uint16_t ExecutionTrace::createBlock() {
120-
// add first block
121-
if (blocks.empty()) {
122-
// add arguments to first block
123-
blocks.emplace_back(blocks.size());
124-
// blocks[0].arguments = arguments;
125-
return blocks.size() - 1;
126-
}
127-
blocks.emplace_back(blocks.size());
128-
return blocks.size() - 1;
136+
auto& block = blocks.emplace_back(blocks.size());
137+
return block.blockId;
129138
}
130139

131140
Block& ExecutionTrace::processControlFlowMerge(operation_identifier oi) {
@@ -168,10 +177,10 @@ Block& ExecutionTrace::processControlFlowMerge(operation_identifier oi) {
168177

169178
// add jump from referenced block to merge block
170179
auto mergeBlockRef = BlockRef(mergedBlockId);
171-
referenceBlock.addOperation({Op::JMP, std::vector<InputVariant> {mergeBlockRef}});
180+
referenceBlock.addOperation({Op::JMP, {mergeBlockRef}});
172181

173182
// add jump from current block to merge block
174-
currentBlock.addOperation({Op::JMP, std::vector<InputVariant> {mergeBlockRef}});
183+
currentBlock.addOperation({Op::JMP, {mergeBlockRef}});
175184

176185
mergeBlock.predecessors.emplace_back(oi.blockIndex);
177186
mergeBlock.predecessors.emplace_back(currentBlockIndex);
@@ -203,17 +212,12 @@ value_ref ExecutionTrace::setArgument(Type type, size_t index) {
203212
return arguments[index];
204213
}
205214

206-
void ExecutionTrace::destruct(nautilus::tracing::value_ref) {
207-
// variableBitset[inputs] = false;
208-
}
209-
210215
std::vector<operation_identifier> ExecutionTrace::getReturn() {
211216
return returnRefs;
212217
}
213218

214219
uint16_t ExecutionTrace::getNextValueRef() {
215-
auto ref = ++lastValueRef;
216-
return ref;
220+
return ++lastValueRef;
217221
}
218222

219223
operation_identifier ExecutionTrace::getNextOperationIdentifier() {
@@ -222,7 +226,6 @@ operation_identifier ExecutionTrace::getNextOperationIdentifier() {
222226
}
223227

224228
void ExecutionTrace::resetExecution() {
225-
// variableBitset.reset();
226229
currentBlockIndex = 0;
227230
currentOperationIndex = 0;
228231
globalTagMap.merge(localTagMap);

nautilus/src/nautilus/tracing/ExecutionTrace.hpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class ExecutionTrace {
2222

2323
void addCmpOperation(Snapshot& snapshot, value_ref inputs);
2424

25-
void destruct(value_ref inputs);
26-
2725
void addAssignmentOperation(Snapshot&, value_ref targetRef, value_ref srcRef, Type resultType);
2826

2927
void addReturn(Snapshot&, Type type, value_ref ref);
@@ -57,46 +55,35 @@ class ExecutionTrace {
5755
* @param blockIndex
5856
* @return Block&
5957
*/
60-
Block& getBlock(uint16_t blockIndex) {
61-
return blocks[blockIndex];
62-
}
58+
Block& getBlock(uint16_t blockIndex);
6359

6460
/**
6561
* @brief Returns a reference to all blocks
6662
* @return std::vector<Block>&
6763
*/
68-
std::vector<Block>& getBlocks() {
69-
return blocks;
70-
}
64+
std::vector<Block>& getBlocks();
7165

7266
/**
7367
* @brief Returns the index to the current block.
7468
* @return uint32_t
7569
*/
76-
uint16_t getCurrentBlockIndex() const {
77-
return currentBlockIndex;
78-
}
70+
uint16_t getCurrentBlockIndex() const;
7971

80-
void addOperation(Snapshot& snapshot, Op& operation, Type& resultType, nautilus::tracing::value_ref targetRef, nautilus::tracing::value_ref srcRef);
72+
void addOperation(Snapshot& snapshot, Op& operation, Type& resultType, value_ref targetRef, value_ref srcRef);
8173

8274
/**
8375
* @brief Returns the current block
8476
* @return Block&
8577
*/
86-
Block& getCurrentBlock() {
87-
return blocks[currentBlockIndex];
88-
}
78+
Block& getCurrentBlock();
8979

9080
TraceOperation& getCurrentOperation();
9181

9282
/**
9383
* @brief Sets the current block
9484
* @param index
9585
*/
96-
void setCurrentBlock(uint16_t index) {
97-
currentOperationIndex = 0;
98-
currentBlockIndex = index;
99-
}
86+
void setCurrentBlock(uint16_t index);
10087

10188
/**
10289
* @brief Processes a control flow merge

0 commit comments

Comments
 (0)