Skip to content

Make SILArgumentConvention a "method"-enum. #6444

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
2 changes: 1 addition & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace swift {
class ValueDecl;
class ModuleDecl;
class ProtocolConformance;
enum class SILArgumentConvention : uint8_t;
struct SILArgumentConvention;
enum OptionalTypeKind : unsigned;
enum PointerTypeKind : unsigned;
enum class ValueOwnershipKind : uint8_t;
Expand Down
84 changes: 3 additions & 81 deletions include/swift/SIL/SILArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,15 @@
#ifndef SWIFT_SIL_SILARGUMENT_H
#define SWIFT_SIL_SILARGUMENT_H

#include "swift/SIL/SILValue.h"
#include "swift/SIL/SILArgumentConvention.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILValue.h"

namespace swift {

class SILBasicBlock;
class SILModule;

/// Conventions for apply operands and function-entry arguments in SIL.
///
/// By design, this is exactly the same as ParameterConvention, plus
/// Indirect_Out.
enum class SILArgumentConvention : uint8_t {
Indirect_In,
Indirect_In_Guaranteed,
Indirect_Inout,
Indirect_InoutAliasable,
Indirect_Out,
Direct_Owned,
Direct_Unowned,
Direct_Deallocating,
Direct_Guaranteed,
};

inline bool isIndirectConvention(SILArgumentConvention convention) {
return convention <= SILArgumentConvention::Indirect_Out;
}

/// Turn a ParameterConvention into a SILArgumentConvention.
inline SILArgumentConvention getSILArgumentConvention(ParameterConvention conv){
switch (conv) {
case ParameterConvention::Indirect_In:
return SILArgumentConvention::Indirect_In;
case ParameterConvention::Indirect_Inout:
return SILArgumentConvention::Indirect_Inout;
case ParameterConvention::Indirect_InoutAliasable:
return SILArgumentConvention::Indirect_InoutAliasable;
case ParameterConvention::Indirect_In_Guaranteed:
return SILArgumentConvention::Indirect_In_Guaranteed;
case ParameterConvention::Direct_Unowned:
return SILArgumentConvention::Direct_Unowned;
case ParameterConvention::Direct_Guaranteed:
return SILArgumentConvention::Direct_Guaranteed;
case ParameterConvention::Direct_Owned:
return SILArgumentConvention::Direct_Owned;
case ParameterConvention::Direct_Deallocating:
return SILArgumentConvention::Direct_Deallocating;
}
llvm_unreachable("covered switch isn't covered?!");
}

inline SILArgumentConvention
SILFunctionType::getSILArgumentConvention(unsigned index) const {
assert(index <= getNumSILArguments());
Expand All @@ -72,44 +30,8 @@ SILFunctionType::getSILArgumentConvention(unsigned index) const {
return SILArgumentConvention::Indirect_Out;
} else {
auto param = getParameters()[index - numIndirectResults];
return swift::getSILArgumentConvention(param.getConvention());
}
}

enum class InoutAliasingAssumption {
/// Assume that an inout indirect parameter may alias other objects.
/// This is the safe assumption an optimization should make if it may break
/// memory safety in case the inout aliasing rule is violation.
Aliasing,

/// Assume that an inout indirect parameter cannot alias other objects.
/// Optimizations should only use this if they can guarantee that they will
/// not break memory safety even if the inout aliasing rule is violated.
NotAliasing
};

/// Returns true if \p conv is a not-aliasing indirect parameter.
/// The \p isInoutAliasing specifies what to assume about the inout convention.
/// See InoutAliasingAssumption.
inline bool isNotAliasedIndirectParameter(SILArgumentConvention conv,
InoutAliasingAssumption isInoutAliasing) {
switch (conv) {
case SILArgumentConvention::Indirect_In:
case SILArgumentConvention::Indirect_Out:
case SILArgumentConvention::Indirect_In_Guaranteed:
return true;

case SILArgumentConvention::Indirect_Inout:
return isInoutAliasing == InoutAliasingAssumption::NotAliasing;

case SILArgumentConvention::Indirect_InoutAliasable:
case SILArgumentConvention::Direct_Unowned:
case SILArgumentConvention::Direct_Guaranteed:
case SILArgumentConvention::Direct_Owned:
case SILArgumentConvention::Direct_Deallocating:
return false;
return SILArgumentConvention(param.getConvention());
}
llvm_unreachable("covered switch isn't covered?!");
}

class SILArgument : public ValueBase {
Expand Down
115 changes: 115 additions & 0 deletions include/swift/SIL/SILArgumentConvention.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//===--- SILArgumentConvention.h ------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_SIL_SILARGUMENTCONVENTION_H
#define SWIFT_SIL_SILARGUMENTCONVENTION_H

#include "swift/AST/Types.h"

namespace swift {

enum class InoutAliasingAssumption {
/// Assume that an inout indirect parameter may alias other objects.
/// This is the safe assumption an optimization should make if it may break
/// memory safety in case the inout aliasing rule is violation.
Aliasing,

/// Assume that an inout indirect parameter cannot alias other objects.
/// Optimizations should only use this if they can guarantee that they will
/// not break memory safety even if the inout aliasing rule is violated.
NotAliasing
};

/// Conventions for apply operands and function-entry arguments in SIL.
///
/// By design, this is exactly the same as ParameterConvention, plus
/// Indirect_Out.
struct SILArgumentConvention {
enum : uint8_t {
Indirect_In,
Indirect_In_Guaranteed,
Indirect_Inout,
Indirect_InoutAliasable,
Indirect_Out,
Direct_Owned,
Direct_Unowned,
Direct_Deallocating,
Direct_Guaranteed,
} Value;

SILArgumentConvention(decltype(Value) NewValue) : Value(NewValue) {}

/// Turn a ParameterConvention into a SILArgumentConvention.
explicit SILArgumentConvention(ParameterConvention Conv) : Value() {
switch (Conv) {
case ParameterConvention::Indirect_In:
Value = SILArgumentConvention::Indirect_In;
return;
case ParameterConvention::Indirect_Inout:
Value = SILArgumentConvention::Indirect_Inout;
return;
case ParameterConvention::Indirect_InoutAliasable:
Value = SILArgumentConvention::Indirect_InoutAliasable;
return;
case ParameterConvention::Indirect_In_Guaranteed:
Value = SILArgumentConvention::Indirect_In_Guaranteed;
return;
case ParameterConvention::Direct_Unowned:
Value = SILArgumentConvention::Direct_Unowned;
return;
case ParameterConvention::Direct_Guaranteed:
Value = SILArgumentConvention::Direct_Guaranteed;
return;
case ParameterConvention::Direct_Owned:
Value = SILArgumentConvention::Direct_Owned;
return;
case ParameterConvention::Direct_Deallocating:
Value = SILArgumentConvention::Direct_Deallocating;
return;
}
llvm_unreachable("covered switch isn't covered?!");
}

operator decltype(Value)() const { return Value; }

bool isIndirectConvention() const {
return Value <= SILArgumentConvention::Indirect_Out;
}

/// Returns true if \p Value is a not-aliasing indirect parameter.
/// The \p isInoutAliasing specifies what to assume about the inout
/// convention.
/// See InoutAliasingAssumption.
bool isNotAliasedIndirectParameter(InoutAliasingAssumption isInoutAliasing) {
switch (Value) {
case SILArgumentConvention::Indirect_In:
case SILArgumentConvention::Indirect_Out:
case SILArgumentConvention::Indirect_In_Guaranteed:
return true;

case SILArgumentConvention::Indirect_Inout:
return isInoutAliasing == InoutAliasingAssumption::NotAliasing;

case SILArgumentConvention::Indirect_InoutAliasable:
case SILArgumentConvention::Direct_Unowned:
case SILArgumentConvention::Direct_Guaranteed:
case SILArgumentConvention::Direct_Owned:
case SILArgumentConvention::Direct_Deallocating:
return false;
}
llvm_unreachable("covered switch isn't covered?!");
}
};

} // namespace swift

#endif
7 changes: 4 additions & 3 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/SIL/Consumption.h"
#include "swift/SIL/SILAllocated.h"
#include "swift/SIL/SILArgumentConvention.h"
#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILLocation.h"
#include "swift/SIL/SILSuccessor.h"
#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILValue.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/Support/TrailingObjects.h"

namespace swift {
Expand Down
4 changes: 2 additions & 2 deletions lib/SILOptimizer/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ bool swift::isNotAliasingArgument(SILValue V,
if (!Arg)
return false;

return isNotAliasedIndirectParameter(Arg->getArgumentConvention(),
isInoutAliasing);
SILArgumentConvention Conv = Arg->getArgumentConvention();
return Conv.isNotAliasedIndirectParameter(isInoutAliasing);
}

/// Check if the parameter \V is based on a local object, e.g. it is an
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/IPO/CapturePromotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ isNonescapingUse(Operand *O, SmallVectorImpl<SILInstruction*> &Mutations) {
auto argIndex = O->getOperandNumber()-1;
auto convention =
AI->getSubstCalleeType()->getSILArgumentConvention(argIndex);
if (isIndirectConvention(convention)) {
if (convention.isIndirectConvention()) {
Mutations.push_back(AI);
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/SILOptimizer/Transforms/AllocBoxToStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ static bool partialApplyEscapes(SILValue V, bool examineApply) {

// apply instructions do not capture the pointer when it is passed
// indirectly
if (isIndirectConvention(
apply->getArgumentConvention(UI->getOperandNumber()-1)))
if (apply->getArgumentConvention(UI->getOperandNumber() - 1)
.isIndirectConvention())
continue;

// Optionally drill down into an apply to see if the operand is
Expand Down
15 changes: 8 additions & 7 deletions lib/SILOptimizer/Transforms/CopyForwarding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,21 @@ static SILArgumentConvention getAddressArgConvention(ApplyInst *Apply,
SILValue Address,
Operand *&Oper) {
Oper = nullptr;
SILArgumentConvention Conv;
auto Args = Apply->getArgumentOperands();
llvm::Optional<unsigned> FoundArgIdx;
for (auto ArgIdx : indices(Args)) {
if (Args[ArgIdx].get() != Address)
continue;

Conv = Apply->getArgumentConvention(ArgIdx);
FoundArgIdx = ArgIdx;
assert(!Oper && "Address can only be passed once as an indirection.");
Oper = &Args[ArgIdx];
#ifndef NDEBUG
break;
#endif
}
assert(Oper && "Address value not passed as an argument to this call.");
return Conv;
return Apply->getArgumentConvention(FoundArgIdx.getValue());
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -457,10 +457,11 @@ bool CopyForwarding::collectUsers() {
/// object. However, we can rely on a subsequent mark_dependent
/// instruction to take that object as an operand, causing it to escape
/// for the purpose of this analysis.
assert(isIndirectConvention(
Apply->getSubstCalleeType()->getSILArgumentConvention(
UI->getOperandNumber() - Apply->getArgumentOperandNumber()))
&& "copy_addr location should be passed indirect");
assert(Apply->getSubstCalleeType()
->getSILArgumentConvention(UI->getOperandNumber() -
Apply->getArgumentOperandNumber())
.isIndirectConvention() &&
"copy_addr location should be passed indirect");
SrcUserInsts.insert(Apply);
continue;
}
Expand Down