Skip to content

Commit d0fed93

Browse files
committed
LLVM 18 support
1 parent 1d9f26c commit d0fed93

21 files changed

+86
-46
lines changed

.circleci/config.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ commands:
1010
steps:
1111
- restore_cache:
1212
keys:
13-
- llvm-source-17-v1
13+
- llvm-source-18-v1
1414
- run:
1515
name: "Fetch LLVM source"
1616
command: make llvm-source
1717
- save_cache:
18-
key: llvm-source-17-v1
18+
key: llvm-source-18-v1
1919
paths:
2020
- llvm-project/clang/lib/Headers
2121
- llvm-project/clang/include
@@ -105,12 +105,12 @@ jobs:
105105
- test-linux:
106106
llvm: "15"
107107
resource_class: large
108-
test-llvm17-go122:
108+
test-llvm18-go122:
109109
docker:
110110
- image: golang:1.22-bullseye
111111
steps:
112112
- test-linux:
113-
llvm: "17"
113+
llvm: "18"
114114
resource_class: large
115115

116116
workflows:
@@ -119,5 +119,5 @@ workflows:
119119
# This tests our lowest supported versions of Go and LLVM, to make sure at
120120
# least the smoke tests still pass.
121121
- test-llvm15-go118
122-
# This tests LLVM 17 support when linking against system libraries.
123-
- test-llvm17-go122
122+
# This tests LLVM 18 support when linking against system libraries.
123+
- test-llvm18-go122

.github/workflows/build-macos.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
uses: actions/cache/restore@v4
4444
id: cache-llvm-source
4545
with:
46-
key: llvm-source-17-${{ matrix.os }}-v2
46+
key: llvm-source-18-${{ matrix.os }}-v2
4747
path: |
4848
llvm-project/clang/lib/Headers
4949
llvm-project/clang/include
@@ -68,7 +68,7 @@ jobs:
6868
uses: actions/cache/restore@v4
6969
id: cache-llvm-build
7070
with:
71-
key: llvm-build-17-${{ matrix.os }}-v2
71+
key: llvm-build-18-${{ matrix.os }}-v2
7272
path: llvm-build
7373
- name: Build LLVM
7474
if: steps.cache-llvm-build.outputs.cache-hit != 'true'
@@ -128,7 +128,7 @@ jobs:
128128
runs-on: macos-latest
129129
strategy:
130130
matrix:
131-
version: [16, 17]
131+
version: [16, 17, 18]
132132
steps:
133133
- name: Set up Homebrew
134134
uses: Homebrew/actions/setup-homebrew@master
@@ -152,8 +152,8 @@ jobs:
152152
- name: Check binary
153153
run: tinygo version
154154
- name: Build TinyGo (default LLVM)
155-
if: matrix.version == 17
155+
if: matrix.version == 18
156156
run: go install
157157
- name: Check binary
158-
if: matrix.version == 17
158+
if: matrix.version == 18
159159
run: tinygo version

.github/workflows/linux.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
uses: actions/cache/restore@v4
4444
id: cache-llvm-source
4545
with:
46-
key: llvm-source-17-linux-alpine-v2
46+
key: llvm-source-18-linux-alpine-v1
4747
path: |
4848
llvm-project/clang/lib/Headers
4949
llvm-project/clang/include
@@ -68,7 +68,7 @@ jobs:
6868
uses: actions/cache/restore@v4
6969
id: cache-llvm-build
7070
with:
71-
key: llvm-build-17-linux-alpine-v2
71+
key: llvm-build-18-linux-alpine-v1
7272
path: llvm-build
7373
- name: Build LLVM
7474
if: steps.cache-llvm-build.outputs.cache-hit != 'true'
@@ -196,7 +196,7 @@ jobs:
196196
uses: actions/cache/restore@v4
197197
id: cache-llvm-source
198198
with:
199-
key: llvm-source-17-linux-asserts-v2
199+
key: llvm-source-18-linux-asserts-v1
200200
path: |
201201
llvm-project/clang/lib/Headers
202202
llvm-project/clang/include
@@ -221,7 +221,7 @@ jobs:
221221
uses: actions/cache/restore@v4
222222
id: cache-llvm-build
223223
with:
224-
key: llvm-build-17-linux-asserts-v2
224+
key: llvm-build-18-linux-asserts-v1
225225
path: llvm-build
226226
- name: Build LLVM
227227
if: steps.cache-llvm-build.outputs.cache-hit != 'true'
@@ -309,7 +309,7 @@ jobs:
309309
uses: actions/cache/restore@v4
310310
id: cache-llvm-source
311311
with:
312-
key: llvm-source-17-linux-v2
312+
key: llvm-source-18-linux-v1
313313
path: |
314314
llvm-project/clang/lib/Headers
315315
llvm-project/clang/include
@@ -334,7 +334,7 @@ jobs:
334334
uses: actions/cache/restore@v4
335335
id: cache-llvm-build
336336
with:
337-
key: llvm-build-17-linux-${{ matrix.goarch }}-v2
337+
key: llvm-build-18-linux-${{ matrix.goarch }}-v1
338338
path: llvm-build
339339
- name: Build LLVM
340340
if: steps.cache-llvm-build.outputs.cache-hit != 'true'

.github/workflows/llvm.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ jobs:
3535
uses: docker/metadata-action@v5
3636
with:
3737
images: |
38-
tinygo/llvm-17
39-
ghcr.io/${{ github.repository_owner }}/llvm-17
38+
tinygo/llvm-18
39+
ghcr.io/${{ github.repository_owner }}/llvm-18
4040
tags: |
4141
type=sha,format=long
4242
type=raw,value=latest

.github/workflows/nix.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
uses: actions/cache/restore@v4
2525
id: cache-llvm-source
2626
with:
27-
key: llvm-source-17-linux-nix-v2
27+
key: llvm-source-18-linux-nix-v1
2828
path: |
2929
llvm-project/compiler-rt
3030
- name: Download LLVM source

.github/workflows/sizediff-install-pkgs.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# still works after checking out the dev branch (that is, when going from LLVM
33
# 16 to LLVM 17 for example, both Clang 16 and Clang 17 are installed).
44

5-
echo 'deb https://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main' | sudo tee /etc/apt/sources.list.d/llvm.list
5+
echo 'deb https://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main' | sudo tee /etc/apt/sources.list.d/llvm.list
66
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
77
sudo apt-get update
88
sudo apt-get install --no-install-recommends -y \
9-
llvm-17-dev \
10-
clang-17 \
11-
libclang-17-dev \
12-
lld-17
9+
llvm-18-dev \
10+
clang-18 \
11+
libclang-18-dev \
12+
lld-18

.github/workflows/sizediff.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
uses: actions/cache@v4
2929
id: cache-llvm-source
3030
with:
31-
key: llvm-source-17-sizediff-v2
31+
key: llvm-source-18-sizediff-v1
3232
path: |
3333
llvm-project/compiler-rt
3434
- name: Download LLVM source

.github/workflows/windows.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
uses: actions/cache/restore@v4
4242
id: cache-llvm-source
4343
with:
44-
key: llvm-source-17-windows-v2
44+
key: llvm-source-18-windows-v1
4545
path: |
4646
llvm-project/clang/lib/Headers
4747
llvm-project/clang/include
@@ -66,7 +66,7 @@ jobs:
6666
uses: actions/cache/restore@v4
6767
id: cache-llvm-build
6868
with:
69-
key: llvm-build-17-windows-v2
69+
key: llvm-build-18-windows-v1
7070
path: llvm-build
7171
- name: Build LLVM
7272
if: steps.cache-llvm-build.outputs.cache-hit != 'true'

GNUmakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LLD_SRC ?= $(LLVM_PROJECTDIR)/lld
1010

1111
# Try to autodetect LLVM build tools.
1212
# Versions are listed here in descending priority order.
13-
LLVM_VERSIONS = 17 16 15
13+
LLVM_VERSIONS = 18 17 16 15
1414
errifempty = $(if $(1),$(1),$(error $(2)))
1515
detect = $(shell which $(call errifempty,$(firstword $(foreach p,$(2),$(shell command -v $(p) 2> /dev/null && echo $(p)))),failed to locate $(1) at any of: $(2)))
1616
toolSearchPathsVersion = $(1)-$(2)
@@ -239,7 +239,7 @@ gen-device-renesas: build/gen-device-svd
239239

240240
# Get LLVM sources.
241241
$(LLVM_PROJECTDIR)/llvm:
242-
git clone -b esp-17.0.1_20240419 --depth=1 https://github.com/espressif/llvm-project $(LLVM_PROJECTDIR)
242+
git clone -b xtensa_release_18.1.2 --depth=1 https://github.com/espressif/llvm-project $(LLVM_PROJECTDIR)
243243
llvm-source: $(LLVM_PROJECTDIR)/llvm
244244

245245
# Configure LLVM.

builder/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
773773
if sizeLevel >= 2 {
774774
// Workaround with roughly the same effect as
775775
// https://reviews.llvm.org/D119342.
776-
// Can hopefully be removed in LLVM 18.
776+
// Can hopefully be removed in LLVM 19.
777777
ldflags = append(ldflags,
778778
"-mllvm", "--rotation-max-header-size=0")
779779
}

cgo/libclang_config_llvm17.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !byollvm && !llvm15 && !llvm16
1+
//go:build !byollvm && llvm17
22

33
package cgo
44

cgo/libclang_config_llvm18.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build !byollvm && !llvm15 && !llvm16 && !llvm17
2+
3+
package cgo
4+
5+
/*
6+
#cgo linux CFLAGS: -I/usr/include/llvm-18 -I/usr/include/llvm-c-18 -I/usr/lib/llvm-18/include
7+
#cgo darwin,amd64 CFLAGS: -I/usr/local/opt/llvm@18/include
8+
#cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/llvm@18/include
9+
#cgo freebsd CFLAGS: -I/usr/local/llvm18/include
10+
#cgo linux LDFLAGS: -L/usr/lib/llvm-18/lib -lclang
11+
#cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/llvm@18/lib -lclang
12+
#cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/llvm@18/lib -lclang
13+
#cgo freebsd LDFLAGS: -L/usr/local/llvm18/lib -lclang
14+
*/
15+
import "C"

compiler/asserts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (b *builder) createChanBoundsCheck(elementSize uint64, bufSize llvm.Value,
135135

136136
// Calculate (^uintptr(0)) >> 1, which is the max value that fits in an
137137
// uintptr if uintptrs were signed.
138-
maxBufSize := llvm.ConstLShr(llvm.ConstNot(llvm.ConstInt(b.uintptrType, 0, false)), llvm.ConstInt(b.uintptrType, 1, false))
138+
maxBufSize := b.CreateLShr(llvm.ConstNot(llvm.ConstInt(b.uintptrType, 0, false)), llvm.ConstInt(b.uintptrType, 1, false), "")
139139
if elementSize > maxBufSize.ZExtValue() {
140140
b.addError(pos, fmt.Sprintf("channel element type is too big (%v bytes)", elementSize))
141141
return
@@ -150,7 +150,7 @@ func (b *builder) createChanBoundsCheck(elementSize uint64, bufSize llvm.Value,
150150

151151
// Make sure maxBufSize has the same type as bufSize.
152152
if maxBufSize.Type() != bufSize.Type() {
153-
maxBufSize = llvm.ConstZExt(maxBufSize, bufSize.Type())
153+
maxBufSize = b.CreateZExt(maxBufSize, bufSize.Type(), "")
154154
}
155155

156156
// Do the check for a too large (or negative) buffer size.

compiler/intrinsics.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func (b *builder) defineIntrinsicFunction() {
2323
b.createMemoryCopyImpl()
2424
case name == "runtime.memzero":
2525
b.createMemoryZeroImpl()
26+
case name == "runtime.stacksave":
27+
b.createStackSaveImpl()
2628
case name == "runtime.KeepAlive":
2729
b.createKeepAliveImpl()
2830
case strings.HasPrefix(name, "runtime/volatile.Load"):
@@ -77,6 +79,14 @@ func (b *builder) createMemoryZeroImpl() {
7779
b.CreateRetVoid()
7880
}
7981

82+
// createStackSaveImpl creates a call to llvm.stacksave.p0 to read the current
83+
// stack pointer.
84+
func (b *builder) createStackSaveImpl() {
85+
b.createFunctionStart(true)
86+
sp := b.readStackPointer()
87+
b.CreateRet(sp)
88+
}
89+
8090
// Return the llvm.memset.p0.i8 function declaration.
8191
func (c *compilerContext) getMemsetFunc() llvm.Value {
8292
fnName := "llvm.memset.p0.i" + strconv.Itoa(c.uintptrType.IntTypeWidth())

compiler/llvm.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,14 @@ func (c *compilerContext) isThumb() bool {
451451
// readStackPointer emits a LLVM intrinsic call that returns the current stack
452452
// pointer as an *i8.
453453
func (b *builder) readStackPointer() llvm.Value {
454-
stacksave := b.mod.NamedFunction("llvm.stacksave")
454+
name := "llvm.stacksave.p0"
455+
if llvmutil.Version() < 18 {
456+
name = "llvm.stacksave" // backwards compatibility with LLVM 17 and below
457+
}
458+
stacksave := b.mod.NamedFunction(name)
455459
if stacksave.IsNil() {
456460
fnType := llvm.FunctionType(b.dataPtrType, nil, false)
457-
stacksave = llvm.AddFunction(b.mod, "llvm.stacksave", fnType)
461+
stacksave = llvm.AddFunction(b.mod, name, fnType)
458462
}
459463
return b.CreateCall(stacksave.GlobalValueType(), stacksave, nil, "")
460464
}

compiler/llvmutil/llvm.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
package llvmutil
99

1010
import (
11+
"strconv"
12+
"strings"
13+
1114
"tinygo.org/x/go-llvm"
1215
)
1316

@@ -203,3 +206,13 @@ func AppendToGlobal(mod llvm.Module, globalName string, values ...llvm.Value) {
203206
used.SetInitializer(usedInitializer)
204207
used.SetLinkage(llvm.AppendingLinkage)
205208
}
209+
210+
// Return the LLVM major version.
211+
func Version() int {
212+
majorStr := strings.Split(llvm.Version, ".")[0]
213+
major, err := strconv.Atoi(majorStr)
214+
if err != nil {
215+
panic("unexpected error while parsing LLVM version: " + err.Error()) // should not happen
216+
}
217+
return major
218+
}

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
export MD5SUM=md5sum
7979
8080
# Ugly hack to make the Clang resources directory available.
81-
export GOFLAGS="\"-ldflags=-X github.com/tinygo-org/tinygo/goenv.clangResourceDir=${llvmPackages_17.clang.cc.lib}/lib/clang/17"\"
81+
export GOFLAGS="\"-ldflags=-X github.com/tinygo-org/tinygo/goenv.clangResourceDir=${llvmPackages_17.clang.cc.lib}/lib/clang/17" -tags=llvm17\"
8282
'';
8383
};
8484
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
golang.org/x/sys v0.16.0
2222
golang.org/x/tools v0.17.0
2323
gopkg.in/yaml.v2 v2.4.0
24-
tinygo.org/x/go-llvm v0.0.0-20240106122909-c2c543540318
24+
tinygo.org/x/go-llvm v0.0.0-20240518103902-697964f2a9dc
2525
)
2626

2727
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
105105
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
106106
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
107107
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
108-
tinygo.org/x/go-llvm v0.0.0-20240106122909-c2c543540318 h1:4KjZvPtcN1UwobevcGbdzeinx0L1i8HDdJu84bu7NI8=
109-
tinygo.org/x/go-llvm v0.0.0-20240106122909-c2c543540318/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0=
108+
tinygo.org/x/go-llvm v0.0.0-20240518103902-697964f2a9dc h1:TCzibFa4oLu+njEP3fnRUmZ+QQeb8BjtOwctgcjzL0k=
109+
tinygo.org/x/go-llvm v0.0.0-20240518103902-697964f2a9dc/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0=

src/runtime/runtime.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ func memmove(dst, src unsafe.Pointer, size uintptr)
4141
// like llvm.memset.p0.i32(ptr, 0, size, false).
4242
func memzero(ptr unsafe.Pointer, size uintptr)
4343

44-
// This intrinsic returns the current stack pointer.
45-
// It is normally used together with llvm.stackrestore but also works to get the
46-
// current stack pointer in a platform-independent way.
47-
//
48-
//export llvm.stacksave
44+
// Return the current stack pointer using the llvm.stacksave.p0 intrinsic.
45+
// It is normally used together with llvm.stackrestore.p0 but also works to get
46+
// the current stack pointer in a platform-independent way.
4947
func stacksave() unsafe.Pointer
5048

5149
//export strlen

transform/optimizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func Optimize(mod llvm.Module, config *compileopts.Config) []error {
5252
// Run some preparatory passes for the Go optimizer.
5353
po := llvm.NewPassBuilderOptions()
5454
defer po.Dispose()
55-
err := mod.RunPasses("globaldce,globalopt,ipsccp,instcombine,adce,function-attrs", llvm.TargetMachine{}, po)
55+
err := mod.RunPasses("globaldce,globalopt,ipsccp,instcombine<no-verify-fixpoint>,adce,function-attrs", llvm.TargetMachine{}, po)
5656
if err != nil {
5757
return []error{fmt.Errorf("could not build pass pipeline: %w", err)}
5858
}

0 commit comments

Comments
 (0)