[clang] Reapply "[clang-repl] Honor -emit-llvm to print incremental IR" (PR #218735)
Vassil Vassilev via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 26 07:54:39 PDT 2026
https://github.com/vgvassilev updated https://github.com/llvm/llvm-project/pull/218735
>From 2b6e40305cb5a097128e3d5250341c49b5fdafce Mon Sep 17 00:00:00 2001
From: Aaron Jomy <aaronjomyjoseph at gmail.com>
Date: Sun, 23 Aug 2026 15:51:42 +0200
Subject: [PATCH] Reapply "[clang-repl] Honor -emit-llvm to print incremental
IR"
---
clang/lib/Interpreter/IncrementalAction.cpp | 1 +
clang/lib/Interpreter/Interpreter.cpp | 7 +++++++
clang/test/Interpreter/emit-llvm.cpp | 21 +++++++++++++++++++++
3 files changed, 29 insertions(+)
create mode 100644 clang/test/Interpreter/emit-llvm.cpp
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 ef1ca31538352..092f3ede771f6 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"
@@ -581,6 +582,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..39d1dc47c9998
--- /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
More information about the cfe-commits
mailing list