This is an archive of the discontinued LLVM Phabricator instance.

CGDecl::emitStoresForConstant fix synthesized constant's name
ClosedPublic

Authored by jfb on Nov 2 2018, 3:48 PM.

Details

Summary

The name of the synthesized constants for constant initialization was using mangling for statics, which isn't generally correct and (in a yet-uncommitted patch) causes the mangler to assert out because the static ends up trying to mangle function parameters and this makes no sense. Instead, mangle to "__const." + FunctionName + "." + DeclName.

Diff Detail

Event Timeline

jfb created this revision.Nov 2 2018, 3:48 PM
jfb added a comment.Nov 2 2018, 3:50 PM

I'm not sure this is the right fix because mangling confuses me. It fixes the assertion I'm encountering in my patch, and I don't think I can create a repro without the patch (since I'm asking to mangle a local in a way we don't seem to right now).

Can you give an example of some code that triggered this with your patch applied? Even if it isn't a real reproducer right now, it would help to try to understand whats happening here.

jfb added a comment.Nov 2 2018, 4:22 PM

Can you give an example of some code that triggered this with your patch applied? Even if it isn't a real reproducer right now, it would help to try to understand whats happening here.

Sure!

I have code like this: getStaticDeclName(CGM, D) where D is vla in:

void foo(int size) {
  int vla[size];
  // ...
}

That sounds more like this use of the mangler isn't manipulating the function type context correctly. But actually I think the problem is that it's ridiculous to assume that arbitrary local declarations have meaningful manglings. Why are we calling getStaticDeclName on a variable that's obviously not static?

jfb added a comment.Nov 2 2018, 10:33 PM

That sounds more like this use of the mangler isn't manipulating the function type context correctly. But actually I think the problem is that it's ridiculous to assume that arbitrary local declarations have meaningful manglings. Why are we calling getStaticDeclName on a variable that's obviously not static?

It was done in CodeGenFunction::EmitAutoVarInit a while ago. I moved it since then, but it's the same thing. I'm happy to mangle it any other way. At the end of the day we just need some name for an (unnamed address) global which is synthesized from a function-local initialization. We could just take the mangled function name and append something to it.

In D54055#1286397, @jfb wrote:

That sounds more like this use of the mangler isn't manipulating the function type context correctly. But actually I think the problem is that it's ridiculous to assume that arbitrary local declarations have meaningful manglings. Why are we calling getStaticDeclName on a variable that's obviously not static?

It was done in CodeGenFunction::EmitAutoVarInit a while ago. I moved it since then, but it's the same thing. I'm happy to mangle it any other way. At the end of the day we just need some name for an (unnamed address) global which is synthesized from a function-local initialization. We could just take the mangled function name and append something to it.

Okay. I assume this is internal-linkage and the name is just for debugging purposes? Maybe we could have an API to generate a best-effort name mangling that could intentionally punt on variably-modified types. (I'm not really sure why the type is being mangled here anyway.)

jfb added a comment.Nov 3 2018, 11:43 AM
In D54055#1286397, @jfb wrote:

That sounds more like this use of the mangler isn't manipulating the function type context correctly. But actually I think the problem is that it's ridiculous to assume that arbitrary local declarations have meaningful manglings. Why are we calling getStaticDeclName on a variable that's obviously not static?

It was done in CodeGenFunction::EmitAutoVarInit a while ago. I moved it since then, but it's the same thing. I'm happy to mangle it any other way. At the end of the day we just need some name for an (unnamed address) global which is synthesized from a function-local initialization. We could just take the mangled function name and append something to it.

Okay. I assume this is internal-linkage and the name is just for debugging purposes? Maybe we could have an API to generate a best-effort name mangling that could intentionally punt on variably-modified types.

I can do that, if you think that's the right approach. Or I can use mangled function name + something (instead of trying to synthesize a static mangling). That seems pretty simple and it'll always work.

(I'm not really sure why the type is being mangled here anyway.)

Maybe the author of http://llvm.org/r127227 knows? :-)

Pfft, if I'm going to be held responsible for my own work, I'm in a lot of trouble. :)

Function name + local variable name WFM.

jfb updated this revision to Diff 173945.Nov 13 2018, 3:12 PM
  • As discussed on the review, change the constant generation to name the synthesized constant using '__const.' + function_name + '.' variable_name. The previous code wasn't generally correct because it was trying to mangle the variable as if it were a static, and in some cases this could pull in e.g. parameter names (which is nonsensical for a static). This only occurs with a patch which I haven't submitted yet, but my change is pretty well tested already as the modified tests show.
jfb added a comment.Nov 13 2018, 3:13 PM

Pfft, if I'm going to be held responsible for my own work, I'm in a lot of trouble. :)

Function name + local variable name WFM.

Your wish is my command (in this specific instance anyways).

jfb updated this revision to Diff 173946.Nov 13 2018, 3:14 PM
  • clang-format
jfb retitled this revision from CXXNameMangler::mangleFunctionParam: fix assertion to CGDecl::emitStoresForConstant fix synthesized constant's name.Nov 13 2018, 3:18 PM
jfb edited the summary of this revision. (Show Details)
rjmccall accepted this revision.Nov 13 2018, 4:05 PM

Seems reasonable to me.

This revision is now accepted and ready to land.Nov 13 2018, 4:05 PM
jfb updated this revision to Diff 174087.Nov 14 2018, 12:52 PM
  • Fix lifetime of the name, Twine bites again.
jfb added a comment.Nov 14 2018, 12:53 PM

Small update to use std::string because Twine lifetimes are sad. This didn't trigger on any of the tests, but did in a stage2 build.

dexonsmith added inline comments.Nov 14 2018, 12:55 PM
lib/CodeGen/CGDecl.cpp
990–998 ↗(On Diff #174087)

Can you just kill Name entirely, throwing this inline, to avoid the malloc?

jfb updated this revision to Diff 174090.Nov 14 2018, 1:14 PM
  • Inline the name creating, save a heap allocation.
jfb marked an inline comment as done.Nov 14 2018, 1:15 PM
jfb added inline comments.
lib/CodeGen/CGDecl.cpp
990–998 ↗(On Diff #174087)

Sure. It seems less readable, but since the function is already factored out it's not that bad.

This revision was automatically updated to reflect the committed changes.
jfb marked an inline comment as done.
nhaehnle removed a subscriber: nhaehnle.Nov 16 2018, 3:41 AM