This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] Check isSafeToSpeculativelyExecute before computeSpeculationCost
AbandonedPublic

Authored by bcl5980 on Jun 16 2022, 3:31 AM.

Details

Reviewers
nikic
fhahn
spatel
Summary

Fix #56038

Diff Detail

Event Timeline

bcl5980 created this revision.Jun 16 2022, 3:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 16 2022, 3:31 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
bcl5980 requested review of this revision.Jun 16 2022, 3:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 16 2022, 3:31 AM
nikic requested changes to this revision.Jun 16 2022, 4:02 AM

I believe the real problem here is that canTrap() only checks for division by zero, but not division of SIGNED_MIN by -1, which also traps. This leads to an inconsistency between canTrap() and isSafeToSpeculativelyExecute().

This revision now requires changes to proceed.Jun 16 2022, 4:02 AM

I believe the real problem here is that canTrap() only checks for division by zero, but not division of SIGNED_MIN by -1, which also traps. This leads to an inconsistency between canTrap() and isSafeToSpeculativelyExecute().

Is this what you want to do ?

--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -592,7 +592,10 @@ static bool canTrapImpl(const Constant *C,
   case Instruction::URem:
   case Instruction::SRem:
     // Div and rem can trap if the RHS is not known to be non-zero.
-    if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
+    if (!isa<ConstantInt>(CE->getOperand(1)) ||
+        CE->getOperand(1)->isNullValue() ||
+        (CE->getOperand(0)->isMinSignedValue() &&
+         CE->getOperand(1)->isAllOnesValue()))
       return true;
     return false;
   }
nikic added a comment.Jun 16 2022, 8:26 AM

@bcl5980 Conceptually, yes., but I believe your specific patch does not handle sdiv (i64 EXPR, i64 -1) correctly. We also only need this for the signed case.

bcl5980 abandoned this revision.Jun 16 2022, 7:10 PM

@bcl5980 Conceptually, yes., but I believe your specific patch does not handle sdiv (i64 EXPR, i64 -1) correctly. We also only need this for the signed case.

@nikic, Thanks for the explain. As I have little knowledge for this part can you help to fix the issue? I will abandon this change. Sorry for the disturbance.