Skip to content

Commit

Permalink
Add a flag to set min os version on darwin platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
aneshlya committed Jan 31, 2025
1 parent 326a7ad commit 7be7370
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/ispc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,7 @@ std::string Target::SupportedCPUs() {

std::string Target::GetTripleString() const {
llvm::Triple triple;
llvm::VersionTuple darwinVersionMin = g->darwinVersionMin;
switch (g->target_os) {
case TargetOS::windows:
if (m_arch == Arch::x86) {
Expand Down Expand Up @@ -2443,7 +2444,15 @@ std::string Target::GetTripleString() const {
exit(1);
}
triple.setVendor(llvm::Triple::VendorType::Apple);
triple.setOS(llvm::Triple::OSType::MacOSX);
if (darwinVersionMin.empty()) {
darwinVersionMin = (m_arch == Arch::x86_64) ? llvm::VersionTuple(10, 12) : llvm::VersionTuple(11, 0);
}
if (darwinVersionMin != llvm::VersionTuple(INT_MAX)) {
triple.setOSName(llvm::Triple::getOSTypeName(llvm::Triple::OSType::MacOSX).str() +
darwinVersionMin.getAsString());
} else {
triple.setOS(llvm::Triple::OSType::MacOSX);
}
break;
case TargetOS::android:
if (m_arch == Arch::x86) {
Expand Down Expand Up @@ -2472,7 +2481,15 @@ std::string Target::GetTripleString() const {
// "arm64-apple-ios"
triple.setArchName("arm64");
triple.setVendor(llvm::Triple::VendorType::Apple);
triple.setOS(llvm::Triple::OSType::IOS);
if (darwinVersionMin.empty()) {
darwinVersionMin = llvm::VersionTuple(11, 0);
}
if (darwinVersionMin != llvm::VersionTuple(INT_MAX)) {
triple.setOSName(llvm::Triple::getOSTypeName(llvm::Triple::OSType::IOS).str() +
darwinVersionMin.getAsString());
} else {
triple.setOS(llvm::Triple::OSType::IOS);
}
break;
case TargetOS::ps4:
if (m_arch != Arch::x86_64) {
Expand Down Expand Up @@ -2861,6 +2878,7 @@ Globals::Globals() {
enableLLVMIntrinsics = false;
mangleFunctionsWithTarget = false;
isMultiTargetCompilation = false;
darwinVersionMin = llvm::VersionTuple();
errorLimit = -1;

enableTimeTrace = false;
Expand Down
6 changes: 6 additions & 0 deletions src/ispc.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <llvm/ADT/APFloat.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/VersionTuple.h>

/** @def ISPC_MAX_NVEC maximum vector size of any of the compliation
targets.
Expand Down Expand Up @@ -901,6 +902,11 @@ struct Globals {

/* When compile time tracing is enabled, set time granularity. */
int timeTraceGranularity;

/* Set macOS/iOS deployment target. The version will be propagated to the triple.
This is required with new linker starting Xcode 15.
https://github.com/ispc/ispc/issues/3143 */
llvm::VersionTuple darwinVersionMin;
};

enum {
Expand Down
17 changes: 17 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ static void lPrintVersion() {
#endif
printf(" [--cpu=<type>]\t\t\tAn alias for [--device=<type>] switch\n");
printf(" [-D<foo>]\t\t\t\t#define given value when running preprocessor\n");
#if defined(ISPC_MACOS_TARGET_ON) || defined(ISPC_IOS_TARGET_ON)
printf(" [--darwin-version-min=<major.minor>]\tSet the minimum macOS/iOS version required for the "
"deployment.\n");
#endif
printf(" [--dev-stub <filename>]\t\tEmit device-side offload stub functions to file\n");
printf(" ");
char cpuHelp[2048];
Expand Down Expand Up @@ -790,6 +794,19 @@ int main(int Argc, char *Argv[]) {
"only 2, 3, 4 and 5 are allowed.",
argv[i] + 16);
}
} else if (!strncmp(argv[i], "--darwin-version-min=", 21)) {
const char *version = argv[i] + 21;
// Validate the version format
std::string versionStr(version);
llvm::VersionTuple versionTuple;
if (!versionStr.empty()) {
if (versionTuple.tryParse(versionStr)) {
errorHandler.AddError("Invalid version format: \"%s\". Use <major_ver.minor_ver>.", version);
}
} else {
versionTuple = llvm::VersionTuple(INT_MAX); // is reserved for "none" version
}
g->darwinVersionMin = versionTuple;
} else if (!strcmp(argv[i], "--print-target")) {
g->printTarget = true;
} else if (!strcmp(argv[i], "--no-omit-frame-pointer")) {
Expand Down
17 changes: 17 additions & 0 deletions tests/lit-tests/darwin-minos-ver-1.ispc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// The test checks that the triple contain minimum OS version if provided.

// RUN: %{ispc} %s --nostdlib --target-os=macos --target=avx2 --arch=x86-64 --emit-llvm-text --nowrap -o - | FileCheck %s --check-prefix=CHECK-MACOS-DEFAULT
// RUN: %{ispc} %s --nostdlib --target-os=macos --target=avx2 --arch=x86-64 --emit-llvm-text --nowrap --darwin-version-min=15.0 -o - | FileCheck %s --check-prefix=CHECK-MACOS-VER
// RUN: %{ispc} %s --nostdlib --target-os=macos --target=avx2 --arch=x86-64 --emit-llvm-text --nowrap --darwin-version-min="" -o - | FileCheck %s --check-prefix=CHECK-MACOS-VER-UNSET
// RUN: not %{ispc} %s --nostdlib --target-os=macos --emit-llvm-text --nowrap --target=host --darwin-version-min=a.b -o - 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR-VER

// REQUIRES: MACOS_HOST && X86_ENABLED

// CHECK-MACOS-DEFAULT: target triple = {{.*}}-apple-macosx10.12"
// CHECK-MACOS-VER: target triple = {{.*}}-apple-macosx15.0"
// CHECK-MACOS-VER-UNSET: target triple = {{.*}}-apple-macosx"

// CHECK-ERROR-VER: Error: Invalid version format: "a.b". Use <major_ver.minor_ver>.
uniform int j;

int foo(int i) { return i + 1; }
26 changes: 26 additions & 0 deletions tests/lit-tests/darwin-minos-ver-2.ispc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// The test checks that the triple contain minimum OS version if provided.
// Note: iOS target is enabled one ARM platforms only.

// RUN: %{ispc} %s --nostdlib --target-os=ios --arch=aarch64 --emit-llvm-text --nowrap --target=neon-i32x4 -o - | FileCheck %s --check-prefix=CHECK-IOS-DEFAULT
// RUN: %{ispc} %s --nostdlib --target-os=ios --arch=aarch64 --emit-llvm-text --nowrap --target=neon-i32x4 --darwin-version-min=15.0 -o - | FileCheck %s --check-prefix=CHECK-IOS-VER
// RUN: %{ispc} %s --nostdlib --target-os=ios --arch=aarch64 --emit-llvm-text --nowrap --target=neon-i32x4 --darwin-version-min="" -o - | FileCheck %s --check-prefix=CHECK-IOS-VER-UNSET
// RUN: %{ispc} %s --nostdlib --target-os=macos --arch=aarch64 --emit-llvm-text --nowrap --target=neon-i32x4 -o - | FileCheck %s --check-prefix=CHECK-MACOS-DEFAULT
// RUN: %{ispc} %s --nostdlib --target-os=macos --arch=aarch64 --emit-llvm-text --nowrap --target=neon-i32x4 --darwin-version-min=15.0 -o - | FileCheck %s --check-prefix=CHECK-MACOS-VER
// RUN: %{ispc} %s --nostdlib --target-os=macos --arch=aarch64 --emit-llvm-text --nowrap --target=neon-i32x4 --darwin-version-min="" -o - | FileCheck %s --check-prefix=CHECK-MACOS-VER-UNSET

// RUN: not %{ispc} %s --nostdlib --target-os=ios --arch=aarch64 --emit-llvm-text --nowrap --target=neon-i32x4 --darwin-version-min=a.b -o - 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR-VER

// REQUIRES: MACOS_HOST && MACOS_ARM_ENABLED

// CHECK-IOS-DEFAULT: target triple = {{.*}}-apple-ios11.0"
// CHECK-IOS-VER: target triple = {{.*}}-apple-ios15.0"
// CHECK-IOS-VER-UNSET: target triple = {{.*}}-apple-ios"

// CHECK-MACOS-DEFAULT: target triple = {{.*}}-apple-macosx11.0"
// CHECK-MACOS-VER: target triple = {{.*}}-apple-macosx15.0"
// CHECK-MACOS-VER-UNSET: target triple = {{.*}}-apple-macosx"

// CHECK-ERROR-VER: Error: Invalid version format: "a.b". Use <major_ver.minor_ver>.
uniform int j;

int foo(int i) { return i + 1; }

0 comments on commit 7be7370

Please sign in to comment.