Skip to content

Commit e167f75

Browse files
authored
[C++20] [Modules] Always emit the inline builtins (#101278)
See the attached test for the motivation example. If we're too greedy to not emit the definition for inline builtins, we may meet a middle end crash. And it should be good to emit inline builtins always.
1 parent ab33c3d commit e167f75

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4022,6 +4022,11 @@ bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
40224022
return true;
40234023

40244024
const auto *F = cast<FunctionDecl>(GD.getDecl());
4025+
// Inline builtins declaration must be emitted. They often are fortified
4026+
// functions.
4027+
if (F->isInlineBuiltinDeclaration())
4028+
return true;
4029+
40254030
if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
40264031
return false;
40274032

@@ -4067,11 +4072,6 @@ bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
40674072
}
40684073
}
40694074

4070-
// Inline builtins declaration must be emitted. They often are fortified
4071-
// functions.
4072-
if (F->isInlineBuiltinDeclaration())
4073-
return true;
4074-
40754075
// PR9614. Avoid cases where the source code is lying to us. An available
40764076
// externally function should have an equivalent function somewhere else,
40774077
// but a function that calls itself through asm label/`__builtin_` trickery is
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// REQUIRES: !system-windows
2+
//
3+
// RUN: rm -rf %t
4+
// RUN: split-file %s %t
5+
// RUN: cd %t
6+
//
7+
// RUN: %clang_cc1 -std=c++20 -O3 %t/a.cppm -emit-module-interface -o %t/a.pcm
8+
// RUN: %clang_cc1 -std=c++20 -O3 %t/test.cc -fmodule-file=a=%t/a.pcm \
9+
// RUN: -emit-llvm -o - | FileCheck %t/test.cc
10+
11+
//--- memmove.h
12+
typedef long unsigned int size_t;
13+
extern "C" void *memmove (void *__dest, const void *__src, size_t __n)
14+
throw () __attribute__ ((__nonnull__ (1, 2)));
15+
extern "C" __inline __attribute__ ((__always_inline__)) __attribute__ ((__gnu_inline__)) void *
16+
memmove (void *__dest, const void *__src, size_t __len) throw ()
17+
{
18+
return __builtin_memmove(__dest, __src, __len);
19+
}
20+
21+
//--- a.cppm
22+
module;
23+
#include "memmove.h"
24+
export module a;
25+
export using ::memmove;
26+
27+
//--- test.cc
28+
import a;
29+
30+
void test() {
31+
int a, b;
32+
unsigned c = 0;
33+
memmove(&a, &b, c);
34+
}
35+
36+
// CHECK-NOT: memmove

0 commit comments

Comments
 (0)