Skip to content

Commit 82fca9e

Browse files
Adding asuint implementation to hlsl
1 parent a88a4ed commit 82fca9e

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"
@@ -18820,14 +18816,14 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1882018816
{}, false, true));
1882118817
}
1882218818
case Builtin::BI__builtin_hlsl_elementwise_asuint: {
18823-
Value *Op = EmitScalarExpr(E->getArg(0));
18824-
E->dump();
18819+
Value *Op = EmitScalarExpr(E->getArg(0)->IgnoreImpCasts());
18820+
1882518821
llvm::Type *DestTy = llvm::Type::getInt32Ty(this->getLLVMContext());
1882618822

18827-
if (Op -> getType()->isVectorTy()){
18828-
auto VecTy = E->getArg(0)->getType()->getAs<VectorType>();
18829-
DestTy = llvm::VectorType::get(DestTy, VecTy->getNumElements(),
18830-
VecTy->isSizelessVectorType());
18823+
if (Op->getType()->isVectorTy()) {
18824+
const VectorType *VecTy = E->getArg(0)->getType()->getAs<VectorType>();
18825+
DestTy = llvm::VectorType::get(
18826+
DestTy, ElementCount::getFixed(VecTy->getNumElements()));
1883118827
}
1883218828

1883318829
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"
@@ -28,8 +27,6 @@
2827
#include "llvm/ADT/SmallVector.h"
2928
#include "llvm/ADT/StringExtras.h"
3029
#include "llvm/ADT/StringRef.h"
31-
#include "llvm/IR/DerivedTypes.h"
32-
#include "llvm/IR/Type.h"
3330
#include "llvm/Support/Casting.h"
3431
#include "llvm/Support/DXILABI.h"
3532
#include "llvm/Support/ErrorHandling.h"
@@ -1468,6 +1465,25 @@ bool CheckVectorElementCallArgs(Sema *S, CallExpr *TheCall) {
14681465
return true;
14691466
}
14701467

1468+
bool CheckArgTypeWithoutImplicits(
1469+
Sema *S, Expr *Arg, QualType ExpectedType,
1470+
llvm::function_ref<bool(clang::QualType PassedType)> Check) {
1471+
1472+
QualType ArgTy = Arg->IgnoreImpCasts()->getType();
1473+
1474+
clang::QualType BaseType =
1475+
ArgTy->isVectorType()
1476+
? ArgTy->getAs<clang::VectorType>()->getElementType()
1477+
: ArgTy;
1478+
1479+
if (Check(BaseType)) {
1480+
S->Diag(Arg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1481+
<< ArgTy << ExpectedType << 1 << 0 << 0;
1482+
return true;
1483+
}
1484+
return false;
1485+
}
1486+
14711487
bool CheckArgsTypesAreCorrect(
14721488
Sema *S, CallExpr *TheCall, QualType ExpectedType,
14731489
llvm::function_ref<bool(clang::QualType PassedType)> Check) {
@@ -1494,6 +1510,14 @@ bool CheckAllArgsHaveFloatRepresentation(Sema *S, CallExpr *TheCall) {
14941510
checkAllFloatTypes);
14951511
}
14961512

1513+
bool CheckArgIsFloatOrIntWithoutImplicits(Sema *S, Expr *Arg) {
1514+
auto checkFloat = [](clang::QualType PassedType) -> bool {
1515+
return !PassedType->isFloat32Type() && !PassedType->isIntegerType();
1516+
};
1517+
1518+
return CheckArgTypeWithoutImplicits(S, Arg, S->Context.FloatTy, checkFloat);
1519+
}
1520+
14971521
bool CheckFloatOrHalfRepresentations(Sema *S, CallExpr *TheCall) {
14981522
auto checkFloatorHalf = [](clang::QualType PassedType) -> bool {
14991523
clang::QualType BaseType =
@@ -1652,16 +1676,9 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
16521676
if (SemaRef.checkArgCount(TheCall, 1))
16531677
return true;
16541678

1655-
ExprResult A = TheCall->getArg(0);
1656-
QualType ArgTyA = A.get()->getType();
1657-
1658-
if(ArgTyA->isVectorType()){
1659-
auto VecTy = TheCall->getArg(0)->getType()->getAs<VectorType>();
1660-
auto ReturnType = this->getASTContext().getVectorType(TheCall->getCallReturnType(this->getASTContext()), VecTy->getNumElements(),
1661-
VectorKind::Generic);
1662-
1663-
TheCall->setType(ReturnType);
1664-
}
1679+
Expr *Arg = TheCall->getArg(0);
1680+
if (CheckArgIsFloatOrIntWithoutImplicits(&SemaRef, Arg))
1681+
return true;
16651682

16661683
break;
16671684
}
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)