@@ -39,6 +39,18 @@ namespace ir {
39
39
namespace llvm {
40
40
namespace internal {
41
41
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
+
42
54
labm8::StatusOr<BasicBlockEntryExit> ProgramGraphBuilder::VisitBasicBlock (
43
55
const ::llvm::BasicBlock& block, const Function* functionMessage,
44
56
InstructionMap* instructions, ArgumentConsumerMap* argumentConsumers,
@@ -194,7 +206,7 @@ labm8::StatusOr<FunctionEntryExits> ProgramGraphBuilder::VisitFunction(
194
206
195
207
if (function.isDeclaration ()) {
196
208
Node* node = AddInstruction (" ; undefined function" , functionMessage);
197
- graph::AddScalarFeature (node, " full_text " , " " );
209
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( " " ) );
198
210
functionEntryExits.first = node;
199
211
functionEntryExits.second .push_back (node);
200
212
return functionEntryExits;
@@ -325,7 +337,7 @@ Node* ProgramGraphBuilder::AddLlvmInstruction(
325
337
const LlvmTextComponents text = textEncoder_.Encode (instruction);
326
338
Node* node = AddInstruction (text.opcode_name , function);
327
339
node->set_block (blockCount_);
328
- graph::AddScalarFeature (node, " full_text " , text.text );
340
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.text ) );
329
341
330
342
// Add profiling information features, if available.
331
343
uint64_t profTotalWeight;
@@ -347,7 +359,7 @@ Node* ProgramGraphBuilder::AddLlvmVariable(const ::llvm::Instruction* operand,
347
359
const LlvmTextComponents text = textEncoder_.Encode (operand);
348
360
Node* node = AddVariable (text.lhs_type , function);
349
361
node->set_block (blockCount_);
350
- graph::AddScalarFeature (node, " full_text " , text.lhs );
362
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.lhs ) );
351
363
352
364
return node;
353
365
}
@@ -357,7 +369,7 @@ Node* ProgramGraphBuilder::AddLlvmVariable(const ::llvm::Argument* argument,
357
369
const LlvmTextComponents text = textEncoder_.Encode (argument);
358
370
Node* node = AddVariable (text.lhs_type , function);
359
371
node->set_block (blockCount_);
360
- graph::AddScalarFeature (node, " full_text " , text.lhs );
372
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.lhs ) );
361
373
362
374
return node;
363
375
}
@@ -366,7 +378,7 @@ Node* ProgramGraphBuilder::AddLlvmConstant(const ::llvm::Constant* constant) {
366
378
const LlvmTextComponents text = textEncoder_.Encode (constant);
367
379
Node* node = AddConstant (text.lhs_type );
368
380
node->set_block (blockCount_);
369
- graph::AddScalarFeature (node, " full_text " , text.text );
381
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.text ) );
370
382
371
383
return node;
372
384
}
@@ -465,6 +477,17 @@ void ProgramGraphBuilder::Clear() {
465
477
programl::graph::ProgramGraphBuilder::Clear ();
466
478
}
467
479
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
+
468
491
} // namespace internal
469
492
} // namespace llvm
470
493
} // namespace ir
0 commit comments