Author: Aaron  Jomy
Date: 2026-08-22T09:06:35Z
New Revision: 2a9b623c95d6ea9e13acefa2a24473b7b87dc238

URL: 
https://github.com/llvm/llvm-project/commit/2a9b623c95d6ea9e13acefa2a24473b7b87dc238
DIFF: 
https://github.com/llvm/llvm-project/commit/2a9b623c95d6ea9e13acefa2a24473b7b87dc238.diff

LOG: [clang-repl] Honor `-emit-llvm` to print incremental IR (#217870)

clang-repl cannot currently emit the LLVM IR it generates. The
command-line emit actions are accepted but folded into
`EmitLLVMOnlyAction` (producing no output), and `frontend::EmitLLVM` is
rejected by the incremental frontend action when `-emit-llvm` is passed.
This prevents users from inspecting IR produced per-input PTU.

This patch teaches clang-repl to honor `-emit-llvm` by allowing the
IncrementalAction to accept `frontend::EmitLLVM` , making the driver
print each input's `llvm::Modul` to stdout instead of executing it,
similar to `clang -emit-llvm`. This enables a lit-level test to
FileCheck the IR produced by clang-repl and the effect of a statement on
it. Exposing this functionality beyond the driver level to allow library
consumers of `clang::Interpreter` to use it is a planned follow-up.

Added: 
    clang/test/Interpreter/emit-llvm.cpp

Modified: 
    clang/lib/Interpreter/IncrementalAction.cpp
    clang/lib/Interpreter/Interpreter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Interpreter/IncrementalAction.cpp 
b/clang/lib/Interpreter/IncrementalAction.cpp
index d22031c8fa893..85f00c36dd5aa 100644
--- a/clang/lib/Interpreter/IncrementalAction.cpp
+++ b/clang/lib/Interpreter/IncrementalAction.cpp
@@ -47,6 +47,7 @@ IncrementalAction::IncrementalAction(CompilerInstance 
&Instance,
         case frontend::EmitBC:
         case frontend::EmitObj:
         case frontend::PrintPreprocessedInput:
+        case frontend::EmitLLVM:
         case frontend::EmitLLVMOnly:
           Act.reset(new EmitLLVMOnlyAction(&LLVMCtx));
           break;

diff  --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 0536fdcd548a2..c9b127a360e5d 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -31,6 +31,7 @@
 #include "clang/Driver/Tool.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendAction.h"
+#include "clang/Frontend/FrontendOptions.h"
 #include "clang/Frontend/MultiplexConsumer.h"
 #include "clang/Frontend/TextDiagnosticBuffer.h"
 #include "clang/FrontendTool/Utils.h"
@@ -568,6 +569,12 @@ Interpreter::Parse(llvm::StringRef Code) {
 
   PartialTranslationUnit &LastPTU = IncrParser->RegisterPTU(*TuOrErr);
 
+  // Under -emit-llvm, print the module IR.
+  if (InitPTUSize && LastPTU.TheModule &&
+      getCompilerInstance()->getFrontendOpts().ProgramAction ==
+          frontend::EmitLLVM)
+    LastPTU.TheModule->print(llvm::outs(), /*AAW=*/nullptr);
+
   return LastPTU;
 }
 

diff  --git a/clang/test/Interpreter/emit-llvm.cpp 
b/clang/test/Interpreter/emit-llvm.cpp
new file mode 100644
index 0000000000000..96f18a6d0d34d
--- /dev/null
+++ b/clang/test/Interpreter/emit-llvm.cpp
@@ -0,0 +1,21 @@
+// REQUIRES: host-supports-jit
+//
+// -emit-llvm prints the IR of each input before running them.
+//
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -emit-llvm | FileCheck %s
+
+extern "C" int add(int a, int b) { return a + b; }
+// CHECK: define {{.*}}i32 @add(
+// CHECK: ret i32
+
+extern "C" int answer = 42;
+// CHECK: @answer = {{.*}}i32 42
+
+extern "C" int neg(int a) { return -a; }
+// CHECK: define {{.*}}i32 @neg(
+
+extern "C" int r = add(19, 23);
+// CHECK: @r = {{.*}}global i32
+// CHECK: call i32 @add(
+r
+// CHECK: (int) 42


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to