Skip to content

Commit 7d56024

Browse files
committed
Upgrade building LLVM-from-source to version 18.1.3
This newer version addresses a miscompile affecting `example_cloth.py` when running it on the CPU. The default build still uses pre-compiled LLVM 15 static libraries fetched from packman, for now, but when we're building LLVM from source we can use the more recent 18.1.3 version. Note that while this still works with GCC 7.4, the static libraries built with that version of GCC have an ABI incompatibility with at least GCC 9 and 10. This affects tests such as `test_copy.py`. Use of `-fabi-version=11` does not address this issue. This isn't a problem for from-source builds as long as these static libraries are not attempted to be used with newer versions of GCC (i.e. not stored in packman).
1 parent c32f7a6 commit 7d56024

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

build_llvm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def build_from_source_for_arch(args, arch, llvm_source):
5858
print(f"Cloning LLVM project from {repo_url}...")
5959

6060
shallow_clone = True # https://github.blog/2020-12-21-get-up-to-speed-with-partial-clone-and-shallow-clone/
61-
version = "15.0.7"
61+
version = "18.1.3"
6262
if shallow_clone:
6363
repo = Repo.clone_from(
6464
repo_url, to_path=llvm_source, single_branch=True, branch=f"llvmorg-{version}", depth=1
@@ -71,9 +71,11 @@ def build_from_source_for_arch(args, arch, llvm_source):
7171

7272
# CMake supports Debug, Release, RelWithDebInfo, and MinSizeRel builds
7373
if args.mode == "release":
74+
msvc_runtime = "MultiThreaded"
7475
# prefer smaller size over aggressive speed
7576
cmake_build_type = "MinSizeRel"
7677
else:
78+
msvc_runtime = "MultiThreadedDebug"
7779
# When args.mode == "debug" we build a Debug version of warp.dll but
7880
# we generally don't want warp-clang.dll to be a slow Debug version.
7981
if args.debug_llvm:
@@ -114,10 +116,7 @@ def build_from_source_for_arch(args, arch, llvm_source):
114116
"-B", build_path,
115117
"-G", "Ninja",
116118
"-D", f"CMAKE_BUILD_TYPE={cmake_build_type}",
117-
"-D", "LLVM_USE_CRT_RELEASE=MT",
118-
"-D", "LLVM_USE_CRT_MINSIZEREL=MT",
119-
"-D", "LLVM_USE_CRT_DEBUG=MTd",
120-
"-D", "LLVM_USE_CRT_RELWITHDEBINFO=MTd",
119+
"-D", f"CMAKE_MSVC_RUNTIME_LIBRARY={msvc_runtime}",
121120
"-D", f"LLVM_TARGETS_TO_BUILD={target_backend};NVPTX",
122121
"-D", "LLVM_ENABLE_PROJECTS=clang",
123122
"-D", "LLVM_ENABLE_ZLIB=FALSE",
@@ -329,6 +328,7 @@ def build_warp_clang_for_arch(args, lib_name, arch):
329328

330329
if os.name == "nt":
331330
libs.append("Version.lib")
331+
libs.append("Ws2_32.lib")
332332
libs.append(f'/LIBPATH:"{libpath}"')
333333
else:
334334
libs = [f"-l{lib[3:-2]}" for lib in libs if os.path.splitext(lib)[1] == ".a"]

warp/native/clang/clang.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
#include <clang/Frontend/CompilerInstance.h>
1212
#include <clang/Basic/DiagnosticOptions.h>
1313
#include <clang/Frontend/TextDiagnosticPrinter.h>
14+
#if LLVM_VERSION_MAJOR >= 18
15+
#include <llvm/Frontend/Debug/Options.h>
16+
#else
17+
#include <llvm/Support/CodeGen.h>
18+
#endif
1419
#include <clang/CodeGen/CodeGenAction.h>
1520
#include <clang/Basic/TargetInfo.h>
1621
#include <clang/Lex/PreprocessorOptions.h>
@@ -21,7 +26,6 @@
2126
#include <llvm/ExecutionEngine/GenericValue.h>
2227
#include <llvm/Target/TargetMachine.h>
2328
#include <llvm/MC/TargetRegistry.h>
24-
#include <llvm/Support/Host.h>
2529
#include <llvm/PassRegistry.h>
2630
#include <llvm/InitializePasses.h>
2731
#include <llvm/IR/LegacyPassManager.h>
@@ -114,7 +118,11 @@ static std::unique_ptr<llvm::Module> cpp_to_llvm(const std::string& input_file,
114118

115119
if(debug)
116120
{
121+
#if LLVM_VERSION_MAJOR >= 18
122+
compiler_invocation.getCodeGenOpts().setDebugInfo(llvm::codegenoptions::FullDebugInfo);
123+
#else
117124
compiler_invocation.getCodeGenOpts().setDebugInfo(clang::codegenoptions::FullDebugInfo);
125+
#endif
118126
}
119127

120128
// Map code to a MemoryBuffer
@@ -174,7 +182,11 @@ static std::unique_ptr<llvm::Module> cuda_to_llvm(const std::string& input_file,
174182

175183
if(debug)
176184
{
185+
#if LLVM_VERSION_MAJOR >= 18
186+
compiler_invocation.getCodeGenOpts().setDebugInfo(llvm::codegenoptions::FullDebugInfo);
187+
#else
177188
compiler_invocation.getCodeGenOpts().setDebugInfo(clang::codegenoptions::FullDebugInfo);
189+
#endif
178190
}
179191

180192
// Map code to a MemoryBuffer
@@ -234,7 +246,11 @@ WP_API int compile_cpp(const char* cpp_src, const char *input_file, const char*
234246
llvm::raw_fd_ostream output(output_file, error_code, llvm::sys::fs::OF_None);
235247

236248
llvm::legacy::PassManager pass_manager;
249+
#if LLVM_VERSION_MAJOR >= 18
250+
llvm::CodeGenFileType file_type = llvm::CodeGenFileType::ObjectFile;
251+
#else
237252
llvm::CodeGenFileType file_type = llvm::CGFT_ObjectFile;
253+
#endif
238254
target_machine->addPassesToEmitFile(pass_manager, output, nullptr, file_type);
239255

240256
pass_manager.run(*module);
@@ -287,7 +303,11 @@ WP_API int compile_cuda(const char* cpp_src, const char *input_file, const char*
287303
llvm::raw_fd_ostream output(output_file, error_code, llvm::sys::fs::OF_None);
288304

289305
llvm::legacy::PassManager pass_manager;
306+
#if LLVM_VERSION_MAJOR >= 18
307+
llvm::CodeGenFileType file_type = llvm::CodeGenFileType::AssemblyFile;
308+
#else
290309
llvm::CodeGenFileType file_type = llvm::CGFT_AssemblyFile;
310+
#endif
291311
target_machine->addPassesToEmitFile(pass_manager, output, nullptr, file_type);
292312

293313
pass_manager.run(*module);
@@ -352,10 +372,17 @@ WP_API int load_obj(const char* object_file, const char* module_name)
352372
#endif
353373

354374
const auto flags = llvm::JITSymbolFlags::Exported | llvm::JITSymbolFlags::Absolute;
375+
#if LLVM_VERSION_MAJOR >= 18
376+
#define SYMBOL(sym) { jit->getExecutionSession().intern(MANGLING_PREFIX #sym), { llvm::orc::ExecutorAddr::fromPtr(&::sym), flags} }
377+
#define SYMBOL_T(sym, T) { jit->getExecutionSession().intern(MANGLING_PREFIX #sym), { llvm::orc::ExecutorAddr::fromPtr(static_cast<T>(&::sym)), flags} }
378+
379+
auto error = dll->define(llvm::orc::absoluteSymbols(llvm::orc::SymbolMap({
380+
#else
355381
#define SYMBOL(sym) { jit->getExecutionSession().intern(MANGLING_PREFIX #sym), { llvm::pointerToJITTargetAddress(&::sym), flags} }
356382
#define SYMBOL_T(sym, T) { jit->getExecutionSession().intern(MANGLING_PREFIX #sym), { llvm::pointerToJITTargetAddress(static_cast<T>(&::sym)), flags} }
357383

358384
auto error = dll->define(llvm::orc::absoluteSymbols({
385+
#endif
359386
SYMBOL(printf), SYMBOL(puts), SYMBOL(putchar),
360387
SYMBOL_T(abs, int(*)(int)), SYMBOL(llabs),
361388
SYMBOL(fmodf), SYMBOL_T(fmod, double(*)(double, double)),
@@ -397,7 +424,11 @@ WP_API int load_obj(const char* object_file, const char* module_name)
397424
#else
398425
SYMBOL(sincosf), SYMBOL_T(sincos, void(*)(double,double*,double*)),
399426
#endif
427+
#if LLVM_VERSION_MAJOR >= 18
428+
})));
429+
#else
400430
}));
431+
#endif
401432

402433
if(error)
403434
{

0 commit comments

Comments
 (0)