Skip to content

Semantics attribute definition file #28037

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 7 commits into from
Nov 20, 2019
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
85 changes: 85 additions & 0 deletions include/swift/AST/SemanticAttrs.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===--- Semantics.def - Semantics Attribute Definitions -------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This macro is used to map the global definition name of a semantic
/// attribute to its raw value.
/// NAME: the global name used in the compiler
/// C_STR: the raw value used in swift
///
/// SEMANTICS_ATTR(NAME, C_STR)
///
//===----------------------------------------------------------------------===//

#ifndef SEMANTICS_ATTR
#error SEMANTICS_ATTR not defined.
#endif

SEMANTICS_ATTR(STRING_EQUALS, "string.equals")
SEMANTICS_ATTR(STRING_MAKE_UTF8, "string.makeUTF8")
SEMANTICS_ATTR(STRING_ESCAPE_PERCENT_GET, "string.escapePercent.get")
SEMANTICS_ATTR(STRING_CONCAT, "string.concat")
SEMANTICS_ATTR(STRING_APPEND, "string.append")
SEMANTICS_ATTR(STRING_INIT_EMPTY, "string.init_empty")
SEMANTICS_ATTR(STRING_PLUS_EQUALS, "string.plusequals")
SEMANTICS_ATTR(FIND_STRING_SWITCH_CASE, "findStringSwitchCase")
SEMANTICS_ATTR(FIND_STRING_SWITCH_CASE_WITH_CACHE, "findStringSwitchCaseWithCache")

SEMANTICS_ATTR(SWIFT_CONCURRENT_ASYNC, "swift.concurrent.async")
SEMANTICS_ATTR(SWIFT_CONCURRENT_SAFE, "swift.concurrent.safe")
SEMANTICS_ATTR(SWIFT_CONCURRENT, "swift.concurrent")

SEMANTICS_ATTR(ARRAY_APPEND_CONTENTS_OF, "array.append_contentsOf")
SEMANTICS_ATTR(ARRAY_APPEND_ELEMENT, "array.append_element")
SEMANTICS_ATTR(ARRAY_CHECK_INDEX, "array.check_index")
SEMANTICS_ATTR(ARRAY_CHECK_SUBSCRIPT, "array.check_subscript")
SEMANTICS_ATTR(ARRAY_GET_CAPACITY, "array.get_capacity")
SEMANTICS_ATTR(ARRAY_GET_COUNT, "array.get_count")
SEMANTICS_ATTR(ARRAY_GET_ELEMENT, "array.get_element")
SEMANTICS_ATTR(ARRAY_GET_ELEMENT_ADDRESS, "array.get_element_address")
SEMANTICS_ATTR(ARRAY_INIT, "array.init")
SEMANTICS_ATTR(ARRAY_INIT_EMPTY, "array.init.empty")
SEMANTICS_ATTR(ARRAY_MAKE_MUTABLE, "array.make_mutable")
SEMANTICS_ATTR(ARRAY_MUTATE_UNKNOWN, "array.mutate_unknown")
SEMANTICS_ATTR(ARRAY_PROPS_IS_NATIVE_TYPE_CHECKED, "array.props.isNativeTypeChecked")
SEMANTICS_ATTR(ARRAY_RESERVE_CAPACITY_FOR_APPEND, "array.reserve_capacity_for_append")
SEMANTICS_ATTR(ARRAY_UNINITIALIZED, "array.uninitialized")
SEMANTICS_ATTR(ARRAY_WITH_UNSAFE_MUTABLE_BUFFER_POINTER, "array.withUnsafeMutableBufferPointer")
SEMANTICS_ATTR(ARRAY_COUNT, "array.count")
SEMANTICS_ATTR(ARRAY_DEALLOC_UNINITIALIZED, "array.dealloc_uninitialized")
SEMANTICS_ATTR(ARRAY_UNINITIALIZED_INTRINSIC, "array.uninitialized_intrinsic")

SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_NEVER, "optimize.sil.specialize.generic.never")
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_PARTIAL_NEVER,
"optimize.sil.specialize.generic.partial.never")
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_SIZE_NEVER,
"optimize.sil.specialize.generic.size.never")

SEMANTICS_ATTR(OSLOG_INTERPOLATION_INIT, "oslog.interpolation.init")
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_INTERPOLATION, "oslog.message.init_interpolation")
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_STRING_LITERAL, "oslog.message.init_stringliteral")

SEMANTICS_ATTR(TYPE_CHECKER_OPEN_EXISTENTIAL, "typechecker._openExistential(_:do:)")
SEMANTICS_ATTR(TYPE_CHECKER_TYPE, "typechecker.type(of:)")
SEMANTICS_ATTR(TYPE_CHECKER_WITHOUT_ACTUALLY_ESCAPING, "typechecker.withoutActuallyEscaping(_:do:)")

SEMANTICS_ATTR(AVAILABILITY_OSVERSION, "availability.osversion")

SEMANTICS_ATTR(CONSTANT_EVALUABLE, "constant_evaluable")
SEMANTICS_ATTR(EXIT, "exit")
SEMANTICS_ATTR(FASTPATH, "fastpath")
SEMANTICS_ATTR(SLOWPATH, "slowpath")
SEMANTICS_ATTR(PROGRAMTERMINATION_POINT, "programtermination_point")
SEMANTICS_ATTR(CONVERT_TO_OBJECTIVE_C, "convertToObjectiveC")

#undef SEMANTICS_ATTR

31 changes: 31 additions & 0 deletions include/swift/AST/SemanticAttrs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===--- Semantics.h - Semantics Attribute Definitions -------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Implementation of the matching definition file.
/// This file holds all semantics attributes as constant string literals.
///
//===----------------------------------------------------------------------===//

#ifndef SWIFT_SEMANTICS_H
#define SWIFT_SEMANTICS_H

#include "llvm/ADT/StringRef.h"

namespace swift {
namespace semantics {
#define SEMANTICS_ATTR(NAME, C_STR) constexpr static const StringLiteral NAME = C_STR;
#include "SemanticAttrs.def"
}
}

#endif
3 changes: 2 additions & 1 deletion lib/IRGen/AllocStackHoisting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILArgument.h"
#include "swift/AST/SemanticAttrs.h"

#include "IRGenModule.h"
#include "NonFixedTypeInfo.h"
Expand Down Expand Up @@ -349,7 +350,7 @@ bool indicatesDynamicAvailabilityCheckUse(SILInstruction *I) {
auto *Apply = dyn_cast<ApplyInst>(I);
if (!Apply)
return false;
if (Apply->hasSemantics("availability.osversion"))
if (Apply->hasSemantics(semantics::AVAILABILITY_OSVERSION))
return true;
auto *FunRef = Apply->getReferencedFunctionOrNull();
if (!FunRef)
Expand Down
5 changes: 3 additions & 2 deletions lib/SILOptimizer/Analysis/ColdBlockInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "swift/SILOptimizer/Analysis/ColdBlockInfo.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SIL/SILArgument.h"
#include "swift/AST/SemanticAttrs.h"

using namespace swift;

Expand Down Expand Up @@ -83,9 +84,9 @@ ColdBlockInfo::BranchHint ColdBlockInfo::getBranchHint(SILValue Cond,
if (F->hasSemanticsAttrs()) {
// fastpath/slowpath attrs are untested because the inliner luckily
// inlines them before the downstream calls.
if (F->hasSemanticsAttr("slowpath"))
if (F->hasSemanticsAttr(semantics::SLOWPATH))
return BranchHint::LikelyFalse;
else if (F->hasSemanticsAttr("fastpath"))
else if (F->hasSemanticsAttr(semantics::FASTPATH))
return BranchHint::LikelyTrue;
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/Mandatory/OSLogOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include "swift/AST/Module.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/Basic/OptimizationMode.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/Demangling/Demangle.h"
#include "swift/Demangling/Demangler.h"
#include "swift/SIL/BasicBlockUtils.h"
Expand Down Expand Up @@ -119,7 +120,7 @@ static SILFunction *getStringMakeUTF8Init(SILInstruction *inst) {
return nullptr;

SILFunction *callee = apply->getCalleeFunction();
if (!callee || !callee->hasSemanticsAttr("string.makeUTF8"))
if (!callee || !callee->hasSemanticsAttr(semantics::STRING_MAKE_UTF8))
return nullptr;
return callee;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "swift/AST/Module.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/Basic/Range.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/DynamicCasts.h"
#include "swift/SIL/InstructionUtils.h"
Expand Down Expand Up @@ -1648,7 +1649,7 @@ SILInstruction *SILCombiner::visitApplyInst(ApplyInst *AI) {
return I;
}
}
if (SF->hasSemanticsAttr("array.uninitialized")) {
if (SF->hasSemanticsAttr(semantics::ARRAY_UNINITIALIZED)) {
UserListTy Users;
// If the uninitialized array is only written into then it can be removed.
if (recursivelyCollectARCUsers(Users, AI)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/Transforms/ObjectOutliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#define DEBUG_TYPE "objectoutliner"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
Expand Down Expand Up @@ -148,7 +149,7 @@ bool ObjectOutliner::isValidUseOfObject(SILInstruction *I, bool isCOWObject,
// There should only be a single call to findStringSwitchCase. But even
// if there are multiple calls, it's not problem - we'll just optimize the
// last one we find.
if (cast<ApplyInst>(I)->hasSemantics("findStringSwitchCase"))
if (cast<ApplyInst>(I)->hasSemantics(semantics::FIND_STRING_SWITCH_CASE))
*FindStringCall = cast<ApplyInst>(I);
return true;

Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/Transforms/PerformanceInliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#define DEBUG_TYPE "sil-inliner"
#include "swift/AST/Module.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/SIL/OptimizationRemark.h"
#include "swift/SILOptimizer/Analysis/SideEffectAnalysis.h"
Expand Down Expand Up @@ -644,7 +645,7 @@ bool SILPerformanceInliner::decideInColdBlock(FullApplySite AI,
static void addWeightCorrection(FullApplySite FAS,
llvm::DenseMap<FullApplySite, int> &WeightCorrections) {
SILFunction *Callee = FAS.getReferencedFunctionOrNull();
if (Callee && Callee->hasSemanticsAttr("array.uninitialized")) {
if (Callee && Callee->hasSemanticsAttr(semantics::ARRAY_UNINITIALIZED)) {
// We want to inline the argument to an array.uninitialized call, because
// this argument is most likely a call to a function which contains the
// buffer allocation for the array. It is essential to inline it for stack
Expand Down
17 changes: 9 additions & 8 deletions lib/SILOptimizer/Utils/ConstExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "swift/AST/SubstitutionMap.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/NullablePtr.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/Demangling/Demangle.h"
#include "swift/SIL/ApplySite.h"
#include "swift/SIL/FormalLinkage.h"
Expand Down Expand Up @@ -62,24 +63,24 @@ enum class WellKnownFunction {
};

static llvm::Optional<WellKnownFunction> classifyFunction(SILFunction *fn) {
if (fn->hasSemanticsAttr("array.init.empty"))
if (fn->hasSemanticsAttr(semantics::ARRAY_INIT_EMPTY))
return WellKnownFunction::ArrayInitEmpty;
if (fn->hasSemanticsAttr("array.uninitialized_intrinsic"))
if (fn->hasSemanticsAttr(semantics::ARRAY_UNINITIALIZED_INTRINSIC))
return WellKnownFunction::AllocateUninitializedArray;
if (fn->hasSemanticsAttr("array.append_element"))
if (fn->hasSemanticsAttr(semantics::ARRAY_APPEND_ELEMENT))
return WellKnownFunction::ArrayAppendElement;
if (fn->hasSemanticsAttr("string.init_empty"))
if (fn->hasSemanticsAttr(semantics::STRING_INIT_EMPTY))
return WellKnownFunction::StringInitEmpty;
// There are two string initializers in the standard library with the
// semantics "string.makeUTF8". They are identical from the perspective of
// the interpreter. One of those functions is probably redundant and not used.
if (fn->hasSemanticsAttr("string.makeUTF8"))
if (fn->hasSemanticsAttr(semantics::STRING_MAKE_UTF8))
return WellKnownFunction::StringMakeUTF8;
if (fn->hasSemanticsAttr("string.append"))
if (fn->hasSemanticsAttr(semantics::STRING_APPEND))
return WellKnownFunction::StringAppend;
if (fn->hasSemanticsAttr("string.equals"))
if (fn->hasSemanticsAttr(semantics::STRING_EQUALS))
return WellKnownFunction::StringEquals;
if (fn->hasSemanticsAttr("string.escapePercent.get"))
if (fn->hasSemanticsAttr(semantics::STRING_ESCAPE_PERCENT_GET))
return WellKnownFunction::StringEscapePercent;
if (fn->hasSemanticsAttrThatStartsWith("programtermination_point"))
return WellKnownFunction::AssertionFailure;
Expand Down
5 changes: 3 additions & 2 deletions lib/SILOptimizer/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/Expr.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/PatternMatch.h"
#include "swift/SIL/SILBuilder.h"
Expand Down Expand Up @@ -1492,7 +1493,7 @@ static bool isApplyOfBuiltin(SILInstruction &I, BuiltinValueKind kind) {
static bool isApplyOfStringConcat(SILInstruction &I) {
if (auto *AI = dyn_cast<ApplyInst>(&I))
if (auto *Fn = AI->getReferencedFunctionOrNull())
if (Fn->hasSemanticsAttr("string.concat"))
if (Fn->hasSemanticsAttr(semantics::STRING_CONCAT))
return true;
return false;
}
Expand Down Expand Up @@ -1561,7 +1562,7 @@ constantFoldGlobalStringTablePointerBuiltin(BuiltinInst *bi,
FullApplySite stringInitSite = FullApplySite::isa(builtinOperand);
if (!stringInitSite || !stringInitSite.getReferencedFunctionOrNull() ||
!stringInitSite.getReferencedFunctionOrNull()->hasSemanticsAttr(
"string.makeUTF8")) {
semantics::STRING_MAKE_UTF8)) {
// Emit diagnostics only on non-transparent functions.
if (enableDiagnostics && !caller->isTransparent()) {
diagnose(caller->getASTContext(), bi->getLoc().getSourceLoc(),
Expand Down
7 changes: 4 additions & 3 deletions lib/SILOptimizer/Utils/Generics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "swift/AST/TypeMatcher.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/Basic/Statistic.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Serialization/SerializedSILLoader.h"
Expand Down Expand Up @@ -372,18 +373,18 @@ static bool createsInfiniteSpecializationLoop(ApplySite Apply) {

static bool shouldNotSpecialize(SILFunction *Callee, SILFunction *Caller,
SubstitutionMap Subs = {}) {
if (Callee->hasSemanticsAttr("optimize.sil.specialize.generic.never"))
if (Callee->hasSemanticsAttr(semantics::OPTIMIZE_SIL_SPECIALIZE_GENERIC_NEVER))
return true;

if (Caller &&
Caller->getEffectiveOptimizationMode() == OptimizationMode::ForSize &&
Callee->hasSemanticsAttr("optimize.sil.specialize.generic.size.never")) {
Callee->hasSemanticsAttr(semantics::OPTIMIZE_SIL_SPECIALIZE_GENERIC_SIZE_NEVER)) {
return true;
}


if (Subs.hasAnySubstitutableParams() &&
Callee->hasSemanticsAttr("optimize.sil.specialize.generic.partial.never"))
Callee->hasSemanticsAttr(semantics::OPTIMIZE_SIL_SPECIALIZE_GENERIC_PARTIAL_NEVER))
return true;

return false;
Expand Down
7 changes: 4 additions & 3 deletions lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/SIL/BasicBlockUtils.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/DynamicCasts.h"
Expand Down Expand Up @@ -725,7 +726,7 @@ bool StringConcatenationOptimizer::extractStringConcatOperands() {
if (!Fn)
return false;

if (ai->getNumArguments() != 3 || !Fn->hasSemanticsAttr("string.concat"))
if (ai->getNumArguments() != 3 || !Fn->hasSemanticsAttr(semantics::STRING_CONCAT))
return false;

// Left and right operands of a string concatenation operation.
Expand Down Expand Up @@ -756,9 +757,9 @@ bool StringConcatenationOptimizer::extractStringConcatOperands() {

// makeUTF8 should have following parameters:
// (start: RawPointer, utf8CodeUnitCount: Word, isASCII: Int1)
if (!((friLeftFun->hasSemanticsAttr("string.makeUTF8")
if (!((friLeftFun->hasSemanticsAttr(semantics::STRING_MAKE_UTF8)
&& aiLeftOperandsNum == 5)
|| (friRightFun->hasSemanticsAttr("string.makeUTF8")
|| (friRightFun->hasSemanticsAttr(semantics::STRING_MAKE_UTF8)
&& aiRightOperandsNum == 5)))
return false;

Expand Down