Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions src/core/postscript/js_evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import { MathClamp } from "../../shared/math_clamp.js";
import { TOKEN } from "./lexer.js";

// Consecutive integers for a dense jump table in _execute.
// 2-slot: ARG, CONST, IF, JUMP, SHIFT. 4-slot: STORE. All others: 1 slot.
// 2-slot: ARG, CONST, IF, JUMP, SHIFT, TEE_TMP, LOAD_TMP, STORE_TMP.
// 4-slot: STORE. All others: 1 slot.
const OP = {
ARG: 0, // [ARG, idx]
CONST: 1, // [CONST, val]
Expand Down Expand Up @@ -67,6 +68,7 @@ const OP = {
MAX: 38,
TEE_TMP: 39, // [TEE_TMP, slot] peek top of stack → tmp[slot], leave on stack
LOAD_TMP: 40, // [LOAD_TMP, slot] push tmp[slot]
STORE_TMP: 41, // [STORE_TMP, slot] pop() → tmp[slot]
};

const _DEG_TO_RAD = Math.PI / 180;
Expand Down Expand Up @@ -279,8 +281,60 @@ class PsJsCompiler {
return true;
}

/**
* Compute the shared nodes used in `node`, which haven't been compiled yet,
* and store them in their tmp slots.
* A shared node is stored in a tmp slot where it's compiled for the first
* time, so this mustn't happen in a branch of a ternary node: the slot
* wouldn't be set when the other branch is taken. Since the operators don't
* have any side effect, computing these nodes unconditionally is fine.
*/
_hoistSharedNodes(node) {
if (this._tmpMap.has(node)) {
return true;
}
switch (node.type) {
case PS_NODE.unary:
if (!this._hoistSharedNodes(node.operand)) {
return false;
}
break;
case PS_NODE.binary:
if (
!this._hoistSharedNodes(node.second) ||
!this._hoistSharedNodes(node.first)
) {
return false;
}
break;
case PS_NODE.ternary:
if (
!this._hoistSharedNodes(node.cond) ||
!this._hoistSharedNodes(node.then) ||
!this._hoistSharedNodes(node.otherwise)
) {
return false;
}
break;
}
if (!node.shared) {
return true;
}
if (!this._compileNodeImpl(node)) {
return false;
}
const slot = this._nextTmp++;
this._tmpMap.set(node, slot);
this.ir.push(OP.STORE_TMP, slot);
return true;
}

_compileTernary(node) {
if (!this._compileNode(node.cond)) {
if (
!this._hoistSharedNodes(node.then) ||
!this._hoistSharedNodes(node.otherwise) ||
!this._compileNode(node.cond)
) {
return false;
}

Expand Down Expand Up @@ -509,6 +563,9 @@ class PsJsCompiler {
case OP.LOAD_TMP:
stack[sp++] = tmp[ir[ip++] | 0];
break;
case OP.STORE_TMP:
tmp[ir[ip++] | 0] = stack[--sp];
break;
}
}
}
Expand Down
61 changes: 59 additions & 2 deletions src/core/postscript/wasm_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,10 @@ class PsWasmCompiler {
* f64/i32 round-trips for comparisons, boolean and/or/xor, and boolean `not`.
*/
_compileNodeAsBoolI32(node) {
if (node.type === PS_NODE.binary) {
// The fast paths below compile the operands directly, hence a shared node,
// which must be computed only once, goes through `_compileNode` instead
// (see the fallback at the end).
if (node.type === PS_NODE.binary && !node.shared) {
// Comparison: leaves i32 directly.
const wasmOp = PsWasmCompiler.#comparisonToOp.get(node.op);
if (wasmOp !== undefined) {
Expand Down Expand Up @@ -819,6 +822,7 @@ class PsWasmCompiler {
// Boolean not: i32.eqz.
if (
node.type === PS_NODE.unary &&
!node.shared &&
node.op === TOKEN.not &&
node.valueType === PS_VALUE_TYPE.boolean
) {
Expand All @@ -842,9 +846,62 @@ class PsWasmCompiler {
return true;
}

/**
* Compute the shared nodes used in `node`, which haven't been compiled yet,
* and store them in their locals.
* A shared node is stored in a local where it's compiled for the first time,
* so this mustn't happen in a branch of a ternary node: the local wouldn't
* be set when the other branch is taken. Since the operators don't have any
* side effect, computing these nodes unconditionally is fine.
*/
_hoistSharedNodes(node) {
if (this._sharedLocals.has(node)) {
return true;
}
switch (node.type) {
case PS_NODE.unary:
if (!this._hoistSharedNodes(node.operand)) {
return false;
}
break;
case PS_NODE.binary:
if (
!this._hoistSharedNodes(node.second) ||
!this._hoistSharedNodes(node.first)
) {
return false;
}
break;
case PS_NODE.ternary:
if (
!this._hoistSharedNodes(node.cond) ||
!this._hoistSharedNodes(node.then) ||
!this._hoistSharedNodes(node.otherwise)
) {
return false;
}
break;
}
if (!node.shared) {
return true;
}
if (!this._compileNodeImpl(node)) {
return false;
}
const local = this._allocLocal();
// None of the uses of the node has been compiled yet.
this._sharedLocals.set(node, { local, remaining: node.sharedCount });
this._emitLocalSet(local);
return true;
}

// Value-returning if/else/end; both branches leave one f64.
_compileTernaryNode(node) {
if (!this._compileNodeAsBoolI32(node.cond)) {
if (
!this._hoistSharedNodes(node.then) ||
!this._hoistSharedNodes(node.otherwise) ||
!this._compileNodeAsBoolI32(node.cond)
) {
return false;
}
this._code.push(OP.if, F64);
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.error

!postscript_type4_many_outputs.pdf
!postscript_type4_shared_branch.pdf
!boundingBox_invalid.pdf
!pdkids.pdf
!tracemonkey.pdf
Expand Down
Binary file added test/pdfs/postscript_type4_shared_branch.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"rounds": 1,
"type": "eq"
},
{
"id": "postscript-type4-shared-branch",
"file": "pdfs/postscript_type4_shared_branch.pdf",
"md5": "e5b4be02df3e63ab2565972f6ec5e331",
"rounds": 1,
"type": "eq"
},
{
"id": "bug1755201",
"file": "pdfs/bug1755201.pdf",
Expand Down
24 changes: 24 additions & 0 deletions test/unit/postscript_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,30 @@ describe("PostScript Type 4 lexer, parser, and Wasm compiler", function () {
expect(r).toBeCloseTo(25, 9);
});

it("CSE: shared subexpression first used in a branch of an if", function () {
// `in0 sin` is used in both branches, but not in the condition.
const src = "{ dup sin exch 0 gt { 1 add } if }";
const domain = [-360, 360];
const range = [-2, 2];
expect(compileAndRun(src, domain, range, [90])).toBeCloseTo(2, 9);
expect(compileAndRun(src, domain, range, [-90])).toBeCloseTo(-1, 9);
expect(compileAndRun(src, domain, range, [-30])).toBeCloseTo(-0.5, 9);
});

it("CSE: condition shared by several ternary nodes", function () {
// `in0 2 mul` is used in the condition of the two ternary nodes created
// by the `if`, and then as the last output: its local mustn't be reused
// in the meantime, e.g. for the temporary needed by `cvi`.
const r = compileAndRun(
"{ exch 2 mul dup dup dup dup 1 gt { pop pop 7 8 } if" +
" 5 -1 roll cvi 3 and 5 -1 roll }",
[0, 10, 0, 10],
[-100, 100, -100, 100, -100, 100, -100, 100, -100, 100],
[3.25, 6.5]
);
expect(r).toEqual([6.5, 7, 8, 2, 6.5]);
});

it("reuses temporary locals across sequential shared-subexpression codegen", function () {
const bytes = compilePostScriptToWasm(
"{ dup 1 add dup mul exch 2 add dup mul add }",
Expand Down