This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Attach no-builtin attributes to function definitions with no Decl
ClosedPublic

Authored by thegameg on Jan 27 2020, 10:54 AM.

Details

Summary

When using -fno-builtin[-<name>], we don't attach the IR attributes to function definitions with no Decl, like the ones created through CreateGlobalInitOrDestructFunction.

This results in projects using -fno-builtin or -ffreestanding to start seeing symbols like _memset_pattern16.

The fix changes the behavior to always add the attribute if LangOptions requests it.

Diff Detail

Event Timeline

thegameg created this revision.Jan 27 2020, 10:54 AM
efriedma added inline comments.Jan 27 2020, 1:46 PM
clang/lib/CodeGen/CGCall.cpp
1916

What happens if we have a TargetDecl that isn't a FunctionDecl? (I think this happens in some cases, but not completely sure which, exactly.)

thegameg marked an inline comment as done.Jan 27 2020, 5:17 PM
thegameg added inline comments.
clang/lib/CodeGen/CGCall.cpp
1916

It looks like that can be triggered by indirect calls:

typedef void T(void);
void test3(T f) {
  f();
}

Since this adds the attribute to definitions and not to call sites, we should never need that.

This patch is for the case where CreateGlobalInitOrDestructFunction ends up re-using the same function to attach the attributes.

I'll update the description to make it more clear.

thegameg retitled this revision from [CodeGen] Attach no-builtin attributes to functions with no Decl to [CodeGen] Attach no-builtin attributes to function definitions with no Decl.Jan 27 2020, 5:20 PM
thegameg edited the summary of this revision. (Show Details)
efriedma added inline comments.Jan 27 2020, 6:58 PM
clang/lib/CodeGen/CGCall.cpp
1916

Are you sure that's the only case where the TargetDecl isn't a FunctionDecl? I'm afraid there might be some weird case where the TargetDecl defines something like a function, but isn't technically a FunctionDecl. Maybe an ObjC method or something like that. And then we end up in a situation where addNonCallSiteNoBuiltinAttributes is never called.

thegameg marked 2 inline comments as done.Jan 28 2020, 7:20 AM
thegameg added inline comments.
clang/lib/CodeGen/CGCall.cpp
1916

Right, it looks like in the test suite this triggers on:

  • BlockDecl
  • ObjCMethodDecl
  • CapturedDecl

I think we should call addNonCallSiteNoBuiltinAttributes for these too.

thegameg updated this revision to Diff 240914.Jan 28 2020, 9:47 AM

Add the attribute to all TargetDecls.

efriedma accepted this revision.Jan 28 2020, 1:44 PM

LGTM

There's maybe some argument that we should be calling getNonClosureContext() or something like that to find the parent function, at least for some attributes. But that seems less critical, and I don't really want to think about which attributes should/should not apply right now.

This revision is now accepted and ready to land.Jan 28 2020, 1:44 PM
This revision was automatically updated to reflect the committed changes.

Thx for the fix @thegameg

clang/lib/CodeGen/CGCall.cpp
1845

Can you remove the trailing [-<name>]?