Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macroCondition: do branch elimination if no runtime impl. is involved #1468

Merged
merged 1 commit into from
Jun 9, 2023
Merged
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
50 changes: 34 additions & 16 deletions packages/macros/src/babel/evaluate-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const unops: { [operator: string]: any } = {
export interface ConfidentResult {
confident: true;
value: any;
hasRuntimeImplementation: boolean;
}

export interface UnknownResult {
Expand Down Expand Up @@ -162,6 +163,8 @@ export class Evaluator {
return confidentObject.value[confidentProperty.value];
}
},
hasRuntimeImplementation:
confidentObject.hasRuntimeImplementation || confidentProperty.hasRuntimeImplementation,
};
}
}
Expand All @@ -174,7 +177,7 @@ export class Evaluator {
return first;
}
if (path.isIdentifier()) {
return { confident: true, value: path.node.name };
return { confident: true, value: path.node.name, hasRuntimeImplementation: false };
}
return { confident: false };
}
Expand All @@ -191,7 +194,7 @@ export class Evaluator {
private realEvaluate(path: NodePath): EvaluateResult {
let builtIn = path.evaluate();
if (isConfidentResult(builtIn)) {
return builtIn;
return { ...builtIn, hasRuntimeImplementation: false };
}

if (path.isMemberExpression()) {
Expand All @@ -205,19 +208,19 @@ export class Evaluator {
}

if (path.isStringLiteral()) {
return { confident: true, value: path.node.value };
return { confident: true, value: path.node.value, hasRuntimeImplementation: false };
}

if (path.isNumericLiteral()) {
return { confident: true, value: path.node.value };
return { confident: true, value: path.node.value, hasRuntimeImplementation: false };
}

if (path.isBooleanLiteral()) {
return { confident: true, value: path.node.value };
return { confident: true, value: path.node.value, hasRuntimeImplementation: false };
}

if (path.isNullLiteral()) {
return { confident: true, value: null };
return { confident: true, value: null, hasRuntimeImplementation: false };
}

if (path.isObjectExpression()) {
Expand Down Expand Up @@ -246,6 +249,9 @@ export class Evaluator {
}
return result;
},
hasRuntimeImplementation: confidentProps.some(
([key, value]) => key.hasRuntimeImplementation || value.hasRuntimeImplementation
),
};
}

Expand All @@ -260,6 +266,7 @@ export class Evaluator {
get value() {
return confidentElements.map(element => element.value);
},
hasRuntimeImplementation: confidentElements.some(el => el.hasRuntimeImplementation),
};
}
}
Expand Down Expand Up @@ -295,7 +302,11 @@ export class Evaluator {
let rightOperand = this.evaluate(path.get('right') as NodePath<t.Expression>);
if (leftOperand.confident && rightOperand.confident) {
let value = binops[operator](leftOperand.value, rightOperand.value);
return { confident: true, value };
return {
confident: true,
value,
hasRuntimeImplementation: leftOperand.hasRuntimeImplementation || rightOperand.hasRuntimeImplementation,
};
}
}
}
Expand All @@ -307,7 +318,11 @@ export class Evaluator {
if (test.confident) {
let result = test.value ? this.evaluate(path.get('consequent')) : this.evaluate(path.get('alternate'));
if (result.confident) {
return result;
return {
confident: true,
value: result.value,
hasRuntimeImplementation: test.hasRuntimeImplementation || result.hasRuntimeImplementation,
};
}
}
}
Expand All @@ -318,7 +333,7 @@ export class Evaluator {
let operand = this.evaluate(path.get('argument') as NodePath<t.Expression>);
if (operand.confident) {
let value = unops[operator](operand.value);
return { confident: true, value };
return { confident: true, value, hasRuntimeImplementation: operand.hasRuntimeImplementation };
}
}
return { confident: false };
Expand All @@ -328,7 +343,7 @@ export class Evaluator {
if (!this.locals.hasOwnProperty(path.node.name)) {
return { confident: false };
}
return { confident: true, value: this.locals[path.node.name] };
return { confident: true, value: this.locals[path.node.name], hasRuntimeImplementation: false };
}

return { confident: false };
Expand All @@ -354,6 +369,7 @@ export class Evaluator {
get value() {
throw new Error(`bug in @embroider/macros: didn't expect to need to evaluate this value`);
},
hasRuntimeImplementation: true,
};
}
}
Expand All @@ -366,19 +382,19 @@ export class Evaluator {
}
let callee = path.get('callee');
if (callee.referencesImport('@embroider/macros', 'dependencySatisfies')) {
return { confident: true, value: dependencySatisfies(path, this.state) };
return { confident: true, value: dependencySatisfies(path, this.state), hasRuntimeImplementation: false };
}
if (callee.referencesImport('@embroider/macros', 'moduleExists')) {
return { confident: true, value: moduleExists(path, this.state) };
return { confident: true, value: moduleExists(path, this.state), hasRuntimeImplementation: false };
}
if (callee.referencesImport('@embroider/macros', 'getConfig')) {
return { confident: true, value: getConfig(path, this.state, 'package') };
return { confident: true, value: getConfig(path, this.state, 'package'), hasRuntimeImplementation: true };
}
if (callee.referencesImport('@embroider/macros', 'getOwnConfig')) {
return { confident: true, value: getConfig(path, this.state, 'own') };
return { confident: true, value: getConfig(path, this.state, 'own'), hasRuntimeImplementation: true };
}
if (callee.referencesImport('@embroider/macros', 'getGlobalConfig')) {
return { confident: true, value: getConfig(path, this.state, 'getGlobalConfig') };
return { confident: true, value: getConfig(path, this.state, 'getGlobalConfig'), hasRuntimeImplementation: true };
}
if (callee.referencesImport('@embroider/macros', 'isDevelopingApp')) {
return {
Expand All @@ -387,19 +403,21 @@ export class Evaluator {
this.state.opts.appPackageRoot &&
this.state.opts.isDevelopingPackageRoots.includes(this.state.opts.appPackageRoot)
),
hasRuntimeImplementation: false,
};
}
if (callee.referencesImport('@embroider/macros', 'isDevelopingThisPackage')) {
return {
confident: true,
value: this.state.opts.isDevelopingPackageRoots.includes(this.state.owningPackage().root),
hasRuntimeImplementation: false,
};
}
if (callee.referencesImport('@embroider/macros', 'isTesting')) {
let g = getConfig(path, this.state, 'getGlobalConfig') as any;
let e = g && g['@embroider/macros'];
let value = Boolean(e && e.isTesting);
return { confident: true, value };
return { confident: true, value, hasRuntimeImplementation: true };
}
return { confident: false };
}
Expand Down
6 changes: 4 additions & 2 deletions packages/macros/src/babel/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ function targetPackage(fromPath: string, packageName: string | undefined, packag
}
}

function collapse(path: NodePath, config: any) {
let evaluator = new Evaluator({ knownPaths: new Map([[path, { confident: true, value: config }]]) });
function collapse(path: NodePath, config: unknown) {
let evaluator = new Evaluator({
knownPaths: new Map([[path, { confident: true, value: config, hasRuntimeImplementation: false }]]),
});

while (true) {
let parentPath = path.parentPath!;
Expand Down
2 changes: 1 addition & 1 deletion packages/macros/src/babel/macro-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function macroCondition(conditionalPath: MacroConditionPath, stat
let consequent = conditionalPath.get('consequent');
let alternate = conditionalPath.get('alternate');

if (state.opts.mode === 'run-time') {
if (state.opts.mode === 'run-time' && predicate.hasRuntimeImplementation !== false) {
let callee = conditionalPath.get('test').get('callee');
callee.replaceWith(state.importUtil.import(callee, state.pathToOurAddon('runtime'), 'macroCondition'));
} else {
Expand Down
Loading