Skip to content

Commit c6f3ac6

Browse files
build: Add compatibility with LLVM 21 (#2030)
This adds LLVM 21 compatibility by adding the relevant code changes guarded by `#ifdev`-statements for backwards compatibility. Avoid deprecated LLVM calls for 21+ Additionally also add CI checks for this. --------- Signed-off-by: Christian Heusel <[email protected]> Signed-off-by: Larry Gritz <[email protected]> Co-authored-by: Larry Gritz <[email protected]>
1 parent 8d34903 commit c6f3ac6

File tree

7 files changed

+97
-33
lines changed

7 files changed

+97
-33
lines changed

.github/workflows/build-steps.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ jobs:
101101
CTEST_TEST_TIMEOUT: ${{inputs.ctest_test_timeout}}
102102
USE_SIMD: ${{inputs.simd}}
103103
FMT_VERSION: ${{inputs.fmt_ver}}
104+
fmt_BUILD_VERSION: ${{inputs.fmt_ver}}
104105
OPENCOLORIO_VERSION: ${{inputs.opencolorio_ver}}
105106
OPENEXR_VERSION: ${{inputs.openexr_ver}}
106107
OPENIMAGEIO_VERSION: ${{inputs.openimageio_ver}}
@@ -134,7 +135,7 @@ jobs:
134135
restore-keys: ${{inputs.nametag}}
135136
- name: Install LLVM and Clang
136137
if: inputs.llvm_action_ver != ''
137-
uses: KyleMayes/install-llvm-action@a7a1a882e2d06ebe05d5bb97c3e1f8c984ae96fc # v2.0.7
138+
uses: KyleMayes/install-llvm-action@98e68e10c96dffcb7bfed8b2144541a66b49aa02 # v2.0.8
138139
with:
139140
version: ${{ inputs.llvm_action_ver }}
140141
- name: Dependencies

.github/workflows/ci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,15 +557,14 @@ jobs:
557557
openimageio_ver: main
558558
python_ver: "3.13"
559559
setenvs: export LLVMBREWVER="@19"
560-
- desc: MacOS-15-ARM aclang16/C++17/py3.13 llvm19 oiio-main
560+
- desc: MacOS-15-ARM aclang16/C++17/py3.13 llvm21 oiio-main
561561
runner: macos-15
562562
nametag: macos15-arm-py313
563563
cc_compiler: clang
564564
cxx_compiler: clang++
565565
cxx_std: 17
566566
python_ver: "3.13"
567567
openimageio_ver: main
568-
setenvs: export LLVMBREWVER="@19"
569568

570569

571570
windows:

INSTALL.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ NEW or CHANGED minimum dependencies since the last major release are **bold**.
4949
$OpenImageIO_ROOT/lib to be in your LD_LIBRARY_PATH (or
5050
DYLD_LIBRARY_PATH on OS X).
5151

52-
* [LLVM](http://www.llvm.org) 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, including
53-
clang libraries.
52+
* [LLVM](http://www.llvm.org) 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
53+
including clang libraries.
5454

5555
* (optional) For GPU rendering on NVIDIA GPUs:
5656
* [OptiX](https://developer.nvidia.com/rtx/ray-tracing/optix) 7.0 or higher.

src/cmake/externalpackages.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ checked_find_package (pugixml REQUIRED
5858
# LLVM library setup
5959
checked_find_package (LLVM REQUIRED
6060
VERSION_MIN 11.0
61-
VERSION_MAX 20.9
61+
VERSION_MAX 21.9
6262
PRINT LLVM_SYSTEM_LIBRARIES CLANG_LIBRARIES
6363
LLVM_SHARED_MODE)
6464
# ensure include directory is added (in case of non-standard locations

src/liboslcomp/oslcomp.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,32 @@ OSLCompilerImpl::preprocess_buffer(const std::string& buffer,
171171
llvm::raw_string_ostream errstream(preproc_errors);
172172
clang::DiagnosticOptions* diagOptions = new clang::DiagnosticOptions();
173173
clang::TextDiagnosticPrinter* diagPrinter
174+
#if OSL_LLVM_VERSION < 210
174175
= new clang::TextDiagnosticPrinter(errstream, diagOptions);
176+
#else
177+
= new clang::TextDiagnosticPrinter(errstream, *diagOptions);
178+
#endif
175179
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagIDs(
176180
new clang::DiagnosticIDs);
177181
clang::DiagnosticsEngine* diagEngine
182+
#if OSL_LLVM_VERSION < 210
178183
= new clang::DiagnosticsEngine(diagIDs, diagOptions, diagPrinter);
184+
#else
185+
= new clang::DiagnosticsEngine(diagIDs, *diagOptions, diagPrinter);
186+
#endif
179187
inst.setDiagnostics(diagEngine);
180188

181189
const std::shared_ptr<clang::TargetOptions> targetopts
182190
= std::make_shared<clang::TargetOptions>(inst.getTargetOpts());
183191
targetopts->Triple = llvm::sys::getDefaultTargetTriple();
184192
clang::TargetInfo* target
193+
#if OSL_LLVM_VERSION < 210
185194
= clang::TargetInfo::CreateTargetInfo(inst.getDiagnostics(),
186195
targetopts);
196+
#else
197+
= clang::TargetInfo::CreateTargetInfo(inst.getDiagnostics(),
198+
*targetopts);
199+
#endif
187200

188201
inst.setTarget(target);
189202

src/liboslexec/llvm_instance.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,7 +2225,11 @@ BackendLLVM::run()
22252225
// The triple is empty with recent versions of LLVM (e.g., 15) for reasons that aren't
22262226
// clear. So we must set them to the expected values.
22272227
// See: https://llvm.org/docs/NVPTXUsage.html
2228+
# if OSL_LLVM_VERSION < 210
22282229
ll.module()->setTargetTriple("nvptx64-nvidia-cuda");
2230+
# else
2231+
ll.module()->setTargetTriple(llvm::Triple("nvptx64-nvidia-cuda"));
2232+
# endif
22292233
ll.module()->setDataLayout(
22302234
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
22312235

src/liboslexec/llvm_util.cpp

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -498,27 +498,13 @@ LLVM_Util::LLVM_Util(const PerThreadInfo& per_thread_info, int debuglevel,
498498
m_llvm_type_longlong = (llvm::Type*)llvm::Type::getInt64Ty(*m_llvm_context);
499499
m_llvm_type_void = (llvm::Type*)llvm::Type::getVoidTy(*m_llvm_context);
500500

501-
m_llvm_type_int_ptr = llvm::PointerType::get(m_llvm_type_int, 0);
502-
m_llvm_type_int8_ptr = llvm::PointerType::get(m_llvm_type_int8, 0);
503-
m_llvm_type_int64_ptr = llvm::PointerType::get(m_llvm_type_int64, 0);
504-
m_llvm_type_bool_ptr = llvm::PointerType::get(m_llvm_type_bool, 0);
505-
m_llvm_type_char_ptr = llvm::PointerType::get(m_llvm_type_char, 0);
506-
m_llvm_type_void_ptr = m_llvm_type_char_ptr;
507-
m_llvm_type_float_ptr = llvm::PointerType::get(m_llvm_type_float, 0);
508-
m_llvm_type_longlong_ptr = llvm::PointerType::get(m_llvm_type_int64, 0);
509-
m_llvm_type_double_ptr = llvm::PointerType::get(m_llvm_type_double, 0);
510-
511501
// A triple is a struct composed of 3 floats
512502
std::vector<llvm::Type*> triplefields(3, m_llvm_type_float);
513503
m_llvm_type_triple = type_struct(triplefields, "Vec3");
514-
m_llvm_type_triple_ptr
515-
= (llvm::PointerType*)llvm::PointerType::get(m_llvm_type_triple, 0);
516504

517505
// A matrix is a struct composed 16 floats
518506
std::vector<llvm::Type*> matrixfields(16, m_llvm_type_float);
519507
m_llvm_type_matrix = type_struct(matrixfields, "Matrix4");
520-
m_llvm_type_matrix_ptr
521-
= (llvm::PointerType*)llvm::PointerType::get(m_llvm_type_matrix, 0);
522508

523509
// Setup up wide aliases
524510
// TODO: why are there casts to the base class llvm::Type *?
@@ -533,6 +519,48 @@ LLVM_Util::LLVM_Util(const PerThreadInfo& per_thread_info, int debuglevel,
533519
m_llvm_type_wide_longlong = llvm_vector_type(m_llvm_type_longlong,
534520
m_vector_width);
535521

522+
// A twide riple is a struct composed of 3 wide floats
523+
std::vector<llvm::Type*> triple_wide_fields(3, m_llvm_type_wide_float);
524+
m_llvm_type_wide_triple = type_struct(triple_wide_fields, "WideVec3");
525+
526+
// A wide matrix is a struct composed 16 wide floats
527+
std::vector<llvm::Type*> matrix_wide_fields(16, m_llvm_type_wide_float);
528+
m_llvm_type_wide_matrix = type_struct(matrix_wide_fields, "WideMatrix4");
529+
530+
#if OSL_LLVM_VERSION >= 210
531+
// All opaque pointers now. Eventually, all the typed ones can go away.
532+
m_llvm_type_void_ptr = llvm::PointerType::get(*m_llvm_context, 0);
533+
m_llvm_type_int_ptr = m_llvm_type_void_ptr;
534+
m_llvm_type_int8_ptr = m_llvm_type_void_ptr;
535+
m_llvm_type_int64_ptr = m_llvm_type_void_ptr;
536+
m_llvm_type_bool_ptr = m_llvm_type_void_ptr;
537+
m_llvm_type_char_ptr = m_llvm_type_void_ptr;
538+
m_llvm_type_float_ptr = m_llvm_type_void_ptr;
539+
m_llvm_type_longlong_ptr = m_llvm_type_void_ptr;
540+
m_llvm_type_double_ptr = m_llvm_type_void_ptr;
541+
m_llvm_type_triple_ptr = m_llvm_type_void_ptr;
542+
m_llvm_type_matrix_ptr = m_llvm_type_void_ptr;
543+
m_llvm_type_wide_char_ptr = m_llvm_type_void_ptr;
544+
m_llvm_type_wide_void_ptr = m_llvm_type_void_ptr;
545+
m_llvm_type_wide_int_ptr = m_llvm_type_void_ptr;
546+
m_llvm_type_wide_bool_ptr = m_llvm_type_void_ptr;
547+
m_llvm_type_wide_float_ptr = m_llvm_type_void_ptr;
548+
#else
549+
// Old style typed pointers. These are marked as deprecated in LLVM 21,
550+
// and will be removed in some subsequent version.
551+
m_llvm_type_int_ptr = llvm::PointerType::get(m_llvm_type_int, 0);
552+
m_llvm_type_int8_ptr = llvm::PointerType::get(m_llvm_type_int8, 0);
553+
m_llvm_type_int64_ptr = llvm::PointerType::get(m_llvm_type_int64, 0);
554+
m_llvm_type_bool_ptr = llvm::PointerType::get(m_llvm_type_bool, 0);
555+
m_llvm_type_char_ptr = llvm::PointerType::get(m_llvm_type_char, 0);
556+
m_llvm_type_void_ptr = m_llvm_type_char_ptr;
557+
m_llvm_type_float_ptr = llvm::PointerType::get(m_llvm_type_float, 0);
558+
m_llvm_type_longlong_ptr = llvm::PointerType::get(m_llvm_type_int64, 0);
559+
m_llvm_type_double_ptr = llvm::PointerType::get(m_llvm_type_double, 0);
560+
m_llvm_type_triple_ptr
561+
= (llvm::PointerType*)llvm::PointerType::get(m_llvm_type_triple, 0);
562+
m_llvm_type_matrix_ptr
563+
= (llvm::PointerType*)llvm::PointerType::get(m_llvm_type_matrix, 0);
536564
m_llvm_type_wide_char_ptr = llvm::PointerType::get(m_llvm_type_wide_char,
537565
0);
538566
m_llvm_type_wide_void_ptr = llvm_vector_type(m_llvm_type_void_ptr,
@@ -542,14 +570,7 @@ LLVM_Util::LLVM_Util(const PerThreadInfo& per_thread_info, int debuglevel,
542570
0);
543571
m_llvm_type_wide_float_ptr = llvm::PointerType::get(m_llvm_type_wide_float,
544572
0);
545-
546-
// A triple is a struct composed of 3 floats
547-
std::vector<llvm::Type*> triple_wide_fields(3, m_llvm_type_wide_float);
548-
m_llvm_type_wide_triple = type_struct(triple_wide_fields, "WideVec3");
549-
550-
// A matrix is a struct composed 16 floats
551-
std::vector<llvm::Type*> matrix_wide_fields(16, m_llvm_type_wide_float);
552-
m_llvm_type_wide_matrix = type_struct(matrix_wide_fields, "WideMatrix4");
573+
#endif
553574

554575
ustring_rep(m_ustring_rep); // setup ustring-related types
555576
}
@@ -567,14 +588,20 @@ LLVM_Util::ustring_rep(UstringRep rep)
567588
OSL_ASSERT(m_ustring_rep == UstringRep::hash);
568589
m_llvm_type_ustring = llvm::Type::getInt64Ty(*m_llvm_context);
569590
}
570-
m_llvm_type_ustring_ptr = llvm::PointerType::get(m_llvm_type_ustring, 0);
571591

572592
// Batched versions haven't been updated to handle hash yet.
573593
// For now leave them using the real ustring regardless of UstringRep
574594
m_llvm_type_wide_ustring = llvm_vector_type(m_llvm_type_real_ustring,
575595
m_vector_width);
596+
597+
#if OSL_LLVM_VERSION >= 210
598+
m_llvm_type_ustring_ptr = m_llvm_type_void_ptr;
599+
m_llvm_type_wide_ustring_ptr = m_llvm_type_void_ptr;
600+
#else
601+
m_llvm_type_ustring_ptr = llvm::PointerType::get(m_llvm_type_ustring, 0);
576602
m_llvm_type_wide_ustring_ptr
577603
= llvm::PointerType::get(m_llvm_type_wide_ustring, 0);
604+
#endif
578605
}
579606

580607

@@ -1840,8 +1867,13 @@ LLVM_Util::nvptx_target_machine()
18401867
&& "PTX compile error: LLVM Target is not initialized");
18411868

18421869
m_nvptx_target_machine = llvm_target->createTargetMachine(
1843-
ModuleTriple.str(), CUDA_TARGET_ARCH, "+ptx50", options,
1844-
llvm::Reloc::Static, llvm::CodeModel::Small,
1870+
#if OSL_LLVM_VERSION >= 210
1871+
llvm::Triple(ModuleTriple.str()),
1872+
#else
1873+
ModuleTriple.str(),
1874+
#endif
1875+
CUDA_TARGET_ARCH, "+ptx50", options, llvm::Reloc::Static,
1876+
llvm::CodeModel::Small,
18451877
#if OSL_LLVM_VERSION >= 180
18461878
llvm::CodeGenOptLevel::Default
18471879
#else
@@ -3287,7 +3319,11 @@ LLVM_Util::type_struct_field_at_index(llvm::Type* type, int index)
32873319
llvm::PointerType*
32883320
LLVM_Util::type_ptr(llvm::Type* type)
32893321
{
3322+
#if OSL_LLVM_VERSION >= 210
3323+
return m_llvm_type_void_ptr;
3324+
#else
32903325
return llvm::PointerType::get(type, 0);
3326+
#endif
32913327
}
32923328

32933329
llvm::Type*
@@ -3335,8 +3371,12 @@ llvm::PointerType*
33353371
LLVM_Util::type_function_ptr(llvm::Type* rettype, cspan<llvm::Type*> params,
33363372
bool varargs)
33373373
{
3374+
#if OSL_LLVM_VERSION >= 210
3375+
return m_llvm_type_void_ptr;
3376+
#else
33383377
llvm::FunctionType* functype = type_function(rettype, params, varargs);
33393378
return llvm::PointerType::getUnqual(functype);
3379+
#endif
33403380
}
33413381

33423382

@@ -4160,8 +4200,7 @@ llvm::Value*
41604200
LLVM_Util::ptr_to_cast(llvm::Value* val, llvm::Type* type,
41614201
const std::string& llname)
41624202
{
4163-
return builder().CreatePointerCast(val, llvm::PointerType::get(type, 0),
4164-
llname);
4203+
return builder().CreatePointerCast(val, type_ptr(type), llname);
41654204
}
41664205

41674206

@@ -4179,14 +4218,22 @@ llvm::Value*
41794218
LLVM_Util::ptr_cast(llvm::Value* val, const TypeDesc& type,
41804219
const std::string& llname)
41814220
{
4221+
#if OSL_LLVM_VERSION >= 210
4222+
return ptr_cast(val, m_llvm_type_void_ptr, llname);
4223+
#else
41824224
return ptr_cast(val, llvm::PointerType::get(llvm_type(type), 0), llname);
4225+
#endif
41834226
}
41844227

41854228

41864229
llvm::Value*
41874230
LLVM_Util::wide_ptr_cast(llvm::Value* val, const TypeDesc& type)
41884231
{
4232+
#if OSL_LLVM_VERSION >= 210
4233+
return ptr_cast(val, m_llvm_type_void_ptr);
4234+
#else
41894235
return ptr_cast(val, llvm::PointerType::get(llvm_vector_type(type), 0));
4236+
#endif
41904237
}
41914238

41924239

0 commit comments

Comments
 (0)