2

I searched around for this, but was unable to find a conclusive answer. What parameters do I need to pass to __builtin_prefetch to fetch a cacheline in exclusive mode (i.e. after prefetching, the cacheline is going to be written to at some point in the future)?

For reference, this is an outline of the code I have

void foo(std::uint8_t* line) {
  assert(!(line % std::hardware_constructive_interference_size));
  prefetch(bytes + std::hardware_constructive_interference_size);
  auto result = bar(line);
  write(bytes + std::hardware_constructive_interference_size);
}

Is there even a benefit in adding a prefetch here? Or will the processor typically always prefetch data in this case?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Curious
  • 20,870
  • 8
  • 61
  • 146
  • 2
    `__builtin_prefetch` has optional argument to indicate whether you want to read or write. Please read the [documentation](https://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Other-Builtins.html). – gudok Feb 22 '19 at 06:44
  • @gudok I see, so the second argument should be a 1 in my case? Also the second part of the question - is there a benefit in prefetching an adjacent cacheline? Or does the processor do that itself usually? – Curious Feb 22 '19 at 06:48
  • @xaxxon On a slightly related note - my guess is that the C++ community is probably the best place to go to for questions like this no? – Curious Feb 22 '19 at 06:55
  • 1
    If they want to answer non-c++ questions, then they can follow other tags. – xaxxon Feb 22 '19 at 06:56
  • 2
    I don't think it was correct to remove the C++ tag. If you're asking how to prefetch a cacheline in exclusive mode in a C++ program, then the tag is appropriate. – Ross Ridge Feb 22 '19 at 06:58
  • @RossRidge This has confused me in the past as well at times. Is there some general SO guidance for tag usage somewhere? I feel like this comes up quite often on questions so would be useful to add if someone has the power to do so – Curious Feb 22 '19 at 06:59
  • 1
    Yes, guidance is given in the tag description: "Use this tag for questions about code (to be) compiled with a C++ compiler." https://stackoverflow.com/tags/c%2b%2b/info – Ross Ridge Feb 22 '19 at 07:05
  • Hmmm, that does seem to direct this as a question suitable for the C++ tag with that wording, I guess if it's alright with @xaxxon we can add that back here? I would love to have more eyes from the C++ community on this :) – Curious Feb 22 '19 at 07:06
  • `prefetchw` is exactly for PF into Exclusive state, in anticipation of a store. https://felixcloutier.com/x86/prefetchw. Supported in Broadwell and later on Intel, and since 3Dnow! with AMD. (Non-faulting for a long time before that on Intel, but before BDW it didn't set the feature bit and ran it as a NOP so SW like Windows could us it unconditionally.) [X64 instructions that behave differently on different CPUs](//stackoverflow.com/q/40621827). GCC uses it for `__builtin_prefetch` if it's available according to `-march=`. – Peter Cordes Feb 22 '19 at 20:48
  • (previous comment copied from the deleted answer to explain why this is a duplicate). – Peter Cordes Feb 22 '19 at 20:50

0 Answers0