Issue 150319
Summary Clang refuses to inline one-liners
Labels clang
Assignees
Reporter StreetwareGames
    Hello, I have a very simple program that when compiled even with optimization flags, is unable to inline a function that just calls another function. I am using the latest version of Clang.

```
clang version 21.1.0-rc1
Target: x86_64-pc-windows-msvc
Thread model: posix
```

Here is the code:
```cpp
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "ucrt.lib")
#pragma comment(lib, "vcruntime.lib")

#define CONST_VTABLE
#define INITGUID
#define OEMRESOURCE
#define STRICT
#define UNICODE
#define WIN32_LEAN_AND_MEAN

#define __STDC_WANT_IEC_60559_TYPES_EXT__

#include <Windows.h>

namespace {
	static inline void foo() {
		MessageBoxW(nullptr, L"Hello from the underworld", nullptr, MB_OK | MB_ICONEXCLAMATION);
	}
}

[[noreturn]]
void WinMainCRTStartup() {
	foo();

	ExitProcess(0);
}
```

And the flags passed to the compiler (placed into a .cmd file):
(and yes I know some are redundant/useless)

```cmd
@echo off

set CPP_STANDARD=-std=gnu++2c

set COMPILE_FLAGS_COMMON=^
	-fenable-matrix ^
	-ffast-math ^
	-ffp-model=aggressive ^
	-flto ^
	-fmerge-all-constants ^
	-fms-extensions ^
	-fno-asynchronous-unwind-tables ^
	-fno-exceptions ^
	-fno-pic ^
	-fno-pie ^
	-fno-rtti ^
	-fno-semantic-interposition ^
	-fno-stack-protector ^
	-fno-threadsafe-statics ^
	-fno-unwind-tables ^
	-fstrict-aliasing ^
	-nostdlib ^
	-nostdlib++ ^
	-fuse-ld=lld ^
	-mno-stack-arg-probe ^
	-w

set COMPILE_FLAGS_WINDOWS=^
	-mpopcnt ^
	-mf16c ^
	-mfma ^
	-mavx2 ^
	-Xlinker -opt:icf^
	-Xlinker -stack:0x100000,0x100000 ^
	-Xlinker -heap:0x0,0x0 ^
	-Xlinker -safeseh:no ^
	-Xlinker -fixed ^
	-Xlinker -subsystem:windows

set COMPILE_FLAGS=%CPP_STANDARD% %COMPILE_FLAGS_COMMON% %COMPILE_FLAGS_WINDOWS%

clang++ -g3 .cpp -o g3.exe %COMPILE_FLAGS%
clang++ -Oz .cpp -o Oz.exe %COMPILE_FLAGS% -Rpass=inline -Rpass-missed=inline -Rpass-analysis=inline
clang++ -O3 .cpp -o O3.exe %COMPILE_FLAGS% -Rpass=inline -Rpass-missed=inline -Rpass-analysis=inline
llvm-strip --strip-all Oz.exe
llvm-strip --strip-all O3.exe

rem just to see what's going on
clang++ -S -fno-verbose-asm -masm=intel -Oz .cpp -o Oz.s -mllvm --x86-asm-syntax=intel %COMPILE_FLAGS%

```

And the output (both -O3 and -Oz fail):
```
.cpp:26:2: remark: '?foo@?A0xADF4186C@@YAXXZ' not inlined into '?WinMainCRTStartup@@YAXXZ' because too costly to inline
      (cost=20, threshold=0) [-Rpass-missed=inline]
   26 |         foo();
      | ^
.cpp:26:2: remark: '?foo@?A0xADF4186C@@YAXXZ' not inlined into '?WinMainCRTStartup@@YAXXZ' because too costly to inline
      (cost=20, threshold=0) [-Rpass-missed=inline]
   26 |         foo();
      | ^
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to