Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,40 @@ uint64_t3 reversebits(uint64_t3);
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_bitreverse)
uint64_t4 reversebits(uint64_t4);

//===----------------------------------------------------------------------===//
// round builtins
//===----------------------------------------------------------------------===//

/// \fn T round(T x)
/// \brief Rounds the specified value \a x to the nearest integer.
/// \param x The specified input value.
///
/// The return value is the \a x parameter, rounded to the nearest integer
/// within a floating-point type. Halfway cases are
/// rounded to the nearest even value.

_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
half round(half);
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
half2 round(half2);
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
half3 round(half3);
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
half4 round(half4);

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
float round(float);
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
float2 round(float2);
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
float3 round(float3);
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_round)
float4 round(float4);

//===----------------------------------------------------------------------===//
// sin builtins
//===----------------------------------------------------------------------===//
Expand Down
53 changes: 53 additions & 0 deletions clang/test/CodeGenHLSL/builtins/round.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
// RUN: --check-prefixes=CHECK,NATIVE_HALF
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF

// NATIVE_HALF: define noundef half @
// NATIVE_HALF: %elt.round = call half @llvm.round.f16(
// NATIVE_HALF: ret half %elt.round
// NO_HALF: define noundef float @"?test_round_half@@YA$halff@$halff@@Z"(
// NO_HALF: %elt.round = call float @llvm.round.f32(
// NO_HALF: ret float %elt.round
half test_round_half(half p0) { return round(p0); }
// NATIVE_HALF: define noundef <2 x half> @
// NATIVE_HALF: %elt.round = call <2 x half> @llvm.round.v2f16
// NATIVE_HALF: ret <2 x half> %elt.round
// NO_HALF: define noundef <2 x float> @
// NO_HALF: %elt.round = call <2 x float> @llvm.round.v2f32(
// NO_HALF: ret <2 x float> %elt.round
half2 test_round_half2(half2 p0) { return round(p0); }
// NATIVE_HALF: define noundef <3 x half> @
// NATIVE_HALF: %elt.round = call <3 x half> @llvm.round.v3f16
// NATIVE_HALF: ret <3 x half> %elt.round
// NO_HALF: define noundef <3 x float> @
// NO_HALF: %elt.round = call <3 x float> @llvm.round.v3f32(
// NO_HALF: ret <3 x float> %elt.round
half3 test_round_half3(half3 p0) { return round(p0); }
// NATIVE_HALF: define noundef <4 x half> @
// NATIVE_HALF: %elt.round = call <4 x half> @llvm.round.v4f16
// NATIVE_HALF: ret <4 x half> %elt.round
// NO_HALF: define noundef <4 x float> @
// NO_HALF: %elt.round = call <4 x float> @llvm.round.v4f32(
// NO_HALF: ret <4 x float> %elt.round
half4 test_round_half4(half4 p0) { return round(p0); }

// CHECK: define noundef float @
// CHECK: %elt.round = call float @llvm.round.f32(
// CHECK: ret float %elt.round
float test_round_float(float p0) { return round(p0); }
// CHECK: define noundef <2 x float> @
// CHECK: %elt.round = call <2 x float> @llvm.round.v2f32
// CHECK: ret <2 x float> %elt.round
float2 test_round_float2(float2 p0) { return round(p0); }
// CHECK: define noundef <3 x float> @
// CHECK: %elt.round = call <3 x float> @llvm.round.v3f32
// CHECK: ret <3 x float> %elt.round
float3 test_round_float3(float3 p0) { return round(p0); }
// CHECK: define noundef <4 x float> @
// CHECK: %elt.round = call <4 x float> @llvm.round.v4f32
// CHECK: ret <4 x float> %elt.round
float4 test_round_float4(float4 p0) { return round(p0); }
27 changes: 27 additions & 0 deletions clang/test/SemaHLSL/BuiltIns/round-errors.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -verify-ignore-unexpected

float test_too_few_arg() {
return __builtin_elementwise_round();
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
}

float2 test_too_many_arg(float2 p0) {
return __builtin_elementwise_round(p0, p0);
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
}

float builtin_bool_to_float_type_promotion(bool p1) {
return __builtin_elementwise_round(p1);
// expected-error@-1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
}

float builtin_round_int_to_float_promotion(int p1) {
return __builtin_elementwise_round(p1);
// expected-error@-1 {{1st argument must be a floating point type (was 'int')}}
}

float2 builtin_round_int2_to_float2_promotion(int2 p1) {
return __builtin_elementwise_round(p1);
// expected-error@-1 {{1st argument must be a floating point type (was 'int2' (aka 'vector<int, 2>'))}}
}
3 changes: 3 additions & 0 deletions llvm/lib/Target/DirectX/DXIL.td
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ class DXILOpMapping<int opCode, DXILOpClass opClass, Intrinsic intrinsic, string
// Concrete definition of DXIL Operation mapping to corresponding LLVM intrinsic
def Sin : DXILOpMapping<13, unary, int_sin,
"Returns sine(theta) for theta in radians.">;
def Round : DXILOpMapping<26, unary, int_round,
"Returns the input rounded to the nearest integer"
"within a floating-point type.">;
def UMax : DXILOpMapping<39, binary, int_umax,
"Unsigned integer maximum. UMax(a,b) = a > b ? a : b">;
def ThreadId : DXILOpMapping<93, threadId, int_dx_thread_id,
Expand Down
31 changes: 31 additions & 0 deletions llvm/test/CodeGen/DirectX/round.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
; RUN: opt -S -dxil-op-lower < %s | FileCheck %s

; Make sure dxil operation function calls for round are generated for float and half.
; CHECK:call float @dx.op.unary.f32(i32 26, float %{{.*}})
; CHECK:call half @dx.op.unary.f16(i32 26, half %{{.*}})

target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
target triple = "dxil-pc-shadermodel6.7-library"

; Function Attrs: noinline nounwind optnone
define noundef float @round_float(float noundef %a) #0 {
entry:
%a.addr = alloca float, align 4
store float %a, ptr %a.addr, align 4
%0 = load float, ptr %a.addr, align 4
%elt.round = call float @llvm.round.f32(float %0)
ret float %elt.round
}

; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
declare float @llvm.round.f32(float) #1

; Function Attrs: noinline nounwind optnone
define noundef half @round_half(half noundef %a) #0 {
entry:
%a.addr = alloca half, align 2
store half %a, ptr %a.addr, align 2
%0 = load half, ptr %a.addr, align 2
%elt.round = call half @llvm.round.f16(half %0)
ret half %elt.round
}