Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
llvm: [14, 15, 16, 17, 18, 19, 20]
llvm: [14, 15, 16, 17, 18, 19, 20, 21]
go-version: [1.18.x, 1.21.x, 1.22.x]
steps:
- name: Checkout
Expand All @@ -24,7 +24,7 @@ jobs:
# run: brew update
# Optional step when a LLVM version is very new.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The brew update guard was moved from llvm == 20 to llvm == 21, but not extended to cover 20. If the LLVM 20 Homebrew formula drifts or becomes unavailable without a brew update, macOS CI for LLVM 20 will silently fail or use a stale index. The original intent was to run brew update for the newest/freshest version. Now that 21 is the newest, consider whether 20 also still needs it, or document why only 21 requires this step.

- name: Update Homebrew
if: matrix.llvm == 20
if: matrix.llvm == 21
run: brew update
- name: Install LLVM
run: HOMEBREW_NO_AUTO_UPDATE=1 brew install llvm@${{ matrix.llvm }}
Expand All @@ -39,7 +39,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
llvm: [14, 15, 16, 17, 18, 19, 20]
llvm: [14, 15, 16, 17, 18, 19, 20, 21]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
23 changes: 21 additions & 2 deletions executionengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package llvm
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "errors"
import (
"errors"
"unsafe"
)

func LinkInMCJIT() { C.LLVMLinkInMCJIT() }
func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
Expand Down Expand Up @@ -110,6 +112,17 @@ func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
return
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewJITCompiler wraps LLVMCreateJITCompilerForModule, which is the legacy pre-MCJIT interface and has been deprecated by LLVM in favour of MCJIT (NewMCJITCompiler already exists in this package) and newer ORC/LLJIT APIs. A doc comment is needed to:

  1. Signal that this is the legacy JIT.
  2. Direct callers toward NewMCJITCompiler or ORC as the modern alternatives.

Without this, callers cannot distinguish between the two JIT constructors in the package.


func NewJITCompiler(m Module, optLevel int) (ee ExecutionEngine, err error) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ee.C = nil is redundant here — Go zero-initializes the named return value, so ee.C is already nil on entry and LLVMCreateJITCompilerForModule does not populate it on failure. The existing NewInterpreter omits this line for the same reason. Keeping it introduces a misleading asymmetry between the two constructors.

var cmsg *C.char
fail := C.LLVMCreateJITCompilerForModule(&ee.C, m.C, C.uint(optLevel), &cmsg)
if fail != 0 {
ee.C = nil
err = errors.New(C.GoString(cmsg))
C.LLVMDisposeMessage(cmsg)
}
return
}
Comment thread
zhouguangyuan0718 marked this conversation as resolved.

func NewMCJITCompilerOptions() MCJITCompilerOptions {
var options C.struct_LLVMMCJITCompilerOptions
C.LLVMInitializeMCJITCompilerOptions(&options, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})))
Expand Down Expand Up @@ -159,6 +172,12 @@ func (ee ExecutionEngine) FindFunction(name string) (f Value) {
return
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLVMGetFunctionAddress returns 0 when the function is not found (or on failure). The binding surfaces this as a uint64(0) with no accompanying error, making it impossible for callers to distinguish "function not found" from "function is at address 0". Callers who blindly cast the result to a function pointer and call it will dereference a null pointer.

Consider mirroring the pattern of FindFunction — either return (uint64, bool) or return 0 with documentation that callers must check for zero before use. At minimum, a doc comment is needed.

Additionally, per LLVM docs, getFunctionAddress may trigger JIT compilation rather than being a simple lookup. Callers who call this in a hot path for the same name should cache the result.

}

func (ee ExecutionEngine) GetFunctionAddress(name string) uint64 {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return uint64(C.LLVMGetFunctionAddress(ee.C, cname))
}

func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
}
Expand Down
13 changes: 0 additions & 13 deletions ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ type (
ComdatSelectionKind C.LLVMComdatSelectionKind
IntPredicate C.LLVMIntPredicate
FloatPredicate C.LLVMRealPredicate
LandingPadClause C.LLVMLandingPadClauseTy
InlineAsmDialect C.LLVMInlineAsmDialect
)

Expand Down Expand Up @@ -355,15 +354,6 @@ const (
FloatPredicateTrue FloatPredicate = C.LLVMRealPredicateTrue
)

//-------------------------------------------------------------------------
// llvm.LandingPadClause
//-------------------------------------------------------------------------

const (
LandingPadCatch LandingPadClause = C.LLVMLandingPadCatch
LandingPadFilter LandingPadClause = C.LLVMLandingPadFilter
)

//-------------------------------------------------------------------------
// llvm.InlineAsmDialect
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -939,9 +929,6 @@ func ConstNUWAdd(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWAdd(lhs.C, rhs.
func ConstSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstSub(lhs.C, rhs.C); return }
func ConstNSWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWSub(lhs.C, rhs.C); return }
func ConstNUWSub(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWSub(lhs.C, rhs.C); return }
func ConstMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstMul(lhs.C, rhs.C); return }
func ConstNSWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNSWMul(lhs.C, rhs.C); return }
func ConstNUWMul(lhs, rhs Value) (v Value) { v.C = C.LLVMConstNUWMul(lhs.C, rhs.C); return }
func ConstXor(lhs, rhs Value) (v Value) { v.C = C.LLVMConstXor(lhs.C, rhs.C); return }

func ConstGEP(t Type, v Value, indices []Value) (rv Value) {
Expand Down
1 change: 0 additions & 1 deletion ir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func TestAttributes(t *testing.T) {
"nest",
"noalias",
"nobuiltin",
"nocapture",
"noduplicate",
"noimplicitfloat",
"noinline",
Expand Down
15 changes: 0 additions & 15 deletions llvm_config_darwin_llvm14.go

This file was deleted.

15 changes: 0 additions & 15 deletions llvm_config_darwin_llvm15.go

This file was deleted.

15 changes: 0 additions & 15 deletions llvm_config_darwin_llvm16.go

This file was deleted.

15 changes: 0 additions & 15 deletions llvm_config_darwin_llvm17.go

This file was deleted.

15 changes: 0 additions & 15 deletions llvm_config_darwin_llvm18.go

This file was deleted.

15 changes: 0 additions & 15 deletions llvm_config_darwin_llvm19.go

This file was deleted.

15 changes: 0 additions & 15 deletions llvm_config_darwin_llvm20.go

This file was deleted.

10 changes: 0 additions & 10 deletions llvm_config_linux_llvm14.go

This file was deleted.

10 changes: 0 additions & 10 deletions llvm_config_linux_llvm15.go

This file was deleted.

10 changes: 0 additions & 10 deletions llvm_config_linux_llvm16.go

This file was deleted.

10 changes: 0 additions & 10 deletions llvm_config_linux_llvm17.go

This file was deleted.

10 changes: 0 additions & 10 deletions llvm_config_linux_llvm18.go

This file was deleted.

10 changes: 0 additions & 10 deletions llvm_config_linux_llvm19.go

This file was deleted.

10 changes: 0 additions & 10 deletions llvm_config_linux_llvm20.go

This file was deleted.

16 changes: 16 additions & 0 deletions llvm_config_llvm14.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !byollvm && llvm14

package llvm

// #cgo darwin,amd64 CPPFLAGS: -I/usr/local/opt/llvm@14/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,amd64 CXXFLAGS: -std=c++14
// #cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/llvm@14/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo darwin,arm64 CPPFLAGS: -I/opt/homebrew/opt/llvm@14/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,arm64 CXXFLAGS: -std=c++14
// #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@14/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo linux CPPFLAGS: -I/usr/lib/llvm-14/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo linux CXXFLAGS: -std=c++14
// #cgo linux LDFLAGS: -L/usr/lib/llvm-14/lib -lLLVM-14
import "C"

type run_build_sh int
16 changes: 16 additions & 0 deletions llvm_config_llvm15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !byollvm && llvm15

package llvm

// #cgo darwin,amd64 CPPFLAGS: -I/usr/local/opt/llvm@15/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,amd64 CXXFLAGS: -std=c++14
// #cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/llvm@15/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo darwin,arm64 CPPFLAGS: -I/opt/homebrew/opt/llvm@15/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,arm64 CXXFLAGS: -std=c++14
// #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@15/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo linux CPPFLAGS: -I/usr/lib/llvm-15/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo linux CXXFLAGS: -std=c++14
// #cgo linux LDFLAGS: -L/usr/lib/llvm-15/lib -lLLVM-15
import "C"

type run_build_sh int
16 changes: 16 additions & 0 deletions llvm_config_llvm16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !byollvm && llvm16

package llvm

// #cgo darwin,amd64 CPPFLAGS: -I/usr/local/opt/llvm@16/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,amd64 CXXFLAGS: -std=c++17
// #cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/llvm@16/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo darwin,arm64 CPPFLAGS: -I/opt/homebrew/opt/llvm@16/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,arm64 CXXFLAGS: -std=c++17
// #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@16/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo linux CPPFLAGS: -I/usr/lib/llvm-16/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo linux CXXFLAGS: -std=c++17
// #cgo linux LDFLAGS: -L/usr/lib/llvm-16/lib -lLLVM-16
import "C"

type run_build_sh int
16 changes: 16 additions & 0 deletions llvm_config_llvm17.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !byollvm && llvm17

package llvm

// #cgo darwin,amd64 CPPFLAGS: -I/usr/local/opt/llvm@17/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,amd64 CXXFLAGS: -std=c++17
// #cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/llvm@17/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo darwin,arm64 CPPFLAGS: -I/opt/homebrew/opt/llvm@17/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,arm64 CXXFLAGS: -std=c++17
// #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@17/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo linux CPPFLAGS: -I/usr/include/llvm-17 -I/usr/include/llvm-c-17 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo linux CXXFLAGS: -std=c++17
// #cgo linux LDFLAGS: -L/usr/lib/llvm-17/lib -lLLVM-17
import "C"

type run_build_sh int
16 changes: 16 additions & 0 deletions llvm_config_llvm18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !byollvm && llvm18

package llvm

// #cgo darwin,amd64 CPPFLAGS: -I/usr/local/opt/llvm@18/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,amd64 CXXFLAGS: -std=c++17
// #cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/llvm@18/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo darwin,arm64 CPPFLAGS: -I/opt/homebrew/opt/llvm@18/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo darwin,arm64 CXXFLAGS: -std=c++17
// #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@18/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM -lz -lm
// #cgo linux CPPFLAGS: -I/usr/include/llvm-18 -I/usr/include/llvm-c-18 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
// #cgo linux CXXFLAGS: -std=c++17
// #cgo linux LDFLAGS: -L/usr/lib/llvm-18/lib -lLLVM-18
import "C"

type run_build_sh int
Loading
Loading