From 320da675705e8700bc1377a5fa22b4cdf1e52704 Mon Sep 17 00:00:00 2001 From: Niklas Mollenhauer Date: Wed, 29 May 2024 18:17:43 +0200 Subject: [PATCH] feat(compiler): Compiler Logical Negation Constant Propagation (#29623) ## Summary Resolves #29622 ## How did you test this change? I verified the implementation using the test. Note: This PR was done without waiting for approval in #29622, so feel free to just close it. --- .../src/Optimization/ConstantPropagation.ts | 19 ++++ .../constant-propagation-unary.expect.md | 86 +++++++++++++++++++ .../compiler/constant-propagation-unary.js | 35 ++++++++ 3 files changed, 140 insertions(+) create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.expect.md create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.js diff --git a/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts b/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts index 04a401143a..e2116c9d94 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts @@ -311,6 +311,25 @@ function evaluateInstruction( } return null; } + case "UnaryExpression": { + switch (value.operator) { + case "!": { + const operand = read(constants, value.value); + if (operand !== null && operand.kind === "Primitive") { + const result: Primitive = { + kind: "Primitive", + value: !operand.value, + loc: value.loc, + }; + instr.value = result; + return result; + } + return null; + } + default: + return null; + } + } case "BinaryExpression": { const lhsValue = read(constants, value.left); const rhsValue = read(constants, value.right); diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.expect.md new file mode 100644 index 0000000000..50b1a10a24 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.expect.md @@ -0,0 +1,86 @@ + +## Input + +```javascript +import { Stringify } from "shared-runtime"; + +function foo() { + let _b; + const b = true; + if (!b) { + _b = "bar"; + } else { + _b = "baz"; + } + + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ( + + ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok)
{"value":{"_b":"baz","b0":false,"n0":true,"n1":false,"n2":false,"n3":false,"s0":true,"s1":false,"s2":false,"u":true,"n":true}}
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.js new file mode 100644 index 0000000000..f952bb1c2d --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.js @@ -0,0 +1,35 @@ +import { Stringify } from "shared-runtime"; + +function foo() { + let _b; + const b = true; + if (!b) { + _b = "bar"; + } else { + _b = "baz"; + } + + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +};