| 1 | //===--------- CIRGenSYCL.cpp - Emit CIR for SYCL kernels -----------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This contains code required for the generation of SYCL kernel code. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "CIRGenFunction.h" |
| 14 | #include "CIRGenModule.h" |
| 15 | |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Attr.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/SYCLKernelInfo.h" |
| 20 | #include "clang/AST/StmtSYCL.h" |
| 21 | |
| 22 | #include "llvm/Support/SaveAndRestore.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | using namespace clang::CIRGen; |
| 26 | |
| 27 | mlir::LogicalResult |
| 28 | CIRGenFunction::emitSYCLKernelCallStmt(const SYCLKernelCallStmt &s) { |
| 29 | // SYCLKernelCallStmt nodes are only present in the bodies of functions |
| 30 | // declared with the sycl_kernel_entry_point attribute. ODR-use of such a |
| 31 | // function in code emitted during device compilation should be diagnosed. |
| 32 | // During device compilation, the offload kernel entry point is emitted in |
| 33 | // place of such a function (see CIRGenModule::emitDeferred), so this |
| 34 | // function is only reached during host compilation. |
| 35 | assert(!getLangOpts().SYCLIsDevice && |
| 36 | "Attempt to emit a SYCL kernel call statement during device " |
| 37 | "compilation" ); |
| 38 | |
| 39 | // During host compilation, the kernel launch statement is emitted in place |
| 40 | // of the original function body. |
| 41 | return emitStmt(s: s.getKernelLaunchStmt(), /*useCurrentScope=*/true); |
| 42 | } |
| 43 | |
| 44 | // Emit the body of a SYCL kernel caller offload entry point. Mirrors the tail |
| 45 | // of generateCode, but is driven by an OutlinedFunctionDecl and an explicit |
| 46 | // argument list rather than a FunctionDecl. |
| 47 | void CIRGenFunction::emitSYCLKernelCaller( |
| 48 | const OutlinedFunctionDecl *outlinedFnDecl, cir::FuncOp funcOp, |
| 49 | cir::FuncType funcType, FunctionArgList &args) { |
| 50 | const Stmt *body = outlinedFnDecl->getBody(); |
| 51 | SourceLocation loc = outlinedFnDecl->getLocation(); |
| 52 | SourceRange bodyRange = body->getSourceRange(); |
| 53 | |
| 54 | // Synthesized entry point: no FunctionDecl, emitted with an empty GlobalDecl. |
| 55 | curGD = GlobalDecl(); |
| 56 | |
| 57 | SourceLocRAIIObject fnLoc{*this, loc}; |
| 58 | |
| 59 | mlir::Location fusedLoc = getLoc(srcLoc: bodyRange); |
| 60 | mlir::Block *entryBB = funcOp.addEntryBlock(); |
| 61 | |
| 62 | SymTableScopeTy varScope(symbolTable); |
| 63 | { |
| 64 | LexicalScope lexScope(*this, fusedLoc, entryBB); |
| 65 | startFunction(gd: GlobalDecl(), returnType: getContext().VoidTy, fn: funcOp, funcType, args, |
| 66 | loc, startLoc: bodyRange.getBegin()); |
| 67 | if (mlir::failed(Result: emitFunctionBody(body))) |
| 68 | return; |
| 69 | if (mlir::failed(Result: funcOp.verifyBody())) |
| 70 | return; |
| 71 | finishFunction(endLoc: body->getEndLoc()); |
| 72 | } |
| 73 | |
| 74 | eraseEmptyAndUnusedBlocks(func: funcOp); |
| 75 | } |
| 76 | |
| 77 | static void setSYCLKernelAttributes(CIRGenFunction &cgf, cir::FuncOp fn) { |
| 78 | mlir::MLIRContext *ctx = &cgf.getMLIRContext(); |
| 79 | // SYCL 2020 device language restrictions require forward progress and |
| 80 | // disallow recursion. |
| 81 | fn->setAttr(name: cir::CIRDialect::getNoRecurseAttrName(), |
| 82 | value: mlir::UnitAttr::get(context: ctx)); |
| 83 | if (cgf.checkIfFunctionMustProgress()) |
| 84 | fn->setAttr(name: cir::CIRDialect::getMustProgressAttrName(), |
| 85 | value: mlir::UnitAttr::get(context: ctx)); |
| 86 | } |
| 87 | |
| 88 | void CIRGenModule::addSYCLModuleIdAttr(cir::FuncOp fn) { |
| 89 | assert(getLangOpts().SYCLIsDevice); |
| 90 | // Classic CodeGen uses the LLVM module identifier, which is the main input |
| 91 | // file name. CIR stores that as the module's symbol name; fall back to the |
| 92 | // main file name so the attribute is never empty, matching classic CodeGen. |
| 93 | StringRef moduleId = |
| 94 | getModule().getSymName().value_or(u: codeGenOpts.MainFileName); |
| 95 | fn->setAttr(name: cir::CIRDialect::getSYCLModuleIdAttrName(), |
| 96 | value: mlir::StringAttr::get(context: &getMLIRContext(), bytes: moduleId)); |
| 97 | } |
| 98 | |
| 99 | void CIRGenModule::emitSYCLKernelCaller(const FunctionDecl *kernelEntryPointFn, |
| 100 | ASTContext &ctx) { |
| 101 | assert(ctx.getLangOpts().SYCLIsDevice && |
| 102 | "SYCL kernel caller offload entry point functions can only be emitted" |
| 103 | " during device compilation" ); |
| 104 | |
| 105 | const auto *kernelEntryPointAttr = |
| 106 | kernelEntryPointFn->getAttr<SYCLKernelEntryPointAttr>(); |
| 107 | assert(kernelEntryPointAttr && "Missing sycl_kernel_entry_point attribute" ); |
| 108 | assert(!kernelEntryPointAttr->isInvalidAttr() && |
| 109 | "sycl_kernel_entry_point attribute is invalid" ); |
| 110 | |
| 111 | // Find the SYCLKernelCallStmt. |
| 112 | SYCLKernelCallStmt *kernelCallStmt = |
| 113 | cast<SYCLKernelCallStmt>(Val: kernelEntryPointFn->getBody()); |
| 114 | |
| 115 | // Retrieve the SYCL kernel caller parameters from the OutlinedFunctionDecl. |
| 116 | FunctionArgList args; |
| 117 | const OutlinedFunctionDecl *outlinedFnDecl = |
| 118 | kernelCallStmt->getOutlinedFunctionDecl(); |
| 119 | args.append(in_start: outlinedFnDecl->param_begin(), in_end: outlinedFnDecl->param_end()); |
| 120 | |
| 121 | // Compute the function info and CIR function type. |
| 122 | const CIRGenFunctionInfo &fnInfo = |
| 123 | getTypes().arrangeDeviceKernelCallerDeclaration(resultType: ctx.VoidTy, args); |
| 124 | cir::FuncType funcType = getTypes().getFunctionType(info: fnInfo); |
| 125 | |
| 126 | // Retrieve the generated name for the SYCL kernel caller function. |
| 127 | CanQualType kernelNameType = |
| 128 | ctx.getCanonicalType(T: kernelEntryPointAttr->getKernelName()); |
| 129 | const SYCLKernelInfo &kernelInfo = ctx.getSYCLKernelInfo(T: kernelNameType); |
| 130 | |
| 131 | // Synthesized from the OutlinedFunctionDecl, not a FunctionDecl, so create |
| 132 | // the function directly with a null FunctionDecl (mirrors classic CodeGen's |
| 133 | // llvm::Function::Create). |
| 134 | cir::FuncOp funcOp = createCIRFunction( |
| 135 | loc: getLoc(cRange: kernelEntryPointFn->getSourceRange()), name: kernelInfo.GetKernelName(), |
| 136 | funcType, /*funcDecl=*/nullptr); |
| 137 | funcOp.setLinkage(cir::GlobalLinkageKind::ExternalLinkage); |
| 138 | |
| 139 | // Emit as a device kernel (e.g. spir_kernel). Classic CodeGen derives this |
| 140 | // from CC_DeviceKernel via SetLLVMFunctionAttributes; CIR does not yet route |
| 141 | // opFuncCallingConv onto the FuncOp, so set it from the target hook. |
| 142 | funcOp.setCallingConv(getTargetCIRGenInfo().getDeviceKernelCallingConv()); |
| 143 | |
| 144 | CIRGenFunction cgf(*this, builder); |
| 145 | |
| 146 | // Route through the shared attribute path so generic function attributes |
| 147 | // (e.g. convergent) are applied, matching classic CodeGen's |
| 148 | // SetLLVMFunctionAttributes. There is no FunctionDecl, so pass an empty |
| 149 | // GlobalDecl. |
| 150 | setCIRFunctionAttributes(gd: GlobalDecl(), info: fnInfo, func: funcOp, /*isThunk=*/false); |
| 151 | |
| 152 | setSYCLKernelAttributes(cgf, fn: funcOp); |
| 153 | addSYCLModuleIdAttr(fn: funcOp); |
| 154 | |
| 155 | assert(!cir::MissingFeatures::setLLVMFunctionFEnvAttributes()); |
| 156 | |
| 157 | // Emit the SYCL kernel caller function. |
| 158 | llvm::SaveAndRestore<CIRGenFunction *> savedCGF(curCGF, &cgf); |
| 159 | { |
| 160 | mlir::OpBuilder::InsertionGuard guard(builder); |
| 161 | cgf.emitSYCLKernelCaller(outlinedFnDecl, funcOp, funcType, args); |
| 162 | } |
| 163 | |
| 164 | setDSOLocal(static_cast<mlir::Operation *>(funcOp)); |
| 165 | |
| 166 | setNonAliasAttributes(gd: GlobalDecl(), op: funcOp); |
| 167 | setCIRFunctionAttributesForDefinition(decl: outlinedFnDecl, f: funcOp); |
| 168 | } |
| 169 | |