1 //===- SIMemoryLegalizer.cpp ----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Memory legalizer - implements memory model. More information can be
12 /// http://llvm.org/docs/AMDGPUUsage.html#memory-model
14 //===----------------------------------------------------------------------===//
17 #include "AMDGPUMachineModuleInfo.h"
18 #include "GCNSubtarget.h"
19 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
20 #include "llvm/ADT/BitmaskEnum.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/Support/AtomicOrdering.h"
24 #include "llvm/Support/TargetParser.h"
27 using namespace llvm::AMDGPU
;
29 #define DEBUG_TYPE "si-memory-legalizer"
30 #define PASS_NAME "SI Memory Legalizer"
32 static cl::opt
<bool> AmdgcnSkipCacheInvalidations(
33 "amdgcn-skip-cache-invalidations", cl::init(false), cl::Hidden
,
34 cl::desc("Use this to skip inserting cache invalidating instructions."));
38 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
40 /// Memory operation flags. Can be ORed together.
45 LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ STORE
)
48 /// Position to insert a new instruction relative to an existing
55 /// The atomic synchronization scopes supported by the AMDGPU target.
56 enum class SIAtomicScope
{
65 /// The distinct address spaces supported by the AMDGPU target for
66 /// atomic memory operation. Can be ORed toether.
67 enum class SIAtomicAddrSpace
{
75 /// The address spaces that can be accessed by a FLAT instruction.
76 FLAT
= GLOBAL
| LDS
| SCRATCH
,
78 /// The address spaces that support atomic instructions.
79 ATOMIC
= GLOBAL
| LDS
| SCRATCH
| GDS
,
81 /// All address spaces.
82 ALL
= GLOBAL
| LDS
| SCRATCH
| GDS
| OTHER
,
84 LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ ALL
)
87 class SIMemOpInfo final
{
90 friend class SIMemOpAccess
;
92 AtomicOrdering Ordering
= AtomicOrdering::NotAtomic
;
93 AtomicOrdering FailureOrdering
= AtomicOrdering::NotAtomic
;
94 SIAtomicScope Scope
= SIAtomicScope::SYSTEM
;
95 SIAtomicAddrSpace OrderingAddrSpace
= SIAtomicAddrSpace::NONE
;
96 SIAtomicAddrSpace InstrAddrSpace
= SIAtomicAddrSpace::NONE
;
97 bool IsCrossAddressSpaceOrdering
= false;
98 bool IsVolatile
= false;
99 bool IsNonTemporal
= false;
101 SIMemOpInfo(AtomicOrdering Ordering
= AtomicOrdering::SequentiallyConsistent
,
102 SIAtomicScope Scope
= SIAtomicScope::SYSTEM
,
103 SIAtomicAddrSpace OrderingAddrSpace
= SIAtomicAddrSpace::ATOMIC
,
104 SIAtomicAddrSpace InstrAddrSpace
= SIAtomicAddrSpace::ALL
,
105 bool IsCrossAddressSpaceOrdering
= true,
106 AtomicOrdering FailureOrdering
=
107 AtomicOrdering::SequentiallyConsistent
,
108 bool IsVolatile
= false,
109 bool IsNonTemporal
= false)
110 : Ordering(Ordering
), FailureOrdering(FailureOrdering
),
111 Scope(Scope
), OrderingAddrSpace(OrderingAddrSpace
),
112 InstrAddrSpace(InstrAddrSpace
),
113 IsCrossAddressSpaceOrdering(IsCrossAddressSpaceOrdering
),
114 IsVolatile(IsVolatile
),
115 IsNonTemporal(IsNonTemporal
) {
117 if (Ordering
== AtomicOrdering::NotAtomic
) {
118 assert(Scope
== SIAtomicScope::NONE
&&
119 OrderingAddrSpace
== SIAtomicAddrSpace::NONE
&&
120 !IsCrossAddressSpaceOrdering
&&
121 FailureOrdering
== AtomicOrdering::NotAtomic
);
125 assert(Scope
!= SIAtomicScope::NONE
&&
126 (OrderingAddrSpace
& SIAtomicAddrSpace::ATOMIC
) !=
127 SIAtomicAddrSpace::NONE
&&
128 (InstrAddrSpace
& SIAtomicAddrSpace::ATOMIC
) !=
129 SIAtomicAddrSpace::NONE
);
131 // There is also no cross address space ordering if the ordering
132 // address space is the same as the instruction address space and
133 // only contains a single address space.
134 if ((OrderingAddrSpace
== InstrAddrSpace
) &&
135 isPowerOf2_32(uint32_t(InstrAddrSpace
)))
136 this->IsCrossAddressSpaceOrdering
= false;
138 // Limit the scope to the maximum supported by the instruction's address
140 if ((InstrAddrSpace
& ~SIAtomicAddrSpace::SCRATCH
) ==
141 SIAtomicAddrSpace::NONE
) {
142 this->Scope
= std::min(Scope
, SIAtomicScope::SINGLETHREAD
);
143 } else if ((InstrAddrSpace
&
144 ~(SIAtomicAddrSpace::SCRATCH
| SIAtomicAddrSpace::LDS
)) ==
145 SIAtomicAddrSpace::NONE
) {
146 this->Scope
= std::min(Scope
, SIAtomicScope::WORKGROUP
);
147 } else if ((InstrAddrSpace
&
148 ~(SIAtomicAddrSpace::SCRATCH
| SIAtomicAddrSpace::LDS
|
149 SIAtomicAddrSpace::GDS
)) == SIAtomicAddrSpace::NONE
) {
150 this->Scope
= std::min(Scope
, SIAtomicScope::AGENT
);
155 /// \returns Atomic synchronization scope of the machine instruction used to
156 /// create this SIMemOpInfo.
157 SIAtomicScope
getScope() const {
161 /// \returns Ordering constraint of the machine instruction used to
162 /// create this SIMemOpInfo.
163 AtomicOrdering
getOrdering() const {
167 /// \returns Failure ordering constraint of the machine instruction used to
168 /// create this SIMemOpInfo.
169 AtomicOrdering
getFailureOrdering() const {
170 return FailureOrdering
;
173 /// \returns The address spaces be accessed by the machine
174 /// instruction used to create this SiMemOpInfo.
175 SIAtomicAddrSpace
getInstrAddrSpace() const {
176 return InstrAddrSpace
;
179 /// \returns The address spaces that must be ordered by the machine
180 /// instruction used to create this SiMemOpInfo.
181 SIAtomicAddrSpace
getOrderingAddrSpace() const {
182 return OrderingAddrSpace
;
185 /// \returns Return true iff memory ordering of operations on
186 /// different address spaces is required.
187 bool getIsCrossAddressSpaceOrdering() const {
188 return IsCrossAddressSpaceOrdering
;
191 /// \returns True if memory access of the machine instruction used to
192 /// create this SIMemOpInfo is volatile, false otherwise.
193 bool isVolatile() const {
197 /// \returns True if memory access of the machine instruction used to
198 /// create this SIMemOpInfo is nontemporal, false otherwise.
199 bool isNonTemporal() const {
200 return IsNonTemporal
;
203 /// \returns True if ordering constraint of the machine instruction used to
204 /// create this SIMemOpInfo is unordered or higher, false otherwise.
205 bool isAtomic() const {
206 return Ordering
!= AtomicOrdering::NotAtomic
;
211 class SIMemOpAccess final
{
213 AMDGPUMachineModuleInfo
*MMI
= nullptr;
215 /// Reports unsupported message \p Msg for \p MI to LLVM context.
216 void reportUnsupported(const MachineBasicBlock::iterator
&MI
,
217 const char *Msg
) const;
219 /// Inspects the target synchronization scope \p SSID and determines
220 /// the SI atomic scope it corresponds to, the address spaces it
221 /// covers, and whether the memory ordering applies between address
223 Optional
<std::tuple
<SIAtomicScope
, SIAtomicAddrSpace
, bool>>
224 toSIAtomicScope(SyncScope::ID SSID
, SIAtomicAddrSpace InstrAddrSpace
) const;
226 /// \return Return a bit set of the address spaces accessed by \p AS.
227 SIAtomicAddrSpace
toSIAtomicAddrSpace(unsigned AS
) const;
229 /// \returns Info constructed from \p MI, which has at least machine memory
231 Optional
<SIMemOpInfo
> constructFromMIWithMMO(
232 const MachineBasicBlock::iterator
&MI
) const;
235 /// Construct class to support accessing the machine memory operands
236 /// of instructions in the machine function \p MF.
237 SIMemOpAccess(MachineFunction
&MF
);
239 /// \returns Load info if \p MI is a load operation, "None" otherwise.
240 Optional
<SIMemOpInfo
> getLoadInfo(
241 const MachineBasicBlock::iterator
&MI
) const;
243 /// \returns Store info if \p MI is a store operation, "None" otherwise.
244 Optional
<SIMemOpInfo
> getStoreInfo(
245 const MachineBasicBlock::iterator
&MI
) const;
247 /// \returns Atomic fence info if \p MI is an atomic fence operation,
248 /// "None" otherwise.
249 Optional
<SIMemOpInfo
> getAtomicFenceInfo(
250 const MachineBasicBlock::iterator
&MI
) const;
252 /// \returns Atomic cmpxchg/rmw info if \p MI is an atomic cmpxchg or
253 /// rmw operation, "None" otherwise.
254 Optional
<SIMemOpInfo
> getAtomicCmpxchgOrRmwInfo(
255 const MachineBasicBlock::iterator
&MI
) const;
258 class SICacheControl
{
261 /// AMDGPU subtarget info.
262 const GCNSubtarget
&ST
;
264 /// Instruction info.
265 const SIInstrInfo
*TII
= nullptr;
269 /// Whether to insert cache invalidating instructions.
272 SICacheControl(const GCNSubtarget
&ST
);
274 /// Sets named bit \p BitName to "true" if present in instruction \p MI.
275 /// \returns Returns true if \p MI is modified, false otherwise.
276 bool enableNamedBit(const MachineBasicBlock::iterator MI
,
277 AMDGPU::CPol::CPol Bit
) const;
281 /// Create a cache control for the subtarget \p ST.
282 static std::unique_ptr
<SICacheControl
> create(const GCNSubtarget
&ST
);
284 /// Update \p MI memory load instruction to bypass any caches up to
285 /// the \p Scope memory scope for address spaces \p
286 /// AddrSpace. Return true iff the instruction was modified.
287 virtual bool enableLoadCacheBypass(const MachineBasicBlock::iterator
&MI
,
289 SIAtomicAddrSpace AddrSpace
) const = 0;
291 /// Update \p MI memory store instruction to bypass any caches up to
292 /// the \p Scope memory scope for address spaces \p
293 /// AddrSpace. Return true iff the instruction was modified.
294 virtual bool enableStoreCacheBypass(const MachineBasicBlock::iterator
&MI
,
296 SIAtomicAddrSpace AddrSpace
) const = 0;
298 /// Update \p MI memory read-modify-write instruction to bypass any caches up
299 /// to the \p Scope memory scope for address spaces \p AddrSpace. Return true
300 /// iff the instruction was modified.
301 virtual bool enableRMWCacheBypass(const MachineBasicBlock::iterator
&MI
,
303 SIAtomicAddrSpace AddrSpace
) const = 0;
305 /// Update \p MI memory instruction of kind \p Op associated with address
306 /// spaces \p AddrSpace to indicate it is volatile and/or nontemporal. Return
307 /// true iff the instruction was modified.
308 virtual bool enableVolatileAndOrNonTemporal(MachineBasicBlock::iterator
&MI
,
309 SIAtomicAddrSpace AddrSpace
,
310 SIMemOp Op
, bool IsVolatile
,
311 bool IsNonTemporal
) const = 0;
313 /// Inserts any necessary instructions at position \p Pos relative
314 /// to instruction \p MI to ensure memory instructions before \p Pos of kind
315 /// \p Op associated with address spaces \p AddrSpace have completed. Used
316 /// between memory instructions to enforce the order they become visible as
317 /// observed by other memory instructions executing in memory scope \p Scope.
318 /// \p IsCrossAddrSpaceOrdering indicates if the memory ordering is between
319 /// address spaces. Returns true iff any instructions inserted.
320 virtual bool insertWait(MachineBasicBlock::iterator
&MI
,
322 SIAtomicAddrSpace AddrSpace
,
324 bool IsCrossAddrSpaceOrdering
,
325 Position Pos
) const = 0;
327 /// Inserts any necessary instructions at position \p Pos relative to
328 /// instruction \p MI to ensure any subsequent memory instructions of this
329 /// thread with address spaces \p AddrSpace will observe the previous memory
330 /// operations by any thread for memory scopes up to memory scope \p Scope .
331 /// Returns true iff any instructions inserted.
332 virtual bool insertAcquire(MachineBasicBlock::iterator
&MI
,
334 SIAtomicAddrSpace AddrSpace
,
335 Position Pos
) const = 0;
337 /// Inserts any necessary instructions at position \p Pos relative to
338 /// instruction \p MI to ensure previous memory instructions by this thread
339 /// with address spaces \p AddrSpace have completed and can be observed by
340 /// subsequent memory instructions by any thread executing in memory scope \p
341 /// Scope. \p IsCrossAddrSpaceOrdering indicates if the memory ordering is
342 /// between address spaces. Returns true iff any instructions inserted.
343 virtual bool insertRelease(MachineBasicBlock::iterator
&MI
,
345 SIAtomicAddrSpace AddrSpace
,
346 bool IsCrossAddrSpaceOrdering
,
347 Position Pos
) const = 0;
349 /// Virtual destructor to allow derivations to be deleted.
350 virtual ~SICacheControl() = default;
354 class SIGfx6CacheControl
: public SICacheControl
{
357 /// Sets GLC bit to "true" if present in \p MI. Returns true if \p MI
358 /// is modified, false otherwise.
359 bool enableGLCBit(const MachineBasicBlock::iterator
&MI
) const {
360 return enableNamedBit(MI
, AMDGPU::CPol::GLC
);
363 /// Sets SLC bit to "true" if present in \p MI. Returns true if \p MI
364 /// is modified, false otherwise.
365 bool enableSLCBit(const MachineBasicBlock::iterator
&MI
) const {
366 return enableNamedBit(MI
, AMDGPU::CPol::SLC
);
371 SIGfx6CacheControl(const GCNSubtarget
&ST
) : SICacheControl(ST
) {};
373 bool enableLoadCacheBypass(const MachineBasicBlock::iterator
&MI
,
375 SIAtomicAddrSpace AddrSpace
) const override
;
377 bool enableStoreCacheBypass(const MachineBasicBlock::iterator
&MI
,
379 SIAtomicAddrSpace AddrSpace
) const override
;
381 bool enableRMWCacheBypass(const MachineBasicBlock::iterator
&MI
,
383 SIAtomicAddrSpace AddrSpace
) const override
;
385 bool enableVolatileAndOrNonTemporal(MachineBasicBlock::iterator
&MI
,
386 SIAtomicAddrSpace AddrSpace
, SIMemOp Op
,
388 bool IsNonTemporal
) const override
;
390 bool insertWait(MachineBasicBlock::iterator
&MI
,
392 SIAtomicAddrSpace AddrSpace
,
394 bool IsCrossAddrSpaceOrdering
,
395 Position Pos
) const override
;
397 bool insertAcquire(MachineBasicBlock::iterator
&MI
,
399 SIAtomicAddrSpace AddrSpace
,
400 Position Pos
) const override
;
402 bool insertRelease(MachineBasicBlock::iterator
&MI
,
404 SIAtomicAddrSpace AddrSpace
,
405 bool IsCrossAddrSpaceOrdering
,
406 Position Pos
) const override
;
409 class SIGfx7CacheControl
: public SIGfx6CacheControl
{
412 SIGfx7CacheControl(const GCNSubtarget
&ST
) : SIGfx6CacheControl(ST
) {};
414 bool insertAcquire(MachineBasicBlock::iterator
&MI
,
416 SIAtomicAddrSpace AddrSpace
,
417 Position Pos
) const override
;
421 class SIGfx90ACacheControl
: public SIGfx7CacheControl
{
424 SIGfx90ACacheControl(const GCNSubtarget
&ST
) : SIGfx7CacheControl(ST
) {};
426 bool enableLoadCacheBypass(const MachineBasicBlock::iterator
&MI
,
428 SIAtomicAddrSpace AddrSpace
) const override
;
430 bool enableStoreCacheBypass(const MachineBasicBlock::iterator
&MI
,
432 SIAtomicAddrSpace AddrSpace
) const override
;
434 bool enableRMWCacheBypass(const MachineBasicBlock::iterator
&MI
,
436 SIAtomicAddrSpace AddrSpace
) const override
;
438 bool enableVolatileAndOrNonTemporal(MachineBasicBlock::iterator
&MI
,
439 SIAtomicAddrSpace AddrSpace
, SIMemOp Op
,
441 bool IsNonTemporal
) const override
;
443 bool insertWait(MachineBasicBlock::iterator
&MI
,
445 SIAtomicAddrSpace AddrSpace
,
447 bool IsCrossAddrSpaceOrdering
,
448 Position Pos
) const override
;
450 bool insertAcquire(MachineBasicBlock::iterator
&MI
,
452 SIAtomicAddrSpace AddrSpace
,
453 Position Pos
) const override
;
455 bool insertRelease(MachineBasicBlock::iterator
&MI
,
457 SIAtomicAddrSpace AddrSpace
,
458 bool IsCrossAddrSpaceOrdering
,
459 Position Pos
) const override
;
462 class SIGfx10CacheControl
: public SIGfx7CacheControl
{
465 /// Sets DLC bit to "true" if present in \p MI. Returns true if \p MI
466 /// is modified, false otherwise.
467 bool enableDLCBit(const MachineBasicBlock::iterator
&MI
) const {
468 return enableNamedBit(MI
, AMDGPU::CPol::DLC
);
473 SIGfx10CacheControl(const GCNSubtarget
&ST
) : SIGfx7CacheControl(ST
) {};
475 bool enableLoadCacheBypass(const MachineBasicBlock::iterator
&MI
,
477 SIAtomicAddrSpace AddrSpace
) const override
;
479 bool enableVolatileAndOrNonTemporal(MachineBasicBlock::iterator
&MI
,
480 SIAtomicAddrSpace AddrSpace
, SIMemOp Op
,
482 bool IsNonTemporal
) const override
;
484 bool insertWait(MachineBasicBlock::iterator
&MI
,
486 SIAtomicAddrSpace AddrSpace
,
488 bool IsCrossAddrSpaceOrdering
,
489 Position Pos
) const override
;
491 bool insertAcquire(MachineBasicBlock::iterator
&MI
,
493 SIAtomicAddrSpace AddrSpace
,
494 Position Pos
) const override
;
497 class SIMemoryLegalizer final
: public MachineFunctionPass
{
501 std::unique_ptr
<SICacheControl
> CC
= nullptr;
503 /// List of atomic pseudo instructions.
504 std::list
<MachineBasicBlock::iterator
> AtomicPseudoMIs
;
506 /// Return true iff instruction \p MI is a atomic instruction that
507 /// returns a result.
508 bool isAtomicRet(const MachineInstr
&MI
) const {
509 return SIInstrInfo::isAtomicRet(MI
);
512 /// Removes all processed atomic pseudo instructions from the current
513 /// function. Returns true if current function is modified, false otherwise.
514 bool removeAtomicPseudoMIs();
516 /// Expands load operation \p MI. Returns true if instructions are
517 /// added/deleted or \p MI is modified, false otherwise.
518 bool expandLoad(const SIMemOpInfo
&MOI
,
519 MachineBasicBlock::iterator
&MI
);
520 /// Expands store operation \p MI. Returns true if instructions are
521 /// added/deleted or \p MI is modified, false otherwise.
522 bool expandStore(const SIMemOpInfo
&MOI
,
523 MachineBasicBlock::iterator
&MI
);
524 /// Expands atomic fence operation \p MI. Returns true if
525 /// instructions are added/deleted or \p MI is modified, false otherwise.
526 bool expandAtomicFence(const SIMemOpInfo
&MOI
,
527 MachineBasicBlock::iterator
&MI
);
528 /// Expands atomic cmpxchg or rmw operation \p MI. Returns true if
529 /// instructions are added/deleted or \p MI is modified, false otherwise.
530 bool expandAtomicCmpxchgOrRmw(const SIMemOpInfo
&MOI
,
531 MachineBasicBlock::iterator
&MI
);
536 SIMemoryLegalizer() : MachineFunctionPass(ID
) {}
538 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
539 AU
.setPreservesCFG();
540 MachineFunctionPass::getAnalysisUsage(AU
);
543 StringRef
getPassName() const override
{
547 bool runOnMachineFunction(MachineFunction
&MF
) override
;
550 } // end namespace anonymous
552 void SIMemOpAccess::reportUnsupported(const MachineBasicBlock::iterator
&MI
,
553 const char *Msg
) const {
554 const Function
&Func
= MI
->getParent()->getParent()->getFunction();
555 DiagnosticInfoUnsupported
Diag(Func
, Msg
, MI
->getDebugLoc());
556 Func
.getContext().diagnose(Diag
);
559 Optional
<std::tuple
<SIAtomicScope
, SIAtomicAddrSpace
, bool>>
560 SIMemOpAccess::toSIAtomicScope(SyncScope::ID SSID
,
561 SIAtomicAddrSpace InstrAddrSpace
) const {
562 if (SSID
== SyncScope::System
)
563 return std::make_tuple(SIAtomicScope::SYSTEM
,
564 SIAtomicAddrSpace::ATOMIC
,
566 if (SSID
== MMI
->getAgentSSID())
567 return std::make_tuple(SIAtomicScope::AGENT
,
568 SIAtomicAddrSpace::ATOMIC
,
570 if (SSID
== MMI
->getWorkgroupSSID())
571 return std::make_tuple(SIAtomicScope::WORKGROUP
,
572 SIAtomicAddrSpace::ATOMIC
,
574 if (SSID
== MMI
->getWavefrontSSID())
575 return std::make_tuple(SIAtomicScope::WAVEFRONT
,
576 SIAtomicAddrSpace::ATOMIC
,
578 if (SSID
== SyncScope::SingleThread
)
579 return std::make_tuple(SIAtomicScope::SINGLETHREAD
,
580 SIAtomicAddrSpace::ATOMIC
,
582 if (SSID
== MMI
->getSystemOneAddressSpaceSSID())
583 return std::make_tuple(SIAtomicScope::SYSTEM
,
584 SIAtomicAddrSpace::ATOMIC
& InstrAddrSpace
,
586 if (SSID
== MMI
->getAgentOneAddressSpaceSSID())
587 return std::make_tuple(SIAtomicScope::AGENT
,
588 SIAtomicAddrSpace::ATOMIC
& InstrAddrSpace
,
590 if (SSID
== MMI
->getWorkgroupOneAddressSpaceSSID())
591 return std::make_tuple(SIAtomicScope::WORKGROUP
,
592 SIAtomicAddrSpace::ATOMIC
& InstrAddrSpace
,
594 if (SSID
== MMI
->getWavefrontOneAddressSpaceSSID())
595 return std::make_tuple(SIAtomicScope::WAVEFRONT
,
596 SIAtomicAddrSpace::ATOMIC
& InstrAddrSpace
,
598 if (SSID
== MMI
->getSingleThreadOneAddressSpaceSSID())
599 return std::make_tuple(SIAtomicScope::SINGLETHREAD
,
600 SIAtomicAddrSpace::ATOMIC
& InstrAddrSpace
,
605 SIAtomicAddrSpace
SIMemOpAccess::toSIAtomicAddrSpace(unsigned AS
) const {
606 if (AS
== AMDGPUAS::FLAT_ADDRESS
)
607 return SIAtomicAddrSpace::FLAT
;
608 if (AS
== AMDGPUAS::GLOBAL_ADDRESS
)
609 return SIAtomicAddrSpace::GLOBAL
;
610 if (AS
== AMDGPUAS::LOCAL_ADDRESS
)
611 return SIAtomicAddrSpace::LDS
;
612 if (AS
== AMDGPUAS::PRIVATE_ADDRESS
)
613 return SIAtomicAddrSpace::SCRATCH
;
614 if (AS
== AMDGPUAS::REGION_ADDRESS
)
615 return SIAtomicAddrSpace::GDS
;
617 return SIAtomicAddrSpace::OTHER
;
620 SIMemOpAccess::SIMemOpAccess(MachineFunction
&MF
) {
621 MMI
= &MF
.getMMI().getObjFileInfo
<AMDGPUMachineModuleInfo
>();
624 Optional
<SIMemOpInfo
> SIMemOpAccess::constructFromMIWithMMO(
625 const MachineBasicBlock::iterator
&MI
) const {
626 assert(MI
->getNumMemOperands() > 0);
628 SyncScope::ID SSID
= SyncScope::SingleThread
;
629 AtomicOrdering Ordering
= AtomicOrdering::NotAtomic
;
630 AtomicOrdering FailureOrdering
= AtomicOrdering::NotAtomic
;
631 SIAtomicAddrSpace InstrAddrSpace
= SIAtomicAddrSpace::NONE
;
632 bool IsNonTemporal
= true;
633 bool IsVolatile
= false;
635 // Validator should check whether or not MMOs cover the entire set of
636 // locations accessed by the memory instruction.
637 for (const auto &MMO
: MI
->memoperands()) {
638 IsNonTemporal
&= MMO
->isNonTemporal();
639 IsVolatile
|= MMO
->isVolatile();
641 toSIAtomicAddrSpace(MMO
->getPointerInfo().getAddrSpace());
642 AtomicOrdering OpOrdering
= MMO
->getSuccessOrdering();
643 if (OpOrdering
!= AtomicOrdering::NotAtomic
) {
644 const auto &IsSyncScopeInclusion
=
645 MMI
->isSyncScopeInclusion(SSID
, MMO
->getSyncScopeID());
646 if (!IsSyncScopeInclusion
) {
647 reportUnsupported(MI
,
648 "Unsupported non-inclusive atomic synchronization scope");
652 SSID
= IsSyncScopeInclusion
.getValue() ? SSID
: MMO
->getSyncScopeID();
653 Ordering
= getMergedAtomicOrdering(Ordering
, OpOrdering
);
654 assert(MMO
->getFailureOrdering() != AtomicOrdering::Release
&&
655 MMO
->getFailureOrdering() != AtomicOrdering::AcquireRelease
);
657 getMergedAtomicOrdering(FailureOrdering
, MMO
->getFailureOrdering());
661 SIAtomicScope Scope
= SIAtomicScope::NONE
;
662 SIAtomicAddrSpace OrderingAddrSpace
= SIAtomicAddrSpace::NONE
;
663 bool IsCrossAddressSpaceOrdering
= false;
664 if (Ordering
!= AtomicOrdering::NotAtomic
) {
665 auto ScopeOrNone
= toSIAtomicScope(SSID
, InstrAddrSpace
);
667 reportUnsupported(MI
, "Unsupported atomic synchronization scope");
670 std::tie(Scope
, OrderingAddrSpace
, IsCrossAddressSpaceOrdering
) =
671 ScopeOrNone
.getValue();
672 if ((OrderingAddrSpace
== SIAtomicAddrSpace::NONE
) ||
673 ((OrderingAddrSpace
& SIAtomicAddrSpace::ATOMIC
) != OrderingAddrSpace
) ||
674 ((InstrAddrSpace
& SIAtomicAddrSpace::ATOMIC
) == SIAtomicAddrSpace::NONE
)) {
675 reportUnsupported(MI
, "Unsupported atomic address space");
679 return SIMemOpInfo(Ordering
, Scope
, OrderingAddrSpace
, InstrAddrSpace
,
680 IsCrossAddressSpaceOrdering
, FailureOrdering
, IsVolatile
,
684 Optional
<SIMemOpInfo
> SIMemOpAccess::getLoadInfo(
685 const MachineBasicBlock::iterator
&MI
) const {
686 assert(MI
->getDesc().TSFlags
& SIInstrFlags::maybeAtomic
);
688 if (!(MI
->mayLoad() && !MI
->mayStore()))
691 // Be conservative if there are no memory operands.
692 if (MI
->getNumMemOperands() == 0)
693 return SIMemOpInfo();
695 return constructFromMIWithMMO(MI
);
698 Optional
<SIMemOpInfo
> SIMemOpAccess::getStoreInfo(
699 const MachineBasicBlock::iterator
&MI
) const {
700 assert(MI
->getDesc().TSFlags
& SIInstrFlags::maybeAtomic
);
702 if (!(!MI
->mayLoad() && MI
->mayStore()))
705 // Be conservative if there are no memory operands.
706 if (MI
->getNumMemOperands() == 0)
707 return SIMemOpInfo();
709 return constructFromMIWithMMO(MI
);
712 Optional
<SIMemOpInfo
> SIMemOpAccess::getAtomicFenceInfo(
713 const MachineBasicBlock::iterator
&MI
) const {
714 assert(MI
->getDesc().TSFlags
& SIInstrFlags::maybeAtomic
);
716 if (MI
->getOpcode() != AMDGPU::ATOMIC_FENCE
)
719 AtomicOrdering Ordering
=
720 static_cast<AtomicOrdering
>(MI
->getOperand(0).getImm());
722 SyncScope::ID SSID
= static_cast<SyncScope::ID
>(MI
->getOperand(1).getImm());
723 auto ScopeOrNone
= toSIAtomicScope(SSID
, SIAtomicAddrSpace::ATOMIC
);
725 reportUnsupported(MI
, "Unsupported atomic synchronization scope");
729 SIAtomicScope Scope
= SIAtomicScope::NONE
;
730 SIAtomicAddrSpace OrderingAddrSpace
= SIAtomicAddrSpace::NONE
;
731 bool IsCrossAddressSpaceOrdering
= false;
732 std::tie(Scope
, OrderingAddrSpace
, IsCrossAddressSpaceOrdering
) =
733 ScopeOrNone
.getValue();
735 if ((OrderingAddrSpace
== SIAtomicAddrSpace::NONE
) ||
736 ((OrderingAddrSpace
& SIAtomicAddrSpace::ATOMIC
) != OrderingAddrSpace
)) {
737 reportUnsupported(MI
, "Unsupported atomic address space");
741 return SIMemOpInfo(Ordering
, Scope
, OrderingAddrSpace
, SIAtomicAddrSpace::ATOMIC
,
742 IsCrossAddressSpaceOrdering
, AtomicOrdering::NotAtomic
);
745 Optional
<SIMemOpInfo
> SIMemOpAccess::getAtomicCmpxchgOrRmwInfo(
746 const MachineBasicBlock::iterator
&MI
) const {
747 assert(MI
->getDesc().TSFlags
& SIInstrFlags::maybeAtomic
);
749 if (!(MI
->mayLoad() && MI
->mayStore()))
752 // Be conservative if there are no memory operands.
753 if (MI
->getNumMemOperands() == 0)
754 return SIMemOpInfo();
756 return constructFromMIWithMMO(MI
);
759 SICacheControl::SICacheControl(const GCNSubtarget
&ST
) : ST(ST
) {
760 TII
= ST
.getInstrInfo();
761 IV
= getIsaVersion(ST
.getCPU());
762 InsertCacheInv
= !AmdgcnSkipCacheInvalidations
;
765 bool SICacheControl::enableNamedBit(const MachineBasicBlock::iterator MI
,
766 AMDGPU::CPol::CPol Bit
) const {
767 MachineOperand
*CPol
= TII
->getNamedOperand(*MI
, AMDGPU::OpName::cpol
);
771 CPol
->setImm(CPol
->getImm() | Bit
);
776 std::unique_ptr
<SICacheControl
> SICacheControl::create(const GCNSubtarget
&ST
) {
777 GCNSubtarget::Generation Generation
= ST
.getGeneration();
778 if (ST
.hasGFX90AInsts())
779 return std::make_unique
<SIGfx90ACacheControl
>(ST
);
780 if (Generation
<= AMDGPUSubtarget::SOUTHERN_ISLANDS
)
781 return std::make_unique
<SIGfx6CacheControl
>(ST
);
782 if (Generation
< AMDGPUSubtarget::GFX10
)
783 return std::make_unique
<SIGfx7CacheControl
>(ST
);
784 return std::make_unique
<SIGfx10CacheControl
>(ST
);
787 bool SIGfx6CacheControl::enableLoadCacheBypass(
788 const MachineBasicBlock::iterator
&MI
,
790 SIAtomicAddrSpace AddrSpace
) const {
791 assert(MI
->mayLoad() && !MI
->mayStore());
792 bool Changed
= false;
794 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
796 case SIAtomicScope::SYSTEM
:
797 case SIAtomicScope::AGENT
:
798 Changed
|= enableGLCBit(MI
);
800 case SIAtomicScope::WORKGROUP
:
801 case SIAtomicScope::WAVEFRONT
:
802 case SIAtomicScope::SINGLETHREAD
:
803 // No cache to bypass.
806 llvm_unreachable("Unsupported synchronization scope");
810 /// The scratch address space does not need the global memory caches
811 /// to be bypassed as all memory operations by the same thread are
812 /// sequentially consistent, and no other thread can access scratch
815 /// Other address spaces do not have a cache.
820 bool SIGfx6CacheControl::enableStoreCacheBypass(
821 const MachineBasicBlock::iterator
&MI
,
823 SIAtomicAddrSpace AddrSpace
) const {
824 assert(!MI
->mayLoad() && MI
->mayStore());
825 bool Changed
= false;
827 /// The L1 cache is write through so does not need to be bypassed. There is no
828 /// bypass control for the L2 cache at the isa level.
833 bool SIGfx6CacheControl::enableRMWCacheBypass(
834 const MachineBasicBlock::iterator
&MI
,
836 SIAtomicAddrSpace AddrSpace
) const {
837 assert(MI
->mayLoad() && MI
->mayStore());
838 bool Changed
= false;
840 /// The L1 cache is write through so does not need to be bypassed. There is no
841 /// bypass control for the L2 cache at the isa level.
846 bool SIGfx6CacheControl::enableVolatileAndOrNonTemporal(
847 MachineBasicBlock::iterator
&MI
, SIAtomicAddrSpace AddrSpace
, SIMemOp Op
,
848 bool IsVolatile
, bool IsNonTemporal
) const {
849 // Only handle load and store, not atomic read-modify-write insructions. The
850 // latter use glc to indicate if the atomic returns a result and so must not
851 // be used for cache control.
852 assert(MI
->mayLoad() ^ MI
->mayStore());
854 // Only update load and store, not LLVM IR atomic read-modify-write
855 // instructions. The latter are always marked as volatile so cannot sensibly
856 // handle it as do not want to pessimize all atomics. Also they do not support
857 // the nontemporal attribute.
858 assert( Op
== SIMemOp::LOAD
|| Op
== SIMemOp::STORE
);
860 bool Changed
= false;
863 if (Op
== SIMemOp::LOAD
)
864 Changed
|= enableGLCBit(MI
);
866 // Ensure operation has completed at system scope to cause all volatile
867 // operations to be visible outside the program in a global order. Do not
868 // request cross address space as only the global address space can be
869 // observable outside the program, so no need to cause a waitcnt for LDS
870 // address space operations.
871 Changed
|= insertWait(MI
, SIAtomicScope::SYSTEM
, AddrSpace
, Op
, false,
878 // Request L1 MISS_EVICT and L2 STREAM for load and store instructions.
879 Changed
|= enableGLCBit(MI
);
880 Changed
|= enableSLCBit(MI
);
887 bool SIGfx6CacheControl::insertWait(MachineBasicBlock::iterator
&MI
,
889 SIAtomicAddrSpace AddrSpace
,
891 bool IsCrossAddrSpaceOrdering
,
892 Position Pos
) const {
893 bool Changed
= false;
895 MachineBasicBlock
&MBB
= *MI
->getParent();
896 DebugLoc DL
= MI
->getDebugLoc();
898 if (Pos
== Position::AFTER
)
902 bool LGKMCnt
= false;
904 if ((AddrSpace
& (SIAtomicAddrSpace::GLOBAL
| SIAtomicAddrSpace::SCRATCH
)) !=
905 SIAtomicAddrSpace::NONE
) {
907 case SIAtomicScope::SYSTEM
:
908 case SIAtomicScope::AGENT
:
911 case SIAtomicScope::WORKGROUP
:
912 case SIAtomicScope::WAVEFRONT
:
913 case SIAtomicScope::SINGLETHREAD
:
914 // The L1 cache keeps all memory operations in order for
915 // wavefronts in the same work-group.
918 llvm_unreachable("Unsupported synchronization scope");
922 if ((AddrSpace
& SIAtomicAddrSpace::LDS
) != SIAtomicAddrSpace::NONE
) {
924 case SIAtomicScope::SYSTEM
:
925 case SIAtomicScope::AGENT
:
926 case SIAtomicScope::WORKGROUP
:
927 // If no cross address space ordering then an "S_WAITCNT lgkmcnt(0)" is
928 // not needed as LDS operations for all waves are executed in a total
929 // global ordering as observed by all waves. Required if also
930 // synchronizing with global/GDS memory as LDS operations could be
931 // reordered with respect to later global/GDS memory operations of the
933 LGKMCnt
|= IsCrossAddrSpaceOrdering
;
935 case SIAtomicScope::WAVEFRONT
:
936 case SIAtomicScope::SINGLETHREAD
:
937 // The LDS keeps all memory operations in order for
938 // the same wavesfront.
941 llvm_unreachable("Unsupported synchronization scope");
945 if ((AddrSpace
& SIAtomicAddrSpace::GDS
) != SIAtomicAddrSpace::NONE
) {
947 case SIAtomicScope::SYSTEM
:
948 case SIAtomicScope::AGENT
:
949 // If no cross address space ordering then an GDS "S_WAITCNT lgkmcnt(0)"
950 // is not needed as GDS operations for all waves are executed in a total
951 // global ordering as observed by all waves. Required if also
952 // synchronizing with global/LDS memory as GDS operations could be
953 // reordered with respect to later global/LDS memory operations of the
955 LGKMCnt
|= IsCrossAddrSpaceOrdering
;
957 case SIAtomicScope::WORKGROUP
:
958 case SIAtomicScope::WAVEFRONT
:
959 case SIAtomicScope::SINGLETHREAD
:
960 // The GDS keeps all memory operations in order for
961 // the same work-group.
964 llvm_unreachable("Unsupported synchronization scope");
968 if (VMCnt
|| LGKMCnt
) {
969 unsigned WaitCntImmediate
=
970 AMDGPU::encodeWaitcnt(IV
,
971 VMCnt
? 0 : getVmcntBitMask(IV
),
972 getExpcntBitMask(IV
),
973 LGKMCnt
? 0 : getLgkmcntBitMask(IV
));
974 BuildMI(MBB
, MI
, DL
, TII
->get(AMDGPU::S_WAITCNT
)).addImm(WaitCntImmediate
);
978 if (Pos
== Position::AFTER
)
984 bool SIGfx6CacheControl::insertAcquire(MachineBasicBlock::iterator
&MI
,
986 SIAtomicAddrSpace AddrSpace
,
987 Position Pos
) const {
991 bool Changed
= false;
993 MachineBasicBlock
&MBB
= *MI
->getParent();
994 DebugLoc DL
= MI
->getDebugLoc();
996 if (Pos
== Position::AFTER
)
999 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
1001 case SIAtomicScope::SYSTEM
:
1002 case SIAtomicScope::AGENT
:
1003 BuildMI(MBB
, MI
, DL
, TII
->get(AMDGPU::BUFFER_WBINVL1
));
1006 case SIAtomicScope::WORKGROUP
:
1007 case SIAtomicScope::WAVEFRONT
:
1008 case SIAtomicScope::SINGLETHREAD
:
1009 // No cache to invalidate.
1012 llvm_unreachable("Unsupported synchronization scope");
1016 /// The scratch address space does not need the global memory cache
1017 /// to be flushed as all memory operations by the same thread are
1018 /// sequentially consistent, and no other thread can access scratch
1021 /// Other address spaces do not have a cache.
1023 if (Pos
== Position::AFTER
)
1029 bool SIGfx6CacheControl::insertRelease(MachineBasicBlock::iterator
&MI
,
1030 SIAtomicScope Scope
,
1031 SIAtomicAddrSpace AddrSpace
,
1032 bool IsCrossAddrSpaceOrdering
,
1033 Position Pos
) const {
1034 return insertWait(MI
, Scope
, AddrSpace
, SIMemOp::LOAD
| SIMemOp::STORE
,
1035 IsCrossAddrSpaceOrdering
, Pos
);
1038 bool SIGfx7CacheControl::insertAcquire(MachineBasicBlock::iterator
&MI
,
1039 SIAtomicScope Scope
,
1040 SIAtomicAddrSpace AddrSpace
,
1041 Position Pos
) const {
1042 if (!InsertCacheInv
)
1045 bool Changed
= false;
1047 MachineBasicBlock
&MBB
= *MI
->getParent();
1048 DebugLoc DL
= MI
->getDebugLoc();
1050 const GCNSubtarget
&STM
= MBB
.getParent()->getSubtarget
<GCNSubtarget
>();
1052 const unsigned InvalidateL1
= STM
.isAmdPalOS() || STM
.isMesa3DOS()
1053 ? AMDGPU::BUFFER_WBINVL1
1054 : AMDGPU::BUFFER_WBINVL1_VOL
;
1056 if (Pos
== Position::AFTER
)
1059 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
1061 case SIAtomicScope::SYSTEM
:
1062 case SIAtomicScope::AGENT
:
1063 BuildMI(MBB
, MI
, DL
, TII
->get(InvalidateL1
));
1066 case SIAtomicScope::WORKGROUP
:
1067 case SIAtomicScope::WAVEFRONT
:
1068 case SIAtomicScope::SINGLETHREAD
:
1069 // No cache to invalidate.
1072 llvm_unreachable("Unsupported synchronization scope");
1076 /// The scratch address space does not need the global memory cache
1077 /// to be flushed as all memory operations by the same thread are
1078 /// sequentially consistent, and no other thread can access scratch
1081 /// Other address spaces do not have a cache.
1083 if (Pos
== Position::AFTER
)
1089 bool SIGfx90ACacheControl::enableLoadCacheBypass(
1090 const MachineBasicBlock::iterator
&MI
,
1091 SIAtomicScope Scope
,
1092 SIAtomicAddrSpace AddrSpace
) const {
1093 assert(MI
->mayLoad() && !MI
->mayStore());
1094 bool Changed
= false;
1096 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
1098 case SIAtomicScope::SYSTEM
:
1099 case SIAtomicScope::AGENT
:
1100 Changed
|= enableGLCBit(MI
);
1102 case SIAtomicScope::WORKGROUP
:
1103 // In threadgroup split mode the waves of a work-group can be executing on
1104 // different CUs. Therefore need to bypass the L1 which is per CU.
1105 // Otherwise in non-threadgroup split mode all waves of a work-group are
1106 // on the same CU, and so the L1 does not need to be bypassed.
1107 if (ST
.isTgSplitEnabled()) Changed
|= enableGLCBit(MI
);
1109 case SIAtomicScope::WAVEFRONT
:
1110 case SIAtomicScope::SINGLETHREAD
:
1111 // No cache to bypass.
1114 llvm_unreachable("Unsupported synchronization scope");
1118 /// The scratch address space does not need the global memory caches
1119 /// to be bypassed as all memory operations by the same thread are
1120 /// sequentially consistent, and no other thread can access scratch
1123 /// Other address spaces do not have a cache.
1128 bool SIGfx90ACacheControl::enableStoreCacheBypass(
1129 const MachineBasicBlock::iterator
&MI
,
1130 SIAtomicScope Scope
,
1131 SIAtomicAddrSpace AddrSpace
) const {
1132 assert(!MI
->mayLoad() && MI
->mayStore());
1133 bool Changed
= false;
1135 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
1137 case SIAtomicScope::SYSTEM
:
1138 case SIAtomicScope::AGENT
:
1139 /// Do not set glc for store atomic operations as they implicitly write
1140 /// through the L1 cache.
1142 case SIAtomicScope::WORKGROUP
:
1143 case SIAtomicScope::WAVEFRONT
:
1144 case SIAtomicScope::SINGLETHREAD
:
1145 // No cache to bypass. Store atomics implicitly write through the L1
1149 llvm_unreachable("Unsupported synchronization scope");
1153 /// The scratch address space does not need the global memory caches
1154 /// to be bypassed as all memory operations by the same thread are
1155 /// sequentially consistent, and no other thread can access scratch
1158 /// Other address spaces do not have a cache.
1163 bool SIGfx90ACacheControl::enableRMWCacheBypass(
1164 const MachineBasicBlock::iterator
&MI
,
1165 SIAtomicScope Scope
,
1166 SIAtomicAddrSpace AddrSpace
) const {
1167 assert(MI
->mayLoad() && MI
->mayStore());
1168 bool Changed
= false;
1170 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
1172 case SIAtomicScope::SYSTEM
:
1173 case SIAtomicScope::AGENT
:
1174 /// Do not set glc for RMW atomic operations as they implicitly bypass
1175 /// the L1 cache, and the glc bit is instead used to indicate if they are
1176 /// return or no-return.
1178 case SIAtomicScope::WORKGROUP
:
1179 case SIAtomicScope::WAVEFRONT
:
1180 case SIAtomicScope::SINGLETHREAD
:
1181 // No cache to bypass. RMW atomics implicitly bypass the L1 cache.
1184 llvm_unreachable("Unsupported synchronization scope");
1191 bool SIGfx90ACacheControl::enableVolatileAndOrNonTemporal(
1192 MachineBasicBlock::iterator
&MI
, SIAtomicAddrSpace AddrSpace
, SIMemOp Op
,
1193 bool IsVolatile
, bool IsNonTemporal
) const {
1194 // Only handle load and store, not atomic read-modify-write insructions. The
1195 // latter use glc to indicate if the atomic returns a result and so must not
1196 // be used for cache control.
1197 assert(MI
->mayLoad() ^ MI
->mayStore());
1199 // Only update load and store, not LLVM IR atomic read-modify-write
1200 // instructions. The latter are always marked as volatile so cannot sensibly
1201 // handle it as do not want to pessimize all atomics. Also they do not support
1202 // the nontemporal attribute.
1203 assert( Op
== SIMemOp::LOAD
|| Op
== SIMemOp::STORE
);
1205 bool Changed
= false;
1208 if (Op
== SIMemOp::LOAD
) {
1209 Changed
|= enableGLCBit(MI
);
1212 // Ensure operation has completed at system scope to cause all volatile
1213 // operations to be visible outside the program in a global order. Do not
1214 // request cross address space as only the global address space can be
1215 // observable outside the program, so no need to cause a waitcnt for LDS
1216 // address space operations.
1217 Changed
|= insertWait(MI
, SIAtomicScope::SYSTEM
, AddrSpace
, Op
, false,
1223 if (IsNonTemporal
) {
1224 // Request L1 MISS_EVICT and L2 STREAM for load and store instructions.
1225 Changed
|= enableGLCBit(MI
);
1226 Changed
|= enableSLCBit(MI
);
1233 bool SIGfx90ACacheControl::insertWait(MachineBasicBlock::iterator
&MI
,
1234 SIAtomicScope Scope
,
1235 SIAtomicAddrSpace AddrSpace
,
1237 bool IsCrossAddrSpaceOrdering
,
1238 Position Pos
) const {
1239 if (ST
.isTgSplitEnabled()) {
1240 // In threadgroup split mode the waves of a work-group can be executing on
1241 // different CUs. Therefore need to wait for global or GDS memory operations
1242 // to complete to ensure they are visible to waves in the other CUs.
1243 // Otherwise in non-threadgroup split mode all waves of a work-group are on
1244 // the same CU, so no need to wait for global memory as all waves in the
1245 // work-group access the same the L1, nor wait for GDS as access are ordered
1247 if (((AddrSpace
& (SIAtomicAddrSpace::GLOBAL
| SIAtomicAddrSpace::SCRATCH
|
1248 SIAtomicAddrSpace::GDS
)) != SIAtomicAddrSpace::NONE
) &&
1249 (Scope
== SIAtomicScope::WORKGROUP
)) {
1250 // Same as GFX7 using agent scope.
1251 Scope
= SIAtomicScope::AGENT
;
1253 // In threadgroup split mode LDS cannot be allocated so no need to wait for
1254 // LDS memory operations.
1255 AddrSpace
&= ~SIAtomicAddrSpace::LDS
;
1257 return SIGfx7CacheControl::insertWait(MI
, Scope
, AddrSpace
, Op
,
1258 IsCrossAddrSpaceOrdering
, Pos
);
1261 bool SIGfx90ACacheControl::insertAcquire(MachineBasicBlock::iterator
&MI
,
1262 SIAtomicScope Scope
,
1263 SIAtomicAddrSpace AddrSpace
,
1264 Position Pos
) const {
1265 if (!InsertCacheInv
)
1268 bool Changed
= false;
1270 MachineBasicBlock
&MBB
= *MI
->getParent();
1271 DebugLoc DL
= MI
->getDebugLoc();
1273 if (Pos
== Position::AFTER
)
1276 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
1278 case SIAtomicScope::SYSTEM
:
1279 // Ensures that following loads will not see stale remote VMEM data or
1280 // stale local VMEM data with MTYPE NC. Local VMEM data with MTYPE RW and
1281 // CC will never be stale due to the local memory probes.
1282 BuildMI(MBB
, MI
, DL
, TII
->get(AMDGPU::BUFFER_INVL2
));
1283 // Inserting a "S_WAITCNT vmcnt(0)" after is not required because the
1284 // hardware does not reorder memory operations by the same wave with
1285 // respect to a preceding "BUFFER_INVL2". The invalidate is guaranteed to
1286 // remove any cache lines of earlier writes by the same wave and ensures
1287 // later reads by the same wave will refetch the cache lines.
1290 case SIAtomicScope::AGENT
:
1293 case SIAtomicScope::WORKGROUP
:
1294 // In threadgroup split mode the waves of a work-group can be executing on
1295 // different CUs. Therefore need to invalidate the L1 which is per CU.
1296 // Otherwise in non-threadgroup split mode all waves of a work-group are
1297 // on the same CU, and so the L1 does not need to be invalidated.
1298 if (ST
.isTgSplitEnabled()) {
1299 // Same as GFX7 using agent scope.
1300 Scope
= SIAtomicScope::AGENT
;
1303 case SIAtomicScope::WAVEFRONT
:
1304 case SIAtomicScope::SINGLETHREAD
:
1308 llvm_unreachable("Unsupported synchronization scope");
1312 /// The scratch address space does not need the global memory cache
1313 /// to be flushed as all memory operations by the same thread are
1314 /// sequentially consistent, and no other thread can access scratch
1317 /// Other address spaces do not have a cache.
1319 if (Pos
== Position::AFTER
)
1322 Changed
|= SIGfx7CacheControl::insertAcquire(MI
, Scope
, AddrSpace
, Pos
);
1327 bool SIGfx90ACacheControl::insertRelease(MachineBasicBlock::iterator
&MI
,
1328 SIAtomicScope Scope
,
1329 SIAtomicAddrSpace AddrSpace
,
1330 bool IsCrossAddrSpaceOrdering
,
1331 Position Pos
) const {
1332 bool Changed
= false;
1334 MachineBasicBlock
&MBB
= *MI
->getParent();
1335 DebugLoc DL
= MI
->getDebugLoc();
1337 if (Pos
== Position::AFTER
)
1340 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
1342 case SIAtomicScope::SYSTEM
:
1343 // Inserting a "S_WAITCNT vmcnt(0)" before is not required because the
1344 // hardware does not reorder memory operations by the same wave with
1345 // respect to a following "BUFFER_WBL2". The "BUFFER_WBL2" is guaranteed
1346 // to initiate writeback of any dirty cache lines of earlier writes by the
1347 // same wave. A "S_WAITCNT vmcnt(0)" is needed after to ensure the
1348 // writeback has completed.
1349 BuildMI(MBB
, MI
, DL
, TII
->get(AMDGPU::BUFFER_WBL2
));
1350 // Followed by same as GFX7, which will ensure the necessary "S_WAITCNT
1351 // vmcnt(0)" needed by the "BUFFER_WBL2".
1354 case SIAtomicScope::AGENT
:
1355 case SIAtomicScope::WORKGROUP
:
1356 case SIAtomicScope::WAVEFRONT
:
1357 case SIAtomicScope::SINGLETHREAD
:
1361 llvm_unreachable("Unsupported synchronization scope");
1365 if (Pos
== Position::AFTER
)
1369 SIGfx7CacheControl::insertRelease(MI
, Scope
, AddrSpace
,
1370 IsCrossAddrSpaceOrdering
, Pos
);
1375 bool SIGfx10CacheControl::enableLoadCacheBypass(
1376 const MachineBasicBlock::iterator
&MI
,
1377 SIAtomicScope Scope
,
1378 SIAtomicAddrSpace AddrSpace
) const {
1379 assert(MI
->mayLoad() && !MI
->mayStore());
1380 bool Changed
= false;
1382 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
1383 /// TODO Do not set glc for rmw atomic operations as they
1384 /// implicitly bypass the L0/L1 caches.
1387 case SIAtomicScope::SYSTEM
:
1388 case SIAtomicScope::AGENT
:
1389 Changed
|= enableGLCBit(MI
);
1390 Changed
|= enableDLCBit(MI
);
1392 case SIAtomicScope::WORKGROUP
:
1393 // In WGP mode the waves of a work-group can be executing on either CU of
1394 // the WGP. Therefore need to bypass the L0 which is per CU. Otherwise in
1395 // CU mode all waves of a work-group are on the same CU, and so the L0
1396 // does not need to be bypassed.
1397 if (!ST
.isCuModeEnabled()) Changed
|= enableGLCBit(MI
);
1399 case SIAtomicScope::WAVEFRONT
:
1400 case SIAtomicScope::SINGLETHREAD
:
1401 // No cache to bypass.
1404 llvm_unreachable("Unsupported synchronization scope");
1408 /// The scratch address space does not need the global memory caches
1409 /// to be bypassed as all memory operations by the same thread are
1410 /// sequentially consistent, and no other thread can access scratch
1413 /// Other address spaces do not have a cache.
1418 bool SIGfx10CacheControl::enableVolatileAndOrNonTemporal(
1419 MachineBasicBlock::iterator
&MI
, SIAtomicAddrSpace AddrSpace
, SIMemOp Op
,
1420 bool IsVolatile
, bool IsNonTemporal
) const {
1422 // Only handle load and store, not atomic read-modify-write insructions. The
1423 // latter use glc to indicate if the atomic returns a result and so must not
1424 // be used for cache control.
1425 assert(MI
->mayLoad() ^ MI
->mayStore());
1427 // Only update load and store, not LLVM IR atomic read-modify-write
1428 // instructions. The latter are always marked as volatile so cannot sensibly
1429 // handle it as do not want to pessimize all atomics. Also they do not support
1430 // the nontemporal attribute.
1431 assert( Op
== SIMemOp::LOAD
|| Op
== SIMemOp::STORE
);
1433 bool Changed
= false;
1437 if (Op
== SIMemOp::LOAD
) {
1438 Changed
|= enableGLCBit(MI
);
1439 Changed
|= enableDLCBit(MI
);
1442 // Ensure operation has completed at system scope to cause all volatile
1443 // operations to be visible outside the program in a global order. Do not
1444 // request cross address space as only the global address space can be
1445 // observable outside the program, so no need to cause a waitcnt for LDS
1446 // address space operations.
1447 Changed
|= insertWait(MI
, SIAtomicScope::SYSTEM
, AddrSpace
, Op
, false,
1452 if (IsNonTemporal
) {
1453 // Request L0/L1 HIT_EVICT and L2 STREAM for load and store instructions.
1454 Changed
|= enableSLCBit(MI
);
1461 bool SIGfx10CacheControl::insertWait(MachineBasicBlock::iterator
&MI
,
1462 SIAtomicScope Scope
,
1463 SIAtomicAddrSpace AddrSpace
,
1465 bool IsCrossAddrSpaceOrdering
,
1466 Position Pos
) const {
1467 bool Changed
= false;
1469 MachineBasicBlock
&MBB
= *MI
->getParent();
1470 DebugLoc DL
= MI
->getDebugLoc();
1472 if (Pos
== Position::AFTER
)
1477 bool LGKMCnt
= false;
1479 if ((AddrSpace
& (SIAtomicAddrSpace::GLOBAL
| SIAtomicAddrSpace::SCRATCH
)) !=
1480 SIAtomicAddrSpace::NONE
) {
1482 case SIAtomicScope::SYSTEM
:
1483 case SIAtomicScope::AGENT
:
1484 if ((Op
& SIMemOp::LOAD
) != SIMemOp::NONE
)
1486 if ((Op
& SIMemOp::STORE
) != SIMemOp::NONE
)
1489 case SIAtomicScope::WORKGROUP
:
1490 // In WGP mode the waves of a work-group can be executing on either CU of
1491 // the WGP. Therefore need to wait for operations to complete to ensure
1492 // they are visible to waves in the other CU as the L0 is per CU.
1493 // Otherwise in CU mode and all waves of a work-group are on the same CU
1494 // which shares the same L0.
1495 if (!ST
.isCuModeEnabled()) {
1496 if ((Op
& SIMemOp::LOAD
) != SIMemOp::NONE
)
1498 if ((Op
& SIMemOp::STORE
) != SIMemOp::NONE
)
1502 case SIAtomicScope::WAVEFRONT
:
1503 case SIAtomicScope::SINGLETHREAD
:
1504 // The L0 cache keeps all memory operations in order for
1505 // work-items in the same wavefront.
1508 llvm_unreachable("Unsupported synchronization scope");
1512 if ((AddrSpace
& SIAtomicAddrSpace::LDS
) != SIAtomicAddrSpace::NONE
) {
1514 case SIAtomicScope::SYSTEM
:
1515 case SIAtomicScope::AGENT
:
1516 case SIAtomicScope::WORKGROUP
:
1517 // If no cross address space ordering then an "S_WAITCNT lgkmcnt(0)" is
1518 // not needed as LDS operations for all waves are executed in a total
1519 // global ordering as observed by all waves. Required if also
1520 // synchronizing with global/GDS memory as LDS operations could be
1521 // reordered with respect to later global/GDS memory operations of the
1523 LGKMCnt
|= IsCrossAddrSpaceOrdering
;
1525 case SIAtomicScope::WAVEFRONT
:
1526 case SIAtomicScope::SINGLETHREAD
:
1527 // The LDS keeps all memory operations in order for
1528 // the same wavesfront.
1531 llvm_unreachable("Unsupported synchronization scope");
1535 if ((AddrSpace
& SIAtomicAddrSpace::GDS
) != SIAtomicAddrSpace::NONE
) {
1537 case SIAtomicScope::SYSTEM
:
1538 case SIAtomicScope::AGENT
:
1539 // If no cross address space ordering then an GDS "S_WAITCNT lgkmcnt(0)"
1540 // is not needed as GDS operations for all waves are executed in a total
1541 // global ordering as observed by all waves. Required if also
1542 // synchronizing with global/LDS memory as GDS operations could be
1543 // reordered with respect to later global/LDS memory operations of the
1545 LGKMCnt
|= IsCrossAddrSpaceOrdering
;
1547 case SIAtomicScope::WORKGROUP
:
1548 case SIAtomicScope::WAVEFRONT
:
1549 case SIAtomicScope::SINGLETHREAD
:
1550 // The GDS keeps all memory operations in order for
1551 // the same work-group.
1554 llvm_unreachable("Unsupported synchronization scope");
1558 if (VMCnt
|| LGKMCnt
) {
1559 unsigned WaitCntImmediate
=
1560 AMDGPU::encodeWaitcnt(IV
,
1561 VMCnt
? 0 : getVmcntBitMask(IV
),
1562 getExpcntBitMask(IV
),
1563 LGKMCnt
? 0 : getLgkmcntBitMask(IV
));
1564 BuildMI(MBB
, MI
, DL
, TII
->get(AMDGPU::S_WAITCNT
)).addImm(WaitCntImmediate
);
1569 BuildMI(MBB
, MI
, DL
, TII
->get(AMDGPU::S_WAITCNT_VSCNT
))
1570 .addReg(AMDGPU::SGPR_NULL
, RegState::Undef
)
1575 if (Pos
== Position::AFTER
)
1581 bool SIGfx10CacheControl::insertAcquire(MachineBasicBlock::iterator
&MI
,
1582 SIAtomicScope Scope
,
1583 SIAtomicAddrSpace AddrSpace
,
1584 Position Pos
) const {
1585 if (!InsertCacheInv
)
1588 bool Changed
= false;
1590 MachineBasicBlock
&MBB
= *MI
->getParent();
1591 DebugLoc DL
= MI
->getDebugLoc();
1593 if (Pos
== Position::AFTER
)
1596 if ((AddrSpace
& SIAtomicAddrSpace::GLOBAL
) != SIAtomicAddrSpace::NONE
) {
1598 case SIAtomicScope::SYSTEM
:
1599 case SIAtomicScope::AGENT
:
1600 BuildMI(MBB
, MI
, DL
, TII
->get(AMDGPU::BUFFER_GL0_INV
));
1601 BuildMI(MBB
, MI
, DL
, TII
->get(AMDGPU::BUFFER_GL1_INV
));
1604 case SIAtomicScope::WORKGROUP
:
1605 // In WGP mode the waves of a work-group can be executing on either CU of
1606 // the WGP. Therefore need to invalidate the L0 which is per CU. Otherwise
1607 // in CU mode and all waves of a work-group are on the same CU, and so the
1608 // L0 does not need to be invalidated.
1609 if (!ST
.isCuModeEnabled()) {
1610 BuildMI(MBB
, MI
, DL
, TII
->get(AMDGPU::BUFFER_GL0_INV
));
1614 case SIAtomicScope::WAVEFRONT
:
1615 case SIAtomicScope::SINGLETHREAD
:
1616 // No cache to invalidate.
1619 llvm_unreachable("Unsupported synchronization scope");
1623 /// The scratch address space does not need the global memory cache
1624 /// to be flushed as all memory operations by the same thread are
1625 /// sequentially consistent, and no other thread can access scratch
1628 /// Other address spaces do not have a cache.
1630 if (Pos
== Position::AFTER
)
1636 bool SIMemoryLegalizer::removeAtomicPseudoMIs() {
1637 if (AtomicPseudoMIs
.empty())
1640 for (auto &MI
: AtomicPseudoMIs
)
1641 MI
->eraseFromParent();
1643 AtomicPseudoMIs
.clear();
1647 bool SIMemoryLegalizer::expandLoad(const SIMemOpInfo
&MOI
,
1648 MachineBasicBlock::iterator
&MI
) {
1649 assert(MI
->mayLoad() && !MI
->mayStore());
1651 bool Changed
= false;
1653 if (MOI
.isAtomic()) {
1654 if (MOI
.getOrdering() == AtomicOrdering::Monotonic
||
1655 MOI
.getOrdering() == AtomicOrdering::Acquire
||
1656 MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
) {
1657 Changed
|= CC
->enableLoadCacheBypass(MI
, MOI
.getScope(),
1658 MOI
.getOrderingAddrSpace());
1661 if (MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
)
1662 Changed
|= CC
->insertWait(MI
, MOI
.getScope(),
1663 MOI
.getOrderingAddrSpace(),
1664 SIMemOp::LOAD
| SIMemOp::STORE
,
1665 MOI
.getIsCrossAddressSpaceOrdering(),
1668 if (MOI
.getOrdering() == AtomicOrdering::Acquire
||
1669 MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
) {
1670 Changed
|= CC
->insertWait(MI
, MOI
.getScope(),
1671 MOI
.getInstrAddrSpace(),
1673 MOI
.getIsCrossAddressSpaceOrdering(),
1675 Changed
|= CC
->insertAcquire(MI
, MOI
.getScope(),
1676 MOI
.getOrderingAddrSpace(),
1683 // Atomic instructions already bypass caches to the scope specified by the
1684 // SyncScope operand. Only non-atomic volatile and nontemporal instructions
1685 // need additional treatment.
1686 Changed
|= CC
->enableVolatileAndOrNonTemporal(MI
, MOI
.getInstrAddrSpace(),
1687 SIMemOp::LOAD
, MOI
.isVolatile(),
1688 MOI
.isNonTemporal());
1692 bool SIMemoryLegalizer::expandStore(const SIMemOpInfo
&MOI
,
1693 MachineBasicBlock::iterator
&MI
) {
1694 assert(!MI
->mayLoad() && MI
->mayStore());
1696 bool Changed
= false;
1698 if (MOI
.isAtomic()) {
1699 if (MOI
.getOrdering() == AtomicOrdering::Monotonic
||
1700 MOI
.getOrdering() == AtomicOrdering::Release
||
1701 MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
) {
1702 Changed
|= CC
->enableStoreCacheBypass(MI
, MOI
.getScope(),
1703 MOI
.getOrderingAddrSpace());
1706 if (MOI
.getOrdering() == AtomicOrdering::Release
||
1707 MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
)
1708 Changed
|= CC
->insertRelease(MI
, MOI
.getScope(),
1709 MOI
.getOrderingAddrSpace(),
1710 MOI
.getIsCrossAddressSpaceOrdering(),
1716 // Atomic instructions already bypass caches to the scope specified by the
1717 // SyncScope operand. Only non-atomic volatile and nontemporal instructions
1718 // need additional treatment.
1719 Changed
|= CC
->enableVolatileAndOrNonTemporal(
1720 MI
, MOI
.getInstrAddrSpace(), SIMemOp::STORE
, MOI
.isVolatile(),
1721 MOI
.isNonTemporal());
1725 bool SIMemoryLegalizer::expandAtomicFence(const SIMemOpInfo
&MOI
,
1726 MachineBasicBlock::iterator
&MI
) {
1727 assert(MI
->getOpcode() == AMDGPU::ATOMIC_FENCE
);
1729 AtomicPseudoMIs
.push_back(MI
);
1730 bool Changed
= false;
1732 if (MOI
.isAtomic()) {
1733 if (MOI
.getOrdering() == AtomicOrdering::Acquire
||
1734 MOI
.getOrdering() == AtomicOrdering::Release
||
1735 MOI
.getOrdering() == AtomicOrdering::AcquireRelease
||
1736 MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
)
1737 /// TODO: This relies on a barrier always generating a waitcnt
1738 /// for LDS to ensure it is not reordered with the completion of
1739 /// the proceeding LDS operations. If barrier had a memory
1740 /// ordering and memory scope, then library does not need to
1741 /// generate a fence. Could add support in this file for
1742 /// barrier. SIInsertWaitcnt.cpp could then stop unconditionally
1743 /// adding S_WAITCNT before a S_BARRIER.
1744 Changed
|= CC
->insertRelease(MI
, MOI
.getScope(),
1745 MOI
.getOrderingAddrSpace(),
1746 MOI
.getIsCrossAddressSpaceOrdering(),
1749 // TODO: If both release and invalidate are happening they could be combined
1750 // to use the single "BUFFER_WBINV*" instruction. This could be done by
1751 // reorganizing this code or as part of optimizing SIInsertWaitcnt pass to
1752 // track cache invalidate and write back instructions.
1754 if (MOI
.getOrdering() == AtomicOrdering::Acquire
||
1755 MOI
.getOrdering() == AtomicOrdering::AcquireRelease
||
1756 MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
)
1757 Changed
|= CC
->insertAcquire(MI
, MOI
.getScope(),
1758 MOI
.getOrderingAddrSpace(),
1767 bool SIMemoryLegalizer::expandAtomicCmpxchgOrRmw(const SIMemOpInfo
&MOI
,
1768 MachineBasicBlock::iterator
&MI
) {
1769 assert(MI
->mayLoad() && MI
->mayStore());
1771 bool Changed
= false;
1773 if (MOI
.isAtomic()) {
1774 if (MOI
.getOrdering() == AtomicOrdering::Monotonic
||
1775 MOI
.getOrdering() == AtomicOrdering::Acquire
||
1776 MOI
.getOrdering() == AtomicOrdering::Release
||
1777 MOI
.getOrdering() == AtomicOrdering::AcquireRelease
||
1778 MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
) {
1779 Changed
|= CC
->enableRMWCacheBypass(MI
, MOI
.getScope(),
1780 MOI
.getInstrAddrSpace());
1783 if (MOI
.getOrdering() == AtomicOrdering::Release
||
1784 MOI
.getOrdering() == AtomicOrdering::AcquireRelease
||
1785 MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
||
1786 MOI
.getFailureOrdering() == AtomicOrdering::SequentiallyConsistent
)
1787 Changed
|= CC
->insertRelease(MI
, MOI
.getScope(),
1788 MOI
.getOrderingAddrSpace(),
1789 MOI
.getIsCrossAddressSpaceOrdering(),
1792 if (MOI
.getOrdering() == AtomicOrdering::Acquire
||
1793 MOI
.getOrdering() == AtomicOrdering::AcquireRelease
||
1794 MOI
.getOrdering() == AtomicOrdering::SequentiallyConsistent
||
1795 MOI
.getFailureOrdering() == AtomicOrdering::Acquire
||
1796 MOI
.getFailureOrdering() == AtomicOrdering::SequentiallyConsistent
) {
1797 Changed
|= CC
->insertWait(MI
, MOI
.getScope(),
1798 MOI
.getInstrAddrSpace(),
1799 isAtomicRet(*MI
) ? SIMemOp::LOAD
:
1801 MOI
.getIsCrossAddressSpaceOrdering(),
1803 Changed
|= CC
->insertAcquire(MI
, MOI
.getScope(),
1804 MOI
.getOrderingAddrSpace(),
1814 bool SIMemoryLegalizer::runOnMachineFunction(MachineFunction
&MF
) {
1815 bool Changed
= false;
1817 SIMemOpAccess
MOA(MF
);
1818 CC
= SICacheControl::create(MF
.getSubtarget
<GCNSubtarget
>());
1820 for (auto &MBB
: MF
) {
1821 for (auto MI
= MBB
.begin(); MI
!= MBB
.end(); ++MI
) {
1823 // Unbundle instructions after the post-RA scheduler.
1824 if (MI
->isBundle() && MI
->mayLoadOrStore()) {
1825 MachineBasicBlock::instr_iterator
II(MI
->getIterator());
1826 for (MachineBasicBlock::instr_iterator I
= ++II
, E
= MBB
.instr_end();
1827 I
!= E
&& I
->isBundledWithPred(); ++I
) {
1828 I
->unbundleFromPred();
1829 for (MachineOperand
&MO
: I
->operands())
1831 MO
.setIsInternalRead(false);
1834 MI
->eraseFromParent();
1835 MI
= II
->getIterator();
1838 if (!(MI
->getDesc().TSFlags
& SIInstrFlags::maybeAtomic
))
1841 if (const auto &MOI
= MOA
.getLoadInfo(MI
))
1842 Changed
|= expandLoad(MOI
.getValue(), MI
);
1843 else if (const auto &MOI
= MOA
.getStoreInfo(MI
))
1844 Changed
|= expandStore(MOI
.getValue(), MI
);
1845 else if (const auto &MOI
= MOA
.getAtomicFenceInfo(MI
))
1846 Changed
|= expandAtomicFence(MOI
.getValue(), MI
);
1847 else if (const auto &MOI
= MOA
.getAtomicCmpxchgOrRmwInfo(MI
))
1848 Changed
|= expandAtomicCmpxchgOrRmw(MOI
.getValue(), MI
);
1852 Changed
|= removeAtomicPseudoMIs();
1856 INITIALIZE_PASS(SIMemoryLegalizer
, DEBUG_TYPE
, PASS_NAME
, false, false)
1858 char SIMemoryLegalizer::ID
= 0;
1859 char &llvm::SIMemoryLegalizerID
= SIMemoryLegalizer::ID
;
1861 FunctionPass
*llvm::createSIMemoryLegalizerPass() {
1862 return new SIMemoryLegalizer();