From f129ae6fd4a737149afe151c6317bdbd82794bed Mon Sep 17 00:00:00 2001 From: Niklas Mollenhauer Date: Wed, 7 May 2025 18:41:59 +0200 Subject: [PATCH 1/3] Implement constant folding for unary minus --- .../src/Optimization/ConstantPropagation.ts | 17 +++++++ ...onstant-propagation-unary-number.expect.md | 47 +++++++++++++++++++ .../constant-propagation-unary-number.js | 12 +++++ .../constant-propagation-unary.expect.md | 2 +- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.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 07cd419230ac4..05f4ef1ae7ffd 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts @@ -327,6 +327,23 @@ function evaluateInstruction( } return null; } + case '-': { + const operand = read(constants, value.value); + if ( + operand !== null && + operand.kind === 'Primitive' && + typeof operand.value === 'number' + ) { + const result: Primitive = { + kind: 'Primitive', + value: operand.value * -1, + loc: value.loc, + }; + instr.value = result; + return result; + } + return null; + } default: return null; } diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md new file mode 100644 index 0000000000000..9f6ac45d3fd87 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; + +function foo() { + const a = -1; + 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":[-2,0]}
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js new file mode 100644 index 0000000000000..498196fe69ae0 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js @@ -0,0 +1,12 @@ +import {Stringify} from 'shared-runtime'; + +function foo() { + const a = -1; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; 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 index 3ba5ea6bb9553..aaea767643b06 100644 --- 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 @@ -58,7 +58,7 @@ function foo() { n0: true, n1: false, n2: false, - n3: !-1, + n3: false, s0: true, s1: false, s2: false, From 2a1ff3500fbc06886c61f2049f0f9e52e1878976 Mon Sep 17 00:00:00 2001 From: Niklas Mollenhauer Date: Wed, 7 May 2025 18:51:43 +0200 Subject: [PATCH 2/3] Add NaN and infinity tests --- ...onstant-propagation-unary-number.expect.md | 22 ++++++++++++++++--- .../constant-propagation-unary-number.js | 6 ++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md index 9f6ac45d3fd87..127e069a89dd0 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md @@ -6,7 +6,11 @@ import {Stringify} from 'shared-runtime'; function foo() { const a = -1; - return ; + return ( + + ); } export const FIXTURE_ENTRYPOINT = { @@ -27,7 +31,19 @@ function foo() { const $ = _c(1); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - t0 = ; + t0 = ( + + ); $[0] = t0; } else { t0 = $[0]; @@ -44,4 +60,4 @@ export const FIXTURE_ENTRYPOINT = { ``` ### Eval output -(kind: ok)
{"value":[-2,0]}
\ No newline at end of file +(kind: ok)
{"value":[-2,0,null,null,null,null,null]}
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js index 498196fe69ae0..dc06961d7ec04 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js @@ -2,7 +2,11 @@ import {Stringify} from 'shared-runtime'; function foo() { const a = -1; - return ; + return ( + + ); } export const FIXTURE_ENTRYPOINT = { From c7e9d7c8edfb30dc72097cfe10acca94fbfee736 Mon Sep 17 00:00:00 2001 From: Niklas Mollenhauer Date: Wed, 7 May 2025 18:56:41 +0200 Subject: [PATCH 3/3] Add comparison between 0 and -0 --- .../constant-propagation-unary-number.expect.md | 15 +++++++++++++-- .../compiler/constant-propagation-unary-number.js | 11 ++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md index 127e069a89dd0..074c3214810bd 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md @@ -8,7 +8,16 @@ function foo() { const a = -1; return ( ); } @@ -36,8 +45,10 @@ function foo() { value={[ -2, 0, + true, -Infinity, -NaN, + -1 * NaN, -1 * Infinity, -1 * -Infinity, @@ -60,4 +71,4 @@ export const FIXTURE_ENTRYPOINT = { ``` ### Eval output -(kind: ok)
{"value":[-2,0,null,null,null,null,null]}
\ No newline at end of file +(kind: ok)
{"value":[-2,0,true,null,null,null,null,null]}
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js index dc06961d7ec04..8ef8ec0e6c79a 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js @@ -4,7 +4,16 @@ function foo() { const a = -1; return ( ); }