Skip to content

Commit

Permalink
Clarify error messages in the LIR parser
Browse files Browse the repository at this point in the history
Summary: Fix a format string and add some context to help debug failing tests.

Reviewed By: mengdilin

Differential Revision: D44593968

fbshipit-source-id: fe613165d5b53b55b195f6c1a885bec93bc0a2c2
  • Loading branch information
swtaarrs authored and facebook-github-bot committed Apr 5, 2023
1 parent 8128886 commit 5824310
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions Jit/lir/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,22 @@ static void expect(bool cond, const char* cur, const char* msg = "") {
JIT_LOG("Starting from %s", cur);
}

throw ParserException(fmt::format("Unable to parse - %s", msg));
throw ParserException(fmt::format("Unable to parse - {}", msg));
}

// Look up an item in the given map. Throw exception if doesn't exist.
template <typename Exc, typename M, typename K>
static auto& map_get_throw(M& map, const K& key) {
template <typename Exc, typename M, typename K, typename... FmtArgs>
static auto& map_get_throw(
M& map,
const K& key,
const char* format,
FmtArgs&&... format_args) {
auto it = map.find(key);
if (it == map.end()) {
throw Exc("Unable to parse - key not in map");
throw Exc(
"Unable to parse - key not in map: " +
fmt::format(
fmt::runtime(format), std::forward<FmtArgs>(format_args)...));
}
return it->second;
}
Expand Down Expand Up @@ -358,7 +365,8 @@ OperandBase::DataType Parser::getOperandDataType(
#undef TYPE_NAME_TO_DATA_TYPE
};

return map_get_throw<ParserException>(type_name_to_data_type, name);
return map_get_throw<ParserException>(
type_name_to_data_type, name, "DataType for {}", name);
}

Instruction::Opcode Parser::getInstrOpcode(const std::string& name) const {
Expand All @@ -369,7 +377,8 @@ Instruction::Opcode Parser::getInstrOpcode(const std::string& name) const {
#undef INSTR_NAME_TO_OPCODE
};

return map_get_throw<ParserException>(instr_name_to_opcode, name);
return map_get_throw<ParserException>(
instr_name_to_opcode, name, "Opcode for {}", name);
}

void Parser::parseInput(const Token& token, const char* code) {
Expand Down Expand Up @@ -415,8 +424,9 @@ void Parser::parseInput(const Token& token, const char* code) {
break;
}
case kId: {
std::string name(code, token.length);
uint64_t imm_addr = map_get_throw<ParserException>(
kSymbolMapping, std::string(code, token.length));
kSymbolMapping, name, "Symbol address for {}", name);
instr_->allocateImmediateInput(
reinterpret_cast<uint64_t>(imm_addr), OperandBase::kObject);
break;
Expand Down Expand Up @@ -456,7 +466,8 @@ void Parser::parseIndirect(
std::regex base_phys = std::regex("\\[(R[0-9A-Z]+):Object");
if (std::regex_search(token.begin(), token.end(), m, base_reg)) {
auto base_id = std::stoll(m.str(1).c_str(), nullptr, 0);
base = map_get_throw<ParserException>(output_index_map_, base_id);
base = map_get_throw<ParserException>(
output_index_map_, base_id, "Output id {}", base_id);
expected_length += m.length();
} else if (std::regex_search(token.begin(), token.end(), m, base_phys)) {
base = jit::codegen::PhyLocation::parse(m.str(1));
Expand All @@ -471,7 +482,8 @@ void Parser::parseIndirect(
bool index_re_success = false;
if (std::regex_search(token.begin(), token.end(), m, index_reg)) {
auto index_id = std::stoll(m.str(1).c_str(), nullptr, 0);
index = map_get_throw<ParserException>(output_index_map_, index_id);
index = map_get_throw<ParserException>(
output_index_map_, index_id, "Output id {}", index_id);
index_re_success = true;
// add 1 for space between base and index operands
expected_length += m.length() + 1;
Expand Down Expand Up @@ -509,15 +521,16 @@ void Parser::fixOperands() {
auto operand = pair.first;
int block_index = pair.second;

operand->setBasicBlock(
map_get_throw<ParserException>(block_index_map_, block_index));
operand->setBasicBlock(map_get_throw<ParserException>(
block_index_map_, block_index, "Block id {}", block_index));
}

for (auto& pair : instr_refs_) {
auto operand = pair.first;
int instr_index = pair.second;

auto instr = map_get_throw<ParserException>(output_index_map_, instr_index);
auto instr = map_get_throw<ParserException>(
output_index_map_, instr_index, "Output id {}", instr_index);
instr->output()->addUse(operand);
}
}
Expand All @@ -528,8 +541,8 @@ void Parser::connectBasicBlocks() {
for (auto& succ_pair : basic_block_succs_) {
BasicBlock* source_block = succ_pair.first;
int dest_block_id = succ_pair.second;
source_block->addSuccessor(
map_get_throw<ParserException>(block_index_map_, dest_block_id));
source_block->addSuccessor(map_get_throw<ParserException>(
block_index_map_, dest_block_id, "Block id {}", dest_block_id));
}
}

Expand Down

0 comments on commit 5824310

Please sign in to comment.