llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

comdats cause inlined linkonce_odr functions to be retained at -O0/-O1

Open dwblaikie opened this issue 11 years ago • 6 comments

Bugzilla Link 22285
Resolution FIXED
Resolved on May 05, 2015 15:14
Version trunk
OS Linux
CC @compnerd,@rnk,@wolfy1961

Extended Description

$ cat inl.ll $f1 = comdat any define void @​f2() { entry: call void @​f1() ret void }

define linkonce_odr void @​f1() comdat { entry: ret void } $ opt -S -inline inl.ll -o - | grep define define void @​f2() { define linkonce_odr void @​f1() comdat { $ cat inl2.ll define void @​f2() { entry: call void @​f1() ret void } define linkonce_odr void @​f1() { entry: ret void } $ opt inl2.ll -S -inline -o - | grep define define void @​f2() { $

The problem seems to be at lib/Transforms/IPO/Inliner.cpp:678: // It is unsafe to drop a function with discardable linkage from a COMDAT // without also dropping the other members of the COMDAT. // The inliner doesn't visit non-function entities which are in COMDAT // groups so it is unsafe to do so unless the linkage is local. if (!F->hasLocalLinkage() && F->hasComdat()) continue;

Which is correct, but very conservative. At higher optimization levels some other pass (I'll check which one in a moment) must kick in and still manage to cleanup these dead comdats.

Since a comdat doesn't refer to its members, it's not trivial to just ask "is this a comdat with only one element" and then drop the function (& comdat) anyway. Maybe it needs a separate pass specifically looking for comdats whos elements are all unused linkonce_odr elements?

dwblaikie avatar Jan 21 '15 21:01 dwblaikie

Fixed in r236539.

majnemer avatar May 05 '15 22:05 majnemer

Could alwaysinline functions be removed safely? Right now the constructor for foo() is generated at O0 and O1 here:

class foo { public: inline __attribute( ( always_inline ) ) foo() { } };

struct bar { foo foo1; foo foo2; foo foo3; foo foo4; };

bar bars[ 512 ];

wolfy1961 avatar Feb 28 '15 02:02 wolfy1961

It's hard to track which global objects are in a comdat because a Comdat is not a Value, but David and I figured that a "number of comdat members" refcount would be good enough and easy to implement. The zero member and single member comdat group cases are interesting special cases.

Yes, reference counting comdats is probably the way to got. This would also make it easy to remove dead comdats. It would also make it easy to ignore dead comdats, allowing the verifier to get back the tests it lost in r230051.

llvmbot avatar Feb 20 '15 22:02 llvmbot

It's hard to track which global objects are in a comdat because a Comdat is not a Value, but David and I figured that a "number of comdat members" refcount would be good enough and easy to implement. The zero member and single member comdat group cases are interesting special cases.

rnk avatar Jan 22 '15 01:01 rnk

Seems GlobalDCE is the pass that is enabled at -O2 and is smart enough to remove these unused comdats.

dwblaikie avatar Jan 21 '15 21:01 dwblaikie

assigned to @majnemer

dwblaikie avatar Jan 21 '15 21:01 dwblaikie