Skip to content

Commit

Permalink
Emit DefineOwnById in bytecode backend
Browse files Browse the repository at this point in the history
Summary:
In BC lowering, keep the LiteralString intact for
`DefineOwnPropertyInst`. This ensures we can now use the new ById
variant in ISel.

Reviewed By: neildhar

Differential Revision: D67109052

fbshipit-source-id: d01fb6e4c091589bfb2fe64e9c8f18b6752d2680
  • Loading branch information
fbmal7 authored and facebook-github-bot committed Mar 5, 2025
1 parent 1da4d06 commit 46e70d3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/BCGen/HBC/BytecodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ static bool isIdOperand(const Instruction *I, unsigned idx) {
CASE_WITH_PROP_IDX(LoadPropertyInst);
CASE_WITH_PROP_IDX(LoadPropertyWithReceiverInst);
CASE_WITH_PROP_IDX(DefineNewOwnPropertyInst);
CASE_WITH_PROP_IDX(DefineOwnPropertyInst);
CASE_WITH_PROP_IDX(StorePropertyLooseInst);
CASE_WITH_PROP_IDX(StorePropertyStrictInst);
CASE_WITH_PROP_IDX(TryLoadGlobalPropertyInst);
Expand Down
40 changes: 30 additions & 10 deletions lib/BCGen/HBC/ISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,23 +927,43 @@ void HBCISel::generateDefineOwnPropertyInst(
auto valueReg = encodeValue(Inst->getStoredValue());
auto objReg = encodeValue(Inst->getObject());
Value *prop = Inst->getProperty();
bool isEnumerable = Inst->getIsEnumerable();

// If the property is a LiteralNumber, the property is enumerable, and it is a
// valid array index, it is coming from an array initialization and we will
// emit it as DefineOwnByIndex.
auto *numProp = llvh::dyn_cast<LiteralNumber>(prop);
if (numProp && isEnumerable) {
if (auto arrayIndex = numProp->convertToArrayIndex()) {
uint32_t index = arrayIndex.getValue();
if (index <= UINT8_MAX) {
BCFGen_->emitDefineOwnByIndex(objReg, valueReg, index);
} else {
BCFGen_->emitDefineOwnByIndexL(objReg, valueReg, index);
}
if (numProp) {
assert(
Inst->getIsEnumerable() &&
"Non-enumerable properties with literal keys should be handled by LoadConstants");
uint32_t index = numProp->convertToArrayIndex().getValue();
if (index <= UINT8_MAX) {
BCFGen_->emitDefineOwnByIndex(objReg, valueReg, index);
} else {
BCFGen_->emitDefineOwnByIndexL(objReg, valueReg, index);
}
return;
}

return;
if (auto *Lit = llvh::dyn_cast<LiteralString>(prop)) {
assert(
Inst->getIsEnumerable() &&
"Non-enumerable properties with literal keys should be handled by LoadConstants");
auto id = BCFGen_->getIdentifierID(Lit);
if (id <= UINT16_MAX) {
BCFGen_->emitDefineOwnById(
objReg,
valueReg,
acquirePropertyWriteCacheIndex(Lit->getValue()),
id);
} else {
BCFGen_->emitDefineOwnByIdLong(
objReg,
valueReg,
acquirePropertyWriteCacheIndex(Lit->getValue()),
id);
}
return;
}

// It is a register operand.
Expand Down
6 changes: 6 additions & 0 deletions lib/BCGen/HBC/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ bool LoadConstants::operandMustBeLiteral(Instruction *Inst, unsigned opIndex) {
if (SOP->getIsEnumerable() && LN->convertToArrayIndex().hasValue())
return true;
}

// LiteralStrings are optimized, when they are enumerable.
if (llvh::isa<LiteralString>(Inst->getOperand(opIndex)) &&
SOP->getIsEnumerable()) {
return true;
}
}

// DefineOwnPropertyInst's isEnumerable is a boolean constant.
Expand Down

0 comments on commit 46e70d3

Please sign in to comment.