Skip to content

[Windows] Support microsoft intrinsics #13655

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

Closed
timurrrr opened this issue Jul 6, 2012 · 11 comments
Closed

[Windows] Support microsoft intrinsics #13655

timurrrr opened this issue Jul 6, 2012 · 11 comments
Labels
bugzilla Issues migrated from bugzilla c++

Comments

@timurrrr
Copy link
Contributor

timurrrr commented Jul 6, 2012

Bugzilla Link 13283
Resolution FIXED
Resolved on Feb 27, 2014 18:31
Version trunk
OS Windows NT
Blocks llvm/llvm-bugzilla-archive#13707
CC @asl,@benlangmuir,@DougGregor,@tritao,@nico,@rnk

Extended Description

E.g. _InterlockedDecrement http://msdn.microsoft.com/en-us/library/windows/desktop/f24ya7ct(v=vs.85).aspx

$ cat interlocked_decrement.c
#include <windows.h>

int main(void) {
LONG val = 1;
_InterlockedDecrement(&val);
return val != 0;
}

$ cl -nologo interlocked_decrement.c && interlocked_decrement.exe && echo "OK"
interlocked_decrement.c
OK

$ clang interlocked_decrement.c
interlocked_decrement.c:5:3: warning: implicit declaration of function '_InterlockedDecrement' is invalid in C99 [-Wimplicit-function-declaration]
_InterlockedDecrement(&val);
^
1 warning generated.
interlocked_decrement-199938.o : error LNK2019: unresolved external symbol __InterlockedDecrement referenced in function _main
a.out : fatal error LNK1120: 1 unresolved externals
clang: error: linker command failed with exit code 1120 (use -v to see invocation)

@asl
Copy link
Collaborator

asl commented Jul 6, 2012

It seems to me they can be implemented using inline asm. So, we can just provide header which might be automagically included when targeting vcpp ABI.

@timurrrr
Copy link
Contributor Author

One intrinsic that looks easy to implement hits me more often than others:

$ cat return_address.cpp
extern "C" {
int printf(const char fmt, ...);
void
_ReturnAddress();
}

int main() {
printf("Return address = %p\n", _ReturnAddress());
}

$ cl -nologo return_address.cpp >/dev/null && echo "OK"
OK

$ clang -Xclang -cxx-abi -Xclang microsoft return_address.cpp
return_address-912325.o : error LNK2019: unresolved external symbol __ReturnAddress referenced in function _main
a.out : fatal error LNK1120: 1 unresolved externals

_ReturnAddress should be equivalent to
__builtin_extract_return_addr(__builtin_return_address(0))

@tritao
Copy link
Mannequin

tritao mannequin commented Aug 27, 2012

http://llvm.org/bugs/show_bug.cgi?id=12074#c4

"The MS compiler provides its own <intrin.h> that includes a lot of other
intrinsic headers for different subsets of CPU functionality (MMX, SSE, etc...)

Some of them are provided by Clang built-in headers and they get picked up, but
unfortunately, there are some problems with some defines being expected.

If those are relaxed, then the compiler errors later because some macros are
defined and Clang provides the intrinsincs as functions instead of extern
declarations.

I got a little farther with a custom <intrin.h> header based on the MinGW-w64
one, some of them need to be implemented in Clang itself, like the
Interlocked* family of functions and some other stuff I did not yet research.

Need some feedback on how we want to proceed on this."

I'd still like some feedback on this. I want to work on it since it's preventing simple things like #include from working.

@tritao
Copy link
Mannequin

tritao mannequin commented Mar 27, 2013

FYI, some intrinsics needed for <type_traits> header support in MSVC 2012 have landed in r178111.

@timurrrr
Copy link
Contributor Author

Looks like Warren has started working on this
http://llvm.org/viewvc/llvm-project?rev=191590

Based on the diff, I assumed _InterlockedExchangeAdd should work by now, but it doesn't:


$ cat test.cpp
#ifndef NOINCLUDE
#include <intrin.h>
#else
extern "C" long _InterlockedExchangeAdd(long volatile * A, long V);
#endif

int main() {
long x = 42;
_InterlockedExchangeAdd(&x, 42);
return x;
}

CL is fine:
$ cl -nologo test.cpp >/dev/null && ./test.exe; echo $?
84

$ cl -nologo -DNOINCLUDE test.cpp >/dev/null && ./test.exe; echo $?
84

Clang is not:
$ clang-cl test.cpp && ./a.out ; echo $?
42

$ clang-cl -DNOINCLUDE test.cpp && ./a.out ; echo $?
test-f04fdc.obj : error LNK2019: unresolved external symbol __InterlockedExchangeAdd referenced in function _main
test.exe : fatal error LNK1120: 1 unresolved externals
clang-cl.exe: error: linker command failed with exit code 1120 (use -v to see invocation)

@timurrrr
Copy link
Contributor Author

timurrrr commented Oct 1, 2013

The result of building the #else clause doesn't change if I add

#pragma instrnsic (_InterlockedExchangeAdd)

@llvmbot
Copy link
Member

llvmbot commented Oct 1, 2013

Looks like Warren has started working on this
http://llvm.org/viewvc/llvm-project?rev=191590

Based on the diff, I assumed _InterlockedExchangeAdd should work by now, but
it doesn't:


$ cat test.cpp
#ifndef NOINCLUDE
#include <intrin.h>
#else
extern "C" long _InterlockedExchangeAdd(long volatile * A, long V);
#endif

int main() {
long x = 42;
_InterlockedExchangeAdd(&x, 42);
return x;
}

CL is fine:
$ cl -nologo test.cpp >/dev/null && ./test.exe; echo $?
84

$ cl -nologo -DNOINCLUDE test.cpp >/dev/null && ./test.exe; echo $?
84

Clang is not:
$ clang-cl test.cpp && ./a.out ; echo $?
42

$ clang-cl -DNOINCLUDE test.cpp && ./a.out ; echo $?
test-f04fdc.obj : error LNK2019: unresolved external symbol
__InterlockedExchangeAdd referenced in function _main
test.exe : fatal error LNK1120: 1 unresolved externals
clang-cl.exe: error: linker command failed with exit code 1120 (use -v to
see invocation)

#include <Intrin.h>
int main() {
long x = 42;
_InterlockedExchangeAdd(&x, 42);
return x;
}

returns 84 for me...

I'm running with the visual studio integrated version of clang-cl (llvm-VS2012 toolchain).

@llvmbot
Copy link
Member

llvmbot commented Jan 23, 2014

This does not compile with clang-cl.exe but it does using cl.exe:

#include <Windows.h>

void** Dest;
void* Exch;
void* Comp;

int main(void)
{
long x = 42;
void* ret = InterlockedCompareExchangePointer(Dest,Exch,Comp);
return (int)ret;
}

@llvmbot
Copy link
Member

llvmbot commented Jan 23, 2014

I updated Interin.h in http://llvm-reviews.chandlerc.com/D2600 by adding _ InterlockedCompareExchangePointer . Please review it and submit it if it's acceptable.

@rnk
Copy link
Collaborator

rnk commented Feb 28, 2014

These programs run successfully, and we have our own intrin.h.

@tritao
Copy link
Mannequin

tritao mannequin commented Nov 26, 2021

mentioned in issue llvm/llvm-bugzilla-archive#13707

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++
Projects
None yet
Development

No branches or pull requests

4 participants