From b968d4da48fd3b9ae703698bfe31ecdc731630fa Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Wed, 16 Aug 2023 15:39:01 +0100 Subject: [PATCH] [hir] Validate globals are equal before propagating Make sure the value of the globals in phi operands are the same before constant propagating them. --- .../src/Optimization/ConstantPropagation.ts | 13 ++++++++ ...t-propagate-global-phis-constant.expect.md | 33 +++++++++++++++++++ ...constant-propagate-global-phis-constant.js | 6 ++++ 3 files changed, 52 insertions(+) create mode 100644 compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md create mode 100644 compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts b/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts index 8b7691ee1d..cba7623a45 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/Optimization/ConstantPropagation.ts @@ -206,6 +206,19 @@ function evaluatePhi(phi: Phi, constants: Constants): Constant | null { } break; } + case "LoadGlobal": { + CompilerError.invariant(value.kind === "LoadGlobal", { + reason: "value kind expected to be LoadGlobal", + loc: null, + suggestions: null, + }); + + // different global values, can't constant propogate + if (operandValue.name !== value.name) { + return null; + } + break; + } default: return null; } diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md new file mode 100644 index 0000000000..5f8201c8f2 --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md @@ -0,0 +1,33 @@ + +## Input + +```javascript +function Test() { + const { tab } = useFoo(); + const currentTab = tab === WAT ? WAT : WAT; + + return ; +} + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function Test() { + const $ = useMemoCache(1); + const { tab } = useFoo(); + tab === WAT ? WAT : WAT; + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js new file mode 100644 index 0000000000..a9b0384026 --- /dev/null +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js @@ -0,0 +1,6 @@ +function Test() { + const { tab } = useFoo(); + const currentTab = tab === WAT ? WAT : WAT; + + return ; +}