Skip to content

Commit

Permalink
refactor: remove spdlog
Browse files Browse the repository at this point in the history
  • Loading branch information
dkales committed Dec 18, 2023
1 parent 97d9264 commit e7045be
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 73 deletions.
1 change: 0 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
pkgs.git
pkgs.ripgrep
pkgs.protobuf3_20
pkgs.spdlog
pkgs.zlib
pkgs.zstd
pkgs.libxml2
Expand Down
85 changes: 45 additions & 40 deletions mlir-assigner/include/mlir-assigner/helper/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,92 +33,97 @@
#include <string_view>
#include <llvm/IR/Instructions.h>

#include <spdlog/spdlog.h>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/format.hpp>

namespace nil {
namespace blueprint {
class logger {
public:
enum class level {
INFO,
DEBUG,
ERROR,
};
logger(boost::log::trivial::severity_level lvl = boost::log::trivial::info)
: lvl(lvl) {
boost::log::core::get()->set_filter(boost::log::trivial::severity >= lvl);
}

logger() {
// spdlog::set_pattern("%L %v");
void set_level(boost::log::trivial::severity_level lvl) {
this->lvl = lvl;
boost::log::core::get()->set_filter(boost::log::trivial::severity >= lvl);
}

spdlog::set_pattern("[%H:%M:%S %z] [%^-%L-%$] %v");
spdlog::set_level(spdlog::level::info);
template <typename... Args> void trace(const char *fmt, const Args &...args) {
// https://stackoverflow.com/questions/25859672/boostformat-with-variadic-template-arguments
BOOST_LOG_TRIVIAL(trace) << boost::str((boost::format(fmt) % ... % args));
}

void set_level(level lvl) {
this->lvl = lvl;
switch (lvl) {
case level::DEBUG:
spdlog::set_level(spdlog::level::debug);
break;
case level::INFO:
spdlog::set_level(spdlog::level::info);
break;
case level::ERROR:
spdlog::set_level(spdlog::level::err);
break;
}
template <typename... Args> void debug(const char *fmt, const Args &...args) {
// https://stackoverflow.com/questions/25859672/boostformat-with-variadic-template-arguments
BOOST_LOG_TRIVIAL(debug) << boost::str((boost::format(fmt) % ... % args));
}

template <typename... Args> void info(const char *fmt, const Args &...args) {
// https://stackoverflow.com/questions/25859672/boostformat-with-variadic-template-arguments
BOOST_LOG_TRIVIAL(info) << boost::str((boost::format(fmt) % ... % args));
}

template <typename... Args> void error(const char *fmt, const Args &...args) {
// https://stackoverflow.com/questions/25859672/boostformat-with-variadic-template-arguments
BOOST_LOG_TRIVIAL(error) << boost::str((boost::format(fmt) % ... % args));
}

template <typename... Args>
void debug(std::string_view fmt, const Args &...args) {
spdlog::debug(fmt, args...);
// TODO: these two functions can be substituted by one when std::format is
// widely supported
void debug(boost::basic_format<char> formated_debug_message) {
BOOST_LOG_TRIVIAL(debug) << boost::str(formated_debug_message);
}

template <typename... Args>
void error(std::string_view fmt, const Args &...args) {
spdlog::error(fmt, args...);
void debug(std::string_view debug_message) {
BOOST_LOG_TRIVIAL(debug) << debug_message;
}

void log_value(const mlir::Value &Value) {
if (lvl < level::DEBUG) {
if (lvl > boost::log::trivial::debug) {
return;
}
std::string str;
llvm::raw_string_ostream ss(str);
ss << Value;
spdlog::debug(str);
BOOST_LOG_TRIVIAL(debug) << str;
}

void log_affine_map(const mlir::AffineMap &AffineMap) {
if (lvl < level::DEBUG) {
if (lvl > boost::log::trivial::debug) {
return;
}
std::string str;
llvm::raw_string_ostream ss(str);
ss << AffineMap;
spdlog::debug(str);
BOOST_LOG_TRIVIAL(debug) << str;
}

void log_attribute(const mlir::Attribute &Attr) {
if (lvl < level::DEBUG) {
if (lvl > boost::log::trivial::debug) {
return;
}
std::string str;
llvm::raw_string_ostream ss(str);
ss << Attr;
spdlog::debug(str);
BOOST_LOG_TRIVIAL(debug) << str;
}

template <typename T> void operator<<(const T &Val) {
if (lvl < level::DEBUG) {
if (lvl > boost::log::trivial::debug) {
return;
}
std::string str;
llvm::raw_string_ostream ss(str);
ss << Val;
spdlog::debug("{}", str);
BOOST_LOG_TRIVIAL(debug) << str;
}

template <typename T> void operator<<(const llvm::ArrayRef<T> &Val) {
if (lvl < level::DEBUG) {
if (lvl > boost::log::trivial::debug) {
return;
}
std::string str;
Expand All @@ -128,11 +133,11 @@ class logger {
ss << V << ", ";
}
ss << "]";
spdlog::debug("{}", str);
BOOST_LOG_TRIVIAL(debug) << str;
}

private:
level lvl = level::INFO;
boost::log::trivial::severity_level lvl;
};
} // namespace blueprint
} // namespace nil
Expand Down
34 changes: 16 additions & 18 deletions mlir-assigner/include/mlir-assigner/parser/evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
#include <mlir-assigner/memory/stack_frame.hpp>
#include <mlir-assigner/parser/input_reader.hpp>

#include <spdlog/common.h>
#include <unordered_map>
#include <map>
#include <unistd.h>
Expand Down Expand Up @@ -193,23 +192,22 @@ class evaluator {
// atm handle only simple loops with one region,block and argument
assert(op.getRegion().hasOneBlock());
assert(op.getRegion().getArguments().size() == 1);
logger.debug("for ({0:d} -> {1:d} step {2:d})", from, to, step);
logger.trace("for (%d -> %d step %d)", from, to, step);
llvm::hash_code counterHash = mlir::hash_value(op.getInductionVar());
logger.debug("inserting hash: {0:x}:{1:d}", std::size_t(counterHash), from);
logger.trace("inserting hash: %x:%d", std::size_t(counterHash), from);
auto res = frames.back().constant_values.insert({counterHash, from});
assert(res.second); // we do not want overrides here, since we delete it
// after loop this should never happen
while (from < to) {
handleRegion(op.getLoopBody());
from += step;
logger.debug("updating hash: {0:x}:{1:d}", std::size_t(counterHash),
from);
logger.trace("updating hash: %x:%d", std::size_t(counterHash), from);
frames.back().constant_values[counterHash] = from;
logger.debug("{0:d} -> {1:d}", from, to);
logger.debug("for done! go next iteration..");
logger.trace("%d -> %d", from, to);
logger.trace("for done! go next iteration..");
}
frames.back().constant_values.erase(counterHash);
logger.debug("deleting: {0:x}", std::size_t(counterHash));
logger.trace("deleting: %x", std::size_t(counterHash));
}

int64_t evaluateForParameter(AffineMap &affineMap,
Expand All @@ -221,11 +219,11 @@ class evaluator {
llvm::SmallVector<int64_t> inVector(affineMap.getNumInputs());
for (unsigned i = 0; i < affineMap.getNumInputs(); ++i) {
llvm::hash_code hash = mlir::hash_value(operands[i]);
logger.debug("looking for: {0:x}", std::size_t(hash));
logger.trace("looking for: %x", std::size_t(hash));
if (frames.back().constant_values.find(hash) ==
frames.back().constant_values.end()) {
logger.log_affine_map(affineMap);
logger.error("CANNOT FIND {0:x}",
logger.error("CANNOT FIND %x",
std::size_t(mlir::hash_value(operands[i])));
exit(-1);
} else {
Expand Down Expand Up @@ -385,7 +383,7 @@ class evaluator {
// Print the operation itself and some of its properties
// Print the operation attributes
std::string opName = op->getName().getIdentifier().str();
logger.debug("visiting {}", opName);
logger.debug("visiting %s", opName);
if (AffineForOp operation = llvm::dyn_cast<AffineForOp>(op)) {
logger.debug("visiting affine for!");
assert(op->getAttrs().size() == 3);
Expand Down Expand Up @@ -426,7 +424,7 @@ class evaluator {
} else if (AffineStoreOp operation = llvm::dyn_cast<AffineStoreOp>(op)) {
// affine.store
auto memRefHash = mlir::hash_value(operation.getMemref());
logger.debug("looking for MemRef {0:x}", size_t(memRefHash));
logger.debug("looking for MemRef %x", size_t(memRefHash));
auto memref = frames.back().memrefs.find(memRefHash);
assert(memref != frames.back().memrefs.end());

Expand Down Expand Up @@ -507,7 +505,7 @@ class evaluator {
// Print the operation itself and some of its properties
// Print the operation attributes
std::string opName = op->getName().getIdentifier().str();
logger.debug("visiting {}", opName);
logger.debug("visiting %s", opName);
if (KrnlGlobalOp operation = llvm::dyn_cast<KrnlGlobalOp>(op)) {
logger.debug("global op");
logger << operation;
Expand Down Expand Up @@ -636,7 +634,7 @@ class evaluator {
mlir::MemRefType MemRefType = mlir::cast<mlir::MemRefType>(lhs.getType());
assert(MemRefType.getShape().size() == 1 &&
"DotProduct must have tensors of rank 1");
logger.debug("computing DotProduct with {0:d} x {0:d}",
logger.debug("computing DotProduct with %d x %d",
MemRefType.getShape().back());
handle_fixedpoint_dot_product_component(operation, zero_var,
frames.back(), bp, assignmnt);
Expand Down Expand Up @@ -678,7 +676,7 @@ class evaluator {
auto insert_res = frames.back().memrefs.insert({hash, m});
assert(insert_res.second); // Reallocating over an existing memref
// should not happen ATM
logger.debug("inserting memref with hash {0:x}", size_t(hash));
logger.debug("inserting memref with hash %x", size_t(hash));
} else if (memref::AllocaOp operation =
llvm::dyn_cast<memref::AllocaOp>(op)) {
// TACEO_TODO: handle cleanup of these stack memrefs
Expand Down Expand Up @@ -720,7 +718,7 @@ class evaluator {
llvm::dyn_cast<memref::StoreOp>(op)) {
// TODO: deduplicate with affine.load
auto memRefHash = mlir::hash_value(operation.getMemref());
logger.debug("looking for MemRef {0:x}", size_t(memRefHash));
logger.debug("looking for MemRef %x", size_t(memRefHash));
auto memref = frames.back().memrefs.find(memRefHash);
assert(memref != frames.back().memrefs.end());

Expand Down Expand Up @@ -774,8 +772,8 @@ class evaluator {
}

void handleOperation(Operation *op) {
logger.debug("visiting operation: {}", op->getName().getIdentifier().str());
logger.debug("current start row: {}", assignmnt.allocated_rows());
logger.debug("visiting operation: %s", op->getName().getIdentifier().str());
logger.debug("current start row: %d", assignmnt.allocated_rows());
Dialect *dial = op->getDialect();
if (!dial) {
logger.error("Encountered an unregistered Dialect");
Expand Down
12 changes: 1 addition & 11 deletions mlir-assigner/include/mlir-assigner/parser/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,7 @@ struct parser {
throw std::runtime_error("Currently only one prover is supported, please "
"set max_num_provers to 1");
}
switch (log_level) {
case boost::log::trivial::severity_level::info:
log.set_level(logger::level::INFO);
break;
case boost::log::trivial::severity_level::debug:
log.set_level(logger::level::DEBUG);
break;
case boost::log::trivial::severity_level::error:
default:
log.set_level(logger::level::ERROR);
}
log.set_level(log_level);
detail::PolicyManager::set_policy(kind);

onnx_mlir::registerDialects(context);
Expand Down
4 changes: 1 addition & 3 deletions mlir-assigner/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ elseif(CMAKE_CROSSCOMPILING)
endif()
endif()

find_package(spdlog REQUIRED)

string(TOUPPER ${CURRENT_PROJECT_NAME} UPPER_CURRENT_PROJECT_NAME)

# get header files; only needed by CMake generators,
Expand Down Expand Up @@ -76,7 +74,7 @@ target_link_libraries(${CURRENT_PROJECT_NAME}
marshalling::crypto3_zk

${Boost_LIBRARIES}
spdlog::spdlog_header_only)
)


target_include_directories(${CURRENT_PROJECT_NAME} PUBLIC
Expand Down

0 comments on commit e7045be

Please sign in to comment.