@@ -39,6 +39,16 @@ 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 ()->mutable_features()->mutable_feature())["strings"]
47
+ .mutable_bytes_list()) {
48
+ // Add an empty
49
+ graph::AddScalarFeature (GetMutableRootNode (), " llvm_string" , AddString (" " ));
50
+ }
51
+
42
52
labm8::StatusOr<BasicBlockEntryExit> ProgramGraphBuilder::VisitBasicBlock (
43
53
const ::llvm::BasicBlock& block, const Function* functionMessage, InstructionMap* instructions,
44
54
ArgumentConsumerMap* argumentConsumers, std::vector<DataEdge>* dataEdgesToAdd) {
@@ -184,7 +194,7 @@ labm8::StatusOr<FunctionEntryExits> ProgramGraphBuilder::VisitFunction(
184
194
185
195
if (function.isDeclaration ()) {
186
196
Node* node = AddInstruction (" ; undefined function" , functionMessage);
187
- graph::AddScalarFeature (node, " full_text " , " " );
197
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( " " ) );
188
198
functionEntryExits.first = node;
189
199
functionEntryExits.second .push_back (node);
190
200
return functionEntryExits;
@@ -305,7 +315,7 @@ Node* ProgramGraphBuilder::AddLlvmInstruction(const ::llvm::Instruction* instruc
305
315
const LlvmTextComponents text = textEncoder_.Encode (instruction);
306
316
Node* node = AddInstruction (text.opcode_name , function);
307
317
node->set_block (blockCount_);
308
- graph::AddScalarFeature (node, " full_text " , text.text );
318
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.text ) );
309
319
310
320
// Add profiling information features, if available.
311
321
uint64_t profTotalWeight;
@@ -327,7 +337,7 @@ Node* ProgramGraphBuilder::AddLlvmVariable(const ::llvm::Instruction* operand,
327
337
const LlvmTextComponents text = textEncoder_.Encode (operand);
328
338
Node* node = AddVariable (text.lhs_type , function);
329
339
node->set_block (blockCount_);
330
- graph::AddScalarFeature (node, " full_text " , text.lhs );
340
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.lhs ) );
331
341
332
342
return node;
333
343
}
@@ -337,7 +347,7 @@ Node* ProgramGraphBuilder::AddLlvmVariable(const ::llvm::Argument* argument,
337
347
const LlvmTextComponents text = textEncoder_.Encode (argument);
338
348
Node* node = AddVariable (text.lhs_type , function);
339
349
node->set_block (blockCount_);
340
- graph::AddScalarFeature (node, " full_text " , text.lhs );
350
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.lhs ) );
341
351
342
352
return node;
343
353
}
@@ -346,7 +356,7 @@ Node* ProgramGraphBuilder::AddLlvmConstant(const ::llvm::Constant* constant) {
346
356
const LlvmTextComponents text = textEncoder_.Encode (constant);
347
357
Node* node = AddConstant (text.lhs_type );
348
358
node->set_block (blockCount_);
349
- graph::AddScalarFeature (node, " full_text " , text.text );
359
+ graph::AddScalarFeature (node, " llvm_string " , AddString ( text.text ) );
350
360
351
361
return node;
352
362
}
@@ -436,6 +446,27 @@ void ProgramGraphBuilder::Clear() {
436
446
programl::graph::ProgramGraphBuilder::Clear ();
437
447
}
438
448
449
+ Node* ProgramGraphBuilder::GetOrCreateType (const ::llvm::Type* type) {
450
+ auto it = types_.find (type);
451
+ if (it == types_.end ()) {
452
+ Node* node = AddLlvmType (type);
453
+ types_[type] = node;
454
+ return node;
455
+ }
456
+ return it->second ;
457
+ }
458
+
459
+ int32_t ProgramGraphBuilder::AddString (const string& text) {
460
+ auto it = stringsListPositions_.find (text);
461
+ if (it == stringsListPositions_.end ()) {
462
+ int32_t index = stringsListPositions_.size ();
463
+ stringsListPositions_[text] = index ;
464
+ stringsList_->add_value (text);
465
+ return index ;
466
+ }
467
+ return it->second ;
468
+ }
469
+
439
470
} // namespace internal
440
471
} // namespace llvm
441
472
} // namespace ir
0 commit comments