Functions have two types, one can be mutated but not the other

Function has its own FunctionType* member as well as a Type* member that it inherits from GlobalValue. The latter can be mutated but not the former, leading to potential strange inconsistencies.

While I realize using mutateType is probably going to trigger a bunch of “you’re doing it wrong” replies, it seems like mutateType, as a necessary evil, should be virtual and do the right wrong thing for Functions too.

Thoughts?

Correction: there are 3 Type * members – one on Function, one on GlobalValue, and one on Value. The last is mutable, but not the other two.

I've been perpetrating some of this for the typeless/opaque pointer IR
stuff (some details here: 2015 LLVM Developers’ Meeting: David Blaikie “Opaque Pointer Types" - YouTube - or
on a few threads on llvm-dev).

I got rid of that extra one in Function - thanks for noticing.

If I recall correctly, even before my work, it wasn't possible to rely
solely on mutateType of the operands so various special cases have been
added (ValueMapper.cpp is a good example of some of these special cases)

I'm not sure why those issues might not have come up when dealing with
Globals. Perhaps you have some examples (eg: linking bitcode with globals -
where the value type gets out of sync with the pointer type? Maybe if we
add some more assertions in to check the two values are in sync we'd find
cases where they've gotten out of sync?)

Thanks. Our use case is pretty simple:

Create a function with some placeholder type.

Figure out the actual function type and create some IR that calls the function with that type. Update function’s type via mutateType.

Dump the module. The function’s “declaration” still shows it has the placeholder type.

This doesn’t cause us any problems because the IR is what matters and it’s got the right description.

FWIW we do this because the transformation from an MSIL signature to an ABI-level signature is somewhat involved, and we’ve intertwined the code to generate the right call sequence with the code that figures out what type you end up with once you’ve done all the various transformations, so to start things off, we need a way to refer to the function without knowing precisely what type it will end up with.

Thanks. Our use case is pretty simple:

Create a function with some placeholder type.

Figure out the actual function type and create some IR that calls the
function with that type. Update function’s type via mutateType.

Dump the module. The function’s “declaration” still shows it has the
placeholder type.

This doesn’t cause us any problems because the IR is what matters and it’s
got the right description.

Hrm. It'd be nice to be able to demonstrate the problem with some usage
inside the LLVM project. I've tried adding an assertion in
llvm::Function::getFunctionType (& tried in GlobalValue::getValueType too)
that asserts that the pointee type of the type matches the value type - but
it does not fire anywhere in the test suite.

Perhaps you could think of other places in the code where we could check
this property and demonstrate that it gets out of sync (the main place
where it could get out of sync is the LTO linker - but also I think the
vectorizer uses mutateType too)?

FWIW we do this because the transformation from an MSIL signature to an
ABI-level signature is somewhat involved, and we’ve intertwined the code to
generate the right call sequence with the code that figures out what type
you end up with once you’ve done all the various transformations, so to
start things off, we need a way to refer to the function without knowing
precisely what type it will end up with.

Interesting to understand - thanks for the context.