From 52ca99ba1295d2b66ae45c4da641c25dfb701ada Mon Sep 17 00:00:00 2001 From: svkeerthy Date: Fri, 17 Oct 2025 23:25:44 +0000 Subject: [PATCH] Use colored error messages --- .../tools/llvm-ir2vec/embeddings-flowaware.ll | 2 +- .../tools/llvm-ir2vec/embeddings-symbolic.ll | 2 +- .../tools/llvm-ir2vec/embeddings-symbolic.mir | 2 +- llvm/test/tools/llvm-ir2vec/error-handling.ll | 2 +- .../test/tools/llvm-ir2vec/error-handling.mir | 8 ++-- llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp | 47 +++++++++++-------- 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/llvm/test/tools/llvm-ir2vec/embeddings-flowaware.ll b/llvm/test/tools/llvm-ir2vec/embeddings-flowaware.ll index b2362f83caf4f..ade228d7a3665 100644 --- a/llvm/test/tools/llvm-ir2vec/embeddings-flowaware.ll +++ b/llvm/test/tools/llvm-ir2vec/embeddings-flowaware.ll @@ -49,7 +49,7 @@ entry: ; CHECK-FUNC-LEVEL-ABC: Function: abc ; CHECK-FUNC-LEVEL-NEXT-ABC: [ 3630.00 3672.00 3714.00 ] -; CHECK-FUNC-DEF: Error: Function 'def' not found +; CHECK-FUNC-DEF: error: Function 'def' not found ; CHECK-BB-LEVEL: Function: abc ; CHECK-BB-LEVEL-NEXT: entry: [ 3630.00 3672.00 3714.00 ] diff --git a/llvm/test/tools/llvm-ir2vec/embeddings-symbolic.ll b/llvm/test/tools/llvm-ir2vec/embeddings-symbolic.ll index f9aa10817e360..9d60e12e8ab90 100644 --- a/llvm/test/tools/llvm-ir2vec/embeddings-symbolic.ll +++ b/llvm/test/tools/llvm-ir2vec/embeddings-symbolic.ll @@ -49,7 +49,7 @@ entry: ; CHECK-FUNC-LEVEL-ABC: Function: abc ; CHECK-FUNC-LEVEL-NEXT-ABC: [ 878.00 889.00 900.00 ] -; CHECK-FUNC-DEF: Error: Function 'def' not found +; CHECK-FUNC-DEF: error: Function 'def' not found ; CHECK-BB-LEVEL: Function: abc ; CHECK-BB-LEVEL-NEXT: entry: [ 878.00 889.00 900.00 ] diff --git a/llvm/test/tools/llvm-ir2vec/embeddings-symbolic.mir b/llvm/test/tools/llvm-ir2vec/embeddings-symbolic.mir index e5f78bfd2090e..ef835fe302f2e 100644 --- a/llvm/test/tools/llvm-ir2vec/embeddings-symbolic.mir +++ b/llvm/test/tools/llvm-ir2vec/embeddings-symbolic.mir @@ -67,7 +67,7 @@ body: | # CHECK-FUNC-LEVEL-ADD-NEXT: Function vector: [ 26.50 27.10 27.70 ] # CHECK-FUNC-LEVEL-ADD-NOT: simple_function -# CHECK-FUNC-MISSING: Error: Function 'missing_function' not found +# CHECK-FUNC-MISSING: error: Function 'missing_function' not found # CHECK-BB-LEVEL: MIR2Vec embeddings for machine function add_function: # CHECK-BB-LEVEL-NEXT: Basic block vectors: diff --git a/llvm/test/tools/llvm-ir2vec/error-handling.ll b/llvm/test/tools/llvm-ir2vec/error-handling.ll index b944ea0eaae8b..8e9e45513434c 100644 --- a/llvm/test/tools/llvm-ir2vec/error-handling.ll +++ b/llvm/test/tools/llvm-ir2vec/error-handling.ll @@ -10,4 +10,4 @@ entry: } ; CHECK-NO-VOCAB: error: IR2Vec vocabulary file path not specified; You may need to set it using --ir2vec-vocab-path -; CHECK-FUNC-NOT-FOUND: Error: Function 'nonexistent' not found +; CHECK-FUNC-NOT-FOUND: error: Function 'nonexistent' not found diff --git a/llvm/test/tools/llvm-ir2vec/error-handling.mir b/llvm/test/tools/llvm-ir2vec/error-handling.mir index 154078c18d647..caec454c9c9d5 100644 --- a/llvm/test/tools/llvm-ir2vec/error-handling.mir +++ b/llvm/test/tools/llvm-ir2vec/error-handling.mir @@ -31,11 +31,11 @@ body: | $eax = COPY %0 RET 0, $eax -# CHECK-NO-VOCAB: Error: Failed to load MIR2Vec vocabulary - MIR2Vec vocabulary file path not specified; set it using --mir2vec-vocab-path +# CHECK-NO-VOCAB: error: Failed to load MIR2Vec vocabulary - MIR2Vec vocabulary file path not specified; set it using --mir2vec-vocab-path -# CHECK-VOCAB-NOT-FOUND: Error: Failed to load MIR2Vec vocabulary +# CHECK-VOCAB-NOT-FOUND: error: Failed to load MIR2Vec vocabulary # CHECK-VOCAB-NOT-FOUND: No such file or directory -# CHECK-INVALID-VOCAB: Error: Failed to load MIR2Vec vocabulary - Missing 'Opcodes' section in vocabulary file +# CHECK-INVALID-VOCAB: error: Failed to load MIR2Vec vocabulary - Missing 'Opcodes' section in vocabulary file -# CHECK-FUNC-NOT-FOUND: Error: Function 'nonexistent_function' not found +# CHECK-FUNC-NOT-FOUND: error: Function 'nonexistent_function' not found diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp index c41cf20539c0d..a723d379129e6 100644 --- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp +++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp @@ -77,6 +77,8 @@ namespace llvm { +static const char *ToolName = "llvm-ir2vec"; + // Common option category for options shared between IR2Vec and MIR2Vec static cl::OptionCategory CommonCategory("Common Options", "Options applicable to both IR2Vec " @@ -258,7 +260,8 @@ class IR2VecTool { /// Generate embeddings for the entire module void generateEmbeddings(raw_ostream &OS) const { if (!Vocab->isValid()) { - OS << "Error: Vocabulary is not valid. IR2VecTool not initialized.\n"; + WithColor::error(errs(), ToolName) + << "Vocabulary is not valid. IR2VecTool not initialized.\n"; return; } @@ -277,8 +280,8 @@ class IR2VecTool { assert(Vocab->isValid() && "Vocabulary is not valid"); auto Emb = Embedder::create(IR2VecEmbeddingKind, F, *Vocab); if (!Emb) { - OS << "Error: Failed to create embedder for function " << F.getName() - << "\n"; + WithColor::error(errs(), ToolName) + << "Failed to create embedder for function " << F.getName() << "\n"; return; } @@ -351,13 +354,14 @@ class MIR2VecTool { public: explicit MIR2VecTool(MachineModuleInfo &MMI) : MMI(MMI) {} - /// Initialize the MIR2Vec vocabulary + /// Initialize MIR2Vec vocabulary bool initializeVocabulary(const Module &M) { MIR2VecVocabProvider Provider(MMI); auto VocabOrErr = Provider.getVocabulary(M); if (!VocabOrErr) { - errs() << "Error: Failed to load MIR2Vec vocabulary - " - << toString(VocabOrErr.takeError()) << "\n"; + WithColor::error(errs(), ToolName) + << "Failed to load MIR2Vec vocabulary - " + << toString(VocabOrErr.takeError()) << "\n"; return false; } Vocab = std::make_unique(std::move(*VocabOrErr)); @@ -367,7 +371,7 @@ class MIR2VecTool { /// Generate embeddings for all machine functions in the module void generateEmbeddings(const Module &M, raw_ostream &OS) const { if (!Vocab) { - OS << "Error: Vocabulary not initialized.\n"; + WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n"; return; } @@ -377,7 +381,8 @@ class MIR2VecTool { MachineFunction *MF = MMI.getMachineFunction(F); if (!MF) { - errs() << "Warning: No MachineFunction for " << F.getName() << "\n"; + WithColor::warning(errs(), ToolName) + << "No MachineFunction for " << F.getName() << "\n"; continue; } @@ -388,13 +393,14 @@ class MIR2VecTool { /// Generate embeddings for a specific machine function void generateEmbeddings(MachineFunction &MF, raw_ostream &OS) const { if (!Vocab) { - OS << "Error: Vocabulary not initialized.\n"; + WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n"; return; } auto Emb = MIREmbedder::create(MIR2VecKind::Symbolic, MF, *Vocab); if (!Emb) { - errs() << "Error: Failed to create embedder for " << MF.getName() << "\n"; + WithColor::error(errs(), ToolName) + << "Failed to create embedder for " << MF.getName() << "\n"; return; } @@ -456,7 +462,8 @@ int main(int argc, char **argv) { std::error_code EC; raw_fd_ostream OS(OutputFilename, EC); if (EC) { - errs() << "Error opening output file: " << EC.message() << "\n"; + WithColor::error(errs(), ToolName) + << "opening output file: " << EC.message() << "\n"; return 1; } @@ -472,13 +479,13 @@ int main(int argc, char **argv) { LLVMContext Context; std::unique_ptr M = parseIRFile(InputFilename, Err, Context); if (!M) { - Err.print(argv[0], errs()); + Err.print(ToolName, errs()); return 1; } if (Error Err = processModule(*M, OS)) { handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EIB) { - errs() << "Error: " << EIB.message() << "\n"; + WithColor::error(errs(), ToolName) << EIB.message() << "\n"; }); return 1; } @@ -499,7 +506,7 @@ int main(int argc, char **argv) { auto MIR = createMIRParserFromFile(InputFilename, Err, Context); if (!MIR) { - Err.print(argv[0], WithColor::error(errs(), argv[0])); + Err.print(ToolName, errs()); return 1; } @@ -511,7 +518,7 @@ int main(int argc, char **argv) { TheTriple.setTriple(sys::getDefaultTargetTriple()); auto TMOrErr = codegen::createTargetMachineForTriple(TheTriple.str()); if (!TMOrErr) { - Err.print(argv[0], WithColor::error(errs(), argv[0])); + Err.print(ToolName, errs()); exit(1); } TM = std::move(*TMOrErr); @@ -520,14 +527,14 @@ int main(int argc, char **argv) { std::unique_ptr M = MIR->parseIRModule(SetDataLayout); if (!M) { - Err.print(argv[0], WithColor::error(errs(), argv[0])); + Err.print(ToolName, errs()); return 1; } // Parse machine functions auto MMI = std::make_unique(TM.get()); if (!MMI || MIR->parseMachineFunctions(*M, *MMI)) { - Err.print(argv[0], WithColor::error(errs(), argv[0])); + Err.print(ToolName, errs()); return 1; } @@ -547,13 +554,15 @@ int main(int argc, char **argv) { // Process single function Function *F = M->getFunction(FunctionName); if (!F) { - errs() << "Error: Function '" << FunctionName << "' not found\n"; + WithColor::error(errs(), ToolName) + << "Function '" << FunctionName << "' not found\n"; return 1; } MachineFunction *MF = MMI->getMachineFunction(*F); if (!MF) { - errs() << "Error: No MachineFunction for " << FunctionName << "\n"; + WithColor::error(errs(), ToolName) + << "No MachineFunction for " << FunctionName << "\n"; return 1; }