@@ -407,39 +407,43 @@ void propagateAspectsThroughCG(Function *F, CallGraphTy &CG,
407
407
// / - checks if return and argument types are using any aspects
408
408
// / - checks if instructions are using any aspects
409
409
// / - updates call graph information
410
- // / - checks if function has "!sycl_used_aspects" metadata
411
- // /
412
- void processFunction (Function &F, FunctionToAspectsMapTy &FunctionToAspects,
410
+ // / - checks if function has "!sycl_used_aspects" and "!sycl_declared_aspects"
411
+ // / metadata and if so collects aspects from this metadata
412
+ void processFunction (Function &F, FunctionToAspectsMapTy &FunctionToUsedAspects,
413
+ FunctionToAspectsMapTy &FunctionToDeclaredAspects,
413
414
TypeToAspectsMapTy &TypesWithAspects, CallGraphTy &CG) {
414
415
const AspectsSetTy RetTyAspects =
415
416
getAspectsFromType (F.getReturnType (), TypesWithAspects);
416
- FunctionToAspects [&F].insert (RetTyAspects.begin (), RetTyAspects.end ());
417
+ FunctionToUsedAspects [&F].insert (RetTyAspects.begin (), RetTyAspects.end ());
417
418
for (Argument &Arg : F.args ()) {
418
419
const AspectsSetTy ArgAspects =
419
420
getAspectsFromType (Arg.getType (), TypesWithAspects);
420
- FunctionToAspects [&F].insert (ArgAspects.begin (), ArgAspects.end ());
421
+ FunctionToUsedAspects [&F].insert (ArgAspects.begin (), ArgAspects.end ());
421
422
}
422
423
423
424
for (Instruction &I : instructions (F)) {
424
425
const AspectsSetTy Aspects =
425
426
getAspectsUsedByInstruction (I, TypesWithAspects);
426
- FunctionToAspects [&F].insert (Aspects.begin (), Aspects.end ());
427
+ FunctionToUsedAspects [&F].insert (Aspects.begin (), Aspects.end ());
427
428
428
429
if (const auto *CI = dyn_cast<CallInst>(&I)) {
429
430
if (!CI->isIndirectCall () && CI->getCalledFunction ())
430
431
CG[&F].insert (CI->getCalledFunction ());
431
432
}
432
433
}
433
434
434
- if (F.hasMetadata (" sycl_used_aspects" )) {
435
- const MDNode *MD = F.getMetadata (" sycl_used_aspects" );
436
- AspectsSetTy Aspects;
437
- for (const MDOperand &Op : MD->operands ()) {
438
- Constant *C = cast<ConstantAsMetadata>(Op.get ())->getValue ();
439
- Aspects.insert (cast<ConstantInt>(C)->getSExtValue ());
435
+ auto CollectAspectsFromMD = [&F](const char * MDName, FunctionToAspectsMapTy &Map) {
436
+ if (const MDNode *MD = F.getMetadata (MDName)) {
437
+ AspectsSetTy Aspects;
438
+ for (const MDOperand &Op : MD->operands ()) {
439
+ Constant *C = cast<ConstantAsMetadata>(Op.get ())->getValue ();
440
+ Aspects.insert (cast<ConstantInt>(C)->getSExtValue ());
441
+ }
442
+ Map[&F].insert (Aspects.begin (), Aspects.end ());
440
443
}
441
- FunctionToAspects[&F].insert (Aspects.begin (), Aspects.end ());
442
- }
444
+ };
445
+ CollectAspectsFromMD (" sycl_used_aspects" , FunctionToUsedAspects);
446
+ CollectAspectsFromMD (" sycl_declared_aspects" , FunctionToDeclaredAspects);
443
447
}
444
448
445
449
// Return true if the function is a SPIRV or SYCL builtin, e.g.
@@ -503,23 +507,34 @@ FunctionToAspectsMapTy
503
507
buildFunctionsToAspectsMap (Module &M, TypeToAspectsMapTy &TypesWithAspects,
504
508
const AspectValueToNameMapTy &AspectValues,
505
509
const std::vector<Function *> &EntryPoints) {
506
- FunctionToAspectsMapTy FunctionToAspects;
510
+ FunctionToAspectsMapTy FunctionToUsedAspects;
511
+ FunctionToAspectsMapTy FunctionToDeclaredAspects;
507
512
CallGraphTy CG;
508
513
509
514
for (Function &F : M.functions ()) {
510
515
if (F.isDeclaration ())
511
516
continue ;
512
- processFunction (F, FunctionToAspects, TypesWithAspects, CG);
517
+ processFunction (F, FunctionToUsedAspects, FunctionToDeclaredAspects,
518
+ TypesWithAspects, CG);
513
519
}
514
520
515
521
SmallPtrSet<const Function *, 16 > Visited;
516
522
for (Function *F : EntryPoints)
517
- propagateAspectsThroughCG (F, CG, FunctionToAspects, Visited);
523
+ propagateAspectsThroughCG (F, CG, FunctionToUsedAspects, Visited);
524
+
525
+ validateUsedAspectsForFunctions (FunctionToUsedAspects, AspectValues,
526
+ EntryPoints, CG);
518
527
519
- validateUsedAspectsForFunctions (FunctionToAspects, AspectValues, EntryPoints,
520
- CG);
528
+ // The set of aspects from FunctionToDeclaredAspects should be merged to the
529
+ // set of FunctionToUsedAspects after validateUsedAspectsForFunctions call to
530
+ // avoid errors during validation.
531
+ Visited.clear ();
532
+ for (Function *F : EntryPoints)
533
+ propagateAspectsThroughCG (F, CG, FunctionToDeclaredAspects, Visited);
534
+ for (const auto &It : FunctionToDeclaredAspects)
535
+ FunctionToUsedAspects[It.first ].insert (It.second .begin (), It.second .end ());
521
536
522
- return FunctionToAspects ;
537
+ return FunctionToUsedAspects ;
523
538
}
524
539
525
540
} // anonymous namespace
@@ -550,10 +565,10 @@ SYCLPropagateAspectsUsagePass::run(Module &M, ModuleAnalysisManager &MAM) {
550
565
551
566
propagateAspectsToOtherTypesInModule (M, TypesWithAspects, AspectValues);
552
567
553
- FunctionToAspectsMapTy FunctionToAspects = buildFunctionsToAspectsMap (
568
+ FunctionToAspectsMapTy FunctionToUsedAspects = buildFunctionsToAspectsMap (
554
569
M, TypesWithAspects, AspectValues, EntryPoints);
555
570
556
- createUsedAspectsMetadataForFunctions (FunctionToAspects );
571
+ createUsedAspectsMetadataForFunctions (FunctionToUsedAspects );
557
572
558
573
setSyclFixedTargetsMD (EntryPoints, TargetFixedAspects, AspectValues);
559
574
0 commit comments