Skip to content

[AST] Change _atomicBitWidth to _hasAtomicBitWidth #69078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2023
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
2 changes: 1 addition & 1 deletion include/swift/AST/PlatformConditionKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ PLATFORM_CONDITION(TargetEnvironment, "targetEnvironment")
PLATFORM_CONDITION_(PtrAuth, "ptrauth")

/// The active arch target's max atomic bit width.
PLATFORM_CONDITION_(AtomicBitWidth, "atomicBitWidth")
PLATFORM_CONDITION_(HasAtomicBitWidth, "hasAtomicBitWidth")

#undef PLATFORM_CONDITION
#undef PLATFORM_CONDITION_
33 changes: 31 additions & 2 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,36 @@ namespace swift {
/// by name.
bool hasFeature(llvm::StringRef featureName) const;

/// Sets the "_atomicBitWidth" conditional.
void setAtomicBitWidth(llvm::Triple triple);
/// Sets the "_hasAtomicBitWidth" conditional.
void setHasAtomicBitWidth(llvm::Triple triple);

/// Set the max atomic bit widths with the given bit width.
void setMaxAtomicBitWidth(unsigned maxWidth) {
switch (maxWidth) {
case 128:
AtomicBitWidths.emplace_back("_128");
LLVM_FALLTHROUGH;
case 64:
AtomicBitWidths.emplace_back("_64");
LLVM_FALLTHROUGH;
case 32:
AtomicBitWidths.emplace_back("_32");
LLVM_FALLTHROUGH;
case 16:
AtomicBitWidths.emplace_back("_16");
LLVM_FALLTHROUGH;
case 8:
AtomicBitWidths.emplace_back("_8");
break;
default:
return;
}
}

/// Removes all atomic bit widths.
void clearAtomicBitWidths() {
AtomicBitWidths.clear();
}

/// Returns true if the given platform condition argument represents
/// a supported target operating system.
Expand Down Expand Up @@ -723,6 +751,7 @@ namespace swift {
}

private:
llvm::SmallVector<std::string, 2> AtomicBitWidths;
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 10>
PlatformConditionValues;
llvm::SmallVector<std::string, 2> CustomConditionalCompilationFlags;
Expand Down
51 changes: 31 additions & 20 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ static const SupportedConditionalValue SupportedConditionalCompilationPtrAuthSch
"_arm64e",
};

static const SupportedConditionalValue SupportedConditionalCompilationAtomicBitWidths[] = {
static const SupportedConditionalValue SupportedConditionalCompilationHasAtomicBitWidths[] = {
"_8",
"_16",
"_32",
"_64",
"_128"
Expand Down Expand Up @@ -137,8 +139,8 @@ ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(con
return SupportedConditionalCompilationTargetEnvironments;
case PlatformConditionKind::PtrAuth:
return SupportedConditionalCompilationPtrAuthSchemes;
case PlatformConditionKind::AtomicBitWidth:
return SupportedConditionalCompilationAtomicBitWidths;
case PlatformConditionKind::HasAtomicBitWidth:
return SupportedConditionalCompilationHasAtomicBitWidths;
}
llvm_unreachable("Unhandled PlatformConditionKind in switch");
}
Expand Down Expand Up @@ -202,7 +204,7 @@ checkPlatformConditionSupported(PlatformConditionKind Kind, StringRef Value,
case PlatformConditionKind::Runtime:
case PlatformConditionKind::TargetEnvironment:
case PlatformConditionKind::PtrAuth:
case PlatformConditionKind::AtomicBitWidth:
case PlatformConditionKind::HasAtomicBitWidth:
return isMatching(Kind, Value, suggestedKind, suggestedValues);
case PlatformConditionKind::CanImport:
// All importable names are valid.
Expand Down Expand Up @@ -244,6 +246,14 @@ checkPlatformCondition(PlatformConditionKind Kind, StringRef Value) const {
return true;
}

if (Kind == PlatformConditionKind::HasAtomicBitWidth) {
for (auto bitWidth : AtomicBitWidths) {
if (bitWidth == Value) {
return true;
}
}
}

return false;
}

Expand Down Expand Up @@ -277,7 +287,7 @@ bool LangOptions::hasFeature(llvm::StringRef featureName) const {
return false;
}

void LangOptions::setAtomicBitWidth(llvm::Triple triple) {
void LangOptions::setHasAtomicBitWidth(llvm::Triple triple) {
// We really want to use Clang's getMaxAtomicInlineWidth(), but that requires
// a Clang::TargetInfo and we're setting up lang opts very early in the
// pipeline before any ASTContext or any ClangImporter instance where we can
Expand All @@ -291,11 +301,11 @@ void LangOptions::setAtomicBitWidth(llvm::Triple triple) {
switch (triple.getSubArch()) {
case llvm::Triple::SubArchType::ARMSubArch_v6m:
case llvm::Triple::SubArchType::ARMSubArch_v7m:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_32");
setMaxAtomicBitWidth(32);
break;

default:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
setMaxAtomicBitWidth(64);
break;
}
break;
Expand All @@ -307,48 +317,48 @@ void LangOptions::setAtomicBitWidth(llvm::Triple triple) {
case llvm::Triple::SubArchType::ARMSubArch_v8m_baseline:
case llvm::Triple::SubArchType::ARMSubArch_v8m_mainline:
case llvm::Triple::SubArchType::ARMSubArch_v8_1m_mainline:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
setMaxAtomicBitWidth(64);
break;

default:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_128");
setMaxAtomicBitWidth(128);
break;
}
break;

// arm64_32 has 32 bit pointer words, but it has the same architecture as
// arm64 and supports 128 bit atomics.
case llvm::Triple::ArchType::aarch64_32:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_128");
setMaxAtomicBitWidth(128);
break;

// PowerPC does not support double word atomics.
case llvm::Triple::ArchType::ppc:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_32");
setMaxAtomicBitWidth(32);
break;

// All of the 64 bit PowerPC flavors do not support double word atomics.
case llvm::Triple::ArchType::ppc64:
case llvm::Triple::ArchType::ppc64le:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
setMaxAtomicBitWidth(64);
break;

// SystemZ (s390x) does not support double word atomics.
case llvm::Triple::ArchType::systemz:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
setMaxAtomicBitWidth(64);
break;

// Wasm32 supports double word atomics.
case llvm::Triple::ArchType::wasm32:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
setMaxAtomicBitWidth(64);
break;

// x86 supports double word atomics.
//
// Technically, this is incorrect. However, on all x86 platforms where Swift
// is deployed this is true.
case llvm::Triple::ArchType::x86:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
setMaxAtomicBitWidth(64);
break;

// x86_64 supports double word atomics.
Expand All @@ -357,7 +367,7 @@ void LangOptions::setAtomicBitWidth(llvm::Triple triple) {
// is deployed this is true. If the ClangImporter ever stops unconditionally
// adding '-mcx16' to its Clang instance, then be sure to update this below.
case llvm::Triple::ArchType::x86_64:
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_128");
setMaxAtomicBitWidth(128);
break;

default:
Expand All @@ -366,17 +376,18 @@ void LangOptions::setAtomicBitWidth(llvm::Triple triple) {
// every arch supports at least word atomics.

if (triple.isArch32Bit()) {
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_32");
setMaxAtomicBitWidth(32);
}

if (triple.isArch64Bit()) {
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
setMaxAtomicBitWidth(64);
}
}
}

std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
clearAllPlatformConditionValues();
clearAtomicBitWidths();

if (triple.getOS() == llvm::Triple::Darwin &&
triple.getVendor() == llvm::Triple::Apple) {
Expand Down Expand Up @@ -546,8 +557,8 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
addPlatformConditionValue(PlatformConditionKind::TargetEnvironment,
"macabi");

// Set the "_atomicBitWidth" platform condition.
setAtomicBitWidth(triple);
// Set the "_hasHasAtomicBitWidth" platform condition.
setHasAtomicBitWidth(triple);

// If you add anything to this list, change the default size of
// PlatformConditionValues to not require an extra allocation
Expand Down
6 changes: 3 additions & 3 deletions lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ class ValidateIfConfigCondition :
return E;
}

// ( 'os' | 'arch' | '_endian' | '_pointerBitWidth' | '_runtime' | '_atomicBitWidth' ) '(' identifier ')''
// ( 'os' | 'arch' | '_endian' | '_pointerBitWidth' | '_runtime' | '_hasAtomicBitWidth' ) '(' identifier ')''
auto Kind = getPlatformConditionKind(*KindName);
if (!Kind.has_value()) {
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_expression);
Expand Down Expand Up @@ -422,8 +422,8 @@ class ValidateIfConfigCondition :
DiagName = "target environment"; break;
case PlatformConditionKind::PtrAuth:
DiagName = "pointer authentication scheme"; break;
case PlatformConditionKind::AtomicBitWidth:
DiagName = "atomic bit width"; break;
case PlatformConditionKind::HasAtomicBitWidth:
DiagName = "has atomic bit width"; break;
case PlatformConditionKind::Runtime:
llvm_unreachable("handled above");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
let i: Int = "Hello"
#endif

#if arch(arm64) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64) && _atomicBitWidth(_128)
#if arch(arm64) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64) && _hasAtomicBitWidth(_128)
class C {}
var x = C()
#endif
#endif
var y = x
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
let i: Int = "Hello"
#endif

#if arch(arm64) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64) && _atomicBitWidth(_128)
#if arch(arm64) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64) && _hasAtomicBitWidth(_128)
class C {}
var x = C()
#endif
#endif
var y = x
4 changes: 3 additions & 1 deletion test/Parse/ConditionalCompilation/arm64IOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
let i: Int = "Hello"
#endif

#if arch(arm64) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64) && _atomicBitWidth(_128)
#if arch(arm64) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64) && _hasAtomicBitWidth(_128)
class C {}
var x = C()
#endif
#endif
var y = x
4 changes: 3 additions & 1 deletion test/Parse/ConditionalCompilation/armAndroidTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
let i: Int = "Hello"
#endif

#if arch(arm) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
#if arch(arm) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_32)
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
class C {}
var x = C()
#endif
#endif
var y = x
4 changes: 3 additions & 1 deletion test/Parse/ConditionalCompilation/armIOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
let i: Int = "Hello"
#endif

#if arch(arm) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
#if arch(arm) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
class C {}
var x = C()
#endif
#endif
var y = x
4 changes: 3 additions & 1 deletion test/Parse/ConditionalCompilation/armWatchOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
let i: Int = "Hello"
#endif

#if arch(arm) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
#if arch(arm) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
class C {}
var x = C()
#endif
#endif
var y = x
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
let i: Int = "Hello"
#endif

#if arch(i386) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
#if arch(i386) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
class C {}
var x = C()
#endif
#endif
var y = x
4 changes: 3 additions & 1 deletion test/Parse/ConditionalCompilation/i386IOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
let i: Int = "Hello"
#endif

#if arch(i386) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
#if arch(i386) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
class C {}
var x = C()
#endif
#endif
var y = x
4 changes: 3 additions & 1 deletion test/Parse/ConditionalCompilation/i386WatchOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
let i: Int = "Hello"
#endif

#if arch(i386) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
#if arch(i386) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
class C {}
var x = C()
#endif
#endif
var y = x
Loading