Skip to content

Assembly fragment fails to compile with clang -O3 #215

@bredelings

Description

@bredelings

Hi Marc,

I have found that the inline assembly fragment in BeagleCPUSSEPlugin.cpp fails to compile on linux with clang.

#else // HAVE_CPUID.H
	// Determine if cpuid supported:
    unsigned int res;
    __asm__("mov %%ecx, %%eax;"
            "xor $200000, %%eax;"
            "xor %%ecx, %%eax;"
            "je no;"
            "mov $1, %%eax;"
            "jmp end;"
            "no: mov $0, %%eax;"
            "end:;"
            : "=a" (res)
            :
            : "cc");

The compilation error looks like this:

$ clang++-16 -DBEAGLE_ACTION -DPACKAGE_BUGREPORT=\"beagle-dev@googlegroups.com\" -DPACKAGE_NAME=\"libhmsbeagle\" -DPACKAGE_STRING="\"libhmsbeagle 4.0.1\"" -DPACKAGE_TARNAME=\"libhmsbeagle\" -DPACKAGE_URL=\"\" -DPACKAGE_VERSION=\"4.0.1\" -DPLUGIN_VERSION=\"40\" -Dhmsbeagle_cpu_sse_EXPORTS -I/home/bredelings/Devel/beagle/git -I/include -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux -O3 -g -pthread -march=native -std=gnu++17 -fPIC -MD -MT libhmsbeagle/CPU/CMakeFiles/hmsbeagle-cpu-sse.dir/BeagleCPUSSEPlugin.cpp.o -MF libhmsbeagle/CPU/CMakeFiles/hmsbeagle-cpu-sse.dir/BeagleCPUSSEPlugin.cpp.o.d -o libhmsbeagle/CPU/CMakeFiles/hmsbeagle-cpu-sse.dir/BeagleCPUSSEPlugin.cpp.o -c /home/bredelings/Devel/beagle/git/libhmsbeagle/CPU/BeagleCPUSSEPlugin.cpp
/home/bredelings/Devel/beagle/git/libhmsbeagle/CPU/BeagleCPUSSEPlugin.cpp:122:13: error: symbol 'no' is already defined
    __asm__("mov %%ecx, %%eax;"
            ^
<inline asm>:1:77: note: instantiated into assembly here
        mov %ecx, %eax;xor $200000, %eax;xor %ecx, %eax;je no;mov $1, %eax;jmp end;no: mov $0, %eax;end:;
                                                                                   ^
/home/bredelings/Devel/beagle/git/libhmsbeagle/CPU/BeagleCPUSSEPlugin.cpp:122:13: error: symbol 'end' is already defined
    __asm__("mov %%ecx, %%eax;"
            ^
<inline asm>:1:94: note: instantiated into assembly here
        mov %ecx, %eax;xor $200000, %eax;xor %ecx, %eax;je no;mov $1, %eax;jmp end;no: mov $0, %eax;end:;
                                                                                                    ^
2 errors generated.

It looks like the code is being inlined into multiple places when optimization is enabled, yielding multiple instances of each assembly label.

However, it also seems like resorting to inline assembly is not needed here. The code only tries to use inline assembly when HAVE_CPUID_H is not defined. Now, we actually do have <cpuid.h>, and if I add -DHAVE_CPUID_H, then the code does compile.

It looks like HAVE_CPUID_H used to be defined, but then became undefined with config.h was removed.

The thing that occurs to me is to make cmake look for <cpuid.h> and then define HAVE_CPUID_H if we fine it. Do you have any thoughts on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions