@@ -301,18 +301,33 @@ void SROAMemoryUseAnalyzer::chopUpAlloca(std::vector<AllocStackInst *> &Worklist
301
301
eraseFromParentWithDebugInsts (AI);
302
302
}
303
303
304
- static bool runSROAOnFunction (SILFunction &Fn) {
304
+ // / Returns true, if values of \ty should be ignored, because \p ty is known
305
+ // / by a high-level SIL optimization. Values of that type must not be split
306
+ // / so that those high-level optimizations can analyze the code.
307
+ static bool isSemanticType (ASTContext &ctxt, SILType ty) {
308
+ NominalTypeDecl *stringDecl = ctxt.getStringDecl ();
309
+ if (ty.getStructOrBoundGenericStruct () == stringDecl)
310
+ return true ;
311
+ return false ;
312
+ }
313
+
314
+ static bool runSROAOnFunction (SILFunction &Fn, bool splitSemanticTypes) {
305
315
std::vector<AllocStackInst *> Worklist;
306
316
bool Changed = false ;
317
+ ASTContext &ctxt = Fn.getModule ().getASTContext ();
307
318
308
319
// For each basic block BB in Fn...
309
320
for (auto &BB : Fn)
310
321
// For each instruction in BB...
311
322
for (auto &I : BB)
312
323
// If the instruction is an alloc stack inst, add it to the worklist.
313
- if (auto *AI = dyn_cast<AllocStackInst>(&I))
324
+ if (auto *AI = dyn_cast<AllocStackInst>(&I)) {
325
+ if (!splitSemanticTypes && isSemanticType (ctxt, AI->getElementType ()))
326
+ continue ;
327
+
314
328
if (shouldExpand (Fn.getModule (), AI->getElementType ()))
315
329
Worklist.push_back (AI);
330
+ }
316
331
317
332
while (!Worklist.empty ()) {
318
333
AllocStackInst *AI = Worklist.back ();
@@ -332,6 +347,11 @@ static bool runSROAOnFunction(SILFunction &Fn) {
332
347
namespace {
333
348
class SILSROA : public SILFunctionTransform {
334
349
350
+ bool splitSemanticTypes;
351
+
352
+ public:
353
+ SILSROA (bool splitSemanticTypes) : splitSemanticTypes(splitSemanticTypes) { }
354
+
335
355
// / The entry point to the transformation.
336
356
void run () override {
337
357
SILFunction *F = getFunction ();
@@ -343,7 +363,7 @@ class SILSROA : public SILFunctionTransform {
343
363
LLVM_DEBUG (llvm::dbgs () << " ***** SROA on function: " << F->getName ()
344
364
<< " *****\n " );
345
365
346
- if (runSROAOnFunction (*F))
366
+ if (runSROAOnFunction (*F, splitSemanticTypes ))
347
367
invalidateAnalysis (SILAnalysis::InvalidationKind::Instructions);
348
368
}
349
369
@@ -352,5 +372,9 @@ class SILSROA : public SILFunctionTransform {
352
372
353
373
354
374
SILTransform *swift::createSROA () {
355
- return new SILSROA ();
375
+ return new SILSROA (/* splitSemanticTypes*/ true );
376
+ }
377
+
378
+ SILTransform *swift::createEarlySROA () {
379
+ return new SILSROA (/* splitSemanticTypes*/ false );
356
380
}
0 commit comments