@@ -163,8 +163,10 @@ bool PerformanceDiagnostics::visitFunction(SILFunction *function,
163
163
continue ;
164
164
165
165
for (SILInstruction &inst : block) {
166
- if (visitInst (&inst, perfConstr, parentLoc))
166
+ if (visitInst (&inst, perfConstr, parentLoc)) {
167
+ LLVM_DEBUG (llvm::dbgs () << inst << *inst.getFunction ());
167
168
return true ;
169
+ }
168
170
169
171
if (auto as = FullApplySite::isa (&inst)) {
170
172
if (isEffectFreeArraySemanticCall (&inst))
@@ -346,22 +348,51 @@ static bool metatypeUsesAreNotRelevant(MetatypeInst *mt) {
346
348
return true ;
347
349
}
348
350
351
+ static bool allowedMetadataUseInEmbeddedSwift (SILInstruction *inst) {
352
+ // Only diagnose metatype and value_metatype instructions, for now.
353
+ if ((isa<ValueMetatypeInst>(inst) || isa<MetatypeInst>(inst))) {
354
+ auto metaTy = cast<SingleValueInstruction>(inst)->getType ().castTo <MetatypeType>();
355
+ if (metaTy->getRepresentation () == MetatypeRepresentation::Thick) {
356
+ Type instTy = metaTy->getInstanceType ();
357
+ if (auto selfType = instTy->getAs <DynamicSelfType>())
358
+ instTy = selfType->getSelfType ();
359
+ // Class metadata are supported in embedded Swift
360
+ return instTy->getClassOrBoundGenericClass () ? true : false ;
361
+ }
362
+ }
363
+
364
+ return true ;
365
+ }
366
+
349
367
bool PerformanceDiagnostics::visitInst (SILInstruction *inst,
350
368
PerformanceConstraints perfConstr,
351
369
LocWithParent *parentLoc) {
352
370
SILType impactType;
353
371
RuntimeEffect impact = getRuntimeEffect (inst, impactType);
354
372
LocWithParent loc (inst->getLoc ().getSourceLoc (), parentLoc);
355
373
356
- if (module .getOptions ().EmbeddedSwift &&
357
- impact & RuntimeEffect::Existential) {
358
- PrettyStackTracePerformanceDiagnostics stackTrace (" existential" , inst);
359
- if (impactType) {
360
- diagnose (loc, diag::embedded_swift_existential_type, impactType.getASTType ());
361
- } else {
362
- diagnose (loc, diag::embedded_swift_existential);
374
+ if (module .getOptions ().EmbeddedSwift ) {
375
+ if (impact & RuntimeEffect::Existential) {
376
+ PrettyStackTracePerformanceDiagnostics stackTrace (" existential" , inst);
377
+ if (impactType) {
378
+ diagnose (loc, diag::embedded_swift_existential_type, impactType.getASTType ());
379
+ } else {
380
+ diagnose (loc, diag::embedded_swift_existential);
381
+ }
382
+ return true ;
383
+ }
384
+
385
+ if (impact & RuntimeEffect::MetaData) {
386
+ if (!allowedMetadataUseInEmbeddedSwift (inst)) {
387
+ PrettyStackTracePerformanceDiagnostics stackTrace (" metatype" , inst);
388
+ if (impactType) {
389
+ diagnose (loc, diag::embedded_swift_metatype_type, impactType.getASTType ());
390
+ } else {
391
+ diagnose (loc, diag::embedded_swift_metatype);
392
+ }
393
+ return true ;
394
+ }
363
395
}
364
- return true ;
365
396
}
366
397
367
398
if (perfConstr == PerformanceConstraints::None)
@@ -512,7 +543,9 @@ void PerformanceDiagnostics::checkNonAnnotatedFunction(SILFunction *function) {
512
543
for (SILInstruction &inst : block) {
513
544
if (function->getModule ().getOptions ().EmbeddedSwift ) {
514
545
auto loc = LocWithParent (inst.getLoc ().getSourceLoc (), nullptr );
515
- visitInst (&inst, PerformanceConstraints::None, &loc);
546
+ if (visitInst (&inst, PerformanceConstraints::None, &loc)) {
547
+ LLVM_DEBUG (llvm::dbgs () << inst << *inst.getFunction ());
548
+ }
516
549
}
517
550
518
551
auto as = FullApplySite::isa (&inst);
0 commit comments