- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
          [mlir][amdgpu] Add rocdl.s.waitcnt wrapper
          #149670
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
f3bc55c
              5320853
              a856915
              811633e
              b02a25c
              0660da5
              611f679
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -419,6 +419,105 @@ struct RawBufferOpLowering : public ConvertOpToLLVMPattern<GpuOp> { | |
| } | ||
| }; | ||
| 
     | 
||
| // TODO: AMDGPU backend already have all this bitpacking logic, we should move | ||
| // it to some common place. | ||
| /// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are decoded as follows: | ||
| /// \p Vmcnt = \p Waitcnt[3:0] (pre-gfx9) | ||
| /// \p Vmcnt = \p Waitcnt[15:14,3:0] (gfx9,10) | ||
| /// \p Vmcnt = \p Waitcnt[15:10] (gfx11) | ||
| /// \p Expcnt = \p Waitcnt[6:4] (pre-gfx11) | ||
| /// \p Expcnt = \p Waitcnt[2:0] (gfx11) | ||
| /// \p Lgkmcnt = \p Waitcnt[11:8] (pre-gfx10) | ||
| /// \p Lgkmcnt = \p Waitcnt[13:8] (gfx10) | ||
| /// \p Lgkmcnt = \p Waitcnt[9:4] (gfx11) | ||
| static FailureOr<unsigned> encodeWaitcnt(Chipset chipset, unsigned vmcnt, | ||
| unsigned expcnt, unsigned lgkmcnt) { | ||
| if (chipset.majorVersion < 9) { | ||
| vmcnt = std::min(15u, vmcnt); | ||
| expcnt = std::min(7u, expcnt); | ||
| lgkmcnt = std::min(15u, lgkmcnt); | ||
| return vmcnt | (expcnt << 4) | (lgkmcnt << 8); | ||
| } | ||
| if (chipset.majorVersion == 9) { | ||
                
      
                  Hardcode84 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| vmcnt = std::min(63u, vmcnt); | ||
| expcnt = std::min(7u, expcnt); | ||
| lgkmcnt = std::min(15u, lgkmcnt); | ||
| unsigned lowBits = vmcnt & 0xF; | ||
| unsigned highBits = (vmcnt >> 4) << 14; | ||
| unsigned otherCnts = (expcnt << 4) | (lgkmcnt << 8); | ||
| return lowBits | highBits | otherCnts; | ||
| } | ||
| if (chipset.majorVersion == 10) { | ||
| vmcnt = std::min(63u, vmcnt); | ||
| expcnt = std::min(7u, expcnt); | ||
| lgkmcnt = std::min(63u, lgkmcnt); | ||
| unsigned lowBits = vmcnt & 0xF; | ||
| unsigned highBits = (vmcnt >> 4) << 14; | ||
| unsigned otherCnts = (expcnt << 4) | (lgkmcnt << 8); | ||
| return lowBits | highBits | otherCnts; | ||
| } | ||
| if (chipset.majorVersion == 11) { | ||
| vmcnt = std::min(63u, vmcnt); | ||
| expcnt = std::min(7u, expcnt); | ||
| lgkmcnt = std::min(63u, lgkmcnt); | ||
| return (vmcnt << 10) | expcnt | (lgkmcnt << 4); | ||
| } | ||
| return failure(); | ||
                
       | 
||
| } | ||
| 
     | 
||
| struct MemoryCounterWaitOpLowering | ||
| : public ConvertOpToLLVMPattern<MemoryCounterWaitOp> { | ||
| MemoryCounterWaitOpLowering(const LLVMTypeConverter &converter, | ||
| Chipset chipset) | ||
| : ConvertOpToLLVMPattern<MemoryCounterWaitOp>(converter), | ||
| chipset(chipset) {} | ||
| 
     | 
||
| Chipset chipset; | ||
| 
     | 
||
| LogicalResult | ||
| matchAndRewrite(MemoryCounterWaitOp op, OpAdaptor adaptor, | ||
| ConversionPatternRewriter &rewriter) const override { | ||
| if (chipset.majorVersion >= 12) { | ||
| Location loc = op.getLoc(); | ||
| if (auto ds = adaptor.getDs()) | ||
                
      
                  kuhar marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| rewriter.create<ROCDL::WaitDscntOp>(loc, *ds); | ||
| 
     | 
||
| if (auto load = adaptor.getLoad()) | ||
| rewriter.create<ROCDL::WaitLoadcntOp>(loc, *load); | ||
| 
     | 
||
| if (auto store = adaptor.getStore()) | ||
| rewriter.create<ROCDL::WaitStorecntOp>(loc, *store); | ||
| 
     | 
||
| if (auto exp = adaptor.getExp()) | ||
| rewriter.create<ROCDL::WaitExpcntOp>(loc, *exp); | ||
| 
     | 
||
| rewriter.eraseOp(op); | ||
| return success(); | ||
| } | ||
| 
     | 
||
| auto getVal = [](Attribute attr) -> unsigned { | ||
| if (attr) | ||
| return cast<IntegerAttr>(attr).getInt(); | ||
| 
     | 
||
| // This value will be clamped to the maximum value for the chipset. | ||
| return 1024 * 1024; | ||
| }; | ||
| unsigned ds = getVal(adaptor.getDsAttr()); | ||
| unsigned load = getVal(adaptor.getLoadAttr()); | ||
| unsigned store = getVal(adaptor.getStoreAttr()); | ||
| unsigned exp = getVal(adaptor.getExpAttr()); | ||
| 
     | 
||
| unsigned vmcnt = std::min(load, store); | ||
                
       | 
||
| 
     | 
||
| FailureOr<unsigned> waitcnt = encodeWaitcnt(chipset, vmcnt, exp, ds); | ||
| if (failed(waitcnt)) | ||
| return op.emitOpError("unsupported chipset"); | ||
| 
     | 
||
| rewriter.replaceOpWithNewOp<ROCDL::SWaitcntOp>(op, *waitcnt); | ||
                
      
                  raikonenfnu marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| return success(); | ||
| } | ||
| }; | ||
| 
     | 
||
| struct LDSBarrierOpLowering : public ConvertOpToLLVMPattern<LDSBarrierOp> { | ||
| LDSBarrierOpLowering(const LLVMTypeConverter &converter, Chipset chipset) | ||
| : ConvertOpToLLVMPattern<LDSBarrierOp>(converter), chipset(chipset) {} | ||
| 
          
            
          
           | 
    @@ -1825,9 +1924,9 @@ void mlir::populateAMDGPUToROCDLConversionPatterns(LLVMTypeConverter &converter, | |
| ROCDL::RawPtrBufferAtomicUminOp>, | ||
| RawBufferOpLowering<RawBufferAtomicCmpswapOp, | ||
| ROCDL::RawPtrBufferAtomicCmpSwap>, | ||
| AMDGPUDPPLowering, LDSBarrierOpLowering, SchedBarrierOpLowering, | ||
| MFMAOpLowering, ScaledMFMAOpLowering, WMMAOpLowering, | ||
| ExtPackedFp8OpLowering, ScaledExtPackedOpLowering, | ||
| AMDGPUDPPLowering, MemoryCounterWaitOpLowering, LDSBarrierOpLowering, | ||
| SchedBarrierOpLowering, MFMAOpLowering, ScaledMFMAOpLowering, | ||
| WMMAOpLowering, ExtPackedFp8OpLowering, ScaledExtPackedOpLowering, | ||
| PackedScaledTruncOpLowering, PackedTrunc2xFp8OpLowering, | ||
| PackedStochRoundFp8OpLowering, GatherToLDSOpLowering, | ||
| TransposeLoadOpLowering>(converter, chipset); | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // RUN: mlir-opt %s -convert-amdgpu-to-rocdl=chipset=gfx942 | FileCheck %s --check-prefixes=CHECK,GFX9 | ||
| // RUN: mlir-opt %s -convert-amdgpu-to-rocdl=chipset=gfx1030 | FileCheck %s --check-prefixes=CHECK,GFX10 | ||
| // RUN: mlir-opt %s -convert-amdgpu-to-rocdl=chipset=gfx1100 | FileCheck %s --check-prefixes=CHECK,GFX11 | ||
| // RUN: mlir-opt %s -convert-amdgpu-to-rocdl=chipset=gfx1201 | FileCheck %s --check-prefixes=CHECK,GFX12 | ||
| 
     | 
||
| // CHECK-LABEL: func @memory_counter_wait | ||
| func.func @memory_counter_wait() { | ||
| // GFX9: rocdl.s.waitcnt 53119 | ||
| // GFX10: rocdl.s.waitcnt 65407 | ||
| // GFX11: rocdl.s.waitcnt 65527 | ||
| // GFX12-NOT: rocdl.s.wait.loadcnt | ||
| // GFX12-NOT: rocdl.s.wait.storecnt | ||
| // GFX12-NOT: rocdl.s.wait.expcnt | ||
| // GFX12-NOT: rocdl.s.wait.dscnt | ||
| amdgpu.memory_counter_wait | ||
| 
     | 
||
| // GFX9: rocdl.s.waitcnt 3952 | ||
| // GFX10: rocdl.s.waitcnt 16240 | ||
| // GFX11: rocdl.s.waitcnt 1015 | ||
| // GFX12: rocdl.s.wait.loadcnt 0 | ||
| amdgpu.memory_counter_wait load(0) | ||
| 
     | 
||
| // GFX9: rocdl.s.waitcnt 3952 | ||
| // GFX10: rocdl.s.waitcnt 16240 | ||
| // GFX11: rocdl.s.waitcnt 1015 | ||
| // GFX12: rocdl.s.wait.storecnt 0 | ||
| amdgpu.memory_counter_wait store(0) | ||
| 
     | 
||
| // GFX9: rocdl.s.waitcnt 53007 | ||
| // GFX10: rocdl.s.waitcnt 65295 | ||
| // GFX11: rocdl.s.waitcnt 65520 | ||
| // GFX12: rocdl.s.wait.expcnt 0 | ||
| amdgpu.memory_counter_wait exp(0) | ||
| 
     | 
||
| // GFX9: rocdl.s.waitcnt 49279 | ||
| // GFX10: rocdl.s.waitcnt 49279 | ||
| // GFX11: rocdl.s.waitcnt 64519 | ||
| // GFX12: rocdl.s.wait.dscnt 0 | ||
| amdgpu.memory_counter_wait ds(0) | ||
| 
     | 
||
| return | ||
| } | 
Uh oh!
There was an error while loading. Please reload this page.