From 6c235b2cceda7a56e4b95a146dd498aa95c90908 Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Mon, 15 Sep 2025 10:51:16 -0700 Subject: [PATCH 01/10] [ObjC][Gen] Emit ObjC strings to respective sections --- lib/IRGen/GenBuiltin.cpp | 5 +-- lib/IRGen/GenClass.cpp | 17 +++++----- lib/IRGen/GenConstant.cpp | 3 +- lib/IRGen/GenDecl.cpp | 31 +++++++++++-------- lib/IRGen/GenKeyPath.cpp | 1 + lib/IRGen/GenMeta.cpp | 14 ++++++--- lib/IRGen/GenObjC.cpp | 20 +++++++++--- lib/IRGen/IRGenModule.h | 1 + .../objc_runtime_visible_conformance.swift | 2 +- 9 files changed, 57 insertions(+), 37 deletions(-) diff --git a/lib/IRGen/GenBuiltin.cpp b/lib/IRGen/GenBuiltin.cpp index 287c07ace9bb1..cd91f6f7b9512 100644 --- a/lib/IRGen/GenBuiltin.cpp +++ b/lib/IRGen/GenBuiltin.cpp @@ -1401,8 +1401,9 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin, auto clangTy = IGF.IGM.getClangType(valueTy->getCanonicalType()); std::string encoding; IGF.IGM.getClangASTContext().getObjCEncodingForType(clangTy, encoding); - - auto globalString = IGF.IGM.getAddrOfGlobalString(encoding); + + auto globalString = IGF.IGM.getAddrOfGlobalString( + encoding, /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); out.add(globalString); return; } diff --git a/lib/IRGen/GenClass.cpp b/lib/IRGen/GenClass.cpp index c3e26fe4009f7..f9e7967758862 100644 --- a/lib/IRGen/GenClass.cpp +++ b/lib/IRGen/GenClass.cpp @@ -1464,7 +1464,7 @@ namespace { // struct category_t { // char const *name; - fields.add(IGM.getAddrOfGlobalString(CategoryName)); + fields.add(IGM.getAddrOfGlobalString(CategoryName, /*sectionName=*/"__TEXT,__objc_classname,cstring_literals"))); // const class_t *theClass; fields.add(getClassMetadataRef()); // const method_list_t *instanceMethods; @@ -1503,7 +1503,7 @@ namespace { // Class super; fields.addNullPointer(IGM.Int8PtrTy); // char const *name; - fields.add(IGM.getAddrOfGlobalString(getEntityName(nameBuffer))); + fields.add(IGM.getAddrOfGlobalString(getEntityName(nameBuffer), /*sectionName=*/"__TEXT,__objc_classname,cstring_literals"))); // const protocol_list_t *baseProtocols; fields.add(buildProtocolList(weakLinkage)); // const method_list_t *requiredInstanceMethods; @@ -1724,7 +1724,7 @@ namespace { } llvm::SmallString<64> buffer; - Name = IGM.getAddrOfGlobalString(getClass()->getObjCRuntimeName(buffer)); + Name = IGM.getAddrOfGlobalString(getClass()->getObjCRuntimeName(buffer), /*sectionName=*/"__TEXT,__objc_classname,cstring_literals")); return Name; } @@ -2088,11 +2088,8 @@ namespace { else fields.addNullPointer(IGM.PtrTy); - // TODO: clang puts this in __TEXT,__objc_methname,cstring_literals - fields.add(IGM.getAddrOfGlobalString(name)); - - // TODO: clang puts this in __TEXT,__objc_methtype,cstring_literals - fields.add(IGM.getAddrOfGlobalString(typeEnc)); + fields.add(IGM.getAddrOfGlobalString(name), /*sectionName=*/"__TEXT,__objc_methname,cstring_literals")); + fields.add(IGM.getAddrOfGlobalString(typeEnc), /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals")); Size size; Alignment alignment; @@ -2228,8 +2225,8 @@ namespace { buildPropertyAttributes(prop, propertyAttributes); auto fields = properties.beginStruct(); - fields.add(IGM.getAddrOfGlobalString(prop->getObjCPropertyName().str())); - fields.add(IGM.getAddrOfGlobalString(propertyAttributes)); + fields.add(IGM.getAddrOfGlobalString(prop->getObjCPropertyName().str(), /*sectionName=*/"__TEXT,__objc_methname,cstring_literals"))); + fields.add(IGM.getAddrOfGlobalString(propertyAttributes, /*sectionName=*/"__TEXT,__objc_methname,cstring_literals"))); fields.finishAndAddTo(properties); } diff --git a/lib/IRGen/GenConstant.cpp b/lib/IRGen/GenConstant.cpp index a8cc25869179e..d21af71d3d5a4 100644 --- a/lib/IRGen/GenConstant.cpp +++ b/lib/IRGen/GenConstant.cpp @@ -79,7 +79,8 @@ llvm::Constant *irgen::emitAddrOfConstantString(IRGenModule &IGM, case StringLiteralInst::Encoding::Bytes: case StringLiteralInst::Encoding::UTF8: case StringLiteralInst::Encoding::UTF8_OSLOG: - return IGM.getAddrOfGlobalString(SLI->getValue(), false, useOSLogEncoding); + return IGM.getAddrOfGlobalString(SLI->getValue(), /*sectionName=*/"", false, + useOSLogEncoding); case StringLiteralInst::Encoding::ObjCSelector: llvm_unreachable("cannot get the address of an Objective-C selector"); diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index 71305d2382ce1..0bf6082e49bbb 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -296,9 +296,10 @@ class ObjCProtocolInitializerVisitor // Check if the ObjC runtime already has a descriptor for this // protocol. If so, use it. SmallString<32> buf; - auto protocolName - = IGM.getAddrOfGlobalString(proto->getObjCRuntimeName(buf)); - + auto protocolName = IGM.getAddrOfGlobalString( + proto->getObjCRuntimeName(buf), + /*sectionName=*/"__TEXT,__objc_classname,cstring_literals"); + auto existing = Builder.CreateCall(objc_getProtocol, protocolName); auto isNull = Builder.CreateICmpEQ(existing, llvm::ConstantPointerNull::get(IGM.ProtocolDescriptorPtrTy)); @@ -4241,8 +4242,9 @@ getObjCClassByNameReference(IRGenModule &IGM, ClassDecl *cls) { auto kind = TypeReferenceKind::DirectObjCClassName; SmallString<64> objcRuntimeNameBuffer; auto ref = IGM.getAddrOfGlobalString( - cls->getObjCRuntimeName(objcRuntimeNameBuffer), - /*willBeRelativelyAddressed=*/true); + cls->getObjCRuntimeName(objcRuntimeNameBuffer), + /*sectionName=*/"__TEXT,__objc_classname,cstring_literals", + /*willBeRelativelyAddressed=*/true); return TypeEntityReference(kind, ref); } @@ -4800,7 +4802,7 @@ void IRGenModule::emitAccessibleFunction(StringRef sectionName, // -- Field: Name (record name) { llvm::Constant *name = - getAddrOfGlobalString(func.getFunctionName(), + getAddrOfGlobalString(func.getFunctionName(), /*sectionName=*/"", /*willBeRelativelyAddressed=*/true); fields.addRelativeAddress(name); } @@ -6023,9 +6025,10 @@ Address IRGenFunction::createAlloca(llvm::Type *type, /// FIXME: willBeRelativelyAddressed is only needed to work around an ld64 bug /// resolving relative references to coalesceable symbols. /// It should be removed when fixed. rdar://problem/22674524 -llvm::Constant *IRGenModule::getAddrOfGlobalString(StringRef data, - bool willBeRelativelyAddressed, - bool useOSLogSection) { +llvm::Constant * +IRGenModule::getAddrOfGlobalString(StringRef data, StringRef sectionName, + bool willBeRelativelyAddressed, + bool useOSLogSection) { useOSLogSection = useOSLogSection && TargetInfo.OutputObjectFormat == llvm::Triple::MachO; @@ -6053,8 +6056,8 @@ llvm::Constant *IRGenModule::getAddrOfGlobalString(StringRef data, (llvm::Twine(".nul") + llvm::Twine(i)).toVector(name); } - auto sectionName = - useOSLogSection ? "__TEXT,__oslogstring,cstring_literals" : ""; + sectionName = + useOSLogSection ? "__TEXT,__oslogstring,cstring_literals" : sectionName; entry = createStringConstant(data, willBeRelativelyAddressed, sectionName, name); @@ -6067,9 +6070,11 @@ IRGenModule::getAddrOfGlobalIdentifierString(StringRef data, if (Lexer::identifierMustAlwaysBeEscaped(data)) { llvm::SmallString<256> name; Mangle::Mangler::appendRawIdentifierForRuntime(data, name); - return getAddrOfGlobalString(name, willBeRelativelyAddressed); + return getAddrOfGlobalString(name, /*sectionName=*/"", + willBeRelativelyAddressed); } - return getAddrOfGlobalString(data, willBeRelativelyAddressed); + return getAddrOfGlobalString(data, /*sectionName=*/"", + willBeRelativelyAddressed); } /// Get or create a global UTF-16 string constant. diff --git a/lib/IRGen/GenKeyPath.cpp b/lib/IRGen/GenKeyPath.cpp index c100fb45613a5..75f015204a89e 100644 --- a/lib/IRGen/GenKeyPath.cpp +++ b/lib/IRGen/GenKeyPath.cpp @@ -1143,6 +1143,7 @@ IRGenModule::getAddrOfKeyPathPattern(KeyPathPattern *pattern, // null otherwise. if (!pattern->getObjCString().empty()) { auto objcString = getAddrOfGlobalString(pattern->getObjCString(), + /*sectionName=*/"", /*relatively addressed*/ true); fields.addRelativeAddress(objcString); } else { diff --git a/lib/IRGen/GenMeta.cpp b/lib/IRGen/GenMeta.cpp index 029edac50bd8a..a52b97ffd076c 100644 --- a/lib/IRGen/GenMeta.cpp +++ b/lib/IRGen/GenMeta.cpp @@ -845,8 +845,9 @@ namespace { IRGenMangler mangler(IGM.Context); auto mangledName = mangler.mangleAnonymousDescriptorName(Name); auto mangledNameConstant = - IGM.getAddrOfGlobalString(mangledName, - /*willBeRelativelyAddressed*/ true); + IGM.getAddrOfGlobalString(mangledName, + /*sectionName=*/"", + /*willBeRelativelyAddressed*/ true); B.addRelativeAddress(mangledNameConstant); } @@ -1277,6 +1278,7 @@ namespace { llvm::Constant *global = nullptr; if (!AssociatedTypeNames.empty()) { global = IGM.getAddrOfGlobalString(AssociatedTypeNames, + /*sectionName=*/"", /*willBeRelativelyAddressed=*/true); } B.addRelativeAddressOrNull(global); @@ -1515,9 +1517,11 @@ namespace { name.pop_back(); assert(name.back() == '\0'); } - - auto nameStr = IGM.getAddrOfGlobalString(name, - /*willBeRelativelyAddressed*/ true); + + auto nameStr = + IGM.getAddrOfGlobalString(name, + /*sectionName=*/"", + /*willBeRelativelyAddressed*/ true); B.addRelativeAddress(nameStr); } diff --git a/lib/IRGen/GenObjC.cpp b/lib/IRGen/GenObjC.cpp index 09eb3a01eba09..d961fd2047c93 100644 --- a/lib/IRGen/GenObjC.cpp +++ b/lib/IRGen/GenObjC.cpp @@ -1318,7 +1318,9 @@ static llvm::Constant *getObjCEncodingForTypes(IRGenModule &IGM, encodingString += llvm::itostr(parmOffset); encodingString += fixedParamsString; encodingString += paramsString; - return IGM.getAddrOfGlobalString(encodingString); + return IGM.getAddrOfGlobalString( + encodingString, + /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); } static llvm::Constant * @@ -1329,16 +1331,19 @@ getObjectEncodingFromClangNode(IRGenModule &IGM, Decl *d, auto clangDecl = d->getClangNode().castAsDecl(); auto &clangASTContext = IGM.getClangASTContext(); std::string typeStr; + std::string sectionName; if (auto objcMethodDecl = dyn_cast(clangDecl)) { typeStr = clangASTContext.getObjCEncodingForMethodDecl( objcMethodDecl, useExtendedEncoding /*extended*/); + sectionName = "__TEXT,__objc_methtype,cstring_literals"; } if (auto objcPropertyDecl = dyn_cast(clangDecl)) { typeStr = clangASTContext.getObjCEncodingForPropertyDecl(objcPropertyDecl, nullptr); + sectionName = "__TEXT,__objc_methname,cstring_literals"; } if (!typeStr.empty()) { - return IGM.getAddrOfGlobalString(typeStr.c_str()); + return IGM.getAddrOfGlobalString(typeStr.c_str(), sectionName); } } return nullptr; @@ -1434,7 +1439,9 @@ irgen::emitObjCGetterDescriptorParts(IRGenModule &IGM, VarDecl *property) { TypeStr += llvm::itostr(ParmOffset); TypeStr += "@0:"; TypeStr += llvm::itostr(PtrSize.getValue()); - descriptor.typeEncoding = IGM.getAddrOfGlobalString(TypeStr.c_str()); + descriptor.typeEncoding = IGM.getAddrOfGlobalString( + TypeStr.c_str(), + /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); descriptor.silFunction = nullptr; descriptor.impl = getObjCGetterPointer(IGM, property, descriptor.silFunction); return descriptor; @@ -1511,7 +1518,9 @@ irgen::emitObjCSetterDescriptorParts(IRGenModule &IGM, ParmOffset = 2 * PtrSize.getValue(); clangASTContext.getObjCEncodingForType(clangType, TypeStr); TypeStr += llvm::itostr(ParmOffset); - descriptor.typeEncoding = IGM.getAddrOfGlobalString(TypeStr.c_str()); + descriptor.typeEncoding = IGM.getAddrOfGlobalString( + TypeStr.c_str(), + /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); descriptor.silFunction = nullptr; descriptor.impl = getObjCSetterPointer(IGM, property, descriptor.silFunction); return descriptor; @@ -1617,7 +1626,8 @@ void irgen::emitObjCIVarInitDestroyDescriptor(IRGenModule &IGM, auto ptrSize = IGM.getPointerSize().getValue(); llvm::SmallString<8> signature; signature = "v" + llvm::itostr(ptrSize * 2) + "@0:" + llvm::itostr(ptrSize); - descriptor.typeEncoding = IGM.getAddrOfGlobalString(signature); + descriptor.typeEncoding = IGM.getAddrOfGlobalString( + signature, /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); /// The third element is the method implementation pointer. descriptor.impl = llvm::ConstantExpr::getBitCast(objcImpl, IGM.Int8PtrTy); diff --git a/lib/IRGen/IRGenModule.h b/lib/IRGen/IRGenModule.h index 627861cb40794..a811a044dd95d 100644 --- a/lib/IRGen/IRGenModule.h +++ b/lib/IRGen/IRGenModule.h @@ -1204,6 +1204,7 @@ class IRGenModule { StringRef Str, bool willBeRelativelyAddressed = false, StringRef sectionName = "", StringRef name = ""); llvm::Constant *getAddrOfGlobalString(StringRef utf8, + StringRef sectionName = "", bool willBeRelativelyAddressed = false, bool useOSLogSection = false); llvm::Constant *getAddrOfGlobalUTF16String(StringRef utf8); diff --git a/test/IRGen/objc_runtime_visible_conformance.swift b/test/IRGen/objc_runtime_visible_conformance.swift index 5d876e687b263..2dde5856888ce 100644 --- a/test/IRGen/objc_runtime_visible_conformance.swift +++ b/test/IRGen/objc_runtime_visible_conformance.swift @@ -15,4 +15,4 @@ extension A : YourProtocol {} // CHECK-SAME: @"$sSo1AC32objc_runtime_visible_conformance10MyProtocolACWP" // DirectObjCClassName // CHECK-SAME: i32 16 -// CHECK: @.str.21.MyRuntimeVisibleClass = private constant [22 x i8] c"MyRuntimeVisibleClass\00" +// CHECK: @.str.21.MyRuntimeVisibleClass = {{.*}} [22 x i8] c"MyRuntimeVisibleClass\00" From f957f45e6b0036102fc5a127d65875c531a7b1ea Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Mon, 15 Sep 2025 13:04:48 -0700 Subject: [PATCH 02/10] factor section names into constants --- lib/IRGen/GenBuiltin.cpp | 2 +- lib/IRGen/GenClass.cpp | 22 +++++++++++++++------- lib/IRGen/GenDecl.cpp | 19 +++++++++---------- lib/IRGen/GenObjC.cpp | 17 +++++++---------- lib/IRGen/IRGenModule.h | 9 +++++++++ 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/lib/IRGen/GenBuiltin.cpp b/lib/IRGen/GenBuiltin.cpp index cd91f6f7b9512..5014f19e1bbe6 100644 --- a/lib/IRGen/GenBuiltin.cpp +++ b/lib/IRGen/GenBuiltin.cpp @@ -1403,7 +1403,7 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin, IGF.IGM.getClangASTContext().getObjCEncodingForType(clangTy, encoding); auto globalString = IGF.IGM.getAddrOfGlobalString( - encoding, /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); + encoding, IRGenModule::ObjCMethodTypeSectionName); out.add(globalString); return; } diff --git a/lib/IRGen/GenClass.cpp b/lib/IRGen/GenClass.cpp index f9e7967758862..9f0eacee0ecc6 100644 --- a/lib/IRGen/GenClass.cpp +++ b/lib/IRGen/GenClass.cpp @@ -1464,7 +1464,8 @@ namespace { // struct category_t { // char const *name; - fields.add(IGM.getAddrOfGlobalString(CategoryName, /*sectionName=*/"__TEXT,__objc_classname,cstring_literals"))); + fields.add(IGM.getAddrOfGlobalString( + CategoryName, IRGenModule::ObjCClassNameSectionName)); // const class_t *theClass; fields.add(getClassMetadataRef()); // const method_list_t *instanceMethods; @@ -1503,7 +1504,8 @@ namespace { // Class super; fields.addNullPointer(IGM.Int8PtrTy); // char const *name; - fields.add(IGM.getAddrOfGlobalString(getEntityName(nameBuffer), /*sectionName=*/"__TEXT,__objc_classname,cstring_literals"))); + fields.add(IGM.getAddrOfGlobalString( + getEntityName(nameBuffer), IRGenModule::ObjCClassNameSectionName)); // const protocol_list_t *baseProtocols; fields.add(buildProtocolList(weakLinkage)); // const method_list_t *requiredInstanceMethods; @@ -1724,7 +1726,8 @@ namespace { } llvm::SmallString<64> buffer; - Name = IGM.getAddrOfGlobalString(getClass()->getObjCRuntimeName(buffer), /*sectionName=*/"__TEXT,__objc_classname,cstring_literals")); + Name = IGM.getAddrOfGlobalString(getClass()->getObjCRuntimeName(buffer), + IRGenModule::ObjCClassNameSectionName); return Name; } @@ -2088,8 +2091,10 @@ namespace { else fields.addNullPointer(IGM.PtrTy); - fields.add(IGM.getAddrOfGlobalString(name), /*sectionName=*/"__TEXT,__objc_methname,cstring_literals")); - fields.add(IGM.getAddrOfGlobalString(typeEnc), /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals")); + fields.add(IGM.getAddrOfGlobalString( + name, IRGenModule::ObjCMethodNameSectionName)); + fields.add(IGM.getAddrOfGlobalString( + typeEnc, IRGenModule::ObjCMethodTypeSectionName)); Size size; Alignment alignment; @@ -2225,8 +2230,11 @@ namespace { buildPropertyAttributes(prop, propertyAttributes); auto fields = properties.beginStruct(); - fields.add(IGM.getAddrOfGlobalString(prop->getObjCPropertyName().str(), /*sectionName=*/"__TEXT,__objc_methname,cstring_literals"))); - fields.add(IGM.getAddrOfGlobalString(propertyAttributes, /*sectionName=*/"__TEXT,__objc_methname,cstring_literals"))); + fields.add( + IGM.getAddrOfGlobalString(prop->getObjCPropertyName().str(), + IRGenModule::ObjCPropertyNameSectionName)); + fields.add(IGM.getAddrOfGlobalString( + propertyAttributes, IRGenModule::ObjCPropertyNameSectionName)); fields.finishAndAddTo(properties); } diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index 0bf6082e49bbb..1b49ba53fb1f7 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -297,8 +297,7 @@ class ObjCProtocolInitializerVisitor // protocol. If so, use it. SmallString<32> buf; auto protocolName = IGM.getAddrOfGlobalString( - proto->getObjCRuntimeName(buf), - /*sectionName=*/"__TEXT,__objc_classname,cstring_literals"); + proto->getObjCRuntimeName(buf), IRGenModule::ObjCClassNameSectionName); auto existing = Builder.CreateCall(objc_getProtocol, protocolName); auto isNull = Builder.CreateICmpEQ(existing, @@ -1057,16 +1056,16 @@ void IRGenModule::SetCStringLiteralSection(llvm::GlobalVariable *GV, case llvm::Triple::MachO: switch (Type) { case ObjCLabelType::ClassName: - GV->setSection("__TEXT,__objc_classname,cstring_literals"); + GV->setSection(IRGenModule::ObjCClassNameSectionName); return; case ObjCLabelType::MethodVarName: - GV->setSection("__TEXT,__objc_methname,cstring_literals"); + GV->setSection(IRGenModule::ObjCMethodNameSectionName); return; case ObjCLabelType::MethodVarType: - GV->setSection("__TEXT,__objc_methtype,cstring_literals"); + GV->setSection(IRGenModule::ObjCMethodTypeSectionName); return; case ObjCLabelType::PropertyName: - GV->setSection("__TEXT,__objc_methname,cstring_literals"); + GV->setSection(IRGenModule::ObjCPropertyNameSectionName); return; } case llvm::Triple::ELF: @@ -4241,10 +4240,10 @@ static TypeEntityReference getObjCClassByNameReference(IRGenModule &IGM, ClassDecl *cls) { auto kind = TypeReferenceKind::DirectObjCClassName; SmallString<64> objcRuntimeNameBuffer; - auto ref = IGM.getAddrOfGlobalString( - cls->getObjCRuntimeName(objcRuntimeNameBuffer), - /*sectionName=*/"__TEXT,__objc_classname,cstring_literals", - /*willBeRelativelyAddressed=*/true); + auto ref = + IGM.getAddrOfGlobalString(cls->getObjCRuntimeName(objcRuntimeNameBuffer), + IRGenModule::ObjCClassNameSectionName, + /*willBeRelativelyAddressed=*/true); return TypeEntityReference(kind, ref); } diff --git a/lib/IRGen/GenObjC.cpp b/lib/IRGen/GenObjC.cpp index d961fd2047c93..40df153e3fc96 100644 --- a/lib/IRGen/GenObjC.cpp +++ b/lib/IRGen/GenObjC.cpp @@ -1318,9 +1318,8 @@ static llvm::Constant *getObjCEncodingForTypes(IRGenModule &IGM, encodingString += llvm::itostr(parmOffset); encodingString += fixedParamsString; encodingString += paramsString; - return IGM.getAddrOfGlobalString( - encodingString, - /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); + return IGM.getAddrOfGlobalString(encodingString, + IRGenModule::ObjCMethodTypeSectionName); } static llvm::Constant * @@ -1335,12 +1334,12 @@ getObjectEncodingFromClangNode(IRGenModule &IGM, Decl *d, if (auto objcMethodDecl = dyn_cast(clangDecl)) { typeStr = clangASTContext.getObjCEncodingForMethodDecl( objcMethodDecl, useExtendedEncoding /*extended*/); - sectionName = "__TEXT,__objc_methtype,cstring_literals"; + sectionName = IRGenModule::ObjCMethodTypeSectionName; } if (auto objcPropertyDecl = dyn_cast(clangDecl)) { typeStr = clangASTContext.getObjCEncodingForPropertyDecl(objcPropertyDecl, nullptr); - sectionName = "__TEXT,__objc_methname,cstring_literals"; + sectionName = IRGenModule::ObjCPropertyNameSectionName; } if (!typeStr.empty()) { return IGM.getAddrOfGlobalString(typeStr.c_str(), sectionName); @@ -1440,8 +1439,7 @@ irgen::emitObjCGetterDescriptorParts(IRGenModule &IGM, VarDecl *property) { TypeStr += "@0:"; TypeStr += llvm::itostr(PtrSize.getValue()); descriptor.typeEncoding = IGM.getAddrOfGlobalString( - TypeStr.c_str(), - /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); + TypeStr.c_str(), IRGenModule::ObjCMethodTypeSectionName); descriptor.silFunction = nullptr; descriptor.impl = getObjCGetterPointer(IGM, property, descriptor.silFunction); return descriptor; @@ -1519,8 +1517,7 @@ irgen::emitObjCSetterDescriptorParts(IRGenModule &IGM, clangASTContext.getObjCEncodingForType(clangType, TypeStr); TypeStr += llvm::itostr(ParmOffset); descriptor.typeEncoding = IGM.getAddrOfGlobalString( - TypeStr.c_str(), - /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); + TypeStr.c_str(), IRGenModule::ObjCMethodTypeSectionName); descriptor.silFunction = nullptr; descriptor.impl = getObjCSetterPointer(IGM, property, descriptor.silFunction); return descriptor; @@ -1627,7 +1624,7 @@ void irgen::emitObjCIVarInitDestroyDescriptor(IRGenModule &IGM, llvm::SmallString<8> signature; signature = "v" + llvm::itostr(ptrSize * 2) + "@0:" + llvm::itostr(ptrSize); descriptor.typeEncoding = IGM.getAddrOfGlobalString( - signature, /*sectionName=*/"__TEXT,__objc_methtype,cstring_literals"); + signature, IRGenModule::ObjCMethodTypeSectionName); /// The third element is the method implementation pointer. descriptor.impl = llvm::ConstantExpr::getBitCast(objcImpl, IGM.Int8PtrTy); diff --git a/lib/IRGen/IRGenModule.h b/lib/IRGen/IRGenModule.h index a811a044dd95d..281bf40bf56ca 100644 --- a/lib/IRGen/IRGenModule.h +++ b/lib/IRGen/IRGenModule.h @@ -1544,6 +1544,15 @@ class IRGenModule { const char *getReflectionTypeRefSectionName(); const char *getMultiPayloadEnumDescriptorSectionName(); + static constexpr const char ObjCClassNameSectionName[] = + "__TEXT,__objc_classname,cstring_literals"; + static constexpr const char ObjCMethodNameSectionName[] = + "__TEXT,__objc_methname,cstring_literals"; + static constexpr const char ObjCMethodTypeSectionName[] = + "__TEXT,__objc_methtype,cstring_literals"; + static constexpr const char ObjCPropertyNameSectionName[] = + "__TEXT,__objc_methname,cstring_literals"; + /// Returns the special builtin types that should be emitted in the stdlib /// module. llvm::ArrayRef getOrCreateSpecialStlibBuiltinTypes(); From 6ce0f8e43edd57f7e74c10e2125eb3c1ff132a13 Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Mon, 15 Sep 2025 15:47:45 -0700 Subject: [PATCH 03/10] use helper function --- lib/IRGen/GenConstant.cpp | 3 +-- lib/IRGen/GenDecl.cpp | 29 ++++++++++++++++------------- lib/IRGen/GenKeyPath.cpp | 1 - lib/IRGen/GenMeta.cpp | 14 +++++--------- lib/IRGen/GenObjC.cpp | 2 +- lib/IRGen/IRGenModule.h | 6 ++++-- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/lib/IRGen/GenConstant.cpp b/lib/IRGen/GenConstant.cpp index d21af71d3d5a4..a8cc25869179e 100644 --- a/lib/IRGen/GenConstant.cpp +++ b/lib/IRGen/GenConstant.cpp @@ -79,8 +79,7 @@ llvm::Constant *irgen::emitAddrOfConstantString(IRGenModule &IGM, case StringLiteralInst::Encoding::Bytes: case StringLiteralInst::Encoding::UTF8: case StringLiteralInst::Encoding::UTF8_OSLOG: - return IGM.getAddrOfGlobalString(SLI->getValue(), /*sectionName=*/"", false, - useOSLogEncoding); + return IGM.getAddrOfGlobalString(SLI->getValue(), false, useOSLogEncoding); case StringLiteralInst::Encoding::ObjCSelector: llvm_unreachable("cannot get the address of an Objective-C selector"); diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index 1b49ba53fb1f7..50ba42d9fe849 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -4240,10 +4240,10 @@ static TypeEntityReference getObjCClassByNameReference(IRGenModule &IGM, ClassDecl *cls) { auto kind = TypeReferenceKind::DirectObjCClassName; SmallString<64> objcRuntimeNameBuffer; - auto ref = - IGM.getAddrOfGlobalString(cls->getObjCRuntimeName(objcRuntimeNameBuffer), - IRGenModule::ObjCClassNameSectionName, - /*willBeRelativelyAddressed=*/true); + auto ref = IGM.getAddrOfGlobalString( + cls->getObjCRuntimeName(objcRuntimeNameBuffer), + /*willBeRelativelyAddressed=*/true, + /*useOSLogSection=*/false, IRGenModule::ObjCClassNameSectionName); return TypeEntityReference(kind, ref); } @@ -4801,7 +4801,7 @@ void IRGenModule::emitAccessibleFunction(StringRef sectionName, // -- Field: Name (record name) { llvm::Constant *name = - getAddrOfGlobalString(func.getFunctionName(), /*sectionName=*/"", + getAddrOfGlobalString(func.getFunctionName(), /*willBeRelativelyAddressed=*/true); fields.addRelativeAddress(name); } @@ -6016,6 +6016,12 @@ Address IRGenFunction::createAlloca(llvm::Type *type, return Address(alloca, type, alignment); } +llvm::Constant *IRGenModule::getAddrOfGlobalString(StringRef data, + const char *sectionName) { + return getAddrOfGlobalString(data, /*willBeRelativelyAddressed=*/false, + /*useOSLogSection=*/false, sectionName); +} + /// Get or create a global string constant. /// /// \returns an i8* with a null terminator; note that embedded nulls @@ -6024,10 +6030,9 @@ Address IRGenFunction::createAlloca(llvm::Type *type, /// FIXME: willBeRelativelyAddressed is only needed to work around an ld64 bug /// resolving relative references to coalesceable symbols. /// It should be removed when fixed. rdar://problem/22674524 -llvm::Constant * -IRGenModule::getAddrOfGlobalString(StringRef data, StringRef sectionName, - bool willBeRelativelyAddressed, - bool useOSLogSection) { +llvm::Constant *IRGenModule::getAddrOfGlobalString( + StringRef data, bool willBeRelativelyAddressed, bool useOSLogSection, + StringRef sectionName) { useOSLogSection = useOSLogSection && TargetInfo.OutputObjectFormat == llvm::Triple::MachO; @@ -6069,11 +6074,9 @@ IRGenModule::getAddrOfGlobalIdentifierString(StringRef data, if (Lexer::identifierMustAlwaysBeEscaped(data)) { llvm::SmallString<256> name; Mangle::Mangler::appendRawIdentifierForRuntime(data, name); - return getAddrOfGlobalString(name, /*sectionName=*/"", - willBeRelativelyAddressed); + return getAddrOfGlobalString(name, willBeRelativelyAddressed); } - return getAddrOfGlobalString(data, /*sectionName=*/"", - willBeRelativelyAddressed); + return getAddrOfGlobalString(data, willBeRelativelyAddressed); } /// Get or create a global UTF-16 string constant. diff --git a/lib/IRGen/GenKeyPath.cpp b/lib/IRGen/GenKeyPath.cpp index 75f015204a89e..c100fb45613a5 100644 --- a/lib/IRGen/GenKeyPath.cpp +++ b/lib/IRGen/GenKeyPath.cpp @@ -1143,7 +1143,6 @@ IRGenModule::getAddrOfKeyPathPattern(KeyPathPattern *pattern, // null otherwise. if (!pattern->getObjCString().empty()) { auto objcString = getAddrOfGlobalString(pattern->getObjCString(), - /*sectionName=*/"", /*relatively addressed*/ true); fields.addRelativeAddress(objcString); } else { diff --git a/lib/IRGen/GenMeta.cpp b/lib/IRGen/GenMeta.cpp index a52b97ffd076c..029edac50bd8a 100644 --- a/lib/IRGen/GenMeta.cpp +++ b/lib/IRGen/GenMeta.cpp @@ -845,9 +845,8 @@ namespace { IRGenMangler mangler(IGM.Context); auto mangledName = mangler.mangleAnonymousDescriptorName(Name); auto mangledNameConstant = - IGM.getAddrOfGlobalString(mangledName, - /*sectionName=*/"", - /*willBeRelativelyAddressed*/ true); + IGM.getAddrOfGlobalString(mangledName, + /*willBeRelativelyAddressed*/ true); B.addRelativeAddress(mangledNameConstant); } @@ -1278,7 +1277,6 @@ namespace { llvm::Constant *global = nullptr; if (!AssociatedTypeNames.empty()) { global = IGM.getAddrOfGlobalString(AssociatedTypeNames, - /*sectionName=*/"", /*willBeRelativelyAddressed=*/true); } B.addRelativeAddressOrNull(global); @@ -1517,11 +1515,9 @@ namespace { name.pop_back(); assert(name.back() == '\0'); } - - auto nameStr = - IGM.getAddrOfGlobalString(name, - /*sectionName=*/"", - /*willBeRelativelyAddressed*/ true); + + auto nameStr = IGM.getAddrOfGlobalString(name, + /*willBeRelativelyAddressed*/ true); B.addRelativeAddress(nameStr); } diff --git a/lib/IRGen/GenObjC.cpp b/lib/IRGen/GenObjC.cpp index 40df153e3fc96..f561784211af1 100644 --- a/lib/IRGen/GenObjC.cpp +++ b/lib/IRGen/GenObjC.cpp @@ -1330,7 +1330,7 @@ getObjectEncodingFromClangNode(IRGenModule &IGM, Decl *d, auto clangDecl = d->getClangNode().castAsDecl(); auto &clangASTContext = IGM.getClangASTContext(); std::string typeStr; - std::string sectionName; + const char *sectionName; if (auto objcMethodDecl = dyn_cast(clangDecl)) { typeStr = clangASTContext.getObjCEncodingForMethodDecl( objcMethodDecl, useExtendedEncoding /*extended*/); diff --git a/lib/IRGen/IRGenModule.h b/lib/IRGen/IRGenModule.h index 281bf40bf56ca..450eb16780d04 100644 --- a/lib/IRGen/IRGenModule.h +++ b/lib/IRGen/IRGenModule.h @@ -1204,9 +1204,11 @@ class IRGenModule { StringRef Str, bool willBeRelativelyAddressed = false, StringRef sectionName = "", StringRef name = ""); llvm::Constant *getAddrOfGlobalString(StringRef utf8, - StringRef sectionName = "", bool willBeRelativelyAddressed = false, - bool useOSLogSection = false); + bool useOSLogSection = false, + StringRef sectionName = ""); + llvm::Constant *getAddrOfGlobalString(StringRef utf8, + const char *sectionName); llvm::Constant *getAddrOfGlobalUTF16String(StringRef utf8); llvm::Constant * getAddrOfGlobalIdentifierString(StringRef utf8, From ffee34485920cd82df1711a1dad0eaadfb5472e5 Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Tue, 23 Sep 2025 15:26:55 -0700 Subject: [PATCH 04/10] add enum --- lib/IRGen/GenBuiltin.cpp | 2 +- lib/IRGen/GenClass.cpp | 22 ++++++------- lib/IRGen/GenConstant.cpp | 4 ++- lib/IRGen/GenDecl.cpp | 65 +++++++++++++++++++++++---------------- lib/IRGen/GenKeyPath.cpp | 1 + lib/IRGen/GenMeta.cpp | 12 +++++--- lib/IRGen/GenObjC.cpp | 18 +++++------ lib/IRGen/IRGenModule.h | 29 +++++++++++------ 8 files changed, 90 insertions(+), 63 deletions(-) diff --git a/lib/IRGen/GenBuiltin.cpp b/lib/IRGen/GenBuiltin.cpp index 5014f19e1bbe6..21bba9a29f133 100644 --- a/lib/IRGen/GenBuiltin.cpp +++ b/lib/IRGen/GenBuiltin.cpp @@ -1403,7 +1403,7 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin, IGF.IGM.getClangASTContext().getObjCEncodingForType(clangTy, encoding); auto globalString = IGF.IGM.getAddrOfGlobalString( - encoding, IRGenModule::ObjCMethodTypeSectionName); + encoding, CStringSectionType::ObjCMethodType); out.add(globalString); return; } diff --git a/lib/IRGen/GenClass.cpp b/lib/IRGen/GenClass.cpp index 9f0eacee0ecc6..b7f59c9f110e9 100644 --- a/lib/IRGen/GenClass.cpp +++ b/lib/IRGen/GenClass.cpp @@ -1464,8 +1464,8 @@ namespace { // struct category_t { // char const *name; - fields.add(IGM.getAddrOfGlobalString( - CategoryName, IRGenModule::ObjCClassNameSectionName)); + fields.add(IGM.getAddrOfGlobalString(CategoryName, + CStringSectionType::ObjCClassName)); // const class_t *theClass; fields.add(getClassMetadataRef()); // const method_list_t *instanceMethods; @@ -1504,8 +1504,8 @@ namespace { // Class super; fields.addNullPointer(IGM.Int8PtrTy); // char const *name; - fields.add(IGM.getAddrOfGlobalString( - getEntityName(nameBuffer), IRGenModule::ObjCClassNameSectionName)); + fields.add(IGM.getAddrOfGlobalString(getEntityName(nameBuffer), + CStringSectionType::ObjCClassName)); // const protocol_list_t *baseProtocols; fields.add(buildProtocolList(weakLinkage)); // const method_list_t *requiredInstanceMethods; @@ -1727,7 +1727,7 @@ namespace { llvm::SmallString<64> buffer; Name = IGM.getAddrOfGlobalString(getClass()->getObjCRuntimeName(buffer), - IRGenModule::ObjCClassNameSectionName); + CStringSectionType::ObjCClassName); return Name; } @@ -2091,10 +2091,10 @@ namespace { else fields.addNullPointer(IGM.PtrTy); - fields.add(IGM.getAddrOfGlobalString( - name, IRGenModule::ObjCMethodNameSectionName)); - fields.add(IGM.getAddrOfGlobalString( - typeEnc, IRGenModule::ObjCMethodTypeSectionName)); + fields.add( + IGM.getAddrOfGlobalString(name, CStringSectionType::ObjCMethodName)); + fields.add(IGM.getAddrOfGlobalString(typeEnc, + CStringSectionType::ObjCMethodType)); Size size; Alignment alignment; @@ -2232,9 +2232,9 @@ namespace { auto fields = properties.beginStruct(); fields.add( IGM.getAddrOfGlobalString(prop->getObjCPropertyName().str(), - IRGenModule::ObjCPropertyNameSectionName)); + CStringSectionType::ObjCPropertyName)); fields.add(IGM.getAddrOfGlobalString( - propertyAttributes, IRGenModule::ObjCPropertyNameSectionName)); + propertyAttributes, CStringSectionType::ObjCPropertyName)); fields.finishAndAddTo(properties); } diff --git a/lib/IRGen/GenConstant.cpp b/lib/IRGen/GenConstant.cpp index a8cc25869179e..4dc9c9190af56 100644 --- a/lib/IRGen/GenConstant.cpp +++ b/lib/IRGen/GenConstant.cpp @@ -79,7 +79,9 @@ llvm::Constant *irgen::emitAddrOfConstantString(IRGenModule &IGM, case StringLiteralInst::Encoding::Bytes: case StringLiteralInst::Encoding::UTF8: case StringLiteralInst::Encoding::UTF8_OSLOG: - return IGM.getAddrOfGlobalString(SLI->getValue(), false, useOSLogEncoding); + return IGM.getAddrOfGlobalString( + SLI->getValue(), useOSLogEncoding ? CStringSectionType::OSLogString + : CStringSectionType::Default); case StringLiteralInst::Encoding::ObjCSelector: llvm_unreachable("cannot get the address of an Objective-C selector"); diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index 50ba42d9fe849..ea27f48d983b3 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -297,7 +297,7 @@ class ObjCProtocolInitializerVisitor // protocol. If so, use it. SmallString<32> buf; auto protocolName = IGM.getAddrOfGlobalString( - proto->getObjCRuntimeName(buf), IRGenModule::ObjCClassNameSectionName); + proto->getObjCRuntimeName(buf), CStringSectionType::ObjCClassName); auto existing = Builder.CreateCall(objc_getProtocol, protocolName); auto isNull = Builder.CreateICmpEQ(existing, @@ -4240,10 +4240,10 @@ static TypeEntityReference getObjCClassByNameReference(IRGenModule &IGM, ClassDecl *cls) { auto kind = TypeReferenceKind::DirectObjCClassName; SmallString<64> objcRuntimeNameBuffer; - auto ref = IGM.getAddrOfGlobalString( - cls->getObjCRuntimeName(objcRuntimeNameBuffer), - /*willBeRelativelyAddressed=*/true, - /*useOSLogSection=*/false, IRGenModule::ObjCClassNameSectionName); + auto ref = + IGM.getAddrOfGlobalString(cls->getObjCRuntimeName(objcRuntimeNameBuffer), + CStringSectionType::ObjCClassName, + /*willBeRelativelyAddressed=*/true); return TypeEntityReference(kind, ref); } @@ -4800,9 +4800,9 @@ void IRGenModule::emitAccessibleFunction(StringRef sectionName, // -- Field: Name (record name) { - llvm::Constant *name = - getAddrOfGlobalString(func.getFunctionName(), - /*willBeRelativelyAddressed=*/true); + llvm::Constant *name = getAddrOfGlobalString( + func.getFunctionName(), CStringSectionType::Default, + /*willBeRelativelyAddressed=*/true); fields.addRelativeAddress(name); } @@ -6016,12 +6016,6 @@ Address IRGenFunction::createAlloca(llvm::Type *type, return Address(alloca, type, alignment); } -llvm::Constant *IRGenModule::getAddrOfGlobalString(StringRef data, - const char *sectionName) { - return getAddrOfGlobalString(data, /*willBeRelativelyAddressed=*/false, - /*useOSLogSection=*/false, sectionName); -} - /// Get or create a global string constant. /// /// \returns an i8* with a null terminator; note that embedded nulls @@ -6030,15 +6024,35 @@ llvm::Constant *IRGenModule::getAddrOfGlobalString(StringRef data, /// FIXME: willBeRelativelyAddressed is only needed to work around an ld64 bug /// resolving relative references to coalesceable symbols. /// It should be removed when fixed. rdar://problem/22674524 -llvm::Constant *IRGenModule::getAddrOfGlobalString( - StringRef data, bool willBeRelativelyAddressed, bool useOSLogSection, - StringRef sectionName) { - useOSLogSection = useOSLogSection && - TargetInfo.OutputObjectFormat == llvm::Triple::MachO; +llvm::Constant * +IRGenModule::getAddrOfGlobalString(StringRef data, CStringSectionType type, + bool willBeRelativelyAddressed) { + if (TargetInfo.OutputObjectFormat != llvm::Triple::MachO) + type = CStringSectionType::Default; + StringRef sectionName; + switch (type) { + case CStringSectionType::Default: + sectionName = ""; + break; + case CStringSectionType::ObjCClassName: + sectionName = ObjCClassNameSectionName; + break; + case CStringSectionType::ObjCMethodName: + sectionName = ObjCMethodNameSectionName; + break; + case CStringSectionType::ObjCMethodType: + sectionName = ObjCMethodTypeSectionName; + break; + case CStringSectionType::ObjCPropertyName: + sectionName = ObjCPropertyNameSectionName; + break; + case CStringSectionType::OSLogString: + sectionName = OSLogStringSectionName; + break; + } // Check whether this string already exists. - auto &entry = useOSLogSection ? GlobalOSLogStrings[data] : - GlobalStrings[data]; + auto &entry = GlobalStrings[type][data]; if (entry.second) { // FIXME: Clear unnamed_addr if the global will be relative referenced @@ -6060,9 +6074,6 @@ llvm::Constant *IRGenModule::getAddrOfGlobalString( (llvm::Twine(".nul") + llvm::Twine(i)).toVector(name); } - sectionName = - useOSLogSection ? "__TEXT,__oslogstring,cstring_literals" : sectionName; - entry = createStringConstant(data, willBeRelativelyAddressed, sectionName, name); return entry.second; @@ -6074,9 +6085,11 @@ IRGenModule::getAddrOfGlobalIdentifierString(StringRef data, if (Lexer::identifierMustAlwaysBeEscaped(data)) { llvm::SmallString<256> name; Mangle::Mangler::appendRawIdentifierForRuntime(data, name); - return getAddrOfGlobalString(name, willBeRelativelyAddressed); + return getAddrOfGlobalString(name, CStringSectionType::Default, + willBeRelativelyAddressed); } - return getAddrOfGlobalString(data, willBeRelativelyAddressed); + return getAddrOfGlobalString(data, CStringSectionType::Default, + willBeRelativelyAddressed); } /// Get or create a global UTF-16 string constant. diff --git a/lib/IRGen/GenKeyPath.cpp b/lib/IRGen/GenKeyPath.cpp index c100fb45613a5..314c59149fd6b 100644 --- a/lib/IRGen/GenKeyPath.cpp +++ b/lib/IRGen/GenKeyPath.cpp @@ -1143,6 +1143,7 @@ IRGenModule::getAddrOfKeyPathPattern(KeyPathPattern *pattern, // null otherwise. if (!pattern->getObjCString().empty()) { auto objcString = getAddrOfGlobalString(pattern->getObjCString(), + CStringSectionType::Default, /*relatively addressed*/ true); fields.addRelativeAddress(objcString); } else { diff --git a/lib/IRGen/GenMeta.cpp b/lib/IRGen/GenMeta.cpp index 029edac50bd8a..47247e1d5bcca 100644 --- a/lib/IRGen/GenMeta.cpp +++ b/lib/IRGen/GenMeta.cpp @@ -845,8 +845,8 @@ namespace { IRGenMangler mangler(IGM.Context); auto mangledName = mangler.mangleAnonymousDescriptorName(Name); auto mangledNameConstant = - IGM.getAddrOfGlobalString(mangledName, - /*willBeRelativelyAddressed*/ true); + IGM.getAddrOfGlobalString(mangledName, CStringSectionType::Default, + /*willBeRelativelyAddressed*/ true); B.addRelativeAddress(mangledNameConstant); } @@ -1277,6 +1277,7 @@ namespace { llvm::Constant *global = nullptr; if (!AssociatedTypeNames.empty()) { global = IGM.getAddrOfGlobalString(AssociatedTypeNames, + CStringSectionType::Default, /*willBeRelativelyAddressed=*/true); } B.addRelativeAddressOrNull(global); @@ -1515,9 +1516,10 @@ namespace { name.pop_back(); assert(name.back() == '\0'); } - - auto nameStr = IGM.getAddrOfGlobalString(name, - /*willBeRelativelyAddressed*/ true); + + auto nameStr = + IGM.getAddrOfGlobalString(name, CStringSectionType::Default, + /*willBeRelativelyAddressed*/ true); B.addRelativeAddress(nameStr); } diff --git a/lib/IRGen/GenObjC.cpp b/lib/IRGen/GenObjC.cpp index f561784211af1..5693f54f9d98d 100644 --- a/lib/IRGen/GenObjC.cpp +++ b/lib/IRGen/GenObjC.cpp @@ -1319,7 +1319,7 @@ static llvm::Constant *getObjCEncodingForTypes(IRGenModule &IGM, encodingString += fixedParamsString; encodingString += paramsString; return IGM.getAddrOfGlobalString(encodingString, - IRGenModule::ObjCMethodTypeSectionName); + CStringSectionType::ObjCMethodType); } static llvm::Constant * @@ -1330,19 +1330,19 @@ getObjectEncodingFromClangNode(IRGenModule &IGM, Decl *d, auto clangDecl = d->getClangNode().castAsDecl(); auto &clangASTContext = IGM.getClangASTContext(); std::string typeStr; - const char *sectionName; + CStringSectionType sectionType; if (auto objcMethodDecl = dyn_cast(clangDecl)) { typeStr = clangASTContext.getObjCEncodingForMethodDecl( objcMethodDecl, useExtendedEncoding /*extended*/); - sectionName = IRGenModule::ObjCMethodTypeSectionName; + sectionType = CStringSectionType::ObjCMethodType; } if (auto objcPropertyDecl = dyn_cast(clangDecl)) { typeStr = clangASTContext.getObjCEncodingForPropertyDecl(objcPropertyDecl, nullptr); - sectionName = IRGenModule::ObjCPropertyNameSectionName; + sectionType = CStringSectionType::ObjCPropertyName; } if (!typeStr.empty()) { - return IGM.getAddrOfGlobalString(typeStr.c_str(), sectionName); + return IGM.getAddrOfGlobalString(typeStr.c_str(), sectionType); } } return nullptr; @@ -1439,7 +1439,7 @@ irgen::emitObjCGetterDescriptorParts(IRGenModule &IGM, VarDecl *property) { TypeStr += "@0:"; TypeStr += llvm::itostr(PtrSize.getValue()); descriptor.typeEncoding = IGM.getAddrOfGlobalString( - TypeStr.c_str(), IRGenModule::ObjCMethodTypeSectionName); + TypeStr.c_str(), CStringSectionType::ObjCMethodType); descriptor.silFunction = nullptr; descriptor.impl = getObjCGetterPointer(IGM, property, descriptor.silFunction); return descriptor; @@ -1517,7 +1517,7 @@ irgen::emitObjCSetterDescriptorParts(IRGenModule &IGM, clangASTContext.getObjCEncodingForType(clangType, TypeStr); TypeStr += llvm::itostr(ParmOffset); descriptor.typeEncoding = IGM.getAddrOfGlobalString( - TypeStr.c_str(), IRGenModule::ObjCMethodTypeSectionName); + TypeStr.c_str(), CStringSectionType::ObjCMethodType); descriptor.silFunction = nullptr; descriptor.impl = getObjCSetterPointer(IGM, property, descriptor.silFunction); return descriptor; @@ -1623,8 +1623,8 @@ void irgen::emitObjCIVarInitDestroyDescriptor(IRGenModule &IGM, auto ptrSize = IGM.getPointerSize().getValue(); llvm::SmallString<8> signature; signature = "v" + llvm::itostr(ptrSize * 2) + "@0:" + llvm::itostr(ptrSize); - descriptor.typeEncoding = IGM.getAddrOfGlobalString( - signature, IRGenModule::ObjCMethodTypeSectionName); + descriptor.typeEncoding = + IGM.getAddrOfGlobalString(signature, CStringSectionType::ObjCMethodType); /// The third element is the method implementation pointer. descriptor.impl = llvm::ConstantExpr::getBitCast(objcImpl, IGM.Int8PtrTy); diff --git a/lib/IRGen/IRGenModule.h b/lib/IRGen/IRGenModule.h index 450eb16780d04..5522e6b7b1550 100644 --- a/lib/IRGen/IRGenModule.h +++ b/lib/IRGen/IRGenModule.h @@ -669,6 +669,15 @@ struct AccessibleFunction { llvm::Constant *address); }; +enum class CStringSectionType { + Default, + ObjCClassName, + ObjCMethodName, + ObjCMethodType, + ObjCPropertyName, + OSLogString, +}; + /// IRGenModule - Primary class for emitting IR for global declarations. /// class IRGenModule { @@ -1203,12 +1212,10 @@ class IRGenModule { std::pair createStringConstant( StringRef Str, bool willBeRelativelyAddressed = false, StringRef sectionName = "", StringRef name = ""); - llvm::Constant *getAddrOfGlobalString(StringRef utf8, - bool willBeRelativelyAddressed = false, - bool useOSLogSection = false, - StringRef sectionName = ""); - llvm::Constant *getAddrOfGlobalString(StringRef utf8, - const char *sectionName); + llvm::Constant *getAddrOfGlobalString( + StringRef utf8, + CStringSectionType sectionType = CStringSectionType::Default, + bool willBeRelativelyAddressed = false); llvm::Constant *getAddrOfGlobalUTF16String(StringRef utf8); llvm::Constant * getAddrOfGlobalIdentifierString(StringRef utf8, @@ -1332,10 +1339,10 @@ class IRGenModule { llvm::DenseMap GlobalGOTEquivalents; llvm::DenseMap GlobalFuncs; llvm::DenseSet GlobalClangDecls; - llvm::StringMap> - GlobalStrings; - llvm::StringMap> - GlobalOSLogStrings; + llvm::DenseMap< + CStringSectionType, + llvm::StringMap>> + GlobalStrings; llvm::StringMap GlobalUTF16Strings; llvm::StringMap> StringsForTypeRef; @@ -1554,6 +1561,8 @@ class IRGenModule { "__TEXT,__objc_methtype,cstring_literals"; static constexpr const char ObjCPropertyNameSectionName[] = "__TEXT,__objc_methname,cstring_literals"; + static constexpr const char OSLogStringSectionName[] = + "__TEXT,__oslogstring,cstring_literals"; /// Returns the special builtin types that should be emitted in the stdlib /// module. From 9e30ce35c213f91b420bc889df59017702314efc Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Tue, 23 Sep 2025 18:45:24 -0700 Subject: [PATCH 05/10] fix test --- .../IRGen/noncopyable_field_descriptors.swift | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/test/IRGen/noncopyable_field_descriptors.swift b/test/IRGen/noncopyable_field_descriptors.swift index 72678dac4b596..bdfc8b36f5c09 100644 --- a/test/IRGen/noncopyable_field_descriptors.swift +++ b/test/IRGen/noncopyable_field_descriptors.swift @@ -39,16 +39,16 @@ public struct NonCopyable: ~Copyable { } // 'MF' constant as a separator that precedes each field descriptor. // NEW: @"$s4test8CC_TestsCMF" = -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.3" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.{{[0-9]+}}" // NEW-SAME: @"symbolic _____yq_G 4test21ConditionallyCopyableOAARi_zrlE" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.4" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.{{[0-9]+}}" // NEW-SAME: @"symbolic _____ySSG 4test21ConditionallyCopyableOAARi_zrlE" // OLD: @"$s4test8CC_TestsCMF" = -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.3" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyq_G.4" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.5" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOySSG.6" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyq_G.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOySSG.{{[0-9]+}}" public class CC_Tests { var ccNCG: ConditionallyCopyable = .none var ccT: ConditionallyCopyable = .none @@ -62,16 +62,16 @@ public class CC_Tests { /// fields until a future runtime says they're safe to reflect. // NEW: @"$s4test8NC_TestsCMF" = -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.5" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.6" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.7" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.8" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.{{[0-9]+}}" // OLD: @"$s4test8NC_TestsCMF" = -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.7" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.8" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.9" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.10" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.{{[0-9]+}}" public class NC_Tests { var ncNCG: NeverCopyable = .none var ncT: NeverCopyable = .none @@ -83,7 +83,7 @@ public class NC_Tests { // NEW: @"$s4test17StdlibTypes_TestsCMF" = // NEW-SAME: @"symbolic xSg" // NEW-SAME: @"symbolic q_Sg" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.9" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.{{[0-9]+}}" // NEW-SAME: @"symbolic SSSg" // NEW-SAME: @"symbolic SPyxG" // NEW-SAME: @"symbolic SPyq_G" @@ -93,7 +93,7 @@ public class NC_Tests { // OLD: @"$s4test17StdlibTypes_TestsCMF" = // OLD-SAME: @"symbolic xSg" // OLD-SAME: @"symbolic q_Sg" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.11" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.{{[0-9]+}}" // OLD-SAME: @"symbolic SSSg" // OLD-SAME: @"symbolic SPyxG" // OLD-SAME: @"symbolic SPyq_G" @@ -115,13 +115,13 @@ public class StdlibTypes_Tests { // NEW: @"$s4test19PlainlyStored_TestsCMF" = // NEW-SAME: @"symbolic x" // NEW-SAME: @"symbolic q_" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.10" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.{{[0-9]+}}" // NEW-SAME: @"symbolic SS" // OLD: @"$s4test19PlainlyStored_TestsCMF" = // OLD-SAME: @"symbolic x" // OLD-SAME: @"symbolic q_" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.12" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.{{[0-9]+}}" // OLD-SAME: @"symbolic SS" public class PlainlyStored_Tests { var ncg: NCG From 47e0f9e05c9f263300966c5cab21063c4f4156a1 Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Tue, 23 Sep 2025 20:02:23 -0700 Subject: [PATCH 06/10] Use stringmap to deduplicate strings by their section name --- lib/IRGen/GenDecl.cpp | 2 +- lib/IRGen/IRGenModule.h | 4 ++-- test/IRGen/objc_extensions.swift | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index ea27f48d983b3..ae1741cfdd5b0 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -6052,7 +6052,7 @@ IRGenModule::getAddrOfGlobalString(StringRef data, CStringSectionType type, } // Check whether this string already exists. - auto &entry = GlobalStrings[type][data]; + auto &entry = GlobalStrings[sectionName][data]; if (entry.second) { // FIXME: Clear unnamed_addr if the global will be relative referenced diff --git a/lib/IRGen/IRGenModule.h b/lib/IRGen/IRGenModule.h index 5522e6b7b1550..a06f739e6dc5f 100644 --- a/lib/IRGen/IRGenModule.h +++ b/lib/IRGen/IRGenModule.h @@ -1339,8 +1339,8 @@ class IRGenModule { llvm::DenseMap GlobalGOTEquivalents; llvm::DenseMap GlobalFuncs; llvm::DenseSet GlobalClangDecls; - llvm::DenseMap< - CStringSectionType, + // Maps sectionName -> string data -> constant + llvm::StringMap< llvm::StringMap>> GlobalStrings; llvm::StringMap GlobalUTF16Strings; diff --git a/test/IRGen/objc_extensions.swift b/test/IRGen/objc_extensions.swift index a9874b87d1412..c1bd20dec940b 100644 --- a/test/IRGen/objc_extensions.swift +++ b/test/IRGen/objc_extensions.swift @@ -13,7 +13,7 @@ import objc_extension_base // Check that metadata for nested enums added in extensions to imported classes // gets emitted concretely. -// CHECK: [[CATEGORY_NAME:@.*]] = private constant [16 x i8] c"objc_extensions\00" +// CHECK: [[CATEGORY_NAME:@.*]] = private unnamed_addr constant [16 x i8] c"objc_extensions\00" // CHECK: [[METHOD_TYPE:@.*]] = private unnamed_addr constant [8 x i8] c"v16@0:8\00" // CHECK-LABEL: @"_CATEGORY_PROTOCOLS_Gizmo_$_objc_extensions" = internal constant From 103148afb13b75d274837f50c8e18c6e2eb508c1 Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Tue, 23 Sep 2025 23:45:42 -0700 Subject: [PATCH 07/10] revert test changes --- .../IRGen/noncopyable_field_descriptors.swift | 36 +++++++++---------- .../objc_runtime_visible_conformance.swift | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/test/IRGen/noncopyable_field_descriptors.swift b/test/IRGen/noncopyable_field_descriptors.swift index bdfc8b36f5c09..72678dac4b596 100644 --- a/test/IRGen/noncopyable_field_descriptors.swift +++ b/test/IRGen/noncopyable_field_descriptors.swift @@ -39,16 +39,16 @@ public struct NonCopyable: ~Copyable { } // 'MF' constant as a separator that precedes each field descriptor. // NEW: @"$s4test8CC_TestsCMF" = -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.3" // NEW-SAME: @"symbolic _____yq_G 4test21ConditionallyCopyableOAARi_zrlE" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.4" // NEW-SAME: @"symbolic _____ySSG 4test21ConditionallyCopyableOAARi_zrlE" // OLD: @"$s4test8CC_TestsCMF" = -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.{{[0-9]+}}" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyq_G.{{[0-9]+}}" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.{{[0-9]+}}" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOySSG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.3" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyq_G.4" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.5" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOySSG.6" public class CC_Tests { var ccNCG: ConditionallyCopyable = .none var ccT: ConditionallyCopyable = .none @@ -62,16 +62,16 @@ public class CC_Tests { /// fields until a future runtime says they're safe to reflect. // NEW: @"$s4test8NC_TestsCMF" = -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.{{[0-9]+}}" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.{{[0-9]+}}" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.{{[0-9]+}}" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.5" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.6" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.7" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.8" // OLD: @"$s4test8NC_TestsCMF" = -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.{{[0-9]+}}" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.{{[0-9]+}}" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.{{[0-9]+}}" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.7" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.8" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.9" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.10" public class NC_Tests { var ncNCG: NeverCopyable = .none var ncT: NeverCopyable = .none @@ -83,7 +83,7 @@ public class NC_Tests { // NEW: @"$s4test17StdlibTypes_TestsCMF" = // NEW-SAME: @"symbolic xSg" // NEW-SAME: @"symbolic q_Sg" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.9" // NEW-SAME: @"symbolic SSSg" // NEW-SAME: @"symbolic SPyxG" // NEW-SAME: @"symbolic SPyq_G" @@ -93,7 +93,7 @@ public class NC_Tests { // OLD: @"$s4test17StdlibTypes_TestsCMF" = // OLD-SAME: @"symbolic xSg" // OLD-SAME: @"symbolic q_Sg" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.11" // OLD-SAME: @"symbolic SSSg" // OLD-SAME: @"symbolic SPyxG" // OLD-SAME: @"symbolic SPyq_G" @@ -115,13 +115,13 @@ public class StdlibTypes_Tests { // NEW: @"$s4test19PlainlyStored_TestsCMF" = // NEW-SAME: @"symbolic x" // NEW-SAME: @"symbolic q_" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.10" // NEW-SAME: @"symbolic SS" // OLD: @"$s4test19PlainlyStored_TestsCMF" = // OLD-SAME: @"symbolic x" // OLD-SAME: @"symbolic q_" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.12" // OLD-SAME: @"symbolic SS" public class PlainlyStored_Tests { var ncg: NCG diff --git a/test/IRGen/objc_runtime_visible_conformance.swift b/test/IRGen/objc_runtime_visible_conformance.swift index 2dde5856888ce..5d876e687b263 100644 --- a/test/IRGen/objc_runtime_visible_conformance.swift +++ b/test/IRGen/objc_runtime_visible_conformance.swift @@ -15,4 +15,4 @@ extension A : YourProtocol {} // CHECK-SAME: @"$sSo1AC32objc_runtime_visible_conformance10MyProtocolACWP" // DirectObjCClassName // CHECK-SAME: i32 16 -// CHECK: @.str.21.MyRuntimeVisibleClass = {{.*}} [22 x i8] c"MyRuntimeVisibleClass\00" +// CHECK: @.str.21.MyRuntimeVisibleClass = private constant [22 x i8] c"MyRuntimeVisibleClass\00" From 8f09b5f3f504b4530ec087faa7a10d5c766e03ae Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Wed, 24 Sep 2025 09:04:48 -0700 Subject: [PATCH 08/10] Fix noncopyable test --- .../IRGen/noncopyable_field_descriptors.swift | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/test/IRGen/noncopyable_field_descriptors.swift b/test/IRGen/noncopyable_field_descriptors.swift index 72678dac4b596..bdfc8b36f5c09 100644 --- a/test/IRGen/noncopyable_field_descriptors.swift +++ b/test/IRGen/noncopyable_field_descriptors.swift @@ -39,16 +39,16 @@ public struct NonCopyable: ~Copyable { } // 'MF' constant as a separator that precedes each field descriptor. // NEW: @"$s4test8CC_TestsCMF" = -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.3" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.{{[0-9]+}}" // NEW-SAME: @"symbolic _____yq_G 4test21ConditionallyCopyableOAARi_zrlE" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.4" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.{{[0-9]+}}" // NEW-SAME: @"symbolic _____ySSG 4test21ConditionallyCopyableOAARi_zrlE" // OLD: @"$s4test8CC_TestsCMF" = -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.3" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyq_G.4" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.5" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOySSG.6" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyxG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyq_G.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOyAA03NonC0VG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test21ConditionallyCopyableOySSG.{{[0-9]+}}" public class CC_Tests { var ccNCG: ConditionallyCopyable = .none var ccT: ConditionallyCopyable = .none @@ -62,16 +62,16 @@ public class CC_Tests { /// fields until a future runtime says they're safe to reflect. // NEW: @"$s4test8NC_TestsCMF" = -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.5" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.6" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.7" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.8" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.{{[0-9]+}}" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.{{[0-9]+}}" // OLD: @"$s4test8NC_TestsCMF" = -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.7" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.8" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.9" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.10" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyxG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyq_G.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOyAA03NonC0VG.{{[0-9]+}}" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test13NeverCopyableOySSG.{{[0-9]+}}" public class NC_Tests { var ncNCG: NeverCopyable = .none var ncT: NeverCopyable = .none @@ -83,7 +83,7 @@ public class NC_Tests { // NEW: @"$s4test17StdlibTypes_TestsCMF" = // NEW-SAME: @"symbolic xSg" // NEW-SAME: @"symbolic q_Sg" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.9" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.{{[0-9]+}}" // NEW-SAME: @"symbolic SSSg" // NEW-SAME: @"symbolic SPyxG" // NEW-SAME: @"symbolic SPyq_G" @@ -93,7 +93,7 @@ public class NC_Tests { // OLD: @"$s4test17StdlibTypes_TestsCMF" = // OLD-SAME: @"symbolic xSg" // OLD-SAME: @"symbolic q_Sg" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.11" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableVSg.{{[0-9]+}}" // OLD-SAME: @"symbolic SSSg" // OLD-SAME: @"symbolic SPyxG" // OLD-SAME: @"symbolic SPyq_G" @@ -115,13 +115,13 @@ public class StdlibTypes_Tests { // NEW: @"$s4test19PlainlyStored_TestsCMF" = // NEW-SAME: @"symbolic x" // NEW-SAME: @"symbolic q_" -// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.10" +// NEW-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.{{[0-9]+}}" // NEW-SAME: @"symbolic SS" // OLD: @"$s4test19PlainlyStored_TestsCMF" = // OLD-SAME: @"symbolic x" // OLD-SAME: @"symbolic q_" -// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.12" +// OLD-SAME: @"get_type_metadata Ri_zr0_l4test11NonCopyableV.{{[0-9]+}}" // OLD-SAME: @"symbolic SS" public class PlainlyStored_Tests { var ncg: NCG From 6aba6bf898351578f827c93b143f6acb3180a66d Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Thu, 25 Sep 2025 12:35:58 -0700 Subject: [PATCH 09/10] make GlobalStrings an array --- lib/IRGen/GenDecl.cpp | 9 ++++----- lib/IRGen/IRGenModule.h | 12 +++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index ae1741cfdd5b0..d4fae9d3035ad 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -1065,7 +1065,7 @@ void IRGenModule::SetCStringLiteralSection(llvm::GlobalVariable *GV, GV->setSection(IRGenModule::ObjCMethodTypeSectionName); return; case ObjCLabelType::PropertyName: - GV->setSection(IRGenModule::ObjCPropertyNameSectionName); + GV->setSection(IRGenModule::ObjCMethodNameSectionName); return; } case llvm::Triple::ELF: @@ -6043,16 +6043,15 @@ IRGenModule::getAddrOfGlobalString(StringRef data, CStringSectionType type, case CStringSectionType::ObjCMethodType: sectionName = ObjCMethodTypeSectionName; break; - case CStringSectionType::ObjCPropertyName: - sectionName = ObjCPropertyNameSectionName; - break; case CStringSectionType::OSLogString: sectionName = OSLogStringSectionName; break; + case CStringSectionType::NumTypes: + llvm_unreachable("invalid type"); } // Check whether this string already exists. - auto &entry = GlobalStrings[sectionName][data]; + auto &entry = GlobalStrings[static_cast(type)][data]; if (entry.second) { // FIXME: Clear unnamed_addr if the global will be relative referenced diff --git a/lib/IRGen/IRGenModule.h b/lib/IRGen/IRGenModule.h index a06f739e6dc5f..6b64264bc93d2 100644 --- a/lib/IRGen/IRGenModule.h +++ b/lib/IRGen/IRGenModule.h @@ -674,8 +674,11 @@ enum class CStringSectionType { ObjCClassName, ObjCMethodName, ObjCMethodType, - ObjCPropertyName, OSLogString, + // Place all new section types above this line + NumTypes, + // Place all alias below this line + ObjCPropertyName = ObjCMethodName, }; /// IRGenModule - Primary class for emitting IR for global declarations. @@ -1340,8 +1343,9 @@ class IRGenModule { llvm::DenseMap GlobalFuncs; llvm::DenseSet GlobalClangDecls; // Maps sectionName -> string data -> constant - llvm::StringMap< - llvm::StringMap>> + std::array< + llvm::StringMap>, + static_cast(CStringSectionType::NumTypes)> GlobalStrings; llvm::StringMap GlobalUTF16Strings; llvm::StringMap> @@ -1559,8 +1563,6 @@ class IRGenModule { "__TEXT,__objc_methname,cstring_literals"; static constexpr const char ObjCMethodTypeSectionName[] = "__TEXT,__objc_methtype,cstring_literals"; - static constexpr const char ObjCPropertyNameSectionName[] = - "__TEXT,__objc_methname,cstring_literals"; static constexpr const char OSLogStringSectionName[] = "__TEXT,__oslogstring,cstring_literals"; From e99fc5ff401690845ec66d48494d145f64e1e2ba Mon Sep 17 00:00:00 2001 From: Ellis Hoag Date: Thu, 25 Sep 2025 12:38:15 -0700 Subject: [PATCH 10/10] make switch clear that methname section is reused for properties --- lib/IRGen/GenDecl.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index d4fae9d3035ad..558255905517f 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -1059,14 +1059,12 @@ void IRGenModule::SetCStringLiteralSection(llvm::GlobalVariable *GV, GV->setSection(IRGenModule::ObjCClassNameSectionName); return; case ObjCLabelType::MethodVarName: + case ObjCLabelType::PropertyName: GV->setSection(IRGenModule::ObjCMethodNameSectionName); return; case ObjCLabelType::MethodVarType: GV->setSection(IRGenModule::ObjCMethodTypeSectionName); return; - case ObjCLabelType::PropertyName: - GV->setSection(IRGenModule::ObjCMethodNameSectionName); - return; } case llvm::Triple::ELF: case llvm::Triple::Wasm: