Skip to content

Commit 3ce3e1b

Browse files
[Frontend] Print least valid pointer value in -print-target-info
This allows swift-driver to use the value when constructing the linker command line for platforms that need it like WebAssembly.
1 parent 2b6ea81 commit 3ce3e1b

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

include/swift/Basic/Platform.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace llvm {
2525
}
2626

2727
namespace swift {
28+
class LangOptions;
2829

2930
enum class DarwinPlatformKind : unsigned {
3031
MacOS,
@@ -75,6 +76,12 @@ namespace swift {
7576
/// that enforces BTCFI by default.
7677
bool tripleBTCFIByDefaultInOpenBSD(const llvm::Triple &triple);
7778

79+
/// Returns the least valid pointer value for the given target triple.
80+
uint64_t
81+
getLeastValidPointerValueForTriple(const llvm::Triple &triple,
82+
const LangOptions &LangOpts,
83+
uint64_t customLeastValidPointerValue);
84+
7885
/// Returns the platform name for a given target triple.
7986
///
8087
/// For example, the iOS simulator has the name "iphonesimulator", while real

lib/Basic/Platform.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Basic/Assertions.h"
14+
#include "swift/ABI/System.h"
15+
#include "swift/Basic/Feature.h"
16+
#include "swift/Basic/LangOptions.h"
1417
#include "swift/Basic/Pack.h"
1518
#include "swift/Basic/Platform.h"
1619
#include "llvm/ADT/StringExtras.h"
@@ -144,6 +147,33 @@ bool swift::tripleBTCFIByDefaultInOpenBSD(const llvm::Triple &triple) {
144147
return triple.isOSOpenBSD() && triple.getArch() == llvm::Triple::aarch64;
145148
}
146149

150+
uint64_t swift::getLeastValidPointerValueForTriple(const llvm::Triple &triple,
151+
const LangOptions &LangOpts,
152+
uint64_t customLeastValidPointerValue) {
153+
if (customLeastValidPointerValue != 0)
154+
return customLeastValidPointerValue;
155+
156+
uint64_t value = SWIFT_ABI_DEFAULT_LEAST_VALID_POINTER;
157+
158+
if (triple.isOSDarwin() && !LangOpts.hasFeature(Feature::Embedded)) {
159+
// Non-embedded Darwin reserves the low 4GB of address space.
160+
switch (triple.getArch()) {
161+
case llvm::Triple::x86_64:
162+
value = SWIFT_ABI_DARWIN_X86_64_LEAST_VALID_POINTER;
163+
break;
164+
case llvm::Triple::aarch64:
165+
value = SWIFT_ABI_DARWIN_ARM64_LEAST_VALID_POINTER;
166+
break;
167+
default:
168+
break;
169+
}
170+
} else if (triple.getArch() == llvm::Triple::wasm32) {
171+
value = SWIFT_ABI_WASM32_LEAST_VALID_POINTER;
172+
}
173+
174+
return value;
175+
}
176+
147177
DarwinPlatformKind swift::getDarwinPlatformKind(const llvm::Triple &triple) {
148178
if (triple.isiOS()) {
149179
if (triple.isTvOS()) {

lib/Basic/TargetInfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ void printTripleInfo(const CompilerInvocation &invocation,
157157
out << " \"pointerWidthInBytes\": "
158158
<< TI->getPointerWidth(clang::LangAS::Default) / TI->getCharWidth()
159159
<< ",\n";
160+
out << " \"leastValidPointerValue\": "
161+
<< getLeastValidPointerValueForTriple(
162+
triple, invocation.getLangOptions(),
163+
invocation.getIRGenOptions().CustomLeastValidPointerValue)
164+
<< ",\n";
160165

161166
if (runtimeVersion) {
162167
out << " \"swiftRuntimeCompatibilityVersion\": \"";

lib/IRGen/SwiftTargetInfo.cpp

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ static void configureARM64(IRGenModule &IGM, const llvm::Triple &triple,
4949
}
5050
setToMask(target.IsObjCPointerBit, 64, SWIFT_ABI_ARM64_IS_OBJC_BIT);
5151

52-
// Non-embedded Darwin reserves the low 4GB of address space.
53-
if (triple.isOSDarwin() &&
54-
!IGM.getSwiftModule()->getASTContext().LangOpts.hasFeature(
55-
Feature::Embedded)) {
56-
target.LeastValidPointerValue =
57-
SWIFT_ABI_DARWIN_ARM64_LEAST_VALID_POINTER;
58-
}
59-
6052
// arm64 has no special objc_msgSend variants, not even stret.
6153
target.ObjCUseStret = false;
6254

@@ -107,13 +99,6 @@ static void configureX86_64(IRGenModule &IGM, const llvm::Triple &triple,
10799
SWIFT_ABI_X86_64_OBJC_RESERVED_BITS_MASK);
108100
}
109101

110-
if (triple.isOSDarwin() &&
111-
!IGM.getSwiftModule()->getASTContext().LangOpts.hasFeature(
112-
Feature::Embedded)) {
113-
target.LeastValidPointerValue =
114-
SWIFT_ABI_DARWIN_X86_64_LEAST_VALID_POINTER;
115-
}
116-
117102
// x86-64 has every objc_msgSend variant known to humankind.
118103
target.ObjCUseFPRet = true;
119104
target.ObjCUseFP2Ret = true;
@@ -184,8 +169,6 @@ static void configureSystemZ(IRGenModule &IGM, const llvm::Triple &triple,
184169
/// Configures target-specific information for wasm32 platforms.
185170
static void configureWasm32(IRGenModule &IGM, const llvm::Triple &triple,
186171
SwiftTargetInfo &target) {
187-
target.LeastValidPointerValue =
188-
SWIFT_ABI_WASM32_LEAST_VALID_POINTER;
189172
}
190173

191174
/// Configure a default target.
@@ -268,8 +251,9 @@ SwiftTargetInfo SwiftTargetInfo::get(IRGenModule &IGM) {
268251
break;
269252
}
270253

271-
if (IGM.getOptions().CustomLeastValidPointerValue != 0)
272-
target.LeastValidPointerValue = IGM.getOptions().CustomLeastValidPointerValue;
254+
target.LeastValidPointerValue = getLeastValidPointerValueForTriple(
255+
triple, IGM.Context.LangOpts,
256+
IGM.getOptions().CustomLeastValidPointerValue);
273257

274258
return target;
275259
}

0 commit comments

Comments
 (0)