bool AddComputation(const string& name, HloComputation* computation,
LocTy name_loc);
- // The map from the instruction name to the instruction. This does not own the
- // instructions.
- std::unordered_map<string, HloInstruction*> instruction_pool_;
- std::unordered_map<string, HloComputation*> computation_pool_;
+ // The map from the instruction/computation name to the
+ // instruction/computation itself and it's location. This does not own the
+ // pointers.
+ std::unordered_map<string, std::pair<HloInstruction*, LocTy>>
+ instruction_pool_;
+ std::unordered_map<string, std::pair<HloComputation*, LocTy>>
+ computation_pool_;
HloLexer lexer_;
std::unique_ptr<HloModule> module_;
return false;
}
- HloInstruction* root =
- tensorflow::gtl::FindPtrOrNull(instruction_pool_, root_name);
+ std::pair<HloInstruction*, LocTy>* root_node =
+ tensorflow::gtl::FindOrNull(instruction_pool_, root_name);
// This means some instruction was marked as ROOT but we didn't find it in the
// pool, which should not happen.
- if (!root_name.empty() && root == nullptr) {
+ if (!root_name.empty() && root_node == nullptr) {
LOG(FATAL) << "instruction " << root_name
<< " was marked as ROOT but the parser has not seen it before";
}
+ HloInstruction* root = root_node == nullptr ? nullptr : root_node->first;
// Now root can be either an existing instruction or a nullptr. If it's a
// nullptr, the implementation of Builder will set the last instruction as
// root instruction.
if (!ParseName(&name)) {
return Error(loc, "expects a instruction name");
}
- HloInstruction* instr =
- tensorflow::gtl::FindPtrOrNull(instruction_pool_, name);
+ std::pair<HloInstruction*, LocTy>* instr =
+ tensorflow::gtl::FindOrNull(instruction_pool_, name);
if (!instr) {
return TokenError(
Printf("instruction '%s' is not defined", name.c_str()));
}
- instructions->push_back(instr);
+ instructions->push_back(instr->first);
} while (EatIfPresent(TokKind::kComma));
return ParseToken(TokKind::kRbrace,
if (!ParseName(&name)) {
return false;
}
- HloInstruction* instruction =
- tensorflow::gtl::FindPtrOrNull(instruction_pool_, name);
+ std::pair<HloInstruction*, LocTy>* instruction =
+ tensorflow::gtl::FindOrNull(instruction_pool_, name);
if (!instruction) {
return Error(loc, StrCat("instruction does not exist: ", name));
}
- operands->push_back(instruction);
+ operands->push_back(instruction->first);
} while (EatIfPresent(TokKind::kComma));
}
return ParseToken(TokKind::kRparen, "expects ')' at the end of operands");
if (!ParseName(&name)) {
return Error(loc, "expects computation name");
}
- *value = tensorflow::gtl::FindPtrOrNull(computation_pool_, name);
- if (*value == nullptr) {
+ std::pair<HloComputation*, LocTy>* computation =
+ tensorflow::gtl::FindOrNull(computation_pool_, name);
+ if (computation == nullptr) {
return Error(loc, StrCat("computation does not exist: ", name));
}
+ *value = computation->first;
return true;
}
bool HloParser::AddInstruction(const string& name, HloInstruction* instruction,
LocTy name_loc) {
- auto result = instruction_pool_.insert({name, instruction});
+ auto result = instruction_pool_.insert({name, {instruction, name_loc}});
if (!result.second) {
- return Error(name_loc, StrCat("instruction already exists: ", name));
+ Error(name_loc, StrCat("instruction already exists: ", name));
+ return Error(/*loc=*/result.first->second.second,
+ "instruction previously defined here");
}
return true;
}
bool HloParser::AddComputation(const string& name, HloComputation* computation,
LocTy name_loc) {
- auto result = computation_pool_.insert({name, computation});
+ auto result = computation_pool_.insert({name, {computation, name_loc}});
if (!result.second) {
- return Error(name_loc, StrCat("computation already exists: ", name));
+ Error(name_loc, StrCat("computation already exists: ", name));
+ return Error(/*loc=*/result.first->second.second,
+ "computation previously defined here");
}
return true;
}