- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[msan] Improve packed multiply-add instrumentation #152941
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 22 commits
      Commits
    
    
            Show all changes
          
          
            26 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      538cd21
              
                [msan] Improve packed multiply-add instrumentation
              
              
                thurstond 2ccc07f
              
                Update comment
              
              
                thurstond d952804
              
                Only allow elements that are exactly zero to clear shadow
              
              
                thurstond f2d3972
              
                Fix MMX instrumentation
              
              
                thurstond 13b3d88
              
                Add comment on types
              
              
                thurstond 7c6bafb
              
                Add return types
              
              
                thurstond 771360f
              
                Add support for packed multiply add on bytes and words
              
              
                thurstond d828067
              
                Make ReductionFactor parameterizable
              
              
                thurstond ff47717
              
                Generalize
              
              
                thurstond 66a1c7d
              
                More comments, assertions. Fix cast.
              
              
                thurstond 59ee901
              
                Add TODO for BF16
              
              
                thurstond ca4d4e3
              
                Update comment
              
              
                thurstond ef6d008
              
                Fix parameter order for 3-operand forms
              
              
                thurstond 49ed5ef
              
                clang-format
              
              
                thurstond fffedf3
              
                Move assertion earlier
              
              
                thurstond e63de12
              
                Revert "clang-format"
              
              
                thurstond f6cdda2
              
                Format
              
              
                thurstond fd4e639
              
                Refactor into horizontalReduce
              
              
                thurstond 4396ca7
              
                Merge remote-tracking branch 'upstream/main' into msan_pmadd_update
              
              
                thurstond a321778
              
                Remove VNNI handlers (will move to separate pull request)
              
              
                thurstond 6fbf1c4
              
                Format
              
              
                thurstond eea5186
              
                Undo AVX512 additions (will move to separate pull request)
              
              
                thurstond b7fe847
              
                Address: Florian's feedback, fix S1/S2 bug, don't change ReturnType
              
              
                thurstond 849b002
              
                Merge remote-tracking branch 'upstream/main' into msan_pmadd_update
              
              
                thurstond df06567
              
                Remove support for 3-operand instructions (will move to separate pull
              
              
                thurstond 8980c0f
              
                Format
              
              
                thurstond 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 | 
|---|---|---|
| 
          
            
          
           | 
    @@ -3641,9 +3641,10 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| setOriginForNaryOp(I); | ||
| } | ||
| 
     | 
||
| // Get an MMX-sized vector type. | ||
| Type *getMMXVectorTy(unsigned EltSizeInBits) { | ||
| const unsigned X86_MMXSizeInBits = 64; | ||
| // Get an MMX-sized (64-bit) vector type, or optionally, other sized | ||
| // vectors. | ||
| Type *getMMXVectorTy(unsigned EltSizeInBits, | ||
| unsigned X86_MMXSizeInBits = 64) { | ||
| assert(EltSizeInBits != 0 && (X86_MMXSizeInBits % EltSizeInBits) == 0 && | ||
| "Illegal MMX vector element size"); | ||
| return FixedVectorType::get(IntegerType::get(*MS.C, EltSizeInBits), | ||
| 
          
            
          
           | 
    @@ -3843,20 +3844,95 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| setOriginForNaryOp(I); | ||
| } | ||
| 
     | 
||
| // Instrument multiply-add intrinsic. | ||
| void handleVectorPmaddIntrinsic(IntrinsicInst &I, | ||
| unsigned MMXEltSizeInBits = 0) { | ||
| Type *ResTy = | ||
| MMXEltSizeInBits ? getMMXVectorTy(MMXEltSizeInBits * 2) : I.getType(); | ||
| // Instrument multiply-add intrinsics. | ||
| // | ||
| // e.g., Two operands: | ||
| // <4 x i32> @llvm.x86.sse2.pmadd.wd(<8 x i16> %a, <8 x i16> %b) | ||
| // <1 x i64> @llvm.x86.mmx.pmadd.wd(<1 x i64> %a, <1 x i64> %b) | ||
| // | ||
| // Three operands: | ||
| // <4 x i32> @llvm.x86.avx512.vpdpbusd.128 | ||
| // (<4 x i32> %s, <4 x i32> %a, <4 x i32> %b) | ||
| // (the result of multiply-add'ing %a and %b is accumulated with %s) | ||
| void handleVectorPmaddIntrinsic(IntrinsicInst &I, unsigned ReductionFactor, | ||
| unsigned EltSizeInBits = 0) { | ||
| IRBuilder<> IRB(&I); | ||
| auto *Shadow0 = getShadow(&I, 0); | ||
| auto *Shadow1 = getShadow(&I, 1); | ||
| Value *S = IRB.CreateOr(Shadow0, Shadow1); | ||
| S = IRB.CreateBitCast(S, ResTy); | ||
| S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)), | ||
| ResTy); | ||
| S = IRB.CreateBitCast(S, getShadowTy(&I)); | ||
| setShadow(&I, S); | ||
| 
     | 
||
| FixedVectorType *ReturnType = cast<FixedVectorType>(I.getType()); | ||
| assert(isa<FixedVectorType>(ReturnType)); | ||
| 
     | 
||
| assert(I.arg_size() == 2 || I.arg_size() == 3); | ||
| FixedVectorType *ParamType = | ||
| cast<FixedVectorType>(I.getArgOperand(0)->getType()); | ||
| assert(ParamType == I.getArgOperand(1)->getType()); | ||
| 
     | 
||
| Value *V1; | ||
| Value *V2; | ||
| 
     | 
||
| if (I.arg_size() == 3) { | ||
| assert(ParamType == ReturnType); | ||
| assert(ParamType == I.getArgOperand(2)->getType()); | ||
| 
     | 
||
| V1 = I.getOperand(1); | ||
| V2 = I.getOperand(2); | ||
| } else { | ||
| V1 = I.getOperand(0); | ||
| V2 = I.getOperand(1); | ||
| } | ||
| 
     | 
||
| assert(ParamType->getPrimitiveSizeInBits() == | ||
| ReturnType->getPrimitiveSizeInBits()); | ||
| 
     | 
||
| // Step 1: instrument multiplication of corresponding vector elements | ||
| Value *S1 = getShadow(&I, 0); | ||
| Value *S2 = getShadow(&I, 1); | ||
| 
     | 
||
| if (EltSizeInBits) { | ||
| if (I.arg_size() != 3) | ||
| ReturnType = cast<FixedVectorType>( | ||
                
       | 
||
| getMMXVectorTy(EltSizeInBits * ReductionFactor, | ||
| ReturnType->getPrimitiveSizeInBits())); | ||
| 
     | 
||
| ParamType = cast<FixedVectorType>( | ||
| getMMXVectorTy(EltSizeInBits, ParamType->getPrimitiveSizeInBits())); | ||
| 
     | 
||
| V1 = IRB.CreateBitCast(V1, ParamType); | ||
| V2 = IRB.CreateBitCast(V2, ParamType); | ||
| 
     | 
||
| S1 = IRB.CreateBitCast(S1, getShadowTy(ParamType)); | ||
| S2 = IRB.CreateBitCast(S2, getShadowTy(ParamType)); | ||
| } | ||
| 
     | 
||
| assert(ParamType->getNumElements() == | ||
| ReturnType->getNumElements() * ReductionFactor); | ||
| 
     | 
||
| Value *S1S2 = IRB.CreateOr(S1, S2); | ||
| 
     | 
||
| // Multiplying an uninitialized / element by zero results in an initialized | ||
| // element. | ||
| Value *Zero = Constant::getNullValue(V1->getType()); | ||
| Value *V1NotZero = IRB.CreateICmpNE(V1, Zero); | ||
| Value *V2NotZero = IRB.CreateICmpNE(V2, Zero); | ||
| Value *V1AndV2NotZero = IRB.CreateAnd(V1NotZero, V2NotZero); | ||
| 
     | 
||
| // After multiplying e.g., <8 x i16> %a, <8 x i16> %b, we should have | ||
| // <8 x i32> %ab, but we cheated and ended up with <8 x i16>. | ||
| S1S2 = IRB.CreateAnd(S1S2, IRB.CreateSExt(V1AndV2NotZero, S1S2->getType())); | ||
| 
     | 
||
| // Step 2: instrument horizontal add | ||
| // e.g., collapse <8 x i16> into <4 x i16> (reduction factor == 2) | ||
| // <16 x i8> into <4 x i8> (reduction factor == 4) | ||
| Value *OrShadow = horizontalReduce(I, ReductionFactor, S1S2, nullptr); | ||
| 
     | 
||
| // Extend to <4 x i32>. | ||
| // For MMX, cast it back to <1 x i64>. | ||
| OrShadow = CreateShadowCast(IRB, OrShadow, getShadowTy(&I)); | ||
| 
     | 
||
| // Accumulate | ||
| if (I.arg_size() == 3) | ||
| OrShadow = IRB.CreateOr(OrShadow, getShadow(&I, 0)); | ||
| 
     | 
||
| setShadow(&I, OrShadow); | ||
| setOriginForNaryOp(I); | ||
| } | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -5391,19 +5467,28 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| handleVectorSadIntrinsic(I); | ||
| break; | ||
| 
     | 
||
| // Multiply and Add Packed Words | ||
| // < 4 x i32> @llvm.x86.sse2.pmadd.wd(<8 x i16>, <8 x i16>) | ||
| // < 8 x i32> @llvm.x86.avx2.pmadd.wd(<16 x i16>, <16 x i16>) | ||
| 
     | 
||
| // Multiply and Add Packed Signed and Unsigned Bytes | ||
| // < 8 x i16> @llvm.x86.ssse3.pmadd.ub.sw.128(<16 x i8>, <16 x i8>) | ||
| // <16 x i16> @llvm.x86.avx2.pmadd.ub.sw(<32 x i8>, <32 x i8>) | ||
| case Intrinsic::x86_sse2_pmadd_wd: | ||
| case Intrinsic::x86_avx2_pmadd_wd: | ||
| case Intrinsic::x86_ssse3_pmadd_ub_sw_128: | ||
| case Intrinsic::x86_avx2_pmadd_ub_sw: | ||
| handleVectorPmaddIntrinsic(I); | ||
| handleVectorPmaddIntrinsic(I, /*ReductionFactor=*/2); | ||
| break; | ||
| 
     | 
||
| // <1 x i64> @llvm.x86.ssse3.pmadd.ub.sw(<1 x i64>, <1 x i64>) | ||
| case Intrinsic::x86_ssse3_pmadd_ub_sw: | ||
| handleVectorPmaddIntrinsic(I, 8); | ||
| handleVectorPmaddIntrinsic(I, /*ReductionFactor=*/2, /*EltSize=*/8); | ||
| break; | ||
| 
     | 
||
| // <1 x i64> @llvm.x86.mmx.pmadd.wd(<1 x i64>, <1 x i64>) | ||
| case Intrinsic::x86_mmx_pmadd_wd: | ||
| handleVectorPmaddIntrinsic(I, 16); | ||
| handleVectorPmaddIntrinsic(I, /*ReductionFactor=*/2, /*EltSize=*/16); | ||
| break; | ||
| 
     | 
||
| case Intrinsic::x86_sse_cmp_ss: | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              
      
      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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So for
I.arg_size() == 3S1 is not the shadow of V1. Is that intentional? If it is, that is quite confusingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, that was a bug.
I've removed the 3-variable case and will put it in a separate pull request, since it the code as-is didn't have test coverage.
I also renamed the variables to Va/Vb/Sa/Sb and assigned them closer together, to avoid repeating the bug in the future pull request.