Skip to content

Commit c098435

Browse files
authored
Add cross builtins and cross HLSL function to DirectX and SPIR-V backend (#109180)
This PR adds the step intrinsic and an HLSL function that uses it. The SPIRV backend is also implemented. Used #106471 as a reference. Fixes #99095
1 parent 1b4b0c4 commit c098435

File tree

15 files changed

+299
-5
lines changed

15 files changed

+299
-5
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4739,6 +4739,12 @@ def HLSLClamp : LangBuiltin<"HLSL_LANG"> {
47394739
let Prototype = "void(...)";
47404740
}
47414741

4742+
def HLSLCross: LangBuiltin<"HLSL_LANG"> {
4743+
let Spellings = ["__builtin_hlsl_cross"];
4744+
let Attributes = [NoThrow, Const];
4745+
let Prototype = "void(...)";
4746+
}
4747+
47424748
def HLSLDotProduct : LangBuiltin<"HLSL_LANG"> {
47434749
let Spellings = ["__builtin_hlsl_dot"];
47444750
let Attributes = [NoThrow, Const];

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10510,8 +10510,8 @@ def err_first_argument_to_cwsc_pdtor_call : Error<
1051010510
def err_second_argument_to_cwsc_not_pointer : Error<
1051110511
"second argument to __builtin_call_with_static_chain must be of pointer type">;
1051210512

10513-
def err_vector_incorrect_num_initializers : Error<
10514-
"%select{too many|too few}0 elements in vector initialization (expected %1 elements, have %2)">;
10513+
def err_vector_incorrect_num_elements : Error<
10514+
"%select{too many|too few}0 elements in vector %select{initialization|operand}3 (expected %1 elements, have %2)">;
1051510515
def err_altivec_empty_initializer : Error<"expected initializer">;
1051610516

1051710517
def err_invalid_neon_type_code : Error<

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18667,6 +18667,21 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1866718667
IsUnsigned ? Intrinsic::dx_uclamp : Intrinsic::dx_clamp,
1866818668
ArrayRef<Value *>{OpX, OpMin, OpMax}, nullptr, "dx.clamp");
1866918669
}
18670+
case Builtin::BI__builtin_hlsl_cross: {
18671+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18672+
Value *Op1 = EmitScalarExpr(E->getArg(1));
18673+
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
18674+
E->getArg(1)->getType()->hasFloatingRepresentation() &&
18675+
"cross operands must have a float representation");
18676+
// make sure each vector has exactly 3 elements
18677+
auto *XVecTy1 = E->getArg(0)->getType()->getAs<VectorType>();
18678+
auto *XVecTy2 = E->getArg(1)->getType()->getAs<VectorType>();
18679+
assert(XVecTy1->getNumElements() == 3 && XVecTy2->getNumElements() == 3 &&
18680+
"input vectors must have 3 elements each");
18681+
return Builder.CreateIntrinsic(
18682+
/*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getCrossIntrinsic(),
18683+
ArrayRef<Value *>{Op0, Op1}, nullptr, "hlsl.cross");
18684+
}
1867018685
case Builtin::BI__builtin_hlsl_dot: {
1867118686
Value *Op0 = EmitScalarExpr(E->getArg(0));
1867218687
Value *Op1 = EmitScalarExpr(E->getArg(1));

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class CGHLSLRuntime {
7474

7575
GENERATE_HLSL_INTRINSIC_FUNCTION(All, all)
7676
GENERATE_HLSL_INTRINSIC_FUNCTION(Any, any)
77+
GENERATE_HLSL_INTRINSIC_FUNCTION(Cross, cross)
7778
GENERATE_HLSL_INTRINSIC_FUNCTION(Frac, frac)
7879
GENERATE_HLSL_INTRINSIC_FUNCTION(Length, length)
7980
GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,28 @@ uint64_t3 reversebits(uint64_t3);
16421642
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_bitreverse)
16431643
uint64_t4 reversebits(uint64_t4);
16441644

1645+
//===----------------------------------------------------------------------===//
1646+
// cross builtins
1647+
//===----------------------------------------------------------------------===//
1648+
1649+
/// \fn T cross(T x, T y)
1650+
/// \brief Returns the cross product of two floating-point, 3D vectors.
1651+
/// \param x [in] The first floating-point, 3D vector.
1652+
/// \param y [in] The second floating-point, 3D vector.
1653+
///
1654+
/// Result is the cross product of x and y, i.e., the resulting
1655+
/// components are, in order :
1656+
/// x[1] * y[2] - y[1] * x[2]
1657+
/// x[2] * y[0] - y[2] * x[0]
1658+
/// x[0] * y[1] - y[0] * x[1]
1659+
1660+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1661+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_cross)
1662+
half3 cross(half3, half3);
1663+
1664+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_cross)
1665+
float3 cross(float3, float3);
1666+
16451667
//===----------------------------------------------------------------------===//
16461668
// rcp builtins
16471669
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,41 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
18281828
return true;
18291829
break;
18301830
}
1831+
case Builtin::BI__builtin_hlsl_cross: {
1832+
if (SemaRef.checkArgCount(TheCall, 2))
1833+
return true;
1834+
if (CheckVectorElementCallArgs(&SemaRef, TheCall))
1835+
return true;
1836+
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
1837+
return true;
1838+
// ensure both args have 3 elements
1839+
int NumElementsArg1 =
1840+
TheCall->getArg(0)->getType()->getAs<VectorType>()->getNumElements();
1841+
int NumElementsArg2 =
1842+
TheCall->getArg(1)->getType()->getAs<VectorType>()->getNumElements();
1843+
1844+
if (NumElementsArg1 != 3) {
1845+
int LessOrMore = NumElementsArg1 > 3 ? 1 : 0;
1846+
SemaRef.Diag(TheCall->getBeginLoc(),
1847+
diag::err_vector_incorrect_num_elements)
1848+
<< LessOrMore << 3 << NumElementsArg1 << /*operand*/ 1;
1849+
return true;
1850+
}
1851+
if (NumElementsArg2 != 3) {
1852+
int LessOrMore = NumElementsArg2 > 3 ? 1 : 0;
1853+
1854+
SemaRef.Diag(TheCall->getBeginLoc(),
1855+
diag::err_vector_incorrect_num_elements)
1856+
<< LessOrMore << 3 << NumElementsArg2 << /*operand*/ 1;
1857+
return true;
1858+
}
1859+
1860+
ExprResult A = TheCall->getArg(0);
1861+
QualType ArgTyA = A.get()->getType();
1862+
// return type is the same as the input type
1863+
TheCall->setType(ArgTyA);
1864+
break;
1865+
}
18311866
case Builtin::BI__builtin_hlsl_dot: {
18321867
if (SemaRef.checkArgCount(TheCall, 2))
18331868
return true;

clang/lib/Sema/SemaInit.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,8 +1976,9 @@ void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
19761976
if (numEltsInit != maxElements) {
19771977
if (!VerifyOnly)
19781978
SemaRef.Diag(IList->getBeginLoc(),
1979-
diag::err_vector_incorrect_num_initializers)
1980-
<< (numEltsInit < maxElements) << maxElements << numEltsInit;
1979+
diag::err_vector_incorrect_num_elements)
1980+
<< (numEltsInit < maxElements) << maxElements << numEltsInit
1981+
<< /*initialization*/ 0;
19811982
hadError = true;
19821983
}
19831984
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
3+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
4+
// RUN: --check-prefixes=CHECK,NATIVE_HALF \
5+
// RUN: -DFNATTRS=noundef -DTARGET=dx
6+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
7+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
8+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF \
9+
// RUN: -DFNATTRS=noundef -DTARGET=dx
10+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
11+
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
12+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
13+
// RUN: --check-prefixes=CHECK,NATIVE_HALF \
14+
// RUN: -DFNATTRS="spir_func noundef" -DTARGET=spv
15+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
16+
// RUN: spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
17+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF \
18+
// RUN: -DFNATTRS="spir_func noundef" -DTARGET=spv
19+
20+
// NATIVE_HALF: define [[FNATTRS]] <3 x half> @
21+
// NATIVE_HALF: call <3 x half> @llvm.[[TARGET]].cross.v3f16(<3 x half>
22+
// NATIVE_HALF: ret <3 x half> %hlsl.cross
23+
// NO_HALF: define [[FNATTRS]] <3 x float> @
24+
// NO_HALF: call <3 x float> @llvm.[[TARGET]].cross.v3f32(<3 x float>
25+
// NO_HALF: ret <3 x float> %hlsl.cross
26+
half3 test_cross_half3(half3 p0, half3 p1)
27+
{
28+
return cross(p0, p1);
29+
}
30+
31+
// CHECK: define [[FNATTRS]] <3 x float> @
32+
// CHECK: %hlsl.cross = call <3 x float> @llvm.[[TARGET]].cross.v3f32(
33+
// CHECK: ret <3 x float> %hlsl.cross
34+
float3 test_cross_float3(float3 p0, float3 p1)
35+
{
36+
return cross(p0, p1);
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -disable-llvm-passes -verify
2+
3+
void test_too_few_arg()
4+
{
5+
return __builtin_hlsl_cross();
6+
// expected-error@-1 {{too few arguments to function call, expected 2, have 0}}
7+
}
8+
9+
void test_too_many_arg(float3 p0)
10+
{
11+
return __builtin_hlsl_cross(p0, p0, p0);
12+
// expected-error@-1 {{too many arguments to function call, expected 2, have 3}}
13+
}
14+
15+
bool builtin_bool_to_float_type_promotion(bool p1)
16+
{
17+
return __builtin_hlsl_cross(p1, p1);
18+
// expected-error@-1 {{passing 'bool' to parameter of incompatible type 'float'}}
19+
}
20+
21+
bool builtin_cross_int_to_float_promotion(int p1)
22+
{
23+
return __builtin_hlsl_cross(p1, p1);
24+
// expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}}
25+
}
26+
27+
bool2 builtin_cross_int2_to_float2_promotion(int2 p1)
28+
{
29+
return __builtin_hlsl_cross(p1, p1);
30+
// expected-error@-1 {{passing 'int2' (aka 'vector<int, 2>') to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(float)))) float' (vector of 2 'float' values)}}
31+
}
32+
33+
float2 builtin_cross_float2(float2 p1, float2 p2)
34+
{
35+
return __builtin_hlsl_cross(p1, p2);
36+
// expected-error@-1 {{too many elements in vector operand (expected 3 elements, have 2)}}
37+
}
38+
39+
float3 builtin_cross_float3_int3(float3 p1, int3 p2)
40+
{
41+
return __builtin_hlsl_cross(p1, p2);
42+
// expected-error@-1 {{all arguments to '__builtin_hlsl_cross' must have the same type}}
43+
}

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def int_dx_all : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty], [IntrNoMem]>
4242
def int_dx_any : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty], [IntrNoMem]>;
4343
def int_dx_clamp : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem]>;
4444
def int_dx_uclamp : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem]>;
45+
def int_dx_cross : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem]>;
4546
def int_dx_saturate : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
4647

4748
def int_dx_dot2 :

0 commit comments

Comments
 (0)