This is an archive of the discontinued LLVM Phabricator instance.

Introduce __builtin_nontemporal_store and __builtin_nontemporal_load.
ClosedPublic

Authored by mzolotukhin on Aug 24 2015, 10:55 PM.

Details

Summary

Currently clang provides no general way to generate nontemporal loads/stores.
There are some architecture specific builtins for doing so (e.g. in x86), but
there is no way to generate non-temporal store on, e.g. AArch64. This patch adds
generic builtins which are expanded to a simple store with '!nontemporal'
attribute in IR.
Previously I tried to tackle the same issue by introducing
attribute((nontemporal)) (see D12221), but was convinced that builtins might
fit for such use better.

Does the patch look good?

Thanks,
Michael

Diff Detail

Event Timeline

mzolotukhin retitled this revision from to Introduce __builtin_nontemporal_store and __builtin_nontemporal_load..
mzolotukhin updated this object.
mzolotukhin added a subscriber: cfe-commits.
rsmith added inline comments.Aug 26 2015, 3:46 PM
include/clang/Basic/Builtins.def
1249–1255

Do we need these typed versions?

include/clang/Basic/DiagnosticSemaKinds.td
6201–6206

Nontemporal loads and stores of, say, vector types seem useful, but I can understand if you want to restrict the complexity of the CodeGen changes here.

lib/CodeGen/CGBuiltin.cpp
128–129

You should pass Val through EmitToMemory first to widen bool from i1 to i8.

149

Likewise, you should pass this though EmitFromMemory.

lib/Sema/SemaChecking.cpp
2236–2242

You should also reject calls with too many arguments here. Maybe just call checkArgCount(*this, TheCall, numArgs)?

2276–2358

Instead of doing all of this, you can just (for stores) check the type of the second parameter or (for loads) set the type of the call expression. (See CheckARMBuiltinExclusiveCall for an example.)

hfinkel edited edge metadata.Aug 27 2015, 12:48 PM

I also like this intrinsic approach better than the type attribute.

The fact that it does not work with builtin vector types, however, is quite unfortunate. Plus, I don't understand why you're implementing another place in CodeGen that generates IR to load and store scalar values. Can't you enhance EmitLoadOfScalar/EmitStoreOfScalar and then use those functions? If nothing else, you're already missing setting of TBAA metadata and other things that these functions do.

mzolotukhin edited edge metadata.

Address review remarks:

  • Remove typed versions - indeed, we don't need them.
  • Allow vector types.
  • Properly handle bool-type (promote i1 to i8).
  • Check arguments number.
  • Simplify SemaBuiltinNontemporalOverloaded (as we don't use typed versions now).
  • Add tests for vector and bool types.

Hi Richard, Hal, and others,

I updated the patch according to review remarks - now we support vector and boolean types too! Could you please take a look?

Thanks,
Michael

lib/CodeGen/CGBuiltin.cpp
128–129

Thanks! Fixed.

149

In this case we already have with i8 value - AFAIU, we get i8* pointer from EmitScalarExpr(E->getArg(0)). Do I miss something here?

lib/Sema/SemaChecking.cpp
2236–2242

Good idea, thanks!

Thanks, but I still have this question:

Plus, I don't understand why you're implementing another place in CodeGen that generates IR to load and store scalar values. Can't you enhance EmitLoadOfScalar/EmitStoreOfScalar and then use those functions? If nothing else, you're already missing setting of TBAA metadata and other things that these functions do.

I still have this question:

Oops, sorry - I missed that. I'll check how can we reuse that logic.

Thanks,
Michael

  • Use EmitStoreOfScalar and EmitLoadOfScalar for generating nontemporal loads and stores.
  • Rebase on TOT.

Hi Hal,

I added Nontemporal field to LValue and started to use it in EmitStoreOfScalar/EmitLoadOfScalar. Does it look like something you had in mind?

Thanks,
Michael

Hi Richard, Hal,

Does the patch look good now?

Thanks,
Michael

Hi Richard, Hal,

Does the patch look good now?

Looks good to me, but please wait for Richard on the changes he requested.

Thanks,
Michael

rsmith accepted this revision.Sep 8 2015, 1:03 PM
rsmith edited edge metadata.

Looks fine to me.

lib/CodeGen/CGBuiltin.cpp
114–128

Replace Make with Emit in both of these.

lib/Sema/SemaChecking.cpp
2245–2246

In the comment on line 2219, you say this has already been done. Please either remove this or fix the comment.

This revision is now accepted and ready to land.Sep 8 2015, 1:03 PM
This revision was automatically updated to reflect the committed changes.

Thanks, Hal, Richard!

I committed the patch with requested changes in r247107.

Michael