Skip to content

Commit 03d8a62

Browse files
authored
Merge pull request #69078 from Azoy/slight-change-of-plans
[AST] Change _atomicBitWidth to _hasAtomicBitWidth
2 parents d61b885 + 234410f commit 03d8a62

26 files changed

+142
-58
lines changed

include/swift/AST/PlatformConditionKinds.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ PLATFORM_CONDITION(TargetEnvironment, "targetEnvironment")
4747
PLATFORM_CONDITION_(PtrAuth, "ptrauth")
4848

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

5252
#undef PLATFORM_CONDITION
5353
#undef PLATFORM_CONDITION_

include/swift/Basic/LangOptions.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,36 @@ namespace swift {
683683
/// by name.
684684
bool hasFeature(llvm::StringRef featureName) const;
685685

686-
/// Sets the "_atomicBitWidth" conditional.
687-
void setAtomicBitWidth(llvm::Triple triple);
686+
/// Sets the "_hasAtomicBitWidth" conditional.
687+
void setHasAtomicBitWidth(llvm::Triple triple);
688+
689+
/// Set the max atomic bit widths with the given bit width.
690+
void setMaxAtomicBitWidth(unsigned maxWidth) {
691+
switch (maxWidth) {
692+
case 128:
693+
AtomicBitWidths.emplace_back("_128");
694+
LLVM_FALLTHROUGH;
695+
case 64:
696+
AtomicBitWidths.emplace_back("_64");
697+
LLVM_FALLTHROUGH;
698+
case 32:
699+
AtomicBitWidths.emplace_back("_32");
700+
LLVM_FALLTHROUGH;
701+
case 16:
702+
AtomicBitWidths.emplace_back("_16");
703+
LLVM_FALLTHROUGH;
704+
case 8:
705+
AtomicBitWidths.emplace_back("_8");
706+
break;
707+
default:
708+
return;
709+
}
710+
}
711+
712+
/// Removes all atomic bit widths.
713+
void clearAtomicBitWidths() {
714+
AtomicBitWidths.clear();
715+
}
688716

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

725753
private:
754+
llvm::SmallVector<std::string, 2> AtomicBitWidths;
726755
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 10>
727756
PlatformConditionValues;
728757
llvm::SmallVector<std::string, 2> CustomConditionalCompilationFlags;

lib/Basic/LangOptions.cpp

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ static const SupportedConditionalValue SupportedConditionalCompilationPtrAuthSch
107107
"_arm64e",
108108
};
109109

110-
static const SupportedConditionalValue SupportedConditionalCompilationAtomicBitWidths[] = {
110+
static const SupportedConditionalValue SupportedConditionalCompilationHasAtomicBitWidths[] = {
111+
"_8",
112+
"_16",
111113
"_32",
112114
"_64",
113115
"_128"
@@ -137,8 +139,8 @@ ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(con
137139
return SupportedConditionalCompilationTargetEnvironments;
138140
case PlatformConditionKind::PtrAuth:
139141
return SupportedConditionalCompilationPtrAuthSchemes;
140-
case PlatformConditionKind::AtomicBitWidth:
141-
return SupportedConditionalCompilationAtomicBitWidths;
142+
case PlatformConditionKind::HasAtomicBitWidth:
143+
return SupportedConditionalCompilationHasAtomicBitWidths;
142144
}
143145
llvm_unreachable("Unhandled PlatformConditionKind in switch");
144146
}
@@ -202,7 +204,7 @@ checkPlatformConditionSupported(PlatformConditionKind Kind, StringRef Value,
202204
case PlatformConditionKind::Runtime:
203205
case PlatformConditionKind::TargetEnvironment:
204206
case PlatformConditionKind::PtrAuth:
205-
case PlatformConditionKind::AtomicBitWidth:
207+
case PlatformConditionKind::HasAtomicBitWidth:
206208
return isMatching(Kind, Value, suggestedKind, suggestedValues);
207209
case PlatformConditionKind::CanImport:
208210
// All importable names are valid.
@@ -244,6 +246,14 @@ checkPlatformCondition(PlatformConditionKind Kind, StringRef Value) const {
244246
return true;
245247
}
246248

249+
if (Kind == PlatformConditionKind::HasAtomicBitWidth) {
250+
for (auto bitWidth : AtomicBitWidths) {
251+
if (bitWidth == Value) {
252+
return true;
253+
}
254+
}
255+
}
256+
247257
return false;
248258
}
249259

@@ -277,7 +287,7 @@ bool LangOptions::hasFeature(llvm::StringRef featureName) const {
277287
return false;
278288
}
279289

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

297307
default:
298-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
308+
setMaxAtomicBitWidth(64);
299309
break;
300310
}
301311
break;
@@ -307,48 +317,48 @@ void LangOptions::setAtomicBitWidth(llvm::Triple triple) {
307317
case llvm::Triple::SubArchType::ARMSubArch_v8m_baseline:
308318
case llvm::Triple::SubArchType::ARMSubArch_v8m_mainline:
309319
case llvm::Triple::SubArchType::ARMSubArch_v8_1m_mainline:
310-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
320+
setMaxAtomicBitWidth(64);
311321
break;
312322

313323
default:
314-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_128");
324+
setMaxAtomicBitWidth(128);
315325
break;
316326
}
317327
break;
318328

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

325335
// PowerPC does not support double word atomics.
326336
case llvm::Triple::ArchType::ppc:
327-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_32");
337+
setMaxAtomicBitWidth(32);
328338
break;
329339

330340
// All of the 64 bit PowerPC flavors do not support double word atomics.
331341
case llvm::Triple::ArchType::ppc64:
332342
case llvm::Triple::ArchType::ppc64le:
333-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
343+
setMaxAtomicBitWidth(64);
334344
break;
335345

336346
// SystemZ (s390x) does not support double word atomics.
337347
case llvm::Triple::ArchType::systemz:
338-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
348+
setMaxAtomicBitWidth(64);
339349
break;
340350

341351
// Wasm32 supports double word atomics.
342352
case llvm::Triple::ArchType::wasm32:
343-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
353+
setMaxAtomicBitWidth(64);
344354
break;
345355

346356
// x86 supports double word atomics.
347357
//
348358
// Technically, this is incorrect. However, on all x86 platforms where Swift
349359
// is deployed this is true.
350360
case llvm::Triple::ArchType::x86:
351-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
361+
setMaxAtomicBitWidth(64);
352362
break;
353363

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

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

368378
if (triple.isArch32Bit()) {
369-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_32");
379+
setMaxAtomicBitWidth(32);
370380
}
371381

372382
if (triple.isArch64Bit()) {
373-
addPlatformConditionValue(PlatformConditionKind::AtomicBitWidth, "_64");
383+
setMaxAtomicBitWidth(64);
374384
}
375385
}
376386
}
377387

378388
std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
379389
clearAllPlatformConditionValues();
390+
clearAtomicBitWidths();
380391

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

549-
// Set the "_atomicBitWidth" platform condition.
550-
setAtomicBitWidth(triple);
560+
// Set the "_hasHasAtomicBitWidth" platform condition.
561+
setHasAtomicBitWidth(triple);
551562

552563
// If you add anything to this list, change the default size of
553564
// PlatformConditionValues to not require an extra allocation

lib/Parse/ParseIfConfig.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class ValidateIfConfigCondition :
380380
return E;
381381
}
382382

383-
// ( 'os' | 'arch' | '_endian' | '_pointerBitWidth' | '_runtime' | '_atomicBitWidth' ) '(' identifier ')''
383+
// ( 'os' | 'arch' | '_endian' | '_pointerBitWidth' | '_runtime' | '_hasAtomicBitWidth' ) '(' identifier ')''
384384
auto Kind = getPlatformConditionKind(*KindName);
385385
if (!Kind.has_value()) {
386386
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_expression);
@@ -422,8 +422,8 @@ class ValidateIfConfigCondition :
422422
DiagName = "target environment"; break;
423423
case PlatformConditionKind::PtrAuth:
424424
DiagName = "pointer authentication scheme"; break;
425-
case PlatformConditionKind::AtomicBitWidth:
426-
DiagName = "atomic bit width"; break;
425+
case PlatformConditionKind::HasAtomicBitWidth:
426+
DiagName = "has atomic bit width"; break;
427427
case PlatformConditionKind::Runtime:
428428
llvm_unreachable("handled above");
429429
}

test/Parse/ConditionalCompilation/aarch64AndroidTarget.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm64) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64) && _atomicBitWidth(_128)
10+
#if arch(arm64) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
11+
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64) && _hasAtomicBitWidth(_128)
1112
class C {}
1213
var x = C()
1314
#endif
15+
#endif
1416
var y = x

test/Parse/ConditionalCompilation/arm64AppleTVOSTarget.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm64) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64) && _atomicBitWidth(_128)
10+
#if arch(arm64) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
11+
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64) && _hasAtomicBitWidth(_128)
1112
class C {}
1213
var x = C()
1314
#endif
15+
#endif
1416
var y = x

test/Parse/ConditionalCompilation/arm64IOSTarget.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm64) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64) && _atomicBitWidth(_128)
10+
#if arch(arm64) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
11+
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64) && _hasAtomicBitWidth(_128)
1112
class C {}
1213
var x = C()
1314
#endif
15+
#endif
1416
var y = x

test/Parse/ConditionalCompilation/armAndroidTarget.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
10+
#if arch(arm) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_32)
11+
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
1112
class C {}
1213
var x = C()
1314
#endif
15+
#endif
1416
var y = x

test/Parse/ConditionalCompilation/armIOSTarget.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
10+
#if arch(arm) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
11+
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
1112
class C {}
1213
var x = C()
1314
#endif
15+
#endif
1416
var y = x

test/Parse/ConditionalCompilation/armWatchOSTarget.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
10+
#if arch(arm) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
11+
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
1112
class C {}
1213
var x = C()
1314
#endif
15+
#endif
1416
var y = x

test/Parse/ConditionalCompilation/i386AppleTVOSTarget.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(i386) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
10+
#if arch(i386) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
11+
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
1112
class C {}
1213
var x = C()
1314
#endif
15+
#endif
1416
var y = x

test/Parse/ConditionalCompilation/i386IOSTarget.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(i386) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
10+
#if arch(i386) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
11+
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
1112
class C {}
1213
var x = C()
1314
#endif
15+
#endif
1416
var y = x

test/Parse/ConditionalCompilation/i386WatchOSTarget.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(i386) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32) && _atomicBitWidth(_64)
10+
#if arch(i386) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
11+
#if _hasAtomicBitWidth(_8) && _hasAtomicBitWidth(_16) && _hasAtomicBitWidth(_32) && _hasAtomicBitWidth(_64)
1112
class C {}
1213
var x = C()
1314
#endif
15+
#endif
1416
var y = x

0 commit comments

Comments
 (0)