@@ -2716,6 +2716,53 @@ static RValue EmitHipStdParUnsupportedBuiltin(CodeGenFunction *CGF,
27162716 return RValue::get(CGF->Builder.CreateCall(UBF, Args));
27172717}
27182718
2719+ static RValue EmitSYCLFreeFunctionKernelBuiltin(CodeGenFunction &CGF,
2720+ const CallExpr *E,
2721+ StringRef NameStr1,
2722+ StringRef NameStr2,
2723+ bool CheckNDRangeDim = false) {
2724+ const Expr *ArgExpr = E->getArg(0)->IgnoreImpCasts();
2725+ auto *UO = dyn_cast<clang::UnaryOperator>(ArgExpr);
2726+ // If this is of the form &function or *function, get to the function
2727+ // sub-expression.
2728+ if (UO && (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref))
2729+ ArgExpr = UO->getSubExpr()->IgnoreParenImpCasts();
2730+ while (isa<CastExpr>(ArgExpr))
2731+ ArgExpr = cast<CastExpr>(ArgExpr)->getSubExpr();
2732+ auto *DRE = dyn_cast<DeclRefExpr>(ArgExpr);
2733+ if (DRE) {
2734+ const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2735+ if (FD && FD->hasAttr<SYCLAddIRAttributesFunctionAttr>()) {
2736+ auto *SAIRAttr = FD->getAttr<SYCLAddIRAttributesFunctionAttr>();
2737+ SmallVector<std::pair<std::string, std::string>, 4> NameValuePairs =
2738+ SAIRAttr->getFilteredAttributeNameValuePairs(CGF.CGM.getContext());
2739+ for (const auto &NVPair : NameValuePairs) {
2740+ if (!NVPair.first.compare(NameStr1) ||
2741+ (!NameStr2.empty() && !!NVPair.first.compare(NameStr2))) {
2742+ if (CheckNDRangeDim) {
2743+ uint64_t Dim = E->getArg(1)
2744+ ->EvaluateKnownConstInt(CGF.CGM.getContext())
2745+ .getZExtValue();
2746+ // Return true only if the dimensions match.
2747+ if (std::stoul(NVPair.second) == Dim)
2748+ return RValue::get(
2749+ llvm::ConstantInt::getTrue(CGF.ConvertType(E->getType())));
2750+ else
2751+ return RValue::get(
2752+ llvm::ConstantInt::getFalse(CGF.ConvertType(E->getType())));
2753+ }
2754+ // Return true if the kernel type matches.
2755+ return RValue::get(
2756+ llvm::ConstantInt::getTrue(CGF.ConvertType(E->getType())));
2757+ }
2758+ }
2759+ }
2760+ }
2761+ // Return false otherwise.
2762+ return RValue::get(
2763+ llvm::ConstantInt::getFalse(CGF.ConvertType(E->getType())));
2764+ }
2765+
27192766RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
27202767 const CallExpr *E,
27212768 ReturnValueSlot ReturnValue) {
@@ -6363,6 +6410,18 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
63636410 auto Str = CGM.GetAddrOfConstantCString(Name, "");
63646411 return RValue::get(Str.getPointer());
63656412 }
6413+ case Builtin::BI__builtin_sycl_is_kernel: {
6414+ return EmitSYCLFreeFunctionKernelBuiltin(
6415+ *this, E, "sycl-single-task-kernel", "sycl-nd-range-kernel");
6416+ }
6417+ case Builtin::BI__builtin_sycl_is_single_task_kernel: {
6418+ return EmitSYCLFreeFunctionKernelBuiltin(*this, E,
6419+ "sycl-single-task-kernel", "");
6420+ }
6421+ case Builtin::BI__builtin_sycl_is_nd_range_kernel: {
6422+ return EmitSYCLFreeFunctionKernelBuiltin(*this, E, "sycl-nd-range-kernel",
6423+ "", /*CheckNDRangeDim=*/true);
6424+ }
63666425 }
63676426
63686427 // If this is an alias for a lib function (e.g. __builtin_sin), emit
0 commit comments