|
| 1 | +#include "triton/Analysis/Allocation.h" |
| 2 | +#include "triton/Analysis/Membar.h" |
| 3 | +#include "triton/Analysis/Utility.h" |
| 4 | +#include "triton/Dialect/TritonGPU/IR/Dialect.h" |
| 5 | +#include "triton/Dialect/TritonNvidiaGPU/IR/Dialect.h" |
| 6 | +#include "triton/Dialect/TritonNvidiaGPU/Transforms/Passes.h" |
| 7 | + |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// On Hopper+, async proxy is separate from generic proxy, so when shared memory |
| 11 | +// is the generic proxy to the async proxy we need to insert a fence to ensure |
| 12 | +// memory consistency. |
| 13 | +// This pass analyzes dependencies and will conservatively insert fences to |
| 14 | +// avoid race conditions between proxies. Async proxy is defined here: |
| 15 | +// https://docs.nvidia.com/cuda/parallel-thread-execution/#async-proxy |
| 16 | +// |
| 17 | +// This pass runs after shared memory allocation, to make sure we insert fences |
| 18 | +// between ops accessing aliasing buffers if needed. |
| 19 | +// |
| 20 | +// We also run a fence insertion pass during optimization phase as it is easier |
| 21 | +// to insert fences at optimial location based on structured control flow. |
| 22 | +// |
| 23 | +//===----------------------------------------------------------------------===// |
| 24 | + |
| 25 | +namespace mlir { |
| 26 | +namespace triton { |
| 27 | +namespace nvidia_gpu { |
| 28 | + |
| 29 | +#define GEN_PASS_DEF_TRITONGPUPROXYFENCEINSERTION |
| 30 | +#include "triton/Dialect/TritonNvidiaGPU/Transforms/Passes.h.inc" |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +bool isAsyncProxyWrite(Operation *op) { |
| 35 | + return isa<triton::nvidia_gpu::AsyncTMACopyGlobalToLocalOp, |
| 36 | + triton::nvidia_gpu::AsyncTMAGatherOp>(op); |
| 37 | +} |
| 38 | + |
| 39 | +Value getSmemDest(Operation *op) { |
| 40 | + if (auto asyncTMACopyGlobalToLocalOp = |
| 41 | + dyn_cast<triton::nvidia_gpu::AsyncTMACopyGlobalToLocalOp>(op)) { |
| 42 | + return asyncTMACopyGlobalToLocalOp.getResult(); |
| 43 | + } |
| 44 | + if (auto asyncTMAGatherOp = |
| 45 | + dyn_cast<triton::nvidia_gpu::AsyncTMAGatherOp>(op)) { |
| 46 | + return asyncTMAGatherOp.getResult(); |
| 47 | + } |
| 48 | + return Value(); |
| 49 | +} |
| 50 | + |
| 51 | +bool isAsyncProxyRead(Operation *op) { |
| 52 | + return isa<triton::nvidia_gpu::WarpGroupDotOp, |
| 53 | + triton::nvidia_gpu::TCGen5MMAOp, |
| 54 | + triton::nvidia_gpu::TCGen5MMAScaledOp, |
| 55 | + triton::nvidia_gpu::AsyncTMACopyGlobalToLocalOp, |
| 56 | + triton::nvidia_gpu::AsyncTMAScatterOp, |
| 57 | + triton::nvidia_gpu::AsyncTMAReduceOp>(op); |
| 58 | +} |
| 59 | + |
| 60 | +bool ignoreOpForProxyFence(Operation *op) { |
| 61 | + return isAsyncProxyRead(op) || isAsyncProxyWrite(op) || |
| 62 | + isa<triton::nvidia_gpu::ArriveBarrierOp, |
| 63 | + triton::nvidia_gpu::TMEMCopyOp, triton::nvidia_gpu::WaitBarrierOp, |
| 64 | + triton::nvidia_gpu::InitBarrierOp, |
| 65 | + triton::nvidia_gpu::InvalBarrierOp>(op); |
| 66 | +} |
| 67 | + |
| 68 | +bool filterFn(Operation *op, Operation *other) { |
| 69 | + return ignoreOpForProxyFence(other); |
| 70 | +} |
| 71 | + |
| 72 | +//===----------------------------------------------------------------------===// |
| 73 | +// Proxy Fence Analysis |
| 74 | +//===----------------------------------------------------------------------===// |
| 75 | +class ProxyFenceAnalysis : public MembarOrFenceAnalysis { |
| 76 | + |
| 77 | +public: |
| 78 | + ProxyFenceAnalysis() = default; |
| 79 | + explicit ProxyFenceAnalysis(Allocation *allocation, MembarFilterFn filter) |
| 80 | + : MembarOrFenceAnalysis(allocation, filter) {} |
| 81 | + |
| 82 | +private: |
| 83 | + /// Updates the BlockInfo operation based on the operation. |
| 84 | + virtual void update(Operation *operation, BlockInfo *blockInfo, |
| 85 | + FuncBlockInfoMapT *funcBlockInfoMap, |
| 86 | + OpBuilder *builder) override; |
| 87 | + |
| 88 | + void insertFence(Operation *operation, OpBuilder *builder); |
| 89 | +}; |
| 90 | + |
| 91 | +void ProxyFenceAnalysis::insertFence(Operation *op, OpBuilder *builder) { |
| 92 | + OpBuilder::InsertionGuard g(*builder); |
| 93 | + builder->create<triton::nvidia_gpu::FenceAsyncSharedOp>(op->getLoc(), false); |
| 94 | +} |
| 95 | + |
| 96 | +void ProxyFenceAnalysis::update(Operation *op, BlockInfo *blockInfo, |
| 97 | + FuncBlockInfoMapT *funcBlockInfoMap, |
| 98 | + OpBuilder *builder) { |
| 99 | + if (isa<triton::nvidia_gpu::FenceAsyncSharedOp>(op)) { |
| 100 | + // If the current op is a fence, we clear previous reads and writes |
| 101 | + blockInfo->sync(); |
| 102 | + return; |
| 103 | + } |
| 104 | + BlockInfo curBlockInfo; |
| 105 | + BlockInfo proxyBlockInfo; |
| 106 | + |
| 107 | + auto scratchBufferId = Allocation::InvalidBufferId; |
| 108 | + if (isa<triton::CallOp>(op)) { |
| 109 | + // Inter-function dependencies |
| 110 | + auto callOpInterface = dyn_cast<CallOpInterface>(op); |
| 111 | + if (auto callee = |
| 112 | + dyn_cast<FunctionOpInterface>(callOpInterface.resolveCallable())) |
| 113 | + curBlockInfo = funcBlockInfoMap->lookup(callee); |
| 114 | + } else { |
| 115 | + // Intra-function dependencies |
| 116 | + if (auto memoryEffectOpInterface = dyn_cast<MemoryEffectOpInterface>(op)) { |
| 117 | + // Explicit buffer |
| 118 | + SmallVector<SideEffects::EffectInstance<MemoryEffects::Effect>> |
| 119 | + effectInstances; |
| 120 | + memoryEffectOpInterface.getEffects(effectInstances); |
| 121 | + for (auto effectInstance : effectInstances) { |
| 122 | + if (auto value = effectInstance.getValue()) { |
| 123 | + for (auto bufferId : allocation->getBufferIds(value)) { |
| 124 | + if (bufferId != Allocation::InvalidBufferId) { |
| 125 | + // TODO: handle proxy read cases. Those are currently handled in |
| 126 | + // FenceInsertionPass where it can generate better placement for |
| 127 | + // the fence. But we should support a safe fallback here. |
| 128 | + if (isAsyncProxyWrite(op)) { |
| 129 | + if (value == getSmemDest(op)) { |
| 130 | + proxyBlockInfo |
| 131 | + .syncWriteIntervals[allocation->getAllocatedInterval( |
| 132 | + bufferId)] |
| 133 | + .insert(op); |
| 134 | + } |
| 135 | + } else if (isa<MemoryEffects::Write>( |
| 136 | + effectInstance.getEffect())) { |
| 137 | + curBlockInfo |
| 138 | + .syncWriteIntervals[allocation->getAllocatedInterval( |
| 139 | + bufferId)] |
| 140 | + .insert(op); |
| 141 | + } else if (isa<MemoryEffects::Read>(effectInstance.getEffect())) { |
| 142 | + curBlockInfo |
| 143 | + .syncReadIntervals[allocation->getAllocatedInterval( |
| 144 | + bufferId)] |
| 145 | + .insert(op); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + scratchBufferId = allocation->getBufferId(op); |
| 153 | + } |
| 154 | + |
| 155 | + // Scratch buffer operations consist of a series of shared memory operations |
| 156 | + // starting from a shared memory write, followed by a series of shared memory |
| 157 | + // read/write operations, mark them as a read. |
| 158 | + if (scratchBufferId != Allocation::InvalidBufferId) { |
| 159 | + auto interval = allocation->getAllocatedInterval(scratchBufferId); |
| 160 | + curBlockInfo.syncReadIntervals[interval].insert(op); |
| 161 | + } |
| 162 | + if (isAsyncProxyWrite(op) || isAsyncProxyRead(op)) { |
| 163 | + if (proxyBlockInfo.isIntersected(*blockInfo, filter)) { |
| 164 | + builder->setInsertionPoint(op); |
| 165 | + insertFence(op, builder); |
| 166 | + blockInfo->sync(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + // Update the region info, even if barrier is inserted, we have to maintain |
| 171 | + // the current op's read/write buffers. |
| 172 | + blockInfo->join(curBlockInfo); |
| 173 | +} |
| 174 | +} // namespace |
| 175 | + |
| 176 | +struct ProxyFenceInsertionPass |
| 177 | + : public impl::TritonGPUProxyFenceInsertionBase<ProxyFenceInsertionPass> { |
| 178 | + |
| 179 | +public: |
| 180 | + using impl::TritonGPUProxyFenceInsertionBase< |
| 181 | + ProxyFenceInsertionPass>::TritonGPUProxyFenceInsertionBase; |
| 182 | + void runOnOperation() override { |
| 183 | + // Only insert fences for compute capability 9.0 |
| 184 | + if (computeCapability < 90) |
| 185 | + return; |
| 186 | + ModuleOp mod = getOperation(); |
| 187 | + ModuleAllocation allocation(mod); |
| 188 | + ModuleMembarOrFenceAnalysis<ProxyFenceAnalysis> analysis(&allocation, |
| 189 | + filterFn); |
| 190 | + analysis.run(); |
| 191 | + } |
| 192 | +}; |
| 193 | + |
| 194 | +} // namespace nvidia_gpu |
| 195 | +} // namespace triton |
| 196 | +} // namespace mlir |
0 commit comments