Skip to content

Commit

Permalink
Updates for LLVM 16 (#369)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Besard <tim.besard@gmail.com>
  • Loading branch information
vchuravy and maleadt authored Oct 24, 2023
1 parent 274fd42 commit 217819a
Show file tree
Hide file tree
Showing 22 changed files with 158 additions and 95 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
*.jl.mem
deps/Manifest.toml
test/Manifest.toml
LocalPreferences.toml
Manifest.toml
5 changes: 5 additions & 0 deletions LocalPreferences.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[LLVM]
# which extensions library to use. normally, this is provided by an artifact,
# but if you are using a custom version of LLVM you will need to provide your own,
# e.g., by running `deps/build_local.jl`.
#libLLVMExtra = "/path/to/libLLVMExtra.so"
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "6.4.0"
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
LLVMExtra_jll = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
Expand Down
7 changes: 5 additions & 2 deletions deps/LLVMExtra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
cmake_minimum_required(VERSION 3.3)

SET(CMAKE_CXX_FLAGS "-Wall -fPIC -fno-rtti")
cmake_policy(SET CMP0074 NEW)
cmake_policy(SET CMP0077 NEW)

project(LLVMExtra
VERSION
Expand All @@ -11,10 +13,11 @@ LANGUAGES
C
)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(LLVM REQUIRED)

find_package(LLVM REQUIRED CONFIG)
set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
${LLVM_CMAKE_DIR}
Expand Down
2 changes: 1 addition & 1 deletion deps/LLVMExtra/include/NewPM.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ LLVMPreservedAnalysesRef LLVMRunNewPMFunctionPassManager(LLVMFunctionPassManager
typedef struct LLVMOpaqueStandardInstrumentations *LLVMStandardInstrumentationsRef;
typedef struct LLVMOpaquePassInstrumentationCallbacks *LLVMPassInstrumentationCallbacksRef;

LLVMStandardInstrumentationsRef LLVMCreateStandardInstrumentations(void);
LLVMStandardInstrumentationsRef LLVMCreateStandardInstrumentations(LLVMContextRef C, LLVMBool DebugLogging, LLVMBool VerifyEach);
LLVMPassInstrumentationCallbacksRef LLVMCreatePassInstrumentationCallbacks(void);

void LLVMDisposeStandardInstrumentations(LLVMStandardInstrumentationsRef SI);
Expand Down
18 changes: 11 additions & 7 deletions deps/LLVMExtra/lib/llvm-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void LLVMExtraAppendToUsed(LLVMModuleRef Mod,
size_t Count)
{
SmallVector<GlobalValue *, 1> GlobalValues;
for (auto *Value : makeArrayRef(Values, Count))
for (auto *Value : ArrayRef(Values, Count))
GlobalValues.push_back(cast<GlobalValue>(unwrap(Value)));
appendToUsed(*unwrap(Mod), GlobalValues);
}
Expand All @@ -246,7 +246,7 @@ void LLVMExtraAppendToCompilerUsed(LLVMModuleRef Mod,
size_t Count)
{
SmallVector<GlobalValue *, 1> GlobalValues;
for (auto *Value : makeArrayRef(Values, Count))
for (auto *Value : ArrayRef(Values, Count))
GlobalValues.push_back(cast<GlobalValue>(unwrap(Value)));
appendToCompilerUsed(*unwrap(Mod), GlobalValues);
}
Expand Down Expand Up @@ -531,7 +531,7 @@ DEFINE_STDCXX_CONVERSION_FUNCTIONS(OperandBundleDef, LLVMOperandBundleDefRef)
LLVMOperandBundleDefRef LLVMCreateOperandBundleDef(const char *Tag, LLVMValueRef *Inputs,
unsigned NumInputs) {
SmallVector<Value*, 1> InputArray;
for (auto *Input : makeArrayRef(Inputs, NumInputs))
for (auto *Input : ArrayRef(Inputs, NumInputs))
InputArray.push_back(unwrap(Input));
return wrap(new OperandBundleDef(std::string(Tag), InputArray));
}
Expand Down Expand Up @@ -566,14 +566,18 @@ LLVMValueRef LLVMBuildCallWithOpBundle(LLVMBuilderRef B, LLVMValueRef Fn,
LLVMOperandBundleDefRef *Bundles, unsigned NumBundles,
const char *Name) {
SmallVector<OperandBundleDef, 1> BundleArray;
for (auto *Bundle : makeArrayRef(Bundles, NumBundles))
for (auto *Bundle : ArrayRef(Bundles, NumBundles))
BundleArray.push_back(*unwrap<OperandBundleDef>(Bundle));

llvm::IRBuilder<> *Builder = unwrap(B);
llvm::ArrayRef<llvm::Value*> args = makeArrayRef(unwrap(Args), NumArgs);
llvm::ArrayRef<llvm::Value*> args = ArrayRef(unwrap(Args), NumArgs);

Value *V = unwrap(Fn);
#if LLVM_VERSION_MAJOR >= 15
FunctionType *FnT = cast<Function>(V)->getFunctionType();
#else
FunctionType *FnT = cast<FunctionType>(V->getType()->getPointerElementType());
#endif
llvm::CallInst *CI = Builder->CreateCall(FnT, unwrap(Fn), args ,BundleArray, Name);
return wrap(CI);
}
Expand All @@ -583,11 +587,11 @@ LLVMValueRef LLVMBuildCallWithOpBundle2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMVa
LLVMOperandBundleDefRef *Bundles, unsigned NumBundles,
const char *Name) {
SmallVector<OperandBundleDef, 1> BundleArray;
for (auto *Bundle : makeArrayRef(Bundles, NumBundles))
for (auto *Bundle : ArrayRef(Bundles, NumBundles))
BundleArray.push_back(*unwrap<OperandBundleDef>(Bundle));

llvm::IRBuilder<> *Builder = unwrap(B);
llvm::ArrayRef<llvm::Value*> args = makeArrayRef(unwrap(Args), NumArgs);
llvm::ArrayRef<llvm::Value*> args = ArrayRef(unwrap(Args), NumArgs);

FunctionType *FTy = unwrap<FunctionType>(Ty);
llvm::CallInst *CI = Builder->CreateCall(FTy, unwrap(Fn), args ,BundleArray, Name);
Expand Down
12 changes: 10 additions & 2 deletions deps/LLVMExtra/lib/newpm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ LLVMPreservedAnalysesRef LLVMRunNewPMFunctionPassManager(LLVMFunctionPassManager
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::StandardInstrumentations, LLVMStandardInstrumentationsRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::PassInstrumentationCallbacks, LLVMPassInstrumentationCallbacksRef)

LLVMStandardInstrumentationsRef LLVMCreateStandardInstrumentations(void) {
return wrap(new llvm::StandardInstrumentations(false));
LLVMStandardInstrumentationsRef LLVMCreateStandardInstrumentations(LLVMContextRef C, LLVMBool DebugLogging, LLVMBool VerifyEach) {
#if LLVM_VERSION_MAJOR >= 16
return wrap(new llvm::StandardInstrumentations(*unwrap(C), DebugLogging, VerifyEach));
#else
return wrap(new llvm::StandardInstrumentations(DebugLogging, VerifyEach));
#endif
}
LLVMPassInstrumentationCallbacksRef LLVMCreatePassInstrumentationCallbacks(void) {
return wrap(new llvm::PassInstrumentationCallbacks());
Expand All @@ -134,7 +138,11 @@ void LLVMAddStandardInstrumentations(LLVMPassInstrumentationCallbacksRef PIC, LL
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::PassBuilder, LLVMPassBuilderRef)

LLVMPassBuilderRef LLVMCreatePassBuilder(LLVMTargetMachineRef TM, LLVMPassInstrumentationCallbacksRef PIC) {
#if LLVM_VERSION_MAJOR >= 16
return wrap(new llvm::PassBuilder(unwrap(TM), llvm::PipelineTuningOptions(), std::nullopt, unwrap(PIC)));
#else
return wrap(new llvm::PassBuilder(unwrap(TM), llvm::PipelineTuningOptions(), llvm::None, unwrap(PIC)));
#endif
}

void LLVMDisposePassBuilder(LLVMPassBuilderRef PB) {
Expand Down
8 changes: 4 additions & 4 deletions deps/build_local.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ LLVM_DIR = joinpath(LLVM.artifact_dir, "lib", "cmake", "llvm")
# build and install
@info "Building" source_dir scratch_dir build_dir LLVM_DIR
cmake() do cmake_path
config_opts = `-DLLVM_DIR=$(LLVM_DIR) -DCMAKE_INSTALL_PREFIX=$(scratch_dir)`
config_opts = `-DLLVM_ROOT=$(LLVM_DIR) -DCMAKE_INSTALL_PREFIX=$(scratch_dir)`
if Sys.iswindows()
# prevent picking up MSVC
config_opts = `$config_opts -G "MSYS Makefiles"`
Expand All @@ -64,10 +64,10 @@ end
lib_path = joinpath(scratch_dir, "lib", only(built_libs))
isfile(lib_path) || error("Could not find library $lib_path in build directory")

# tell LLVMExtra_jll to load our library instead of the default artifact one
# tell LLVM.jl to load our library instead of the default artifact one
set_preferences!(
joinpath(dirname(@__DIR__), "LocalPreferences.toml"),
"LLVMExtra_jll",
"libLLVMExtra_path" => lib_path;
"LLVM",
"libLLVMExtra" => lib_path;
force=true,
)
4 changes: 2 additions & 2 deletions lib/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ mutable struct LLVMOpaquePassInstrumentationCallbacks end

const LLVMPassInstrumentationCallbacksRef = Ptr{LLVMOpaquePassInstrumentationCallbacks}

function LLVMCreateStandardInstrumentations()
ccall((:LLVMCreateStandardInstrumentations, libLLVMExtra), LLVMStandardInstrumentationsRef, ())
function LLVMCreateStandardInstrumentations(C, DebugLogging, VerifyEach)
ccall((:LLVMCreateStandardInstrumentations, libLLVMExtra), LLVMStandardInstrumentationsRef, (LLVMContextRef, LLVMBool, LLVMBool), C, DebugLogging, VerifyEach)
end

function LLVMCreatePassInstrumentationCallbacks()
Expand Down
18 changes: 17 additions & 1 deletion src/LLVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const libllvm_version = Base.libllvm_version

module API
using CEnum
using Preferences

# LLVM C API
using ..LLVM
Expand All @@ -44,7 +45,14 @@ let
end

# LLVMExtra
import LLVMExtra_jll: libLLVMExtra
using LLVMExtra_jll
if has_preference(LLVM, "libLLVMExtra")
const libLLVMExtra = load_preference(LLVM, "libLLVMExtra")
else
if isdefined(LLVMExtra_jll, :libLLVMExtra)
import LLVMExtra_jll: libLLVMExtra
end
end
include(joinpath(@__DIR__, "..", "lib", "libLLVM_extra.jl"))

# Julia LLVM functionality
Expand Down Expand Up @@ -97,6 +105,14 @@ function __init__()
@debug "Using LLVM $libllvm_version at $(Base.libllvm_path())"

# sanity checks
if !isdefined(API, :libLLVMExtra)
@error """LLVM extensions library unavailable for your platform:
$(Base.BinaryPlatforms.triplet(API.LLVMExtra_jll.host_platform))
LLVM.jl will not be functional.
If you are using a custom version of LLVM, try building a
custom version of LLVMExtra_jll using `deps/build_local.jl`"""
end
if libllvm_version != Base.libllvm_version
# this checks that the precompilation image isn't being used
# after having upgraded Julia and the contained LLVM library.
Expand Down
5 changes: 1 addition & 4 deletions src/core/value/constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ register(ConstantVector, API.LLVMConstantVectorValueKind)

export ConstantExpr,

const_neg, const_nswneg, const_nuwneg, const_fneg, const_not, const_add,
const_neg, const_nswneg, const_nuwneg, const_not, const_add,
const_nswadd, const_nuwadd, const_sub, const_nswsub, const_nuwsub, const_mul,
const_nswmul, const_nuwmul, const_and, const_or, const_xor, const_icmp, const_fcmp,
const_shl, const_lshr, const_ashr, const_gep, const_inbounds_gep, const_trunc,
Expand All @@ -373,9 +373,6 @@ const_nswneg(val::Constant) =
const_nuwneg(val::Constant) =
Value(API.LLVMConstNUWNeg(val))

const_fneg(val::Constant) =
Value(API.LLVMConstFNeg(val))

const_not(val::Constant) =
Value(API.LLVMConstNot(val))

Expand Down
8 changes: 6 additions & 2 deletions src/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ export ismultithreaded

ismultithreaded() = convert(Core.Bool, API.LLVMIsMultithreaded())

for subsystem in [:Core, :TransformUtils, :ScalarOpts, :ObjCARCOpts, :Vectorization,
:InstCombine, :IPO, :Instrumentation, :Analysis, :IPA, :CodeGen, :Target]
const subsystems = [:Core, :TransformUtils, :ScalarOpts, :Vectorization, :InstCombine,
:IPO, :Analysis, :IPA, :CodeGen, :Target]
if LLVM.version() < v"16"
append!(subsystems, [:ObjCARCOpts, :Instrumentation])
end
for subsystem in subsystems
jl_fname = Symbol(:Initialize, subsystem)
api_fname = Symbol(:LLVM, jl_fname)
@eval begin
Expand Down
6 changes: 4 additions & 2 deletions src/newpm/analyses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ Base.show(io::IO, aa::AAManager) = print(io, analysis_string(aa))

# Function Alias Analysis
@alias_analysis "basic-aa" BasicAA
@alias_analysis "cfl-anders-aa" CFLAndersAA
@alias_analysis "cfl-steens-aa" CFLSteensAA
@alias_analysis "objc-arc-aa" ObjCARCAA
@alias_analysis "scev-aa" SCEVAA
@alias_analysis "scoped-noalias-aa" ScopedNoAliasAA
@alias_analysis "tbaa" TypeBasedAA
if LLVM.version() < v"16"
@alias_analysis "cfl-anders-aa" CFLAndersAA
@alias_analysis "cfl-steens-aa" CFLSteensAA
end

add!(am::AAManager, aa::NewPMAliasAnalysis) = push!(am.aas, analysis_string(aa))
add!(am::AAManager, aas::AbstractVector{<:NewPMAliasAnalysis}) =
Expand Down
3 changes: 2 additions & 1 deletion src/newpm/instrumentation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Base.unsafe_convert(::Type{API.LLVMPassInstrumentationCallbacksRef}, pic::PassIn
PassInstrumentationCallbacks(si) = PassInstrumentationCallbacks(API.LLVMCreatePassInstrumentationCallbacks(), si, [])
PassInstrumentationCallbacks() = PassInstrumentationCallbacks(API.LLVMStandardInstrumentationsRef(C_NULL))

StandardInstrumentationCallbacks() = PassInstrumentationCallbacks(API.LLVMCreateStandardInstrumentations())
StandardInstrumentationCallbacks(; debug_logging::Base.Bool=false, verify_each::Base.Bool=false) =
PassInstrumentationCallbacks(API.LLVMCreateStandardInstrumentations(context(), debug_logging, verify_each))

function PassInstrumentationCallbacks(f::Core.Function, args...; kwargs...)
pic = PassInstrumentationCallbacks(args...; kwargs...)
Expand Down
Loading

0 comments on commit 217819a

Please sign in to comment.