-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LLVM should have a nocapture parameter attribute for functions that don't let a pointer value escape #2427
Comments
assigned to @nlewycky |
This could make AddressMightEscape more aggressive in BasicAA, giving better mod/ref info for calls. This property is true of a huge variety of pointer arguments to functions, e.g. strlen, memcpy, etc. Knowledge of this property allows us to optimize things like this: char buffer[100]; strlen(buffer); // buffer doesn't escape x = buffer[0]; This property could also be inferred for a lot of functions by globalsmodref. |
Since strlen is 'readonly' this doesn't seem like a good example. |
The point here is that strlen doesn't capture the pointer, so foo couldn't modify it. nocapture and readonly are orthogonal concepts. |
Can a readonly function capture a pointer? It can't write it |
This is readnone but does capture its argument: |
OK, but llvm-gcc nonetheless seems to have no trouble Also, what is the policy with attribute propagation vs having My answer is that it speeds things up if later the module is I like the structure of having (1) an attribute deducing analysis |
It must be 'proving' that it is unnecessary some other way. It would be interesting to find out how it decides it is useless.
That would make sense, alternatively we could add it to simplifylibcalls (yuck), or add a llvm-specific table somewhere in the front-end (less yuck, but still strange). I have no preference for how it is implemented in llvm-gcc.
Right.
I'm not sure what you mean by "we have the analysis anyway". An analysis and attrs are two very different things. Attributes are nice because they can be cheaply computed by a variety of means (e.g. tables) and are efficient to propagate in this case. Analyses are nice because they can do arbitrary weird things, like deciding that the function writes, but only reads one argument, or something. You can also put ad-hoc knowledge into passes, such as the mod/ref behavior of printf or scanf, which requires analyzing the format string. The current GlobalsModRef was written before we had attributes, so it is probably doing too much or doing what it does in the wrong way now. It should be revisited. Ideally running it wouldn't build up heavy heap datastructures, it would just add propagate and update tags on the functions.
Me too :)
That would be reasonable too. At this point, I'm mostly concerned with optimizing various memory things, like your crazy memcpy cases, so I'm focused on readonly/nocapture etc, but optimizing and improving EH info is also useful. -Chris |
The other useful thing about this is that it lets us do partial mem2reg/sroa for stuff that would otherwise be prevented due to byref args. For example: void foo(int &X) NOINLINE { X = 1; } int bar() { for (i = 0 .. 100) Right now, foo pins X to the stack so mem2reg can't promote it in the loop. If we had no capture, we could promote allocas and just store/reload around calls like this. -Chris |
I added this a while ago. Missed optz'ns with nocapture should get their own bugs. |
Fix the guaranteed alignment of memory returned by malloc/new on Darwin
Extended Description
Chris pointed out that it would be useful to have a "nocapture" parameter attribute. Its semantics would be that a pointer value passed in with the "nocapture" attribute can be assumed never to escape from the callee (never stored to a global or passed to another function where it could escape).
The text was updated successfully, but these errors were encountered: