Why does F# compiler generates IL NOP even in release builds? - f#

I've just stumbled across this and I'm a bit puzzled.
I have an out-of-the-box VS 2010 F# project, with all default settings, targeting .NET 4.0.
The F# code is like this:
let test(a:int, b:int, c:int) = min a (min b c)
When I compile it for release, the generated IL contains some strange NOP
instructions scattered around. Like this:
The generated IL for this (with all default settings):
.method public static int32 test(int32 a,
int32 b,
int32 c) cil managed
{
// Code size 20 (0x14)
.maxstack 4
.locals init ([0] int32 V_0)
// HERE
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: bge.s IL_0009
IL_0005: ldarg.1
// HERE
IL_0006: nop
IL_0007: br.s IL_000b
IL_0009: ldarg.2
// HERE
IL_000a: nop
IL_000b: stloc.0
IL_000c: ldarg.0
IL_000d: ldloc.0
IL_000e: bge.s IL_0012
IL_0010: ldarg.0
IL_0011: ret
IL_0012: ldloc.0
IL_0013: ret
} // end of method Module1::test
My .fsproj project configuration is:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<Tailcalls>true</Tailcalls>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<DocumentationFile>bin\Release\TestFsIL.XML</DocumentationFile>
</PropertyGroup>
Now, if I comment out line <DebugType>pdbonly</DebugType>, the NOP instructions
disappear. But of course so does the PDB file!
.method public static int32 test(int32 a,
int32 b,
int32 c) cil managed
{
// Code size 17 (0x11)
.maxstack 4
.locals init (int32 V_0)
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: bge.s IL_0007
IL_0004: ldarg.1
IL_0005: br.s IL_0008
IL_0007: ldarg.2
IL_0008: stloc.0
IL_0009: ldarg.0
IL_000a: ldloc.0
IL_000b: bge.s IL_000f
IL_000d: ldarg.0
IL_000e: ret
IL_000f: ldloc.0
IL_0010: ret
} // end of method Module1::test
There is also a subtle different in the .locals line:
.locals init ([0] int32 V_0)
vs
.locals init (int32 V_0)
When I tried C# compiler, it generates NOP instructions only in the
debug builds, but these seem to go away in release builds, even when
PDB files are included using <DebugType>pdbonly</DebugType>.
Questions:
Why are NOP instructions generated in F# in release build when PDB files are included, when C# seems to be able to avoid this.
Is there any way to get rid of those NOP, but still have the PDB files?
PS. There are related questions on SO here
and here,
but all the answers there say
you are compiling in debug mode, if you compile in release mode, the NOP goes away
which contradicts my experience with F# compiler as demonstrated.

I don't exactly know how this works internally in the F# compiler (somebody from the F# team may have a better answer), but my guess is that generating nop instructions is just a fairly easy way to generate locations that can be referred to from the pdb file.
The pdb file needs to specify some IL range for expression in the code where you can place a breakpoint - and this is the case in both Debug and Release mode. This means that if you place a breakpoint somewhere, there needs to be a corresponding location in the IL. However, if there is no actual instruction, corresponding to the source code location, the compiler needs to insert something - so it adds nop.
In Release mode, the F# compiler does more optimizations, but if you want pdb files, it still needs to provide location for all source code locations. This might not be needed in C#, because C# maps more closely to the source IL, but it might be harder to avoid this in F#.

Related

Exception thrown in SurfaceImageSource.as()

I'm back to C++ after a 31-year absence and have spent the last few months learning everything I can about Windows app development, UWP, C++/WinRT, DirectX, WinUI 3, XAML, etc. All within Visual Studio 2022 Community.
I'm currently working on creating an app template that brings together DirectX and WinUI 3, allowing me to draw on a surface within XAML/UI3. So far I've been able to create separate apps with each component (modifying existing Microsoft templates), but I'm struggling a bit bringing them all together.
The closest example I've found (using C++/WinRT) is the "DirectX and XAML interop" tutorial. I copied the example into my WinUI 3 project and it compiles no errors and the UI runs fine until I click on the button that invokes the tutorial code. Then it crashes trying to create a winrt::com_ptr to the SurfaceImageSource. I've spent a couple of days now trying to figure out why. The code in question is :
(C++/WinRT)
SurfaceImageSource surfaceImageSource( 500, 500 ) ;
winrt :: com_ptr <::ISurfaceImageSourceNativeWithD2D> sisNativeWithD2D {
surfaceImageSource.as <::ISurfaceImageSourceNativeWithD2D> () } ;
If I replace "surfaceImageSource.as" with "surfaceImageSource.try_as" the call returns, but .get() on the com_ptr returns nullptr. So it looks like internally, QueryInterface is not succeeding.
Any help with this would be greatly appreciated since I don't know where to go from here.
Thanks.
*** EDIT *** : additional information added per IInspectable's comment
Thanks IInspectable for your help. I can reproduce this with the following minimal setup :
1 - Create a new project from "Blank App, Packaged (WinUI 3 in Desktop)" C++/WinRT template. This project creates a blank window with a UI3 "Click Me" button in its center.
2 - In the file "MainWindow.xaml.cpp" :
a - at the top, add :
#include <windows.ui.xaml.media.dxinterop.h>
#include <winrt/Microsoft.UI.Xaml.Media.Imaging.h>
using namespace winrt::Microsoft::UI::Xaml::Media::Imaging;
b - further down, in the method "MainWindow::myButton_Click()" add the two lines mentioned earlier :
SurfaceImageSource surfaceImageSource(500, 500);
winrt::com_ptr <::ISurfaceImageSourceNativeWithD2D> sisNativeWithD2D{
surfaceImageSource.as <::ISurfaceImageSourceNativeWithD2D>() };
3 - that's it. Rebuild and the program crashes on the button click.
I put a breakpoint between the two lines above.
I then put another breakpoint in "Generated Files\winrt\base.h" on first line of "struct IUnknown :: as()":
template <typename To>
auto as() const
{ (breakpoint here)
return impl::as<To>(m_ptr);
}
When I continue execution, the program crashes immediately before returning to the next line of myButton_Click() (as far as I can determine).
Debug Output
'My_WinUI_3_project_4.exe' (Win32): Loaded 'C:\Program Files\WindowsApps\Microsoft.WindowsAppRuntime.1.1_1004.584.2120.0_x64__8wekyb3d8bbwe\Microsoft.DirectManipulation.dll'.
onecoreuap\windows\frameworkudk\dxprivatescommon.cpp(53)\Microsoft.Internal.FrameworkUdk.dll!00007FFFABE09BC5: (caller: 00007FFFABE5ED0D) ReturnHr(1) tid(2508) 80004002 This interface is not supported
onecoreuap\windows\frameworkudk\dxprivatescommon.cpp(53)\Microsoft.Internal.FrameworkUdk.dll!00007FFFABE09BC5: (caller: 00007FFFABE5ED0D) ReturnHr(2) tid(2508) 80004002 This interface is not supported
onecoreuap\windows\frameworkudk\dxprivatescommon.cpp(53)\Microsoft.Internal.FrameworkUdk.dll!00007FFFABE09BC5: (caller: 00007FFFABE5ED0D) ReturnHr(3) tid(3670) 80004002 This interface is not supported
'My_WinUI_3_project_4.exe' (Win32): Loaded 'C:\Windows\System32\cabinet.dll'.
'My_WinUI_3_project_4.exe' (Win32): Loaded 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\XamlDiagnostics\x64\WinUI3\Microsoft.VisualStudio.DesignTools.WinUITap.dll'.
Microsoft.UI.Xaml.dll!00007FFF8DAF3D45: (caller: 00007FFF90651CAF) ReturnHr(1) tid(2508) 80070057 Incorrect parameter.
Microsoft.UI.Xaml.dll!00007FFF8DAF3D45: (caller: 00007FFF90651CAF) ReturnHr(2) tid(2508) 80070057 Incorrect parameter.
Microsoft.UI.Xaml.dll!00007FFF8DAF3D45: (caller: 00007FFF90651CAF) ReturnHr(3) tid(2508) 80070057 Incorrect parameter.
Exception thrown at 0x00007FF83C644FD9 (KernelBase.dll) in My_WinUI_3_project_4.exe: WinRT originate error - 0x80004002 : 'This interface is not supported'.
Exception thrown at 0x00007FF83C644FD9 in My_WinUI_3_project_4.exe: Microsoft C++ exception: winrt::hresult_no_interface at memory location 0x0000005C234FA538.
Exception thrown at 0x00007FF83C644FD9 in My_WinUI_3_project_4.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
A breakpoint instruction (__debugbreak() statement or a similar call) was executed in My_WinUI_3_project_4.exe.
Call Stack
My_WinUI_3_project_4.exe!winrt::My_WinUI_3_project_4::implementation::App::{ctor}::__l2::<lambda>(const winrt::Windows::Foundation::IInspectable & __formal, const winrt::Microsoft::UI::Xaml::UnhandledExceptionEventArgs & e) Line 31 C++
My_WinUI_3_project_4.exe!winrt::impl::delegate<winrt::Microsoft::UI::Xaml::UnhandledExceptionEventHandler,void <lambda>(const winrt::Windows::Foundation::IInspectable &, const winrt::Microsoft::UI::Xaml::UnhandledExceptionEventArgs &)>::Invoke(void * sender, void * e) Line 4824 C++
[External Code]
My_WinUI_3_project_4.exe!winrt::impl::consume_Microsoft_UI_Xaml_IApplicationStatics<winrt::Microsoft::UI::Xaml::IApplicationStatics>::Start(const winrt::Microsoft::UI::Xaml::ApplicationInitializationCallback & callback) Line 157 C++
My_WinUI_3_project_4.exe!winrt::Microsoft::UI::Xaml::Application::Start::__l2::<lambda>(const winrt::Microsoft::UI::Xaml::IApplicationStatics & f) Line 12146 C++
My_WinUI_3_project_4.exe!winrt::impl::factory_cache_entry<winrt::Microsoft::UI::Xaml::Application,winrt::Microsoft::UI::Xaml::IApplicationStatics>::call<void <lambda>(const winrt::Microsoft::UI::Xaml::IApplicationStatics &) &>(winrt::Microsoft::UI::Xaml::Application::Start::__l2::void <lambda>(const winrt::Microsoft::UI::Xaml::IApplicationStatics &) & callback) Line 6286 C++
My_WinUI_3_project_4.exe!winrt::impl::call_factory<winrt::Microsoft::UI::Xaml::Application,winrt::Microsoft::UI::Xaml::IApplicationStatics,void <lambda>(const winrt::Microsoft::UI::Xaml::IApplicationStatics &)>(winrt::Microsoft::UI::Xaml::Application::Start::__l2::void <lambda>(const winrt::Microsoft::UI::Xaml::IApplicationStatics &) && callback) Line 6309 C++
My_WinUI_3_project_4.exe!winrt::Microsoft::UI::Xaml::Application::Start(const winrt::Microsoft::UI::Xaml::ApplicationInitializationCallback & callback) Line 12147 C++
[External Code]
This:
#include <windows.ui.xaml.media.dxinterop.h>
should be this:
#include <microsoft.ui.xaml.media.dxinterop.h>
when working with WinUI 3.0 Desktop apps and not UWP.

VS2019 and _NO_CRT_STDIO_INLINE, how to explain this weirdo?

Please check my short code below.
pwrapper.h
#include <stdio.h>
#include <stdarg.h>
extern"C" int mm_printfA(const char *fmt, ...);
extern"C" int mm_printfW(const wchar_t *fmt, ...);
pwrapper.cpp
#include "pwrapper.h"
int mm_printfA(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
int ret = vprintf(fmt, args);
va_end(args);
return ret;
}
int mm_printfW(const wchar_t *fmt, ...)
{
va_list args;
va_start(args, fmt);
int ret = vwprintf(fmt, args);
va_end(args);
return ret;
}
main.cpp
#include "pwrapper.h"
// cl /MT /D _NO_CRT_STDIO_INLINE main.cpp pwrapper.cpp
void main()
{
mm_printfA("What is %d?\n", 123);
}
#if 0
void usedull()
{
vprintf(NULL, NULL);
vwprintf(NULL, NULL);
}
#endif
For some reason, I need to compile it with _NO_CRT_STDIO_INLINE, like this:
cl /MT /D _NO_CRT_STDIO_INLINE main.cpp pwrapper.cpp
But link stage fails saying unresolved external symbol vwprintf and vprintf .
A very weird workaround I find out is: Enable the usedull() function body -- although never be called, and, link through pwrapper.lib, using bb.bat below:
#setlocal EnableDelayedExpansion
#set CFLAGS=/D _NO_CRT_STDIO_INLINE
cl /nologo /c /MT %CFLAGS% pwrapper.cpp
#if errorlevel 1 exit /b 4
lib /nologo /out:pwrapper.lib pwrapper.obj
#if errorlevel 1 exit /b 4
cl /nologo /c /MT main.cpp
#if errorlevel 1 exit /b 4
link /nologo main.obj pwrapper.lib
#if errorlevel 1 exit /b 4
Well, this really works, but why?
This is not a pleasant workaround, because each exe project needs to include a "useless" usedull() function. So, is there any better way?
I really can't tell why this workaround works, an explanation of it is very welcome.
==== Some Clarification ====
There were two main.cpp in my original post. Let me name them separately for later reference in case someone would bother to answer this weird question.
main.0.cpp refers to the one without usedull().
main.1.cpp refers to the one with usedull().
In this question, I use VC++ headers and libs for application(not for kernel), and
I compile main.0.cpp and main.1.cpp without _NO_CRT_STDIO_INLINE.
I always compile pwrapper.cpp with _NO_CRT_STDIO_INLINE.
Whether having pwrapper.obj go through pwrapper.lib produce the same result in this issue.
In short:
compiling pwrapper.cpp with -D _NO_CRT_STDIO_INLINE tells the compiler you are going to provide your own implementation of vprintf and vwprintf at link time, and
compiling main.cpp without -D _NO_CRT_STDIO_INLINE tells the compiler to include implementations of vprintf and vwprintf which are used at link time to satisfy both the references from usedull and mm_printfA/mm_printfW
so, this particular combination works to resolve all undefined symbols at link time. See below for more discussion however.
Discussion
In stdio.h, vprintf (which I'll focus on, but vwprintf is configured in the same way) is defined like so:
_Check_return_opt_
_CRT_STDIO_INLINE int __CRTDECL vprintf(
_In_z_ _Printf_format_string_ char const* const _Format,
va_list _ArgList
)
#if defined _NO_CRT_STDIO_INLINE
;
#else
{
return _vfprintf_l(stdout, _Format, NULL, _ArgList);
}
#endif
Note that
if _NO_CRT_STDIO_INLINE is defined, this becomes a forward declaration
whereas if it is not defined, the full body is included in the compilation of the including translation unit.
Additionally, in corecrt_stdio_config.h whether _NO_CRT_STDIO_INLINE is defined determines the value of _CRT_STDIO_INLINE; if it is defined, _CRT_STDIO_INLINE is defined as empty, otherwise it is defined as __inline.
Putting these together,
if _NO_CRT_STDIO_INLINE is not defined, these functions will be candidates for inline expansion,
otherwise a separate implementation of that function will need to be provided at link time.
Default Compilation (no /O1, /O2, no _NO_CRT_STDIO_INLINE)
The above works with the specific compile and link invocations you are using, as without optimization the compiler will simply include the function body in the compilation of main.1.obj. You can see this using dumpbin; running dumpbin -symbols main.1.obj | find "| vprintf" prints:
01D 00000000 SECT8 notype () External | vprintf
showing that main.1.obj provides vprintf as an externally available symbol.
Checking pwrapper.obj, we get:
00A 00000000 UNDEF notype () External | vprintf
showing that vprintf is undefined in this object file, and will need to be provided at link time.
Optimisation for Inline Expansion
However, if we change the optimisation option for inline expansion, we get different results. Using even the first level of optimisation (-Ob1, included in -O1 and -O2) like so:
cl -c -Ob1 main.1.cpp
causes the compiler to incorporate the body of vprintf directly into usedull, and remove the separate implementation of vprintf, which can be confirmed using dumpbin. So, as you would now expect, attempting to link main.1.obj and pwrapper.obj together will once again give your original error:
pwrapper.obj : error LNK2019: unresolved external symbol vwprintf referenced in function mm_printfW
pwrapper.obj : error LNK2019: unresolved external symbol vprintf referenced in function mm_printfA
main.exe : fatal error LNK1120: 2 unresolved externals
Multiple Implementations?
So, following on from that it is apparent that compiling both files with -D _NO_CRT_STDIO_INLINE will fail as there will be no implementations of the relevant methods. What about if both are compiled without this definition?
If we check the object files, both have defined symbols for vprintf:
01D 00000000 SECT8 notype () External | vprintf
and:
01A 00000000 SECT7 notype () External | vprintf
which under normal circumstances would result in errors due both to multiple definitions of a symbol and violations of the One Definition Rule. However, when performing inline expansion, the compiler and linker have your back. As per 2:
Rather than expand an inline function defined in a header file, the compiler may create it as a callable function in more than one translation unit. The compiler marks the generated function for the linker to prevent one-definition-rule (ODR) violations.

What does __attribute__((constructor)) compile to for iOS? How to do it in hand-written asm?

For learning purposes I have been writing stuff in assembly. Currently I'm trying to do the equivalent of C's __attribute__((constructor)) in assembly but I'm not sure how to do that.
So my question, what do attributes compile to? I understand some may be quite different so I'm mainly asking about constructor.
Thanks to #Jester.
For my case (arm64 on iOS), it created a new section under __DATA.
.section __TEXT, __text, regular, pure_instructions
.global _constructor
.p2align 2
_constructor:
...
ret
.section __DATA, __mod_init_func, mod_init_funcs
.p2align 4
.quad _constructor

Msado60_Backcompat crashes in CCommand::CreateParameter

I am developing an ADO application (32 bit) on Windows 7 64 bit SP1 (all updates installed): The app must run on Win XP. According to http://support.microsoft.com/kb/2517589/en-us I am using msado60_backcompat. That worked well until lately, but now it crashes.
My code (snippets):
_CommandPtr cmd(__uuidof(Command));
cmd->ActiveConnection = dbconn;
cmd->CommandText = _T("SELECT [si] FROM [TTable] WHERE [TTable].[ti1]=?");
cmd->Parameters->Append(cmd->CreateParameter(L"#ti1", adTinyInt, adParamInput, 1, 7));
CreateParameter is implemented in msado60_backcompat:
inline _ParameterPtr Command15::CreateParameter ( _bstr_t Name, enum DataTypeEnum Type, enum ParameterDirectionEnum Direction, ADO_LONGPTR Size, const _variant_t & Value )
{
struct _Parameter * _result = 0;
HRESULT _hr = raw_CreateParameter(Name, Type, Direction, Size, Value, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _ParameterPtr(_result, false);
}
raw_CreateParameter() calls into msado15.dll into CCommand::CreateParameter. There a crash occurs at offset 0x34f (offset inside the function):
First-chance exception at 0x655ed5a6 (msado15.dll) in adosqlbugcheck.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x655ed5a6 (msado15.dll) in adosqlbugcheck.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Msado60_Backcompat.tlb: 73728 bytes, 29.4.2011
msado15.dll: 6.1.7601.17514, 1019904 bytes, 21.11.2010
The error does not occur if I use msado15.dll.
Can someone reproduce the error? Is there a solution?
The issue is solved. I was #import-ing the msado60_backcompat.tlb from "C:\Program Files\Common Files". If I import the version from "C:\Program Files (x86)\Common Files" it works. The compiler generates tlh files from both tlb files with exactly the same UUIDs and everything else, they only differ in that the one contains
typedef __int64 ADO_LONGPTR;
typedef ADO_LONGPTR PositionEnum_Param;
While the other contains
typedef long ADO_LONGPTR;
typedef enum PositionEnum PositionEnum_Param;
From my understanding of COM interfaces this should not happen. But since MS commits they really made a mistake it seems to just be the way it is.
Just for information, MS has a new solution coming up: http://blogs.msdn.com/b/psssql/archive/2011/10/03/yes-we-made-a-mistake-and-are-finally-going-to-fix-it.aspx.

x86 masm hello world

I am trying to compile a hello world on windows with the ML and LINK that ship with VS 2010.
.MODEL FLAT
.STACK 4096
.data
msg db "Hello World!",0
.code
INCLUDELIB MSVCRT
EXTRN printf:NEAR
EXTRN exit:NEAR
PUBLIC _main
_main PROC
mov eax, offset msg
push eax
call printf
mov eax,0
push eax
call exit
_main ENDP
END _main
I keep getting linker errors saying that printf and exit are unresolved external symbols. I have a couple of questions.
What are the command line options to use with ML and LINK to compile and resolve the error messages.
Is there another way to display text output to the screen using assembly code rather than calling c runtime functions like printf?
You need to use underscored names for C functions, since that's how the compiler emits them on assembly
level.
You should clean up the stack after calling printf and other CRT functions, since they use cdecl calling convention (caller stack cleanup). Strictly speaking you should do it after _exit too, but that's less important since it never returns.
To use CRT functions you have to initialize CRT. You can check how it's done in the file VC\crt\src\crt0.c
Here's a minimal file that worked for me (I used static library because I have VS2008 and didn't want to fiddle with manifests to make it work with DLL).
.386
.MODEL FLAT
.STACK 4096
.data
msg db "Hello World!",0
.code
INCLUDELIB LIBCMT
EXTRN _printf:NEAR
EXTRN _exit:NEAR
EXTRN __heap_init:NEAR
EXTRN __mtinit:NEAR
EXTRN __ioinit:NEAR
PUBLIC _main
_main PROC
push 1
call __heap_init
add esp, 4
push 1
call __mtinit
add esp, 4
call __ioinit
mov eax, offset msg
push eax
call _printf
pop ecx
mov eax,0
push eax
call _exit
_main ENDP
END _main
For MSVCRT the initialization is different, e.g. you need to call set_app_type
To not rely on CRT, you have to use the OS APIs. In case of Win32 that would be Win32 functions such as WriteFile (with GetStdHandle(STD_OUTPUT_HANDLE) for the file handle). See some examples here.

Resources