From 13a18fecdd12fc70175d7d3e3a7678b7af9f6bc1 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Thu, 29 Sep 2016 03:32:04 +0000 Subject: [PATCH] [PR30341] Alias must point to a definition Inlining the destructor caused the compiler to generate bad IR which failed the Verifier in the backend. https://llvm.org/bugs/show_bug.cgi?id=30341 This patch disables alias to available_externally definitions. Reviewers: eugenis, rsmith Differential Revision: https://reviews.llvm.org/D24682 llvm-svn: 282679 --- clang/lib/CodeGen/CGCXX.cpp | 14 ++++++-------- clang/test/CodeGenCXX/alias-available-externally.cpp | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 clang/test/CodeGenCXX/alias-available-externally.cpp diff --git a/clang/lib/CodeGen/CGCXX.cpp b/clang/lib/CodeGen/CGCXX.cpp index 40f1bc4..d33555f 100644 --- a/clang/lib/CodeGen/CGCXX.cpp +++ b/clang/lib/CodeGen/CGCXX.cpp @@ -134,6 +134,11 @@ bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, llvm::GlobalValue::LinkageTypes TargetLinkage = getFunctionLinkage(TargetDecl); + // available_externally definitions aren't real definitions, so we cannot + // create an alias to one. + if (TargetLinkage == llvm::GlobalValue::AvailableExternallyLinkage) + return true; + // Check if we have it already. StringRef MangledName = getMangledName(AliasDecl); llvm::GlobalValue *Entry = GetGlobalValue(MangledName); @@ -156,14 +161,7 @@ bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, // Instead of creating as alias to a linkonce_odr, replace all of the uses // of the aliasee. - if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) && - (TargetLinkage != llvm::GlobalValue::AvailableExternallyLinkage || - !TargetDecl.getDecl()->hasAttr())) { - // FIXME: An extern template instantiation will create functions with - // linkage "AvailableExternally". In libc++, some classes also define - // members with attribute "AlwaysInline" and expect no reference to - // be generated. It is desirable to reenable this optimisation after - // corresponding LLVM changes. + if (llvm::GlobalValue::isDiscardableIfUnused(Linkage)) { addReplacement(MangledName, Aliasee); return false; } diff --git a/clang/test/CodeGenCXX/alias-available-externally.cpp b/clang/test/CodeGenCXX/alias-available-externally.cpp new file mode 100644 index 0000000..264af55 --- /dev/null +++ b/clang/test/CodeGenCXX/alias-available-externally.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -O1 -std=c++11 -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s +// Clang should not generate alias to available_externally definitions. +// Check that the destructor of Foo is defined. +// CHECK: define linkonce_odr void @_ZN3FooD2Ev +template +struct String { + String() {} + ~String(); +}; + +template +inline __attribute__((visibility("hidden"), always_inline)) +String::~String() {} + +extern template struct String; + +struct Foo : public String { Foo() { String s; } }; + +Foo f; -- 2.7.4