-fmodules-codegen should not emit extern templates

If a header contains 'extern template', then the template should be provided
somewhere by an explicit instantiation, so it is not necessary to generate
a copy. Worse, this can lead to an unresolved symbol, because the codegen's
object file will not actually contain functions from such a template
because of the GVA_AvailableExternally, but the object file for the explicit
instantiation will not contain them either because it will be blocked
by the information provided by the module.

Differential Revision: https://reviews.llvm.org/D69779
This commit is contained in:
Luboš Luňák 2019-11-03 20:47:40 +01:00
parent f52d71736b
commit 729530f68f
4 changed files with 25 additions and 2 deletions

View File

@ -2437,11 +2437,12 @@ void ASTRecordWriter::AddFunctionDefinition(const FunctionDecl *FD) {
}
if (Writer->Context->getLangOpts().ModulesCodegen) {
// Under -fmodules-codegen, codegen is performed for all non-internal,
// non-always_inline functions.
// non-always_inline functions, unless they are available elsewhere.
if (!FD->hasAttr<AlwaysInlineAttr>()) {
if (!Linkage)
Linkage = Writer->Context->GetGVALinkageForFunction(FD);
ModulesCodegen = *Linkage != GVA_Internal;
ModulesCodegen =
*Linkage != GVA_Internal && *Linkage != GVA_AvailableExternally;
}
}
}

View File

@ -0,0 +1,9 @@
// RUN: %clang_cc1 -triple=x86_64-linux-gnu -fmodules -fmodules-codegen -emit-module -fmodule-name=foo %S/codegen-extern-template.modulemap -x c++ -o %t.pcm
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fmodules -fmodule-file=%t.pcm %s -emit-llvm -o - | FileCheck %s
// expected-no-diagnostics
#include "codegen-extern-template.h"
template int foo<int>();
// CHECK: define weak_odr i32 @_Z3fooIiET_v

View File

@ -0,0 +1,12 @@
// header for codegen-extern-template.cpp
#ifndef CODEGEN_EXTERN_TEMPLATE_H
#define CODEGEN_EXTERN_TEMPLATE_H
template <typename T>
inline T foo() { return 10; }
extern template int foo<int>();
inline int bar() { return foo<int>(); }
#endif

View File

@ -0,0 +1 @@
module foo { header "codegen-extern-template.h" }