Skip to content

Commit 06132c8

Browse files
committed
Refactor: Improve logic and remove hardcoded Clang version
1 parent a50d7d3 commit 06132c8

File tree

5 files changed

+110
-27
lines changed

5 files changed

+110
-27
lines changed

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class Interpreter {
152152
llvm::Error Undo(unsigned N = 1);
153153

154154
/// Link a dynamic library
155-
llvm::Error LoadDynamicLibrary(const char *name, bool UseEPC = false);
155+
llvm::Error LoadDynamicLibrary(const char *name);
156156

157157
/// \returns the \c ExecutorAddr of a \c GlobalDecl. This interface uses
158158
/// the CodeGenModule's internal mangling cache to avoid recomputing the

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -718,20 +718,13 @@ llvm::Error Interpreter::Undo(unsigned N) {
718718
return llvm::Error::success();
719719
}
720720

721-
llvm::Error Interpreter::LoadDynamicLibrary(const char *name, bool UseEPC) {
721+
llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
722722
auto EE = getExecutionEngine();
723723
if (!EE)
724724
return EE.takeError();
725725

726-
auto &DL = EE->getDataLayout();
727-
if (UseEPC) {
728-
if (auto DLSG = llvm::orc::EPCDynamicLibrarySearchGenerator::Load(
729-
EE->getExecutionSession(), name))
730-
EE->getMainJITDylib().addGenerator(std::move(*DLSG));
731-
else
732-
return DLSG.takeError();
733-
} else if (auto DLSG = llvm::orc::DynamicLibrarySearchGenerator::Load(
734-
name, DL.getGlobalPrefix()))
726+
if (auto DLSG = llvm::orc::EPCDynamicLibrarySearchGenerator::Load(
727+
EE->getExecutionSession(), name))
735728
EE->getMainJITDylib().addGenerator(std::move(*DLSG));
736729
else
737730
return DLSG.takeError();

clang/lib/Interpreter/RemoteJITUtils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
//
9+
// FIXME: Unify this code with similar functionality in llvm-jitlink.
10+
//
11+
//===----------------------------------------------------------------------===//
812

913
#include "clang/Interpreter/RemoteJITUtils.h"
1014

@@ -260,4 +264,4 @@ connectTCPSocket(StringRef NetworkAddress, bool UseSharedMemory,
260264
std::make_unique<DynamicThreadPoolTaskDispatcher>(std::nullopt),
261265
std::move(S), *SockFD, *SockFD);
262266
#endif
263-
}
267+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// REQUIRES: host-supports-jit
2+
3+
// RUN: cat %s | clang-repl -oop-executor -orc-runtime | FileCheck %s
4+
5+
extern "C" int printf(const char *, ...);
6+
7+
int intVar = 0;
8+
double doubleVar = 3.14;
9+
%undo
10+
double doubleVar = 2.71;
11+
12+
auto r1 = printf("intVar = %d\n", intVar);
13+
// CHECK: intVar = 0
14+
auto r2 = printf("doubleVar = %.2f\n", doubleVar);
15+
// CHECK: doubleVar = 2.71
16+
17+
// Test redefinition with inline and static functions.
18+
int add(int a, int b, int c) { return a + b + c; }
19+
%undo // Revert to the initial version of add
20+
inline int add(int a, int b) { return a + b; }
21+
22+
auto r3 = printf("add(1, 2) = %d\n", add(1, 2));
23+
// CHECK-NEXT: add(1, 2) = 3
24+
25+
// Test inline and lambda functions with variations.
26+
inline int square(int x) { return x * x; }
27+
auto lambdaSquare = [](int x) { return x * x; };
28+
auto lambdaMult = [](int a, int b) { return a * b; };
29+
30+
auto r4 = printf("square(4) = %d\n", square(4));
31+
// CHECK-NEXT: square(4) = 16
32+
auto lambda_r1 = printf("lambdaSquare(5) = %d\n", lambdaSquare(5));
33+
// CHECK-NEXT: lambdaSquare(5) = 25
34+
auto lambda_r2 = printf("lambdaMult(2, 3) = %d\n", lambdaMult(2, 3));
35+
// CHECK-NEXT: lambdaMult(2, 3) = 6
36+
37+
%undo // Undo previous lambda assignments
38+
auto lambda_r3 = lambdaMult(3, 4); // Should fail or revert to the original lambda
39+
40+
// Test weak and strong symbol linkage.
41+
int __attribute__((weak)) weakFunc() { return 42; }
42+
int strongFunc() { return 100; }
43+
%undo // Revert the weak function
44+
45+
auto r5 = printf("weakFunc() = %d\n", weakFunc());
46+
// CHECK: weakFunc() = 42
47+
auto r6 = printf("strongFunc() = %d\n", strongFunc());
48+
// CHECK-NEXT: strongFunc() = 100
49+
50+
// Weak variable linkage with different types.
51+
int varA = 20;
52+
static __typeof(varA) weakVarA __attribute__((__weakref__("varA")));
53+
char charVar = 'c';
54+
static __typeof(charVar) weakCharVar __attribute__((__weakref__("charVar")));
55+
auto r7 = printf("weakVarA = %d\n", weakVarA);
56+
// CHECK: weakVarA = 20
57+
auto r8 = printf("weakCharVar = %c\n", weakCharVar);
58+
// CHECK-NEXT: weakCharVar = c
59+
60+
// Test complex lambdas with captures.
61+
int captureVar = 5;
62+
auto captureLambda = [](int x) { return x + captureVar; };
63+
int result1 = captureLambda(10);
64+
%undo // Undo capture lambda
65+
66+
auto r9 = printf("captureLambda(10) = %d\n", result1);
67+
// CHECK: captureLambda(10) = 15
68+
69+
// Multiline statement test with arithmetic operations.
70+
int sum = \
71+
5 + \
72+
10;
73+
int prod = sum * 2;
74+
auto r10 = printf("sum = %d, prod = %d\n", sum, prod);
75+
// CHECK: sum = 15, prod = 30
76+
77+
// Test multiline functions and macro behavior.
78+
#define MULTIPLY(a, b) ((a) * (b))
79+
80+
int complexFunc(int x) \
81+
{ \
82+
return MULTIPLY(x, 2) + x; \
83+
}
84+
85+
auto r11 = printf("complexFunc(5) = %d\n", complexFunc(5));
86+
// CHECK: complexFunc(5) = 15
87+
88+
%quit

clang/tools/clang-repl/ClangRepl.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "clang/Interpreter/RemoteJITUtils.h"
1414

1515
#include "clang/Basic/Diagnostic.h"
16+
#include "clang/Basic/Version.h"
17+
#include "clang/Config/config.h"
1618
#include "clang/Frontend/CompilerInstance.h"
1719
#include "clang/Frontend/FrontendDiagnostic.h"
1820
#include "clang/Interpreter/CodeCompletion.h"
@@ -62,7 +64,7 @@ static llvm::cl::opt<std::string> OOPExecutorConnect(
6264
llvm::cl::value_desc("<hostname>:<port>"));
6365
static llvm::cl::opt<std::string>
6466
OrcRuntimePath("orc-runtime", llvm::cl::desc("Path to the ORC runtime"),
65-
llvm::cl::cat(OOPCategory));
67+
llvm::cl::ValueOptional, llvm::cl::cat(OOPCategory));
6668
static llvm::cl::opt<bool> UseSharedMemory(
6769
"use-shared-memory",
6870
llvm::cl::desc("Use shared memory to transfer generated code and data"),
@@ -98,22 +100,21 @@ static llvm::Error sanitizeOopArguments(const char *ArgV0) {
98100
llvm::inconvertibleErrorCode());
99101
}
100102

101-
// Out-of-process executors must run with the ORC runtime for destructor
102-
// support.
103+
// Out-of-process executors require the ORC runtime.
103104
if (OrcRuntimePath.empty() && (OOPExecutor.getNumOccurrences() ||
104105
OOPExecutorConnect.getNumOccurrences())) {
105-
llvm::SmallString<256> OrcPath(llvm::sys::fs::getMainExecutable(
106+
llvm::SmallString<256> BasePath(llvm::sys::fs::getMainExecutable(
106107
ArgV0, reinterpret_cast<void *>(&sanitizeOopArguments)));
107-
llvm::sys::path::remove_filename(OrcPath); // Remove clang-repl filename.
108-
llvm::sys::path::remove_filename(OrcPath); // Remove ./bin directory.
108+
llvm::sys::path::remove_filename(BasePath); // Remove clang-repl filename.
109+
llvm::sys::path::remove_filename(BasePath); // Remove ./bin directory.
109110
llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
110-
llvm::StringRef Path;
111+
llvm::sys::path::append(BasePath, CLANG_INSTALL_LIBDIR_BASENAME, "clang",
112+
CLANG_VERSION_MAJOR_STRING);
111113
if (SystemTriple.isOSBinFormatELF())
112-
Path = "lib/clang/20/lib/x86_64-unknown-linux-gnu/liborc_rt.a";
114+
OrcRuntimePath =
115+
BasePath.str().str() + "lib/x86_64-unknown-linux-gnu/liborc_rt.a";
113116
else if (SystemTriple.isOSBinFormatMachO())
114-
Path = "lib/clang/20/lib/darwin/liborc_rt_osx.a";
115-
llvm::sys::path::append(OrcPath, Path);
116-
OrcRuntimePath = OrcPath.str().str();
117+
OrcRuntimePath = BasePath.str().str() + "/lib/darwin/liborc_rt_osx.a";
117118
}
118119

119120
// If -oop-executor was used but no value was specified then use a sensible
@@ -276,12 +277,10 @@ int main(int argc, const char **argv) {
276277
}
277278

278279
std::unique_ptr<llvm::orc::LLJITBuilder> JB;
279-
bool UseEPCSearchGen = false;
280280
if (EPC) {
281281
CB.SetTargetTriple(EPC->getTargetTriple().getTriple());
282282
JB = ExitOnErr(
283283
clang::Interpreter::createLLJITBuilder(std::move(EPC), OrcRuntimePath));
284-
UseEPCSearchGen = true;
285284
}
286285

287286
// FIXME: Investigate if we could use runToolOnCodeWithArgs from tooling. It
@@ -354,8 +353,7 @@ int main(int argc, const char **argv) {
354353
if (auto Err = Interp->Undo())
355354
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
356355
} else if (Input.rfind("%lib ", 0) == 0) {
357-
if (auto Err =
358-
Interp->LoadDynamicLibrary(Input.data() + 5, UseEPCSearchGen))
356+
if (auto Err = Interp->LoadDynamicLibrary(Input.data() + 5))
359357
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
360358
} else if (auto Err = Interp->ParseAndExecute(Input)) {
361359
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");

0 commit comments

Comments
 (0)