Nvc++ / g++ bitfield incompatibility

Hi everyone,

I’m observing a strange behaviour when I try to call a function located in a shared library compiled with g++ from another program compiled with nvc++. The parameters passed to a called function seem to get corrupted during the function call.

Here you can find a minimal example showing off this behaviour. The example code uses g++-9 to compile a shared library and nvc++ to compile the main.

test.zip (2.4 KB)

compile commands

mkdir build
cd build
cmake ..
make -j
./src/main

The expected output is ###### pid 42 kid 0 but I actually get ###### pid <some_random_number> kid 42 with changing results on every run.

Here is a short explanation of what the code does:

The class myclass is compiled with g++ to create a shared library providing the static method myclass::foo.

myclass.hpp

[...]
class myclass {
    public:
    struct {
        uint64_t a : 24;
        uint64_t b : 32;
        uint64_t c : 8;
    } fields;
    static myclass foo(uint8_t pid, uint32_t key_id);
};

myclass.cpp

[...]
myclass myclass::foo(uint8_t pid, uint32_t key_id)
{
    std::cout << "###### pid " << int(pid) << " kid " << int(key_id) << std::endl;
    myclass result;
    return result;
}

The main function is compiled with nvc++ and calls foo.

main.cpp

int main(int argc, char* argv[]) {
    auto var = 42;
    auto tmp = myclass::foo(var, 0);
    return 0;
}

As far as I can tell, the problem occurs only if the static function is not void and is located behind a bitfield with specific bit widths. When I compile both, the library and the main, with the same compiler (no matter if g++ or nvc++) everything works as expected. Unfortunately for the project in which I ran into those problems, we are not able change the library toolchain to nvc++ due to other requirements.

Is anyone able to reproduce this behaviour with the given example project?
Are there known bugs that could be related to this?
Or am I just missing some obvious undefined behaviour causing stuff here?

Thank you and best regards
Josh

Edit:
nvc++ version : 23.1-0
g++ version : 9.4.0

Thanks for the report Josh. I’ve filed an issue report, TPR #36572, and sent it to engineering for investigation.

I tired the code with multiple versions of g++ and nvc++, as well as several systems. The compiler version don’t seem to matter, but the error does only occur on x86_64, it seems to get correct results on ARM.

Hi Mat, thank you for your effort. I forgot to mention that I ran the program on x86_64, so that fits your tests. I will try to work around this issue for now and I’m looking forward to the results of the engineering.

Hi Mat,
is there an update on this bug and could you give me a status update on your issue report? In another project we ran into a similar problem when linking non-nvc++ libraries against nvc+±compiled binaries.

Thank you and best regards
Josh

Hi Josh,

Looks like a fix made it into 25.1 which was just released last week.

-Mat

1 Like

Hi Mat, thanks for the fast reply. What a timing, I will test the new version asap. Thank you for your support :)

I just tested two projects with the new HPC-SDK and I was unable to reproduce the errors. Great. For the record, we had the same problems linking Qt5 qwt libraries with nvc++, now it works without any issues.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.