fix: do not evaluate left shifts with huge shift amounts This PR adds the option `shiftLeft.threshold` (default: 65536). `whnf`, `simp`, and `grind` no longer evaluate a left shift of literals when the shift amount is above this threshold, and report a warning instead, as they already do for exponents above `exponentiation.threshold`. Previously, a shift amount of 2^32 or more aborted the Lean process with `INTERNAL PANIC: Nat.shiftl exponent is too big`, e.g., in `example : (1 <<< 4294967296 : Nat) = 0 := by simp`. `cbv` and the compiler use the same bound as a constant. The runtime implementation of `Nat.shiftLeft` panics when the shifted value is not `0` and the shift amount does not fit in 32 bits, and several procedures evaluated `<<<` on `Nat`, `Int`, `Fin`, and `BitVec` literals without checking the shift amount. The new functions `isSafeShiftLeft` and `checkShiftLeft` accept a shift when the shifted value is `0`, or the shift amount is at most the threshold and fits in 32 bits. They are now used by `Meta.reduceNat?`, the `Nat`, `Fin`, and `BitVec` shift simprocs, the `grind` propagators for `Nat` and `BitVec` shifts, `Sym` ground evaluation (new field `EvalStepConfig.maxShiftLeft`), and the LCNF constant folder (`natShiftLeftThreshold`). Closes #15193 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/src/Lean/Compiler/LCNF/Simp/ConstantFold.lean b/src/Lean/Compiler/LCNF/Simp/ConstantFold.lean index 77538a5..5ce43af 100644 --- a/src/Lean/Compiler/LCNF/Simp/ConstantFold.lean +++ b/src/Lean/Compiler/LCNF/Simp/ConstantFold.lean
@@ -526,6 +526,22 @@ return .some (.lit (.nat (value₁ ^ value₂))) else return none +-- TODO: add option for controlling the limit +def natShiftLeftThreshold := 65536 + +/-- +Folds `Nat.shiftLeft` on literals when the shift amount is below `natShiftLeftThreshold`. +The threshold must fit in 32 bits: the runtime implementation of `Nat.shiftLeft` aborts the process +on larger shift amounts. +-/ +def foldNatShiftLeft (args : Array (Arg .pure)) : FolderM (Option (LetValue .pure)) := do + let #[.fvar fvarId₁, .fvar fvarId₂] := args | return none + let some value₁ ← getNatLit fvarId₁ | return none + let some value₂ ← getNatLit fvarId₂ | return none + if value₂ < natShiftLeftThreshold then + return .some (.lit (.nat (value₁ <<< value₂))) + else + return none /-- Folder for ofNat operations on fixed-sized integer types. @@ -616,7 +632,7 @@ (``UInt64.mod, Folder.first #[Folder.mkBinary UInt64.mod, Folder.rightNeutral (0 : UInt64) (· % ·) @UInt64.mod_zero, Folder.leftAnnihilator (0 : UInt64) 0 (· % ·) @UInt64.zero_mod, Folder.rightAnnihilator (1 : UInt64) 0 (· % ·) @UInt64.mod_one, Folder.sameArg (0 : UInt64) (· % ·) @UInt64.mod_self]), (``USize.mod, Folder.first #[Folder.mkBinaryUSize UInt64.mod UInt32.mod, Folder.rightNeutralUSize 0 0, Folder.leftAnnihilatorUSize 0 0 0 0, Folder.rightAnnihilatorUSize 1 1 0 0, Folder.sameArgUSize 0 0]), - (``Nat.shiftLeft, Folder.first #[Folder.mkBinary Nat.shiftLeft, Folder.rightNeutral 0 Nat.shiftLeft (by intros; rfl), Folder.leftAnnihilator 0 0 Nat.shiftLeft Nat.zero_shiftLeft]), + (``Nat.shiftLeft, Folder.first #[foldNatShiftLeft, Folder.rightNeutral 0 Nat.shiftLeft (by intros; rfl), Folder.leftAnnihilator 0 0 Nat.shiftLeft Nat.zero_shiftLeft]), (``UInt8.shiftLeft, Folder.first #[Folder.mkBinary UInt8.shiftLeft, Folder.rightNeutral 0 UInt8.shiftLeft @UInt8.shiftLeft_zero, Folder.leftAnnihilator (0 : UInt8) 0 UInt8.shiftLeft @UInt8.zero_shiftLeft]), (``UInt16.shiftLeft, Folder.first #[Folder.mkBinary UInt16.shiftLeft, Folder.rightNeutral 0 UInt16.shiftLeft @UInt16.shiftLeft_zero, Folder.leftAnnihilator (0 : UInt16) 0 UInt16.shiftLeft @UInt16.zero_shiftLeft]), (``UInt32.shiftLeft, Folder.first #[Folder.mkBinary UInt32.shiftLeft, Folder.rightNeutral 0 UInt32.shiftLeft @UInt32.shiftLeft_zero, Folder.leftAnnihilator (0 : UInt32) 0 UInt32.shiftLeft @UInt32.zero_shiftLeft]),
diff --git a/src/Lean/Meta/Sym/DSimp/EvalGround.lean b/src/Lean/Meta/Sym/DSimp/EvalGround.lean index 3d48b6d..321b817 100644 --- a/src/Lean/Meta/Sym/DSimp/EvalGround.lean +++ b/src/Lean/Meta/Sym/DSimp/EvalGround.lean
@@ -8,6 +8,7 @@ public import Lean.Meta.Sym.DSimp.DSimpM import Lean.Meta.Sym.LitValues import Lean.Meta.Offset +import Lean.Util.SafeExponentiation namespace Lean.Meta.Sym.DSimp /-! @@ -243,7 +244,29 @@ abbrev shift [ShiftLeft α] [ShiftRight α] (left : Bool) (a b : α) : α := if left then a <<< b else a >>> b -def evalShift (left : Bool) (α β : Expr) (a b : Expr) : DSimpM Result := +/-- +Returns `false` if `a : α` and `b : β` are literals such that `a <<< b` is not safe to evaluate +(see `isSafeShiftLeft`). Only shift amounts that are not bounded by a machine word size are checked. +-/ +def isSafeShiftLeftLits (maxShiftLeft : Nat) (α β a b : Expr) : Bool := Id.run do + let amount? : Option Nat := match_expr β with + | Nat => getNatValue? b + | Fin _ => (·.val.val) <$> getFinValue? b + | BitVec _ => (·.val.toNat) <$> getBitVecValue? b + | _ => none + let some amount := amount? | return true + let value? : Option Nat := match_expr α with + | Nat => getNatValue? a + | Int => Int.natAbs <$> getIntValue? a + | Fin _ => (·.val.val) <$> getFinValue? a + | BitVec _ => (·.val.toNat) <$> getBitVecValue? a + | _ => none + let some value := value? | return true + return isSafeShiftLeft value amount maxShiftLeft + +def evalShift (left : Bool) (maxShiftLeft : Nat) (α β : Expr) (a b : Expr) : DSimpM Result := do + if left then + unless isSafeShiftLeftLits maxShiftLeft α β a b do return .rfl if isSameExpr α β then match_expr α with | Nat => evalBinNat (shift left) a b @@ -371,6 +394,13 @@ let e ← share <| toExpr <| op a.val i return .step e (done := true) +def evalBitVecShiftLeft (maxShiftLeft : Nat) (a i : Expr) : DSimpM Result := do + let some a := getBitVecValue? a | return .rfl + let some i := getNatValue? i | return .rfl + unless isSafeShiftLeft a.val.toNat i maxShiftLeft do return .rfl + let e ← share <| toExpr <| a.val.shiftLeft i + return .step e (done := true) + abbrev evalBitVecNatBool (op : {n : Nat} → BitVec n → Nat → Bool) (a i : Expr) : DSimpM Result := do let some a := getBitVecValue? a | return .rfl let some i := getNatValue? i | return .rfl @@ -395,9 +425,10 @@ let e ← share <| toExpr <| BitVec.ofNat m a.val.toNat return .step e (done := true) -def evalBitVecShiftLeftZeroExtend (a m : Expr) : DSimpM Result := do +def evalBitVecShiftLeftZeroExtend (maxShiftLeft : Nat) (a m : Expr) : DSimpM Result := do let some a := getBitVecValue? a | return .rfl let some m := getNatValue? m | return .rfl + unless isSafeShiftLeft a.val.toNat m maxShiftLeft do return .rfl let e ← share <| toExpr <|a.val.shiftLeftZeroExtend m return .step e (done := true) @@ -501,6 +532,8 @@ public structure EvalStepConfig where maxExponent := 255 + /-- Maximum left shift amount evaluated by `evalGround`. -/ + maxShiftLeft := 65536 /-- Simplification procedure that evaluates ground terms of builtin types. @@ -524,8 +557,8 @@ | HAnd.hAnd α _ _ _ a b => evalAnd α a b | HXor.hXor α _ _ _ a b => evalXOr α a b | HOr.hOr α _ _ _ a b => evalOr α a b - | HShiftLeft.hShiftLeft α β _ _ a b => evalShift (left := true) α β a b - | HShiftRight.hShiftRight α β _ _ a b => evalShift (left := false) α β a b + | HShiftLeft.hShiftLeft α β _ _ a b => evalShift (left := true) config.maxShiftLeft α β a b + | HShiftRight.hShiftRight α β _ _ a b => evalShift (left := false) config.maxShiftLeft α β a b | Inv.inv α _ a => evalInv α a | Neg.neg α _ a => return skipIfUnchanged e (← evalNeg α a) | Complement.complement α _ a => evalComplement α a @@ -553,7 +586,7 @@ | BitVec _ => evalBitVecNatBool BitVec.getLsbD a i | _ => return .rfl | BitVec.append _ _ a b => evalBitVecAppend a b - | BitVec.shiftLeft _ a i => evalBitVecNatBitVec BitVec.shiftLeft a i + | BitVec.shiftLeft _ a i => evalBitVecShiftLeft config.maxShiftLeft a i | BitVec.ushiftRight _ a i => evalBitVecNatBitVec BitVec.ushiftRight a i | BitVec.sshiftRight _ a i => evalBitVecNatBitVec BitVec.sshiftRight a i | BitVec.sshiftRight' _ _ a b => evalBinBitVec' BitVec.sshiftRight' a b @@ -580,7 +613,7 @@ | BitVec.signExtend _ v a => evalBitVecExtend BitVec.signExtend v a | BitVec.setWidth' _ w _ a => evalBitVecExtend BitVec.setWidth w a | BitVec.cast _ m _ a => evalBitVecCast m a - | BitVec.shiftLeftZeroExtend _ a m => evalBitVecShiftLeftZeroExtend a m + | BitVec.shiftLeftZeroExtend _ a m => evalBitVecShiftLeftZeroExtend config.maxShiftLeft a m | BitVec.extractLsb' _ start len a => evalBitVecExtractLsb' start len a | BitVec.replicate _ i a => evalBitVecReplicate i a | BitVec.allOnes n => evalBitVecAllOnes n
diff --git a/src/Lean/Meta/Sym/Simp/EvalGround.lean b/src/Lean/Meta/Sym/Simp/EvalGround.lean index ded9149..d7c3093 100644 --- a/src/Lean/Meta/Sym/Simp/EvalGround.lean +++ b/src/Lean/Meta/Sym/Simp/EvalGround.lean
@@ -10,6 +10,7 @@ import Lean.Meta.Sym.LitValues import Lean.Meta.StringLitProof import Lean.Meta.Offset +import Lean.Util.SafeExponentiation import Init.Data.UInt.Lemmas namespace Lean.Meta.Sym.Simp @@ -96,6 +97,8 @@ public structure EvalStepConfig where /-- Maximum exponent evaluated by `evalGround`. -/ maxExponent := 255 + /-- Maximum left shift amount evaluated by `evalGround`. -/ + maxShiftLeft := 65536 /-- When `true` (default: `true`), bit-vector literals are represented with `BitVec.ofNat`; otherwise the `OfNat.ofNat` form is used (the representation used by `grind`). @@ -311,7 +314,29 @@ abbrev shift [ShiftLeft α] [ShiftRight α] (left : Bool) (a b : α) : α := if left then a <<< b else a >>> b -def evalShift (left : Bool) (α β : Expr) (a b : Expr) : EvalM Result := +/-- +Returns `false` if `a : α` and `b : β` are literals such that `a <<< b` is not safe to evaluate +(see `isSafeShiftLeft`). Only shift amounts that are not bounded by a machine word size are checked. +-/ +def isSafeShiftLeftLits (maxShiftLeft : Nat) (α β a b : Expr) : Bool := Id.run do + let amount? : Option Nat := match_expr β with + | Nat => getNatValue? b + | Fin _ => (·.val.val) <$> getFinValue? b + | BitVec _ => (·.val.toNat) <$> getBitVecValue? b + | _ => none + let some amount := amount? | return true + let value? : Option Nat := match_expr α with + | Nat => getNatValue? a + | Int => Int.natAbs <$> getIntValue? a + | Fin _ => (·.val.val) <$> getFinValue? a + | BitVec _ => (·.val.toNat) <$> getBitVecValue? a + | _ => none + let some value := value? | return true + return isSafeShiftLeft value amount maxShiftLeft + +def evalShift (left : Bool) (α β : Expr) (a b : Expr) : EvalM Result := do + if left then + unless isSafeShiftLeftLits (← read).maxShiftLeft α β a b do return .rfl if isSameExpr α β then match_expr α with | Nat => evalBinNat (shift left) a b @@ -686,6 +711,13 @@ let e ← mkBitVecLit (op a.val i) return .step e (mkApp2 (mkConst ``Eq.refl [1]) αExpr e) (done := true) +def evalBitVecShiftLeft (αExpr : Expr) (a i : Expr) : EvalM Result := do + let some a := getBitVecValue? a | return .rfl + let some i := getNatValue? i | return .rfl + unless isSafeShiftLeft a.val.toNat i (← read).maxShiftLeft do return .rfl + let e ← mkBitVecLit (a.val.shiftLeft i) + return .step e (mkApp2 (mkConst ``Eq.refl [1]) αExpr e) (done := true) + def evalBitVecAppend (a b : Expr) : EvalM Result := do let some a := getBitVecValue? a | return .rfl let some b := getBitVecValue? b | return .rfl @@ -720,6 +752,7 @@ def evalBitVecShiftLeftZeroExtend (a m : Expr) : EvalM Result := do let some a := getBitVecValue? a | return .rfl let some m ← evalNat m |>.run | return .rfl + unless isSafeShiftLeft a.val.toNat m (← read).maxShiftLeft do return .rfl let e ← mkBitVecLit (a.val.shiftLeftZeroExtend m) return .step e (mkRflBitVec e (a.n + m)) (done := true) @@ -911,7 +944,7 @@ | BitVec _ => evalBitVecNatBool BitVec.getLsbD a i | _ => return .rfl | BitVec.append _ _ a b => evalBitVecAppend a b - | BitVec.shiftLeft w a i => evalBitVecNatBitVec (mkBitVecType w) BitVec.shiftLeft a i + | BitVec.shiftLeft w a i => evalBitVecShiftLeft (mkBitVecType w) a i | BitVec.ushiftRight w a i => evalBitVecNatBitVec (mkBitVecType w) BitVec.ushiftRight a i | BitVec.sshiftRight w a i => evalBitVecNatBitVec (mkBitVecType w) BitVec.sshiftRight a i | BitVec.sshiftRight' n _ a b => evalBinBitVec' BitVec.sshiftRight' (mkBitVecType n) a b
diff --git a/src/Lean/Meta/Tactic/Grind/Arith/Propagate.lean b/src/Lean/Meta/Tactic/Grind/Arith/Propagate.lean index d13bed6..6c97ba0 100644 --- a/src/Lean/Meta/Tactic/Grind/Arith/Propagate.lean +++ b/src/Lean/Meta/Tactic/Grind/Arith/Propagate.lean
@@ -10,6 +10,7 @@ import Lean.Meta.Tactic.Grind.Arith.CommRing.NonCommRingM import Lean.Meta.Tactic.Grind.Arith.CommRing.NonCommSemiringM public import Lean.Meta.Tactic.Grind.PropagatorAttr +import Lean.Util.SafeExponentiation public section namespace Lean.Meta.Grind.Arith @@ -39,7 +40,10 @@ `mask = 15`. -/ -def propagateNatBinOp (declName : Name) (congrThmName : Name) (op : Nat → Nat → Nat) (e : Expr) : GoalM Unit := do +/-- +`op k₁ k₂` returns `none` when the operation should not be evaluated at the given literals. +-/ +def propagateNatBinOpM (declName : Name) (congrThmName : Name) (op : Nat → Nat → GoalM (Option Nat)) (e : Expr) : GoalM Unit := do let arity := 6 unless e.isAppOfArity declName arity do return () unless e.getArg! 0 |>.isConstOf ``Nat do return () @@ -50,17 +54,22 @@ let b := e.getArg! (arity - 1) arity let bRoot ← getRoot b let some k₂ ← getNatValue? bRoot | return () - let k := op k₁ k₂ + let some k ← op k₁ k₂ | return () let r ← shareCommon (mkNatLit k) internalize r 0 let h := mkApp8 (mkConst congrThmName) a b aRoot bRoot r (← mkEqProof a aRoot) (← mkEqProof b bRoot) eagerReflBoolTrue pushEq e r h +def propagateNatBinOp (declName : Name) (congrThmName : Name) (op : Nat → Nat → Nat) (e : Expr) : GoalM Unit := + propagateNatBinOpM declName congrThmName (fun k₁ k₂ => return some (op k₁ k₂)) e + builtin_grind_propagator propagateNatAnd ↑HAnd.hAnd := propagateNatBinOp ``HAnd.hAnd ``Grind.Nat.and_congr (· &&& ·) builtin_grind_propagator propagateNatOr ↑HOr.hOr := propagateNatBinOp ``HOr.hOr ``Grind.Nat.or_congr (· ||| ·) builtin_grind_propagator propagateNatXOr ↑HXor.hXor := propagateNatBinOp ``HXor.hXor ``Grind.Nat.xor_congr (· ^^^ ·) builtin_grind_propagator propagateNatShiftLeft ↑HShiftLeft.hShiftLeft := - propagateNatBinOp ``HShiftLeft.hShiftLeft ``Grind.Nat.shiftLeft_congr (· <<< ·) + propagateNatBinOpM ``HShiftLeft.hShiftLeft ``Grind.Nat.shiftLeft_congr fun k₁ k₂ => do + unless (← checkShiftLeft k₁ k₂) do return none + return some (k₁ <<< k₂) builtin_grind_propagator propagateNatShiftRight ↑HShiftRight.hShiftRight := propagateNatBinOp ``HShiftRight.hShiftRight ``Grind.Nat.shiftRight_congr (· >>> ·)
diff --git a/src/Lean/Meta/Tactic/Grind/BitVec.lean b/src/Lean/Meta/Tactic/Grind/BitVec.lean index 29a009e..7bf5d66 100644 --- a/src/Lean/Meta/Tactic/Grind/BitVec.lean +++ b/src/Lean/Meta/Tactic/Grind/BitVec.lean
@@ -11,6 +11,7 @@ import Lean.ToExpr import Lean.Meta.Tactic.Grind.Simp public import Lean.Meta.Tactic.Grind.PropagatorAttr +import Lean.Util.SafeExponentiation public section namespace Lean.Meta.Grind @@ -113,13 +114,18 @@ else return none -/-- Table entry for `op : BitVec n → Nat → BitVec n` (shifts, rotations). -/ +/-- +Table entry for `op : BitVec n → Nat → BitVec n` (shifts, rotations). +`isLeftShift` must be `true` for left shifts: they are evaluated only if `checkShiftLeft` succeeds. +-/ @[inline] private def shiftBV (declName : Name) (arity : Nat) - (op : {n : Nat} → BitVec n → Nat → BitVec n) (e : Expr) : GoalM Unit := do + (op : {n : Nat} → BitVec n → Nat → BitVec n) (e : Expr) (isLeftShift := false) : GoalM Unit := do unless e.isAppOfArity declName arity do return () binOp e fun r₁ r₂ => do let some ⟨n, v⟩ ← getBitVecValue? r₁ | return none let some i ← getNatValue? r₂ | return none + if isLeftShift then + unless (← checkShiftLeft v.toNat i) do return none some <$> mkBVLit n (op v i) /-- Table entry for `op : BitVec n → Nat → Bool` (`getLsbD`, `getMsbD`). -/ @@ -198,7 +204,7 @@ some <$> mkBVLit (n₁ + n₂) (v₁ ++ v₂) builtin_grind_propagator propagateBVShiftLeft ↑BitVec.shiftLeft := - shiftBV ``BitVec.shiftLeft 3 BitVec.shiftLeft + shiftBV ``BitVec.shiftLeft 3 BitVec.shiftLeft (isLeftShift := true) builtin_grind_propagator propagateBVUShiftRight ↑BitVec.ushiftRight := shiftBV ``BitVec.ushiftRight 3 BitVec.ushiftRight builtin_grind_propagator propagateBVSShiftRight ↑BitVec.sshiftRight := @@ -208,21 +214,25 @@ builtin_grind_propagator propagateBVRotateRight ↑BitVec.rotateRight := shiftBV ``BitVec.rotateRight 3 BitVec.rotateRight -/-- `x <<< i` and `x >>> i` where the shift amount is a `Nat` or a `BitVec`. -/ +/-- +`x <<< i` and `x >>> i` where the shift amount is a `Nat` or a `BitVec`. +`isLeftShift` must be `true` for left shifts: they are evaluated only if `checkShiftLeft` succeeds. +-/ @[inline] private def hShiftBV (declName : Name) - (op : {n : Nat} → BitVec n → Nat → BitVec n) (e : Expr) : GoalM Unit := do + (op : {n : Nat} → BitVec n → Nat → BitVec n) (e : Expr) (isLeftShift := false) : GoalM Unit := do unless e.isAppOfArity declName 6 do return () binOp e fun r₁ r₂ => do let some ⟨n, v⟩ ← getBitVecValue? r₁ | return none - if let some i ← getNatValue? r₂ then - some <$> mkBVLit n (op v i) - else if let some ⟨_, w⟩ ← getBitVecValue? r₂ then - some <$> mkBVLit n (op v w.toNat) - else - return none + let i ← + if let some i ← getNatValue? r₂ then pure i + else if let some ⟨_, w⟩ ← getBitVecValue? r₂ then pure w.toNat + else return none + if isLeftShift then + unless (← checkShiftLeft v.toNat i) do return none + some <$> mkBVLit n (op v i) builtin_grind_propagator propagateBVHShiftLeft ↑HShiftLeft.hShiftLeft := - hShiftBV ``HShiftLeft.hShiftLeft (· <<< ·) + hShiftBV ``HShiftLeft.hShiftLeft (· <<< ·) (isLeftShift := true) builtin_grind_propagator propagateBVHShiftRight ↑HShiftRight.hShiftRight := hShiftBV ``HShiftRight.hShiftRight (· >>> ·)
diff --git a/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/BitVec.lean b/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/BitVec.lean index 360cd9f..39085ea 100644 --- a/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/BitVec.lean +++ b/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/BitVec.lean
@@ -8,6 +8,7 @@ prelude public import Lean.Meta.Tactic.Simp.BuiltinSimprocs.Int import Init.Data.BitVec.Lemmas +import Lean.Util.SafeExponentiation public section @@ -88,6 +89,17 @@ return .done <| (← toExpr' (op v.value i)) /-- +Helper function for reducing the left shift of a bitvector literal by a natural number literal. +-/ +@[inline] private def reduceShiftLeftCore (declName : Name) (arity : Nat) (e : Expr) : SimpM DStep := do + unless e.isAppOfArity declName arity do return .continue + let some v ← fromExpr? e.appFn!.appArg! | return .continue + let some i ← Nat.fromExpr? e.appArg! | return .continue + let warning := (← Simp.getConfig).warnExponents + unless (← checkShiftLeft v.value.toNat i (warning := warning)) do return .continue + return .done <| (← toExpr' (v.value <<< i)) + +/-- Helper function for reducing `x <<< i` and `x >>> i` where `i` is a bitvector literal, into one that is a natural number literal. -/ @@ -203,7 +215,7 @@ set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine /-- Simplification procedure for shift left on `BitVec`. -/ builtin_dsimproc [simp, seval] reduceShiftLeft (BitVec.shiftLeft _ _) := - reduceShift ``BitVec.shiftLeft 3 BitVec.shiftLeft + reduceShiftLeftCore ``BitVec.shiftLeft 3 set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine /-- Simplification procedure for unsigned shift right on `BitVec`. -/ builtin_dsimproc [simp, seval] reduceUShiftRight (BitVec.ushiftRight _ _) := @@ -215,7 +227,7 @@ set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine /-- Simplification procedure for shift left on `BitVec`. -/ builtin_dsimproc [simp, seval] reduceHShiftLeft ((_ <<< _ : BitVec _)) := - reduceShift ``HShiftLeft.hShiftLeft 6 (· <<< ·) + reduceShiftLeftCore ``HShiftLeft.hShiftLeft 6 set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine /-- Simplification procedure for converting a shift with a bit-vector literal into a natural number literal. -/ builtin_dsimproc [simp, seval] reduceHShiftLeft' ((_ <<< (_ : BitVec _) : BitVec _)) := @@ -380,6 +392,8 @@ let_expr shiftLeftZeroExtend _ v m ← e | return .continue let some v ← fromExpr? v | return .continue let some m ← Nat.fromExpr? m | return .continue + let warning := (← Simp.getConfig).warnExponents + unless (← checkShiftLeft v.value.toNat m (warning := warning)) do return .continue return .done <| (← toExpr' (v.value.shiftLeftZeroExtend m)) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine
diff --git a/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Fin.lean b/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Fin.lean index 7da39fc..54edf60 100644 --- a/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Fin.lean +++ b/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Fin.lean
@@ -8,6 +8,7 @@ prelude public import Lean.Meta.Tactic.Simp.BuiltinSimprocs.Nat import Init.Omega +import Lean.Util.SafeExponentiation public section @@ -94,7 +95,16 @@ builtin_dsimproc [simp, seval] reduceXor ((_ ^^^ _ : Fin _)) := reduceBin ``HXor.hXor 6 (· ^^^ ·) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine -builtin_dsimproc [simp, seval] reduceShiftLeft ((_ <<< _ : Fin _)) := reduceBin ``HShiftLeft.hShiftLeft 6 (· <<< ·) +builtin_dsimproc [simp, seval] reduceShiftLeft ((_ <<< _ : Fin _)) := fun e => do + unless e.isAppOfArity ``HShiftLeft.hShiftLeft 6 do return .continue + let some v₁ ← fromExpr? e.appFn!.appArg! | return .continue + let some v₂ ← fromExpr? e.appArg! | return .continue + if h : v₁.n = v₂.n then + let warning := (← Simp.getConfig).warnExponents + unless (← checkShiftLeft v₁.value.val v₂.value.val (warning := warning)) do return .continue + return .done <| toExpr (v₁.value <<< (h ▸ v₂.value)) + else + return .continue set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine builtin_dsimproc [simp, seval] reduceShiftRight ((_ >>> _ : Fin _)) := reduceBin ``HShiftRight.hShiftRight 6 (· >>> ·)
diff --git a/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Nat.lean b/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Nat.lean index e67ebea..491a277 100644 --- a/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Nat.lean +++ b/src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Nat.lean
@@ -99,8 +99,13 @@ builtin_dsimproc [simp, seval] reduceOr ((_ ||| _ : Nat)) := reduceBin ``HOr.hOr 6 (· ||| ·) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine -builtin_dsimproc [simp, seval] reduceShiftLeft ((_ <<< _ : Nat)) := - reduceBin ``HShiftLeft.hShiftLeft 6 (· <<< ·) +builtin_dsimproc [simp, seval] reduceShiftLeft ((_ <<< _ : Nat)) := fun e => do + let_expr HShiftLeft.hShiftLeft _ _ _ _ n m := e | return .continue + let some n ← fromExpr? n | return .continue + let some m ← fromExpr? m | return .continue + let warning := (← Simp.getConfig).warnExponents + unless (← checkShiftLeft n m (warning := warning)) do return .continue + return .done <| toExpr (n <<< m) set_option linter.coreInternal.internalModule false in -- User-facing builtin simprocs are fine builtin_dsimproc [simp, seval] reduceShiftRight ((_ >>> _ : Nat)) :=
diff --git a/src/Lean/Meta/WHNF.lean b/src/Lean/Meta/WHNF.lean index 39ce5a2..08e6d91 100644 --- a/src/Lean/Meta/WHNF.lean +++ b/src/Lean/Meta/WHNF.lean
@@ -1057,6 +1057,13 @@ trace[Meta.isDefEq.whnf.reduceBinOp] "{a} ^ {b}" return mkRawNatLit <| a ^ b +def reduceShiftLeft (a b : Expr) : MetaM (Option Expr) := + withNatValue a fun a => + withNatValue b fun b => OptionT.run do + guard (← checkShiftLeft a b) + trace[Meta.isDefEq.whnf.reduceBinOp] "{a} <<< {b}" + return mkRawNatLit <| a <<< b + def reduceBinNatPred (f : Nat → Nat → Bool) (a b : Expr) : MetaM (Option Expr) := do withNatValue a fun a => withNatValue b fun b => @@ -1083,7 +1090,7 @@ | ``Nat.land => reduceBinNatOp Nat.land a1 a2 | ``Nat.lor => reduceBinNatOp Nat.lor a1 a2 | ``Nat.xor => reduceBinNatOp Nat.xor a1 a2 - | ``Nat.shiftLeft => reduceBinNatOp Nat.shiftLeft a1 a2 + | ``Nat.shiftLeft => reduceShiftLeft a1 a2 | ``Nat.shiftRight => reduceBinNatOp Nat.shiftRight a1 a2 | _ => return none | _ =>
diff --git a/src/Lean/Util/SafeExponentiation.lean b/src/Lean/Util/SafeExponentiation.lean index b8eecd8..275e4df 100644 --- a/src/Lean/Util/SafeExponentiation.lean +++ b/src/Lean/Util/SafeExponentiation.lean
@@ -35,4 +35,44 @@ else return true +register_builtin_option shiftLeft.threshold : Nat := { + defValue := 65536 + descr := "maximum shift amount for \ + which left shift operations are safe to evaluate. When a shift amount \ + is a value greater than this threshold, the left shift will not be evaluated, \ + and a warning will be logged. This helps to prevent the system from becoming \ + unresponsive due to excessively large computations." +} + +/-- +Returns `true` if the left shift `a <<< n` is safe to evaluate given the threshold `maxShift`: +`a` is `0`, or `n` is `≤ maxShift` and fits in 32 bits. + +The runtime implementation of `Nat.shiftLeft` aborts the process when the shifted value is not `0` +and the shift amount does not fit in 32 bits. Thus, every procedure that evaluates a left shift of +literals whose shift amount is not bounded by a machine word size must use this function or +`checkShiftLeft`. +-/ +def isSafeShiftLeft (a n maxShift : Nat) : Bool := + a == 0 || (n ≤ maxShift && n < UInt32.size) + +/-- +Returns `true` if `isSafeShiftLeft a n threshold`, where `threshold` is the value of the option +`shiftLeft.threshold`. Otherwise, reports a warning and returns `false`. +This method ensures there is at most one warning message of this kind in the message log. +-/ +def checkShiftLeft (a n : Nat) (warning := true) : CoreM Bool := do + if a == 0 then return true + let threshold ← getRecordedOption shiftLeft.threshold + if n > threshold then + if (← pure warning <&&> logMessageKind `unsafe.shiftLeft) then + logWarning s!"shift amount {n} exceeds the threshold {threshold}, left shift operation was not evaluated, use `set_option {shiftLeft.threshold.name} <num>` to set a new threshold" + return false + else if n ≥ UInt32.size then + if (← pure warning <&&> logMessageKind `unsafe.shiftLeft) then + logWarning s!"shift amount {n} does not fit in 32 bits, left shift operation was not evaluated" + return false + else + return true + end Lean
diff --git a/tests/elab/grind_safeShiftLeft.lean b/tests/elab/grind_safeShiftLeft.lean new file mode 100644 index 0000000..7163cc3 --- /dev/null +++ b/tests/elab/grind_safeShiftLeft.lean
@@ -0,0 +1,91 @@ +/-! +`grind` does not evaluate left shifts of literals whose shift amount is above the +`shiftLeft.threshold` option. The runtime implementation of `Nat.shiftLeft` aborts the process +when the shift amount does not fit in 32 bits (#15193). +-/ + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x : Nat) (h : x = 4294967296) : (1 <<< x : Nat) = 0 := by grind + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example : (1 <<< 4294967296 : Nat) = 0 := by grind + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x y : BitVec 8) (n : Nat) (h : n = 4294967296) (hx : x = 1#8) : x <<< n = y := by grind + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x y : BitVec 8) (n : Nat) (h : n = 4294967296) (hx : x = 1#8) : x.shiftLeft n = y := by grind + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +example (x y : BitVec 8) (n : BitVec 64) (h : n = 4294967296#64) (hx : x = 1#8) : x <<< n = y := by grind + +#guard_msgs in +example (x y : Nat) (h : x = 4294967296) (hy : y = 0) (f : Nat → Nat) : f (y <<< x) = f 0 := by grind + +/-- +warning: shift amount 4294967296 does not fit in 32 bits, left shift operation was not evaluated +--- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +set_option shiftLeft.threshold 5000000000 in +example (x : Nat) (h : x = 4294967296) : (1 <<< x : Nat) = 0 := by grind + +#guard_msgs in +example (x : Nat) (h : x = 10) : (1 <<< x : Nat) = 1024 := by grind + +#guard_msgs in +example (x : Nat) (h : x = 65536) : (1 <<< x : Nat) % 2 = 0 := by grind + +#guard_msgs in +example (x : BitVec 8) (n : Nat) (h : n = 3) (hx : x = 1#8) : x <<< n = 8#8 := by grind + +#guard_msgs in +example (x : BitVec 8) (n : Nat) (h : n = 3) (hx : x = 1#8) : x.shiftLeft n = 8#8 := by grind + +#guard_msgs in +example (x : BitVec 8) (n : BitVec 8) (h : n = 3#8) (hx : x = 1#8) : x <<< n = 8#8 := by grind + +/-- +warning: shift amount 10 exceeds the threshold 2, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +-/ +#guard_msgs in +set_option shiftLeft.threshold 2 in +example (x y : Nat) (h : x = 10) (hy : y = 3) (f : Nat → Nat) : f (y <<< x) = f 3072 := by grind + +#guard_msgs in +example (x y : Nat) (h : x = 10) (hy : y = 3) (f : Nat → Nat) : f (y <<< x) = f 3072 := by grind + +/-- +warning: shift amount 3 exceeds the threshold 2, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `grind` has failed, use `+verbose` to obtain diagnostic information +-/ +#guard_msgs in +set_option shiftLeft.threshold 2 in +example (x : BitVec 8) (y : BitVec 8) (n : Nat) (h : n = 3) (hx : x = 1#8) : x <<< n = y := by grind
diff --git a/tests/elab/safeShiftLeft.lean b/tests/elab/safeShiftLeft.lean new file mode 100644 index 0000000..a346a6f --- /dev/null +++ b/tests/elab/safeShiftLeft.lean
@@ -0,0 +1,340 @@ +/-! +Left shifts of literals whose shift amount is above the `shiftLeft.threshold` option are not +evaluated by `whnf` and `simp`. `cbv` and the compiler use the default value of the option as a +constant bound. The runtime implementation of `Nat.shiftLeft` aborts the process when the shift +amount does not fit in 32 bits (#15193). +-/ + +-- `whnf` + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example : (1 <<< 4294967296 : Nat) = 0 := by rfl + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example : (1 <<< 4294967296 : Nat) = 0 := by decide + +#guard_msgs in +example : (1 <<< 100 : Nat) = 1267650600228229401496703205376 := by rfl + +#guard_msgs in +example : (1 <<< 65536 : Nat) % 2 = 0 := by rfl + +/-- +warning: shift amount 9 exceeds the threshold 8, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +-/ +#guard_msgs in +set_option shiftLeft.threshold 8 in +example : (1 <<< 9 : Nat) = 512 := by rfl + +#guard_msgs in +set_option shiftLeft.threshold 100000 in +example : (1 <<< 65537 : Nat) % 2 = 0 := by rfl + +#guard_msgs in +example : (0 <<< 4294967296 : Nat) = 0 := by rfl + +-- `simp` + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: unsolved goals +⊢ False +-/ +#guard_msgs in +example : (1 <<< 4294967296 : Nat) = 0 := by simp + +/-- +error: unsolved goals +⊢ False +-/ +#guard_msgs in +example : (1 <<< 4294967296 : Nat) = 0 := by simp -warnExponents + +/-- +warning: shift amount 65537 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : Nat) : (1 <<< 65537 : Nat) = x := by simp + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : Fin 5000000000) : ((3 : Fin 5000000000) <<< (4294967296 : Fin 5000000000)) = x := by simp + +/-- +warning: shift amount 4294967296 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: unsolved goals +x : BitVec (8 + 4294967296) +⊢ (1#8).shiftLeftZeroExtend 4294967296 = x +-/ +#guard_msgs in +example (x : BitVec (8 + 4294967296)) : BitVec.shiftLeftZeroExtend 1#8 4294967296 = x := by simp + +#guard_msgs in +example : (1#8 <<< (4294967296 : Nat)) = 0#8 := by simp + +#guard_msgs in +example : (1#8 <<< 4294967296#64) = 0#8 := by simp + +#guard_msgs in +example : BitVec.shiftLeft 1#8 4294967296 = 0#8 := by simp + +/-- +warning: shift amount 70000 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : BitVec 100000) : (1#100000 <<< (70000 : Nat)) = x := by simp only [BitVec.reduceHShiftLeft] + +/-- +warning: shift amount 70000 exceeds the threshold 65536, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `simp` made no progress +-/ +#guard_msgs in +example (x : BitVec 100000) : BitVec.shiftLeft 1#100000 70000 = x := by simp only [BitVec.reduceShiftLeft] + +#guard_msgs in +example : (1 <<< 100 : Nat) = 1267650600228229401496703205376 := by simp + +#guard_msgs in +example : (1 <<< 65536 : Nat) % 2 = 0 := by simp + +#guard_msgs in +example : (1#8 <<< (3 : Nat)) = 8#8 := by simp + +#guard_msgs in +example : (1#8 <<< 3#8) = 8#8 := by simp + +#guard_msgs in +example : BitVec.shiftLeft 1#8 3 = 8#8 := by simp + +#guard_msgs in +example : BitVec.shiftLeftZeroExtend 1#8 3 = 8#11 := by simp + +#guard_msgs in +example : ((3 : Fin 10) <<< (1 : Fin 10)) = 6 := by simp + +#guard_msgs in +set_option shiftLeft.threshold 100000 in +example : (1 <<< 65537 : Nat) % 2 = 0 := by simp + +/-- +warning: shift amount 9 exceeds the threshold 8, left shift operation was not evaluated, use `set_option shiftLeft.threshold <num>` to set a new threshold +--- +error: `simp` made no progress +-/ +#guard_msgs in +set_option shiftLeft.threshold 8 in +example (x : Nat) : (1 <<< 9 : Nat) = x := by simp + +#guard_msgs in +example : (0 <<< 4294967296 : Nat) = 0 := by simp only [Nat.reduceShiftLeft] + +#guard_msgs in +example : (0#8 <<< (4294967296 : Nat)) = 0#8 := by simp only [BitVec.reduceHShiftLeft] + +#guard_msgs in +example : BitVec.shiftLeft 0#8 4294967296 = 0#8 := by simp only [BitVec.reduceShiftLeft] + +#guard_msgs in +example : ((0 : Fin 5000000000) <<< (4294967296 : Fin 5000000000)) = 0 := by simp only [Fin.reduceShiftLeft] + +/-- +warning: shift amount 4294967296 does not fit in 32 bits, left shift operation was not evaluated +--- +error: `simp` made no progress +-/ +#guard_msgs in +set_option shiftLeft.threshold 5000000000 in +example (x : Nat) : (1 <<< 4294967296 : Nat) = x := by simp + +/-- +warning: shift amount 4294967296 does not fit in 32 bits, left shift operation was not evaluated +--- +error: `simp` made no progress +-/ +#guard_msgs in +set_option shiftLeft.threshold 5000000000 in +example (x : BitVec 8) : (1#8 <<< (4294967296 : Nat)) = x := by simp only [BitVec.reduceHShiftLeft] + +-- `cbv` + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example : (1 <<< 4294967296 : Nat) = 0 := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example : ((1 : Int) <<< (4294967296 : Nat)) = 0 := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example : (1#8 <<< (4294967296 : Nat)) = 0#8 := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example : (1#8 <<< 4294967296#64) = 0#8 := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example : ((3 : Fin 5000000000) <<< (4294967296 : Fin 5000000000)) = 0 := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example : BitVec.shiftLeft 1#8 4294967296 = 0#8 := by cbv + +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +example (x : BitVec (8 + 4294967296)) : BitVec.shiftLeftZeroExtend 1#8 4294967296 = x := by cbv + +#guard_msgs in +example : (1 <<< 100 : Nat) = 1267650600228229401496703205376 := by cbv + +#guard_msgs in +example : (1 <<< 65536 : Nat) % 2 = 0 := by cbv + +#guard_msgs in +example : ((1 : Int) <<< (3 : Nat)) = 8 := by cbv + +#guard_msgs in +example : (1#8 <<< (3 : Nat)) = 8#8 := by cbv + +#guard_msgs in +example : (1#8 <<< 3#8) = 8#8 := by cbv + +#guard_msgs in +example : BitVec.shiftLeft 1#8 3 = 8#8 := by cbv + +#guard_msgs in +example : BitVec.shiftLeftZeroExtend 1#8 3 = 8#11 := by cbv + +#guard_msgs in +example : ((3 : Fin 10) <<< (1 : Fin 10)) = 6 := by cbv + +#guard_msgs in +example : ((1 : UInt64) <<< (4294967296 : UInt64)) = 1 := by cbv + +-- `cbv` uses its own bound (`maxShiftLeft`), and does not read the `shiftLeft.threshold` option. +/-- +error: maximum recursion depth has been reached +use `set_option maxRecDepth <num>` to increase limit +use `set_option diagnostics true` to get diagnostic information +-/ +#guard_msgs in +set_option shiftLeft.threshold 100000 in +example : (1 <<< 65537 : Nat) % 2 = 0 := by cbv + +#guard_msgs in +example : (0 <<< 4294967296 : Nat) = 0 := by cbv + +#guard_msgs in +example : ((0 : Int) <<< (4294967296 : Nat)) = 0 := by cbv + +#guard_msgs in +example : (0#8 <<< (4294967296 : Nat)) = 0#8 := by cbv + +#guard_msgs in +example : (0#8 <<< 4294967296#64) = 0#8 := by cbv + +#guard_msgs in +example : BitVec.shiftLeft 0#8 4294967296 = 0#8 := by cbv + +#guard_msgs in +example : ((0 : Fin 5000000000) <<< (4294967296 : Fin 5000000000)) = 0 := by cbv + +-- Compiler: the constant folder evaluates shift amounts below 65536 only. + +#guard_msgs in +def shiftHuge : Nat := 1 <<< 4294967296 + +#guard_msgs in +def shiftZero : Nat := 0 <<< 4294967296 + +/-- +trace: [Compiler.result] size: 3 + def shiftNoFold._closed_0 : tobj := + let _x.1 := 65536; + let _x.2 := 1; + let _x.3 := Nat.shiftLeft _x.2 _x.1; + return _x.3 +[Compiler.result] size: 2 + def shiftNoFold @&x : tobj := + let _x.1 := shiftNoFold._closed_0; + let _x.2 := Nat.add x _x.1; + return _x.2 +[Compiler.result] size: 2 + def shiftNoFold._boxed x : tobj := + let res := shiftNoFold x; + dec x; + return res +-/ +#guard_msgs in +set_option trace.Compiler.result true in +def shiftNoFold (x : Nat) : Nat := x + (1 <<< 65536) + +/-- +trace: [Compiler.result] size: 2 + def shiftFold @&x : tobj := + let _x.1 := 256; + let _x.2 := Nat.add x _x.1; + return _x.2 +[Compiler.result] size: 2 + def shiftFold._boxed x : tobj := + let res := shiftFold x; + dec x; + return res +-/ +#guard_msgs in +set_option trace.Compiler.result true in +def shiftFold (x : Nat) : Nat := x + (1 <<< 8)