mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[cxx-interop] Support SIMD types in reverse interop
This supports both built-in vector types and the SIMDX<T> Swift types that are used through type aliases like float3. These SIMD types are passed via compatibility structs (with their valued memcpy-d into the ABI compatible struct) as they have special ABI rules. E.g., a float3 is not passed as float3, but as float4 on the ABI level. Previously, we did not expose simd types from Swift to C++ (unless the SIMD types were impoted from clang). rdar://153218744
This commit is contained in:
@@ -38,6 +38,7 @@
|
||||
#include "clang/CodeGen/ModuleBuilder.h"
|
||||
#include "clang/CodeGen/SwiftCallingConv.h"
|
||||
#include "llvm/IR/DerivedTypes.h"
|
||||
#include <optional>
|
||||
|
||||
using namespace swift;
|
||||
using namespace irgen;
|
||||
@@ -59,14 +60,25 @@ getPrimitiveTypeFromLLVMType(ASTContext &ctx, const llvm::Type *type) {
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
} else if (type->isFloatTy()) {
|
||||
}
|
||||
if (type->isFloatTy()) {
|
||||
return ctx.getFloatType();
|
||||
} else if (type->isDoubleTy()) {
|
||||
}
|
||||
if (type->isDoubleTy()) {
|
||||
return ctx.getDoubleType();
|
||||
} else if (type->isPointerTy()) {
|
||||
}
|
||||
if (type->isPointerTy()) {
|
||||
return ctx.getOpaquePointerType();
|
||||
}
|
||||
// FIXME: Handle vector type.
|
||||
if (const auto *vecTy = dyn_cast<llvm::VectorType>(type)) {
|
||||
auto elemTy = getPrimitiveTypeFromLLVMType(ctx, vecTy->getElementType());
|
||||
if (!elemTy)
|
||||
return std::nullopt;
|
||||
auto elemCount = vecTy->getElementCount();
|
||||
if (!elemCount.isFixed())
|
||||
return std::nullopt;
|
||||
return BuiltinVectorType::get(ctx, *elemTy, elemCount.getFixedValue());
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,15 +13,16 @@
|
||||
#include "PrimitiveTypeMapping.h"
|
||||
#include "swift/AST/ASTContext.h"
|
||||
#include "swift/AST/Decl.h"
|
||||
#include "swift/AST/Identifier.h"
|
||||
#include "swift/AST/Module.h"
|
||||
#include "swift/Basic/Assertions.h"
|
||||
#include "swift/ClangImporter/ClangImporter.h"
|
||||
#include <optional>
|
||||
|
||||
using namespace swift;
|
||||
|
||||
/// Find the implementation of the named type in the named module if loaded.
|
||||
static TypeDecl *findTypeInModuleByName(ASTContext &ctx,
|
||||
Identifier moduleName,
|
||||
static TypeDecl *findTypeInModuleByName(ASTContext &ctx, Identifier moduleName,
|
||||
Identifier typeName) {
|
||||
auto module = ctx.getLoadedModule(moduleName);
|
||||
if (!module)
|
||||
@@ -69,18 +70,15 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
|
||||
|
||||
// Map stdlib types.
|
||||
#define MAP(SWIFT_NAME, CLANG_REPR, NEEDS_NULLABILITY) \
|
||||
addMappedType(ctx.StdlibModuleName, \
|
||||
ctx.getIdentifier(#SWIFT_NAME), \
|
||||
addMappedType(ctx.StdlibModuleName, ctx.getIdentifier(#SWIFT_NAME), \
|
||||
{CLANG_REPR, std::optional<StringRef>(CLANG_REPR), \
|
||||
std::optional<StringRef>(CLANG_REPR), NEEDS_NULLABILITY})
|
||||
#define MAP_C(SWIFT_NAME, OBJC_REPR, C_REPR, NEEDS_NULLABILITY) \
|
||||
addMappedType(ctx.StdlibModuleName, \
|
||||
ctx.getIdentifier(#SWIFT_NAME), \
|
||||
addMappedType(ctx.StdlibModuleName, ctx.getIdentifier(#SWIFT_NAME), \
|
||||
{OBJC_REPR, std::optional<StringRef>(C_REPR), \
|
||||
std::optional<StringRef>(C_REPR), NEEDS_NULLABILITY})
|
||||
#define MAP_CXX(SWIFT_NAME, OBJC_REPR, C_REPR, CXX_REPR, NEEDS_NULLABILITY) \
|
||||
addMappedType(ctx.StdlibModuleName, \
|
||||
ctx.getIdentifier(#SWIFT_NAME), \
|
||||
addMappedType(ctx.StdlibModuleName, ctx.getIdentifier(#SWIFT_NAME), \
|
||||
{OBJC_REPR, std::optional<StringRef>(C_REPR), \
|
||||
std::optional<StringRef>(CXX_REPR), NEEDS_NULLABILITY})
|
||||
|
||||
@@ -140,10 +138,10 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
|
||||
{"BOOL", std::nullopt, std::nullopt, false});
|
||||
addMappedType(ctx.Id_ObjectiveC, ctx.getIdentifier("Selector"),
|
||||
{"SEL", std::nullopt, std::nullopt, true});
|
||||
addMappedType(ctx.Id_ObjectiveC,
|
||||
ctx.getIdentifier(swift::getSwiftName(
|
||||
KnownFoundationEntity::NSZone)),
|
||||
{"struct _NSZone *", std::nullopt, std::nullopt, true});
|
||||
addMappedType(
|
||||
ctx.Id_ObjectiveC,
|
||||
ctx.getIdentifier(swift::getSwiftName(KnownFoundationEntity::NSZone)),
|
||||
{"struct _NSZone *", std::nullopt, std::nullopt, true});
|
||||
|
||||
addMappedType(ctx.Id_Darwin, ctx.getIdentifier("DarwinBoolean"),
|
||||
{"Boolean", std::nullopt, std::nullopt, false});
|
||||
@@ -157,17 +155,33 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
|
||||
// Use typedefs we set up for SIMD vector types.
|
||||
#define MAP_SIMD_TYPE(BASENAME, _, __) \
|
||||
StringRef simd2##BASENAME = "swift_" #BASENAME "2"; \
|
||||
addMappedType(ctx.Id_simd, ctx.getIdentifier(#BASENAME "2"), \
|
||||
{simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false}, \
|
||||
/*applyToUnderlying*/false); \
|
||||
addMappedType( \
|
||||
ctx.Id_simd, ctx.getIdentifier(#BASENAME "2"), \
|
||||
{simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false, true}, \
|
||||
/*applyToUnderlying*/ false); \
|
||||
addMappedType( \
|
||||
ctx.Id_simd, ctx.getIdentifier("simd_" #BASENAME "2"), \
|
||||
{simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false, true}, \
|
||||
/*applyToUnderlying*/ false); \
|
||||
StringRef simd3##BASENAME = "swift_" #BASENAME "3"; \
|
||||
addMappedType(ctx.Id_simd, ctx.getIdentifier(#BASENAME "3"), \
|
||||
{simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false}, \
|
||||
/*applyToUnderlying*/false); \
|
||||
addMappedType( \
|
||||
ctx.Id_simd, ctx.getIdentifier(#BASENAME "3"), \
|
||||
{simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false, true}, \
|
||||
/*applyToUnderlying*/ false); \
|
||||
addMappedType( \
|
||||
ctx.Id_simd, ctx.getIdentifier("simd_" #BASENAME "3"), \
|
||||
{simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false, true}, \
|
||||
/*applyToUnderlying*/ false); \
|
||||
StringRef simd4##BASENAME = "swift_" #BASENAME "4"; \
|
||||
addMappedType(ctx.Id_simd, ctx.getIdentifier(#BASENAME "4"), \
|
||||
{simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false}, \
|
||||
/*applyToUnderlying*/false);
|
||||
addMappedType( \
|
||||
ctx.Id_simd, ctx.getIdentifier(#BASENAME "4"), \
|
||||
{simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false, true}, \
|
||||
/*applyToUnderlying*/ false); \
|
||||
addMappedType( \
|
||||
ctx.Id_simd, ctx.getIdentifier("simd_" #BASENAME "4"), \
|
||||
{simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false, true}, \
|
||||
/*applyToUnderlying*/ false);
|
||||
|
||||
#include "swift/ClangImporter/SIMDMappedTypes.def"
|
||||
static_assert(SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4,
|
||||
"must add or remove special name mappings if max number of "
|
||||
@@ -192,7 +206,8 @@ PrimitiveTypeMapping::getMappedTypeInfoOrNull(const TypeDecl *typeDecl) {
|
||||
std::optional<PrimitiveTypeMapping::ClangTypeInfo>
|
||||
PrimitiveTypeMapping::getKnownObjCTypeInfo(const TypeDecl *typeDecl) {
|
||||
if (auto *typeInfo = getMappedTypeInfoOrNull(typeDecl))
|
||||
return ClangTypeInfo{typeInfo->objcName, typeInfo->canBeNullable};
|
||||
return ClangTypeInfo{typeInfo->objcName, typeInfo->canBeNullable,
|
||||
typeInfo->simd};
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
@@ -200,7 +215,8 @@ std::optional<PrimitiveTypeMapping::ClangTypeInfo>
|
||||
PrimitiveTypeMapping::getKnownCTypeInfo(const TypeDecl *typeDecl) {
|
||||
if (auto *typeInfo = getMappedTypeInfoOrNull(typeDecl)) {
|
||||
if (typeInfo->cName)
|
||||
return ClangTypeInfo{*typeInfo->cName, typeInfo->canBeNullable};
|
||||
return ClangTypeInfo{*typeInfo->cName, typeInfo->canBeNullable,
|
||||
typeInfo->simd};
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
@@ -209,7 +225,25 @@ std::optional<PrimitiveTypeMapping::ClangTypeInfo>
|
||||
PrimitiveTypeMapping::getKnownCxxTypeInfo(const TypeDecl *typeDecl) {
|
||||
if (auto *typeInfo = getMappedTypeInfoOrNull(typeDecl)) {
|
||||
if (typeInfo->cxxName)
|
||||
return ClangTypeInfo{*typeInfo->cxxName, typeInfo->canBeNullable};
|
||||
return ClangTypeInfo{*typeInfo->cxxName, typeInfo->canBeNullable,
|
||||
typeInfo->simd};
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<PrimitiveTypeMapping::ClangTypeInfo>
|
||||
PrimitiveTypeMapping::getKnownSIMDTypeInfo(Type t, ASTContext &ctx) {
|
||||
auto vecTy = t->getAs<BuiltinVectorType>();
|
||||
if (!vecTy)
|
||||
return std::nullopt;
|
||||
|
||||
auto elemTy = vecTy->getElementType();
|
||||
auto numElems = vecTy->getNumElements();
|
||||
|
||||
std::string elemTyName = elemTy.getString();
|
||||
// While the element type starts with an upper case, vector types start with
|
||||
// lower case.
|
||||
elemTyName[0] = std::tolower(elemTyName[0]);
|
||||
Identifier swiftName = ctx.getIdentifier("swift_" + elemTyName + std::to_string(numElems));
|
||||
return ClangTypeInfo{swiftName.str(), false, true};
|
||||
}
|
||||
|
||||
@@ -21,18 +21,21 @@ namespace swift {
|
||||
|
||||
class ASTContext;
|
||||
class TypeDecl;
|
||||
class Type;
|
||||
|
||||
/// Provides a mapping from Swift's primitive types to C / Objective-C / C++
|
||||
/// primitive types.
|
||||
///
|
||||
/// Certain types have mappings that differ in different language modes.
|
||||
/// For example, Swift's `Int` maps to `NSInteger` for Objective-C declarations,
|
||||
/// but to something like `intptr_t` or `swift::Int` for C and C++ declarations.
|
||||
/// For example, Swift's `Int` maps to `NSInteger` for Objective-C
|
||||
/// declarations, but to something like `intptr_t` or `swift::Int` for C and
|
||||
/// C++ declarations.
|
||||
class PrimitiveTypeMapping {
|
||||
public:
|
||||
struct ClangTypeInfo {
|
||||
StringRef name;
|
||||
bool canBeNullable;
|
||||
bool simd;
|
||||
};
|
||||
|
||||
/// Returns the Objective-C type name and nullability for the given Swift
|
||||
@@ -47,6 +50,8 @@ public:
|
||||
/// primitive type declaration, or \c None if no such type name exists.
|
||||
std::optional<ClangTypeInfo> getKnownCxxTypeInfo(const TypeDecl *typeDecl);
|
||||
|
||||
std::optional<ClangTypeInfo> getKnownSIMDTypeInfo(Type t, ASTContext &ctx);
|
||||
|
||||
private:
|
||||
void initialize(ASTContext &ctx);
|
||||
|
||||
@@ -58,6 +63,7 @@ private:
|
||||
// The C++ name of the Swift type.
|
||||
std::optional<StringRef> cxxName;
|
||||
bool canBeNullable;
|
||||
bool simd = false;
|
||||
};
|
||||
|
||||
FullClangTypeInfo *getMappedTypeInfoOrNull(const TypeDecl *typeDecl);
|
||||
|
||||
@@ -52,48 +52,62 @@ getKnownTypeInfo(const TypeDecl *typeDecl, PrimitiveTypeMapping &typeMapping,
|
||||
: typeMapping.getKnownCTypeInfo(typeDecl);
|
||||
}
|
||||
|
||||
bool isKnownType(Type t, PrimitiveTypeMapping &typeMapping,
|
||||
OutputLanguageMode languageMode) {
|
||||
enum class KnownTypeKind { Known, KnownSIMD, Unknown };
|
||||
|
||||
KnownTypeKind isKnownType(Type t, PrimitiveTypeMapping &typeMapping,
|
||||
OutputLanguageMode languageMode, ASTContext &ctx) {
|
||||
if (auto *typeAliasType = dyn_cast<TypeAliasType>(t.getPointer())) {
|
||||
auto aliasInfo =
|
||||
getKnownTypeInfo(typeAliasType->getDecl(), typeMapping, languageMode);
|
||||
if (aliasInfo != std::nullopt)
|
||||
return true;
|
||||
return aliasInfo->simd ? KnownTypeKind::KnownSIMD : KnownTypeKind::Known;
|
||||
return isKnownType(typeAliasType->getSinglyDesugaredType(), typeMapping,
|
||||
languageMode);
|
||||
languageMode, ctx);
|
||||
}
|
||||
|
||||
const TypeDecl *typeDecl;
|
||||
auto *tPtr = t->isOptional() ? t->getOptionalObjectType()->getDesugaredType()
|
||||
: t->getDesugaredType();
|
||||
if (auto *bgt = dyn_cast<BoundGenericStructType>(tPtr)) {
|
||||
return bgt->isUnsafePointer() || bgt->isUnsafeMutablePointer();
|
||||
return (bgt->isUnsafePointer() || bgt->isUnsafeMutablePointer())
|
||||
? KnownTypeKind::Known
|
||||
: KnownTypeKind::Unknown;
|
||||
}
|
||||
if (auto *structType = dyn_cast<StructType>(tPtr)) {
|
||||
auto nullableInfo =
|
||||
getKnownTypeInfo(structType->getDecl(), typeMapping, languageMode);
|
||||
if (nullableInfo && nullableInfo->canBeNullable)
|
||||
return true;
|
||||
return nullableInfo->simd ? KnownTypeKind::KnownSIMD
|
||||
: KnownTypeKind::Known;
|
||||
}
|
||||
if (auto *classType = dyn_cast<ClassType>(tPtr)) {
|
||||
return classType->getClassOrBoundGenericClass()->hasClangNode() &&
|
||||
isa<clang::ObjCInterfaceDecl>(
|
||||
classType->getClassOrBoundGenericClass()->getClangDecl());
|
||||
return (classType->getClassOrBoundGenericClass()->hasClangNode() &&
|
||||
isa<clang::ObjCInterfaceDecl>(
|
||||
classType->getClassOrBoundGenericClass()->getClangDecl()))
|
||||
? KnownTypeKind::Known
|
||||
: KnownTypeKind::Unknown;
|
||||
}
|
||||
if (t->getAs<BuiltinVectorType>())
|
||||
return typeMapping.getKnownSIMDTypeInfo(t, ctx) ? KnownTypeKind::KnownSIMD
|
||||
: KnownTypeKind::Unknown;
|
||||
|
||||
if (auto *structDecl = t->getStructOrBoundGenericStruct())
|
||||
typeDecl = structDecl;
|
||||
else
|
||||
return false;
|
||||
return getKnownTypeInfo(typeDecl, typeMapping, languageMode) != std::nullopt;
|
||||
return KnownTypeKind::Unknown;
|
||||
return getKnownTypeInfo(typeDecl, typeMapping, languageMode) != std::nullopt
|
||||
? KnownTypeKind::Known
|
||||
: KnownTypeKind::Unknown;
|
||||
}
|
||||
|
||||
bool isKnownCxxType(Type t, PrimitiveTypeMapping &typeMapping) {
|
||||
return isKnownType(t, typeMapping, OutputLanguageMode::Cxx);
|
||||
KnownTypeKind isKnownCxxType(Type t, PrimitiveTypeMapping &typeMapping,
|
||||
ASTContext &ctx) {
|
||||
return isKnownType(t, typeMapping, OutputLanguageMode::Cxx, ctx);
|
||||
}
|
||||
|
||||
bool isKnownCType(Type t, PrimitiveTypeMapping &typeMapping) {
|
||||
return isKnownType(t, typeMapping, OutputLanguageMode::ObjC);
|
||||
KnownTypeKind isKnownCType(Type t, PrimitiveTypeMapping &typeMapping,
|
||||
ASTContext &ctx) {
|
||||
return isKnownType(t, typeMapping, OutputLanguageMode::ObjC, ctx);
|
||||
}
|
||||
|
||||
struct CFunctionSignatureTypePrinterModifierDelegate {
|
||||
@@ -151,7 +165,7 @@ public:
|
||||
static void
|
||||
printGenericReturnScaffold(raw_ostream &os, StringRef templateParamName,
|
||||
llvm::function_ref<void(StringRef)> bodyOfReturn) {
|
||||
printReturnScaffold(nullptr, os, templateParamName, templateParamName,
|
||||
printReturnScaffold(false, os, templateParamName, templateParamName,
|
||||
bodyOfReturn);
|
||||
}
|
||||
|
||||
@@ -165,13 +179,19 @@ public:
|
||||
llvm::raw_string_ostream unqualTypeNameOS(typeName);
|
||||
unqualTypeNameOS << typeDecl->getName();
|
||||
}
|
||||
printReturnScaffold(typeDecl, os, fullQualifiedType, typeName,
|
||||
printReturnScaffold(isTrivial(typeDecl), os, fullQualifiedType, typeName,
|
||||
bodyOfReturn);
|
||||
}
|
||||
|
||||
static void
|
||||
printSIMDReturnScaffold(StringRef simdTypeName, raw_ostream &os,
|
||||
llvm::function_ref<void(StringRef)> bodyOfReturn) {
|
||||
printReturnScaffold(true, os, simdTypeName, simdTypeName, bodyOfReturn);
|
||||
}
|
||||
|
||||
private:
|
||||
static void
|
||||
printReturnScaffold(const clang::TagDecl *typeDecl, raw_ostream &os,
|
||||
printReturnScaffold(bool isTrivial, raw_ostream &os,
|
||||
StringRef fullQualifiedType, StringRef typeName,
|
||||
llvm::function_ref<void(StringRef)> bodyOfReturn) {
|
||||
os << "alignas(alignof(" << fullQualifiedType << ")) char storage[sizeof("
|
||||
@@ -180,7 +200,7 @@ private:
|
||||
<< fullQualifiedType << " *>(storage);\n";
|
||||
bodyOfReturn("storage");
|
||||
os << ";\n";
|
||||
if (isTrivial(typeDecl)) {
|
||||
if (isTrivial) {
|
||||
// Trivial object can be just copied and not destroyed.
|
||||
os << "return *storageObjectPtr;\n";
|
||||
return;
|
||||
@@ -468,7 +488,8 @@ public:
|
||||
llvm::SaveAndRestore<FunctionSignatureTypeUse> typeUseNormal(
|
||||
typeUseKind, FunctionSignatureTypeUse::TypeReference);
|
||||
// FIXME: We can definitely support pointers to known Clang types.
|
||||
if (!isKnownCType(args.front(), typeMapping))
|
||||
if (isKnownCType(args.front(), typeMapping, BGT->getASTContext()) ==
|
||||
KnownTypeKind::Unknown)
|
||||
return ClangRepresentation(ClangRepresentation::unsupported);
|
||||
auto partRepr = visitPart(args.front(), OTK_None, /*isInOutParam=*/false);
|
||||
if (partRepr.isUnsupported())
|
||||
@@ -577,9 +598,11 @@ DeclAndTypeClangFunctionPrinter::printClangFunctionReturnType(
|
||||
static void addABIRecordToTypeEncoding(llvm::raw_ostream &typeEncodingOS,
|
||||
clang::CharUnits offset,
|
||||
clang::CharUnits end, Type t,
|
||||
PrimitiveTypeMapping &typeMapping) {
|
||||
auto info =
|
||||
typeMapping.getKnownCTypeInfo(t->getNominalOrBoundGenericNominal());
|
||||
PrimitiveTypeMapping &typeMapping,
|
||||
ASTContext &ctx) {
|
||||
auto info = typeMapping.getKnownSIMDTypeInfo(t, ctx);
|
||||
if (!info)
|
||||
info = typeMapping.getKnownCTypeInfo(t->getNominalOrBoundGenericNominal());
|
||||
assert(info);
|
||||
typeEncodingOS << '_';
|
||||
for (char c : info->name) {
|
||||
@@ -610,7 +633,8 @@ static std::string encodeTypeInfo(const T &abiTypeInfo,
|
||||
ClangSyntaxPrinter(moduleContext->getASTContext(), typeEncodingOS).printBaseName(moduleContext);
|
||||
abiTypeInfo.enumerateRecordMembers(
|
||||
[&](clang::CharUnits offset, clang::CharUnits end, Type t) {
|
||||
addABIRecordToTypeEncoding(typeEncodingOS, offset, end, t, typeMapping);
|
||||
addABIRecordToTypeEncoding(typeEncodingOS, offset, end, t, typeMapping,
|
||||
moduleContext->getASTContext());
|
||||
});
|
||||
return std::move(typeEncodingOS.str());
|
||||
}
|
||||
@@ -658,7 +682,8 @@ static bool printDirectReturnOrParamCType(
|
||||
clang::CharUnits end, Type t) {
|
||||
lastOffset = offset;
|
||||
++Count;
|
||||
addABIRecordToTypeEncoding(typeEncodingOS, offset, end, t, typeMapping);
|
||||
addABIRecordToTypeEncoding(typeEncodingOS, offset, end, t, typeMapping,
|
||||
emittedModule->getASTContext());
|
||||
}))
|
||||
return false;
|
||||
if (isResultType && Count == 0) {
|
||||
@@ -670,7 +695,8 @@ static bool printDirectReturnOrParamCType(
|
||||
assert(Count > 0 && "missing return values");
|
||||
|
||||
// FIXME: is this "prettyfying" logic sound for multiple return values?
|
||||
if (isKnownCType(valueType, typeMapping) ||
|
||||
if (isKnownCType(valueType, typeMapping, emittedModule->getASTContext()) ==
|
||||
KnownTypeKind::Known ||
|
||||
(Count == 1 && lastOffset.isZero() && !valueType->hasTypeParameter() &&
|
||||
(valueType->isAnyClassReferenceType() ||
|
||||
isOptionalObjCExistential(valueType) ||
|
||||
@@ -689,7 +715,10 @@ static bool printDirectReturnOrParamCType(
|
||||
abiTypeInfo.enumerateRecordMembers([&](clang::CharUnits offset,
|
||||
clang::CharUnits end, Type t) {
|
||||
auto info =
|
||||
typeMapping.getKnownCTypeInfo(t->getNominalOrBoundGenericNominal());
|
||||
typeMapping.getKnownSIMDTypeInfo(t, emittedModule->getASTContext());
|
||||
if (!info)
|
||||
info =
|
||||
typeMapping.getKnownCTypeInfo(t->getNominalOrBoundGenericNominal());
|
||||
os << " " << info->name;
|
||||
if (info->canBeNullable)
|
||||
os << " _Nullable";
|
||||
@@ -968,8 +997,8 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
|
||||
->isAnyClassReferenceType());
|
||||
if (isConst)
|
||||
functionSignatureOS << "const ";
|
||||
if (isKnownCType(param.getParamDecl().getInterfaceType(),
|
||||
typeMapping) ||
|
||||
if (isKnownCType(param.getParamDecl().getInterfaceType(), typeMapping,
|
||||
FD->getASTContext()) != KnownTypeKind::Unknown ||
|
||||
(!param.getParamDecl().getInterfaceType()->hasTypeParameter() &&
|
||||
param.getParamDecl()
|
||||
.getInterfaceType()
|
||||
@@ -1085,7 +1114,9 @@ void DeclAndTypeClangFunctionPrinter::printCxxToCFunctionParameterUse(
|
||||
Type type, StringRef name, const ModuleDecl *moduleContext, bool isInOut,
|
||||
bool isIndirect, std::string directTypeEncoding, bool forceSelf) {
|
||||
auto namePrinter = [&]() { ClangSyntaxPrinter(moduleContext->getASTContext(), os).printIdentifier(name); };
|
||||
if (!isKnownCxxType(type, typeMapping) &&
|
||||
auto knownTypeKind =
|
||||
isKnownCxxType(type, typeMapping, moduleContext->getASTContext());
|
||||
if (knownTypeKind != KnownTypeKind::Known &&
|
||||
!hasKnownOptionalNullableCxxMapping(type)) {
|
||||
if (type->is<GenericTypeParamType>()) {
|
||||
os << "swift::" << cxx_synthesis::getCxxImplNamespaceName()
|
||||
@@ -1120,21 +1151,21 @@ void DeclAndTypeClangFunctionPrinter::printCxxToCFunctionParameterUse(
|
||||
os << "::" << cxx_synthesis::getCxxImplNamespaceName()
|
||||
<< "::swift_interop_passDirect_" << directTypeEncoding << '(';
|
||||
}
|
||||
if (decl->hasClangNode()) {
|
||||
if (!directTypeEncoding.empty())
|
||||
os << "reinterpret_cast<const char *>(";
|
||||
os << "swift::" << cxx_synthesis::getCxxImplNamespaceName()
|
||||
<< "::getOpaquePointer(";
|
||||
namePrinter();
|
||||
if (decl->hasClangNode() || knownTypeKind == KnownTypeKind::KnownSIMD) {
|
||||
if (!directTypeEncoding.empty())
|
||||
os << "reinterpret_cast<const char *>(";
|
||||
os << "swift::" << cxx_synthesis::getCxxImplNamespaceName()
|
||||
<< "::getOpaquePointer(";
|
||||
namePrinter();
|
||||
os << ')';
|
||||
if (!directTypeEncoding.empty())
|
||||
os << ')';
|
||||
if (!directTypeEncoding.empty())
|
||||
os << ')';
|
||||
} else {
|
||||
ClangValueTypePrinter(os, cPrologueOS, interopContext)
|
||||
.printParameterCxxToCUseScaffold(
|
||||
moduleContext,
|
||||
[&]() { printTypeImplTypeSpecifier(type, moduleContext); },
|
||||
namePrinter, forceSelf);
|
||||
ClangValueTypePrinter(os, cPrologueOS, interopContext)
|
||||
.printParameterCxxToCUseScaffold(
|
||||
moduleContext,
|
||||
[&]() { printTypeImplTypeSpecifier(type, moduleContext); },
|
||||
namePrinter, forceSelf);
|
||||
}
|
||||
if (!directTypeEncoding.empty())
|
||||
os << ')';
|
||||
@@ -1469,7 +1500,9 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
|
||||
|
||||
// Values types are returned either direcly in their C representation, or
|
||||
// indirectly by a pointer.
|
||||
if (!isKnownCxxType(resultTy, typeMapping) &&
|
||||
auto knownTypeKind =
|
||||
isKnownCxxType(resultTy, typeMapping, moduleContext->getASTContext());
|
||||
if (knownTypeKind != KnownTypeKind::Known &&
|
||||
!hasKnownOptionalNullableCxxMapping(resultTy)) {
|
||||
if (const auto *gtpt = resultTy->getAs<GenericTypeParamType>()) {
|
||||
printGenericReturnSequence(os, gtpt, printCallToCFunc);
|
||||
@@ -1506,6 +1539,16 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
|
||||
printCallToCFunc(/*firstParam=*/resultPointerName);
|
||||
}
|
||||
};
|
||||
if (knownTypeKind == KnownTypeKind::KnownSIMD) {
|
||||
if (auto *typeAliasType =
|
||||
dyn_cast<TypeAliasType>(resultTy.getPointer())) {
|
||||
auto info = getKnownTypeInfo(typeAliasType->getDecl(), typeMapping,
|
||||
OutputLanguageMode::Cxx);
|
||||
ClangTypeHandler::printSIMDReturnScaffold(info->name, os,
|
||||
valueTypeReturnThunker);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (decl->hasClangNode()) {
|
||||
ClangTypeHandler handler(decl->getClangDecl());
|
||||
assert(handler.isRepresentable());
|
||||
|
||||
@@ -43,5 +43,14 @@ public func passStruct(_ x : Struct) {
|
||||
}
|
||||
|
||||
// CHECK: class SWIFT_SYMBOL("s:8UseCxxTy6StructV") Struct final {
|
||||
// CHECK-NOT: init(
|
||||
// CHECK: // Unavailable in C++: Swift global function 'passStruct(_:)'
|
||||
|
||||
// CHECK: SWIFT_INLINE_THUNK void passStruct(const Struct& x) noexcept SWIFT_SYMBOL("s:8UseCxxTy10passStructyyAA0E0VF") {
|
||||
// CHECK-NEXT: UseCxxTy::_impl::$s8UseCxxTy10passStructyyAA0E0VF(UseCxxTy::_impl::swift_interop_passDirect_UseCxxTy_swift_float4_0_16_swift_float4_16_32_swift_float4_32_48_swift_float4_48_64(UseCxxTy::_impl::_impl_Struct::getOpaquePointer(x)));
|
||||
// CHECK-NEXT:}
|
||||
|
||||
// CHECK: SWIFT_INLINE_THUNK Struct Struct::init() {
|
||||
// CHECK-NEXT: return UseCxxTy::_impl::_impl_Struct::returnNewValue([&](char * _Nonnull result) SWIFT_INLINE_THUNK_ATTRIBUTES {
|
||||
// CHECK-NEXT: UseCxxTy::_impl::swift_interop_returnDirect_UseCxxTy_swift_float4_0_16_swift_float4_16_32_swift_float4_32_48_swift_float4_48_64(result, UseCxxTy::_impl::$s8UseCxxTy6StructVACycfC());
|
||||
// CHECK-NEXT: });
|
||||
// CHECK-NEXT: }
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// RUN: %empty-directory(%t)
|
||||
|
||||
// RUN: %target-swift-frontend %S/swift-simd-in-cxx.swift -module-name SIMD -clang-header-expose-decls=all-public -typecheck -verify -emit-clang-header-path %t/simd.h
|
||||
|
||||
// RUN: %target-interop-build-clangxx -c %s -I %t -o %t/swift-simd-execution.o
|
||||
// RUN: %target-interop-build-swift %S/swift-simd-in-cxx.swift -o %t/swift-simd-execution -Xlinker %t/swift-simd-execution.o -module-name SIMD -Xfrontend -entry-point-function-name -Xfrontend swiftMain
|
||||
|
||||
// RUN: %target-codesign %t/swift-simd-execution
|
||||
// RUN: %target-run %t/swift-simd-execution | %FileCheck %s
|
||||
|
||||
// REQUIRES: executable_test
|
||||
// REQUIRES: rdar157848231
|
||||
|
||||
#include "simd.h"
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
swift_float3 vec{1.0, 2.0, 3.0};
|
||||
SIMD::swiftThingSIMD(vec);
|
||||
// CHECK: SIMD3<Float>(1.0, 2.0, 3.0)
|
||||
SIMD::swiftThingSIMD2(vec);
|
||||
// CHECK: SIMD3<Float>(1.0, 2.0, 3.0)
|
||||
swift_float3 vec2 = SIMD::swiftThingSIMD3();
|
||||
std::cout << vec2.x << " " << vec2.y << " " << vec2.z << "\n";
|
||||
// CHECK: 4 5 6
|
||||
vec2 = SIMD::swiftThingSIMD4();
|
||||
std::cout << vec2.x << " " << vec2.y << " " << vec2.z << "\n";
|
||||
// CHECK: 4 5 6
|
||||
return 0;
|
||||
}
|
||||
|
||||
61
test/Interop/SwiftToCxx/stdlib/swift-simd-in-cxx.swift
Normal file
61
test/Interop/SwiftToCxx/stdlib/swift-simd-in-cxx.swift
Normal file
@@ -0,0 +1,61 @@
|
||||
// RUN: %empty-directory(%t)
|
||||
|
||||
// RUN: %target-swift-frontend %s -module-name UseSIMD -cxx-interoperability-mode=default -clang-header-expose-decls=all-public -typecheck -verify -emit-clang-header-path %t/UseSIMD.h
|
||||
// RUN: %FileCheck %s < %t/UseSIMD.h
|
||||
|
||||
// RUN: %target-interop-build-clangxx -std=gnu++20 -fobjc-arc -c -x objective-c++-header %t/UseSIMD.h -o %t/o.o
|
||||
|
||||
// REQUIRES: objc_interop
|
||||
// REQUIRES: rdar157848231
|
||||
|
||||
import simd
|
||||
|
||||
public func swiftThingScalar(a: Float) {}
|
||||
|
||||
public func swiftThingSIMD(a: float3) {
|
||||
print(a)
|
||||
}
|
||||
|
||||
public func swiftThingSIMD2(a: simd_float3) {
|
||||
print(a)
|
||||
}
|
||||
|
||||
public func swiftThingSIMD3() -> float3 {
|
||||
float3(4, 5, 6)
|
||||
}
|
||||
|
||||
public func swiftThingSIMD4() -> simd_float3 {
|
||||
simd_float3(4, 5, 6)
|
||||
}
|
||||
|
||||
// CHECK: SWIFT_EXTERN void $s7UseSIMD010swiftThingB01ays5SIMD3VySfG_tF(struct swift_interop_passStub_UseSIMD_swift_float4_0_16 a) SWIFT_NOEXCEPT SWIFT_CALL; // swiftThingSIMD(a:)
|
||||
// CHECK: SWIFT_EXTERN void $s7UseSIMD15swiftThingSIMD21ays5SIMD3VySfG_tF(struct swift_interop_passStub_UseSIMD_swift_float4_0_16 a) SWIFT_NOEXCEPT SWIFT_CALL; // swiftThingSIMD2(a:)
|
||||
// CHECK: SWIFT_EXTERN struct swift_interop_returnStub_UseSIMD_swift_float4_0_16 $s7UseSIMD15swiftThingSIMD3s0E0VySfGyF(void) SWIFT_NOEXCEPT SWIFT_CALL; // swiftThingSIMD3()
|
||||
// CHECK: SWIFT_EXTERN struct swift_interop_returnStub_UseSIMD_swift_float4_0_16 $s7UseSIMD15swiftThingSIMD4s5SIMD3VySfGyF(void) SWIFT_NOEXCEPT SWIFT_CALL; // swiftThingSIMD4()
|
||||
// CHECK: SWIFT_EXTERN void $s7UseSIMD16swiftThingScalar1aySf_tF(float a) SWIFT_NOEXCEPT SWIFT_CALL; // swiftThingScalar(a:)
|
||||
|
||||
// CHECK: SWIFT_INLINE_THUNK void swiftThingSIMD(swift_float3 a) noexcept SWIFT_SYMBOL("s:7UseSIMD010swiftThingB01ays5SIMD3VySfG_tF") {
|
||||
// CHECK-NEXT: UseSIMD::_impl::$s7UseSIMD010swiftThingB01ays5SIMD3VySfG_tF(UseSIMD::_impl::swift_interop_passDirect_UseSIMD_swift_float4_0_16(reinterpret_cast<const char *>(swift::_impl::getOpaquePointer(a))));
|
||||
// CHECK-NEXT: }
|
||||
|
||||
// CHECK: WIFT_INLINE_THUNK void swiftThingSIMD2(swift_float3 a) noexcept SWIFT_SYMBOL("s:7UseSIMD15swiftThingSIMD21ays5SIMD3VySfG_tF") {
|
||||
// CHECK-NEXT: UseSIMD::_impl::$s7UseSIMD15swiftThingSIMD21ays5SIMD3VySfG_tF(UseSIMD::_impl::swift_interop_passDirect_UseSIMD_swift_float4_0_16(reinterpret_cast<const char *>(swift::_impl::getOpaquePointer(a))));
|
||||
// CHECK-NEXT: }
|
||||
|
||||
// CHECK: SWIFT_INLINE_THUNK swift_float3 swiftThingSIMD3() noexcept SWIFT_SYMBOL("s:7UseSIMD15swiftThingSIMD3s0E0VySfGyF") SWIFT_WARN_UNUSED_RESULT {
|
||||
// CHECK-NEXT: alignas(alignof(swift_float3)) char storage[sizeof(swift_float3)];
|
||||
// CHECK-NEXT: auto * _Nonnull storageObjectPtr = reinterpret_cast<swift_float3 *>(storage);
|
||||
// CHECK-NEXT: UseSIMD::_impl::swift_interop_returnDirect_UseSIMD_swift_float4_0_16(storage, UseSIMD::_impl::$s7UseSIMD15swiftThingSIMD3s0E0VySfGyF());
|
||||
// CHECK-NEXT: return *storageObjectPtr;
|
||||
// CHECK-NEXT: }
|
||||
|
||||
// CHECK: SWIFT_INLINE_THUNK swift_float3 swiftThingSIMD4() noexcept SWIFT_SYMBOL("s:7UseSIMD15swiftThingSIMD4s5SIMD3VySfGyF") SWIFT_WARN_UNUSED_RESULT {
|
||||
// CHECK-NEXT: alignas(alignof(swift_float3)) char storage[sizeof(swift_float3)];
|
||||
// CHECK-NEXT: auto * _Nonnull storageObjectPtr = reinterpret_cast<swift_float3 *>(storage);
|
||||
// CHECK-NEXT: UseSIMD::_impl::swift_interop_returnDirect_UseSIMD_swift_float4_0_16(storage, UseSIMD::_impl::$s7UseSIMD15swiftThingSIMD4s5SIMD3VySfGyF());
|
||||
// CHECK-NEXT: return *storageObjectPtr;
|
||||
// CHECK-NEXT: }
|
||||
|
||||
// CHECK: SWIFT_INLINE_THUNK void swiftThingScalar(float a) noexcept SWIFT_SYMBOL("s:7UseSIMD16swiftThingScalar1aySf_tF") {
|
||||
// CHECK-NEXT: UseSIMD::_impl::$s7UseSIMD16swiftThingScalar1aySf_tF(a);
|
||||
// CHECK-NEXT: }
|
||||
Reference in New Issue
Block a user