-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathllvm-19.ab51eccf88f5.patch
More file actions
243 lines (230 loc) · 10.4 KB
/
llvm-19.ab51eccf88f5.patch
File metadata and controls
243 lines (230 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
From 3031818f4e7162cc6210f94adb8de988c75d13cc Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke@in.tum.de>
Date: Wed, 15 Jan 2025 11:00:36 +0100
Subject: [PATCH] [Clang][Flang] Add TPDE back-end
To compile, add the tpde2 repository to clang/lib/CodeGen/tpde2, e.g.
via a symlink. TPDE can be enabled using "-ftpde". By default, TPDE
failures cause a fallback to LLVM, this can be disabled with
"-ftpde-abort".
---
clang/include/clang/Basic/CodeGenOptions.def | 3 ++
clang/include/clang/Driver/Options.td | 12 +++++++
clang/lib/CodeGen/BackendUtil.cpp | 31 +++++++++++++++++++
clang/lib/CodeGen/CMakeLists.txt | 10 ++++++
clang/lib/Driver/ToolChains/Clang.cpp | 4 +++
clang/lib/Driver/ToolChains/Flang.cpp | 3 +-
.../include/flang/Frontend/CodeGenOptions.def | 3 ++
flang/lib/Frontend/CMakeLists.txt | 2 ++
flang/lib/Frontend/CompilerInvocation.cpp | 9 ++++++
flang/lib/Frontend/FrontendActions.cpp | 31 +++++++++++++++++++
10 files changed, 107 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index 12808eb275fa..ee163c217501 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -454,6 +454,9 @@ ENUM_CODEGENOPT(ZeroCallUsedRegs, llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind,
/// non-deleting destructors. (No effect on Microsoft ABI.)
CODEGENOPT(CtorDtorReturnThis, 1, 0)
+CODEGENOPT(TPDEEnable, 1, 0) ///< Enable TPDE
+CODEGENOPT(TPDEAbort, 1, 0) ///< Error on TPDE abort instead of LLVM fallback
+
/// FIXME: Make DebugOptions its own top-level .def file.
#include "DebugOptions.def"
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3498,6 +3498,18 @@ defm disable_block_signature_string : BoolFOption<"disable-block-signature-strin
NegFlag<SetFalse, [], [ClangOption], "Don't disable">,
BothFlags<[], [CC1Option], " block signature string)">>;
+
+defm tpde : BoolFOption<"tpde",
+ CodeGenOpts<"TPDEEnable">, DefaultFalse,
+ PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Enable">,
+ NegFlag<SetFalse, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Disable">,
+ BothFlags<[], [ClangOption, CC1Option, FlangOption, FC1Option], " TPDE back-end">>;
+defm tpde_abort : BoolFOption<"tpde-abort",
+ CodeGenOpts<"TPDEAbort">, DefaultFalse,
+ PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Abort">,
+ NegFlag<SetFalse, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Do not abort">,
+ BothFlags<[], [ClangOption, CC1Option, FlangOption, FC1Option], " on TPDE error">>;
+
def fomit_frame_pointer : Flag<["-"], "fomit-frame-pointer">, Group<f_Group>,
Visibility<[ClangOption, FlangOption]>,
HelpText<"Omit the frame pointer from functions that don't need it. "
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -9,6 +9,7 @@
#include "clang/CodeGen/BackendUtil.h"
#include "BackendConsumer.h"
#include "LinkInModulesPass.h"
+#include "tpde-llvm/LLVMCompiler.hpp"
#include "clang/Basic/CodeGenOptions.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/LangOptions.h"
@@ -1118,6 +1119,35 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
void EmitAssemblyHelper::RunCodegenPipeline(
BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
std::unique_ptr<llvm::ToolOutputFile> &DwoOS) {
+ if (CodeGenOpts.TPDEEnable) {
+ if (Action != Backend_EmitObj) {
+ Diags.Report(
+ Diags.getCustomDiagID(DiagnosticsEngine::Error,
+ "TPDE only supports emitting object files"));
+ return;
+ }
+
+ PrettyStackTraceString CrashInfo("TPDE");
+ llvm::TimeTraceScope TimeScope("TPDE");
+
+ auto Compiler = tpde_llvm::LLVMCompiler::create(TargetTriple);
+ std::vector<uint8_t> ObjectBuf;
+ if (Compiler && Compiler->compile_to_elf(*TheModule, ObjectBuf)) {
+ OS->write(reinterpret_cast<char *>(ObjectBuf.data()), ObjectBuf.size());
+ return;
+ }
+
+ if (CodeGenOpts.TPDEAbort) {
+ Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error,
+ "TPDE compilation failed"));
+ return;
+ }
+
+ Diags.Report(
+ Diags.getCustomDiagID(DiagnosticsEngine::Warning,
+ "TPDE compilation failed, falling back to LLVM"));
+ }
+
// We still use the legacy PM to run the codegen pipeline since the new PM
// does not work with the codegen pipeline.
// FIXME: make the new PM work with the codegen pipeline.
diff --git a/clang/lib/CodeGen/CMakeLists.txt b/clang/lib/CodeGen/CMakeLists.txt
index 2a179deddcc3..f900898c7bbd 100644
--- a/clang/lib/CodeGen/CMakeLists.txt
+++ b/clang/lib/CodeGen/CMakeLists.txt
@@ -157,3 +157,13 @@ add_clang_library(clangCodeGen
clangLex
clangSerialization
)
+
+if (LLVM_LINK_LLVM_DYLIB)
+ set(TPDE_LINK_LLVM_STATIC OFF)
+else()
+ set(TPDE_LINK_LLVM_STATIC ON)
+endif()
+set(LLVM_FOUND TRUE)
+set(LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
+add_subdirectory(tpde2)
+target_link_libraries(clangCodeGen PRIVATE $<BUILD_LOCAL_INTERFACE:tpde_llvm>)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7838,6 +7838,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
}
+ Args.AddLastArg(CmdArgs, options::OPT_ftpde, options::OPT_fno_tpde);
+ Args.AddLastArg(CmdArgs, options::OPT_ftpde_abort,
+ options::OPT_fno_tpde_abort);
+
if (Arg *A = Args.getLastArg(options::OPT_fglobal_isel,
options::OPT_fno_global_isel)) {
CmdArgs.push_back("-mllvm");
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -150,7 +150,8 @@ void Flang::addCodegenOptions(const ArgList &Args,
options::OPT_flang_deprecated_no_hlfir,
options::OPT_flang_experimental_integer_overflow,
options::OPT_fno_ppc_native_vec_elem_order,
- options::OPT_fppc_native_vec_elem_order});
+ options::OPT_fppc_native_vec_elem_order,
+ options::OPT_ftpde, options::OPT_ftpde_abort});
}
void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def
--- a/flang/include/flang/Frontend/CodeGenOptions.def
+++ b/flang/include/flang/Frontend/CodeGenOptions.def
@@ -40,5 +40,8 @@ ENUM_CODEGENOPT(DebugInfo, llvm::codegenoptions::DebugInfoKind, 4, llvm::codeg
ENUM_CODEGENOPT(VecLib, llvm::driver::VectorLibrary, 3, llvm::driver::VectorLibrary::NoLibrary) ///< Vector functions library to use
ENUM_CODEGENOPT(FramePointer, llvm::FramePointerKind, 2, llvm::FramePointerKind::None) ///< Enable the usage of frame pointers
+CODEGENOPT(TPDEEnable, 1, 0)
+CODEGENOPT(TPDEAbort, 1, 0)
+
#undef CODEGENOPT
#undef ENUM_CODEGENOPT
diff --git a/flang/lib/Frontend/CMakeLists.txt b/flang/lib/Frontend/CMakeLists.txt
--- a/flang/lib/Frontend/CMakeLists.txt
+++ b/flang/lib/Frontend/CMakeLists.txt
@@ -62,5 +62,7 @@ add_flang_library(flangFrontend
CLANG_LIBS
clangBasic
+ clangCodeGen
clangDriver
)
+target_link_libraries(flangFrontend PRIVATE $<BUILD_LOCAL_INTERFACE:tpde_llvm>)
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -415,6 +415,15 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
clang::driver::options::OPT_funderscoring, false)) {
opts.Underscoring = 0;
}
+
+ if (args.hasFlag(clang::driver::options::OPT_ftpde,
+ clang::driver::options::OPT_fno_tpde, false)) {
+ opts.TPDEEnable = 1;
+ if (args.hasFlag(clang::driver::options::OPT_ftpde_abort,
+ clang::driver::options::OPT_fno_tpde_abort, false)) {
+ opts.TPDEAbort = 1;
+ }
+ }
}
/// Parses all target input arguments and populates the target
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -41,6 +41,7 @@
#include "mlir/Support/LLVM.h"
#include "mlir/Target/LLVMIR/Import.h"
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
+#include "tpde-llvm/LLVMCompiler.hpp"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticFrontend.h"
#include "clang/Basic/FileManager.h"
@@ -924,6 +925,35 @@ static void generateMachineCodeOrAssemblyImpl(clang::DiagnosticsEngine &diags,
(act == BackendActionTy::Backend_EmitAssembly)) &&
"Unsupported action");
+ if (codeGenOpts.TPDEEnable) {
+ if (act != BackendActionTy::Backend_EmitObj) {
+ diags.Report(
+ diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
+ "TPDE only supports emitting object files"));
+ return;
+ }
+
+ llvm::TimeTraceScope timeScope("TPDE");
+
+ llvm::Triple triple(llvmModule.getTargetTriple());
+ auto compiler = tpde_llvm::LLVMCompiler::create(triple);
+ std::vector<uint8_t> objectBuf;
+ if (compiler && compiler->compile_to_elf(llvmModule, objectBuf)) {
+ os.write(reinterpret_cast<char *>(objectBuf.data()), objectBuf.size());
+ return;
+ }
+
+ if (codeGenOpts.TPDEAbort) {
+ diags.Report(diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
+ "TPDE compilation failed"));
+ return;
+ }
+
+ diags.Report(
+ diags.getCustomDiagID(clang::DiagnosticsEngine::Warning,
+ "TPDE compilation failed, falling back to LLVM"));
+ }
+
// Set-up the pass manager, i.e create an LLVM code-gen pass pipeline.
// Currently only the legacy pass manager is supported.
// TODO: Switch to the new PM once it's available in the backend.
--
2.47.0