-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathInliningOptimizer.java
More file actions
313 lines (270 loc) · 11.1 KB
/
Copy pathInliningOptimizer.java
File metadata and controls
313 lines (270 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dev.cel.optimizer.optimizers;
import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.UnsignedLong;
import dev.cel.bundle.Cel;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelMutableAst;
import dev.cel.common.Operator;
import dev.cel.common.ast.CelConstant;
import dev.cel.common.ast.CelExpr.ExprKind.Kind;
import dev.cel.common.ast.CelMutableExpr;
import dev.cel.common.ast.CelMutableExpr.CelMutableCall;
import dev.cel.common.ast.CelMutableExpr.CelMutableComprehension;
import dev.cel.common.ast.CelMutableExpr.CelMutableStruct;
import dev.cel.common.navigation.CelNavigableMutableAst;
import dev.cel.common.navigation.CelNavigableMutableExpr;
import dev.cel.common.types.CelKind;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;
import dev.cel.common.values.NullValue;
import dev.cel.optimizer.AstMutator;
import dev.cel.optimizer.CelAstOptimizer;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Stream;
/**
* Performs optimization for inlining variables within function calls and select statements with
* their associated AST.
*/
public final class InliningOptimizer implements CelAstOptimizer {
private final ImmutableList<InlineVariable> inlineVariables;
private final AstMutator astMutator;
/**
* Creates a new {@code InliningOptimizer} with one or more {@link InlineVariable}s.
*
* <p>Note that the variables to be inlined can be a dependency to one other based on the supplied
* ordering. This allows for recursive inlining where a replacement value might itself contain
* variables that need to be inlined.
*
* <p>For example, given a source expression {@code "a + b"} and inline variables in the following
* order:
*
* <ul>
* <li>{@code {a: b, b: 2}}, result: {@code 2 + 2}.
* <li>{@code {b: 2, a: b}}, result: {@code b + 2}.
* </ul>
*/
public static InliningOptimizer newInstance(InlineVariable... inlineVariables) {
return newInstance(ImmutableList.copyOf(inlineVariables));
}
/**
* Creates a new {@code InliningOptimizer}.
*
* @see #newInstance(InlineVariable...)
*/
public static InliningOptimizer newInstance(List<InlineVariable> inlineVariables) {
return newInstance(InliningOptions.newBuilder().build(), ImmutableList.copyOf(inlineVariables));
}
/**
* Creates a new {@code InliningOptimizer}.
*
* @see #newInstance(InlineVariable...)
* @param options {@link InliningOptions} to customize the inlining behavior with.
*/
public static InliningOptimizer newInstance(
InliningOptions options, InlineVariable... inlineVariables) {
return newInstance(options, ImmutableList.copyOf(inlineVariables));
}
/**
* Creates a new {@code InliningOptimizer}.
*
* @see #newInstance(InlineVariable...)
* @param options {@link InliningOptions} to customize the inlining behavior with.
*/
public static InliningOptimizer newInstance(
InliningOptions options, List<InlineVariable> inlineVariables) {
return new InliningOptimizer(options, ImmutableList.copyOf(inlineVariables));
}
@Override
public OptimizationResult optimize(CelAbstractSyntaxTree ast, Cel cel) {
CelMutableAst mutableAst = CelMutableAst.fromCelAst(ast);
for (InlineVariable inlineVariable : inlineVariables) {
ImmutableList<CelNavigableMutableExpr> inlinableExprs =
CelNavigableMutableAst.fromAst(mutableAst)
.getRoot()
.allNodes()
.filter(node -> canInline(node, inlineVariable.name()))
.collect(toImmutableList());
for (CelNavigableMutableExpr inlinableExpr : inlinableExprs) {
CelMutableAst inlineVariableAst = CelMutableAst.fromCelAst(inlineVariable.ast());
CelMutableExpr replacementExpr = inlineVariableAst.expr();
if (inlinableExpr.getKind().equals(Kind.SELECT)
&& inlinableExpr.expr().select().testOnly()) {
replacementExpr = rewritePresenceExpr(inlineVariable, replacementExpr);
}
mutableAst =
astMutator.replaceSubtree(
mutableAst,
CelMutableAst.of(replacementExpr, inlineVariableAst.source()),
inlinableExpr.id());
}
}
return OptimizationResult.create(astMutator.renumberIdsConsecutively(mutableAst).toParsedAst());
}
private static CelMutableExpr rewritePresenceExpr(
InlineVariable inlineVariable, CelMutableExpr replacementExpr) {
if (replacementExpr.getKind().equals(Kind.SELECT)) {
// Preserve testOnly property for Select replacements (has(A) -> has(B))
replacementExpr.select().setTestOnly(true);
return replacementExpr;
}
CelType replacementType =
inlineVariable
.ast()
.getType(replacementExpr.id())
.orElseThrow(() -> new NoSuchElementException("Type is not present."));
if (isSizerType(replacementType)) {
// has(X) -> X.size() != 0
return createNotEquals(
CelMutableExpr.ofCall(CelMutableCall.create(replacementExpr, "size")),
CelMutableExpr.ofConstant(CelConstant.ofValue(0)));
}
if (replacementType.isAssignableFrom(SimpleType.NULL_TYPE)) {
// has(X) -> X != null
// This covers well-known wrapper types
return createNotEquals(
replacementExpr, CelMutableExpr.ofConstant(CelConstant.ofValue(NullValue.NULL_VALUE)));
}
return getZeroValueExpr(replacementType, replacementExpr)
.map(zeroValue -> createNotEquals(replacementExpr, zeroValue))
.orElseThrow(
() ->
new IllegalArgumentException(
String.format(
"Unable to inline expression type %s into presence test",
replacementType.name())));
}
private static Optional<CelMutableExpr> getZeroValueExpr(
CelType type, CelMutableExpr replacementExpr) {
switch (type.kind()) {
case BOOL:
return Optional.of(CelMutableExpr.ofConstant(CelConstant.ofValue(false)));
case DOUBLE:
return Optional.of(CelMutableExpr.ofConstant(CelConstant.ofValue(0.0d)));
case INT:
return Optional.of(CelMutableExpr.ofConstant(CelConstant.ofValue(0)));
case UINT:
return Optional.of(CelMutableExpr.ofConstant(CelConstant.ofValue(UnsignedLong.ZERO)));
case TIMESTAMP:
return Optional.of(
CelMutableExpr.ofCall(
CelMutableCall.create(
"timestamp", CelMutableExpr.ofConstant(CelConstant.ofValue(0)))));
case DURATION:
return Optional.of(
CelMutableExpr.ofCall(
CelMutableCall.create(
"duration", CelMutableExpr.ofConstant(CelConstant.ofValue("0")))));
case STRUCT:
return Optional.of(
CelMutableExpr.ofStruct(
CelMutableStruct.create(
replacementExpr.struct().messageName(), new ArrayList<>())));
default:
return Optional.empty();
}
}
private static CelMutableExpr createNotEquals(CelMutableExpr left, CelMutableExpr right) {
return CelMutableExpr.ofCall(
CelMutableCall.create(Operator.NOT_EQUALS.getFunction(), left, right));
}
private static boolean isSizerType(CelType type) {
return type.kind().equals(CelKind.LIST)
|| type.kind().equals(CelKind.MAP)
|| type.equals(SimpleType.STRING)
|| type.equals(SimpleType.BYTES);
}
private static boolean canInline(CelNavigableMutableExpr node, String identifier) {
boolean matches = maybeToQualifiedName(node).map(name -> name.equals(identifier)).orElse(false);
if (!matches) {
return false;
}
for (CelNavigableMutableExpr p = node.parent().orElse(null);
p != null;
p = p.parent().orElse(null)) {
if (p.getKind() != Kind.COMPREHENSION) {
continue;
}
CelMutableComprehension comp = p.expr().comprehension();
boolean shadows =
Stream.of(comp.iterVar(), comp.iterVar2(), comp.accuVar()).anyMatch(identifier::equals);
if (shadows) {
return false;
}
}
return true;
}
private static Optional<String> maybeToQualifiedName(CelNavigableMutableExpr node) {
if (node.getKind().equals(Kind.IDENT)) {
return Optional.of(node.expr().ident().name());
}
if (node.getKind().equals(Kind.SELECT)) {
return node.children()
.findFirst()
.flatMap(InliningOptimizer::maybeToQualifiedName)
.map(operandName -> operandName + "." + node.expr().select().field());
}
return Optional.empty();
}
/** Represents a variable to be inlined. */
@AutoValue
public abstract static class InlineVariable {
public abstract String name();
public abstract CelAbstractSyntaxTree ast();
/**
* Creates a new {@link InlineVariable} with the given name and AST.
*
* <p>The name must be a simple identifier or a qualified name (e.g. "a.b.c") and cannot be an
* internal variable (starting with @).
*/
public static InlineVariable of(String name, CelAbstractSyntaxTree ast) {
if (name.startsWith("@")) {
throw new IllegalArgumentException("Internal variables cannot be inlined: " + name);
}
return new AutoValue_InliningOptimizer_InlineVariable(name, ast);
}
}
/** Options to configure how Inlining behaves. */
@AutoValue
public abstract static class InliningOptions {
public abstract int maxIterationLimit();
/** Builder for configuring the {@link InliningOptimizer.InliningOptions}. */
@AutoValue.Builder
public abstract static class Builder {
/**
* Limit the number of iteration while inlining variables. An exception is thrown if the
* iteration count exceeds the set value.
*/
public abstract InliningOptions.Builder maxIterationLimit(int value);
public abstract InliningOptimizer.InliningOptions build();
Builder() {}
}
/** Returns a new options builder with recommended defaults pre-configured. */
public static InliningOptimizer.InliningOptions.Builder newBuilder() {
return new AutoValue_InliningOptimizer_InliningOptions.Builder().maxIterationLimit(400);
}
InliningOptions() {}
}
private InliningOptimizer(
InliningOptions options, ImmutableList<InlineVariable> inlineVariables) {
this.inlineVariables = inlineVariables;
this.astMutator = AstMutator.newInstance(options.maxIterationLimit());
}
}