Skip to content

Commit b79795f

Browse files
refactor trace to ir conversion phase (#53)
1 parent cc7e9e7 commit b79795f

File tree

6 files changed

+137
-352
lines changed

6 files changed

+137
-352
lines changed

nautilus/src/nautilus/compiler/ir/IRDumpHandler.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,24 @@ std::shared_ptr<NESIRDumpHandler> NESIRDumpHandler::create(std::ostream& out) {
1717
return std::make_shared<NESIRDumpHandler>(out);
1818
}
1919

20-
const BasicBlock* NESIRDumpHandler::getNextLowerOrEqualLevelBasicBlock(const BasicBlock* thenBlock, int ifParentBlockLevel) {
20+
const BasicBlock* NESIRDumpHandler::getNextLowerOrEqualLevelBasicBlock(const BasicBlock* thenBlock) {
2121
auto& terminatorOp = thenBlock->getOperations().back();
2222
if (terminatorOp->getOperationType() == Operation::OperationType::BranchOp) {
2323
auto branchOp = dynamic_cast<BranchOperation*>(terminatorOp.get());
24-
if (branchOp->getNextBlockInvocation().getBlock()->getScopeLevel() <= (uint32_t) ifParentBlockLevel) {
25-
return branchOp->getNextBlockInvocation().getBlock();
26-
} else {
27-
return getNextLowerOrEqualLevelBasicBlock(branchOp->getNextBlockInvocation().getBlock(), ifParentBlockLevel);
28-
}
24+
return getNextLowerOrEqualLevelBasicBlock(branchOp->getNextBlockInvocation().getBlock());
2925
} else if (terminatorOp->getOperationType() == Operation::OperationType::IfOp) {
3026
auto ifOp = dynamic_cast<IfOperation*>(terminatorOp.get());
3127
if (ifOp->getFalseBlockInvocation().getBlock() != nullptr) {
32-
return getNextLowerOrEqualLevelBasicBlock(ifOp->getFalseBlockInvocation().getBlock(), ifParentBlockLevel);
28+
return getNextLowerOrEqualLevelBasicBlock(ifOp->getFalseBlockInvocation().getBlock());
3329
} else {
34-
return getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock(), ifParentBlockLevel);
30+
return getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock());
3531
}
3632
} else { // ReturnOp todo changed #3234
3733
return nullptr;
3834
}
3935
}
4036

41-
void NESIRDumpHandler::dumpHelper(Operation* terminatorOp, int32_t) {
37+
void NESIRDumpHandler::dumpHelper(Operation* terminatorOp) {
4238
switch (terminatorOp->getOperationType()) {
4339
case Operation::OperationType::BranchOp: {
4440
auto branchOp = static_cast<BranchOperation*>(terminatorOp);
@@ -47,8 +43,7 @@ void NESIRDumpHandler::dumpHelper(Operation* terminatorOp, int32_t) {
4743
}
4844
case Operation::OperationType::IfOp: {
4945
auto ifOp = static_cast<IfOperation*>(terminatorOp);
50-
auto lastTerminatorOp = getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock(),
51-
ifOp->getTrueBlockInvocation().getBlock()->getScopeLevel() - 1); // todo can lead to error #3234
46+
auto lastTerminatorOp = getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock());
5247
dumpHelper(ifOp->getTrueBlockInvocation().getBlock());
5348
dumpHelper(ifOp->getFalseBlockInvocation().getBlock());
5449
if (lastTerminatorOp) {
@@ -79,7 +74,7 @@ void NESIRDumpHandler::dumpHelper(const BasicBlock* basicBlock) {
7974
out << std::string(4, ' ') << operation->toString() << " :" << toString(operation->getStamp()) << std::endl;
8075
}
8176
auto& terminatorOp = basicBlock->getOperations().back();
82-
dumpHelper(terminatorOp.get(), basicBlock->getScopeLevel());
77+
dumpHelper(terminatorOp.get());
8378
}
8479
}
8580

nautilus/src/nautilus/compiler/ir/IRDumpHandler.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@ class NESIRDumpHandler {
3636
* function.
3737
* @param basicBlock: Initially the block that we want to find the next BB for. Replaced while recursively
3838
* traversing NESIR.
39-
* @param blockScopeLevel: The scopeLevel of the initial BB that we are searching the next same/higher level BB for.
40-
* @return IR::BasicBlockPtr: SharedPtr to the next block that resides on the same or on a higher level.
4139
*/
42-
const BasicBlock* getNextLowerOrEqualLevelBasicBlock(const BasicBlock* basicBlock, int blockScopeLevel);
40+
const BasicBlock* getNextLowerOrEqualLevelBasicBlock(const BasicBlock* basicBlock);
4341

4442
/**
4543
* @brief Handle dumping terminator operations(LoopOp, BranchOp, IfOp, ReturnOp) to the 'out' stringstream.
4644
*
4745
* @param terminatorOp: Terminator operation that we append to the 'out' stringstream.
4846
* @param scopeLevel: scopeLevel of the BasicBlock that is terminated by the terminator operation.
4947
*/
50-
void dumpHelper(Operation* terminatorOp, int32_t scopeLevel);
48+
void dumpHelper(Operation* terminatorOp);
5149

5250
/**
5351
* @brief Handle dumping BasicBlocks to the 'out' stringstream. Print all operations, then handle the terminatorOp.

nautilus/src/nautilus/compiler/ir/blocks/BasicBlock.cpp

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
#include <utility>
88

99
namespace nautilus::compiler::ir {
10-
BasicBlock::BasicBlock(const std::string& identifier, int32_t scopeLevel, std::vector<std::unique_ptr<Operation>>& operations, std::vector<std::unique_ptr<BasicBlockArgument>>& arguments)
11-
: identifier(identifier), scopeLevel(scopeLevel), numLoopBackEdges(0), operations(std::move(operations)), arguments(std::move(arguments)) {
10+
BasicBlock::BasicBlock(uint16_t identifier, std::vector<std::unique_ptr<BasicBlockArgument>>& arguments) : identifier(identifier), operations(), arguments(std::move(arguments)) {
1211
}
1312

1413
void BasicBlock::addNextBlock(BasicBlock* nextBlock, const std::vector<Operation*>& ops) {
@@ -27,32 +26,8 @@ void BasicBlock::addNextBlock(BasicBlock* nextBlock, const std::vector<Operation
2726

2827
BasicBlock::~BasicBlock() = default;
2928

30-
const std::string& BasicBlock::getIdentifier() const {
31-
return identifier;
32-
}
33-
34-
void BasicBlock::setIdentifier(const std::string& identifier) {
35-
this->identifier = identifier;
36-
}
37-
38-
uint32_t BasicBlock::getScopeLevel() const {
39-
return scopeLevel;
40-
}
41-
42-
void BasicBlock::setScopeLevel(uint32_t scopeLevel) {
43-
this->scopeLevel = scopeLevel;
44-
}
45-
46-
uint32_t BasicBlock::getNumLoopBackEdges() {
47-
return numLoopBackEdges;
48-
}
49-
50-
void BasicBlock::incrementNumLoopBackEdge() {
51-
++this->numLoopBackEdges;
52-
}
53-
54-
bool BasicBlock::isLoopHeaderBlock() {
55-
return numLoopBackEdges > 0;
29+
const std::string BasicBlock::getIdentifier() const {
30+
return std::to_string(identifier);
5631
}
5732

5833
const std::vector<std::unique_ptr<Operation>>& BasicBlock::getOperations() const {
@@ -82,7 +57,7 @@ uint64_t BasicBlock::getIndexOfArgument(Operation* arg) {
8257

8358
void BasicBlock::replaceTerminatorOperation(Operation* loopOperation) {
8459
operations.pop_back();
85-
operations.emplace_back(std::move(loopOperation));
60+
operations.emplace_back(loopOperation);
8661
}
8762

8863
BasicBlock* BasicBlock::addOperation(std::unique_ptr<Operation> operation) {

nautilus/src/nautilus/compiler/ir/blocks/BasicBlock.hpp

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,11 @@ class BasicBlock {
1515
* @param Operations: A list of Operations that are executed in the BasicBlock.
1616
* @param nextBlocks : The BasicBlock that is next in the control flow of the execution.
1717
*/
18-
explicit BasicBlock(const std::string& identifier, int32_t scopeLevel,
19-
std::vector<std::unique_ptr<Operation>>& operations,
20-
std::vector<std::unique_ptr<BasicBlockArgument>>& arguments);
18+
explicit BasicBlock(uint16_t identifier, std::vector<std::unique_ptr<BasicBlockArgument>>& arguments);
2119

2220
virtual ~BasicBlock();
2321

24-
[[nodiscard]] const std::string& getIdentifier() const;
25-
26-
void setIdentifier(const std::string& identifier);
27-
28-
[[nodiscard]] uint32_t getScopeLevel() const;
29-
30-
void setScopeLevel(uint32_t scopeLevel);
31-
32-
/**
33-
* @brief Get the number of edges that lead back from the loop body to the loop header.
34-
*/
35-
[[nodiscard]] uint32_t getNumLoopBackEdges();
36-
37-
/**
38-
* @brief Increment counter for edges that lead back from the loop body to the loop header.
39-
*/
40-
void incrementNumLoopBackEdge();
41-
42-
/**
43-
* @brief Check if the counter for edges that lead back from the loop body to the loop header is > 0.
44-
*/
45-
[[nodiscard]] bool isLoopHeaderBlock();
22+
[[nodiscard]] const std::string getIdentifier() const;
4623

4724
[[nodiscard]] const std::vector<std::unique_ptr<Operation>>& getOperations() const;
4825

@@ -83,15 +60,12 @@ class BasicBlock {
8360

8461
uint64_t getIndexOfArgument(Operation* arg);
8562

86-
// void popOperation();
8763
void replaceTerminatorOperation(Operation* newTerminatorOperation);
8864

8965
[[nodiscard]] std::pair<const BasicBlock*, const BasicBlock*> getNextBlocks();
9066

9167
private:
92-
std::string identifier;
93-
uint32_t scopeLevel;
94-
uint32_t numLoopBackEdges;
68+
uint16_t identifier;
9569
std::vector<std::unique_ptr<Operation>> operations;
9670
std::vector<std::unique_ptr<BasicBlockArgument>> arguments;
9771
std::vector<BasicBlock*> predecessors;

0 commit comments

Comments
 (0)