This is an archive of the discontinued LLVM Phabricator instance.

[InstrProf] Allow noprofile functions to be inlined
AbandonedPublic

Authored by ellis on Jul 29 2022, 4:39 PM.

Details

Summary

Allow callees with the noprofile attribute to be inlined into profiled callers.

We *keep* the restriction that profiled callees *cannot* be inlined into callers with the noprofile attribute.

This was discussed in [0].

Also, remove the areOutlineCompatible() function which appears to be
unused.

[0] https://discourse.llvm.org/t/why-does-the-noprofile-attribute-restrict-inlining/64108

Diff Detail

Event Timeline

ellis created this revision.Jul 29 2022, 4:39 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 29 2022, 4:39 PM
ellis edited the summary of this revision. (Show Details)Jul 29 2022, 4:44 PM
ellis published this revision for review.Jul 29 2022, 4:52 PM

Please let me know if there is a reason for preventing noprofile functions from being inlined into profiled functions. If there is one I'd like to understand it.

Herald added a project: Restricted Project. · View Herald TranscriptJul 29 2022, 4:52 PM
MaskRay accepted this revision.Aug 2 2022, 12:34 AM

I think the update makes sense.

This revision is now accepted and ready to land.Aug 2 2022, 12:34 AM

Do you mind editing the description to use caller and callee rather than foo?

nickdesaulniers added inline comments.Aug 2 2022, 9:31 AM
llvm/lib/IR/Attributes.cpp
2042–2044

Could we create a new CompatRule fn for this? I imagine we might want to reuse such logic for other attributes in the future.

ellis edited the summary of this revision. (Show Details)Aug 2 2022, 12:27 PM
ellis added inline comments.Aug 2 2022, 12:33 PM
llvm/lib/IR/Attributes.cpp
2042–2044

IMO the CompatRule unnecessarily obfuscates the code. It took me a while to find this issue because I didn't see CompatRule on this attribute. Also, it requires reading several Attributes.* files to fully understand what's going on.

Instead, I could pull this out into a regular function and we can later add more calls with the other attributes. Let me know what you think!

nickdesaulniers accepted this revision.Aug 2 2022, 2:28 PM

I observed no new build or runtime warnings when compiling the linux kernel with gcov enabled (CONFIG_GCOV_KERNEL=y + CONFIG_GCOV_PROFILE_ALL=y) or kcsan (CONFIG_KCSAN=y) (x86).

ellis updated this revision to Diff 449457.Aug 2 2022, 3:19 PM

Create function to check attribute compatability so that we can easily do this for more attributes in the future.

ellis added a comment.Aug 2 2022, 3:19 PM

I observed no new build or runtime warnings when compiling the linux kernel with gcov enabled (CONFIG_GCOV_KERNEL=y + CONFIG_GCOV_PROFILE_ALL=y) or kcsan (CONFIG_KCSAN=y) (x86).

Thanks for checking!

melver added a comment.Aug 3 2022, 1:05 AM

I observed no new build or runtime warnings when compiling the linux kernel with gcov enabled (CONFIG_GCOV_KERNEL=y + CONFIG_GCOV_PROFILE_ALL=y) or kcsan (CONFIG_KCSAN=y) (x86).

Thanks for checking!

If a callee with the noprofile attribute is inlined into a profiled function, will it then also be profiled?
Is it possible that a noprofile function could receive instrumentation through inlining now?

If so, this is wrong and will cause problems. Perhaps for profiling instrumentation it works differently, but for the sanitizers this was an issue we fought for a while to get right everywhere: https://lore.kernel.org/lkml/CANpmjNNRz5OVKb6PE7K6GjfoGbht_ZhyPkNG9aD+KjNDzK7hGg@mail.gmail.com/

llvm/test/Transforms/Inline/inline_noprofile.ll
22

noprofile (no space)

ellis added a comment.Aug 3 2022, 12:25 PM

I observed no new build or runtime warnings when compiling the linux kernel with gcov enabled (CONFIG_GCOV_KERNEL=y + CONFIG_GCOV_PROFILE_ALL=y) or kcsan (CONFIG_KCSAN=y) (x86).

Thanks for checking!

If a callee with the noprofile attribute is inlined into a profiled function, will it then also be profiled?
Is it possible that a noprofile function could receive instrumentation through inlining now?

If so, this is wrong and will cause problems. Perhaps for profiling instrumentation it works differently, but for the sanitizers this was an issue we fought for a while to get right everywhere: https://lore.kernel.org/lkml/CANpmjNNRz5OVKb6PE7K6GjfoGbht_ZhyPkNG9aD+KjNDzK7hGg@mail.gmail.com/

The callee won’t technically be profiled, but because it was inlined it could be mixed with instrumentation code. My reasoning was, if a function interacts badly when inlined into an instrumented function, then it probably shouldn’t be inlined at all.

...
If a callee with the noprofile attribute is inlined into a profiled function, will it then also be profiled?
Is it possible that a noprofile function could receive instrumentation through inlining now?

If so, this is wrong and will cause problems. Perhaps for profiling instrumentation it works differently, but for the sanitizers this was an issue we fought for a while to get right everywhere: https://lore.kernel.org/lkml/CANpmjNNRz5OVKb6PE7K6GjfoGbht_ZhyPkNG9aD+KjNDzK7hGg@mail.gmail.com/

The callee won’t technically be profiled, but because it was inlined it could be mixed with instrumentation code. My reasoning was, if a function interacts badly when inlined into an instrumented function, then it probably shouldn’t be inlined at all.

As long as the code in the callee doesn't receive any instrumentation, I think we're fine; i.e. the instructions in the callee are not intermingled with instrumentation, and from the machine's point of view what's missing is the call overhead.

ellis abandoned this revision.Aug 4 2022, 2:53 PM

...
If a callee with the noprofile attribute is inlined into a profiled function, will it then also be profiled?
Is it possible that a noprofile function could receive instrumentation through inlining now?

If so, this is wrong and will cause problems. Perhaps for profiling instrumentation it works differently, but for the sanitizers this was an issue we fought for a while to get right everywhere: https://lore.kernel.org/lkml/CANpmjNNRz5OVKb6PE7K6GjfoGbht_ZhyPkNG9aD+KjNDzK7hGg@mail.gmail.com/

The callee won’t technically be profiled, but because it was inlined it could be mixed with instrumentation code. My reasoning was, if a function interacts badly when inlined into an instrumented function, then it probably shouldn’t be inlined at all.

As long as the code in the callee doesn't receive any instrumentation, I think we're fine; i.e. the instructions in the callee are not intermingled with instrumentation, and from the machine's point of view what's missing is the call overhead.

I believe this patch does cause the instructions in the callee to "intermingle with instrumentation", so it might not be safe without source changes.

I've just created the skipprofile attribute which doesn't have this inlining restriction, so this patch isn't necessary. I've also updated the docs for noprofile to be more clear.

Thanks for clarifying this case and I'll abandon this diff now.