Skip to content

Commit

Permalink
use fmt instead of sstream
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrulich committed Oct 4, 2024
1 parent 122bdef commit 40a7d8c
Show file tree
Hide file tree
Showing 985 changed files with 9,593 additions and 5,948 deletions.
1 change: 1 addition & 0 deletions nautilus/include/nautilus/common/config.h.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#cmakedefine ENABLE_TRACING
#cmakedefine ENABLE_LOGGING
#cmakedefine ENABLE_BC_BACKEND
#cmakedefine ENABLE_C_BACKEND
#cmakedefine ENABLE_MLIR_BACKEND
Expand Down
2 changes: 0 additions & 2 deletions nautilus/include/nautilus/tracing/TypedValueRef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ struct TypedValueRef {
bool operator>=(const TypedValueRef& rhs) const {
return !(*this < rhs);
}

[[nodiscard]] std::string toString() const;
};

class TypedValueRefHolder {
Expand Down
2 changes: 1 addition & 1 deletion nautilus/src/nautilus/compiler/DumpHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DumpHandler {
[[nodiscard]] bool dumpToConsole() const;
[[nodiscard]] bool dumpToFile() const;
const engine::Options& options;
const CompilationUnitID& id;
[[maybe_unused]] const CompilationUnitID& id;
const std::filesystem::path rootPath;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ void BCLoweringProvider::RegisterProvider::freeRegister() {

std::tuple<Code, RegisterFile> BCLoweringProvider::LoweringContext::process() {
defaultRegisterFile.fill(0);
auto functionOperation = ir->getRootOperation();
const auto& functionOperation = ir->getRootOperation();
RegisterFrame rootFrame;
auto functionBasicBlock = functionOperation->getFunctionBasicBlock();
for (auto i = 0ull; i < functionBasicBlock->getArguments().size(); i++) {
auto& argument = functionBasicBlock->getArguments()[i];
const auto& functionBasicBlock = functionOperation.getFunctionBasicBlock();
for (auto i = 0ull; i < functionBasicBlock.getArguments().size(); i++) {
auto& argument = functionBasicBlock.getArguments()[i];
auto argumentRegister = registerProvider.allocRegister();
rootFrame.setValue(argument->getIdentifier(), argumentRegister);
program.arguments.emplace_back(argumentRegister);
}
this->process(functionBasicBlock, rootFrame);
this->process(&functionBasicBlock, rootFrame);
// NES_INFO("Allocated Registers: " <<
// this->registerProvider.allocRegister());
return std::make_tuple(program, defaultRegisterFile);
Expand Down Expand Up @@ -944,7 +944,7 @@ void BCLoweringProvider::LoweringContext::process(ir::ShiftOperation* shiftOpera
program.blocks[block].code.emplace_back(oc);
}

void BCLoweringProvider::LoweringContext::process(ir::BasicBlockInvocation& bi, short block, RegisterFrame& parentFrame) {
void BCLoweringProvider::LoweringContext::process(const ir::BasicBlockInvocation& bi, short block, RegisterFrame& parentFrame) {
auto blockInputArguments = bi.getArguments();
auto& blockTargetArguments = bi.getBlock()->getArguments();
std::vector<short> tempArgs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class BCLoweringProvider {

void process(ir::BranchOperation* opt, short block, RegisterFrame& frame);

void process(ir::BasicBlockInvocation& opt, short block, RegisterFrame& frame);
void process(const ir::BasicBlockInvocation& opt, short block, RegisterFrame& frame);

void process(ir::LoadOperation* opt, short block, RegisterFrame& frame);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ std::string CPPLoweringProvider::LoweringContext::getType(const Type& stamp) {

std::stringstream CPPLoweringProvider::LoweringContext::process() {

auto functionOperation = ir->getRootOperation();
const auto& functionOperation = ir->getRootOperation();
RegisterFrame rootFrame;
std::vector<std::string> arguments;
auto functionBasicBlock = functionOperation->getFunctionBasicBlock();
for (auto i = 0ull; i < functionBasicBlock->getArguments().size(); i++) {
auto argument = functionBasicBlock->getArguments()[i].get();
const auto& functionBasicBlock = functionOperation.getFunctionBasicBlock();
for (auto i = 0ull; i < functionBasicBlock.getArguments().size(); i++) {
auto argument = functionBasicBlock.getArguments()[i].get();
auto var = getVariable(argument->getIdentifier());
rootFrame.setValue(argument->getIdentifier(), var);
arguments.emplace_back(getType(argument->getStamp()) + " " + var);
}
this->process(functionBasicBlock, rootFrame);
this->process(&functionBasicBlock, rootFrame);

std::stringstream pipelineCode;
pipelineCode << "\n";
Expand Down Expand Up @@ -179,7 +179,7 @@ void CPPLoweringProvider::LoweringContext::process(ir::StoreOperation* storeOp,
blocks[blockIndex] << "*reinterpret_cast<" << type << "*>(" << address << ") = " << value << ";\n";
}

void CPPLoweringProvider::LoweringContext::process(ir::BasicBlockInvocation& bi, short blockIndex, RegisterFrame& parentFrame) {
void CPPLoweringProvider::LoweringContext::process(const ir::BasicBlockInvocation& bi, short blockIndex, RegisterFrame& parentFrame) {
auto blockInputArguments = bi.getArguments();
auto& blockTargetArguments = bi.getBlock()->getArguments();
blocks[blockIndex] << "// prepare block arguments\n";
Expand Down Expand Up @@ -351,8 +351,7 @@ void CPPLoweringProvider::LoweringContext::process(const std::unique_ptr<ir::Ope
return;
}
default: {
throw NotImplementedException("Operation is not implemented:" + opt->toString());
return;
throw NotImplementedException("Operation is not implemented");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CPPLoweringProvider {

std::string process(const ir::BasicBlock*, RegisterFrame& frame);

void process(ir::BasicBlockInvocation& opt, short block, RegisterFrame& frame);
void process(const ir::BasicBlockInvocation& opt, short block, RegisterFrame& frame);

void process(const std::unique_ptr<ir::Operation>& operation, short block, RegisterFrame& frame);

Expand Down Expand Up @@ -97,13 +97,11 @@ class CPPLoweringProvider {
blockArguments << getType(constValue->getStamp()) << " " << var << ";\n";
frame.setValue(constValue->getIdentifier(), var);

blocks[blockIndex] << var << " = (" << getType(constValue->getStamp()) << ")" << constValue->getValue()
<< ";\n";
blocks[blockIndex] << var << " = (" << getType(constValue->getStamp()) << ")" << constValue->getValue() << ";\n";
}

template <class Type>
void processBinary(const std::unique_ptr<ir::Operation>& o, const std::string& operation, short blockIndex,
RegisterFrame& frame) {
void processBinary(const std::unique_ptr<ir::Operation>& o, const std::string& operation, short blockIndex, RegisterFrame& frame) {
auto op = static_cast<Type*>(o.get());
auto leftInput = frame.getValue(op->getLeftInput()->getIdentifier());
auto rightInput = frame.getValue(op->getRightInput()->getIdentifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void MLIRLoweringProvider::generateMLIR(const ir::BasicBlock* basicBlock, ValueF
void MLIRLoweringProvider::generateMLIR(const std::unique_ptr<ir::Operation>& operation, ValueFrame& frame) {
switch (operation->getOperationType()) {
case ir::Operation::OperationType::FunctionOp:
generateMLIR(as<ir::FunctionOperation>(operation), frame);
// generateMLIR(as<ir::FunctionOperation>(operation), frame);
break;
case ir::Operation::OperationType::ConstIntOp:
generateMLIR(as<ir::ConstIntOperation>(operation), frame);
Expand Down Expand Up @@ -356,23 +356,23 @@ void MLIRLoweringProvider::generateMLIR(ir::AndOperation* andOperation, ValueFra
);
}

void MLIRLoweringProvider::generateMLIR(ir::FunctionOperation* functionOp, ValueFrame& frame) {
void MLIRLoweringProvider::generateMLIR(const ir::FunctionOperation& functionOp, ValueFrame& frame) {
// Generate execute function. Set input/output types and get its entry block.
llvm::SmallVector<mlir::Type> inputTypes(0);
for (auto& inputArg : functionOp->getFunctionBasicBlock()->getArguments()) {
for (auto& inputArg : functionOp.getFunctionBasicBlock().getArguments()) {
inputTypes.emplace_back(getMLIRType(inputArg->getStamp()));
}
llvm::SmallVector<mlir::Type> outputTypes(1, getMLIRType(functionOp->getOutputArg()));
llvm::SmallVector<mlir::Type> outputTypes(1, getMLIRType(functionOp.getOutputArg()));
;
auto functionInOutTypes = builder->getFunctionType(inputTypes, outputTypes);
auto loc = getNameLoc("EntryPoint");
auto mlirFunction = builder->create<mlir::func::FuncOp>(loc, functionOp->getName(), functionInOutTypes);
auto mlirFunction = builder->create<mlir::func::FuncOp>(loc, functionOp.getName(), functionInOutTypes);

// Avoid function name mangling.
mlirFunction->setAttr("llvm.emit_c_interface", mlir::UnitAttr::get(context));
if (isUnsignedInteger(functionOp->getStamp())) {
if (isUnsignedInteger(functionOp.getStamp())) {
mlirFunction.setResultAttr(0, "llvm.zeroext", mlir::UnitAttr::get(context));
} else if (isSignedInteger(functionOp->getStamp())) {
} else if (isSignedInteger(functionOp.getStamp())) {
mlirFunction.setResultAttr(0, "llvm.signext", mlir::UnitAttr::get(context));
}
// mlirFunction.setArgAttr(0, "llvm.signext", mlir::UnitAttr::get(context));
Expand All @@ -395,14 +395,14 @@ void MLIRLoweringProvider::generateMLIR(ir::FunctionOperation* functionOp, Value

// Store references to function args in the valueMap map.
auto valueMapIterator = mlirFunction.args_begin();
for (int i = 0; i < (int) functionOp->getFunctionBasicBlock()->getArguments().size(); ++i) {
frame.setValue(functionOp->getFunctionBasicBlock()->getArguments().at(i)->getIdentifier(), valueMapIterator[i]
for (int i = 0; i < (int) functionOp.getFunctionBasicBlock().getArguments().size(); ++i) {
frame.setValue(functionOp.getFunctionBasicBlock().getArguments().at(i)->getIdentifier(), valueMapIterator[i]

);
}

// Generate MLIR for operations in function body (BasicBlock).
generateMLIR(functionOp->getFunctionBasicBlock(), frame);
generateMLIR(&functionOp.getFunctionBasicBlock(), frame);

theModule.push_back(mlirFunction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class MLIRLoweringProvider {
*/
void generateMLIR(const std::unique_ptr<ir::Operation>& operation, ValueFrame& frame);

void generateMLIR(ir::FunctionOperation* funcOp, ValueFrame& frame);
void generateMLIR(const ir::FunctionOperation& funcOp, ValueFrame& frame);

void generateMLIR(ir::ConstIntOperation* constIntOp, ValueFrame& frame);

Expand Down Expand Up @@ -147,9 +147,7 @@ class MLIRLoweringProvider {
* @param varArgs: Include variable arguments.
* @return FlatSymbolRefAttr: Reference to function used in CallOps.
*/
::mlir::FlatSymbolRefAttr insertExternalFunction(const std::string& name, void* functionPtr,
::mlir::Type resultType, std::vector<::mlir::Type> argTypes,
bool varArgs);
::mlir::FlatSymbolRefAttr insertExternalFunction(const std::string& name, void* functionPtr, ::mlir::Type resultType, std::vector<::mlir::Type> argTypes, bool varArgs);

/**
* @brief Generates a Name(d)Loc(ation) that is attached to the operation.
Expand Down
2 changes: 1 addition & 1 deletion nautilus/src/nautilus/compiler/ir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ add_subdirectory(operations)
add_subdirectory(phases)
#add_subdirectory(Types)

add_source_files(nautilus IRGraph.cpp IRDumpHandler.cpp)
add_source_files(nautilus IRGraph.cpp)
87 changes: 0 additions & 87 deletions nautilus/src/nautilus/compiler/ir/IRDumpHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,87 +0,0 @@

#include "nautilus/compiler/ir/IRDumpHandler.hpp"
#include "nautilus/compiler/ir/operations/BranchOperation.hpp"
#include "nautilus/compiler/ir/operations/FunctionOperation.hpp"
#include "nautilus/compiler/ir/operations/IfOperation.hpp"
#include "nautilus/compiler/ir/operations/Operation.hpp"
#include <iostream>

namespace nautilus::compiler::ir {

NESIRDumpHandler::~NESIRDumpHandler() = default;

NESIRDumpHandler::NESIRDumpHandler(std::ostream& out) : out(out) {
}

std::shared_ptr<NESIRDumpHandler> NESIRDumpHandler::create(std::ostream& out) {
return std::make_shared<NESIRDumpHandler>(out);
}

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());
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());
} else {
return getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock());
}
} else { // ReturnOp todo changed #3234
return nullptr;
}
}

void NESIRDumpHandler::dumpHelper(Operation* terminatorOp) {
switch (terminatorOp->getOperationType()) {
case Operation::OperationType::BranchOp: {
auto branchOp = static_cast<BranchOperation*>(terminatorOp);
dumpHelper(branchOp->getNextBlockInvocation().getBlock());
break;
}
case Operation::OperationType::IfOp: {
auto ifOp = static_cast<IfOperation*>(terminatorOp);
auto lastTerminatorOp = getNextLowerOrEqualLevelBasicBlock(ifOp->getTrueBlockInvocation().getBlock());
dumpHelper(ifOp->getTrueBlockInvocation().getBlock());
dumpHelper(ifOp->getFalseBlockInvocation().getBlock());
if (lastTerminatorOp) {
dumpHelper(lastTerminatorOp);
}
break;
}
case Operation::OperationType::ReturnOp:
break;
default:
break;
}
}

void NESIRDumpHandler::dumpHelper(const BasicBlock* basicBlock) {
if (!visitedBlocks.contains(basicBlock->getIdentifier())) {
// int32_t indent = basicBlock->getScopeLevel() + 1;
visitedBlocks.emplace(basicBlock->getIdentifier());
out << '\n' << "Block_" << basicBlock->getIdentifier() << '(';
if (basicBlock->getArguments().size() > 0) {
out << basicBlock->getArguments().at(0)->getIdentifier().toString() << ":" << toString(basicBlock->getArguments().at(0)->getStamp());
for (int i = 1; i < (int) basicBlock->getArguments().size(); ++i) {
out << ", " << basicBlock->getArguments().at(i)->getIdentifier().toString() << ":" << toString(basicBlock->getArguments().at(i)->getStamp());
}
}
out << "):" << '\n';
for (auto& operation : basicBlock->getOperations()) {
out << std::string(4, ' ') << operation->toString() << " :" << toString(operation->getStamp()) << std::endl;
}
auto& terminatorOp = basicBlock->getOperations().back();
dumpHelper(terminatorOp.get());
}
}

void NESIRDumpHandler::dump(const std::unique_ptr<FunctionOperation>& funcOp) {
out << funcOp->toString() << " {";
dumpHelper(funcOp->getFunctionBasicBlock());
out << "}\n";
}

} // namespace nautilus::compiler::ir
58 changes: 0 additions & 58 deletions nautilus/src/nautilus/compiler/ir/IRDumpHandler.hpp
Original file line number Diff line number Diff line change
@@ -1,58 +0,0 @@
#pragma once

#include "nautilus/compiler/ir/IRGraph.hpp"
#include "nautilus/compiler/ir/blocks/BasicBlock.hpp"
#include <memory>
#include <unordered_set>

namespace nautilus::compiler::ir {

/**
* @brief Converts query plans and pipeline plans to the .nesviz format and dumps them to a file.m
*/
class NESIRDumpHandler {

public:
virtual ~NESIRDumpHandler();

static std::shared_ptr<NESIRDumpHandler> create(std::ostream& out);

explicit NESIRDumpHandler(std::ostream& out);

/**
* @brief Dump the NESIR of the funcOp into the 'out' stringstream.
* @param funcOp: FunctionOperation that exists on the top level of a NESIR module.
*/
void dump(const std::unique_ptr<FunctionOperation>& funcOp);

private:
std::ostream& out;
std::unordered_set<std::string> visitedBlocks; // We keep track of visited blocks to avoid multi or infinite
// dumping.

/**
* @brief Traverses the NESIR to find a BB that is on the same or higher 'blockScopeLevel' compared to the initial
* 'basicBlock'. Note: There is always a 'next block', since we always have a return block at the very end of a
* function.
* @param basicBlock: Initially the block that we want to find the next BB for. Replaced while recursively
* traversing NESIR.
*/
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);

/**
* @brief Handle dumping BasicBlocks to the 'out' stringstream. Print all operations, then handle the terminatorOp.
*
* @param basicBlock: The basicBlock that is dumped to the 'out' stringstream.
*/
void dumpHelper(const BasicBlock* basicBlock);
};

} // namespace nautilus::compiler::ir
Loading

0 comments on commit 40a7d8c

Please sign in to comment.