r192300 - Use aliases for more constructors and destructors.

Reid Kleckner rnk at google.com
Wed Oct 9 16:04:27 PDT 2013


Another problem I encountered is that the Microsoft linker doesn't like it
if you chain weak aliases like so:

$ cat t.ll
@my_alias = alias linkonce_odr void ()* @my_inline
@my_alias2 = alias linkonce_odr void ()* @my_alias
define linkonce_odr void @my_inline() {
  ret void
}
define i32 @main() {
  call void @my_alias2()
  ret i32 0
}

$ llc t.ll -o tll.obj -filetype=obj && link tll.obj -defaultlib:libcmt.lib
-out:t.exe -nologo
tll.obj : error LNK2001: unresolved external symbol _my_alias2
tll.obj : error LNK2001: unresolved external symbol _my_alias

To work around this, when should we forward my_alias2 through to my_inline?
 In clang, LLVM, or MC?


On Wed, Oct 9, 2013 at 9:13 AM, Rafael Espindola <rafael.espindola at gmail.com
> wrote:

> Author: rafael
> Date: Wed Oct  9 11:13:15 2013
> New Revision: 192300
>
> URL: http://llvm.org/viewvc/llvm-project?rev=192300&view=rev
> Log:
> Use aliases for more constructors and destructors.
>
> With this patch we produce alias for cases like
>
> template<typename T>
> struct foobar {
>   foobar() {
>   }
> };
> template struct foobar<void>;
>
> It is safe to use aliases to weak symbols, as long and the alias itself is
> also
> weak.
>
> Modified:
>     cfe/trunk/lib/CodeGen/CGCXX.cpp
>     cfe/trunk/test/CodeGenCXX/destructors.cpp
>
> Modified: cfe/trunk/lib/CodeGen/CGCXX.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCXX.cpp?rev=192300&r1=192299&r2=192300&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CGCXX.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CGCXX.cpp Wed Oct  9 11:13:15 2013
> @@ -108,32 +108,17 @@ bool CodeGenModule::TryEmitDefinitionAsA
>    // support aliases with that linkage, fail.
>    llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl);
>
> -  switch (Linkage) {
> -  // We can definitely emit aliases to definitions with external linkage.
> -  case llvm::GlobalValue::ExternalLinkage:
> -  case llvm::GlobalValue::ExternalWeakLinkage:
> -    break;
> -
> -  // Same with local linkage.
> -  case llvm::GlobalValue::InternalLinkage:
> -  case llvm::GlobalValue::PrivateLinkage:
> -  case llvm::GlobalValue::LinkerPrivateLinkage:
> -    break;
> -
> -  // We should try to support linkonce linkages.
> -  case llvm::GlobalValue::LinkOnceAnyLinkage:
> -  case llvm::GlobalValue::LinkOnceODRLinkage:
> -    return true;
> -
> -  // Other linkages will probably never be supported.
> -  default:
> +  // We can't use an alias if the linkage is not valid for one.
> +  if (!llvm::GlobalAlias::isValidLinkage(Linkage))
>      return true;
> -  }
>
>    llvm::GlobalValue::LinkageTypes TargetLinkage
>      = getFunctionLinkage(TargetDecl);
>
> -  if (llvm::GlobalValue::isWeakForLinker(TargetLinkage))
> +  // Don't create an strong alias to a linker weak symbol. If the linker
> +  // decides to drop the symbol, the alias would become undefined.
> +  if (llvm::GlobalValue::isWeakForLinker(TargetLinkage) &&
> +      !llvm::GlobalValue::isWeakForLinker(Linkage))
>      return true;
>
>    // Derive the type for the alias.
>
> Modified: cfe/trunk/test/CodeGenCXX/destructors.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/destructors.cpp?rev=192300&r1=192299&r2=192300&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/CodeGenCXX/destructors.cpp (original)
> +++ cfe/trunk/test/CodeGenCXX/destructors.cpp Wed Oct  9 11:13:15 2013
> @@ -6,10 +6,16 @@
>  // CHECK: @_ZN5test11OD2Ev = alias {{.*}} @_ZN5test11AD2Ev
>  // CHECK: @_ZN5test11SD2Ev = alias bitcast {{.*}} @_ZN5test11AD2Ev
>
> +// CHECK: @_ZN6test106foobarIvEC1Ev = alias weak_odr void
> (%"struct.test10::foobar"*)* @_ZN6test106foobarIvEC2Ev
> +
> +// CHECK: @_ZN6test116foobarIvEC1Ev = alias linkonce_odr void
> (%"struct.test11::foobar"*)* @_ZN6test116foobarIvEC2Ev
> +
>  // CHECK: @_ZN5test312_GLOBAL__N_11DD1Ev = alias internal {{.*}}
> @_ZN5test312_GLOBAL__N_11DD2Ev
>  // CHECK: @_ZN5test312_GLOBAL__N_11DD2Ev = alias internal bitcast {{.*}}
> @_ZN5test312_GLOBAL__N_11CD2Ev
>  // CHECK: @_ZN5test312_GLOBAL__N_11CD1Ev = alias internal {{.*}}
> @_ZN5test312_GLOBAL__N_11CD2Ev
>
> +// CHECK: @_ZN6PR752617allocator_derivedD1Ev = alias linkonce_odr void
> (%"struct.PR7526::allocator_derived"*)* @_ZN6PR752617allocator_derivedD2Ev
> +
>  struct A {
>    int a;
>
> @@ -44,9 +50,6 @@ namespace PR7526 {
>    // CHECK: call void @__cxa_call_unexpected
>    allocator::~allocator() throw() { foo(); }
>
> -  // CHECK-LABEL: define linkonce_odr void
> @_ZN6PR752617allocator_derivedD1Ev(%"struct.PR7526::allocator_derived"*
> %this) unnamed_addr
> -  // CHECK-NOT: call void @__cxa_call_unexpected
> -  // CHECK:     }
>    void foo() {
>      allocator_derived ad;
>    }
> @@ -419,3 +422,25 @@ namespace test9 {
>    // CHECK: ret void
>
>    // CHECK: attributes [[NUW]] = {{[{].*}} nounwind {{.*[}]}}
> +
> +
> +namespace test10 {
> +  template<typename T>
> +  struct foobar {
> +    foobar() {
> +    }
> +  };
> +
> +  template struct foobar<void>;
> +}
> +
> +namespace test11 {
> +  void g();
> +  template<typename T>
> +  struct foobar {
> +    foobar() {
> +      g();
> +    }
> +  };
> +  foobar<void> x;
> +}
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131009/133e8697/attachment.html>


More information about the cfe-commits mailing list