Skip to content

Commit 291364b

Browse files
authored
Merge pull request #28037 from zoecarver/semantics-def
Semantics attribute definition file
2 parents 238d097 + 49ceefc commit 291364b

File tree

12 files changed

+149
-23
lines changed

12 files changed

+149
-23
lines changed

include/swift/AST/SemanticAttrs.def

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===--- Semantics.def - Semantics Attribute Definitions -------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
///
13+
/// \file
14+
/// This macro is used to map the global definition name of a semantic
15+
/// attribute to its raw value.
16+
/// NAME: the global name used in the compiler
17+
/// C_STR: the raw value used in swift
18+
///
19+
/// SEMANTICS_ATTR(NAME, C_STR)
20+
///
21+
//===----------------------------------------------------------------------===//
22+
23+
#ifndef SEMANTICS_ATTR
24+
#error SEMANTICS_ATTR not defined.
25+
#endif
26+
27+
SEMANTICS_ATTR(STRING_EQUALS, "string.equals")
28+
SEMANTICS_ATTR(STRING_MAKE_UTF8, "string.makeUTF8")
29+
SEMANTICS_ATTR(STRING_ESCAPE_PERCENT_GET, "string.escapePercent.get")
30+
SEMANTICS_ATTR(STRING_CONCAT, "string.concat")
31+
SEMANTICS_ATTR(STRING_APPEND, "string.append")
32+
SEMANTICS_ATTR(STRING_INIT_EMPTY, "string.init_empty")
33+
SEMANTICS_ATTR(STRING_PLUS_EQUALS, "string.plusequals")
34+
SEMANTICS_ATTR(FIND_STRING_SWITCH_CASE, "findStringSwitchCase")
35+
SEMANTICS_ATTR(FIND_STRING_SWITCH_CASE_WITH_CACHE, "findStringSwitchCaseWithCache")
36+
37+
SEMANTICS_ATTR(SWIFT_CONCURRENT_ASYNC, "swift.concurrent.async")
38+
SEMANTICS_ATTR(SWIFT_CONCURRENT_SAFE, "swift.concurrent.safe")
39+
SEMANTICS_ATTR(SWIFT_CONCURRENT, "swift.concurrent")
40+
41+
SEMANTICS_ATTR(ARRAY_APPEND_CONTENTS_OF, "array.append_contentsOf")
42+
SEMANTICS_ATTR(ARRAY_APPEND_ELEMENT, "array.append_element")
43+
SEMANTICS_ATTR(ARRAY_CHECK_INDEX, "array.check_index")
44+
SEMANTICS_ATTR(ARRAY_CHECK_SUBSCRIPT, "array.check_subscript")
45+
SEMANTICS_ATTR(ARRAY_GET_CAPACITY, "array.get_capacity")
46+
SEMANTICS_ATTR(ARRAY_GET_COUNT, "array.get_count")
47+
SEMANTICS_ATTR(ARRAY_GET_ELEMENT, "array.get_element")
48+
SEMANTICS_ATTR(ARRAY_GET_ELEMENT_ADDRESS, "array.get_element_address")
49+
SEMANTICS_ATTR(ARRAY_INIT, "array.init")
50+
SEMANTICS_ATTR(ARRAY_INIT_EMPTY, "array.init.empty")
51+
SEMANTICS_ATTR(ARRAY_MAKE_MUTABLE, "array.make_mutable")
52+
SEMANTICS_ATTR(ARRAY_MUTATE_UNKNOWN, "array.mutate_unknown")
53+
SEMANTICS_ATTR(ARRAY_PROPS_IS_NATIVE_TYPE_CHECKED, "array.props.isNativeTypeChecked")
54+
SEMANTICS_ATTR(ARRAY_RESERVE_CAPACITY_FOR_APPEND, "array.reserve_capacity_for_append")
55+
SEMANTICS_ATTR(ARRAY_UNINITIALIZED, "array.uninitialized")
56+
SEMANTICS_ATTR(ARRAY_WITH_UNSAFE_MUTABLE_BUFFER_POINTER, "array.withUnsafeMutableBufferPointer")
57+
SEMANTICS_ATTR(ARRAY_COUNT, "array.count")
58+
SEMANTICS_ATTR(ARRAY_DEALLOC_UNINITIALIZED, "array.dealloc_uninitialized")
59+
SEMANTICS_ATTR(ARRAY_UNINITIALIZED_INTRINSIC, "array.uninitialized_intrinsic")
60+
61+
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_NEVER, "optimize.sil.specialize.generic.never")
62+
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_PARTIAL_NEVER,
63+
"optimize.sil.specialize.generic.partial.never")
64+
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_SIZE_NEVER,
65+
"optimize.sil.specialize.generic.size.never")
66+
67+
SEMANTICS_ATTR(OSLOG_INTERPOLATION_INIT, "oslog.interpolation.init")
68+
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_INTERPOLATION, "oslog.message.init_interpolation")
69+
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_STRING_LITERAL, "oslog.message.init_stringliteral")
70+
71+
SEMANTICS_ATTR(TYPE_CHECKER_OPEN_EXISTENTIAL, "typechecker._openExistential(_:do:)")
72+
SEMANTICS_ATTR(TYPE_CHECKER_TYPE, "typechecker.type(of:)")
73+
SEMANTICS_ATTR(TYPE_CHECKER_WITHOUT_ACTUALLY_ESCAPING, "typechecker.withoutActuallyEscaping(_:do:)")
74+
75+
SEMANTICS_ATTR(AVAILABILITY_OSVERSION, "availability.osversion")
76+
77+
SEMANTICS_ATTR(CONSTANT_EVALUABLE, "constant_evaluable")
78+
SEMANTICS_ATTR(EXIT, "exit")
79+
SEMANTICS_ATTR(FASTPATH, "fastpath")
80+
SEMANTICS_ATTR(SLOWPATH, "slowpath")
81+
SEMANTICS_ATTR(PROGRAMTERMINATION_POINT, "programtermination_point")
82+
SEMANTICS_ATTR(CONVERT_TO_OBJECTIVE_C, "convertToObjectiveC")
83+
84+
#undef SEMANTICS_ATTR
85+

include/swift/AST/SemanticAttrs.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===--- Semantics.h - Semantics Attribute Definitions -------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
///
13+
/// \file
14+
/// Implementation of the matching definition file.
15+
/// This file holds all semantics attributes as constant string literals.
16+
///
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_SEMANTICS_H
20+
#define SWIFT_SEMANTICS_H
21+
22+
#include "llvm/ADT/StringRef.h"
23+
24+
namespace swift {
25+
namespace semantics {
26+
#define SEMANTICS_ATTR(NAME, C_STR) constexpr static const StringLiteral NAME = C_STR;
27+
#include "SemanticAttrs.def"
28+
}
29+
}
30+
31+
#endif

lib/IRGen/AllocStackHoisting.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/SIL/SILBuilder.h"
2222
#include "swift/SIL/SILInstruction.h"
2323
#include "swift/SIL/SILArgument.h"
24+
#include "swift/AST/SemanticAttrs.h"
2425

2526
#include "IRGenModule.h"
2627
#include "NonFixedTypeInfo.h"
@@ -349,7 +350,7 @@ bool indicatesDynamicAvailabilityCheckUse(SILInstruction *I) {
349350
auto *Apply = dyn_cast<ApplyInst>(I);
350351
if (!Apply)
351352
return false;
352-
if (Apply->hasSemantics("availability.osversion"))
353+
if (Apply->hasSemantics(semantics::AVAILABILITY_OSVERSION))
353354
return true;
354355
auto *FunRef = Apply->getReferencedFunctionOrNull();
355356
if (!FunRef)

lib/SILOptimizer/Analysis/ColdBlockInfo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/SILOptimizer/Analysis/ColdBlockInfo.h"
1414
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
1515
#include "swift/SIL/SILArgument.h"
16+
#include "swift/AST/SemanticAttrs.h"
1617

1718
using namespace swift;
1819

@@ -83,9 +84,9 @@ ColdBlockInfo::BranchHint ColdBlockInfo::getBranchHint(SILValue Cond,
8384
if (F->hasSemanticsAttrs()) {
8485
// fastpath/slowpath attrs are untested because the inliner luckily
8586
// inlines them before the downstream calls.
86-
if (F->hasSemanticsAttr("slowpath"))
87+
if (F->hasSemanticsAttr(semantics::SLOWPATH))
8788
return BranchHint::LikelyFalse;
88-
else if (F->hasSemanticsAttr("fastpath"))
89+
else if (F->hasSemanticsAttr(semantics::FASTPATH))
8990
return BranchHint::LikelyTrue;
9091
}
9192
}

lib/SILOptimizer/Mandatory/OSLogOptimization.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include "swift/AST/Module.h"
7878
#include "swift/AST/SubstitutionMap.h"
7979
#include "swift/Basic/OptimizationMode.h"
80+
#include "swift/AST/SemanticAttrs.h"
8081
#include "swift/Demangling/Demangle.h"
8182
#include "swift/Demangling/Demangler.h"
8283
#include "swift/SIL/BasicBlockUtils.h"
@@ -121,7 +122,7 @@ static SILFunction *getStringMakeUTF8Init(SILInstruction *inst) {
121122
return nullptr;
122123

123124
SILFunction *callee = apply->getCalleeFunction();
124-
if (!callee || !callee->hasSemanticsAttr("string.makeUTF8"))
125+
if (!callee || !callee->hasSemanticsAttr(semantics::STRING_MAKE_UTF8))
125126
return nullptr;
126127
return callee;
127128
}

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/Module.h"
1717
#include "swift/AST/SubstitutionMap.h"
1818
#include "swift/Basic/Range.h"
19+
#include "swift/AST/SemanticAttrs.h"
1920
#include "swift/SIL/DebugUtils.h"
2021
#include "swift/SIL/DynamicCasts.h"
2122
#include "swift/SIL/InstructionUtils.h"
@@ -1648,7 +1649,7 @@ SILInstruction *SILCombiner::visitApplyInst(ApplyInst *AI) {
16481649
return I;
16491650
}
16501651
}
1651-
if (SF->hasSemanticsAttr("array.uninitialized")) {
1652+
if (SF->hasSemanticsAttr(semantics::ARRAY_UNINITIALIZED)) {
16521653
UserListTy Users;
16531654
// If the uninitialized array is only written into then it can be removed.
16541655
if (recursivelyCollectARCUsers(Users, AI)) {

lib/SILOptimizer/Transforms/ObjectOutliner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

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

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#define DEBUG_TYPE "sil-inliner"
1414
#include "swift/AST/Module.h"
15+
#include "swift/AST/SemanticAttrs.h"
1516
#include "swift/SIL/MemAccessUtils.h"
1617
#include "swift/SIL/OptimizationRemark.h"
1718
#include "swift/SILOptimizer/Analysis/SideEffectAnalysis.h"
@@ -644,7 +645,7 @@ bool SILPerformanceInliner::decideInColdBlock(FullApplySite AI,
644645
static void addWeightCorrection(FullApplySite FAS,
645646
llvm::DenseMap<FullApplySite, int> &WeightCorrections) {
646647
SILFunction *Callee = FAS.getReferencedFunctionOrNull();
647-
if (Callee && Callee->hasSemanticsAttr("array.uninitialized")) {
648+
if (Callee && Callee->hasSemanticsAttr(semantics::ARRAY_UNINITIALIZED)) {
648649
// We want to inline the argument to an array.uninitialized call, because
649650
// this argument is most likely a call to a function which contains the
650651
// buffer allocation for the array. It is essential to inline it for stack

lib/SILOptimizer/Utils/ConstExpr.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/SubstitutionMap.h"
1717
#include "swift/Basic/Defer.h"
1818
#include "swift/Basic/NullablePtr.h"
19+
#include "swift/AST/SemanticAttrs.h"
1920
#include "swift/Demangling/Demangle.h"
2021
#include "swift/SIL/ApplySite.h"
2122
#include "swift/SIL/FormalLinkage.h"
@@ -62,24 +63,24 @@ enum class WellKnownFunction {
6263
};
6364

6465
static llvm::Optional<WellKnownFunction> classifyFunction(SILFunction *fn) {
65-
if (fn->hasSemanticsAttr("array.init.empty"))
66+
if (fn->hasSemanticsAttr(semantics::ARRAY_INIT_EMPTY))
6667
return WellKnownFunction::ArrayInitEmpty;
67-
if (fn->hasSemanticsAttr("array.uninitialized_intrinsic"))
68+
if (fn->hasSemanticsAttr(semantics::ARRAY_UNINITIALIZED_INTRINSIC))
6869
return WellKnownFunction::AllocateUninitializedArray;
69-
if (fn->hasSemanticsAttr("array.append_element"))
70+
if (fn->hasSemanticsAttr(semantics::ARRAY_APPEND_ELEMENT))
7071
return WellKnownFunction::ArrayAppendElement;
71-
if (fn->hasSemanticsAttr("string.init_empty"))
72+
if (fn->hasSemanticsAttr(semantics::STRING_INIT_EMPTY))
7273
return WellKnownFunction::StringInitEmpty;
7374
// There are two string initializers in the standard library with the
7475
// semantics "string.makeUTF8". They are identical from the perspective of
7576
// the interpreter. One of those functions is probably redundant and not used.
76-
if (fn->hasSemanticsAttr("string.makeUTF8"))
77+
if (fn->hasSemanticsAttr(semantics::STRING_MAKE_UTF8))
7778
return WellKnownFunction::StringMakeUTF8;
78-
if (fn->hasSemanticsAttr("string.append"))
79+
if (fn->hasSemanticsAttr(semantics::STRING_APPEND))
7980
return WellKnownFunction::StringAppend;
80-
if (fn->hasSemanticsAttr("string.equals"))
81+
if (fn->hasSemanticsAttr(semantics::STRING_EQUALS))
8182
return WellKnownFunction::StringEquals;
82-
if (fn->hasSemanticsAttr("string.escapePercent.get"))
83+
if (fn->hasSemanticsAttr(semantics::STRING_ESCAPE_PERCENT_GET))
8384
return WellKnownFunction::StringEscapePercent;
8485
if (fn->hasSemanticsAttrThatStartsWith("programtermination_point"))
8586
return WellKnownFunction::AssertionFailure;

lib/SILOptimizer/Utils/ConstantFolding.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "swift/AST/DiagnosticsSIL.h"
1616
#include "swift/AST/Expr.h"
17+
#include "swift/AST/SemanticAttrs.h"
1718
#include "swift/SIL/InstructionUtils.h"
1819
#include "swift/SIL/PatternMatch.h"
1920
#include "swift/SIL/SILBuilder.h"
@@ -1492,7 +1493,7 @@ static bool isApplyOfBuiltin(SILInstruction &I, BuiltinValueKind kind) {
14921493
static bool isApplyOfStringConcat(SILInstruction &I) {
14931494
if (auto *AI = dyn_cast<ApplyInst>(&I))
14941495
if (auto *Fn = AI->getReferencedFunctionOrNull())
1495-
if (Fn->hasSemanticsAttr("string.concat"))
1496+
if (Fn->hasSemanticsAttr(semantics::STRING_CONCAT))
14961497
return true;
14971498
return false;
14981499
}
@@ -1561,7 +1562,7 @@ constantFoldGlobalStringTablePointerBuiltin(BuiltinInst *bi,
15611562
FullApplySite stringInitSite = FullApplySite::isa(builtinOperand);
15621563
if (!stringInitSite || !stringInitSite.getReferencedFunctionOrNull() ||
15631564
!stringInitSite.getReferencedFunctionOrNull()->hasSemanticsAttr(
1564-
"string.makeUTF8")) {
1565+
semantics::STRING_MAKE_UTF8)) {
15651566
// Emit diagnostics only on non-transparent functions.
15661567
if (enableDiagnostics && !caller->isTransparent()) {
15671568
diagnose(caller->getASTContext(), bi->getLoc().getSourceLoc(),

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/TypeMatcher.h"
1818
#include "swift/AST/DiagnosticEngine.h"
1919
#include "swift/AST/DiagnosticsSIL.h"
20+
#include "swift/AST/SemanticAttrs.h"
2021
#include "swift/Basic/Statistic.h"
2122
#include "swift/AST/TypeCheckRequests.h"
2223
#include "swift/Serialization/SerializedSILLoader.h"
@@ -372,18 +373,18 @@ static bool createsInfiniteSpecializationLoop(ApplySite Apply) {
372373

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

378379
if (Caller &&
379380
Caller->getEffectiveOptimizationMode() == OptimizationMode::ForSize &&
380-
Callee->hasSemanticsAttr("optimize.sil.specialize.generic.size.never")) {
381+
Callee->hasSemanticsAttr(semantics::OPTIMIZE_SIL_SPECIALIZE_GENERIC_SIZE_NEVER)) {
381382
return true;
382383
}
383384

384385

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

389390
return false;

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
1414
#include "swift/AST/GenericSignature.h"
1515
#include "swift/AST/SubstitutionMap.h"
16+
#include "swift/AST/SemanticAttrs.h"
1617
#include "swift/SIL/BasicBlockUtils.h"
1718
#include "swift/SIL/DebugUtils.h"
1819
#include "swift/SIL/DynamicCasts.h"
@@ -725,7 +726,7 @@ bool StringConcatenationOptimizer::extractStringConcatOperands() {
725726
if (!Fn)
726727
return false;
727728

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

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

757758
// makeUTF8 should have following parameters:
758759
// (start: RawPointer, utf8CodeUnitCount: Word, isASCII: Int1)
759-
if (!((friLeftFun->hasSemanticsAttr("string.makeUTF8")
760+
if (!((friLeftFun->hasSemanticsAttr(semantics::STRING_MAKE_UTF8)
760761
&& aiLeftOperandsNum == 5)
761-
|| (friRightFun->hasSemanticsAttr("string.makeUTF8")
762+
|| (friRightFun->hasSemanticsAttr(semantics::STRING_MAKE_UTF8)
762763
&& aiRightOperandsNum == 5)))
763764
return false;
764765

0 commit comments

Comments
 (0)