Using Linker with a cloned module

Hi,

I was trying to use the Linker with a module cloned from another module
via CloneModule(), and it seems the Linker does not expect this use case.

The issue is in the addTypeMapping() function, which removes a name of
a StructType from the Src module if an isomorphic StructType was found
in the Dst module.

This behavior was introduced in r222986:
http://llvm.org/viewvc/llvm-project?view=revision&revision=222986
"Change how we keep track of which types are in the dest module.
Instead of keeping an explicit set, just drop the names of types we choose
to map to some other type."

This approach does not work if we pass a module clone created by
CloneModule() as the Src, because 'prototype' and 'clone' modules share
the same Type objects, therefore a type name change in one module would
affect the other module as well.
My expectation is that the 'prototype' should not be changed after we
link a 'clone'.

Is there any expected limitations on using cloned modules?

I've attached a unittest to cover this issue.
If fails with the current trunk:
Failure:
  Value of: STyNamesAfter.size()
    Actual: 0
  Expected: STyNamesBefore.size()
  Which is: 1
Failure:
  Value of: NameAfterLink
    Actual: ""
  Expected: NameBeforeLink
  Which is: "struct.foo.0"

    - Andrew

Hi,

My current take on this issue is that named types should not be entirely stored in the context.

We should only store there the “anonymous” type, and have the mapping of name -> type in the modules themselves.

There might be some good reasons why this wouldn’t work, but I haven’t looked deeply enough to be sure.