From 729530f68fe135ad41d470fbed019cc5e31ac8a5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= Date: Sun, 3 Nov 2019 20:47:40 +0100 Subject: [PATCH] -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 --- clang/lib/Serialization/ASTWriterDecl.cpp | 5 +++-- clang/test/Modules/codegen-extern-template.cpp | 9 +++++++++ clang/test/Modules/codegen-extern-template.h | 12 ++++++++++++ clang/test/Modules/codegen-extern-template.modulemap | 1 + 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 clang/test/Modules/codegen-extern-template.cpp create mode 100644 clang/test/Modules/codegen-extern-template.h create mode 100644 clang/test/Modules/codegen-extern-template.modulemap diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp index a553936..74a49fa 100644 --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -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()) { if (!Linkage) Linkage = Writer->Context->GetGVALinkageForFunction(FD); - ModulesCodegen = *Linkage != GVA_Internal; + ModulesCodegen = + *Linkage != GVA_Internal && *Linkage != GVA_AvailableExternally; } } } diff --git a/clang/test/Modules/codegen-extern-template.cpp b/clang/test/Modules/codegen-extern-template.cpp new file mode 100644 index 0000000..3f0bd15 --- /dev/null +++ b/clang/test/Modules/codegen-extern-template.cpp @@ -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(); + +// CHECK: define weak_odr i32 @_Z3fooIiET_v diff --git a/clang/test/Modules/codegen-extern-template.h b/clang/test/Modules/codegen-extern-template.h new file mode 100644 index 0000000..56ee036 --- /dev/null +++ b/clang/test/Modules/codegen-extern-template.h @@ -0,0 +1,12 @@ +// header for codegen-extern-template.cpp +#ifndef CODEGEN_EXTERN_TEMPLATE_H +#define CODEGEN_EXTERN_TEMPLATE_H + +template +inline T foo() { return 10; } + +extern template int foo(); + +inline int bar() { return foo(); } + +#endif diff --git a/clang/test/Modules/codegen-extern-template.modulemap b/clang/test/Modules/codegen-extern-template.modulemap new file mode 100644 index 0000000..8e050e5 --- /dev/null +++ b/clang/test/Modules/codegen-extern-template.modulemap @@ -0,0 +1 @@ +module foo { header "codegen-extern-template.h" } -- 2.7.4