From fce403b7a010e9e4bb303e2776adb9bd6c747ed0 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 4 Nov 2021 00:22:42 +0100 Subject: [PATCH] targets: match LLVM triple to the one Clang uses The target triples have to match mostly to be able to link LLVM modules. Linking LLVM modules is already possible (the triples already match), but testing becomes much easier when they match exactly. For macOS, I picked "macosx10.12.0". That's an old and unsupported version, but I had to pick _something_. Clang by default uses "macos10.4.0", which is much older. --- builder/builder_test.go | 5 +++++ compileopts/target.go | 12 +++++++++++- compiler/atomic.go | 5 ++--- compiler/testdata/goroutine-cortex-m-qemu.ll | 2 +- compiler/testdata/intrinsics-cortex-m-qemu.ll | 2 +- targets/cortex-m.json | 1 - targets/cortex-m0.json | 2 +- targets/cortex-m0plus.json | 2 +- targets/cortex-m3.json | 2 +- targets/cortex-m33.json | 2 +- targets/cortex-m4.json | 2 +- targets/cortex-m7.json | 2 +- targets/gameboy-advance.json | 2 +- targets/riscv32.json | 2 +- targets/riscv64.json | 2 +- 15 files changed, 29 insertions(+), 16 deletions(-) diff --git a/builder/builder_test.go b/builder/builder_test.go index 9ce05689..970c82ea 100644 --- a/builder/builder_test.go +++ b/builder/builder_test.go @@ -107,6 +107,11 @@ func testClangAttributes(t *testing.T, options *compileopts.Options) { } defer mod.Dispose() + // Check whether the LLVM target matches. + if mod.Target() != config.Triple() { + t.Errorf("target has LLVM triple %#v but Clang makes it LLVM triple %#v", config.Triple(), mod.Target()) + } + // Check the "target-cpu" string attribute of the add function. add := mod.NamedFunction("add") var cpu string diff --git a/compileopts/target.go b/compileopts/target.go index 0293f2b4..df269a26 100644 --- a/compileopts/target.go +++ b/compileopts/target.go @@ -165,7 +165,6 @@ func LoadTarget(options *Options) (*TargetSpec, error) { if options.Target == "" { // Configure based on GOOS/GOARCH environment variables (falling back to // runtime.GOOS/runtime.GOARCH), and generate a LLVM target based on it. - llvmos := options.GOOS llvmarch := map[string]string{ "386": "i386", "amd64": "x86_64", @@ -175,6 +174,17 @@ func LoadTarget(options *Options) (*TargetSpec, error) { if llvmarch == "" { llvmarch = options.GOARCH } + llvmos := options.GOOS + if llvmos == "darwin" { + // Use macosx* instead of darwin, otherwise darwin/arm64 will refer + // to iOS! + llvmos = "macosx10.12.0" + if llvmarch == "aarch64" { + // Looks like Apple prefers to call this architecture ARM64 + // instead of AArch64. + llvmarch = "arm64" + } + } // Target triples (which actually have four components, but are called // triples for historical reasons) have the form: // arch-vendor-os-environment diff --git a/compiler/atomic.go b/compiler/atomic.go index b9a3e1f4..f8fb51bf 100644 --- a/compiler/atomic.go +++ b/compiler/atomic.go @@ -38,11 +38,10 @@ func (b *builder) createAtomicOp(call *ssa.CallCommon) (llvm.Value, bool) { old := b.getValue(call.Args[1]) newVal := b.getValue(call.Args[2]) if strings.HasSuffix(name, "64") { - arch := strings.Split(b.Triple, "-")[0] - if strings.HasPrefix(arch, "arm") && strings.HasSuffix(arch, "m") { + if strings.HasPrefix(b.Triple, "thumb") { // Work around a bug in LLVM, at least LLVM 11: // https://reviews.llvm.org/D95891 - // Check for armv6m, armv7, armv7em, and perhaps others. + // Check for thumbv6m, thumbv7, thumbv7em, and perhaps others. // See also: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html compareAndSwap := b.mod.NamedFunction("__sync_val_compare_and_swap_8") if compareAndSwap.IsNil() { diff --git a/compiler/testdata/goroutine-cortex-m-qemu.ll b/compiler/testdata/goroutine-cortex-m-qemu.ll index 279ecc5a..d34a1eb4 100644 --- a/compiler/testdata/goroutine-cortex-m-qemu.ll +++ b/compiler/testdata/goroutine-cortex-m-qemu.ll @@ -1,7 +1,7 @@ ; ModuleID = 'goroutine.go' source_filename = "goroutine.go" target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" -target triple = "armv7m-unknown-unknown-eabi" +target triple = "thumbv7m-unknown-unknown-eabi" %runtime.channel = type { i32, i32, i8, %runtime.channelBlockedList*, i32, i32, i32, i8* } %runtime.channelBlockedList = type { %runtime.channelBlockedList*, %"internal/task.Task"*, %runtime.chanSelectState*, { %runtime.channelBlockedList*, i32, i32 } } diff --git a/compiler/testdata/intrinsics-cortex-m-qemu.ll b/compiler/testdata/intrinsics-cortex-m-qemu.ll index bf767bf9..cc088a6a 100644 --- a/compiler/testdata/intrinsics-cortex-m-qemu.ll +++ b/compiler/testdata/intrinsics-cortex-m-qemu.ll @@ -1,7 +1,7 @@ ; ModuleID = 'intrinsics.go' source_filename = "intrinsics.go" target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" -target triple = "armv7m-unknown-unknown-eabi" +target triple = "thumbv7m-unknown-unknown-eabi" declare noalias nonnull i8* @runtime.alloc(i32, i8*, i8*, i8*) diff --git a/targets/cortex-m.json b/targets/cortex-m.json index dacf2181..c2661393 100644 --- a/targets/cortex-m.json +++ b/targets/cortex-m.json @@ -10,7 +10,6 @@ "automatic-stack-size": true, "default-stack-size": 2048, "cflags": [ - "-mthumb", "-Werror", "-fshort-enums", "-fomit-frame-pointer", diff --git a/targets/cortex-m0.json b/targets/cortex-m0.json index b1e943f0..4cb4987c 100644 --- a/targets/cortex-m0.json +++ b/targets/cortex-m0.json @@ -1,5 +1,5 @@ { "inherits": ["cortex-m"], - "llvm-target": "armv6m-unknown-unknown-eabi", + "llvm-target": "thumbv6m-unknown-unknown-eabi", "cpu": "cortex-m0" } diff --git a/targets/cortex-m0plus.json b/targets/cortex-m0plus.json index 7fef099b..4b364f06 100644 --- a/targets/cortex-m0plus.json +++ b/targets/cortex-m0plus.json @@ -1,5 +1,5 @@ { "inherits": ["cortex-m"], - "llvm-target": "armv6m-unknown-unknown-eabi", + "llvm-target": "thumbv6m-unknown-unknown-eabi", "cpu": "cortex-m0plus" } diff --git a/targets/cortex-m3.json b/targets/cortex-m3.json index a2dc974a..629d578e 100644 --- a/targets/cortex-m3.json +++ b/targets/cortex-m3.json @@ -1,5 +1,5 @@ { "inherits": ["cortex-m"], - "llvm-target": "armv7m-unknown-unknown-eabi", + "llvm-target": "thumbv7m-unknown-unknown-eabi", "cpu": "cortex-m3" } diff --git a/targets/cortex-m33.json b/targets/cortex-m33.json index 0dc79a5f..9b74ad16 100644 --- a/targets/cortex-m33.json +++ b/targets/cortex-m33.json @@ -1,6 +1,6 @@ { "inherits": ["cortex-m"], - "llvm-target": "armv7m-unknown-unknown-eabi", + "llvm-target": "thumbv7m-unknown-unknown-eabi", "cflags": [ "-mfloat-abi=soft" ] diff --git a/targets/cortex-m4.json b/targets/cortex-m4.json index dd966d71..84601e4d 100644 --- a/targets/cortex-m4.json +++ b/targets/cortex-m4.json @@ -1,6 +1,6 @@ { "inherits": ["cortex-m"], - "llvm-target": "armv7em-unknown-unknown-eabi", + "llvm-target": "thumbv7em-unknown-unknown-eabi", "cpu": "cortex-m4", "cflags": [ "-mfloat-abi=soft" diff --git a/targets/cortex-m7.json b/targets/cortex-m7.json index dbe11dfd..08bb6595 100644 --- a/targets/cortex-m7.json +++ b/targets/cortex-m7.json @@ -1,6 +1,6 @@ { "inherits": ["cortex-m"], - "llvm-target": "armv7em-unknown-unknown-eabi", + "llvm-target": "thumbv7em-unknown-unknown-eabi", "cpu": "cortex-m7", "cflags": [ "-mfloat-abi=soft" diff --git a/targets/gameboy-advance.json b/targets/gameboy-advance.json index c929ab76..eec103c5 100644 --- a/targets/gameboy-advance.json +++ b/targets/gameboy-advance.json @@ -1,5 +1,5 @@ { - "llvm-target": "arm4-unknown-unknown-eabi", + "llvm-target": "armv4t-unknown-unknown-eabi", "cpu": "arm7tdmi", "build-tags": ["gameboyadvance", "arm7tdmi", "baremetal", "linux", "arm"], "goos": "linux", diff --git a/targets/riscv32.json b/targets/riscv32.json index 17cb7a27..c3c8dff5 100644 --- a/targets/riscv32.json +++ b/targets/riscv32.json @@ -1,6 +1,6 @@ { "inherits": ["riscv"], - "llvm-target": "riscv32--none", + "llvm-target": "riscv32-unknown-none", "build-tags": ["tinygo.riscv32"], "scheduler": "tasks", "default-stack-size": 2048, diff --git a/targets/riscv64.json b/targets/riscv64.json index 58510f67..c2378e97 100644 --- a/targets/riscv64.json +++ b/targets/riscv64.json @@ -1,6 +1,6 @@ { "inherits": ["riscv"], - "llvm-target": "riscv64--none", + "llvm-target": "riscv64-unknown-none", "build-tags": ["tinygo.riscv64"], "cflags": [ "-march=rv64gc",