Skip to content

Commit a5c2236

Browse files
committed
[SGF][Distributed] emitStringLiteral and use it in distributed
1 parent 6622a3c commit a5c2236

File tree

3 files changed

+36
-49
lines changed

3 files changed

+36
-49
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
16581658
} // end anonymous namespace
16591659

16601660
// TODO: move onto SGF directly and reuse in SILGenDistributed and other places
1661-
static PreparedArguments emitStringLiteral(SILGenFunction &SGF, Expr *E,
1661+
static PreparedArguments emitStringLiteralArgs(SILGenFunction &SGF, SILLocation E,
16621662
StringRef Str, SGFContext C,
16631663
StringLiteralExpr::Encoding encoding) {
16641664
uint64_t Length;
@@ -1695,7 +1695,6 @@ static PreparedArguments emitStringLiteral(SILGenFunction &SGF, Expr *E,
16951695
auto Int1Ty = SILType::getBuiltinIntegerType(1, SGF.getASTContext());
16961696
auto *isASCIIInst = SGF.B.createIntegerLiteral(E, Int1Ty, isASCII);
16971697

1698-
16991698
ManagedValue EltsArray[] = {
17001699
ManagedValue::forUnmanaged(string),
17011700
ManagedValue::forUnmanaged(lengthInst),
@@ -1840,7 +1839,7 @@ static bool hasUnownedInnerPointerResult(CanSILFunctionType fnType) {
18401839
static inline PreparedArguments
18411840
buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
18421841
StringLiteralExpr *stringLiteral) {
1843-
return emitStringLiteral(SGF, stringLiteral, stringLiteral->getValue(), C,
1842+
return emitStringLiteralArgs(SGF, stringLiteral, stringLiteral->getValue(), C,
18441843
stringLiteral->getEncoding());
18451844
}
18461845

@@ -1902,7 +1901,7 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
19021901
RegexLiteralExpr *expr) {
19031902
auto &ctx = SGF.getASTContext();
19041903
// %0 = string_literal <regex text>
1905-
auto strLiteralArgs = emitStringLiteral(SGF, expr, expr->getRegexText(), C,
1904+
auto strLiteralArgs = emitStringLiteralArgs(SGF, expr, expr->getRegexText(), C,
19061905
StringLiteralExpr::Encoding::UTF8);
19071906
// %1 = function_ref String.init(
19081907
// _builtinStringLiteral:utf8CodeUnitCount:isASCII:)
@@ -1949,20 +1948,20 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
19491948
case MagicIdentifierLiteralExpr::FileIDSpelledAsFile:
19501949
case MagicIdentifierLiteralExpr::FileID: {
19511950
std::string value = loc.isValid() ? SGF.getMagicFileIDString(loc) : "";
1952-
return emitStringLiteral(SGF, magicLiteral, value, C,
1951+
return emitStringLiteralArgs(SGF, magicLiteral, value, C,
19531952
magicLiteral->getStringEncoding());
19541953
}
19551954

19561955
case MagicIdentifierLiteralExpr::FilePathSpelledAsFile:
19571956
case MagicIdentifierLiteralExpr::FilePath: {
19581957
StringRef value = loc.isValid() ? SGF.getMagicFilePathString(loc) : "";
1959-
return emitStringLiteral(SGF, magicLiteral, value, C,
1958+
return emitStringLiteralArgs(SGF, magicLiteral, value, C,
19601959
magicLiteral->getStringEncoding());
19611960
}
19621961

19631962
case MagicIdentifierLiteralExpr::Function: {
19641963
StringRef value = loc.isValid() ? SGF.getMagicFunctionString() : "";
1965-
return emitStringLiteral(SGF, magicLiteral, value, C,
1964+
return emitStringLiteralArgs(SGF, magicLiteral, value, C,
19661965
magicLiteral->getStringEncoding());
19671966
}
19681967

@@ -2012,6 +2011,24 @@ static inline PreparedArguments buildBuiltinLiteralArgs(SILGenFunction &SGF,
20122011
}
20132012
}
20142013

2014+
ManagedValue SILGenFunction::emitStringLiteral(SILLocation loc,
2015+
StringRef text,
2016+
StringLiteralExpr::Encoding encoding,
2017+
SGFContext ctx) {
2018+
auto &C = getASTContext();
2019+
2020+
// === Prepare the arguments
2021+
auto args = emitStringLiteralArgs(*this, loc, text, ctx, encoding);
2022+
2023+
// === Find the constructor
2024+
auto strInitDecl = C.getStringBuiltinInitDecl(C.getStringDecl());
2025+
2026+
RValue r = emitApplyAllocatingInitializer(loc, strInitDecl, std::move(args),
2027+
/*overriddenSelfType*/ Type(), ctx);
2028+
2029+
return std::move(r).getScalarValue();
2030+
}
2031+
20152032
//===----------------------------------------------------------------------===//
20162033
// Argument Emission
20172034
//===----------------------------------------------------------------------===//

lib/SILGen/SILGenDistributed.cpp

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "swift/Basic/Defer.h"
3030
#include "swift/SIL/SILArgument.h"
3131
#include "swift/SIL/SILDeclRef.h"
32-
#include "swift/SIL/SILUndef.h"
3332
#include "swift/SIL/TypeLowering.h"
3433
#include "swift/SILOptimizer/Utils/DistributedActor.h"
3534

@@ -1320,9 +1319,6 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
13201319
B.emitBlock(makeRemoteCallTargetBB);
13211320
createVoidPhiArgument(*this, ctx, makeRemoteCallTargetBB);
13221321

1323-
auto mangledName = thunk.mangle(SILDeclRef::ManglingKind::Default);
1324-
auto mangledNameRef = llvm::StringRef(mangledName.c_str(), mangledName.size()); // FIXME(distributed): can just pass the mangledName?
1325-
13261322
// --- Get the `RemoteCallTarget` type
13271323

13281324
// %28 = alloc_stack $RemoteCallTarget, let, name "target" // users: %58, %57, %50, %77, %76, %37
@@ -1333,43 +1329,10 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
13331329
auto remoteCallTargetMetatype = getLoweredType(MetatypeType::get(remoteCallTargetTy));
13341330
auto remoteCallTargetMetatypeValue = B.createMetatype(loc, remoteCallTargetMetatype);
13351331

1336-
// %30 = string_literal utf8 "MANGLED_NAME" // user: %35
1337-
auto mangledNameLiteral =
1338-
B.createStringLiteral(loc, mangledNameRef,
1339-
StringLiteralInst::Encoding::UTF8);
1340-
1341-
// %31 = integer_literal $Builtin.Word, 12 // user: %35
1342-
auto codeUnitCountLiteral =
1343-
B.createIntegerLiteral(loc,
1344-
SILType::getBuiltinWordType(ctx),
1345-
mangledName.size());
1346-
1347-
// %32 = integer_literal $Builtin.Int1, -1 // user: %35
1348-
auto isAsciiLiteral =
1349-
B.createIntegerLiteral(loc,
1350-
SILType::getBuiltinIntegerType(1, ctx),
1351-
-1);
1352-
1353-
// %33 = metatype $@thin String.Type // user: %35
1354-
auto StringMetaTy = CanMetatypeType::get(CanType(ctx.getStringType()), MetatypeRepresentation::Thin);
1355-
auto stringSelf =
1356-
B.createMetatype(loc,
1357-
SILType::getPrimitiveObjectType(StringMetaTy));
1358-
1359-
// // function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
1360-
// %34 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String // user: %35
1361-
auto stringInitDeclRef = ctx.getStringBuiltinInitDecl(ctx.getStringDecl());
1362-
auto stringInitRef = SILDeclRef(stringInitDeclRef.getDecl(), SILDeclRef::Kind::Allocator);
1363-
// NotForDefinition since it seems the body of the init function is not there yet:
1364-
auto stringInitFn = builder.getOrCreateFunction(loc, stringInitRef, NotForDefinition);
1365-
auto stringInitFnRef = B.createFunctionRef(loc, stringInitFn);
1366-
1367-
// %35 = apply %34(%30, %31, %32, %33) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String // user: %37
1368-
// auto x = std::move(mangledNameLiteral).getScalarValue()
1369-
auto mangledNameStringValue =
1370-
B.createApply(loc, stringInitFnRef, {},
1371-
/*args*/{mangledNameLiteral, codeUnitCountLiteral,
1372-
isAsciiLiteral, stringSelf});
1332+
auto mangledName = thunk.mangle(SILDeclRef::ManglingKind::Default);
1333+
auto mangledNameRef = llvm::StringRef(mangledName.c_str(), mangledName.size()); // FIXME(distributed): can just pass the mangledName?
1334+
1335+
auto mangledNameString = emitStringLiteral(loc, mangledNameRef); // FIXME(distributed): trouble with the cleanups running in error BB too...
13731336

13741337
// --- Create the RemoteCallTarget instance, passing the mangledNameString
13751338
// function_ref RemoteCallTarget.init(_mangledName:)
@@ -1384,7 +1347,7 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
13841347
// %37 = apply %36(%28, %35, %29) : $@convention(method) (@owned String, @thin RemoteCallTarget.Type) -> @out RemoteCallTarget
13851348
B.createApply(
13861349
loc, remoteCallTargetInitFn, {},
1387-
{/*out*/ remoteCallTargetValue.getValue(), mangledNameStringValue,
1350+
{/*out*/ remoteCallTargetValue.getValue(), mangledNameString.forward(*this),
13881351
remoteCallTargetMetatypeValue});
13891352

13901353
// === Prepare `actorSystem.remoteCall()` --------------------------------

lib/SILGen/SILGenFunction.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,13 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
10721072
SourceLocArgs
10731073
emitSourceLocationArgs(SourceLoc loc, SILLocation emitLoc);
10741074

1075+
ManagedValue
1076+
emitStringLiteral(SILLocation loc,
1077+
StringRef text,
1078+
StringLiteralExpr::Encoding encoding = StringLiteralExpr::Encoding::UTF8,
1079+
SGFContext ctx = SGFContext());
1080+
1081+
10751082
/// Emit a call to the library intrinsic _doesOptionalHaveValue.
10761083
///
10771084
/// The result is a Builtin.Int1.

0 commit comments

Comments
 (0)