Skip to content

Commit d9fa028

Browse files
changing implementation of asuint to use bit_cast
1 parent 16a0216 commit d9fa028

File tree

8 files changed

+72
-89
lines changed

8 files changed

+72
-89
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4763,12 +4763,6 @@ def HLSLSaturate : LangBuiltin<"HLSL_LANG"> {
47634763
let Prototype = "void(...)";
47644764
}
47654765

4766-
def HLSLBitCast32 : LangBuiltin<"HLSL_LANG"> {
4767-
let Spellings = ["__builtin_hlsl_bit_cast_32"];
4768-
let Attributes = [NoThrow, Const];
4769-
let Prototype = "void(...)";
4770-
}
4771-
47724766
def HLSLSelect : LangBuiltin<"HLSL_LANG"> {
47734767
let Spellings = ["__builtin_hlsl_select"];
47744768
let Attributes = [NoThrow, Const];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18870,25 +18870,6 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1887018870
Intrinsic::ID ID = CGM.getHLSLRuntime().getWaveIsFirstLaneIntrinsic();
1887118871
return EmitRuntimeCall(Intrinsic::getDeclaration(&CGM.getModule(), ID));
1887218872
}
18873-
case Builtin::BI__builtin_hlsl_bit_cast_32: {
18874-
Value *Op = EmitScalarExpr(E->getArg(0));
18875-
18876-
llvm::Type *DestTy = ConvertType(E->getCallReturnType(getContext()));
18877-
18878-
if (DestTy->isVectorTy()) {
18879-
const VectorType *VecTy =
18880-
E->getCallReturnType(getContext())->getAs<VectorType>();
18881-
DestTy = ConvertType(VecTy->getElementType());
18882-
}
18883-
18884-
if (Op->getType()->isVectorTy()) {
18885-
const VectorType *VecTy = E->getArg(0)->getType()->getAs<VectorType>();
18886-
DestTy = llvm::VectorType::get(
18887-
DestTy, ElementCount::getFixed(VecTy->getNumElements()));
18888-
}
18889-
18890-
return Builder.CreateBitCast(Op, DestTy);
18891-
}
1889218873
case Builtin::BI__builtin_hlsl_elementwise_sign: {
1889318874
Value *Op0 = EmitScalarExpr(E->getArg(0));
1889418875
llvm::Type *Xty = Op0->getType();

clang/lib/Headers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ set(hlsl_h
8787
set(hlsl_subdir_files
8888
hlsl/hlsl_basic_types.h
8989
hlsl/hlsl_intrinsics.h
90+
hlsl/hlsl_details.h
9091
)
9192
set(hlsl_files
9293
${hlsl_h}

clang/lib/Headers/hlsl/hlsl_details.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===----- hlsl_intrinsics.h - HLSL definitions for intrinsics ----------===//
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+
10+
#ifndef _HLSL_HLSL_DETAILS_H_
11+
#define _HLSL_HLSL_DETAILS_H_
12+
13+
namespace __details {
14+
#define HLSL_BUILTIN_ATTRIBUTES\
15+
__attribute__((__always_inline__, __nodebug__)) static inline
16+
17+
template<bool B, typename T>
18+
struct enable_if { };
19+
20+
template<typename T>
21+
struct enable_if<true, T> {
22+
using Type = T;
23+
};
24+
25+
template<bool B, typename T=void>
26+
using enable_if_t = typename enable_if<B,T>::Type;
27+
28+
template<typename U, typename T, int N>
29+
HLSL_BUILTIN_ATTRIBUTES vector<U, N> bit_cast(vector<T, N> V) {
30+
_Static_assert(sizeof(U) == sizeof(T), "casting types must be same bit size.");
31+
return __builtin_bit_cast(vector<U, N>, V);
32+
}
33+
34+
template<typename U, typename T>
35+
HLSL_BUILTIN_ATTRIBUTES U bit_cast(T F) {
36+
_Static_assert(sizeof(U) == sizeof(T), "casting types must be same bit size.");
37+
return __builtin_bit_cast(U, F);
38+
}
39+
40+
} // namespace hlsl
41+
42+
#endif //_HLSL_HLSL_DETAILS_H_

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define _HLSL_HLSL_INTRINSICS_H_
1111

1212
namespace hlsl {
13+
#include "hlsl_details.h"
1314

1415
// Note: Functions in this file are sorted alphabetically, then grouped by base
1516
// element type, and the element types are sorted by size, then singed integer,
@@ -395,32 +396,16 @@ float4 asin(float4);
395396
/// \brief Interprets the bit pattern of x as an unsigned integer.
396397
/// \param Val The input value.
397398

398-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
399-
uint asuint(int);
400-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
401-
uint2 asuint(int2);
402-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
403-
uint3 asuint(int3);
404-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
405-
uint4 asuint(int4);
406-
407-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
408-
uint asuint(uint);
409-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
410-
uint2 asuint(uint2);
411-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
412-
uint3 asuint(uint3);
413-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
414-
uint4 asuint(uint4);
415-
416-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
417-
uint asuint(float);
418-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
419-
uint2 asuint(float2);
420-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
421-
uint3 asuint(float3);
422-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_bit_cast_32)
423-
uint4 asuint(float4);
399+
template <typename T, int N>
400+
HLSL_BUILTIN_ATTRIBUTES vector<uint, N>
401+
asuint(vector<T, N> V) {
402+
return __details::bit_cast<uint, T, N>(V);
403+
}
404+
405+
template <typename T>
406+
HLSL_BUILTIN_ATTRIBUTES uint asuint(T F) {
407+
return __details::bit_cast<uint, T>(F);
408+
}
424409

425410
//===----------------------------------------------------------------------===//
426411
// atan builtins

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,18 +1497,6 @@ bool CheckAllArgsHaveFloatRepresentation(Sema *S, CallExpr *TheCall) {
14971497
checkAllFloatTypes);
14981498
}
14991499

1500-
bool CheckNotFloatAndInt(Sema *S, CallExpr *TheCall) {
1501-
auto checkFloat = [](clang::QualType PassedType) -> bool {
1502-
clang::QualType BaseType =
1503-
PassedType->isVectorType()
1504-
? PassedType->getAs<clang::VectorType>()->getElementType()
1505-
: PassedType;
1506-
return !(BaseType->isFloat32Type() || BaseType->isIntegerType());
1507-
};
1508-
1509-
return CheckArgsTypesAreCorrect(S, TheCall, S->Context.FloatTy, checkFloat);
1510-
}
1511-
15121500
bool CheckFloatOrHalfRepresentations(Sema *S, CallExpr *TheCall) {
15131501
auto checkFloatorHalf = [](clang::QualType PassedType) -> bool {
15141502
clang::QualType BaseType =
@@ -1766,14 +1754,6 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
17661754
return true;
17671755
break;
17681756
}
1769-
case Builtin::BI__builtin_hlsl_bit_cast_32: {
1770-
if (SemaRef.checkArgCount(TheCall, 1))
1771-
return true;
1772-
1773-
if (CheckNotFloatAndInt(&SemaRef, TheCall))
1774-
return true;
1775-
break;
1776-
}
17771757
case Builtin::BI__builtin_elementwise_acos:
17781758
case Builtin::BI__builtin_elementwise_asin:
17791759
case Builtin::BI__builtin_elementwise_atan:
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+
uint4 test_asuint_too_many_arg(float p0, float p1) {
5+
return asuint(p0, p1);
6+
// expected-error@-1 {{no matching function for call to 'asuint'}}
7+
}
8+
9+
uint test_asuint_double(double p1) {
10+
return asuint(p1);
11+
// expected-error@-1{clang/20/include/hlsl/hlsl_details.h} {{static assertion failed due to requirement 'sizeof(unsigned int) == sizeof(double)'}}
12+
}
13+
14+
15+
uint test_asuint_half(half p1) {
16+
return asuint(p1);
17+
// expected-error@-1{clang/20/include/hlsl/hlsl_details.h} {{static assertion failed due to requirement 'sizeof(unsigned int) == sizeof(half)'}}
18+
}

clang/test/SemaHLSL/BuiltIns/bit-cast-32-errors.hlsl

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)