Skip to content

Commit e37e475

Browse files
authored
Merge pull request #31035 from martinboehme/emit-called-func
Bug fix: Emit code for C++ inline function called from another inline function
2 parents d49c83c + 4eeb953 commit e37e475

File tree

4 files changed

+104
-8
lines changed

4 files changed

+104
-8
lines changed

lib/IRGen/GenClangDecl.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,34 @@ class ClangDeclRefFinder
3434
return true;
3535
}
3636
};
37+
38+
// If any (re)declaration of `decl` contains executable code, returns that
39+
// redeclaration; otherwise, returns nullptr.
40+
// In the case of a function, executable code is contained in the function
41+
// definition. In the case of a variable, executable code can be contained in
42+
// the initializer of the variable.
43+
clang::Decl *getDeclWithExecutableCode(clang::Decl *decl) {
44+
if (auto fd = dyn_cast<clang::FunctionDecl>(decl)) {
45+
const clang::FunctionDecl *definition;
46+
if (fd->hasBody(definition)) {
47+
return const_cast<clang::FunctionDecl *>(definition);
48+
}
49+
} else if (auto vd = dyn_cast<clang::VarDecl>(decl)) {
50+
clang::VarDecl *initializingDecl = vd->getInitializingDeclaration();
51+
if (initializingDecl) {
52+
return initializingDecl;
53+
}
54+
}
55+
56+
return nullptr;
57+
}
58+
3759
} // end anonymous namespace
3860

3961
void IRGenModule::emitClangDecl(const clang::Decl *decl) {
40-
auto valueDecl = dyn_cast<clang::ValueDecl>(decl);
41-
if (!valueDecl || valueDecl->isExternallyVisible()) {
62+
// Fast path for the case where `decl` doesn't contain executable code, so it
63+
// can't reference any other declarations that we would need to emit.
64+
if (getDeclWithExecutableCode(const_cast<clang::Decl *>(decl)) == nullptr) {
4265
ClangCodeGen->HandleTopLevelDecl(
4366
clang::DeclGroupRef(const_cast<clang::Decl*>(decl)));
4467
return;
@@ -69,12 +92,9 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
6992

7093
while (!stack.empty()) {
7194
auto *next = const_cast<clang::Decl *>(stack.pop_back_val());
72-
if (auto fn = dyn_cast<clang::FunctionDecl>(next)) {
73-
const clang::FunctionDecl *definition;
74-
if (fn->hasBody(definition)) {
75-
refFinder.TraverseDecl(const_cast<clang::FunctionDecl *>(definition));
76-
next = const_cast<clang::FunctionDecl *>(definition);
77-
}
95+
if (clang::Decl *executableDecl = getDeclWithExecutableCode(next)) {
96+
refFinder.TraverseDecl(executableDecl);
97+
next = executableDecl;
7898
}
7999
ClangCodeGen->HandleTopLevelDecl(clang::DeclGroupRef(next));
80100
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifdef __cplusplus
2+
#define INLINE inline
3+
#else
4+
// When compiling as C, make the functions `static inline`. This is the flavor
5+
// of inline functions for which we require the behavior checked by this test.
6+
// Non-static C inline functions don't require Swift to emit LLVM IR for them
7+
// because the idea is that there will we one `.c` file that declares them
8+
// `extern inline`, causing an out-of-line definition to be emitted to the
9+
// corresponding .o file.
10+
#define INLINE static inline
11+
#endif
12+
13+
INLINE int notCalled() {
14+
return 42;
15+
}
16+
17+
INLINE int calledTransitively() {
18+
return 42;
19+
}
20+
21+
#ifdef __cplusplus
22+
class C {
23+
public:
24+
int memberFunctionCalledTransitively() {
25+
return 42;
26+
}
27+
};
28+
29+
inline int calledTransitivelyFromVarInit() {
30+
return 42;
31+
}
32+
33+
inline int varUsedFromSwift = calledTransitivelyFromVarInit();
34+
#else
35+
// C only allows constant initializers for variables with static storage
36+
// duration, so there's no way to initialize this with the result of a call to
37+
// an inline method. Just provide _some_ definition of `varImportedToSwift` so
38+
// we can import it in the test.
39+
static int varUsedFromSwift = 42;
40+
#endif
41+
42+
INLINE int calledFromSwift() {
43+
#ifdef __cplusplus
44+
C c;
45+
return calledTransitively() + c.memberFunctionCalledTransitively();
46+
#else
47+
return calledTransitively();
48+
#endif
49+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module EmitCalledInlineFunction {
2+
header "emit-called-inline-function.h"
3+
export *
4+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Test that we emit LLVM IR for inline functions that are called directly or
2+
// transitively from Swift.
3+
//
4+
// Test that we don't emit LLVM IR for inline functions that are not called from
5+
// Swift.
6+
7+
// RUN: %empty-directory(%t)
8+
// RUN: %target-swift-frontend %s -I %S/Inputs -Xcc -std=c99 -emit-ir -o - | %FileCheck %s -check-prefix C99 --implicit-check-not notCalled
9+
// RUN: %target-swift-frontend %s -I %S/Inputs -enable-cxx-interop -emit-ir -o - | %FileCheck %s -check-prefix CXX --implicit-check-not notCalled
10+
11+
import EmitCalledInlineFunction
12+
13+
// C99-DAG: define internal i32 @calledFromSwift()
14+
// C99-DAG: define internal i32 @calledTransitively()
15+
16+
// CXX-DAG: define linkonce_odr{{( dso_local)?}} i32 @{{_Z15calledFromSwiftv|"\?calledFromSwift@@YAHXZ"}}()
17+
// CXX-DAG: define linkonce_odr{{( dso_local)?}} i32 @{{_Z18calledTransitivelyv|"\?calledTransitively@@YAHXZ"}}()
18+
// CXX-DAG: define linkonce_odr{{( dso_local)?}} i32 @{{_ZN1C32memberFunctionCalledTransitivelyEv|"\?memberFunctionCalledTransitively@C@@QEAAHXZ"}}(%class.C* %this)
19+
// CXX-DAG: define linkonce_odr{{( dso_local)?}} i32 @{{_Z29calledTransitivelyFromVarInitv|"\?calledTransitivelyFromVarInit@@YAHXZ"}}()
20+
21+
calledFromSwift()
22+
23+
let _ = varUsedFromSwift

0 commit comments

Comments
 (0)