Skip to content

Commit

Permalink
Use LLVM 15 by default, add experimental LLVM 16 support
Browse files Browse the repository at this point in the history
Changes required to use LLVM 15 by default, and to support LLVM 16
experimentally, include:

CI config:
- Bump all LLVM 14 configurations to use LLVM 15
- Dump the LLVM 15 configurations to use LLVM 16

llvmlite:
- Always set opaque pointers to false (they are the default in both 15
  and 16, unlike 14).
- Don't initialize of `ObjCARCOpts` in LLVM 16 and above. This was
  removed in LLVM 16 by:

  ```
  commit 4153f989bab0f2f300fa8d3001ebeef7b6d9672c
  Author: Arthur Eubanks <[email protected]>
  Date:   Sun Oct 2 13:20:21 2022 -0700

      [ObjCARC] Remove legacy PM versions of optimization passes
  ```
- Remove the `AggressiveInstCombine` and `PruneEH` passes. These were
  removed from the legacy pass manager in LLVM 16 by:

  ```
  commit 70dc3b811e4926fa2c88bd3b53b29c46fcba1a90
  Author: Arthur Eubanks <[email protected]>
  Date:   Mon Oct 31 14:50:38 2022 -0700

      [AggressiveInstCombine] Remove legacy PM pass
  ```

  and

  ```
  commit 46fc75ab28b78a730ea21fd7daba6443937bfaac
  Author: Sebastian Peryt <[email protected]>
  Date:   Mon Sep 26 18:31:32 2022 -0700

      [NFC][2/n] Remove PrunePH pass
  ```
- Modify `reserveAllocationSpace` in the memory manager to use `Align`
  for the type of alignments in LLVM 16 - this mirrors an upstream
  change.
- Remove LLVM 14-specific code paths (and one vestigial LLVM < 9 path).
- Update the function attributes test to recognize the new form of
  memory attributes, - `memory(<action>)` as opposed to individual
  attributes like `readonly`. See:
  https://releases.llvm.org/16.0.0/docs/LangRef.html#function-attributes
  • Loading branch information
gmarkall committed Jul 5, 2024
1 parent cb8baa4 commit 65bcb03
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 101 deletions.
8 changes: 4 additions & 4 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
PYTHON: '3.12'
CONDA_ENV: cienv

llvm15:
llvm16:
PYTHON: '3.12'
CONDA_ENV: cienv
LLVM: '15'
LLVM: '16'

- template: buildscripts/azure/azure-linux-macos.yml
parameters:
Expand Down Expand Up @@ -67,10 +67,10 @@ jobs:
CONDA_ENV: cienv
WHEEL: 'yes'

llvm15:
llvm16:
PYTHON: '3.12'
CONDA_ENV: cienv
LLVM: '15'
LLVM: '16'

- template: buildscripts/azure/azure-windows.yml
parameters:
Expand Down
4 changes: 2 additions & 2 deletions buildscripts/azure/azure-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
PYTHON: '3.12'
CONDA_ENV: cienv

llvm15:
llvm16:
PYTHON: '3.12'
CONDA_ENV: cienv
LLVM: '15'
LLVM: '16'

steps:

Expand Down
2 changes: 1 addition & 1 deletion buildscripts/incremental/build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ call activate %CONDA_ENV%
@rem - https://github.com/conda-forge/llvmdev-feedstock/issues/175
@rem - https://github.com/conda-forge/llvmdev-feedstock/pull/223
@rem - https://github.com/MicrosoftDocs/visualstudio-docs/issues/7774
if "%LLVM%"=="15" (
if "%LLVM%"=="16" (
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat"
if %errorlevel% neq 0 exit /b %errorlevel%
)
Expand Down
4 changes: 2 additions & 2 deletions buildscripts/incremental/setup_conda_environment.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ call activate %CONDA_ENV%
if %errorlevel% neq 0 exit /b %errorlevel%

@rem Install llvmdev
if "%LLVM%"=="15" (
if "%LLVM%"=="16" (
set LLVMDEV_CHANNEL="conda-forge"
) else (
set LLVMDEV_CHANNEL="numba/label/dev"
set LLVMDEV_CHANNEL="numba"
)

call conda install -y -q -c %LLVMDEV_CHANNEL% llvmdev="%LLVM%" libxml2
Expand Down
6 changes: 3 additions & 3 deletions buildscripts/incremental/setup_conda_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ source activate $CONDA_ENV
set -v

# Install llvmdev (separate channel, for now)
if [ "$LLVM" == "15" ]; then
$CONDA_INSTALL -c conda-forge llvmdev="15"
if [ "$LLVM" == "16" ]; then
$CONDA_INSTALL -c conda-forge llvmdev="16"
else
$CONDA_INSTALL -c numba/label/dev llvmdev="14.*"
$CONDA_INSTALL -c numba llvmdev="15.*"
fi

# Install the compiler toolchain, for osx, bootstrapping needed
Expand Down
8 changes: 4 additions & 4 deletions ffi/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ def main_posix(kind, library_ext):
else:
(version, _) = out.split('.', 1)
version = int(version)
if version == 15:
msg = ("Building with LLVM 15; note that LLVM 15 support is "
if version == 16:
msg = ("Building with LLVM 16; note that LLVM 16 support is "
"presently experimental")
show_warning(msg)
elif version != 14:
elif version != 15:

msg = ("Building llvmlite requires LLVM 14, got "
msg = ("Building llvmlite requires LLVM 15, got "
"{!r}. Be sure to set LLVM_CONFIG to the right executable "
"path.\nRead the documentation at "
"http://llvmlite.pydata.org/ for more information about "
Expand Down
4 changes: 0 additions & 4 deletions ffi/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ LLVMPY_DisposeString(const char *msg) { free(const_cast<char *>(msg)); }
API_EXPORT(LLVMContextRef)
LLVMPY_GetGlobalContext() {
auto context = LLVMGetGlobalContext();
#if LLVM_VERSION_MAJOR > 14
LLVMContextSetOpaquePointers(context, false);
#endif
return context;
}

API_EXPORT(LLVMContextRef)
LLVMPY_ContextCreate() {
LLVMContextRef context = LLVMContextCreate();
#if LLVM_VERSION_MAJOR > 14
LLVMContextSetOpaquePointers(context, false);
#endif
return context;
}

Expand Down
2 changes: 2 additions & 0 deletions ffi/initfini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ extern "C" {
INIT(Core)
INIT(TransformUtils)
INIT(ScalarOpts)
#if LLVM_VERSION_MAJOR < 16
INIT(ObjCARCOpts)
#endif
INIT(Vectorization)
INIT(InstCombine)
INIT(IPO)
Expand Down
39 changes: 25 additions & 14 deletions ffi/memorymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,23 @@ bool LlvmliteMemoryManager::hasSpace(const MemoryGroup &MemGroup,
return false;
}

void LlvmliteMemoryManager::reserveAllocationSpace(
uintptr_t CodeSize, uint32_t CodeAlign, uintptr_t RODataSize,
uint32_t RODataAlign, uintptr_t RWDataSize, uint32_t RWDataAlign) {
void LlvmliteMemoryManager::reserveAllocationSpace(uintptr_t CodeSize,
LLVMLITE_ALIGN CodeAlign,
uintptr_t RODataSize,
LLVMLITE_ALIGN RODataAlign,
uintptr_t RWDataSize,
LLVMLITE_ALIGN RWDataAlign) {
LLVM_DEBUG(
dbgs()
<< "\nLlvmliteMemoryManager::reserveAllocationSpace() request:\n\n");
LLVM_DEBUG(dbgs() << "Code size / align: " << format_hex(CodeSize, 2, true)
<< " / " << CodeAlign << "\n");
<< " / " << GET_ALIGN_VALUE(CodeAlign) << "\n");
LLVM_DEBUG(dbgs() << "ROData size / align: "
<< format_hex(RODataSize, 2, true) << " / " << RODataAlign
<< "\n");
<< format_hex(RODataSize, 2, true) << " / "
<< GET_ALIGN_VALUE(RODataAlign) << "\n");
LLVM_DEBUG(dbgs() << "RWData size / align: "
<< format_hex(RWDataSize, 2, true) << " / " << RWDataAlign
<< "\n");
<< format_hex(RWDataSize, 2, true) << " / "
<< GET_ALIGN_VALUE(RWDataAlign) << "\n");

if (CodeSize == 0 && RODataSize == 0 && RWDataSize == 0) {
LLVM_DEBUG(dbgs() << "No memory requested - returning early.\n");
Expand All @@ -152,23 +155,31 @@ void LlvmliteMemoryManager::reserveAllocationSpace(
// Code alignment needs to be at least the stub alignment - however, we
// don't have an easy way to get that here so as a workaround, we assume
// it's 8, which is the largest value I observed across all platforms.
#if LLVM_VERSION_MAJOR < 16
constexpr uint32_t StubAlign = 8;
CodeAlign = std::max(CodeAlign, StubAlign);
#else
constexpr uint64_t StubAlign = 8;
#endif

CodeAlign = LLVMLITE_ALIGN(std::max(GET_ALIGN_VALUE(CodeAlign), StubAlign));

// ROData and RWData may not need to be aligned to the StubAlign, but the
// stub alignment seems like a reasonable (if slightly arbitrary) minimum
// alignment for them that should not cause any issues on all (i.e. 64-bit)
// platforms.
RODataAlign = std::max(RODataAlign, StubAlign);
RWDataAlign = std::max(RWDataAlign, StubAlign);
RODataAlign =
LLVMLITE_ALIGN(std::max(GET_ALIGN_VALUE(RODataAlign), StubAlign));
RWDataAlign =
LLVMLITE_ALIGN(std::max(GET_ALIGN_VALUE(RWDataAlign), StubAlign));

// Get space required for each section. Use the same calculation as
// allocateSection because we need to be able to satisfy it.
uintptr_t RequiredCodeSize = alignTo(CodeSize, CodeAlign) + CodeAlign;
uintptr_t RequiredCodeSize =
alignTo(CodeSize, CodeAlign) + GET_ALIGN_VALUE(CodeAlign);
uintptr_t RequiredRODataSize =
alignTo(RODataSize, RODataAlign) + RODataAlign;
alignTo(RODataSize, RODataAlign) + GET_ALIGN_VALUE(RODataAlign);
uintptr_t RequiredRWDataSize =
alignTo(RWDataSize, RWDataAlign) + RWDataAlign;
alignTo(RWDataSize, RWDataAlign) + GET_ALIGN_VALUE(RWDataAlign);
uint64_t TotalSize =
RequiredCodeSize + RequiredRODataSize + RequiredRWDataSize;

Expand Down
15 changes: 12 additions & 3 deletions ffi/memorymanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class __attribute__((visibility("default"))) ErrorInfoBase;
#include <string>
#include <system_error>

#if LLVM_VERSION_MAJOR < 16
#define LLVMLITE_ALIGN uint32_t
#define GET_ALIGN_VALUE(align) align
#else
#define LLVMLITE_ALIGN Align
#define GET_ALIGN_VALUE(align) align.value()
#endif

namespace llvm {

/// This is a simple memory manager which implements the methods called by
Expand Down Expand Up @@ -174,11 +182,12 @@ class API_EXPORT(LlvmliteMemoryManager : public RTDyldMemoryManager) {

virtual bool needsToReserveAllocationSpace() override { return true; }

virtual void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
virtual void reserveAllocationSpace(uintptr_t CodeSize,
LLVMLITE_ALIGN CodeAlign,
uintptr_t RODataSize,
uint32_t RODataAlign,
LLVMLITE_ALIGN RODataAlign,
uintptr_t RWDataSize,
uint32_t RWDataAlign) override;
LLVMLITE_ALIGN RWDataAlign) override;

private:
struct FreeMemBlock {
Expand Down
9 changes: 0 additions & 9 deletions ffi/orcjit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,7 @@ LLVMPY_LLJITLookup(std::shared_ptr<LLJIT> *lljit, const char *dylib_name,
return nullptr;
}

#if LLVM_VERSION_MAJOR > 14
*addr = sym->getValue();
#else
*addr = sym->getAddress();
#endif

return new JITDylibTracker(*lljit, *dylib,
std::move(dylib->createResourceTracker()));
}
Expand Down Expand Up @@ -339,11 +334,7 @@ LLVMPY_LLJIT_Link(std::shared_ptr<LLJIT> *lljit, const char *libraryName,
LLVMDisposeErrorMessage(message);
return nullptr;
}
#if LLVM_VERSION_MAJOR > 14
exports[export_idx].address = lookup->getValue();
#else
exports[export_idx].address = lookup->getAddress();
#endif
}
return new JITDylibTracker(*lljit, *dylib,
std::move(dylib->getDefaultResourceTracker()));
Expand Down
26 changes: 4 additions & 22 deletions ffi/passmanagers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,8 @@ LLVMPY_AddCallGraphDOTPrinterPass(LLVMPassManagerRef PM) {

API_EXPORT(void)
LLVMPY_AddDotDomPrinterPass(LLVMPassManagerRef PM, bool showBody) {
#if LLVM_VERSION_MAJOR > 14
unwrap(PM)->add(showBody ? llvm::createDomPrinterWrapperPassPass()
: llvm::createDomOnlyPrinterWrapperPassPass());
#else
unwrap(PM)->add(showBody ? llvm::createDomPrinterPass()
: llvm::createDomOnlyPrinterPass());
#endif
}

API_EXPORT(void)
Expand All @@ -178,13 +173,8 @@ LLVMPY_AddGlobalsModRefAAPass(LLVMPassManagerRef PM) {

API_EXPORT(void)
LLVMPY_AddDotPostDomPrinterPass(LLVMPassManagerRef PM, bool showBody) {
#if LLVM_VERSION_MAJOR > 14
unwrap(PM)->add(showBody ? llvm::createPostDomPrinterWrapperPassPass()
: llvm::createPostDomOnlyPrinterWrapperPassPass());
#else
unwrap(PM)->add(showBody ? llvm::createPostDomPrinterPass()
: llvm::createPostDomOnlyPrinterPass());
#endif
}

API_EXPORT(void)
Expand Down Expand Up @@ -255,13 +245,6 @@ LLVMPY_AddAlwaysInlinerPass(LLVMPassManagerRef PM, bool insertLifetime) {
unwrap(PM)->add(llvm::createAlwaysInlinerLegacyPass(insertLifetime));
}

#if LLVM_VERSION_MAJOR < 15
API_EXPORT(void)
LLVMPY_AddArgPromotionPass(LLVMPassManagerRef PM, unsigned int maxElements) {
unwrap(PM)->add(llvm::createArgumentPromotionPass(maxElements));
}
#endif

API_EXPORT(void)
LLVMPY_AddBreakCriticalEdgesPass(LLVMPassManagerRef PM) {
unwrap(PM)->add(llvm::createBreakCriticalEdgesPass());
Expand Down Expand Up @@ -293,10 +276,12 @@ LLVMPY_AddDeadCodeEliminationPass(LLVMPassManagerRef PM) {
unwrap(PM)->add(createDeadCodeEliminationPass());
}

#if LLVM_VERSION_MAJOR < 16
API_EXPORT(void)
LLVMPY_AddAggressiveInstructionCombiningPass(LLVMPassManagerRef PM) {
unwrap(PM)->add(createAggressiveInstCombinerPass());
}
#endif

API_EXPORT(void)
LLVMPY_AddInternalizePass(LLVMPassManagerRef PM) {
Expand Down Expand Up @@ -349,12 +334,7 @@ LLVMPY_AddLoopUnrollAndJamPass(LLVMPassManagerRef PM) {
API_EXPORT(void)
LLVMPY_AddLoopUnswitchPass(LLVMPassManagerRef PM, bool optimizeForSize,
bool hasBranchDivergence) {
#if LLVM_VERSION_MAJOR > 14
unwrap(PM)->add(createSimpleLoopUnswitchLegacyPass(optimizeForSize));
#else
unwrap(PM)->add(
createLoopUnswitchPass(optimizeForSize, hasBranchDivergence));
#endif
}

API_EXPORT(void)
Expand Down Expand Up @@ -392,10 +372,12 @@ LLVMPY_AddPartialInliningPass(LLVMPassManagerRef PM) {
unwrap(PM)->add(createPartialInliningPass());
}

#if LLVM_VERSION_MAJOR < 16
API_EXPORT(void)
LLVMPY_AddPruneExceptionHandlingPass(LLVMPassManagerRef PM) {
unwrap(PM)->add(createPruneEHPass());
}
#endif

API_EXPORT(void)
LLVMPY_AddReassociatePass(LLVMPassManagerRef PM) {
Expand Down
1 change: 1 addition & 0 deletions ffi/targets.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "core.h"
#include "llvm-c/Target.h"
#include "llvm-c/TargetMachine.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/LegacyPassManager.h"
Expand Down
2 changes: 0 additions & 2 deletions ffi/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

// the following is needed for WriteGraph()
#include "llvm/Analysis/CFGPrinter.h"
#if LLVM_VERSION_MAJOR > 14
#include "llvm/Support/GraphWriter.h"
#endif

/* An iterator around a attribute list, including the stop condition */
struct AttributeListIterator {
Expand Down
Loading

0 comments on commit 65bcb03

Please sign in to comment.