- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.2k
Handle side-effects in impBoxPatternMatch #90496
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
      
      
            EgorBo
  merged 8 commits into
  dotnet:main
from
EgorBo:handle-sideeffects-in-impBoxPatternMatch
  
      
      
   
  Aug 14, 2023 
      
    
  
     Merged
                    Changes from 5 commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9613a71
              
                Be less conservative in impBoxPatternMatch
              
              
                EgorBo f791c02
              
                Fix asserts
              
              
                EgorBo 168e6ec
              
                Actually, it's no longer needed
              
              
                EgorBo 587eb27
              
                clean up
              
              
                EgorBo 9eefa4e
              
                Update importer.cpp
              
              
                EgorBo 9335402
              
                Address feedback
              
              
                EgorBo 313cef2
              
                wrong one
              
              
                EgorBo 4372334
              
                less conservative
              
              
                EgorBo 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 | 
|---|---|---|
|  | @@ -2869,54 +2869,17 @@ int Compiler::impBoxPatternMatch(CORINFO_RESOLVED_TOKEN* pResolvedToken, | |
| return 0; | ||
| } | ||
|  | ||
| GenTree* const treeToBox = impStackTop().val; | ||
| bool canOptimize = true; | ||
| GenTree* treeToNullcheck = nullptr; | ||
|  | ||
| // Can the thing being boxed cause a side effect? | ||
| if ((treeToBox->gtFlags & GTF_SIDE_EFFECT) != 0) | ||
| { | ||
| // Is this a side effect we can replicate cheaply? | ||
| if (((treeToBox->gtFlags & GTF_SIDE_EFFECT) == GTF_EXCEPT) && treeToBox->OperIs(GT_BLK, GT_IND)) | ||
| { | ||
| // If the only side effect comes from the dereference itself, yes. | ||
| GenTree* const addr = treeToBox->AsOp()->gtGetOp1(); | ||
|  | ||
| if ((addr->gtFlags & GTF_SIDE_EFFECT) != 0) | ||
| { | ||
| canOptimize = false; | ||
| } | ||
| else if (fgAddrCouldBeNull(addr)) | ||
| { | ||
| treeToNullcheck = addr; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| canOptimize = false; | ||
| } | ||
| } | ||
|  | ||
| if (canOptimize) | ||
| if ((opts == BoxPatterns::IsByRefLike) || | ||
| (info.compCompHnd->getBoxHelper(pResolvedToken->hClass) == CORINFO_HELP_BOX)) | ||
| { | ||
| if ((opts == BoxPatterns::IsByRefLike) || | ||
| info.compCompHnd->getBoxHelper(pResolvedToken->hClass) == CORINFO_HELP_BOX) | ||
| GenTree* op = impPopStack().val; | ||
| if ((op->gtFlags & GTF_SIDE_EFFECT) != 0) | ||
| { | ||
| JITDUMP("\n Importing BOX; BR_TRUE/FALSE as %sconstant\n", | ||
| treeToNullcheck == nullptr ? "" : "nullcheck+"); | ||
| impPopStack(); | ||
|  | ||
| GenTree* result = gtNewIconNode(1); | ||
|  | ||
| if (treeToNullcheck != nullptr) | ||
| { | ||
| GenTree* nullcheck = gtNewNullCheck(treeToNullcheck, compCurBB); | ||
| result = gtNewOperNode(GT_COMMA, TYP_INT, nullcheck, result); | ||
| } | ||
|  | ||
| impPushOnStack(result, typeInfo(TYP_INT)); | ||
| return 0; | ||
| impStoreTemp(lvaGrabTemp(true DEBUGARG("spill side effects")), op, CHECK_SPILL_ALL); | ||
| } | ||
| JITDUMP("\n Importing BOX; BR_TRUE/FALSE as constant\n") | ||
| impPushOnStack(gtNewTrue(), typeInfo(TYP_INT)); | ||
| return 0; | ||
| } | ||
| } | ||
| break; | ||
|  | @@ -2926,7 +2889,7 @@ int Compiler::impBoxPatternMatch(CORINFO_RESOLVED_TOKEN* pResolvedToken, | |
| { | ||
| // First, let's see if we can fold BOX+ISINST to just null if ISINST is known to return null | ||
| // for the given argument. Don't make inline observations for this case. | ||
| if ((opts == BoxPatterns::None) && ((impStackTop().val->gtFlags & GTF_SIDE_EFFECT) == 0) && | ||
| if ((opts == BoxPatterns::None) && | ||
| (info.compCompHnd->getBoxHelper(pResolvedToken->hClass) == CORINFO_HELP_BOX)) | ||
| { | ||
| CORINFO_RESOLVED_TOKEN isInstTok; | ||
|  | @@ -2935,7 +2898,12 @@ int Compiler::impBoxPatternMatch(CORINFO_RESOLVED_TOKEN* pResolvedToken, | |
| TypeCompareState::MustNot) | ||
| { | ||
| JITDUMP("\n Importing BOX; ISINST; as null\n"); | ||
| impPopStack(); | ||
|  | ||
| GenTree* op = impPopStack().val; | ||
| if ((op->gtFlags & GTF_SIDE_EFFECT) != 0) | ||
| { | ||
| impStoreTemp(lvaGrabTemp(true DEBUGARG("spill side effects")), op, CHECK_SPILL_ALL); | ||
| } | ||
| impPushOnStack(gtNewNull(), typeInfo(TYP_REF)); | ||
| return 1 + sizeof(mdToken); | ||
| } | ||
|  | @@ -2958,78 +2926,79 @@ int Compiler::impBoxPatternMatch(CORINFO_RESOLVED_TOKEN* pResolvedToken, | |
| return 1 + sizeof(mdToken); | ||
| } | ||
|  | ||
| if ((impStackTop().val->gtFlags & GTF_SIDE_EFFECT) == 0) | ||
| CorInfoHelpFunc foldAsHelper; | ||
| if (opts == BoxPatterns::IsByRefLike) | ||
| { | ||
| CorInfoHelpFunc foldAsHelper; | ||
| if (opts == BoxPatterns::IsByRefLike) | ||
| { | ||
| // Treat ByRefLike types as if they were regular boxing operations | ||
| // so they can be elided. | ||
| foldAsHelper = CORINFO_HELP_BOX; | ||
| } | ||
| else | ||
| { | ||
| foldAsHelper = info.compCompHnd->getBoxHelper(pResolvedToken->hClass); | ||
| } | ||
| // Treat ByRefLike types as if they were regular boxing operations | ||
| // so they can be elided. | ||
| foldAsHelper = CORINFO_HELP_BOX; | ||
| } | ||
| else | ||
| { | ||
| foldAsHelper = info.compCompHnd->getBoxHelper(pResolvedToken->hClass); | ||
| } | ||
|  | ||
| if (foldAsHelper == CORINFO_HELP_BOX) | ||
| { | ||
| CORINFO_RESOLVED_TOKEN isInstResolvedToken; | ||
| if (foldAsHelper == CORINFO_HELP_BOX) | ||
| { | ||
| CORINFO_RESOLVED_TOKEN isInstResolvedToken; | ||
|  | ||
| impResolveToken(codeAddr + 1, &isInstResolvedToken, CORINFO_TOKENKIND_Casting); | ||
| impResolveToken(codeAddr + 1, &isInstResolvedToken, CORINFO_TOKENKIND_Casting); | ||
|  | ||
| TypeCompareState castResult = | ||
| info.compCompHnd->compareTypesForCast(pResolvedToken->hClass, | ||
| isInstResolvedToken.hClass); | ||
| if (castResult != TypeCompareState::May) | ||
| { | ||
| JITDUMP("\n Importing BOX; ISINST; BR_TRUE/FALSE as constant\n"); | ||
| impPopStack(); | ||
| TypeCompareState castResult = | ||
| info.compCompHnd->compareTypesForCast(pResolvedToken->hClass, | ||
| isInstResolvedToken.hClass); | ||
|  | ||
| impPushOnStack(gtNewIconNode((castResult == TypeCompareState::Must) ? 1 : 0), | ||
| typeInfo(TYP_INT)); | ||
| if (castResult != TypeCompareState::May) | ||
| { | ||
| JITDUMP("\n Importing BOX; ISINST; BR_TRUE/FALSE as constant\n"); | ||
|  | ||
| // Skip the next isinst instruction | ||
| return 1 + sizeof(mdToken); | ||
| GenTree* op = impPopStack().val; | ||
| if ((op->gtFlags & GTF_SIDE_EFFECT) != 0) | ||
| { | ||
| impStoreTemp(lvaGrabTemp(true DEBUGARG("spill side effects")), op, | ||
| CHECK_SPILL_ALL); | ||
| } | ||
| impPushOnStack(gtNewIconNode((castResult == TypeCompareState::Must) ? 1 : 0), | ||
| typeInfo(TYP_INT)); | ||
| return 1 + sizeof(mdToken); | ||
| } | ||
| else if (foldAsHelper == CORINFO_HELP_BOX_NULLABLE) | ||
| { | ||
| // For nullable we're going to fold it to "ldfld hasValue + brtrue/brfalse" or | ||
| // "ldc.i4.0 + brtrue/brfalse" in case if the underlying type is not castable to | ||
| // the target type. | ||
| CORINFO_RESOLVED_TOKEN isInstResolvedToken; | ||
| impResolveToken(codeAddr + 1, &isInstResolvedToken, CORINFO_TOKENKIND_Casting); | ||
| } | ||
| else if ((foldAsHelper == CORINFO_HELP_BOX_NULLABLE) && | ||
| ((impStackTop().val->gtFlags & GTF_SIDE_EFFECT) == 0)) | ||
| 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. Didn't do it for nullable due to empty diffs for it (would have to add tests as the logic is a bit different). | ||
| { | ||
| // For nullable we're going to fold it to "ldfld hasValue + brtrue/brfalse" or | ||
| // "ldc.i4.0 + brtrue/brfalse" in case if the underlying type is not castable to | ||
| // the target type. | ||
| CORINFO_RESOLVED_TOKEN isInstResolvedToken; | ||
| impResolveToken(codeAddr + 1, &isInstResolvedToken, CORINFO_TOKENKIND_Casting); | ||
|  | ||
| CORINFO_CLASS_HANDLE nullableCls = pResolvedToken->hClass; | ||
| CORINFO_CLASS_HANDLE underlyingCls = info.compCompHnd->getTypeForBox(nullableCls); | ||
| CORINFO_CLASS_HANDLE nullableCls = pResolvedToken->hClass; | ||
| CORINFO_CLASS_HANDLE underlyingCls = info.compCompHnd->getTypeForBox(nullableCls); | ||
|  | ||
| TypeCompareState castResult = | ||
| info.compCompHnd->compareTypesForCast(underlyingCls, | ||
| isInstResolvedToken.hClass); | ||
| TypeCompareState castResult = | ||
| info.compCompHnd->compareTypesForCast(underlyingCls, isInstResolvedToken.hClass); | ||
|  | ||
| if (castResult == TypeCompareState::Must) | ||
| { | ||
| GenTree* objToBox = impPopStack().val; | ||
| if (castResult == TypeCompareState::Must) | ||
| { | ||
| GenTree* objToBox = impPopStack().val; | ||
|  | ||
| // Spill struct to get its address (to access hasValue field) | ||
| // TODO-Bug?: verify if flags matter here | ||
| GenTreeFlags indirFlags = GTF_EMPTY; | ||
| objToBox = impGetNodeAddr(objToBox, CHECK_SPILL_ALL, &indirFlags); | ||
| // Spill struct to get its address (to access hasValue field) | ||
| // TODO-Bug?: verify if flags matter here | ||
| GenTreeFlags indirFlags = GTF_EMPTY; | ||
| objToBox = impGetNodeAddr(objToBox, CHECK_SPILL_ALL, &indirFlags); | ||
|  | ||
| static_assert_no_msg(OFFSETOF__CORINFO_NullableOfT__hasValue == 0); | ||
| impPushOnStack(gtNewIndir(TYP_BOOL, objToBox), typeInfo(TYP_INT)); | ||
| static_assert_no_msg(OFFSETOF__CORINFO_NullableOfT__hasValue == 0); | ||
| impPushOnStack(gtNewIndir(TYP_BOOL, objToBox), typeInfo(TYP_INT)); | ||
|  | ||
| JITDUMP("\n Importing BOX; ISINST; BR_TRUE/FALSE as nullableVT.hasValue\n"); | ||
| return 1 + sizeof(mdToken); | ||
| } | ||
| else if (castResult == TypeCompareState::MustNot) | ||
| { | ||
| impPopStack(); | ||
| impPushOnStack(gtNewIconNode(0), typeInfo(TYP_INT)); | ||
| JITDUMP("\n Importing BOX; ISINST; BR_TRUE/FALSE as constant (false)\n"); | ||
| return 1 + sizeof(mdToken); | ||
| } | ||
| JITDUMP("\n Importing BOX; ISINST; BR_TRUE/FALSE as nullableVT.hasValue\n"); | ||
| return 1 + sizeof(mdToken); | ||
| } | ||
| else if (castResult == TypeCompareState::MustNot) | ||
| { | ||
| impPopStack(); | ||
| impPushOnStack(gtNewIconNode(0), typeInfo(TYP_INT)); | ||
| JITDUMP("\n Importing BOX; ISINST; BR_TRUE/FALSE as constant (false)\n"); | ||
| return 1 + sizeof(mdToken); | ||
| } | ||
| } | ||
| } | ||
|  | ||
  
    
      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
    
  
  
    
              
  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.
This is a bit of an unusual way of doing this in the importer, use
impSpillSideEffectinstead?Also we already do this from the caller for
BoxPatterns::IsByRefLike. Should it be unified?Uh oh!
There was an error while loading. Please reload this page.
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.
Ah, thanks for the hint. addressed.
Looks like that one has to be always done regardless wether we match a pattern or not while here we only do this when we match it. Presumably the byref case is rare so it's ok to call it twice for simplicity?
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.
I don't think so (note that the comment is misleading -- we
BADCODEtoday on unmatched patterns, we do not generate code to throwInvalidProgramExceptionat runtime). But I'm ok with leaving it to avoid further risk and potentially cleaning it up in .NET 9, assuming you want this in .NET 8.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.
Ah, I see, yeah let's clean it up in .NET 9.0, it has a potential to slightly refactor the whole thing