@@ -163,8 +163,10 @@ bool PerformanceDiagnostics::visitFunction(SILFunction *function,
163163 continue ;
164164
165165 for (SILInstruction &inst : block) {
166- if (visitInst (&inst, perfConstr, parentLoc))
166+ if (visitInst (&inst, perfConstr, parentLoc)) {
167+ LLVM_DEBUG (llvm::dbgs () << inst << *inst.getFunction ());
167168 return true ;
169+ }
168170
169171 if (auto as = FullApplySite::isa (&inst)) {
170172 if (isEffectFreeArraySemanticCall (&inst))
@@ -346,22 +348,51 @@ static bool metatypeUsesAreNotRelevant(MetatypeInst *mt) {
346348 return true ;
347349}
348350
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+
349367bool PerformanceDiagnostics::visitInst (SILInstruction *inst,
350368 PerformanceConstraints perfConstr,
351369 LocWithParent *parentLoc) {
352370 SILType impactType;
353371 RuntimeEffect impact = getRuntimeEffect (inst, impactType);
354372 LocWithParent loc (inst->getLoc ().getSourceLoc (), parentLoc);
355373
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+ }
363395 }
364- return true ;
365396 }
366397
367398 if (perfConstr == PerformanceConstraints::None)
@@ -512,7 +543,9 @@ void PerformanceDiagnostics::checkNonAnnotatedFunction(SILFunction *function) {
512543 for (SILInstruction &inst : block) {
513544 if (function->getModule ().getOptions ().EmbeddedSwift ) {
514545 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+ }
516549 }
517550
518551 auto as = FullApplySite::isa (&inst);
0 commit comments