2

In which scenarios should I care about it/place it for LLVM? I have read the following doc but need more detailed examples if anyone could lift .What does volatile mean exactly in LLVM? Citation: Volatile Memory Accesses, where "volatile" is defined in LLVM.

hddmss
  • 231
  • 1
  • 12
  • 1
    The typical example is an access to a memory-mapped hardware register. The mere act of loading from such an address may have side effects, and subsequent loads may return different data even if there has been no store in the meantime. Likewise, a store may have side effects even if it the data is the same as a previous store. As such, loads and stores must not be reordered nor optimized away even if they otherwise appear redundant. – Nate Eldredge Aug 20 '21 at 21:34
  • 2
    It seems to me like the linked document explains pretty precisely what `volatile` means to LLVM. Did you have specific questions about how LLVM handles it, or was your question more about when it would be used in the first place? – Nate Eldredge Aug 20 '21 at 21:35
  • Also see https://stackoverflow.com/questions/72552/why-does-volatile-exist, https://stackoverflow.com/questions/4437527/why-do-we-use-volatile-keyword?noredirect=1&lq=1 for a discussion at the level of C/C++. – Nate Eldredge Aug 20 '21 at 21:37
  • @NateEldredge Thanks for your comments and pointers. Yeah, I want to know both when `volatile` should be used and how LLVM handles it. Following your first comment, does that mean I should always want to place volatile for LLVM if I don't want the loads and stores be reordered (if they can feasibly reorder) at runtime? – hddmss Aug 20 '21 at 21:51
  • Right. I guess another way to say it is, if you *don't* use volatile, the compiler can assume the address behaves like normal memory that is not being accessed by any other thread or hardware: the only loads and stores are those which the compiler knows about, they have no side effects, and a load can always be assumed to return the value most recently stored. – Nate Eldredge Aug 20 '21 at 21:59
  • Wait a second, runtime? `volatile` only affects how LLVM emits load and store instructions, i.e. the "program order" in which those instructions execute. It does not inhibit the CPU from changing the order in which the loads and stores become visible to an external observer (e.g. another core). If you need that then you have to insert the proper barriers or ordering constraints yourself, which is described further down in that document. In essence `volatile` will give you compiler-only barriers but not runtime memory barriers. – Nate Eldredge Aug 20 '21 at 23:29
  • @NateEldredge Ouch! Runtime is harder to deal with I guess... – hddmss Aug 21 '21 at 00:20

0 Answers0