- 
                Notifications
    You must be signed in to change notification settings 
- Fork 15k
[mlir][vector] Add mask elimination transform #99314
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      8a861c4
              
                [mlir][vector] Add mask elimination transform
              
              
                MacDue 6e14573
              
                Fixups
              
              
                MacDue 8d09451
              
                Comments
              
              
                MacDue f6acf2d
              
                Review fixups
              
              
                MacDue 692ce6e
              
                Fixups
              
              
                MacDue 259d1b0
              
                Share logic with `CreateMaskFolder`
              
              
                MacDue 7eaa991
              
                Update comments
              
              
                MacDue File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            118 changes: 118 additions & 0 deletions
          
          118 
        
  mlir/lib/Dialect/Vector/Transforms/VectorMaskElimination.cpp
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| //===- VectorMaskElimination.cpp - Eliminate Vector Masks -----------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|  | ||
| #include "mlir/Dialect/Arith/IR/Arith.h" | ||
| #include "mlir/Dialect/Utils/StaticValueUtils.h" | ||
| #include "mlir/Dialect/Vector/IR/ScalableValueBoundsConstraintSet.h" | ||
| #include "mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h" | ||
| #include "mlir/Dialect/Vector/Transforms/VectorTransforms.h" | ||
| #include "mlir/Interfaces/FunctionInterfaces.h" | ||
|  | ||
| using namespace mlir; | ||
| using namespace mlir::vector; | ||
| namespace { | ||
|  | ||
| /// Attempts to resolve a (scalable) CreateMaskOp to an all-true constant mask. | ||
| /// All-true masks can then be eliminated by simple folds. | ||
| LogicalResult resolveAllTrueCreateMaskOp(IRRewriter &rewriter, | ||
| vector::CreateMaskOp createMaskOp, | ||
| VscaleRange vscaleRange) { | ||
| auto maskType = createMaskOp.getVectorType(); | ||
| auto maskTypeDimScalableFlags = maskType.getScalableDims(); | ||
| auto maskTypeDimSizes = maskType.getShape(); | ||
|  | ||
| struct UnknownMaskDim { | ||
| size_t position; | ||
| Value dimSize; | ||
| }; | ||
|  | ||
| // Loop over the CreateMaskOp operands and collect unknown dims (i.e. dims | ||
| // that are not obviously constant). If any constant dimension is not all-true | ||
| // bail out early (as this transform only trying to resolve all-true masks). | ||
| // This avoids doing value-bounds anaylis in cases like: | ||
| // `%mask = vector.create_mask %dynamicValue, %c2 : vector<8x4xi1>` | ||
| // ...where it is known the mask is not all-true by looking at `%c2`. | ||
| SmallVector<UnknownMaskDim> unknownDims; | ||
| for (auto [i, dimSize] : llvm::enumerate(createMaskOp.getOperands())) { | ||
| if (auto intSize = getConstantIntValue(dimSize)) { | ||
| // Mask not all-true for this dim. | ||
| if (maskTypeDimScalableFlags[i] || intSize < maskTypeDimSizes[i]) | ||
| return failure(); | ||
|         
                  banach-space marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| } else if (auto vscaleMultiplier = getConstantVscaleMultiplier(dimSize)) { | ||
| // Mask not all-true for this dim. | ||
| if (vscaleMultiplier < maskTypeDimSizes[i]) | ||
| return failure(); | ||
| } else { | ||
| // Unknown (without further analysis). | ||
| unknownDims.push_back(UnknownMaskDim{i, dimSize}); | ||
| } | ||
| } | ||
|  | ||
| for (auto [i, dimSize] : unknownDims) { | ||
| // Compute the lower bound for the unknown dimension (i.e. the smallest | ||
| // value it could be). | ||
| FailureOr<ConstantOrScalableBound> dimLowerBound = | ||
| vector::ScalableValueBoundsConstraintSet::computeScalableBound( | ||
| dimSize, {}, vscaleRange.vscaleMin, vscaleRange.vscaleMax, | ||
| presburger::BoundType::LB); | ||
| if (failed(dimLowerBound)) | ||
| return failure(); | ||
|         
                  c-rhodes marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| auto dimLowerBoundSize = dimLowerBound->getSize(); | ||
| if (failed(dimLowerBoundSize)) | ||
| return failure(); | ||
| if (dimLowerBoundSize->scalable) { | ||
| // 1. The lower bound, LB, is scalable. If LB is < the mask dim size then | ||
| // this dim is not all-true. | ||
| if (dimLowerBoundSize->baseSize < maskTypeDimSizes[i]) | ||
| return failure(); | ||
| } else { | ||
| // 2. The lower bound, LB, is a constant. | ||
| // - If the mask dim size is scalable then this dim is not all-true. | ||
| if (maskTypeDimScalableFlags[i]) | ||
| return failure(); | ||
| // - If LB < the _fixed-size_ mask dim size then this dim is not all-true. | ||
| if (dimLowerBoundSize->baseSize < maskTypeDimSizes[i]) | ||
| return failure(); | ||
| } | ||
| } | ||
|  | ||
| // Replace createMaskOp with an all-true constant. This should result in the | ||
| // mask being removed in most cases (as xfer ops + vector.mask have folds to | ||
| // remove all-true masks). | ||
| auto allTrue = rewriter.create<vector::ConstantMaskOp>( | ||
| createMaskOp.getLoc(), maskType, ConstantMaskKind::AllTrue); | ||
| rewriter.replaceAllUsesWith(createMaskOp, allTrue); | ||
| return success(); | ||
| } | ||
|  | ||
| } // namespace | ||
|  | ||
| namespace mlir::vector { | ||
|  | ||
| void eliminateVectorMasks(IRRewriter &rewriter, FunctionOpInterface function, | ||
| std::optional<VscaleRange> vscaleRange) { | ||
| // TODO: Support fixed-size case. This is less likely to be useful as for | ||
| // fixed-size code dimensions are all static so masks tend to fold away. | ||
| if (!vscaleRange) | ||
| return; | ||
|  | ||
| OpBuilder::InsertionGuard g(rewriter); | ||
|  | ||
| // Build worklist so we can safely insert new ops in | ||
| // `resolveAllTrueCreateMaskOp()`. | ||
| SmallVector<vector::CreateMaskOp> worklist; | ||
| function.walk([&](vector::CreateMaskOp createMaskOp) { | ||
| worklist.push_back(createMaskOp); | ||
| }); | ||
|  | ||
| rewriter.setInsertionPointToStart(&function.front()); | ||
| for (auto mask : worklist) | ||
| (void)resolveAllTrueCreateMaskOp(rewriter, mask, *vscaleRange); | ||
| } | ||
|  | ||
| } // namespace mlir::vector | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.