1 | //===--------- SPIR.cpp - Emit LLVM Code for 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 Builtin calls as LLVM code. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "CGHLSLRuntime.h" |
14 | #include "CodeGenFunction.h" |
15 | #include "clang/Basic/TargetBuiltins.h" |
16 | #include "llvm/IR/Intrinsics.h" |
17 | |
18 | using namespace clang; |
19 | using namespace CodeGen; |
20 | using namespace llvm; |
21 | |
22 | Value *CodeGenFunction::EmitSPIRVBuiltinExpr(unsigned BuiltinID, |
23 | const CallExpr *E) { |
24 | switch (BuiltinID) { |
25 | case SPIRV::BI__builtin_spirv_distance: { |
26 | Value *X = EmitScalarExpr(E: E->getArg(Arg: 0)); |
27 | Value *Y = EmitScalarExpr(E: E->getArg(Arg: 1)); |
28 | assert(E->getArg(0)->getType()->hasFloatingRepresentation() && |
29 | E->getArg(1)->getType()->hasFloatingRepresentation() && |
30 | "Distance operands must have a float representation" ); |
31 | assert(E->getArg(0)->getType()->isVectorType() && |
32 | E->getArg(1)->getType()->isVectorType() && |
33 | "Distance operands must be a vector" ); |
34 | return Builder.CreateIntrinsic( |
35 | /*ReturnType=*/RetTy: X->getType()->getScalarType(), ID: Intrinsic::spv_distance, |
36 | Args: ArrayRef<Value *>{X, Y}, FMFSource: nullptr, Name: "spv.distance" ); |
37 | } |
38 | case SPIRV::BI__builtin_spirv_length: { |
39 | Value *X = EmitScalarExpr(E: E->getArg(Arg: 0)); |
40 | assert(E->getArg(0)->getType()->hasFloatingRepresentation() && |
41 | "length operand must have a float representation" ); |
42 | assert(E->getArg(0)->getType()->isVectorType() && |
43 | "length operand must be a vector" ); |
44 | return Builder.CreateIntrinsic( |
45 | /*ReturnType=*/RetTy: X->getType()->getScalarType(), ID: Intrinsic::spv_length, |
46 | Args: ArrayRef<Value *>{X}, FMFSource: nullptr, Name: "spv.length" ); |
47 | } |
48 | case SPIRV::BI__builtin_spirv_reflect: { |
49 | Value *I = EmitScalarExpr(E: E->getArg(Arg: 0)); |
50 | Value *N = EmitScalarExpr(E: E->getArg(Arg: 1)); |
51 | assert(E->getArg(0)->getType()->hasFloatingRepresentation() && |
52 | E->getArg(1)->getType()->hasFloatingRepresentation() && |
53 | "Reflect operands must have a float representation" ); |
54 | assert(E->getArg(0)->getType()->isVectorType() && |
55 | E->getArg(1)->getType()->isVectorType() && |
56 | "Reflect operands must be a vector" ); |
57 | return Builder.CreateIntrinsic( |
58 | /*ReturnType=*/RetTy: I->getType(), ID: Intrinsic::spv_reflect, |
59 | Args: ArrayRef<Value *>{I, N}, FMFSource: nullptr, Name: "spv.reflect" ); |
60 | } |
61 | case SPIRV::BI__builtin_spirv_smoothstep: { |
62 | Value *Min = EmitScalarExpr(E: E->getArg(Arg: 0)); |
63 | Value *Max = EmitScalarExpr(E: E->getArg(Arg: 1)); |
64 | Value *X = EmitScalarExpr(E: E->getArg(Arg: 2)); |
65 | assert(E->getArg(0)->getType()->hasFloatingRepresentation() && |
66 | E->getArg(1)->getType()->hasFloatingRepresentation() && |
67 | E->getArg(2)->getType()->hasFloatingRepresentation() && |
68 | "SmoothStep operands must have a float representation" ); |
69 | return Builder.CreateIntrinsic( |
70 | /*ReturnType=*/RetTy: Min->getType(), ID: Intrinsic::spv_smoothstep, |
71 | Args: ArrayRef<Value *>{Min, Max, X}, /*FMFSource=*/nullptr, |
72 | Name: "spv.smoothstep" ); |
73 | } |
74 | case SPIRV::BI__builtin_spirv_faceforward: { |
75 | Value *N = EmitScalarExpr(E: E->getArg(Arg: 0)); |
76 | Value *I = EmitScalarExpr(E: E->getArg(Arg: 1)); |
77 | Value *Ng = EmitScalarExpr(E: E->getArg(Arg: 2)); |
78 | assert(E->getArg(0)->getType()->hasFloatingRepresentation() && |
79 | E->getArg(1)->getType()->hasFloatingRepresentation() && |
80 | E->getArg(2)->getType()->hasFloatingRepresentation() && |
81 | "FaceForward operands must have a float representation" ); |
82 | return Builder.CreateIntrinsic( |
83 | /*ReturnType=*/RetTy: N->getType(), ID: Intrinsic::spv_faceforward, |
84 | Args: ArrayRef<Value *>{N, I, Ng}, /*FMFSource=*/nullptr, Name: "spv.faceforward" ); |
85 | } |
86 | case SPIRV::BI__builtin_spirv_generic_cast_to_ptr_explicit: { |
87 | Value *Ptr = EmitScalarExpr(E: E->getArg(Arg: 0)); |
88 | assert(E->getArg(0)->getType()->hasPointerRepresentation() && |
89 | E->getArg(1)->getType()->hasIntegerRepresentation() && |
90 | "GenericCastToPtrExplicit takes a pointer and an int" ); |
91 | llvm::Type *Res = getTypes().ConvertType(T: E->getType()); |
92 | assert(Res->isPointerTy() && |
93 | "GenericCastToPtrExplicit doesn't return a pointer" ); |
94 | llvm::CallInst *Call = Builder.CreateIntrinsic( |
95 | /*ReturnType=*/RetTy: Res, ID: Intrinsic::spv_generic_cast_to_ptr_explicit, |
96 | Args: ArrayRef<Value *>{Ptr}, FMFSource: nullptr, Name: "spv.generic_cast" ); |
97 | Call->addRetAttr(Kind: llvm::Attribute::AttrKind::NoUndef); |
98 | return Call; |
99 | } |
100 | } |
101 | return nullptr; |
102 | } |
103 | |