| 1 | //===---- CIRGenBuiltinSPIRV.cpp - Emit CIR for SPIR-V builtins -----------===// |
| 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 to emit SPIR-V Builtin calls. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "CIRGenFunction.h" |
| 14 | |
| 15 | #include "mlir/IR/Value.h" |
| 16 | #include "clang/Basic/TargetBuiltins.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | using namespace clang::CIRGen; |
| 20 | |
| 21 | /// Emit a builtin that takes a dimension index and returns a size_t value, |
| 22 | /// e.g. __builtin_spirv_global_invocation_id(dim). |
| 23 | static mlir::Value emitDimensionQuery(CIRGenFunction &cgf, const CallExpr *expr, |
| 24 | llvm::StringRef intrinsicName) { |
| 25 | mlir::Value dim = cgf.emitScalarExpr(e: expr->getArg(Arg: 0)); |
| 26 | return cgf.getBuilder().emitIntrinsicCallOp( |
| 27 | loc: cgf.getLoc(srcLoc: expr->getExprLoc()), str: intrinsicName, |
| 28 | resTy: cgf.convertType(t: expr->getType()), op: mlir::ValueRange{dim}); |
| 29 | } |
| 30 | |
| 31 | std::optional<mlir::Value> |
| 32 | CIRGenFunction::emitSPIRVBuiltinExpr(unsigned builtinId, const CallExpr *expr) { |
| 33 | auto emitNYI = [&]() -> mlir::Value { |
| 34 | cgm.errorNYI(expr->getSourceRange(), |
| 35 | std::string("unimplemented SPIR-V builtin call: " ) + |
| 36 | getContext().BuiltinInfo.getName(ID: builtinId)); |
| 37 | return mlir::Value{}; |
| 38 | }; |
| 39 | |
| 40 | // Keep the cases in the same order as the classic codegen |
| 41 | // (clang/lib/CodeGen/TargetBuiltins/SPIR.cpp) to ease review. |
| 42 | switch (builtinId) { |
| 43 | case SPIRV::BI__builtin_spirv_distance: |
| 44 | case SPIRV::BI__builtin_spirv_length: |
| 45 | case SPIRV::BI__builtin_spirv_reflect: |
| 46 | case SPIRV::BI__builtin_spirv_refract: |
| 47 | case SPIRV::BI__builtin_spirv_smoothstep: |
| 48 | case SPIRV::BI__builtin_spirv_faceforward: |
| 49 | case SPIRV::BI__builtin_spirv_generic_cast_to_ptr_explicit: |
| 50 | case SPIRV::BI__builtin_spirv_subgroup_shuffle: |
| 51 | return emitNYI(); |
| 52 | case SPIRV::BI__builtin_spirv_num_workgroups: |
| 53 | return emitDimensionQuery(cgf&: *this, expr, intrinsicName: "spv.num.workgroups" ); |
| 54 | case SPIRV::BI__builtin_spirv_workgroup_size: |
| 55 | return emitDimensionQuery(cgf&: *this, expr, intrinsicName: "spv.workgroup.size" ); |
| 56 | case SPIRV::BI__builtin_spirv_workgroup_id: |
| 57 | return emitDimensionQuery(cgf&: *this, expr, intrinsicName: "spv.group.id" ); |
| 58 | case SPIRV::BI__builtin_spirv_local_invocation_id: |
| 59 | return emitDimensionQuery(cgf&: *this, expr, intrinsicName: "spv.thread.id.in.group" ); |
| 60 | case SPIRV::BI__builtin_spirv_global_invocation_id: |
| 61 | return emitDimensionQuery(cgf&: *this, expr, intrinsicName: "spv.thread.id" ); |
| 62 | case SPIRV::BI__builtin_spirv_global_size: |
| 63 | return emitDimensionQuery(cgf&: *this, expr, intrinsicName: "spv.global.size" ); |
| 64 | case SPIRV::BI__builtin_spirv_global_offset: |
| 65 | return emitDimensionQuery(cgf&: *this, expr, intrinsicName: "spv.global.offset" ); |
| 66 | case SPIRV::BI__builtin_spirv_ddx: |
| 67 | case SPIRV::BI__builtin_spirv_ddy: |
| 68 | case SPIRV::BI__builtin_spirv_fwidth: |
| 69 | return emitNYI(); |
| 70 | default: |
| 71 | // Builtins with a ClangBuiltin mapping on the intrinsic (e.g. the subgroup |
| 72 | // queries) are handled by the generic path in emitBuiltinExpr. |
| 73 | return std::nullopt; |
| 74 | } |
| 75 | } |
| 76 | |