Skip to content

Commit 07e14a8

Browse files
authored
Merge pull request #4261 from swiftwasm/main
[pull] swiftwasm from main
2 parents 3e99145 + 352b3a2 commit 07e14a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1150
-648
lines changed

include/swift/APIDigester/ModuleAnalyzerNodes.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace api {
6666
const uint8_t DIGESTER_JSON_VERSION = 7; // push SDKNodeRoot to lower-level
6767
const uint8_t DIGESTER_JSON_DEFAULT_VERSION = 0; // Use this version number for files before we have a version number in json.
6868
const StringRef ABIRootKey = "ABIRoot";
69+
const StringRef ConstValuesKey = "ConstValues";
6970

7071
class SDKNode;
7172
typedef SDKNode* NodePtr;
@@ -737,6 +738,8 @@ struct TypeInitInfo {
737738
StringRef ValueOwnership;
738739
};
739740

741+
struct PayLoad;
742+
740743
class SwiftDeclCollector: public VisibleDeclConsumer {
741744
SDKContext &Ctx;
742745
SDKNode *RootNode;
@@ -757,7 +760,7 @@ class SwiftDeclCollector: public VisibleDeclConsumer {
757760

758761
// Serialize the content of all roots to a given file using JSON format.
759762
void serialize(StringRef Filename);
760-
static void serialize(StringRef Filename, SDKNode *Root);
763+
static void serialize(StringRef Filename, SDKNode *Root, PayLoad otherInfo);
761764

762765
// After collecting decls, either from imported modules or from a previously
763766
// serialized JSON file, using this function to get the root of the SDK.
@@ -806,6 +809,7 @@ SDKNodeRoot *getSDKNodeRoot(SDKContext &SDKCtx,
806809

807810
SDKNodeRoot *getEmptySDKNodeRoot(SDKContext &SDKCtx);
808811

812+
void dumpSDKRoot(SDKNodeRoot *Root, PayLoad load, StringRef OutputFile);
809813
void dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile);
810814

811815
int dumpSDKContent(const CompilerInvocation &InitInvok,

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,6 @@ class ASTContext final {
367367
/// i.e. true if the entry is [key: alias_name, value: (real_name, true)].
368368
mutable llvm::DenseMap<Identifier, std::pair<Identifier, bool>> ModuleAliasMap;
369369

370-
/// The maximum arity of `_StringProcessing.Tuple{n}`.
371-
static constexpr unsigned StringProcessingTupleDeclMaxArity = 8;
372-
/// Cached `_StringProcessing.Tuple{n}` declarations.
373-
mutable SmallVector<StructDecl *, StringProcessingTupleDeclMaxArity - 2>
374-
StringProcessingTupleDecls;
375-
376370
/// Retrieve the allocator for the given arena.
377371
llvm::BumpPtrAllocator &
378372
getAllocator(AllocationArena arena = AllocationArena::Permanent) const;
@@ -629,14 +623,6 @@ class ASTContext final {
629623
/// Retrieve _StringProcessing.Regex.init(_regexString: String, version: Int).
630624
ConcreteDeclRef getRegexInitDecl(Type regexType) const;
631625

632-
/// Retrieve the max arity that `_StringProcessing.Tuple{arity}` was
633-
/// instantiated for.
634-
unsigned getStringProcessingTupleDeclMaxArity() const;
635-
636-
/// Retrieve the `_StringProcessing.Tuple{arity}` declaration for the given
637-
/// arity.
638-
StructDecl *getStringProcessingTupleDecl(unsigned arity) const;
639-
640626
/// Retrieve the declaration of Swift.<(Int, Int) -> Bool.
641627
FuncDecl *getLessThanIntDecl() const;
642628

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4837,9 +4837,6 @@ ERROR(string_processing_lib_missing,none,
48374837
ERROR(regex_capture_types_failed_to_decode,none,
48384838
"failed to decode capture types for regular expression literal; this may "
48394839
"be a compiler bug", ())
4840-
ERROR(regex_too_many_captures,none,
4841-
"too many captures in regular expression literal; the current limit is "
4842-
"%0", (unsigned))
48434840

48444841
//------------------------------------------------------------------------------
48454842
// MARK: Type Check Types

include/swift/AST/KnownSDKTypes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,5 @@ KNOWN_SDK_TYPE_DECL(Distributed, RemoteCallTarget, StructDecl, 0)
4949

5050
// String processing
5151
KNOWN_SDK_TYPE_DECL(StringProcessing, Regex, StructDecl, 1)
52-
KNOWN_SDK_TYPE_DECL(StringProcessing, DynamicCaptures, EnumDecl, 0)
5352

5453
#undef KNOWN_SDK_TYPE_DECL

include/swift/AST/Types.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5773,9 +5773,6 @@ class OpenedArchetypeType final : public ArchetypeType,
57735773

57745774
/// Retrieve the ID number of this opened existential.
57755775
UUID getOpenedExistentialID() const;
5776-
5777-
/// Retrieve the opened existential type
5778-
Type getOpenedExistentialType() const;
57795776

57805777
/// Return the archetype that represents the root generic parameter of its
57815778
/// interface type.
@@ -6415,8 +6412,7 @@ inline bool TypeBase::isOpenedExistentialWithError() {
64156412

64166413
CanType T = getCanonicalType();
64176414
if (auto archetype = dyn_cast<OpenedArchetypeType>(T)) {
6418-
auto openedExistentialType = archetype->getOpenedExistentialType();
6419-
return openedExistentialType->isExistentialWithError();
6415+
return archetype->getExistentialType()->isExistentialWithError();
64206416
}
64216417
return false;
64226418
}

include/swift/SIL/SILBasicBlock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,12 @@ public SwiftObjectHeader {
426426
/// the debug scope for newly created instructions.
427427
const SILDebugScope *getScopeOfFirstNonMetaInstruction();
428428

429+
/// Whether the block has any phi arguments.
430+
///
431+
/// Note that a block could have an argument and still return false. The
432+
/// argument must also satisfy SILPhiArgument::isPhiArgument.
433+
bool hasPhi() const;
434+
429435
//===--------------------------------------------------------------------===//
430436
// Debugging
431437
//===--------------------------------------------------------------------===//

include/swift/SIL/SILCloner.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,10 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
248248
return asImpl().remapASTType(ty);
249249
}
250250

251-
void remapOpenedType(CanOpenedArchetypeType archetypeTy) {
252-
auto existentialTy = archetypeTy->getOpenedExistentialType()->getCanonicalType();
251+
void remapRootOpenedType(CanOpenedArchetypeType archetypeTy) {
252+
assert(archetypeTy->isRoot());
253+
254+
auto existentialTy = archetypeTy->getExistentialType()->getCanonicalType();
253255
auto replacementTy = OpenedArchetypeType::get(
254256
getOpASTType(existentialTy),
255257
archetypeTy->getInterfaceType());
@@ -2199,7 +2201,7 @@ template<typename ImplClass>
21992201
void
22002202
SILCloner<ImplClass>::visitOpenExistentialAddrInst(OpenExistentialAddrInst *Inst) {
22012203
// Create a new archetype for this opened existential type.
2202-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2204+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22032205

22042206
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22052207
recordClonedInstruction(
@@ -2212,7 +2214,7 @@ template <typename ImplClass>
22122214
void SILCloner<ImplClass>::visitOpenExistentialValueInst(
22132215
OpenExistentialValueInst *Inst) {
22142216
// Create a new archetype for this opened existential type.
2215-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2217+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22162218

22172219
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22182220
recordClonedInstruction(
@@ -2235,7 +2237,7 @@ visitOpenExistentialMetatypeInst(OpenExistentialMetatypeInst *Inst) {
22352237
exType = exMetatype->getExistentialInstanceType()->getCanonicalType();
22362238
openedType = cast<MetatypeType>(openedType).getInstanceType();
22372239
}
2238-
remapOpenedType(cast<OpenedArchetypeType>(openedType));
2240+
remapRootOpenedType(cast<OpenedArchetypeType>(openedType));
22392241

22402242
if (!Inst->getOperand()->getType().canUseExistentialRepresentation(
22412243
ExistentialRepresentation::Class)) {
@@ -2259,7 +2261,7 @@ void
22592261
SILCloner<ImplClass>::
22602262
visitOpenExistentialRefInst(OpenExistentialRefInst *Inst) {
22612263
// Create a new archetype for this opened existential type.
2262-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2264+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22632265

22642266
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22652267
recordClonedInstruction(
@@ -2276,7 +2278,7 @@ void
22762278
SILCloner<ImplClass>::
22772279
visitOpenExistentialBoxInst(OpenExistentialBoxInst *Inst) {
22782280
// Create a new archetype for this opened existential type.
2279-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2281+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22802282

22812283
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22822284
recordClonedInstruction(Inst, getBuilder().createOpenExistentialBox(
@@ -2290,7 +2292,7 @@ void
22902292
SILCloner<ImplClass>::
22912293
visitOpenExistentialBoxValueInst(OpenExistentialBoxValueInst *Inst) {
22922294
// Create a new archetype for this opened existential type.
2293-
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
2295+
remapRootOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
22942296

22952297
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
22962298
recordClonedInstruction(

include/swift/SILOptimizer/Analysis/Reachability.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "swift/SIL/BasicBlockDatastructures.h"
2828
#include "swift/SIL/SILBasicBlock.h"
29+
#include "swift/SIL/SILSuccessor.h"
30+
#include "llvm/ADT/STLExtras.h"
2931

3032
namespace swift {
3133

@@ -44,16 +46,19 @@ namespace swift {
4446
/// // Mark the beginning of a block reachable. Only called once per block.
4547
/// // Typically a BasicBlockSet wrapper.
4648
/// void markReachableBegin(SILBasicBlock *block);
47-
///
49+
///
4850
/// // Mark the end of a block reachable. Only called once per block.
4951
/// // Typically a BasicBlockSet wrapper.
5052
/// void markReachableEnd(SILBasicBlock *block);
5153
///
5254
/// // Return true if \p inst is a barrier. Called once for each reachable
53-
/// // instruction, assuming that each lastUse is itself a barrier.
55+
/// // instruction, assuming that each lastUsePoint is itself a barrier.
5456
/// // Used by the data flow client to perform additional book-keeping,
5557
/// // such as recording debug_value instructions.
5658
/// bool checkReachableBarrier(SILInstruction *inst);
59+
///
60+
/// // Return true if any of \p block's phis are barriers.
61+
/// bool checkReachablePhiBarrier(SILBasicBlock *block);
5762
/// };
5863
template <typename BlockReachability>
5964
class BackwardReachability {
@@ -70,7 +75,8 @@ class BackwardReachability {
7075
// Initialize data flow starting points before running solveBackward.
7176
void initLastUse(SILInstruction *lastUsePoint) {
7277
auto *lastUseBlock = lastUsePoint->getParent();
73-
if (canReachBlockBegin(lastUsePoint)) {
78+
auto *previous = lastUsePoint->getPreviousInstruction();
79+
if (!previous || canReachBlockBegin(previous)) {
7480
pushPreds(lastUseBlock);
7581
}
7682
}
@@ -116,6 +122,10 @@ class BackwardReachability {
116122

117123
// Propagate global data flow from \p succBB to its predecessors.
118124
void pushPreds(SILBasicBlock *succBB) {
125+
if (succBB->hasPhi())
126+
if (reachableBlocks.checkReachablePhiBarrier(succBB))
127+
return;
128+
119129
reachableBlocks.markReachableBegin(succBB);
120130

121131
for (SILBasicBlock *predBB : succBB->getPredecessorBlocks()) {

include/swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class CanonicalizeBorrowScope {
148148
};
149149

150150
bool shrinkBorrowScope(
151-
BeginBorrowInst *bbi, InstructionDeleter &deleter,
151+
BeginBorrowInst const &bbi, InstructionDeleter &deleter,
152152
SmallVectorImpl<CopyValueInst *> &modifiedCopyValueInsts);
153153

154154
bool foldDestroysOfCopiedLexicalBorrow(BeginBorrowInst *bbi,

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,9 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
746746
} else if (keyString == ABIRootKey) {
747747
Result = constructSDKNode(Ctx,
748748
cast<llvm::yaml::MappingNode>(Pair.getValue()));
749+
} else if (keyString == ConstValuesKey) {
750+
// We don't need to consume the const values from the compiler-side
751+
Pair.skip();
749752
} else {
750753
Ctx.diagnose(Pair.getKey(), diag::sdk_node_unrecognized_key,
751754
keyString);
@@ -2216,8 +2219,81 @@ static parseJsonEmit(SDKContext &Ctx, StringRef FileName) {
22162219
}
22172220
return {std::move(FileBufOrErr.get()), Result};
22182221
}
2222+
enum class ConstKind: uint8_t {
2223+
String = 0,
2224+
Int,
2225+
};
2226+
2227+
struct ConstExprInfo {
2228+
StringRef filePath;
2229+
ConstKind kind;
2230+
unsigned offset = 0;
2231+
unsigned length = 0;
2232+
StringRef value;
2233+
ConstExprInfo(StringRef filePath, ConstKind kind, unsigned offset,
2234+
unsigned length, StringRef value):
2235+
filePath(filePath), kind(kind), offset(offset), length(length), value(value) {}
2236+
ConstExprInfo() = default;
2237+
};
2238+
2239+
class ConstExtractor: public ASTWalker {
2240+
SDKContext &SCtx;
2241+
ASTContext &Ctx;
2242+
SourceManager &SM;
2243+
std::vector<ConstExprInfo> allConsts;
2244+
2245+
void record(Expr *E, ConstKind kind, StringRef Value) {
2246+
auto startLoc = E->getStartLoc();
2247+
// Asserts?
2248+
if (startLoc.isInvalid())
2249+
return;
2250+
auto endLoc = E->getEndLoc();
2251+
assert(endLoc.isValid());
2252+
endLoc = Lexer::getLocForEndOfToken(SM, endLoc);
2253+
auto bufferId = SM.findBufferContainingLoc(startLoc);
2254+
auto length = SM.getByteDistance(startLoc, endLoc);
2255+
auto file = SM.getIdentifierForBuffer(bufferId);
2256+
auto offset = SM.getLocOffsetInBuffer(startLoc, bufferId);
2257+
allConsts.emplace_back(file, kind, offset, length, Value);
2258+
}
2259+
2260+
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
2261+
if (E->isSemanticallyConstExpr()) {
2262+
if (auto *SL = dyn_cast<StringLiteralExpr>(E)) {
2263+
record(SL, ConstKind::String, SL->getValue());
2264+
}
2265+
}
2266+
return { true, E };
2267+
}
2268+
public:
2269+
ConstExtractor(SDKContext &SCtx, ASTContext &Ctx): SCtx(SCtx), Ctx(Ctx),
2270+
SM(Ctx.SourceMgr) {}
2271+
void extract(ModuleDecl *MD) { MD->walk(*this); }
2272+
std::vector<ConstExprInfo> &getAllConstValues() { return allConsts; }
2273+
};
22192274
} // End of anonymous namespace
22202275

2276+
template <> struct swift::json::ObjectTraits<ConstExprInfo> {
2277+
static void mapping(Output &out, ConstExprInfo &info) {
2278+
out.mapRequired("filePath", info.filePath);
2279+
StringRef kind;
2280+
switch(info.kind) {
2281+
#define CASE(X) case ConstKind::X: kind = #X; break;
2282+
CASE(String)
2283+
CASE(Int)
2284+
#undef CASE
2285+
}
2286+
out.mapRequired("kind", kind);
2287+
out.mapRequired("offset", info.offset);
2288+
out.mapRequired("length", info.length);
2289+
out.mapRequired("value", info.value);
2290+
}
2291+
};
2292+
2293+
struct swift::ide::api::PayLoad {
2294+
std::vector<ConstExprInfo> *allContsValues = nullptr;
2295+
};
2296+
22212297
// Construct all roots vector from a given file where a forest was
22222298
// previously dumped.
22232299
void SwiftDeclCollector::deSerialize(StringRef Filename) {
@@ -2226,20 +2302,24 @@ void SwiftDeclCollector::deSerialize(StringRef Filename) {
22262302
}
22272303

22282304
// Serialize the content of all roots to a given file using JSON format.
2229-
void SwiftDeclCollector::serialize(StringRef Filename, SDKNode *Root) {
2305+
void SwiftDeclCollector::serialize(StringRef Filename, SDKNode *Root,
2306+
PayLoad OtherInfo) {
22302307
std::error_code EC;
22312308
llvm::raw_fd_ostream fs(Filename, EC, llvm::sys::fs::OF_None);
22322309
json::Output yout(fs);
22332310
assert(Root->getKind() == SDKNodeKind::Root);
22342311
SDKNodeRoot &root = *static_cast<SDKNodeRoot*>(Root);
22352312
yout.beginObject();
22362313
yout.mapRequired(ABIRootKey, root);
2314+
if (auto *constValues = OtherInfo.allContsValues) {
2315+
yout.mapRequired(ConstValuesKey, *constValues);
2316+
}
22372317
yout.endObject();
22382318
}
22392319

22402320
// Serialize the content of all roots to a given file using JSON format.
22412321
void SwiftDeclCollector::serialize(StringRef Filename) {
2242-
SwiftDeclCollector::serialize(Filename, RootNode);
2322+
SwiftDeclCollector::serialize(Filename, RootNode, PayLoad());
22432323
}
22442324

22452325
SDKNodeRoot *
@@ -2305,16 +2385,21 @@ swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
23052385
return Collector.getSDKRoot();
23062386
}
23072387

2308-
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile) {
2388+
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, PayLoad load,
2389+
StringRef OutputFile) {
23092390
assert(Root);
23102391
auto Opts = Root->getSDKContext().getOpts();
23112392
if (Opts.Verbose)
23122393
llvm::errs() << "Dumping SDK...\n";
2313-
SwiftDeclCollector::serialize(OutputFile, Root);
2394+
SwiftDeclCollector::serialize(OutputFile, Root, load);
23142395
if (Opts.Verbose)
23152396
llvm::errs() << "Dumped to "<< OutputFile << "\n";
23162397
}
23172398

2399+
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile) {
2400+
dumpSDKRoot(Root, PayLoad(), OutputFile);
2401+
}
2402+
23182403
int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
23192404
const llvm::StringSet<> &ModuleNames,
23202405
StringRef OutputFile, CheckerOptions Opts) {
@@ -2357,7 +2442,11 @@ void swift::ide::api::dumpModuleContent(ModuleDecl *MD, StringRef OutputFile,
23572442
SDKContext ctx(opts);
23582443
SwiftDeclCollector collector(ctx);
23592444
collector.lookupVisibleDecls({MD});
2360-
dumpSDKRoot(collector.getSDKRoot(), OutputFile);
2445+
ConstExtractor extractor(ctx, MD->getASTContext());
2446+
extractor.extract(MD);
2447+
PayLoad payload;
2448+
payload.allContsValues = &extractor.getAllConstValues();
2449+
dumpSDKRoot(collector.getSDKRoot(), payload, OutputFile);
23612450
}
23622451

23632452
int swift::ide::api::findDeclUsr(StringRef dumpPath, CheckerOptions Opts) {

0 commit comments

Comments
 (0)