Skip to content

Commit 55a6cad

Browse files
Adding asuint implementation to hlsl
1 parent c6434c0 commit 55a6cad

File tree

5 files changed

+86
-81
lines changed

5 files changed

+86
-81
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@
2727
#include "clang/AST/Decl.h"
2828
#include "clang/AST/OSLog.h"
2929
#include "clang/AST/OperationKinds.h"
30-
#include "clang/Basic/Builtins.h"
3130
#include "clang/Basic/TargetBuiltins.h"
3231
#include "clang/Basic/TargetInfo.h"
3332
#include "clang/Basic/TargetOptions.h"
34-
#include "clang/Basic/TokenKinds.h"
3533
#include "clang/CodeGen/CGFunctionInfo.h"
3634
#include "clang/Frontend/FrontendDiagnostic.h"
3735
#include "llvm/ADT/APFloat.h"
@@ -41,7 +39,6 @@
4139
#include "llvm/ADT/StringExtras.h"
4240
#include "llvm/Analysis/ValueTracking.h"
4341
#include "llvm/IR/DataLayout.h"
44-
#include "llvm/IR/DerivedTypes.h"
4542
#include "llvm/IR/InlineAsm.h"
4643
#include "llvm/IR/Intrinsics.h"
4744
#include "llvm/IR/IntrinsicsAArch64.h"
@@ -65,7 +62,6 @@
6562
#include "llvm/Support/ConvertUTF.h"
6663
#include "llvm/Support/MathExtras.h"
6764
#include "llvm/Support/ScopedPrinter.h"
68-
#include "llvm/Support/raw_ostream.h"
6965
#include "llvm/TargetParser/AArch64TargetParser.h"
7066
#include "llvm/TargetParser/RISCVISAInfo.h"
7167
#include "llvm/TargetParser/X86TargetParser.h"
@@ -18871,14 +18867,14 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1887118867
{}, false, true));
1887218868
}
1887318869
case Builtin::BI__builtin_hlsl_elementwise_asuint: {
18874-
Value *Op = EmitScalarExpr(E->getArg(0));
18875-
E->dump();
18870+
Value *Op = EmitScalarExpr(E->getArg(0)->IgnoreImpCasts());
18871+
1887618872
llvm::Type *DestTy = llvm::Type::getInt32Ty(this->getLLVMContext());
1887718873

18878-
if (Op -> getType()->isVectorTy()){
18879-
auto VecTy = E->getArg(0)->getType()->getAs<VectorType>();
18880-
DestTy = llvm::VectorType::get(DestTy, VecTy->getNumElements(),
18881-
VecTy->isSizelessVectorType());
18874+
if (Op->getType()->isVectorTy()) {
18875+
const VectorType *VecTy = E->getArg(0)->getType()->getAs<VectorType>();
18876+
DestTy = llvm::VectorType::get(
18877+
DestTy, ElementCount::getFixed(VecTy->getNumElements()));
1888218878
}
1888318879

1888418880
return Builder.CreateBitCast(Op, DestTy);

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,17 @@ bool any(double4);
367367
/// \brief Returns the arcsine of the input value, \a Val.
368368
/// \param Val The input value.
369369

370+
#ifdef __HLSL_ENABLE_16_BIT
371+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_asin)
372+
half asin(half);
373+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_asin)
374+
half2 asin(half2);
375+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_asin)
376+
half3 asin(half3);
377+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_asin)
378+
half4 asin(half4);
379+
#endif
380+
370381
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_asin)
371382
float asin(float);
372383
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_asin)
@@ -377,11 +388,11 @@ _HLSL_BUILTIN_ALIAS(__builtin_elementwise_asin)
377388
float4 asin(float4);
378389

379390
//===----------------------------------------------------------------------===//
380-
// asin builtins
391+
// asuint builtins
381392
//===----------------------------------------------------------------------===//
382393

383-
/// \fn uint asin(T Val)
384-
/// \brief Reinterprest.
394+
/// \fn uint asuint(T Val)
395+
/// \brief Interprets the bit pattern of x as an unsigned integer.
385396
/// \param Val The input value.
386397

387398
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
@@ -393,16 +404,6 @@ uint3 asuint(float3);
393404
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
394405
uint4 asuint(float4);
395406

396-
397-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
398-
uint asuint(double);
399-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
400-
uint2 asuint(double2);
401-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
402-
uint3 asuint(double3);
403-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
404-
uint4 asuint(double4);
405-
406407
//===----------------------------------------------------------------------===//
407408
// atan builtins
408409
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "clang/Sema/SemaHLSL.h"
12-
#include "clang/AST/ASTContext.h"
1312
#include "clang/AST/Decl.h"
1413
#include "clang/AST/DeclBase.h"
1514
#include "clang/AST/DeclCXX.h"
@@ -29,8 +28,6 @@
2928
#include "llvm/ADT/SmallVector.h"
3029
#include "llvm/ADT/StringExtras.h"
3130
#include "llvm/ADT/StringRef.h"
32-
#include "llvm/IR/DerivedTypes.h"
33-
#include "llvm/IR/Type.h"
3431
#include "llvm/Support/Casting.h"
3532
#include "llvm/Support/DXILABI.h"
3633
#include "llvm/Support/ErrorHandling.h"
@@ -1473,6 +1470,25 @@ bool CheckVectorElementCallArgs(Sema *S, CallExpr *TheCall) {
14731470
return true;
14741471
}
14751472

1473+
bool CheckArgTypeWithoutImplicits(
1474+
Sema *S, Expr *Arg, QualType ExpectedType,
1475+
llvm::function_ref<bool(clang::QualType PassedType)> Check) {
1476+
1477+
QualType ArgTy = Arg->IgnoreImpCasts()->getType();
1478+
1479+
clang::QualType BaseType =
1480+
ArgTy->isVectorType()
1481+
? ArgTy->getAs<clang::VectorType>()->getElementType()
1482+
: ArgTy;
1483+
1484+
if (Check(BaseType)) {
1485+
S->Diag(Arg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1486+
<< ArgTy << ExpectedType << 1 << 0 << 0;
1487+
return true;
1488+
}
1489+
return false;
1490+
}
1491+
14761492
bool CheckArgsTypesAreCorrect(
14771493
Sema *S, CallExpr *TheCall, QualType ExpectedType,
14781494
llvm::function_ref<bool(clang::QualType PassedType)> Check) {
@@ -1499,6 +1515,14 @@ bool CheckAllArgsHaveFloatRepresentation(Sema *S, CallExpr *TheCall) {
14991515
checkAllFloatTypes);
15001516
}
15011517

1518+
bool CheckArgIsFloatOrIntWithoutImplicits(Sema *S, Expr *Arg) {
1519+
auto checkFloat = [](clang::QualType PassedType) -> bool {
1520+
return !PassedType->isFloat32Type() && !PassedType->isIntegerType();
1521+
};
1522+
1523+
return CheckArgTypeWithoutImplicits(S, Arg, S->Context.FloatTy, checkFloat);
1524+
}
1525+
15021526
bool CheckFloatOrHalfRepresentations(Sema *S, CallExpr *TheCall) {
15031527
auto checkFloatorHalf = [](clang::QualType PassedType) -> bool {
15041528
clang::QualType BaseType =
@@ -1760,16 +1784,9 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
17601784
if (SemaRef.checkArgCount(TheCall, 1))
17611785
return true;
17621786

1763-
ExprResult A = TheCall->getArg(0);
1764-
QualType ArgTyA = A.get()->getType();
1765-
1766-
if(ArgTyA->isVectorType()){
1767-
auto VecTy = TheCall->getArg(0)->getType()->getAs<VectorType>();
1768-
auto ReturnType = this->getASTContext().getVectorType(TheCall->getCallReturnType(this->getASTContext()), VecTy->getNumElements(),
1769-
VectorKind::Generic);
1770-
1771-
TheCall->setType(ReturnType);
1772-
}
1787+
Expr *Arg = TheCall->getArg(0);
1788+
if (CheckArgIsFloatOrIntWithoutImplicits(&SemaRef, Arg))
1789+
return true;
17731790

17741791
break;
17751792
}
Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,26 @@
11
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -o - | FileCheck %s
22

3-
// // CHECK-LABEL: builtin_test_asuint_float
4-
// // CHECK: bitcast float %0 to i32
5-
// // CHECK: ret <4 x i32> %dx.clamp
6-
// export uint builtin_test_asuint_float(float p0) {
7-
// return __builtin_hlsl_elementwise_asuint(p0);
8-
// }
93

10-
11-
// // CHECK-LABEL: builtin_test_asuint_float
12-
// // CHECK: bitcast float %0 to i32
13-
// // CHECK: ret <4 x i32> %dx.clamp
14-
// export uint builtin_test_asuint_double(double p0) {
15-
// return __builtin_hlsl_elementwise_asuint(p0);
16-
// }
17-
18-
19-
// // CHECK-LABEL: builtin_test_asuint_float
20-
// // CHECK: bitcast float %0 to i32
21-
// // CHECK: ret <4 x i32> %dx.clamp
22-
// export uint builtin_test_asuint_half(half p0) {
23-
// return __builtin_hlsl_elementwise_asuint(p0);
24-
// }
25-
26-
27-
// // CHECK-LABEL: builtin_test_asuint_float
28-
// // CHECK: bitcast float %0 to i32
29-
// // CHECK: ret <4 x i32> %dx.clamp
30-
// export uint4 builtin_test_asuint_float_vector(float p0) {
31-
// return __builtin_hlsl_elementwise_asuint(p0);
32-
// }
33-
34-
35-
// CHECK-LABEL: builtin_test_asuint_float
36-
// CHECK: bitcast float %0 to i32
37-
// CHECK: ret <4 x i32> %dx.clamp
38-
export uint4 builtin_test_asuint_floa4t(float p0) {
4+
// CHECK-LABEL: test_asuint4_uint
5+
// CHECK: ret i32 %0
6+
export uint test_asuint4_uint(uint p0) {
397
return asuint(p0);
408
}
419

42-
// export uint4 builtin_test_asuint4_uint(uint p0) {
43-
// return __builtin_hlsl_elementwise_asuint(p0);
44-
// }
45-
10+
// CHECK-LABEL: test_asuint4_int
11+
// CHECK: %splat.splatinsert = insertelement <4 x i32> poison, i32 %0, i64 0
12+
export uint4 test_asuint4_int(int p0) {
13+
return asuint(p0);
14+
}
4615

47-
// export uint4 builtin_test_asuint4_int(int p0) {
48-
// return __builtin_hlsl_elementwise_asuint(p0);
49-
// }
16+
// CHECK-LABEL: test_asuint_float
17+
// CHECK: %1 = bitcast float %0 to i32
18+
export uint test_asuint_float(float p0) {
19+
return asuint(p0);
20+
}
5021

51-
// export uint builtin_test_asuint_float(float p0) {
52-
// return __builtin_hlsl_elementwise_asuint(p0);
53-
// }
22+
// CHECK-LABEL: test_asuint_float
23+
// CHECK: %1 = bitcast <4 x float> %0 to <4 x i32>
24+
export uint4 test_asuint_float4(float4 p0) {
25+
return asuint(p0);
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected
2+
3+
4+
export uint4 test_asuint_too_many_arg(float p0, float p1) {
5+
return __builtin_hlsl_elementwise_asuint(p0, p1);
6+
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
7+
}
8+
9+
10+
export uint fn(double p1) {
11+
return asuint(p1);
12+
// expected-error@-1 {{passing 'double' to parameter of incompatible type 'float'}}
13+
}
14+
15+
export uint fn(half p1) {
16+
return asuint(p1);
17+
// expected-error@-1 {{passing 'half' to parameter of incompatible type 'float'}}
18+
}

0 commit comments

Comments
 (0)