- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[Matrix] Propagate shape information through PHI insts #141681
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 31 commits
      Commits
    
    
            Show all changes
          
          
            36 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b1b79aa
              
                [Matrix] Propagate shape information through PHI instructions
              
              
                jroelofs 71b99d3
              
                move formerly unsupported test to new home
              
              
                jroelofs 905c1e9
              
                clang-format
              
              
                jroelofs 9ee44f0
              
                add test for ConstantDataVector lowering
              
              
                jroelofs 169960d
              
                move report_fatal_error outside of NDEBUG block
              
              
                jroelofs b64a134
              
                Merge branch 'main' into jroelofs/lower-matrix-phi
              
              
                jroelofs 18951cd
              
                fix bad merge
              
              
                jroelofs f8aea05
              
                use col major load intrinsics
              
              
                jroelofs e56b225
              
                add tests for phi's consuming phi's, and phi's with more than two inputs
              
              
                jroelofs ffbc73f
              
                handle phi's more like other ops. instcombine will clean up after us
              
              
                jroelofs 15fd60b
              
                handle phi's with shape mismatch
              
              
                jroelofs 88bd8cb
              
                Merge branch 'main' into jroelofs/lower-matrix-phi
              
              
                jroelofs 655eb88
              
                simplify getMatrix shim
              
              
                jroelofs e262f76
              
                test the other order of shape mismatch
              
              
                jroelofs 2c86c2f
              
                clang-format
              
              
                jroelofs 86d3545
              
                Merge remote-tracking branch 'origin/main' into jroelofs/lower-matrix…
              
              
                jroelofs 501414f
              
                Merge remote-tracking branch 'origin/main' into jroelofs/lower-matrix…
              
              
                jroelofs 2e5b2d4
              
                clang-format
              
              
                jroelofs f048181
              
                Merge remote-tracking branch 'origin/main' into jroelofs/lower-matrix…
              
              
                jroelofs 7511d17
              
                Merge remote-tracking branch 'origin/main' into jroelofs/lower-matrix…
              
              
                jroelofs 2821467
              
                Merge remote-tracking branch 'origin/main' into jroelofs/lower-matrix…
              
              
                jroelofs 4ba4e66
              
                [Matrix] Fix a crash in VisitSelectInst due to iteration length mismatch
              
              
                jroelofs 4cbc839
              
                review feedback: parens for initializer
              
              
                jroelofs 6f8ec49
              
                review feedback: rename to GetMatrix
              
              
                jroelofs 104c126
              
                Merge remote-tracking branch 'origin/main' into jroelofs/lower-matrix…
              
              
                jroelofs 67ead37
              
                drop code for splitting constants, add test for it
              
              
                jroelofs c9b3992
              
                split phi's in two phases
              
              
                jroelofs da17d10
              
                Merge remote-tracking branch 'origin/main' into jroelofs/lower-matrix…
              
              
                jroelofs dd55682
              
                clang-format
              
              
                jroelofs 08be3b4
              
                Merge branch 'main' into jroelofs/lower-matrix-phi
              
              
                jroelofs 5a991f7
              
                rm constant.ll
              
              
                jroelofs e9d0e62
              
                test that shows reshape shuffles are inserted in the correct spot
              
              
                jroelofs a760840
              
                florian's suggestion is a little simpler: we already know it's a phi
              
              
                jroelofs a62ef50
              
                also use getInsertionPointAtDef() for non-inst phi operand reshape pl…
              
              
                jroelofs 47dd2e2
              
                inline the lambda shim, to simplify
              
              
                jroelofs 0debc08
              
                rm unnecessary include
              
              
                jroelofs 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -32,6 +32,7 @@ | |
| #include "llvm/Analysis/ValueTracking.h" | ||
| #include "llvm/Analysis/VectorUtils.h" | ||
| #include "llvm/IR/CFG.h" | ||
| #include "llvm/IR/Constants.h" | ||
| #include "llvm/IR/DataLayout.h" | ||
| #include "llvm/IR/DebugInfoMetadata.h" | ||
| #include "llvm/IR/DerivedTypes.h" | ||
| 
          
            
          
           | 
    @@ -288,6 +289,7 @@ static bool isUniformShape(Value *V) { | |
| } | ||
| 
     | 
||
| switch (I->getOpcode()) { | ||
| case Instruction::PHI: | ||
| case Instruction::FNeg: | ||
| return true; | ||
| default: | ||
| 
          
            
          
           | 
    @@ -1136,7 +1138,27 @@ class LowerMatrixIntrinsics { | |
| 
     | 
||
| Changed |= !FusedInsts.empty(); | ||
| 
     | 
||
| // Fourth, lower remaining instructions with shape information. | ||
| // Fourth, pre-process all the PHINode's. The incoming values will be | ||
| // assigned later in VisitPHI. | ||
| for (Instruction *Inst : MatrixInsts) { | ||
| auto *PHI = dyn_cast<PHINode>(Inst); | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were missing an early continue here if Inst is in FusedInsts, which prevents heap-use-after-free if Inst has been removed before. W/o that, the tests fail with ASan. Should be fixed in 0816bb3  | 
||
| if (!PHI) | ||
| continue; | ||
| 
     | 
||
| const ShapeInfo &SI = ShapeMap.at(Inst); | ||
| auto *EltTy = cast<FixedVectorType>(PHI->getType())->getElementType(); | ||
| MatrixTy PhiM(SI.NumRows, SI.NumColumns, EltTy); | ||
| 
     | 
||
| IRBuilder<> Builder(Inst); | ||
| for (unsigned VI = 0, VE = PhiM.getNumVectors(); VI != VE; ++VI) | ||
| PhiM.setVector(VI, Builder.CreatePHI(PhiM.getVectorTy(), | ||
| PHI->getNumIncomingValues(), | ||
| PHI->getName())); | ||
| assert(!Inst2ColumnMatrix.contains(PHI) && "map already contains phi?"); | ||
| Inst2ColumnMatrix[PHI] = PhiM; | ||
| } | ||
| 
     | 
||
| // Fifth, lower remaining instructions with shape information. | ||
| for (Instruction *Inst : MatrixInsts) { | ||
| if (FusedInsts.count(Inst)) | ||
| continue; | ||
| 
        
          
        
         | 
    @@ -1161,6 +1183,8 @@ class LowerMatrixIntrinsics { | |
| Result = VisitLoad(cast<LoadInst>(Inst), SI, Op1, Builder); | ||
| else if (match(Inst, m_Store(m_Value(Op1), m_Value(Op2)))) | ||
| Result = VisitStore(cast<StoreInst>(Inst), SI, Op1, Op2, Builder); | ||
| else if (auto *PHI = dyn_cast<PHINode>(Inst)) | ||
| Result = VisitPHI(PHI, SI, Builder); | ||
| else | ||
| continue; | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -1458,7 +1482,8 @@ class LowerMatrixIntrinsics { | |
| IRBuilder<> &Builder) { | ||
| auto inserted = Inst2ColumnMatrix.insert(std::make_pair(Inst, Matrix)); | ||
| (void)inserted; | ||
| assert(inserted.second && "multiple matrix lowering mapping"); | ||
| assert((inserted.second || isa<PHINode>(Inst)) && | ||
| "multiple matrix lowering mapping"); | ||
| 
     | 
||
| ToRemove.push_back(Inst); | ||
| Value *Flattened = nullptr; | ||
| 
          
            
          
           | 
    @@ -2239,6 +2264,41 @@ class LowerMatrixIntrinsics { | |
| Builder); | ||
| } | ||
| 
     | 
||
| MatrixTy VisitPHI(PHINode *Inst, const ShapeInfo &SI, IRBuilder<> &Builder) { | ||
| // Shim this->getMatrix to adjust where it creates new instructions, which | ||
| // it may need to insert for re-shaping. | ||
| auto GetMatrix = [this, &Builder, SI, Inst](Value *MatrixVal) -> MatrixTy { | ||
| IRBuilder<>::InsertPointGuard IPG(Builder); | ||
| if (auto *MatrixInst = dyn_cast<Instruction>(MatrixVal)) { | ||
| if (auto MaybeIP = MatrixInst->getInsertionPointAfterDef()) | ||
| Builder.SetInsertPoint(*MaybeIP); | ||
| } else | ||
| Builder.SetInsertPoint(Inst->getIterator()); | ||
| 
     | 
||
| return this->getMatrix(MatrixVal, SI, Builder); | ||
| }; | ||
| 
     | 
||
| MatrixTy PhiM = GetMatrix(Inst); | ||
| 
     | 
||
| for (auto [IncomingV, IncomingB] : | ||
| llvm::zip_equal(Inst->incoming_values(), Inst->blocks())) { | ||
| MatrixTy OpM = GetMatrix(IncomingV); | ||
| 
     | 
||
| for (unsigned VI = 0, VE = PhiM.getNumVectors(); VI != VE; ++VI) { | ||
| PHINode *NewPHI = cast<PHINode>(PhiM.getVector(VI)); | ||
| NewPHI->addIncoming(OpM.getVector(VI), IncomingB); | ||
| } | ||
| } | ||
| 
     | 
||
| // finalizeLowering() may also insert instructions in some cases. The safe | ||
| // place for those is at the end of the initial block of PHIs. | ||
| auto IP = Inst->getInsertionPointAfterDef(); | ||
                
      
                  jroelofs marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| assert(IP.has_value() && | ||
| "expected to find a valid insertion point after the phi"); | ||
| Builder.SetInsertPoint(*IP); | ||
| return PhiM; | ||
| } | ||
| 
     | 
||
| /// Lower binary operators. | ||
| MatrixTy VisitBinaryOperator(BinaryOperator *Inst, const ShapeInfo &SI, | ||
| IRBuilder<> &Builder) { | ||
| 
          
            
          
           | 
    ||
      
      Oops, something went wrong.
        
    
  
      
      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.