Skip to content

Add custom kernel name demangler #9134

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

donwalkarsoham
Copy link
Collaborator

@donwalkarsoham donwalkarsoham commented Aug 7, 2025

Problem solved by the commit

XRT was separate demangler APIs for Linux (GCC API) and Windows (UnDecorateSymbolName) while AIEBU has mangling implementation similar to the one on Linux.

Bug / issue (if any) fixed, which PR introduced the bug, how it was discovered

N/A

How problem was solved, alternative solutions (if any) and why they were rejected

  1. Add custom demangler in XRT common for both Linux and Windows platforms which matches the mangler in AIEBU.
  2. Currently, it supports only argument types char, void and int like AIEBU.
  3. The demangler is similar to the Itanium ABI style: _Z<length><name><types>
  • length : number of characters in the name string.
  • name : kernel name in string
  • types : kernel argument data type as below.
    • 'c' represents the arg is a char.
    • 'v' represents the arg is a void.
    • 'i' represents the arg is an int.
    • 'P' represents the arg is a pointer. Hence, "Pc" = char*, "Pv" = void*, "Pi" = int*
  1. Will be adding support for uint32_t and uint64_t later.

Risks (if any) associated the changes in the commit

  1. Break existing demangling on Linux

What has been tested and how, request additional testing if necessary

1> Successfully ran End to end test on aie4 on Windows and Linux as given below. (It also prints the mangled and demangled name)

1. Test on aie4 Windows with 3 args-

starting vadd thread number: 0
====== 0: vadd started =====
15820: Closing adapter 00000000400000405983770: -> mcdm::device::device(0x40000000)
6805810: hw_type is 5, is_iommu_disabled = 0
6840330: <- mcdm::device::device() this(0x23006370860) handle(0x40000080) adapter(0x40000000)
...
Mangled name: _Z3DPUPcPcPc
Demangled kname: DPU(char*, char*, char*)
...
19298676990: <- mcdm::device::device()
====== 0: vadd passed =====

2. Test onaie4 Linux with 3 args

root@qemuvm:~# ./advanaik/bins/bin/xrt_test 0
====== 0: npu3 xrt vadd started =====
837: PID(982): Created pcidev (0000:02:00.4)
...
mangled kname :_Z3DPUPcPcPc
demangled kname :DPU(char*, char*, char*)
...
19087159717: PID(982): Closed 3
====== 0: npu3 xrt vadd PASSED =====

3. Test onaie4 Linux with no arg

====== 1: npu3 xrt move memtiles started =====
688: PID(989): Created pcidev (0000:02:00.4)
...
mangled kname :_Z3DPU
demangled kname :DPU()
...
7751058106: PID(989): Closed 3
====== 1: npu3 xrt move memtiles PASSED =====

4. Test on aie2p Windows

[INFO] Host test code is created device object
[INFO] ===== Thread 0 Start =====
Host test will create kernel: DPU:dpu
Host code is creating hw_context...
----------- mangled name: _Z3DPUPcPcPcPcPc
----------- Demangled name: DPU(char*, char*, char*, char*, char*)
...
Thread 0, Iteration 0 Finished
===== Thread 0 Finished =====

5. Test on aie2p Linux

_Z3DPUPcPcPcPcPc
DPU(char*, char*, char*, char*, char*)

Documentation impact (if any)

N/A

@donwalkarsoham donwalkarsoham requested a review from stsoe as a code owner August 7, 2025 01:02
@gbuildx
Copy link
Collaborator

gbuildx commented Aug 7, 2025

donwalkarsoham - is not a collaborator
Can XRT admins please validate PR

@gbuildx
Copy link
Collaborator

gbuildx commented Aug 7, 2025

Can one of the admins verify this patch?

@donwalkarsoham donwalkarsoham changed the title Dev Add custom demangler in XRT Aug 7, 2025
@donwalkarsoham donwalkarsoham changed the title Add custom demangler in XRT Add custom demangler Aug 7, 2025
@donwalkarsoham donwalkarsoham changed the title Add custom demangler Add custom kernel name demangler Aug 7, 2025
Signed-off-by: Soham Donwalkar <[email protected]>
@gbuildx
Copy link
Collaborator

gbuildx commented Aug 7, 2025

donwalkarsoham - is not a collaborator
Can XRT admins please validate PR

{
//Check if mangled prefix "_Z" is present and length is greater than mangled_prefix_length
if (mangled.size() <= mangled_prefix_length || mangled.substr(0, mangled_prefix_length) != "_Z")
return "Not a mangled name";
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should throw instead of returning

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Copy link
Collaborator

@larry9523 larry9523 left a comment

Choose a reason for hiding this comment

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

Please throw instead of return on error

std::unique_ptr<char, decltype(&std::free)> demangled_name
(abi::__cxa_demangle(mangled_name.c_str(), nullptr, nullptr, &status), std::free);
if (idx >= s.size())
return "?";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this an error condition? If so, we should throw

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, added throw.

Copy link
Collaborator

@stsoe stsoe left a comment

Choose a reason for hiding this comment

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

It is possible to use GCC style mangling and write our own Windows mangler / demangler that follows the GCC style? I am asking because it would be useful to have elf files be compatible with available tools, e.g. c++filt?

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions



// map of mangled argument types to their demangled string representations
static const std::map<char, std::string> demangle_type_map = {
Copy link

Choose a reason for hiding this comment

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

warning: initialization of 'demangle_type_map' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]

static const std::map<char, std::string> demangle_type_map = {
                                         ^
Additional context

/usr/include/c++/13/bits/stl_map.h:239: possibly throwing constructor declared here

      map(initializer_list<value_type> __l,
      ^

Copy link
Collaborator

Choose a reason for hiding this comment

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

This clang-tidy warning is valid. Let's move this static std::map inside a function that implements the lookup.

std::string
get_demangle_type(char c)
{
  static std::map<...> ...;
  auto it = demangle_type_map.find(c);
  return (it != demangle_type_map.end()) 
    ? it->second 
    : "unknown";
}

@donwalkarsoham
Copy link
Collaborator Author

donwalkarsoham commented Aug 7, 2025

It is possible to use GCC style mangling and write our own Windows mangler / demangler that follows the GCC style? I am asking because it would be useful to have elf files be compatible with available tools, e.g. c++filt?

Yes, AIEBU and with this PR XRT will be using Itanium ABI style mangling which is a GCC style mangling independent of the platform (Windows and Linux). I checked online, c++filt supports demangling for Itanium ABI style mangling on Windows and Linux. If the windows system has a GCC or Clang installation on Windows (e.g., through MinGW-w64 or Cygwin) then it will have c++filt.
Thus ELFs generated using Itanium ABI style can be demangled using c++filt on Windows and Linux.

I checked on Linux using c++filt and and it was able to demangle correctly. This below mangling style is used by AIEBU and present in ELF. Note - AIEBU needs some change wrt to scalar types like int, char, uint32_t, etc. which I will be making after this PR gets merged.

% c++filt _Z3DPUPvPcPi
DPU(void*, char*, int*)

@donwalkarsoham donwalkarsoham requested a review from stsoe August 7, 2025 19:21
@stsoe
Copy link
Collaborator

stsoe commented Aug 7, 2025

It is possible to use GCC style mangling and write our own Windows mangler / demangler that follows the GCC style? I am asking because it would be useful to have elf files be compatible with available tools, e.g. c++filt?

Okay, that comment made no sense. We are only implementing demangle, mangling is in aiebu and is GCC style.



// map of mangled argument types to their demangled string representations
static const std::map<char, std::string> demangle_type_map = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This clang-tidy warning is valid. Let's move this static std::map inside a function that implements the lookup.

std::string
get_demangle_type(char c)
{
  static std::map<...> ...;
  auto it = demangle_type_map.find(c);
  return (it != demangle_type_map.end()) 
    ? it->second 
    : "unknown";
}

static std::string
demangle(const std::string& mangled_name)
demangle_arg_type(const std::string& s, size_t& idx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need this function. Leverage get_demangle_type and pass the char to get the type for. Leave string (s) and index (idx) with caller.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As per our discussion offline, made the changes.

Comment on lines 450 to 455
// Append "*" for pointer types
if (c == 'P')
return demangle_arg_type(s, idx) + "*";

auto it = demangle_type_map.find(c);
return (it != demangle_type_map.end()) ? it->second : "unknown";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Fold into get_demangle_type

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As per our discussion offline, made the changes.

return demangle_arg_type(s, idx) + "*";

auto it = demangle_type_map.find(c);
return (it != demangle_type_map.end()) ? it->second : "unknown";
Copy link
Collaborator

Choose a reason for hiding this comment

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

how does the caller deal with "unknown"

Copy link
Collaborator Author

@donwalkarsoham donwalkarsoham Aug 7, 2025

Choose a reason for hiding this comment

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

Currently, the function args will have unknown in it. I can throw and error even if one of the args are not mangled correctly.

@donwalkarsoham
Copy link
Collaborator Author

It is possible to use GCC style mangling and write our own Windows mangler / demangler that follows the GCC style? I am asking because it would be useful to have elf files be compatible with available tools, e.g. c++filt?

Okay, that comment made no sense. We are only implementing demangle, mangling is in aiebu and is GCC style.

Sorry if my response was confusing. Let me rephrase it.
AIEBU has its own implementation for mangling which follows GCC style (Itanium ABI) for pointer type arguments - https://github.com/Xilinx/aiebu/blob/49807d3f811933cacdab83094a6b1e08dc509ed7/src/cpp/preprocessor/preprocessor_input.h#L45

In this PR we are implementing demangling in XRT matching the same style which is again following GCC style(Itanium Style).
If your question was, if c++filt supports this type of mangling, then on online research it says that c++filt does supports this type of mangling.
c++filt can be used on Linux and Windows both. I tested on Linux and c++filt demangled the string as expected.
Please let me know if I answered your question correctly, or if I am missing something.

@stsoe
Copy link
Collaborator

stsoe commented Aug 7, 2025

It is possible to use GCC style mangling and write our own Windows mangler / demangler that follows the GCC style? I am asking because it would be useful to have elf files be compatible with available tools, e.g. c++filt?

Okay, that comment made no sense. We are only implementing demangle, mangling is in aiebu and is GCC style.

Sorry if my response was confusing. Let me rephrase it. AIEBU has its own implementation for mangling which follows GCC style (Itanium ABI) for pointer type arguments - https://github.com/Xilinx/aiebu/blob/49807d3f811933cacdab83094a6b1e08dc509ed7/src/cpp/preprocessor/preprocessor_input.h#L45

In this PR we are implementing demangling in XRT matching the same style which is again following GCC style(Itanium Style). If your question was, if c++filt supports this type of mangling, then on online research it says that c++filt does supports this type of mangling. c++filt can be used on Linux and Windows both. I tested on Linux and c++filt demangled the string as expected. Please let me know if I answered your question correctly, or if I am missing something.

Haha, my " didn't make sense" comment was comment to my own comment, not to yours which I hadn't seen when I wrote mine. :-)

@stsoe
Copy link
Collaborator

stsoe commented Aug 7, 2025

Please sign-off your commit(s)

@donwalkarsoham
Copy link
Collaborator Author

It is possible to use GCC style mangling and write our own Windows mangler / demangler that follows the GCC style? I am asking because it would be useful to have elf files be compatible with available tools, e.g. c++filt?

Okay, that comment made no sense. We are only implementing demangle, mangling is in aiebu and is GCC style.

Sorry if my response was confusing. Let me rephrase it. AIEBU has its own implementation for mangling which follows GCC style (Itanium ABI) for pointer type arguments - https://github.com/Xilinx/aiebu/blob/49807d3f811933cacdab83094a6b1e08dc509ed7/src/cpp/preprocessor/preprocessor_input.h#L45
In this PR we are implementing demangling in XRT matching the same style which is again following GCC style(Itanium Style). If your question was, if c++filt supports this type of mangling, then on online research it says that c++filt does supports this type of mangling. c++filt can be used on Linux and Windows both. I tested on Linux and c++filt demangled the string as expected. Please let me know if I answered your question correctly, or if I am missing something.

Haha, my " didn't make sense" comment was comment to my own comment, not to yours which I hadn't seen when I wrote mine. :-)

Haha ok, got it :-)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

if (it == demangle_type_map.end())
throw std::runtime_error("Unknown type character in mangled name: " + std::string(1, c));

return it->second;
Copy link

Choose a reason for hiding this comment

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

warning: misleading indentation; statement is not part of the previous 'if' [clang-diagnostic-misleading-indentation]

    return it->second;
    ^
Additional context

src/runtime_src/core/common/api/xrt_module.cpp:453: previous statement is here

  if (it == demangle_type_map.end())
  ^

if (it == demangle_type_map.end())
throw std::runtime_error("Unknown type character in mangled name: " + std::string(1, c));

return it->second;
Copy link
Collaborator

Choose a reason for hiding this comment

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

ident?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

Copy link

github-actions bot commented Aug 7, 2025

clang-tidy review says "All clean, LGTM! 👍"

@donwalkarsoham donwalkarsoham self-assigned this Aug 7, 2025
Comment on lines +492 to +493
while (idx < mangled.size())
args.push_back(get_demangled_type(mangled, idx));
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about

Suggested change
while (idx < mangled.size())
args.push_back(get_demangled_type(mangled, idx));
while (idx < mangled.size())
args.push_back(get_demangled_type(mangled[idx++]));

or simpler maybe:

for (auto c : mangled)
  args.push_back(get_demangled_type(c));

static std::string
demangle(const std::string& mangled_name)
get_demangled_type(const std::string& s, size_t& idx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does this function need s and idx.? Can't it just take char c. It feels wrong that this function updates idx. I imagine caller should do get_mangled_type(name[idx++])

Signed-off-by: Soham Donwalkar <[email protected]>
Signed-off-by: Soham Donwalkar <[email protected]>
Signed-off-by: Soham Donwalkar <[email protected]>
Copy link

github-actions bot commented Aug 7, 2025

clang-tidy review says "All clean, LGTM! 👍"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants