Skip to content

Commit

Permalink
Merge pull request #237 from ruby-llvm/llvm-18-linux
Browse files Browse the repository at this point in the history
LLVM 18.1.3 - Without Mac CI
  • Loading branch information
thoughtafter committed Apr 6, 2024
2 parents 1a0b687 + 18ff063 commit 389d3cb
Show file tree
Hide file tree
Showing 23 changed files with 338 additions and 241 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, macos-12, macos-13]
os: [ubuntu-22.04]
ruby: ['2.7', '3.0', '3.1', '3.2', '3.3']
continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' }}
steps:
- name: Install LLVM on macos
if: startsWith(matrix.os, 'macos')
run: |
brew update --auto-update
brew install llvm@17
echo "$(brew --prefix llvm@17)/bin" >> $GITHUB_PATH
brew install llvm@18
echo "$(brew --prefix llvm@18)/bin" >> $GITHUB_PATH
sudo mkdir -p /opt/local/
sudo ln -s $(brew --prefix llvm@17)/lib /opt/local/lib
sudo ln -s $(brew --prefix llvm@18)/lib /opt/local/lib
# ffi will only look for files in a small set of directories, so llvm needs to be there
# https://github.com/ffi/ffi/blob/master/lib/ffi/dynamic_library.rb#L33
# /usr/lib /usr/local/lib /opt/local/lib /opt/homebrew/lib
Expand All @@ -34,19 +34,19 @@ jobs:
if: startsWith(matrix.os, 'macos')
run: |
echo $(brew --repository)
echo $(brew --prefix llvm@17)
echo $(brew info llvm@17)
echo $(brew --prefix llvm@18)
echo $(brew info llvm@18)
echo "$PATH"
echo $(llvm-config --ldflags)
ls -l /usr/local/Cellar/llvm@*/*/lib/libLLVM.dylib
ls -l $(brew --prefix llvm@17)/lib/libLLVM.dylib
ls -l $(brew --prefix llvm@18)/lib/libLLVM.dylib
ls -l /opt/local/lib/libLLVM.dylib
- name: Install LLVM on ubuntu
if: startsWith(matrix.os, 'ubuntu')
run: |
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 17
sudo ./llvm.sh 18
- name: 'Ubuntu debug'
if: startsWith(matrix.os, 'ubuntu')
run: |
Expand Down
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ Gemspec/DevelopmentDependencies:

Style/AccessModifierDeclarations:
Enabled: false

# this one is wrong
Style/DoubleNegation:
Enabled: false
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
### Changed
- Deprecate unwind instruction
- Attribute to_s and inspect call LLVM Attribute::getAsString() for a better and more consistent string
- Switch to LLVM 18
- Breaking changes to optimizing inline and always-inline
- Previously, an inliner-threshold of nil would be no inlining
- Currently, always-inline and inline are included, matching O0-3, unless disabled by parameter
- Changes to LLVM const functions
### Added
- LLVM::Module#clone_module to clone a module entirely.
- Attribute methods: readnone? readonly? writeonly? which work for new and old attributes
Expand Down
2 changes: 1 addition & 1 deletion lib/llvm/analysis_ffi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module LLVM::C
extend FFI::Library
ffi_lib ["libLLVM-17.so.1", "libLLVM.so.17", "LLVM-17"]
ffi_lib ["libLLVM-18.so.1", "libLLVM.so.18", "LLVM-18"]

def self.attach_function(name, *_)
begin; super; rescue FFI::NotFoundError => e
Expand Down
6 changes: 6 additions & 0 deletions lib/llvm/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ module C
# * @see llvm::Value::getValueID()
# */
attach_function :get_value_kind, :LLVMGetValueKind, [:pointer], :value_kind

attach_function :get_poison, :LLVMGetPoison, [:pointer], :pointer

attach_function :const_int_get_sext_value, :LLVMConstIntGetSExtValue, [:pointer], :long_long

attach_function :const_int_get_zext_value, :LLVMConstIntGetZExtValue, [:pointer], :ulong_long
end

# Yields a pointer suitable for storing an LLVM output message.
Expand Down
2 changes: 1 addition & 1 deletion lib/llvm/core/bitcode_ffi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module LLVM::C
extend FFI::Library
ffi_lib ["libLLVM-17.so.1", "libLLVM.so.17", "LLVM-17"]
ffi_lib ["libLLVM-18.so.1", "libLLVM.so.18", "LLVM-18"]

def self.attach_function(name, *_)
begin; super; rescue FFI::NotFoundError => e
Expand Down
4 changes: 4 additions & 0 deletions lib/llvm/core/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def null
ConstantExpr.from_ptr(C.const_null(self))
end

def poison
ConstantExpr.from_ptr(C.get_poison(self))
end

# Creates a pointer type with this type and the given address space.
def pointer(address_space = 0)
Type.pointer(self, address_space)
Expand Down
90 changes: 70 additions & 20 deletions lib/llvm/core/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,21 @@ def self.all_ones
end

# Creates a ConstantInt from an integer.
def self.from_i(n, signed = true)
from_ptr(C.const_int(type, n, signed ? 1 : 0))
def self.from_i(int, signed = true)
width = type.width
return type.poison if !fits_width?(int, width, signed)

from_ptr(C.const_int(type, int, signed ? 1 : 0))
end

# does int fit in width
# allow 1 for signed i1 (though really it's -1 to 0)
def self.fits_width?(int, width, signed)
if signed
int.bit_length < width || int == 1
else
int >= 0 && int.bit_length <= width
end
end

def self.parse(str, radix = 10)
Expand Down Expand Up @@ -442,22 +455,22 @@ def nuw_mul(rhs)

# Unsigned division.
def udiv(rhs)
raise "constant udiv removed in LLVM 15"
self.class.from_i(to_ui / rhs.to_ui, false)
end

# Signed division.
def /(rhs)
raise "constant sdiv removed in LLVM 15"
self.class.from_i(to_si / rhs.to_si, true)
end

# Unsigned remainder.
def urem(rhs)
raise "constant urem removed in LLVM 15"
self.class.from_i(to_ui % rhs.to_ui, false)
end

# Signed remainder.
def rem(rhs)
raise "constant srem removed in LLVM 15"
self.class.from_i(to_si % rhs.to_si, true)
end

# Boolean negation.
Expand All @@ -468,15 +481,16 @@ def ~@
alias not ~

# Integer AND.
# was: self.class.from_ptr(C.const_and(self, rhs))
def &(rhs)
self.class.from_ptr(C.const_and(self, rhs))
self.class.from_i(to_i & rhs.to_i)
end

alias and &

# Integer OR.
def |(rhs)
self.class.from_ptr(C.const_or(self, rhs))
self.class.from_i(to_i | rhs.to_i)
end

alias or |
Expand Down Expand Up @@ -528,25 +542,45 @@ def int_to_ptr(type)
ConstantExpr.from_ptr(C.const_int_to_ptr(self, type))
end

def to_ui
to_i(false)
end

def to_si
to_i(true)
end

# constant zext
# was: self.class.from_ptr(C.const_z_ext(self, type))
def zext(type)
self.class.from_ptr(C.const_z_ext(self, type))
type.from_i(to_ui)
end

# constant sext
# was: self.class.from_ptr(C.const_s_ext(self, type))
def sext(type)
self.class.from_ptr(C.const_s_ext(self, type))
type.from_i(to_si)
end
alias_method :ext, :sext

# constant trunc
# was: self.class.from_ptr(C.const_trunc(self, type))
def trunc(type)
self.class.from_ptr(C.const_trunc(self, type))
type.from_i(to_i)
end

# LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
# was: self.class.from_ptr(C.const_si_to_fp(self, type))
def to_f(type)
self.class.from_ptr(C.const_si_to_fp(self, type))
type.from_f(to_i.to_f)
end

def to_i(signed = true)
if signed
C.const_int_get_sext_value(self)
else
C.const_int_get_zext_value(self)
end
end
end

Expand Down Expand Up @@ -600,27 +634,31 @@ def self.parse(type, str)

# Negation.
def -@
raise "constant fneg removed in LLVM 16"
self.class.from_f(-to_f)
end

# Returns the result of adding this ConstantReal to rhs.
def +(rhs)
raise "constant fadd removed in LLVM 15"
self.class.from_f(to_f + rhs.to_f)
end

def -(rhs)
self.class.from_f(to_f - rhs.to_f)
end

# Returns the result of multiplying this ConstantReal by rhs.
def *(rhs)
raise "constant fmul removed in LLVM 15"
self.class.from_f(to_f * rhs.to_f)
end

# Returns the result of dividing this ConstantReal by rhs.
def /(rhs)
raise "constant fdiv removed in LLVM 15"
self.class.from_f(to_f / rhs.to_f) # rubocop:disable Style/FloatDivision
end

# Remainder.
def rem(rhs)
raise "constant frem removed in LLVM 15"
self.class.from_f(to_f.divmod(rhs.to_f).last)
end

# Floating point comparison using the predicate specified via the first
Expand All @@ -647,19 +685,31 @@ def fcmp(pred, rhs)

# constant FPToSI
# LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
# was: self.class.from_ptr(C.const_fp_to_si(self, type))
def to_i(type)
self.class.from_ptr(C.const_fp_to_si(self, type))
type.from_i(to_f.to_i)
end

# Constant FPExt
# this is a signed extension
# was: self.class.from_ptr(C.const_fp_ext(self, type))
def ext(type)
self.class.from_ptr(C.const_fp_ext(self, type))
type.from_f(to_f)
end
alias_method :sext, :ext

# was: self.class.from_ptr(C.const_fp_trunc(self, type))
def trunc(type)
self.class.from_ptr(C.const_fp_trunc(self, type))
type.from_f(to_f)
end

# get double from value
def to_f
double = nil
FFI::MemoryPointer.new(:bool, 1) do |loses_info|
double = C.const_real_get_double(self, loses_info)
end
double
end

end
Expand Down
Loading

0 comments on commit 389d3cb

Please sign in to comment.