Skip to content

[NFC] Minor cleanup related to Clang types in SIL #34056

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
24 changes: 20 additions & 4 deletions include/swift/AST/ExtInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ class ASTExtInfoBuilder {
: bits(bits), clangTypeInfo(clangTypeInfo) {}

public:
// Constructor with all defaults.
/// An ExtInfoBuilder for a typical Swift function: @convention(swift),
/// @escaping, non-throwing, non-differentiable.
ASTExtInfoBuilder()
: ASTExtInfoBuilder(Representation::Swift, false, false,
DifferentiabilityKind::NonDifferentiable, nullptr) {}
Expand Down Expand Up @@ -447,6 +448,8 @@ class ASTExtInfo {
};

public:
/// An ExtInfo for a typical Swift function: @convention(swift), @escaping,
/// non-throwing, non-differentiable.
ASTExtInfo() : builder() { builder.checkInvariants(); };

/// Create a builder with the same state as \c this.
Expand Down Expand Up @@ -598,17 +601,22 @@ class SILExtInfoBuilder {
}

public:
// Constructor with all defaults.
SILExtInfoBuilder() : SILExtInfoBuilder(0, ClangTypeInfo(nullptr)) {}
/// An ExtInfoBuilder for a typical Swift function: thick, @escaping,
/// non-pseudogeneric, non-differentiable.
SILExtInfoBuilder()
: SILExtInfoBuilder(makeBits(SILFunctionTypeRepresentation::Thick, false,
false, false,
DifferentiabilityKind::NonDifferentiable),
ClangTypeInfo(nullptr)) {}

// Constructor for polymorphic type.
SILExtInfoBuilder(Representation rep, bool isPseudogeneric, bool isNoEscape,
bool isAsync, DifferentiabilityKind diffKind,
const clang::Type *type)
: SILExtInfoBuilder(makeBits(rep, isPseudogeneric, isNoEscape, isAsync,
diffKind),
ClangTypeInfo(type)) {}

// Constructor for polymorphic type.
SILExtInfoBuilder(ASTExtInfoBuilder info, bool isPseudogeneric)
: SILExtInfoBuilder(makeBits(info.getSILRepresentation(), isPseudogeneric,
info.isNoEscape(), info.isAsync(),
Expand Down Expand Up @@ -686,25 +694,30 @@ class SILExtInfoBuilder {

// Note that we don't have setters. That is by design, use
// the following with methods instead of mutating these objects.
LLVM_NODISCARD
SILExtInfoBuilder withRepresentation(Representation rep) const {
return SILExtInfoBuilder((bits & ~RepresentationMask) | (unsigned)rep,
shouldStoreClangType(rep) ? clangTypeInfo
: ClangTypeInfo());
}
LLVM_NODISCARD
SILExtInfoBuilder withIsPseudogeneric(bool isPseudogeneric = true) const {
return SILExtInfoBuilder(isPseudogeneric ? (bits | PseudogenericMask)
: (bits & ~PseudogenericMask),
clangTypeInfo);
}
LLVM_NODISCARD
SILExtInfoBuilder withNoEscape(bool noEscape = true) const {
return SILExtInfoBuilder(noEscape ? (bits | NoEscapeMask)
: (bits & ~NoEscapeMask),
clangTypeInfo);
}
LLVM_NODISCARD
SILExtInfoBuilder withAsync(bool isAsync = true) const {
return SILExtInfoBuilder(isAsync ? (bits | AsyncMask) : (bits & ~AsyncMask),
clangTypeInfo);
}
LLVM_NODISCARD
SILExtInfoBuilder
withDifferentiabilityKind(DifferentiabilityKind differentiability) const {
return SILExtInfoBuilder(
Expand Down Expand Up @@ -750,13 +763,16 @@ class SILExtInfo {
};

public:
/// An ExtInfo for a typical Swift function: thick, @escaping,
/// non-pseudogeneric, non-differentiable.
SILExtInfo() : builder() { builder.checkInvariants(); };

SILExtInfo(ASTExtInfo info, bool isPseudogeneric)
: builder(info.intoBuilder(), isPseudogeneric) {
builder.checkInvariants();
}

/// A default ExtInfo but with a Thin convention.
static SILExtInfo getThin() {
return SILExtInfoBuilder(SILExtInfoBuilder::Representation::Thin, false,
false, false,
Expand Down
19 changes: 2 additions & 17 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ModuleDecl;
enum DeclAttrKind : unsigned;
class SynthesizedExtensionAnalyzer;
struct PrintOptions;

class SILPrintContext;

/// Necessary information for archetype transformation during printing.
struct TypeTransformContext {
Expand Down Expand Up @@ -594,22 +594,7 @@ struct PrintOptions {
static PrintOptions printDocInterface();

/// Retrieve the set of options suitable for printing SIL functions.
static PrintOptions printSIL(bool printFullConvention = false) {
PrintOptions result;
result.PrintLongAttrsOnSeparateLines = true;
result.PrintStorageRepresentationAttrs = true;
result.AbstractAccessors = false;
result.PrintForSIL = true;
result.PrintInSILBody = true;
result.PreferTypeRepr = false;
result.PrintIfConfig = false;
result.OpaqueReturnTypePrinting =
OpaqueReturnTypePrintingMode::StableReference;
if (printFullConvention)
result.PrintFunctionRepresentationAttrs =
PrintOptions::FunctionRepresentationMode::Full;
return result;
}
static PrintOptions printSIL(const SILPrintContext *silPrintCtx = nullptr);

static PrintOptions printQualifiedSILType() {
PrintOptions result = PrintOptions::printSIL();
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3792,6 +3792,7 @@ namespace {

OS << "\n";
Indent += 2;
// [TODO: Improve-Clang-type-printing]
if (!T->getClangTypeInfo().empty()) {
std::string s;
llvm::raw_string_ostream os(s);
Expand Down Expand Up @@ -3842,6 +3843,7 @@ namespace {
OS << '\n';
T->getInvocationSubstitutions().dump(OS, SubstitutionMap::DumpStyle::Full,
Indent+2);
// [TODO: Improve-Clang-type-printing]
if (!T->getClangTypeInfo().empty()) {
std::string s;
llvm::raw_string_ostream os(s);
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/IR/AbstractionPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ void AbstractionPattern::print(raw_ostream &out) const {
}
getType().dump(out);
out << ", ";
// [TODO: Improve-Clang-type-printing]
// It would be better to use print, but we need a PrintingPolicy
// for that, for which we need a clang LangOptions, and... ugh.
clang::QualType(getClangType(), 0).dump();
Expand Down
29 changes: 22 additions & 7 deletions lib/SIL/IR/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ void SILType::dump() const {
static void printSILFunctionNameAndType(
llvm::raw_ostream &OS, const SILFunction *function,
llvm::DenseMap<CanType, Identifier> &sugaredTypeNames,
bool printFullConvention = false) {
const SILPrintContext *silPrintContext = nullptr) {
function->printName(OS);
OS << " : $";
auto *genEnv = function->getGenericEnvironment();
Expand Down Expand Up @@ -496,7 +496,7 @@ static void printSILFunctionNameAndType(
sugaredTypeNames[archetypeTy->getCanonicalType()] = name;
}
}
auto printOptions = PrintOptions::printSIL(printFullConvention);
auto printOptions = PrintOptions::printSIL(silPrintContext);
printOptions.GenericSig = genSig;
printOptions.AlternativeTypeNames =
sugaredTypeNames.empty() ? nullptr : &sugaredTypeNames;
Expand Down Expand Up @@ -571,8 +571,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
SILPrintContext &PrintCtx,
llvm::DenseMap<CanType, Identifier> *AlternativeTypeNames = nullptr)
: Ctx(PrintCtx), PrintState{{PrintCtx.OS()},
PrintOptions::printSIL(
PrintCtx.printFullConvention())},
PrintOptions::printSIL(&PrintCtx)},
LastBufferID(0) {
PrintState.ASTOptions.AlternativeTypeNames = AlternativeTypeNames;
PrintState.ASTOptions.PrintForSIL = true;
Expand Down Expand Up @@ -2685,8 +2684,7 @@ void SILFunction::print(SILPrintContext &PrintCtx) const {
OS << "[ossa] ";

llvm::DenseMap<CanType, Identifier> sugaredTypeNames;
printSILFunctionNameAndType(OS, this, sugaredTypeNames,
PrintCtx.printFullConvention());
printSILFunctionNameAndType(OS, this, sugaredTypeNames, &PrintCtx);

if (!isExternalDeclaration()) {
if (auto eCount = getEntryCount()) {
Expand Down Expand Up @@ -2971,7 +2969,7 @@ static void printFileIDMap(SILPrintContext &Ctx, const FileIDMap map) {
}

void SILProperty::print(SILPrintContext &Ctx) const {
PrintOptions Options = PrintOptions::printSIL(Ctx.printFullConvention());
PrintOptions Options = PrintOptions::printSIL(&Ctx);

auto &OS = Ctx.OS();
OS << "sil_property ";
Expand Down Expand Up @@ -3612,3 +3610,20 @@ ID SILPrintContext::getID(const SILNode *node) {
ID R = {ID::SSAValue, ValueToIDMap[node]};
return R;
}

PrintOptions PrintOptions::printSIL(const SILPrintContext *ctx) {
PrintOptions result;
result.PrintLongAttrsOnSeparateLines = true;
result.PrintStorageRepresentationAttrs = true;
result.AbstractAccessors = false;
result.PrintForSIL = true;
result.PrintInSILBody = true;
result.PreferTypeRepr = false;
result.PrintIfConfig = false;
result.OpaqueReturnTypePrinting =
OpaqueReturnTypePrintingMode::StableReference;
if (ctx && ctx->printFullConvention())
result.PrintFunctionRepresentationAttrs =
PrintOptions::FunctionRepresentationMode::Full;
return result;
}
9 changes: 1 addition & 8 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,7 @@ SILGenModule::getKeyPathProjectionCoroutine(bool isReadAccess,
: ParameterConvention::Indirect_In_Guaranteed },
};

auto extInfo =
SILFunctionType::ExtInfoBuilder(SILFunctionTypeRepresentation::Thin,
/*pseudogeneric*/ false,
/*non-escaping*/ false,
/*async*/ false,
DifferentiabilityKind::NonDifferentiable,
/*clangFunctionType*/ nullptr)
.build();
auto extInfo = SILFunctionType::ExtInfo::getThin();

auto functionTy = SILFunctionType::get(sig, extInfo,
SILCoroutineKind::YieldOnce,
Expand Down
10 changes: 4 additions & 6 deletions lib/SILGen/SILGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,10 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
};
auto NSApplicationMainType = SILFunctionType::get(
nullptr,
SILFunctionType::ExtInfoBuilder()
// Should be C calling convention, but NSApplicationMain
// has an overlay to fix the type of argv.
.withRepresentation(SILFunctionType::Representation::Thin)
.build(),
SILCoroutineKind::None, ParameterConvention::Direct_Unowned, argTypes,
// Should be C calling convention, but NSApplicationMain
// has an overlay to fix the type of argv.
SILFunctionType::ExtInfo::getThin(), SILCoroutineKind::None,
ParameterConvention::Direct_Unowned, argTypes,
/*yields*/ {},
SILResultInfo(argc->getType().getASTType(), ResultConvention::Unowned),
/*error result*/ None, SubstitutionMap(), SubstitutionMap(),
Expand Down
16 changes: 2 additions & 14 deletions lib/SILOptimizer/Transforms/Outliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,7 @@ CanSILFunctionType BridgedProperty::getOutlinedFunctionType(SILModule &M) {
Results.push_back(SILResultInfo(
switchInfo.Br->getArg(0)->getType().getASTType(),
ResultConvention::Owned));
auto ExtInfo = SILFunctionType::ExtInfoBuilder(
SILFunctionType::Representation::Thin,
/*pseudogeneric*/ false, /*noescape*/ false,
/*async*/ false, DifferentiabilityKind::NonDifferentiable,
/*clangFunctionType*/ nullptr)
.build();
auto ExtInfo = SILFunctionType::ExtInfo::getThin();
auto FunctionType = SILFunctionType::get(
nullptr, ExtInfo, SILCoroutineKind::None,
ParameterConvention::Direct_Unowned, Parameters, /*yields*/ {},
Expand Down Expand Up @@ -1177,14 +1172,7 @@ CanSILFunctionType ObjCMethodCall::getOutlinedFunctionType(SILModule &M) {
++OrigSigIdx;
}

auto ExtInfo =
SILFunctionType::ExtInfoBuilder(SILFunctionType::Representation::Thin,
/*pseudogeneric*/ false,
/*noescape*/ false,
/*async*/ false,
DifferentiabilityKind::NonDifferentiable,
/*clangFunctionType*/ nullptr)
.build();
auto ExtInfo = SILFunctionType::ExtInfo::getThin();

SmallVector<SILResultInfo, 4> Results;
// If we don't have a bridged return we changed from @autoreleased to @owned
Expand Down
15 changes: 4 additions & 11 deletions lib/SILOptimizer/UtilityPasses/BugReducerTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,10 @@ class BugReducerTester : public SILFunctionTransform {
ResultInfoArray.push_back(
SILResultInfo(EmptyTupleCanType, ResultConvention::Unowned));
auto FuncType = SILFunctionType::get(
nullptr,
SILFunctionType::ExtInfoBuilder(
SILFunctionType::Representation::Thin, false /*isPseudoGeneric*/,
false /*noescape*/, false /*async*/,
DifferentiabilityKind::NonDifferentiable,
nullptr /*clangFunctionType*/)
.build(),
SILCoroutineKind::None, ParameterConvention::Direct_Unowned,
ArrayRef<SILParameterInfo>(), ArrayRef<SILYieldInfo>(), ResultInfoArray,
None, SubstitutionMap(), SubstitutionMap(),
getFunction()->getModule().getASTContext());
nullptr, SILFunctionType::ExtInfo::getThin(), SILCoroutineKind::None,
ParameterConvention::Direct_Unowned, ArrayRef<SILParameterInfo>(),
ArrayRef<SILYieldInfo>(), ResultInfoArray, None, SubstitutionMap(),
SubstitutionMap(), getFunction()->getModule().getASTContext());

SILOptFunctionBuilder FunctionBuilder(*this);
SILFunction *F = FunctionBuilder.getOrCreateSharedFunction(
Expand Down
52 changes: 20 additions & 32 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1889,27 +1889,21 @@ static std::pair<Type, Type> getTypeOfReferenceWithSpecialTypeCheckingSemantics(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
TVO_CanBindToNoEscape);
FunctionType::Param arg(escapeClosure);
auto bodyClosure = FunctionType::get(
arg, result,
FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift,
/*noescape*/ true,
/*throws*/ true,
DifferentiabilityKind::NonDifferentiable,
/*clangFunctionType*/ nullptr)
.build());
auto bodyClosure = FunctionType::get(arg, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(true)
.withThrows(true)
.build());
FunctionType::Param args[] = {
FunctionType::Param(noescapeClosure),
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
};

auto refType = FunctionType::get(
args, result,
FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift,
/*noescape*/ false,
/*throws*/ true,
DifferentiabilityKind::NonDifferentiable,
/*clangFunctionType*/ nullptr)
.build());
auto refType = FunctionType::get(args, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(false)
.withThrows(true)
.build());
return {refType, refType};
}
case DeclTypeCheckingSemantics::OpenExistential: {
Expand All @@ -1928,26 +1922,20 @@ static std::pair<Type, Type> getTypeOfReferenceWithSpecialTypeCheckingSemantics(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
TVO_CanBindToNoEscape);
FunctionType::Param bodyArgs[] = {FunctionType::Param(openedTy)};
auto bodyClosure = FunctionType::get(
bodyArgs, result,
FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift,
/*noescape*/ true,
/*throws*/ true,
DifferentiabilityKind::NonDifferentiable,
/*clangFunctionType*/ nullptr)
.build());
auto bodyClosure = FunctionType::get(bodyArgs, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(true)
.withThrows(true)
.build());
FunctionType::Param args[] = {
FunctionType::Param(existentialTy),
FunctionType::Param(bodyClosure, CS.getASTContext().getIdentifier("do")),
};
auto refType = FunctionType::get(
args, result,
FunctionType::ExtInfoBuilder(FunctionType::Representation::Swift,
/*noescape*/ false,
/*throws*/ true,
DifferentiabilityKind::NonDifferentiable,
/*clangFunctionType*/ nullptr)
.build());
auto refType = FunctionType::get(args, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(false)
.withThrows(true)
.build());
return {refType, refType};
}
}
Expand Down