7777
7878namespace llvm {
7979
80+ static const char *ToolName = " llvm-ir2vec" ;
81+
8082// Common option category for options shared between IR2Vec and MIR2Vec
8183static cl::OptionCategory CommonCategory (" Common Options" ,
8284 " Options applicable to both IR2Vec "
@@ -258,7 +260,8 @@ class IR2VecTool {
258260 // / Generate embeddings for the entire module
259261 void generateEmbeddings (raw_ostream &OS) const {
260262 if (!Vocab->isValid ()) {
261- OS << " Error: Vocabulary is not valid. IR2VecTool not initialized.\n " ;
263+ WithColor::error (errs (), ToolName)
264+ << " Vocabulary is not valid. IR2VecTool not initialized.\n " ;
262265 return ;
263266 }
264267
@@ -277,8 +280,8 @@ class IR2VecTool {
277280 assert (Vocab->isValid () && " Vocabulary is not valid" );
278281 auto Emb = Embedder::create (IR2VecEmbeddingKind, F, *Vocab);
279282 if (!Emb) {
280- OS << " Error: Failed to create embedder for function " << F. getName ( )
281- << " \n " ;
283+ WithColor::error ( errs (), ToolName )
284+ << " Failed to create embedder for function " << F. getName () << " \n " ;
282285 return ;
283286 }
284287
@@ -351,13 +354,14 @@ class MIR2VecTool {
351354public:
352355 explicit MIR2VecTool (MachineModuleInfo &MMI) : MMI(MMI) {}
353356
354- // / Initialize the MIR2Vec vocabulary
357+ // / Initialize MIR2Vec vocabulary
355358 bool initializeVocabulary (const Module &M) {
356359 MIR2VecVocabProvider Provider (MMI);
357360 auto VocabOrErr = Provider.getVocabulary (M);
358361 if (!VocabOrErr) {
359- errs () << " Error: Failed to load MIR2Vec vocabulary - "
360- << toString (VocabOrErr.takeError ()) << " \n " ;
362+ WithColor::error (errs (), ToolName)
363+ << " Failed to load MIR2Vec vocabulary - "
364+ << toString (VocabOrErr.takeError ()) << " \n " ;
361365 return false ;
362366 }
363367 Vocab = std::make_unique<MIRVocabulary>(std::move (*VocabOrErr));
@@ -367,7 +371,7 @@ class MIR2VecTool {
367371 // / Generate embeddings for all machine functions in the module
368372 void generateEmbeddings (const Module &M, raw_ostream &OS) const {
369373 if (!Vocab) {
370- OS << " Error: Vocabulary not initialized.\n " ;
374+ WithColor::error ( errs (), ToolName) << " Vocabulary not initialized.\n " ;
371375 return ;
372376 }
373377
@@ -377,7 +381,8 @@ class MIR2VecTool {
377381
378382 MachineFunction *MF = MMI.getMachineFunction (F);
379383 if (!MF) {
380- errs () << " Warning: No MachineFunction for " << F.getName () << " \n " ;
384+ WithColor::warning (errs (), ToolName)
385+ << " No MachineFunction for " << F.getName () << " \n " ;
381386 continue ;
382387 }
383388
@@ -388,13 +393,14 @@ class MIR2VecTool {
388393 // / Generate embeddings for a specific machine function
389394 void generateEmbeddings (MachineFunction &MF, raw_ostream &OS) const {
390395 if (!Vocab) {
391- OS << " Error: Vocabulary not initialized.\n " ;
396+ WithColor::error ( errs (), ToolName) << " Vocabulary not initialized.\n " ;
392397 return ;
393398 }
394399
395400 auto Emb = MIREmbedder::create (MIR2VecKind::Symbolic, MF, *Vocab);
396401 if (!Emb) {
397- errs () << " Error: Failed to create embedder for " << MF.getName () << " \n " ;
402+ WithColor::error (errs (), ToolName)
403+ << " Failed to create embedder for " << MF.getName () << " \n " ;
398404 return ;
399405 }
400406
@@ -456,7 +462,8 @@ int main(int argc, char **argv) {
456462 std::error_code EC;
457463 raw_fd_ostream OS (OutputFilename, EC);
458464 if (EC) {
459- errs () << " Error opening output file: " << EC.message () << " \n " ;
465+ WithColor::error (errs (), ToolName)
466+ << " opening output file: " << EC.message () << " \n " ;
460467 return 1 ;
461468 }
462469
@@ -472,13 +479,13 @@ int main(int argc, char **argv) {
472479 LLVMContext Context;
473480 std::unique_ptr<Module> M = parseIRFile (InputFilename, Err, Context);
474481 if (!M) {
475- Err.print (argv[ 0 ] , errs ());
482+ Err.print (ToolName , errs ());
476483 return 1 ;
477484 }
478485
479486 if (Error Err = processModule (*M, OS)) {
480487 handleAllErrors (std::move (Err), [&](const ErrorInfoBase &EIB) {
481- errs () << " Error: " << EIB.message () << " \n " ;
488+ WithColor::error ( errs (), ToolName) << EIB.message () << " \n " ;
482489 });
483490 return 1 ;
484491 }
@@ -499,7 +506,7 @@ int main(int argc, char **argv) {
499506
500507 auto MIR = createMIRParserFromFile (InputFilename, Err, Context);
501508 if (!MIR) {
502- Err.print (argv[ 0 ], WithColor::error ( errs (), argv[ 0 ] ));
509+ Err.print (ToolName, errs ());
503510 return 1 ;
504511 }
505512
@@ -511,7 +518,7 @@ int main(int argc, char **argv) {
511518 TheTriple.setTriple (sys::getDefaultTargetTriple ());
512519 auto TMOrErr = codegen::createTargetMachineForTriple (TheTriple.str ());
513520 if (!TMOrErr) {
514- Err.print (argv[ 0 ], WithColor::error ( errs (), argv[ 0 ] ));
521+ Err.print (ToolName, errs ());
515522 exit (1 );
516523 }
517524 TM = std::move (*TMOrErr);
@@ -520,14 +527,14 @@ int main(int argc, char **argv) {
520527
521528 std::unique_ptr<Module> M = MIR->parseIRModule (SetDataLayout);
522529 if (!M) {
523- Err.print (argv[ 0 ], WithColor::error ( errs (), argv[ 0 ] ));
530+ Err.print (ToolName, errs ());
524531 return 1 ;
525532 }
526533
527534 // Parse machine functions
528535 auto MMI = std::make_unique<MachineModuleInfo>(TM.get ());
529536 if (!MMI || MIR->parseMachineFunctions (*M, *MMI)) {
530- Err.print (argv[ 0 ], WithColor::error ( errs (), argv[ 0 ] ));
537+ Err.print (ToolName, errs ());
531538 return 1 ;
532539 }
533540
@@ -547,13 +554,15 @@ int main(int argc, char **argv) {
547554 // Process single function
548555 Function *F = M->getFunction (FunctionName);
549556 if (!F) {
550- errs () << " Error: Function '" << FunctionName << " ' not found\n " ;
557+ WithColor::error (errs (), ToolName)
558+ << " Function '" << FunctionName << " ' not found\n " ;
551559 return 1 ;
552560 }
553561
554562 MachineFunction *MF = MMI->getMachineFunction (*F);
555563 if (!MF) {
556- errs () << " Error: No MachineFunction for " << FunctionName << " \n " ;
564+ WithColor::error (errs (), ToolName)
565+ << " No MachineFunction for " << FunctionName << " \n " ;
557566 return 1 ;
558567 }
559568
0 commit comments