@@ -39,6 +39,18 @@ namespace ir {
3939namespace llvm {
4040namespace internal {
4141
42+ ProgramGraphBuilder::ProgramGraphBuilder (const ProgramGraphOptions& options)
43+ : programl::graph::ProgramGraphBuilder(),
44+ options_ (options),
45+ blockCount_(0 ),
46+ stringsList_((*GetMutableProgramGraph ()
47+ ->mutable_features()
48+ ->mutable_feature())["strings"]
49+ .mutable_bytes_list()) {
50+ // Add an empty string for the root node.
51+ graph::AddScalarFeature (GetMutableRootNode (), " llvm_string" , AddString (" " ));
52+ }
53+
4254labm8::StatusOr<BasicBlockEntryExit> ProgramGraphBuilder::VisitBasicBlock (
4355 const ::llvm::BasicBlock& block, const Function* functionMessage,
4456 InstructionMap* instructions, ArgumentConsumerMap* argumentConsumers,
@@ -194,7 +206,7 @@ labm8::StatusOr<FunctionEntryExits> ProgramGraphBuilder::VisitFunction(
194206
195207 if (function.isDeclaration ()) {
196208 Node* node = AddInstruction (" ; undefined function" , functionMessage);
197- graph::AddScalarFeature (node, " full_text " , " " );
209+ graph::AddScalarFeature (node, " llvm_string " , AddString ( " " ) );
198210 functionEntryExits.first = node;
199211 functionEntryExits.second .push_back (node);
200212 return functionEntryExits;
@@ -325,7 +337,7 @@ Node* ProgramGraphBuilder::AddLlvmInstruction(
325337 const LlvmTextComponents text = textEncoder_.Encode (instruction);
326338 Node* node = AddInstruction (text.opcode_name , function);
327339 node->set_block (blockCount_);
328- graph::AddScalarFeature (node, " full_text " , text.text );
340+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.text ) );
329341
330342 // Add profiling information features, if available.
331343 uint64_t profTotalWeight;
@@ -347,7 +359,7 @@ Node* ProgramGraphBuilder::AddLlvmVariable(const ::llvm::Instruction* operand,
347359 const LlvmTextComponents text = textEncoder_.Encode (operand);
348360 Node* node = AddVariable (text.lhs_type , function);
349361 node->set_block (blockCount_);
350- graph::AddScalarFeature (node, " full_text " , text.lhs );
362+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.lhs ) );
351363
352364 return node;
353365}
@@ -357,7 +369,7 @@ Node* ProgramGraphBuilder::AddLlvmVariable(const ::llvm::Argument* argument,
357369 const LlvmTextComponents text = textEncoder_.Encode (argument);
358370 Node* node = AddVariable (text.lhs_type , function);
359371 node->set_block (blockCount_);
360- graph::AddScalarFeature (node, " full_text " , text.lhs );
372+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.lhs ) );
361373
362374 return node;
363375}
@@ -366,7 +378,7 @@ Node* ProgramGraphBuilder::AddLlvmConstant(const ::llvm::Constant* constant) {
366378 const LlvmTextComponents text = textEncoder_.Encode (constant);
367379 Node* node = AddConstant (text.lhs_type );
368380 node->set_block (blockCount_);
369- graph::AddScalarFeature (node, " full_text " , text.text );
381+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.text ) );
370382
371383 return node;
372384}
@@ -465,6 +477,17 @@ void ProgramGraphBuilder::Clear() {
465477 programl::graph::ProgramGraphBuilder::Clear ();
466478}
467479
480+ int32_t ProgramGraphBuilder::AddString (const string& text) {
481+ auto it = stringsListPositions_.find (text);
482+ if (it == stringsListPositions_.end ()) {
483+ int32_t index = stringsListPositions_.size ();
484+ stringsListPositions_[text] = index;
485+ stringsList_->add_value (text);
486+ return index;
487+ }
488+ return it->second ;
489+ }
490+
468491} // namespace internal
469492} // namespace llvm
470493} // namespace ir
0 commit comments