|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +#include "cinderx/Jit/hir/sink_primitive_box.h" |
| 4 | + |
| 5 | +#include "cinderx/Jit/hir/hir.h" |
| 6 | + |
| 7 | +#include <unordered_map> |
| 8 | +#include <unordered_set> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +namespace cinderx::jit::hir { |
| 12 | + |
| 13 | +void SinkPrimitiveBox::run(Function& func) { |
| 14 | + // Registers that escape as a real PyObject, i.e. appear as a data operand of |
| 15 | + // some consuming instruction. UseType is excluded: it is a no-op type |
| 16 | + // assertion (it keeps a GuardType alive), not a real consumer, so a box used |
| 17 | + // only by UseType and deopt frame state does not actually escape. |
| 18 | + std::unordered_set<Register*> escapes; |
| 19 | + for (auto& block : func.cfg.blocks) { |
| 20 | + for (Instr& instr : block) { |
| 21 | + if (instr.isUseType()) { |
| 22 | + continue; |
| 23 | + } |
| 24 | + for (std::size_t i = 0, n = instr.numOperands(); i < n; ++i) { |
| 25 | + escapes.insert(instr.getOperand(i)); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Map each sinkable box's result to its unboxed source value, remember the |
| 31 | + // box instructions, and collect the UseType assertions on those boxes (which |
| 32 | + // become meaningless once the box is gone). |
| 33 | + std::unordered_map<Register*, Register*> sink_map; |
| 34 | + std::vector<Instr*> dead_instrs; |
| 35 | + for (auto& block : func.cfg.blocks) { |
| 36 | + for (Instr& instr : block) { |
| 37 | + if (!instr.isPrimitiveBox()) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + auto& box = static_cast<PrimitiveBox&>(instr); |
| 41 | + // Limited to floats for now: deopt re-boxes a CDouble via PyFloat. |
| 42 | + if (!(box.type() <= TCDouble)) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + if (!escapes.contains(box.output())) { |
| 46 | + sink_map.emplace(box.output(), box.value()); |
| 47 | + dead_instrs.push_back(&instr); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (sink_map.empty()) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Drop UseType assertions on sunk boxes before rewriting, so we don't rewrite |
| 57 | + // them to a primitive-typed register (which would be type-inconsistent). |
| 58 | + for (auto& block : func.cfg.blocks) { |
| 59 | + for (Instr& instr : block) { |
| 60 | + if (instr.isUseType() && sink_map.contains(instr.getOperand(0))) { |
| 61 | + dead_instrs.push_back(&instr); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Rewrite the boxes' remaining (frame-state only) uses to the unboxed value. |
| 67 | + // The deopt machinery records the unboxed value's kind and re-boxes it if a |
| 68 | + // deopt fires, so the box is no longer needed on the fast path. |
| 69 | + for (auto& block : func.cfg.blocks) { |
| 70 | + for (Instr& instr : block) { |
| 71 | + instr.visitUses([&](Register*& reg) { |
| 72 | + auto it = sink_map.find(reg); |
| 73 | + if (it != sink_map.end()) { |
| 74 | + reg = it->second; |
| 75 | + } |
| 76 | + return true; |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // The boxes (and their UseType assertions) now have no uses. |
| 82 | + for (Instr* instr : dead_instrs) { |
| 83 | + instr->unlink(); |
| 84 | + delete instr; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace cinderx::jit::hir |
0 commit comments