llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-hlsl @llvm/pr-subscribers-backend-directx @llvm/pr-subscribers-clang Author: Farzon Lotfi (farzonl) <details> <summary>Changes</summary> - Builtins.td - add the round builtin - CGBuiltin.cpp - add the builtin to DirectX intrinsic mapping hlsl_intrinsics.h - add the round api - SemaChecking.cpp - add type checks for builtin - IntrinsicsDirectX.td - add the round intrinsic - DXIL.td add the llvm intrinsic to DXIL lowering mapping - This change implements: #<!-- -->70077 --- Full diff: https://github.com/llvm/llvm-project/pull/83520.diff 9 Files Affected: - (modified) clang/include/clang/Basic/Builtins.td (+6) - (modified) clang/lib/CodeGen/CGBuiltin.cpp (+8) - (modified) clang/lib/Headers/hlsl/hlsl_intrinsics.h (+34) - (modified) clang/lib/Sema/SemaChecking.cpp (+7) - (added) clang/test/CodeGenHLSL/builtins/round.hlsl (+53) - (added) clang/test/SemaHLSL/BuiltIns/round-errors.hlsl (+27) - (modified) llvm/include/llvm/IR/IntrinsicsDirectX.td (+1) - (modified) llvm/lib/Target/DirectX/DXIL.td (+3) - (added) llvm/test/CodeGen/DirectX/round.ll (+43) ``````````diff diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 2c83dca248fb7d..cb59a7b26f729f 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -4548,6 +4548,12 @@ def HLSLLerp : LangBuiltin<"HLSL_LANG"> { let Prototype = "void(...)"; } +def HLSLRound : LangBuiltin<"HLSL_LANG"> { + let Spellings = ["__builtin_hlsl_elementwise_round"]; + let Attributes = [NoThrow, Const]; + let Prototype = "void(...)"; +} + // Builtins for XRay. def XRayCustomEvent : Builtin { let Spellings = ["__xray_customevent"]; diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 98684448f4ff5c..a27390c4e85fb7 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -18052,6 +18052,14 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID, /*ReturnType*/ Op0->getType(), Intrinsic::dx_frac, ArrayRef<Value *>{Op0}, nullptr, "dx.frac"); } + case Builtin::BI__builtin_hlsl_elementwise_round: { + Value *Op0 = EmitScalarExpr(E->getArg(0)); + if (!E->getArg(0)->getType()->hasFloatingRepresentation()) + llvm_unreachable("round operand must have a float representation"); + return Builder.CreateIntrinsic( + /*ReturnType*/ Op0->getType(), Intrinsic::dx_round, + ArrayRef<Value *>{Op0}, nullptr, "dx.round"); + } } return nullptr; } diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index 0aa8651ba80dc4..4f52b001440584 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -831,6 +831,40 @@ uint64_t3 reversebits(uint64_t3); _HLSL_BUILTIN_ALIAS(__builtin_elementwise_bitreverse) uint64_t4 reversebits(uint64_t4); +//===----------------------------------------------------------------------===// +// frac builtins +//===----------------------------------------------------------------------===// + +/// \fn T round(T x) +/// \brief Rounds the specified value to the nearest integer. Halfway cases are +/// rounded to the nearest even. \a x parameter. \param x The specified input +/// value. +/// +/// The return value is the \a x parameter, rounded to the nearest integer +/// within a floating-point type. + +_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2) +_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_round) +half round(half); +_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2) +_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_round) +half2 round(half2); +_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2) +_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_round) +half3 round(half3); +_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2) +_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_round) +half4 round(half4); + +_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_round) +float round(float); +_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_round) +float2 round(float2); +_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_round) +float3 round(float3); +_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_round) +float4 round(float4); + //===----------------------------------------------------------------------===// // sin builtins //===----------------------------------------------------------------------===// diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 690bdaa63d058b..56e4264f85ccdd 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -5294,6 +5294,13 @@ bool Sema::CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { return true; break; } + case Builtin::BI__builtin_hlsl_elementwise_round: { + if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) + return true; + if (CheckAllArgsHaveFloatRepresentation(this, TheCall)) + return true; + break; + } } return false; } diff --git a/clang/test/CodeGenHLSL/builtins/round.hlsl b/clang/test/CodeGenHLSL/builtins/round.hlsl new file mode 100644 index 00000000000000..6d582791447af2 --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/round.hlsl @@ -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: %dx.round = call half @llvm.dx.round.f16( +// NATIVE_HALF: ret half %dx.round +// NO_HALF: define noundef float @"?test_round_half@@YA$halff@$halff@@Z"( +// NO_HALF: %dx.round = call float @llvm.dx.round.f32( +// NO_HALF: ret float %dx.round +half test_round_half(half p0) { return round(p0); } +// NATIVE_HALF: define noundef <2 x half> @ +// NATIVE_HALF: %dx.round = call <2 x half> @llvm.dx.round.v2f16 +// NATIVE_HALF: ret <2 x half> %dx.round +// NO_HALF: define noundef <2 x float> @ +// NO_HALF: %dx.round = call <2 x float> @llvm.dx.round.v2f32( +// NO_HALF: ret <2 x float> %dx.round +half2 test_round_half2(half2 p0) { return round(p0); } +// NATIVE_HALF: define noundef <3 x half> @ +// NATIVE_HALF: %dx.round = call <3 x half> @llvm.dx.round.v3f16 +// NATIVE_HALF: ret <3 x half> %dx.round +// NO_HALF: define noundef <3 x float> @ +// NO_HALF: %dx.round = call <3 x float> @llvm.dx.round.v3f32( +// NO_HALF: ret <3 x float> %dx.round +half3 test_round_half3(half3 p0) { return round(p0); } +// NATIVE_HALF: define noundef <4 x half> @ +// NATIVE_HALF: %dx.round = call <4 x half> @llvm.dx.round.v4f16 +// NATIVE_HALF: ret <4 x half> %dx.round +// NO_HALF: define noundef <4 x float> @ +// NO_HALF: %dx.round = call <4 x float> @llvm.dx.round.v4f32( +// NO_HALF: ret <4 x float> %dx.round +half4 test_round_half4(half4 p0) { return round(p0); } + +// CHECK: define noundef float @ +// CHECK: %dx.round = call float @llvm.dx.round.f32( +// CHECK: ret float %dx.round +float test_round_float(float p0) { return round(p0); } +// CHECK: define noundef <2 x float> @ +// CHECK: %dx.round = call <2 x float> @llvm.dx.round.v2f32 +// CHECK: ret <2 x float> %dx.round +float2 test_round_float2(float2 p0) { return round(p0); } +// CHECK: define noundef <3 x float> @ +// CHECK: %dx.round = call <3 x float> @llvm.dx.round.v3f32 +// CHECK: ret <3 x float> %dx.round +float3 test_round_float3(float3 p0) { return round(p0); } +// CHECK: define noundef <4 x float> @ +// CHECK: %dx.round = call <4 x float> @llvm.dx.round.v4f32 +// CHECK: ret <4 x float> %dx.round +float4 test_round_float4(float4 p0) { return round(p0); } diff --git a/clang/test/SemaHLSL/BuiltIns/round-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/round-errors.hlsl new file mode 100644 index 00000000000000..9af07df5e40499 --- /dev/null +++ b/clang/test/SemaHLSL/BuiltIns/round-errors.hlsl @@ -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_hlsl_elementwise_round(); + // expected-error@-1 {{too few arguments to function call, expected 1, have 0}} +} + +float2 test_too_many_arg(float2 p0) { + return __builtin_hlsl_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_hlsl_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_hlsl_elementwise_round(p1); + // expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}} +} + +float2 builtin_round_int2_to_float2_promotion(int2 p1) { + return __builtin_hlsl_elementwise_round(p1); + // 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)}} +} diff --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td b/llvm/include/llvm/IR/IntrinsicsDirectX.td index b44d1c6d3d2f06..d389193fe97e37 100644 --- a/llvm/include/llvm/IR/IntrinsicsDirectX.td +++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td @@ -26,6 +26,7 @@ def int_dx_dot : [IntrNoMem, IntrWillReturn, Commutative] >; def int_dx_frac : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>; +def int_dx_round : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>; def int_dx_lerp : Intrinsic<[LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>], diff --git a/llvm/lib/Target/DirectX/DXIL.td b/llvm/lib/Target/DirectX/DXIL.td index 67ef7986622092..bab2c252c99d68 100644 --- a/llvm/lib/Target/DirectX/DXIL.td +++ b/llvm/lib/Target/DirectX/DXIL.td @@ -220,6 +220,9 @@ def Sin : DXILOpMapping<13, unary, int_sin, "Returns sine(theta) for theta in radians.">; def UMax : DXILOpMapping<39, binary, int_umax, "Unsigned integer maximum. UMax(a,b) = a > b ? a : b">; +def Round : DXILOpMapping<26, unary, int_dx_round, + "Returns the input rounded to the nearest integer" + "within a floating-point type.">; def ThreadId : DXILOpMapping<93, threadId, int_dx_thread_id, "Reads the thread ID">; def GroupId : DXILOpMapping<94, groupId, int_dx_group_id, diff --git a/llvm/test/CodeGen/DirectX/round.ll b/llvm/test/CodeGen/DirectX/round.ll new file mode 100644 index 00000000000000..6b4a74f2513589 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/round.ll @@ -0,0 +1,43 @@ +; 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 @_Z3foof(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 + %dx.round = call float @llvm.dx.round.f32(float %0) + ret float %dx.round +} + +; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn +declare float @llvm.dx.round.f32(float) #1 + +; Function Attrs: noinline nounwind optnone +define noundef half @_Z3barDh(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 + %dx.round = call half @llvm.dx.round.f16(half %0) + ret half %dx.round +} + +; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn +declare half @llvm.dx.round.f16(half) #1 + +attributes #0 = { noinline nounwind optnone "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } +attributes #1 = { nocallback nofree nosync nounwind readnone speculatable willreturn } + +!llvm.module.flags = !{!0} +!llvm.ident = !{!1} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 73417c517644db5c419c85c0b3cb6750172fcab5)"} \ No newline at end of file `````````` </details> https://github.com/llvm/llvm-project/pull/83520 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits