Skip to content
Open
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
73 changes: 73 additions & 0 deletions src/cpp/common/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: MIT
// Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.

#ifndef AIEBU_COMMON_LOGGER_H
#define AIEBU_COMMON_LOGGER_H

#include <iostream>
#include <sstream>

namespace aiebu {

// Log Levels
enum class log_level {
error = 0, // Always shown
warn = 1, // Warnings
info = 2, // Informational messages (shown with verbose)
debug = 3 // Debug messages (shown with verbose)
};

// Global log level (default: errors and warnings)
inline log_level g_log_level = log_level::warn;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'g_log_level' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

inline log_level g_log_level = log_level::warn;
                 ^


// Set log level
inline void set_log_level(log_level level) {
g_log_level = level;
}

// Get log level
inline log_level get_log_level() {
return g_log_level;
}

// Enable verbose mode (shows info and debug messages)
inline void enable_verbose_logging() {
g_log_level = log_level::debug;
}

// Disable verbose mode (back to default: errors and warnings)
inline void disable_verbose_logging() {
g_log_level = log_level::warn;
}

} // namespace aiebu

// Logging macros
#define LOG_ERROR(msg) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: function-like macro 'LOG_ERROR' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]

#define LOG_ERROR(msg) \
        ^

do { \
std::cerr << "[ERROR] " << msg << std::endl; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]

Suggested change
std::cerr << "[ERROR] " << msg << std::endl; \
std::cerr << "[ERROR] " << (msg) << std::endl; \

} while (0)

#define LOG_WARN(msg) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: function-like macro 'LOG_WARN' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]

#define LOG_WARN(msg) \
        ^

do { \
if (aiebu::g_log_level >= aiebu::log_level::warn) { \
std::cout << "[WARN] " << msg << std::endl; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]

Suggested change
std::cout << "[WARN] " << msg << std::endl; \
std::cout << "[WARN] " << (msg) << std::endl; \

} \
} while (0)

#define LOG_INFO(msg) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: function-like macro 'LOG_INFO' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]

#define LOG_INFO(msg) \
        ^

do { \
if (aiebu::g_log_level >= aiebu::log_level::info) { \
std::cout << msg << std::endl; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]

Suggested change
std::cout << msg << std::endl; \
std::cout << (msg) << std::endl; \

} \
} while (0)

#define LOG_DEBUG(msg) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: function-like macro 'LOG_DEBUG' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]

#define LOG_DEBUG(msg) \
        ^

do { \
if (aiebu::g_log_level >= aiebu::log_level::debug) { \
std::cout << "[DEBUG] " << msg << std::endl; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]

Suggested change
std::cout << "[DEBUG] " << msg << std::endl; \
std::cout << "[DEBUG] " << (msg) << std::endl; \

} \
} while (0)

#endif // AIEBU_COMMON_LOGGER_H

3 changes: 2 additions & 1 deletion src/cpp/elf/elfwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.

#include "elfwriter.h"
#include "logger.h"

namespace aiebu {

Expand Down Expand Up @@ -118,7 +119,7 @@ elf_writer::
finalize()
{
add_note(NT_XRT_UID, ".note.xrt.UID", m_uid.calculate());
std::cout << "UID:" << m_uid.str() << "\n";
LOG_INFO("UID:" << m_uid.str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]

  LOG_INFO("UID:" << m_uid.str());
  ^
Additional context

src/cpp/common/logger.h:58: expanded from macro 'LOG_INFO'

  do { \
  ^

std::stringstream stream;
stream << std::noskipws;
//m_elfio.save( "hello_32" );
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/encoder/aie2ps/aie2ps_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "aie2ps_encoder.h"

#include "aiebu/aiebu_error.h"
#include "logger.h"

#include <cassert>

Expand Down Expand Up @@ -79,6 +80,10 @@ process(std::shared_ptr<preprocessed_output> input)
}
}

// Report (only if verbose flag is set or log level is info or higher)
if (get_log_level() >= log_level::info)
m_report.summary(std::cout);

// Debug JSON serialization
json dbg_json = m_debug.to_json();

Expand Down
3 changes: 3 additions & 0 deletions src/cpp/preprocessor/aie2/aie2_blob_preprocessor_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "utils.h"
#include "file_utils.h"
#include "preprocessor_input.h"
#include "asm/asm_parser.h"
#include <boost/format.hpp>
#include <boost/property_tree/json_parser.hpp>

Expand Down Expand Up @@ -105,6 +106,8 @@ class aie2_blob_preprocessor_input : public preprocessor_input
{
if (lib == legacydpuxclbin)
arg_offset = 1;
else if (lib == "verbose")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how you are passing this flag from aiebu-asm, that code is missing

enable_verbose_logging();
else
std::cout << "Invalid flag: " << lib << ", ignored !!!" << std::endl;
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message for invalid flags should use LOG_WARN instead of std::cout for consistency with the new logging system.

Suggested change
std::cout << "Invalid flag: " << lib << ", ignored !!!" << std::endl;
LOG_WARN("Invalid flag: %s, ignored !!!", lib.c_str());

Copilot uses AI. Check for mistakes.
}
Expand Down
26 changes: 17 additions & 9 deletions src/cpp/preprocessor/aie2ps/aie2ps_preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class aie2ps_preprocessor: public preprocessor
{
const std::string disable_dump_map = "disabledump";
const std::string full_dump_map = "fulldump";
const std::string verbose_asm = "verbose";
std::shared_ptr<std::map<std::string, std::shared_ptr<isa_op>>> m_isa;
public:
aie2ps_preprocessor() {}
Expand All @@ -45,25 +46,32 @@ class aie2ps_preprocessor: public preprocessor
{
//auto keys = tinput->get_keys();
const std::string prefix = "opt_level_";
std::shared_ptr<asm_parser> parser(new asm_parser(tinput->get_ctrlcode_data(), tinput->get_include_paths()));
parser->parse_lines();
auto collist = parser->get_col_list();
isa i;
uint32_t optimize = 0;
m_isa = i.get_isamap();
auto toutput = std::make_shared<aie2ps_preprocessed_output>(parser->get_partition_info());

// Process flags before parsing to set global verbose flag
auto flags = tinput->get_flags();
uint32_t optimize = 0;
asm_dump_flag debug_flag = asm_dump_flag::text;
for (const auto& flag: flags)
{
if (flag == disable_dump_map)
toutput->set_debug(asm_dump_flag::disable);
debug_flag = asm_dump_flag::disable;
else if (flag == full_dump_map)
toutput->set_debug(asm_dump_flag::full);
debug_flag = asm_dump_flag::full;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont need verbose.

      if (flag == disable_dump_map)
        debug_flag = asm_dump_flag::disable;
      else if (flag == full_dump_map) {
        debug_flag = asm_dump_flag::full;
        enable_verbose_logging();
      }

else if (flag == verbose_asm)
enable_verbose_logging();
else if (flag.find(prefix) == 0)
optimize = std::stoi(flag.substr(prefix.size()));
else
std::cout << "Invalid flag: " << flag << ", ignored !!!" << std::endl;
}
Comment on lines 51 to 66
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message for invalid flags (line 65) should use LOG_WARN instead of std::cout for consistency with the new logging system.

Copilot uses AI. Check for mistakes.

std::shared_ptr<asm_parser> parser(new asm_parser(tinput->get_ctrlcode_data(), tinput->get_include_paths()));
parser->parse_lines();
auto collist = parser->get_col_list();
isa i;
m_isa = i.get_isamap();
auto toutput = std::make_shared<aie2ps_preprocessed_output>(parser->get_partition_info());
toutput->set_debug(debug_flag);
auto& controlpkts = tinput->get_controlpkt();
auto& ctrlpkt_id_map = tinput->get_ctrlpkt_id_map();
toutput->set_optmization(optimize);
Expand Down
15 changes: 7 additions & 8 deletions src/cpp/preprocessor/asm/asm_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ operate(std::shared_ptr<asm_parser> parserptr, const smatch& sm)
else if (is_annotation_section(args[0]))
m_parserptr->set_annotation_state();
else
std::cout << "section directive with unknown section found:" << args[0] << std::endl;
LOG_WARN("section directive with unknown section found:" << args[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]

    LOG_WARN("section directive with unknown section found:" << args[0]);
    ^
Additional context

src/cpp/common/logger.h:51: expanded from macro 'LOG_WARN'

  do { \
  ^

}

void
Expand All @@ -229,18 +229,17 @@ operate(std::shared_ptr<asm_parser> parserptr, const smatch& sm)
m_parserptr = parserptr;
static const regex pattern(R"(\.partition\s+(\d+)(column|core:(\d+)mem))");
smatch match;
// LOG_INFO("PARTITION:" << sm[0].str());
std::cout << "PARTITION:" << sm[0].str() << std::endl;
LOG_INFO("PARTITION:" << sm[0].str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]

  LOG_INFO("PARTITION:" << sm[0].str());
  ^
Additional context

src/cpp/common/logger.h:58: expanded from macro 'LOG_INFO'

  do { \
  ^

std::string line = sm[0].str();
if (regex_match(line, match, pattern)) {
if (match[2] == "column") {
std::cout << "Column count: " << match[1] << std::endl;
LOG_INFO("Column count: " << match[1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]

      LOG_INFO("Column count: " << match[1]);
      ^
Additional context

src/cpp/common/logger.h:58: expanded from macro 'LOG_INFO'

  do { \
  ^

m_parserptr->set_numcolumn(to_uinteger<uint32_t>(match[1]));
} else {
m_parserptr->set_numcore(to_uinteger<uint32_t>(match[1]));
m_parserptr->set_nummem(to_uinteger<uint32_t>(match[3]));
std::cout << "Core count: " << match[1] << std::endl;
std::cout << "Memory size: " << match[3] << std::endl;
LOG_INFO("Core count: " << match[1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]

      LOG_INFO("Core count: " << match[1]);
      ^
Additional context

src/cpp/common/logger.h:58: expanded from macro 'LOG_INFO'

  do { \
  ^

LOG_INFO("Memory size: " << match[3]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]

      LOG_INFO("Memory size: " << match[3]);
      ^
Additional context

src/cpp/common/logger.h:58: expanded from macro 'LOG_INFO'

  do { \
  ^

}
} else
throw error(error::error_code::invalid_asm, "Invalid format!! " + line + "\n");
Expand All @@ -254,7 +253,7 @@ read_include_file(std::string filename)
if (!file.is_open()) {
return false;
}
// std::cout << "Reading file:" << filename << std::endl;
LOG_INFO("Reading file:" << filename);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]

  LOG_INFO("Reading file:" << filename);
  ^
Additional context

src/cpp/common/logger.h:58: expanded from macro 'LOG_INFO'

  do { \
  ^

std::string line;
m_parserptr->set_data_state(false);

Expand Down Expand Up @@ -369,7 +368,7 @@ read_pad_file(std::string& name, std::string& filename)
return false;
}

// std::cout << "Reading file:" << filename << std::endl;
LOG_INFO("Reading file:" << filename);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]

  LOG_INFO("Reading file:" << filename);
  ^
Additional context

src/cpp/common/logger.h:58: expanded from macro 'LOG_INFO'

  do { \
  ^

std::string line;
m_parserptr->set_data_state(false);

Expand Down
1 change: 1 addition & 0 deletions src/cpp/preprocessor/asm/asm_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "code_section.h"
#include "utils.h"
#include "file_utils.h"
#include "logger.h"
#include "common/regex_wrapper.h"

#include <map>
Expand Down
41 changes: 39 additions & 2 deletions src/cpp/utils/target/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "target.h"
#include "utils.h"
#include "file_utils.h"
#include "logger.h"

std::map<uint32_t, std::vector<char> >
aiebu::utilities::
Expand Down Expand Up @@ -338,13 +339,15 @@ target_aie4::assemble(const sub_cmd_options &_options)

cxxopts::Options all_options("Target aie4 Options", m_description);

std::string log_level_str;
try {
all_options.add_options()
("outputelf,o", "ELF output file name", cxxopts::value<decltype(output_elffile)>())
("asm,c", "ASM File", cxxopts::value<decltype(input_file)>())
("j,json", "control packet Patching json file", cxxopts::value<decltype(external_buffers_file)>())
("L,libpath", "libs path", cxxopts::value<decltype(libpaths)>())
("f,flag", "flags", cxxopts::value<decltype(flags)>())
("f,flag", "flags (e.g., 'verbose' for detailed output, 'disabledump', 'fulldump')", cxxopts::value<decltype(flags)>())
("log-level", "log level: error, warn, info, debug (default: warn)", cxxopts::value<decltype(log_level_str)>())
("help,h", "show help message and exit", cxxopts::value<bool>()->default_value("false"))
;

Expand Down Expand Up @@ -379,6 +382,22 @@ target_aie4::assemble(const sub_cmd_options &_options)
if (result.count("json"))
external_buffers_file = result["json"].as<decltype(external_buffers_file)>();

if (result.count("log-level")) {
log_level_str = result["log-level"].as<decltype(log_level_str)>();
if (log_level_str == "error")
aiebu::set_log_level(aiebu::log_level::error);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how you will set log_level when user is calling lib apis?

else if (log_level_str == "warn")
aiebu::set_log_level(aiebu::log_level::warn);
else if (log_level_str == "info")
aiebu::set_log_level(aiebu::log_level::info);
else if (log_level_str == "debug")
aiebu::set_log_level(aiebu::log_level::debug);
else {
auto errMsg = boost::format("Invalid log level: %s. Valid options: error, warn, info, debug\n") % log_level_str;
throw std::runtime_error(errMsg.str());
}
}
Comment on lines +385 to +399
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log level validation logic is duplicated in both the aie4 target (lines 385-399) and config target (lines 475-489). Consider extracting this validation into a reusable helper function to reduce code duplication and improve maintainability.

Copilot uses AI. Check for mistakes.

}
catch (const cxxopts::exceptions::exception& e) {
std::cout << all_options.help({"", "Target aie4 Options"});
Expand Down Expand Up @@ -408,15 +427,17 @@ aiebu::utilities::
asm_config_parser::parser(const sub_cmd_options &options)
{
std::string json_file;
std::string log_level_str;
cxxopts::Options all_options("Target config Options", m_description);
uint32_t optimization_level =0;

try {
all_options.add_options()
("o,outputelf", "ELF output file name", cxxopts::value<decltype(output_elffile)>())
("j,json", "control packet Patching json file", cxxopts::value<decltype(json_file)>())
("f,flag", "flags", cxxopts::value<decltype(flags)>())
("f,flag", "flags (e.g., 'verbose' for detailed output, 'disabledump', 'fulldump')", cxxopts::value<decltype(flags)>())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont need verbose here, we can merge verbose functionality with fulldump for aie2ps/aie4/aie2ps_config/aie4_config

("O,optimization", "optimization level (1-4)", cxxopts::value<int>()->default_value("0"))
("log-level", "log level: error, warn, info, debug (default: warn)", cxxopts::value<decltype(log_level_str)>())
("h,help", "show help message and exit", cxxopts::value<bool>()->default_value("false"))
;

Expand Down Expand Up @@ -451,6 +472,22 @@ asm_config_parser::parser(const sub_cmd_options &options)
flags.insert(flags.end(), extra_flags.begin(), extra_flags.end());
}

if (result.count("log-level")) {
log_level_str = result["log-level"].as<decltype(log_level_str)>();
if (log_level_str == "error")
aiebu::set_log_level(aiebu::log_level::error);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

else if (log_level_str == "warn")
aiebu::set_log_level(aiebu::log_level::warn);
else if (log_level_str == "info")
aiebu::set_log_level(aiebu::log_level::info);
else if (log_level_str == "debug")
aiebu::set_log_level(aiebu::log_level::debug);
else {
auto errMsg = boost::format("Invalid log level: %s. Valid options: error, warn, info, debug\n") % log_level_str;
throw std::runtime_error(errMsg.str());
}
}

}
catch (const cxxopts::exceptions::exception& e) {
std::cout << all_options.help({"", "Target config Options"});
Expand Down
3 changes: 2 additions & 1 deletion src/cpp/utils/target/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "aiebu/aiebu_assembler.h"
#include "aiebu/aiebu_error.h"
#include "logger.h"

#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -41,7 +42,7 @@ class target
inline void write_elf(const aiebu::aiebu_assembler& as, const std::string& outfile)
{
auto e = as.get_elf();
std::cout << "elf size:" << e.size() << "\n";
LOG_INFO("elf size:" << e.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: avoid do-while loops [cppcoreguidelines-avoid-do-while]

    LOG_INFO("elf size:" << e.size());
    ^
Additional context

src/cpp/common/logger.h:58: expanded from macro 'LOG_INFO'

  do { \
  ^

std::ofstream output_file(outfile, std::ios_base::binary);
output_file.write(e.data(), e.size());
}
Expand Down