This is an archive of the discontinued LLVM Phabricator instance.

[ThinLTO] Convert dead alias to declarations
ClosedPublic

Authored by tejohnson on Feb 2 2018, 10:21 AM.

Details

Summary

This complements the fixes in r323633 and r324075 which drop the
definitions of dead functions and variables, respectively.

Fixes PR36208.

Diff Detail

Repository
rL LLVM

Event Timeline

tejohnson created this revision.Feb 2 2018, 10:21 AM
grimar added inline comments.Feb 5 2018, 5:25 AM
lib/LTO/LTOBackend.cpp
414 ↗(On Diff #132625)

With the new change convertToDeclaration becomes a dangerous function to use in loops.

Currently there is one more place where code iterates over aliases in a "old" way and may also fail I think:
https://github.com/llvm-mirror/llvm/blob/master/lib/Transforms/IPO/FunctionImport.cpp#L670
https://github.com/llvm-mirror/llvm/blob/master/lib/Transforms/IPO/FunctionImport.cpp#L689

What about if we stop calling eraseFromParent from convertToDeclatation
and do something explicit like:

static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
                            const ModuleSummaryIndex &Index) {
  std::vector<GlobalValue *> ReplacedGlobals;
  for (auto &GV : Mod.global_values())
    if (GlobalValueSummary *GVS = DefinedGlobals.lookup(GV.getGUID()))
      if (!Index.isGlobalValueLive(GVS))
        if (!convertToDeclaration(GV))
          ReplacedGlobals.push_back(&GV);

  for (GlobalValue *GV : ReplacedGlobals)
    GV->eraseFromParent();
}

So convertToDeclaration would return true if value was converted to declaration and
false if it was replaced.

Though we can refactor this place separatelly in a followups, probably with use of
some better approach, but at least caller from FunctionImport.cpp should also be changed too then.

lib/Transforms/IPO/FunctionImport.cpp
634 ↗(On Diff #132625)

Looks else part is not covered by any tests atm.
(I replaced it with else assert(false) and check-all showed no failtures).

Suggest to add variable with alias to your testcase
and check them are converted to declarations, like:

; RUN:   -r %t1.bc,var,x -r %t1.bc,varAlias,x \
; RUN:   -o %t2.o -save-temps
...
; DROP-DAG: @var = external global i32
; DROP-DAG: @varAlias = external global i32
...
@var = global i32 99
@varAlias = alias i32, i32* @var
tejohnson marked 2 inline comments as done.Feb 5 2018, 6:38 AM
tejohnson added inline comments.
lib/LTO/LTOBackend.cpp
414 ↗(On Diff #132625)

I changed as suggested as that is less problematic for other callsites.
Note that there won't currently be a problem at the other callsite since it doesn't call convertToDeclaration with an alias (otherwise it previously would have hit the lllvm_unreachable). I added an assert there that convertToDeclaration returns true, and a fixme that it should be changed once thinLTOResolveWeakForLinkerGUID is changed to handle aliases (which it can now be with this change in convertToDeclaration).

tejohnson updated this revision to Diff 132820.Feb 5 2018, 6:39 AM
tejohnson marked an inline comment as done.

Address comments

grimar accepted this revision.Feb 5 2018, 7:00 AM

LGTM, thanks !
(minor suggestions below)

lib/LTO/LTOBackend.cpp
410 ↗(On Diff #132820)

Please format (it should be 2 lines after clang-formatting I think).

414 ↗(On Diff #132625)

Note that there won't currently be a problem at the other callsite since it doesn't call convertToDeclaration with an alias > (otherwise it previously would have hit the llvm_unreachable).

Probably yes, though it is also seems can be possible that we do not have such test that would go that path to hit unreachable at this moment.

lib/Transforms/IPO/FunctionImport.cpp
684 ↗(On Diff #132820)

I suggest to use llvm_unreachable as we explicitly do not support/implement this path
while assert will be just eliminated in release builds.

if (!convertToDeclaration(GV))
  llvm_unreachable("Expected GV to be converted");
This revision is now accepted and ready to land.Feb 5 2018, 7:00 AM
tejohnson marked 2 inline comments as done.Feb 5 2018, 7:17 AM
tejohnson added inline comments.
lib/LTO/LTOBackend.cpp
410 ↗(On Diff #132820)

clang-format did this, but it turns out it had the wrong style set as my default. Fixed.

tejohnson updated this revision to Diff 132827.Feb 5 2018, 7:18 AM
tejohnson marked an inline comment as done.

Address comments

This revision was automatically updated to reflect the committed changes.