Skip to content

Commit 6a5dc12

Browse files
Grab identifiers from REPLACE_OPCODE_IF_EVALUATES_PURE
1 parent 74a0208 commit 6a5dc12

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

Tools/cases_generator/optimizer_generator.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,38 @@ def emit_stackref_override(
195195
self.out.emit("_stackref ")
196196
return True
197197

198+
199+
def replace_opcode_if_evaluates_pure_identifiers(uop: Uop) -> list[str]:
200+
token = None
201+
iterator = uop.body.tokens()
202+
for token in iterator:
203+
if token.kind == "IDENTIFIER" and token.text == "REPLACE_OPCODE_IF_EVALUATES_PURE":
204+
break
205+
assert token is not None
206+
assert token.kind == "IDENTIFIER" and token.text == "REPLACE_OPCODE_IF_EVALUATES_PURE", uop.name
207+
assert next(iterator).kind == "LPAREN"
208+
idents = []
209+
for token in iterator:
210+
if token.kind == "RPAREN":
211+
break
212+
if token.kind == "IDENTIFIER":
213+
idents.append(token.text)
214+
return idents
215+
216+
198217
def write_uop_pure_evaluation_region_header(
199218
uop: Uop,
219+
override: Uop,
200220
out: CWriter,
201221
stack: Stack,
202222
) -> None:
203223
emitter = OptimizerConstantEmitter(out, {}, uop)
204224
emitter.emit("if (\n")
205-
assert len(uop.stack.inputs) > 0, "Pure operations must have at least 1 input"
206-
for inp in uop.stack.inputs[:-1]:
207-
emitter.emit(f"sym_is_safe_const(ctx, {inp.name}) &&\n")
208-
emitter.emit(f"sym_is_safe_const(ctx, {uop.stack.inputs[-1].name})\n")
225+
input_identifiers = replace_opcode_if_evaluates_pure_identifiers(override)
226+
assert len(input_identifiers) > 0, "Pure operations must have at least 1 input"
227+
for inp in input_identifiers[:-1]:
228+
emitter.emit(f"sym_is_safe_const(ctx, {inp}) &&\n")
229+
emitter.emit(f"sym_is_safe_const(ctx, {input_identifiers[-1]})\n")
209230
emitter.emit(') {\n')
210231
# Declare variables, before they are shadowed.
211232
for inp in uop.stack.inputs:
@@ -280,7 +301,7 @@ def write_uop(
280301
var.in_local = False
281302
replace_opcode_if_evaluates_pure = uop_variable_used(override, "REPLACE_OPCODE_IF_EVALUATES_PURE")
282303
if replace_opcode_if_evaluates_pure:
283-
write_uop_pure_evaluation_region_header(uop, out, stack)
304+
write_uop_pure_evaluation_region_header(uop, override, out, stack)
284305
out.start_line()
285306
emitter = OptimizerEmitter(out, {})
286307
_, storage = emitter.emit_tokens(override, storage, inst=None, emit_braces=False)

0 commit comments

Comments
 (0)