Skip to content

[silgen] Eliminate an unnecessary static function emitArrayToPointer. #11514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/SILGen/RValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ class RValue {
CanType type;
unsigned elementsToBeAdded;

/// Flag value used to mark an rvalue as invalid, because it was
/// consumed or it was default-initialized.
/// \brief Flag value used to mark an rvalue as invalid.
///
/// The reasons why this can be true is:
///
/// 1. The RValue was consumed.
/// 2. The RValue was default-initialized.
/// 3. The RValue was emitted into an SGFContext initialization.
enum : unsigned {
Null = ~0U,
Used = Null - 1,
Expand Down
9 changes: 9 additions & 0 deletions lib/SILGen/SILGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// This file defines SILGenBuilder, a subclass of SILBuilder that provides APIs
/// that traffic in ManagedValue. The intention is that if one is using a
/// SILGenBuilder, the SILGenBuilder will handle preserving ownership invariants
/// (or assert upon failure) freeing the implementor of such concerns.
///
//===----------------------------------------------------------------------===//

#ifndef SWIFT_SILGEN_SILGENBUILDER_H
#define SWIFT_SILGEN_SILGENBUILDER_H
Expand Down
88 changes: 39 additions & 49 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4670,52 +4670,6 @@ ManagedValue SILGenFunction::emitLValueToPointer(SILLocation loc, LValue &&lv,
.getAsSingleValue(*this, loc);
}

static std::pair<ManagedValue, ManagedValue>
emitArrayToPointer(SILGenFunction &SGF, SILLocation loc, ManagedValue array,
SILGenFunction::ArrayAccessInfo accessInfo) {
auto &ctx = SGF.getASTContext();

FuncDecl *converter;
if (accessInfo.AccessKind == AccessKind::Read) {
converter = ctx.getConvertConstArrayToPointerArgument(nullptr);
if (array.isLValue())
array = SGF.B.createLoadCopy(loc, array);

} else {
converter = ctx.getConvertMutableArrayToPointerArgument(nullptr);
assert(array.isLValue());
}

// Invoke the conversion intrinsic, which will produce an owner-pointer pair.
auto *M = SGF.SGM.M.getSwiftModule();
auto firstSubMap =
accessInfo.ArrayType->getContextSubstitutionMap(M, ctx.getArrayDecl());
auto secondSubMap =
accessInfo.PointerType->getContextSubstitutionMap(M,
SGF.getPointerProtocol());

auto *genericSig = converter->getGenericSignature();
auto subMap =
SubstitutionMap::combineSubstitutionMaps(firstSubMap,
secondSubMap,
CombineSubstitutionMaps::AtIndex,
1, 0,
genericSig);

SmallVector<ManagedValue, 2> resultScalars;
SGF.emitApplyOfLibraryIntrinsic(loc, converter, subMap, array, SGFContext())
.getAll(resultScalars);
assert(resultScalars.size() == 2);

// Mark the dependence of the pointer on the owner value.
auto owner = resultScalars[0];
auto pointer = resultScalars[1].forward(SGF);
pointer = SGF.B.createMarkDependence(loc, pointer, owner.getValue());

// The owner's already in its own cleanup. Return the pointer.
return {ManagedValue::forTrivialObjectRValue(pointer), owner};
}

RValue RValueEmitter::visitArrayToPointerExpr(ArrayToPointerExpr *E,
SGFContext C) {
FormalEvaluationScope writeback(SGF);
Expand All @@ -4734,7 +4688,7 @@ RValue RValueEmitter::visitArrayToPointerExpr(ArrayToPointerExpr *E,
array = SGF.emitRValueAsSingleValue(subExpr);
}

auto pointer = ::emitArrayToPointer(SGF, E, array, accessInfo).first;
auto pointer = SGF.emitArrayToPointer(E, array, accessInfo).first;
return RValue(SGF, E, pointer);
}

Expand All @@ -4749,13 +4703,49 @@ SILGenFunction::emitArrayToPointer(SILLocation loc, LValue &&lv,
ArrayAccessInfo accessInfo) {
auto array =
emitAddressOfLValue(loc, std::move(lv), accessInfo.AccessKind);
return ::emitArrayToPointer(*this, loc, array, accessInfo);
return emitArrayToPointer(loc, array, accessInfo);
}

std::pair<ManagedValue, ManagedValue>
SILGenFunction::emitArrayToPointer(SILLocation loc, ManagedValue array,
ArrayAccessInfo accessInfo) {
return ::emitArrayToPointer(*this, loc, array, accessInfo);
auto &ctx = getASTContext();

FuncDecl *converter;
if (accessInfo.AccessKind == AccessKind::Read) {
converter = ctx.getConvertConstArrayToPointerArgument(nullptr);
if (array.isLValue())
array = B.createLoadCopy(loc, array);

} else {
converter = ctx.getConvertMutableArrayToPointerArgument(nullptr);
assert(array.isLValue());
}

// Invoke the conversion intrinsic, which will produce an owner-pointer pair.
auto *M = SGM.M.getSwiftModule();
auto firstSubMap =
accessInfo.ArrayType->getContextSubstitutionMap(M, ctx.getArrayDecl());
auto secondSubMap = accessInfo.PointerType->getContextSubstitutionMap(
M, getPointerProtocol());

auto *genericSig = converter->getGenericSignature();
auto subMap = SubstitutionMap::combineSubstitutionMaps(
firstSubMap, secondSubMap, CombineSubstitutionMaps::AtIndex, 1, 0,
genericSig);

SmallVector<ManagedValue, 2> resultScalars;
emitApplyOfLibraryIntrinsic(loc, converter, subMap, array, SGFContext())
.getAll(resultScalars);
assert(resultScalars.size() == 2);

// Mark the dependence of the pointer on the owner value.
auto owner = resultScalars[0];
auto pointer = resultScalars[1].forward(*this);
pointer = B.createMarkDependence(loc, pointer, owner.getValue());

// The owner's already in its own cleanup. Return the pointer.
return {ManagedValue::forTrivialObjectRValue(pointer), owner};
}

RValue RValueEmitter::visitStringToPointerExpr(StringToPointerExpr *E,
Expand Down