Basic: query the target pointer width from clang

Use the `clang::TargetInfo` to query the target pointer size for the
given triple. This is meant to enable us to properly determine
`CMAKE_SIZEOF_VOID_P`.
This commit is contained in:
Saleem Abdulrasool
2025-02-07 12:42:00 -08:00
parent 024f4bbd21
commit a04c75a43a
4 changed files with 52 additions and 16 deletions

View File

@@ -33,7 +33,8 @@ namespace targetinfo {
void printTargetInfo(const CompilerInvocation &invocation, void printTargetInfo(const CompilerInvocation &invocation,
llvm::raw_ostream &out); llvm::raw_ostream &out);
void printTripleInfo(const llvm::Triple &triple, void printTripleInfo(const CompilerInvocation &invocation,
const llvm::Triple &triple,
std::optional<llvm::VersionTuple> runtimeVersion, std::optional<llvm::VersionTuple> runtimeVersion,
llvm::raw_ostream &out); llvm::raw_ostream &out);
} // namespace targetinfo } // namespace targetinfo

View File

@@ -97,6 +97,7 @@ _swift_gyb_target_sources(swiftBasic PRIVATE
UnicodeExtendedGraphemeClusters.cpp.gyb) UnicodeExtendedGraphemeClusters.cpp.gyb)
target_include_directories(swiftBasic PRIVATE target_include_directories(swiftBasic PRIVATE
clangBasic
${UUID_INCLUDE}) ${UUID_INCLUDE})
target_link_libraries(swiftBasic PUBLIC target_link_libraries(swiftBasic PUBLIC

View File

@@ -16,6 +16,7 @@
#include "swift/Basic/StringExtras.h" #include "swift/Basic/StringExtras.h"
#include "swift/Frontend/Frontend.h" #include "swift/Frontend/Frontend.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
using namespace swift; using namespace swift;
@@ -52,9 +53,11 @@ static void printCompatibilityLibrary(
printedAny = true; printedAny = true;
} }
namespace swift {
namespace targetinfo {
/// Print information about the selected target in JSON. /// Print information about the selected target in JSON.
void targetinfo::printTargetInfo(const CompilerInvocation &invocation, void printTargetInfo(const CompilerInvocation &invocation,
llvm::raw_ostream &out) { llvm::raw_ostream &out) {
out << "{\n"; out << "{\n";
// Compiler version, as produced by --version. // Compiler version, as produced by --version.
@@ -67,12 +70,12 @@ void targetinfo::printTargetInfo(const CompilerInvocation &invocation,
invocation.getIRGenOptions().AutolinkRuntimeCompatibilityLibraryVersion; invocation.getIRGenOptions().AutolinkRuntimeCompatibilityLibraryVersion;
auto &langOpts = invocation.getLangOptions(); auto &langOpts = invocation.getLangOptions();
out << " \"target\": "; out << " \"target\": ";
printTripleInfo(langOpts.Target, runtimeVersion, out); printTripleInfo(invocation, langOpts.Target, runtimeVersion, out);
out << ",\n"; out << ",\n";
if (auto &variant = langOpts.TargetVariant) { if (auto &variant = langOpts.TargetVariant) {
out << " \"targetVariant\": "; out << " \"targetVariant\": ";
printTripleInfo(*variant, runtimeVersion, out); printTripleInfo(invocation, *variant, runtimeVersion, out);
out << ",\n"; out << ",\n";
} }
@@ -112,9 +115,10 @@ void targetinfo::printTargetInfo(const CompilerInvocation &invocation,
} }
// Print information about the target triple in JSON. // Print information about the target triple in JSON.
void targetinfo::printTripleInfo( void printTripleInfo(const CompilerInvocation &invocation,
const llvm::Triple &triple, const llvm::Triple &triple,
std::optional<llvm::VersionTuple> runtimeVersion, llvm::raw_ostream &out) { std::optional<llvm::VersionTuple> runtimeVersion,
llvm::raw_ostream &out) {
out << "{\n"; out << "{\n";
out << " \"triple\": \""; out << " \"triple\": \"";
@@ -130,7 +134,21 @@ void targetinfo::printTripleInfo(
out << "\",\n"; out << "\",\n";
out << " \"platform\": \"" << getPlatformNameForTriple(triple) << "\",\n"; out << " \"platform\": \"" << getPlatformNameForTriple(triple) << "\",\n";
out << " \"arch\": \"" << swift::getMajorArchitectureName(triple) << "\",\n"; out << " \"arch\": \"" << swift::getMajorArchitectureName(triple)
<< "\",\n";
clang::DiagnosticsEngine DE{new clang::DiagnosticIDs(),
new clang::DiagnosticOptions(),
new clang::IgnoringDiagConsumer()};
std::shared_ptr<clang::TargetOptions> TO =
std::make_shared<clang::TargetOptions>();
TO->Triple = triple.str();
clang::TargetInfo *TI = clang::TargetInfo::CreateTargetInfo(DE, TO);
out << " \"pointerWidthInBits\": "
<< TI->getPointerWidth(clang::LangAS::Default) << ",\n";
out << " \"pointerWidthInBytes\": "
<< TI->getPointerWidth(clang::LangAS::Default) / TI->getCharWidth()
<< ",\n";
if (runtimeVersion) { if (runtimeVersion) {
out << " \"swiftRuntimeCompatibilityVersion\": \""; out << " \"swiftRuntimeCompatibilityVersion\": \"";
@@ -140,15 +158,14 @@ void targetinfo::printTripleInfo(
// Compatibility libraries that need to be linked. // Compatibility libraries that need to be linked.
out << " \"compatibilityLibraries\": ["; out << " \"compatibilityLibraries\": [";
bool printedAnyCompatibilityLibrary = false; bool printedAnyCompatibilityLibrary = false;
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName, ForceLoad) \ #define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName, ForceLoad) \
printCompatibilityLibrary( \ printCompatibilityLibrary(*runtimeVersion, llvm::VersionTuple Version, \
*runtimeVersion, llvm::VersionTuple Version, #Filter, LibraryName, \ #Filter, LibraryName, ForceLoad, \
ForceLoad, printedAnyCompatibilityLibrary, out); printedAnyCompatibilityLibrary, out);
#include "swift/Frontend/BackDeploymentLibs.def" #include "swift/Frontend/BackDeploymentLibs.def"
if (printedAnyCompatibilityLibrary) { if (printedAnyCompatibilityLibrary)
out << "\n "; out << "\n ";
}
out << " ],\n"; out << " ],\n";
} else { } else {
out << " \"compatibilityLibraries\": [ ],\n"; out << " \"compatibilityLibraries\": [ ],\n";
@@ -160,3 +177,5 @@ void targetinfo::printTripleInfo(
out << " }"; out << " }";
} }
} // namespace targetinfo
} // namespace swift

View File

@@ -16,6 +16,9 @@
// RUN: %swift_driver -print-target-info -target x86_64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS-SIM %s // RUN: %swift_driver -print-target-info -target x86_64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS-SIM %s
// RUN: %swift_frontend_plain -target thumbv7-unknown-windows-msvc -print-target-info | %FileCheck -check-prefix CHECK-PTR-SIZE-32 %s
// RUN: %swift_frontend_plain -target aarch64-unknown-windows-msvc -print-target-info | %FileCheck -check-prefix CHECK-PTR-SIZE-64 %s
// CHECK-IOS: "compilerVersion": "{{.*}}Swift version // CHECK-IOS: "compilerVersion": "{{.*}}Swift version
// CHECK-IOS: "target": { // CHECK-IOS: "target": {
@@ -107,3 +110,15 @@
// CHECK-IOS-SIM: "swiftRuntimeCompatibilityVersion": "5.0", // CHECK-IOS-SIM: "swiftRuntimeCompatibilityVersion": "5.0",
// CHECK-IOS-SIM: "librariesRequireRPath": true // CHECK-IOS-SIM: "librariesRequireRPath": true
// CHECK-IOS-SIM: } // CHECK-IOS-SIM: }
// CHECK-PTR-SIZE-32: "target": {
// CHECK-PTR-SIZE-32: "triple": "thumbv7-unknown-windows-msvc",
// CHECK-PTR-SIZE-32: "pointerWidthInBits": 32,
// CHECK-PTR-SIZE-32: "pointerWidthInBytes": 4,
// CHECK-PTR-SIZE-32: }
// CHECK-PTR-SIZE-64: "target": {
// CHECK-PTR-SIZE-64: "triple": "aarch64-unknown-windows-msvc",
// CHECK-PTR-SIZE-64: "pointerWidthInBits": 64,
// CHECK-PTR-SIZE-64: "pointerWidthInBytes": 8,
// CHECK-PTR-SIZE-64: }