Skip to content

Commit edd7eaa

Browse files
committed
OSLog: os_log strings should be in the __oslogstring section on machO
rdar://121384422
1 parent 443489f commit edd7eaa

File tree

20 files changed

+91
-41
lines changed

20 files changed

+91
-41
lines changed

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extension Instruction {
126126
return false
127127
case let sli as StringLiteralInst:
128128
switch sli.encoding {
129-
case .Bytes, .UTF8:
129+
case .Bytes, .UTF8, .UTF8_OSLOG:
130130
return true
131131
case .ObjCSelector:
132132
// Objective-C selector string literals cannot be used in static

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ final public class StringLiteralInst : SingleValueInstruction {
750750
case UTF8
751751
/// UTF-8 encoding of an Objective-C selector.
752752
case ObjCSelector
753+
case UTF8_OSLOG
753754
}
754755

755756
public var value: StringRef { StringRef(bridged: bridged.StringLiteralInst_getValue()) }
@@ -759,6 +760,7 @@ final public class StringLiteralInst : SingleValueInstruction {
759760
case 0: return .Bytes
760761
case 1: return .UTF8
761762
case 2: return .ObjCSelector
763+
case 3: return .UTF8_OSLOG
762764
default: fatalError("invalid encoding in StringLiteralInst")
763765
}
764766
}

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4328,6 +4328,7 @@ class StringLiteralInst final
43284328
UTF8 = 1,
43294329
/// UTF-8 encoding of an Objective-C selector.
43304330
ObjCSelector = 2,
4331+
UTF8_OSLOG = 3,
43314332
};
43324333

43334334
private:

lib/IRGen/GenConstant.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@ llvm::Constant *irgen::emitConstantFP(IRGenModule &IGM, FloatLiteralInst *FLI) {
106106

107107
llvm::Constant *irgen::emitAddrOfConstantString(IRGenModule &IGM,
108108
StringLiteralInst *SLI) {
109-
switch (SLI->getEncoding()) {
109+
auto encoding = SLI->getEncoding();
110+
bool useOSLogEncoding = encoding == StringLiteralInst::Encoding::UTF8_OSLOG;
111+
112+
switch (encoding) {
110113
case StringLiteralInst::Encoding::Bytes:
111114
case StringLiteralInst::Encoding::UTF8:
112-
return IGM.getAddrOfGlobalString(SLI->getValue());
115+
case StringLiteralInst::Encoding::UTF8_OSLOG:
116+
return IGM.getAddrOfGlobalString(SLI->getValue(), false, useOSLogEncoding);
113117

114118
case StringLiteralInst::Encoding::ObjCSelector:
115119
llvm_unreachable("cannot get the address of an Objective-C selector");

lib/IRGen/GenDecl.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5878,9 +5878,15 @@ Address IRGenFunction::createAlloca(llvm::Type *type,
58785878
/// resolving relative references to coalesceable symbols.
58795879
/// It should be removed when fixed. rdar://problem/22674524
58805880
llvm::Constant *IRGenModule::getAddrOfGlobalString(StringRef data,
5881-
bool willBeRelativelyAddressed) {
5881+
bool willBeRelativelyAddressed,
5882+
bool useOSLogSection) {
5883+
useOSLogSection = useOSLogSection &&
5884+
TargetInfo.OutputObjectFormat == llvm::Triple::MachO;
5885+
58825886
// Check whether this string already exists.
5883-
auto &entry = GlobalStrings[data];
5887+
auto &entry = useOSLogSection ? GlobalOSLogStrings[data] :
5888+
GlobalStrings[data];
5889+
58845890
if (entry.second) {
58855891
// FIXME: Clear unnamed_addr if the global will be relative referenced
58865892
// to work around an ld64 bug. rdar://problem/22674524
@@ -5900,9 +5906,12 @@ llvm::Constant *IRGenModule::getAddrOfGlobalString(StringRef data,
59005906
name[i] = '_';
59015907
(llvm::Twine(".nul") + llvm::Twine(i)).toVector(name);
59025908
}
5903-
5909+
5910+
auto sectionName =
5911+
useOSLogSection ? "__TEXT,__oslogstring,cstring_literals" : "";
5912+
59045913
entry = createStringConstant(data, willBeRelativelyAddressed,
5905-
/*sectionName*/ "", name);
5914+
sectionName, name);
59065915
return entry.second;
59075916
}
59085917

lib/IRGen/IRGenModule.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,8 @@ class IRGenModule {
11081108
StringRef Str, bool willBeRelativelyAddressed = false,
11091109
StringRef sectionName = "", StringRef name = "");
11101110
llvm::Constant *getAddrOfGlobalString(StringRef utf8,
1111-
bool willBeRelativelyAddressed = false);
1111+
bool willBeRelativelyAddressed = false,
1112+
bool useOSLogSection = false);
11121113
llvm::Constant *getAddrOfGlobalUTF16String(StringRef utf8);
11131114
llvm::Constant *getAddrOfObjCSelectorRef(StringRef selector);
11141115
llvm::Constant *getAddrOfObjCSelectorRef(SILDeclRef method);
@@ -1218,6 +1219,8 @@ class IRGenModule {
12181219
llvm::DenseSet<const clang::Decl *> GlobalClangDecls;
12191220
llvm::StringMap<std::pair<llvm::GlobalVariable*, llvm::Constant*>>
12201221
GlobalStrings;
1222+
llvm::StringMap<std::pair<llvm::GlobalVariable*, llvm::Constant*>>
1223+
GlobalOSLogStrings;
12211224
llvm::StringMap<llvm::Constant*> GlobalUTF16Strings;
12221225
llvm::StringMap<std::pair<llvm::GlobalVariable*, llvm::Constant*>>
12231226
StringsForTypeRef;

lib/SIL/IR/SILPrinter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
16741674
switch (kind) {
16751675
case StringLiteralInst::Encoding::Bytes: return "bytes ";
16761676
case StringLiteralInst::Encoding::UTF8: return "utf8 ";
1677+
case StringLiteralInst::Encoding::UTF8_OSLOG: return "oslog ";
16771678
case StringLiteralInst::Encoding::ObjCSelector: return "objc_selector ";
16781679
}
16791680
llvm_unreachable("bad string literal encoding");

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,6 +2729,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
27292729
encoding = StringLiteralInst::Encoding::ObjCSelector;
27302730
} else if (P.Tok.getText() == "bytes") {
27312731
encoding = StringLiteralInst::Encoding::Bytes;
2732+
} else if (P.Tok.getText() == "oslog") {
2733+
encoding = StringLiteralInst::Encoding::UTF8_OSLOG;
27322734
} else {
27332735
P.diagnose(P.Tok, diag::sil_string_invalid_encoding, P.Tok.getText());
27342736
return true;

lib/SILGen/SILGenApply.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,7 @@ static PreparedArguments emitStringLiteralArgs(SILGenFunction &SGF, SILLocation
18521852
break;
18531853

18541854
case StringLiteralInst::Encoding::Bytes:
1855+
case StringLiteralInst::Encoding::UTF8_OSLOG:
18551856
case StringLiteralInst::Encoding::ObjCSelector:
18561857
llvm_unreachable("these cannot be formed here");
18571858
}

lib/SILOptimizer/Mandatory/OSLogOptimization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ static SILValue emitCodeForSymbolicValue(SymbolicValue symVal,
637637

638638
StringRef stringVal = symVal.getStringValue();
639639
StringLiteralInst *stringLitInst = builder.createStringLiteral(
640-
loc, stringVal, StringLiteralInst::Encoding::UTF8);
640+
loc, stringVal, StringLiteralInst::Encoding::UTF8_OSLOG);
641641

642642
// Create a builtin word for the size of the string
643643
IntegerLiteralInst *sizeInst = builder.createIntegerLiteral(
@@ -1427,7 +1427,7 @@ suppressGlobalStringTablePointerError(SingleValueInstruction *oslogMessage) {
14271427
for (BuiltinInst *bi : globalStringTablePointerInsts) {
14281428
SILBuilderWithScope builder(bi);
14291429
StringLiteralInst *stringLiteral = builder.createStringLiteral(
1430-
bi->getLoc(), StringRef(""), StringLiteralInst::Encoding::UTF8);
1430+
bi->getLoc(), StringRef(""), StringLiteralInst::Encoding::UTF8_OSLOG);
14311431
bi->replaceAllUsesWith(stringLiteral);
14321432
// The builtin instruction is likely dead. But since we are iterating over
14331433
// many instructions, do the cleanup at the end.

0 commit comments

Comments
 (0)