This is an archive of the discontinued LLVM Phabricator instance.

Attribute inline
Needs ReviewPublic

Authored by zahiraam on Apr 14 2017, 11:47 AM.

Details

Summary

Added support for attribute inline for a __declspec declaration.

Diff Detail

Event Timeline

zahiraam created this revision.Apr 14 2017, 11:47 AM
aaron.ballman added a subscriber: cfe-commits.
aaron.ballman added inline comments.Apr 14 2017, 11:53 AM
include/clang/Basic/Attr.td
869

I cannot see any documentation on MSDN for this spelling of the attribute, and MSVC 2015 rejects it. What is the motivating use case for this spelling?

ksh-3.2$ cat test3.c
static void __declspec(inline) foo() {
}

int main()
{

foo();

}

ksh-3.2$ cl test3.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

test3.c
Microsoft (R) Incremental Linker Version 14.00.24215.1
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test3.exe
test3.obj
ksh-3.2$

This is for an application that we are running here that is failing.

ksh-3.2$ cat test3.c
static void __declspec(inline) foo() {
}

int main()
{

foo();

}

ksh-3.2$ cl test3.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

test3.c
Microsoft (R) Incremental Linker Version 14.00.24215.1
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test3.exe
test3.obj
ksh-3.2$

This is for an application that we are running here that is failing.

I just tested this on 19.10.25017, 19.10.25206.0 and 14.0.25431.01 and none of them accept your code.

https://godbolt.org/g/xk1ciG

Forgot to mention that this is for C only. CL doesn't compile this declaration in a C++ context.
Please see:
https://godbolt.org/g/WRhyjL

Forgot to mention that this is for C only. CL doesn't compile this declaration in a C++ context.
Please see:
https://godbolt.org/g/WRhyjL

Oh, that's fun.

It's not at all clear that this isn't simply an MSVC bug. Can you work around this in your project rather than introduce an undocumented MSVC attribute?

rsmith added inline comments.Apr 14 2017, 12:55 PM
include/clang/Basic/Attr.td
869

Also, it seems rather unlikely that MSVC would implement the GNU inline semantics. Have you investigated the actual semantic effects of this __declspec attribute and checked they match the GNU semantics?

Yes it behaves the same way than the Gnu inline.

Pushed the submit too fast ...
Before I submitted this review, I have done some experiments and inline and declspec(inline) have the same behavior. Compiling with /Ob0 disables inlining. With -O1 or -O2, inline happens.

rsmith edited edge metadata.Apr 14 2017, 1:13 PM

Pushed the submit too fast ...
Before I submitted this review, I have done some experiments and inline and declspec(inline) have the same behavior. Compiling with /Ob0 disables inlining. With -O1 or -O2, inline happens.

What do you mean, "inline and declspec(inline) have the same behavior"? What exactly did you test? The inline keyword means quite different things in different language modes, and none of them are the same as the meaning of __attribute__((gnu_inline)) (which is what you aliased this to). Some of these differences are quite subtle (for instance, the precise behavior of extern inline and static inline, and what happens if you redeclare a function with a different set of specifiers).

Yes I did compare "inline" with "declspec(inline)" and not with "__attribute((gnu_inline))" and didn't test it whit different specifiers. I can do that.
If behavior is different we probably have to add an additional attribute?

Yes I did compare "inline" with "declspec(inline)" and not with "__attribute((gnu_inline))" and didn't test it whit different specifiers. I can do that.
If behavior is different we probably have to add an additional attribute?

It's not that it needs an additional attribute, but that your patch will require additional work. However, my question remains unanswered: can you fix your project? Is this in a header file of some well-used library? We typically do not implement undocumented compiler features of other compilers unless there's a compelling reason to do so.

From some very superficial testing, it looks like CL treats __declspec(inline) exactly like a synonym for inline (it even rejects them both appearing on the same declaration, complaining about a duplicate inline specifier). So modeling this as GNUInlineAttr is definitely wrong, and this should probably be setting the inline flag on the function. But I agree with Aaron: we need some evidence that implementing this in Clang is worthwhile (rather than changing the codebase to use the inline keyword instead). We aim to be CL-compatible for common code patterns, not to be 100% compatible with all code that CL accepts.