[llvm-branch-commits] [llvm] [IR2Vec][llvm-ir2vec] Add support for reading from stdin (PR #149213)
S. VenkataKeerthy via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Jul 16 16:46:29 PDT 2025
https://github.com/svkeerthy updated https://github.com/llvm/llvm-project/pull/149213
>From 1e2226100f1068b27e96766bd69e0876a2a98663 Mon Sep 17 00:00:00 2001
From: svkeerthy <venkatakeerthy at google.com>
Date: Wed, 16 Jul 2025 22:01:47 +0000
Subject: [PATCH] support-stdin-input-llvm-ir2vec
---
llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp | 27 ++++++++++++++++++++------
1 file changed, 21 insertions(+), 6 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
index c9e2c7c713e18..3e6cb4b64fde5 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
+++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
@@ -38,6 +38,7 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
@@ -48,10 +49,10 @@ namespace ir2vec {
static cl::OptionCategory IR2VecToolCategory("IR2Vec Tool Options");
-static cl::opt<std::string> InputFilename(cl::Positional,
- cl::desc("<input bitcode file>"),
- cl::Required,
- cl::cat(IR2VecToolCategory));
+static cl::opt<std::string>
+ InputFilename(cl::Positional,
+ cl::desc("<input bitcode file or '-' for stdin>"),
+ cl::init("-"), cl::cat(IR2VecToolCategory));
static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"),
@@ -283,10 +284,24 @@ int main(int argc, char **argv) {
if (Mode == TripletMode && Level.getNumOccurrences() > 0)
errs() << "Warning: --level option is ignored in triplet mode\n";
- // Parse the input LLVM IR file
+ // Parse the input LLVM IR file or stdin
SMDiagnostic Err;
LLVMContext Context;
- std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
+ std::unique_ptr<Module> M;
+
+ if (InputFilename == "-") {
+ // Read from stdin
+ auto StdinBuffer = MemoryBuffer::getSTDIN();
+ if (std::error_code EC = StdinBuffer.getError()) {
+ errs() << "Error reading from stdin: " << EC.message() << "\n";
+ return 1;
+ }
+ M = parseIR(StdinBuffer.get()->getMemBufferRef(), Err, Context);
+ } else {
+ // Read from file
+ M = parseIRFile(InputFilename, Err, Context);
+ }
+
if (!M) {
Err.print(argv[0], errs());
return 1;
More information about the llvm-branch-commits
mailing list