From 07549ac9ab7433e9fd83996a952662d0fcd53354 Mon Sep 17 00:00:00 2001 From: Martin Kustermann Date: Wed, 9 Aug 2023 11:31:47 +0000 Subject: [PATCH] [vm] When marking an instruction as deopt to before an instruction, ensure prune-count=0 The [Environment] represents 2 different things: * before (env, deopt-id): The state of unoptimized frame right before the IL instruction is executed * after (env, deopt-id): The state of unoptimized frame right after the call machine instruction instruction (i.e. which may not be after the IL instruction - as it may still have to drop arguments) The environment's prune-count specifies the number of entries from the [Environment] to be removed to get from before-env to after-env. This prune-count includes generally most of `::InputCount()` - as most IR instructions consume their inputs (which location summary may e.g. require in registers) before doing any call. Though it doesn't include arguments as we currently have a stack-based calling convention and both optimized and unoptimized code will explicitly drop them after the machine call instruction. Our optimizing compiler may emit speculative instructions. Those will receive an environment that makes before (env, deopt-id) and after (env, deopt-id) the same - as both eager and lazy deopt will target a before (env, deopt-id). => We should ensure that the prune-count is set to 0, ensuring we won't prune anything in case an IL instruction was marked with `MarkAsLazyDeoptToBeforeDeoptId`. => This is a preparation for inlining force-optimized functions: If we inline them and some callee IL instruction performs lazy deopt it should re-try the call without any pruned inputs. TEST=ci Change-Id: I091c9fa962b376200dc5cfb6ea8c9a47ef43810f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/319440 Commit-Queue: Martin Kustermann Reviewed-by: Slava Egorov --- runtime/vm/compiler/backend/il.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/runtime/vm/compiler/backend/il.h b/runtime/vm/compiler/backend/il.h index f92e9d9f43e..206426ea9e3 100644 --- a/runtime/vm/compiler/backend/il.h +++ b/runtime/vm/compiler/backend/il.h @@ -10839,6 +10839,9 @@ class Environment : public ZoneAllocated { void MarkAsLazyDeoptToBeforeDeoptId() { bitfield_ = LazyDeoptToBeforeDeoptId::update(true, bitfield_); + // As eager and lazy deopts will target the before environment, we do not + // want to prune inputs on lazy deopts. + bitfield_ = LazyDeoptPruningBits::update(0, bitfield_); } // This environment belongs to an optimistically hoisted instruction. @@ -10847,6 +10850,9 @@ class Environment : public ZoneAllocated { void MarkAsHoisted() { bitfield_ = Hoisted::update(true, bitfield_); } Environment* GetLazyDeoptEnv(Zone* zone) { + if (LazyDeoptToBeforeDeoptId()) { + ASSERT(LazyDeoptPruneCount() == 0); + } const intptr_t num_args_to_prune = LazyDeoptPruneCount(); if (num_args_to_prune == 0) return this; return DeepCopy(zone, Length() - num_args_to_prune);