Skip to content
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

refactor trace to ir conversion phase #53

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions nautilus/src/nautilus/compiler/ir/IRDumpHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,24 @@ std::shared_ptr<NESIRDumpHandler> NESIRDumpHandler::create(std::ostream& out) {
return std::make_shared<NESIRDumpHandler>(out);
}

const BasicBlock* NESIRDumpHandler::getNextLowerOrEqualLevelBasicBlock(const BasicBlock* thenBlock, int ifParentBlockLevel) {
const BasicBlock* NESIRDumpHandler::getNextLowerOrEqualLevelBasicBlock(const BasicBlock* thenBlock) {
auto& terminatorOp = thenBlock->getOperations().back();
if (terminatorOp->getOperationType() == Operation::OperationType::BranchOp) {
auto branchOp = dynamic_cast<BranchOperation*>(terminatorOp.get());
if (branchOp->getNextBlockInvocation().getBlock()->getScopeLevel() <= (uint32_t) ifParentBlockLevel) {
return branchOp->getNextBlockInvocation().getBlock();
} else {
return getNextLowerOrEqualLevelBasicBlock(branchOp->getNextBlockInvocation().getBlock(), ifParentBlockLevel);
}
return getNextLowerOrEqualLevelBasicBlock(branchOp->getNextBlockInvocation().getBlock());
} else if (terminatorOp->getOperationType() == Operation::OperationType::IfOp) {
auto ifOp = dynamic_cast<IfOperation*>(terminatorOp.get());
if (ifOp->getFalseBlockInvocation().getBlock() != nullptr) {
return getNextLowerOrEqualLevelBasicBlock(ifOp->getFalseBlockInvocation().getBlock(), ifParentBlockLevel);
return getNextLowerOrEqualLevelBasicBlock(ifOp->getFalseBlockInvocation().getBlock());
} else {
return getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock(), ifParentBlockLevel);
return getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock());
}
} else { // ReturnOp todo changed #3234
return nullptr;
}
}

void NESIRDumpHandler::dumpHelper(Operation* terminatorOp, int32_t) {
void NESIRDumpHandler::dumpHelper(Operation* terminatorOp) {
switch (terminatorOp->getOperationType()) {
case Operation::OperationType::BranchOp: {
auto branchOp = static_cast<BranchOperation*>(terminatorOp);
Expand All @@ -47,8 +43,7 @@ void NESIRDumpHandler::dumpHelper(Operation* terminatorOp, int32_t) {
}
case Operation::OperationType::IfOp: {
auto ifOp = static_cast<IfOperation*>(terminatorOp);
auto lastTerminatorOp = getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock(),
ifOp->getTrueBlockInvocation().getBlock()->getScopeLevel() - 1); // todo can lead to error #3234
auto lastTerminatorOp = getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock());
dumpHelper(ifOp->getTrueBlockInvocation().getBlock());
dumpHelper(ifOp->getFalseBlockInvocation().getBlock());
if (lastTerminatorOp) {
Expand Down Expand Up @@ -79,7 +74,7 @@ void NESIRDumpHandler::dumpHelper(const BasicBlock* basicBlock) {
out << std::string(4, ' ') << operation->toString() << " :" << toString(operation->getStamp()) << std::endl;
}
auto& terminatorOp = basicBlock->getOperations().back();
dumpHelper(terminatorOp.get(), basicBlock->getScopeLevel());
dumpHelper(terminatorOp.get());
}
}

Expand Down
6 changes: 2 additions & 4 deletions nautilus/src/nautilus/compiler/ir/IRDumpHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@ class NESIRDumpHandler {
* function.
* @param basicBlock: Initially the block that we want to find the next BB for. Replaced while recursively
* traversing NESIR.
* @param blockScopeLevel: The scopeLevel of the initial BB that we are searching the next same/higher level BB for.
* @return IR::BasicBlockPtr: SharedPtr to the next block that resides on the same or on a higher level.
*/
const BasicBlock* getNextLowerOrEqualLevelBasicBlock(const BasicBlock* basicBlock, int blockScopeLevel);
const BasicBlock* getNextLowerOrEqualLevelBasicBlock(const BasicBlock* basicBlock);

/**
* @brief Handle dumping terminator operations(LoopOp, BranchOp, IfOp, ReturnOp) to the 'out' stringstream.
*
* @param terminatorOp: Terminator operation that we append to the 'out' stringstream.
* @param scopeLevel: scopeLevel of the BasicBlock that is terminated by the terminator operation.
*/
void dumpHelper(Operation* terminatorOp, int32_t scopeLevel);
void dumpHelper(Operation* terminatorOp);

/**
* @brief Handle dumping BasicBlocks to the 'out' stringstream. Print all operations, then handle the terminatorOp.
Expand Down
33 changes: 4 additions & 29 deletions nautilus/src/nautilus/compiler/ir/blocks/BasicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#include <utility>

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

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

BasicBlock::~BasicBlock() = default;

const std::string& BasicBlock::getIdentifier() const {
return identifier;
}

void BasicBlock::setIdentifier(const std::string& identifier) {
this->identifier = identifier;
}

uint32_t BasicBlock::getScopeLevel() const {
return scopeLevel;
}

void BasicBlock::setScopeLevel(uint32_t scopeLevel) {
this->scopeLevel = scopeLevel;
}

uint32_t BasicBlock::getNumLoopBackEdges() {
return numLoopBackEdges;
}

void BasicBlock::incrementNumLoopBackEdge() {
++this->numLoopBackEdges;
}

bool BasicBlock::isLoopHeaderBlock() {
return numLoopBackEdges > 0;
const std::string BasicBlock::getIdentifier() const {
return std::to_string(identifier);
}

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

void BasicBlock::replaceTerminatorOperation(Operation* loopOperation) {
operations.pop_back();
operations.emplace_back(std::move(loopOperation));
operations.emplace_back(loopOperation);
}

BasicBlock* BasicBlock::addOperation(std::unique_ptr<Operation> operation) {
Expand Down
32 changes: 3 additions & 29 deletions nautilus/src/nautilus/compiler/ir/blocks/BasicBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,11 @@ class BasicBlock {
* @param Operations: A list of Operations that are executed in the BasicBlock.
* @param nextBlocks : The BasicBlock that is next in the control flow of the execution.
*/
explicit BasicBlock(const std::string& identifier, int32_t scopeLevel,
std::vector<std::unique_ptr<Operation>>& operations,
std::vector<std::unique_ptr<BasicBlockArgument>>& arguments);
explicit BasicBlock(uint16_t identifier, std::vector<std::unique_ptr<BasicBlockArgument>>& arguments);

virtual ~BasicBlock();

[[nodiscard]] const std::string& getIdentifier() const;

void setIdentifier(const std::string& identifier);

[[nodiscard]] uint32_t getScopeLevel() const;

void setScopeLevel(uint32_t scopeLevel);

/**
* @brief Get the number of edges that lead back from the loop body to the loop header.
*/
[[nodiscard]] uint32_t getNumLoopBackEdges();

/**
* @brief Increment counter for edges that lead back from the loop body to the loop header.
*/
void incrementNumLoopBackEdge();

/**
* @brief Check if the counter for edges that lead back from the loop body to the loop header is > 0.
*/
[[nodiscard]] bool isLoopHeaderBlock();
[[nodiscard]] const std::string getIdentifier() const;

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

Expand Down Expand Up @@ -83,15 +60,12 @@ class BasicBlock {

uint64_t getIndexOfArgument(Operation* arg);

// void popOperation();
void replaceTerminatorOperation(Operation* newTerminatorOperation);

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

private:
std::string identifier;
uint32_t scopeLevel;
uint32_t numLoopBackEdges;
uint16_t identifier;
std::vector<std::unique_ptr<Operation>> operations;
std::vector<std::unique_ptr<BasicBlockArgument>> arguments;
std::vector<BasicBlock*> predecessors;
Expand Down
Loading