Skip to content

Commit

Permalink
[HLSL] Implementation of the fmod intrinsic
Browse files Browse the repository at this point in the history
This change implements the frontend for llvm#99118
Builtins.td           - add the fmod builtin
CGBuiltin.cpp         - add the builtin to DirectX intrinsic mapping
hlsl_intrinsics.h     - add the fmod api
SemaHLSL.cpp          - add type checks for builtin
IntrinsicsDirectX.td  - add the fmod DirectX intrinsic
  • Loading branch information
lizhengxing committed Sep 13, 2024
1 parent ff1de24 commit 565ec72
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -4782,6 +4782,12 @@ def HLSLStep: LangBuiltin<"HLSL_LANG"> {
let Prototype = "void(...)";
}

def HLSLFmod : LangBuiltin<"HLSL_LANG"> {
let Spellings = ["__builtin_hlsl_elementwise_fmod"];
let Attributes = [NoThrow, Const];
let Prototype = "void(...)";
}

// Builtins for XRay.
def XRayCustomEvent : Builtin {
let Spellings = ["__xray_customevent"];
Expand Down
19 changes: 19 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18709,6 +18709,25 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
CGM.getHLSLRuntime().getNormalizeIntrinsic(), ArrayRef<Value *>{X},
nullptr, "hlsl.normalize");
}
case Builtin::BI__builtin_hlsl_elementwise_fmod: {
Value *Op0 = EmitScalarExpr(E->getArg(0));
Value *Op1 = EmitScalarExpr(E->getArg(1));
if (!E->getArg(0)->getType()->hasFloatingRepresentation() ||
(E->getArg(0)->getType() != E->getArg(1)->getType()))
llvm_unreachable("fmod operands must have the same float representation");

llvm::Triple::ArchType Arch = CGM.getTarget().getTriple().getArch();
assert(((Arch == llvm::Triple::dxil) || (Arch == llvm::Triple::spirv)) &&
"Unknown target architecture");

if (CGM.getTarget().getTriple().getArch() == llvm::Triple::dxil)
return Builder.CreateIntrinsic(
/*ReturnType=*/Op0->getType(), Intrinsic::dx_fmod,
ArrayRef<Value *>{Op0, Op1}, nullptr, "dx.fmod");

// HLSL's Fmod builtin is equivalent to SPIRV's OpFRem instruction
return Builder.CreateFRem(Op0, Op1, "fmod");
}
case Builtin::BI__builtin_hlsl_elementwise_frac: {
Value *Op0 = EmitScalarExpr(E->getArg(0));
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
Expand Down
33 changes: 33 additions & 0 deletions clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,39 @@ float3 floor(float3);
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
float4 floor(float4);

//===----------------------------------------------------------------------===//
// fmod builtins
//===----------------------------------------------------------------------===//

/// \fn T fmod(T x, T y)
/// \brief Returns the linear interpolation of x to y.
/// \param x [in] The dividend.
/// \param y [in] The divisor.
///
/// Return the floating-point remainder of the x parameter divided by the y parameter.

_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
half fmod(half, half);
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
half2 fmod(half2, half2);
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
half3 fmod(half3, half3);
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
half4 fmod(half4, half4);

_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
float fmod(float, float);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
float2 fmod(float2, float2);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
float3 fmod(float3, float3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_fmod)
float4 fmod(float4, float4);

//===----------------------------------------------------------------------===//
// frac builtins
//===----------------------------------------------------------------------===//
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,18 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
return true;
break;
}
case Builtin::BI__builtin_hlsl_elementwise_fmod: {
if (SemaRef.checkArgCount(TheCall, 2))
return true;
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
return true;

ExprResult A = TheCall->getArg(0);
QualType ArgTyA = A.get()->getType();
// return type is the same as the input type
TheCall->setType(ArgTyA);
break;
}
case Builtin::BI__builtin_hlsl_select: {
if (SemaRef.checkArgCount(TheCall, 3))
return true;
Expand Down
122 changes: 122 additions & 0 deletions clang/test/CodeGenHLSL/builtins/fmod.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// DirectX target:
//
// 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=DX-CHECK,DX-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=DX-CHECK,DX-NO_HALF



// Spirv target:
//
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
// RUN: --check-prefixes=SPV-CHECK,SPV-NATIVE_HALF

// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
// RUN: spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
// RUN: -o - | FileCheck %s --check-prefixes=SPV-CHECK,SPV-NO_HALF



// DX-NATIVE_HALF: define noundef half @
// DX-NATIVE_HALF: %dx.fmod = call half @llvm.dx.fmod.f16(
// DX-NATIVE_HALF: ret half %dx.fmod
// DX-NO_HALF: define noundef float @
// DX-NO_HALF: %dx.fmod = call float @llvm.dx.fmod.f32(
// DX-NO_HALF: ret float %dx.fmod
//
// SPV-NATIVE_HALF: define spir_func noundef half @
// SPV-NATIVE_HALF: %fmod = frem half
// SPV-NATIVE_HALF: ret half %fmod
// SPV-NO_HALF: define spir_func noundef float @
// SPV-NO_HALF: %fmod = frem float
// SPV-NO_HALF: ret float %fmod
half test_fmod_half(half p0, half p1) { return fmod(p0, p1); }

// DX-NATIVE_HALF: define noundef <2 x half> @
// DX-NATIVE_HALF: %dx.fmod = call <2 x half> @llvm.dx.fmod.v2f16
// DX-NATIVE_HALF: ret <2 x half> %dx.fmod
// DX-NO_HALF: define noundef <2 x float> @
// DX-NO_HALF: %dx.fmod = call <2 x float> @llvm.dx.fmod.v2f32(
// DX-NO_HALF: ret <2 x float> %dx.fmod
//
// SPV-NATIVE_HALF: define spir_func noundef <2 x half> @
// SPV-NATIVE_HALF: %fmod = frem <2 x half>
// SPV-NATIVE_HALF: ret <2 x half> %fmod
// SPV-NO_HALF: define spir_func noundef <2 x float> @
// SPV-NO_HALF: %fmod = frem <2 x float>
// SPV-NO_HALF: ret <2 x float> %fmod
half2 test_fmod_half2(half2 p0, half2 p1) { return fmod(p0, p1); }

// DX-NATIVE_HALF: define noundef <3 x half> @
// DX-NATIVE_HALF: %dx.fmod = call <3 x half> @llvm.dx.fmod.v3f16
// DX-NATIVE_HALF: ret <3 x half> %dx.fmod
// DX-NO_HALF: define noundef <3 x float> @
// DX-NO_HALF: %dx.fmod = call <3 x float> @llvm.dx.fmod.v3f32(
// DX-NO_HALF: ret <3 x float> %dx.fmod
//
// SPV-NATIVE_HALF: define spir_func noundef <3 x half> @
// SPV-NATIVE_HALF: %fmod = frem <3 x half>
// SPV-NATIVE_HALF: ret <3 x half> %fmod
// SPV-NO_HALF: define spir_func noundef <3 x float> @
// SPV-NO_HALF: %fmod = frem <3 x float>
// SPV-NO_HALF: ret <3 x float> %fmod
half3 test_fmod_half3(half3 p0, half3 p1) { return fmod(p0, p1); }

// DX-NATIVE_HALF: define noundef <4 x half> @
// DX-NATIVE_HALF: %dx.fmod = call <4 x half> @llvm.dx.fmod.v4f16
// DX-NATIVE_HALF: ret <4 x half> %dx.fmod
// DX-NO_HALF: define noundef <4 x float> @
// DX-NO_HALF: %dx.fmod = call <4 x float> @llvm.dx.fmod.v4f32(
// DX-NO_HALF: ret <4 x float> %dx.fmod
//
// SPV-NATIVE_HALF: define spir_func noundef <4 x half> @
// SPV-NATIVE_HALF: %fmod = frem <4 x half>
// SPV-NATIVE_HALF: ret <4 x half> %fmod
// SPV-NO_HALF: define spir_func noundef <4 x float> @
// SPV-NO_HALF: %fmod = frem <4 x float>
// SPV-NO_HALF: ret <4 x float> %fmod
half4 test_fmod_half4(half4 p0, half4 p1) { return fmod(p0, p1); }

// DX-CHECK: define noundef float @
// DX-CHECK: %dx.fmod = call float @llvm.dx.fmod.f32(
// DX-CHECK: ret float %dx.fmod
//
// SPV-CHECK: define spir_func noundef float @
// SPV-CHECK: %fmod = frem float
// SPV-CHECK: ret float %fmod
float test_fmod_float(float p0, float p1) { return fmod(p0, p1); }

// DX-CHECK: define noundef <2 x float> @
// DX-CHECK: %dx.fmod = call <2 x float> @llvm.dx.fmod.v2f32
// DX-CHECK: ret <2 x float> %dx.fmod
//
// SPV-CHECK: define spir_func noundef <2 x float> @
// SPV-CHECK: %fmod = frem <2 x float>
// SPV-CHECK: ret <2 x float> %fmod
float2 test_fmod_float2(float2 p0, float2 p1) { return fmod(p0, p1); }

// DX-CHECK: define noundef <3 x float> @
// DX-CHECK: %dx.fmod = call <3 x float> @llvm.dx.fmod.v3f32
// DX-CHECK: ret <3 x float> %dx.fmod
//
// SPV-CHECK: define spir_func noundef <3 x float> @
// SPV-CHECK: %fmod = frem <3 x float>
// SPV-CHECK: ret <3 x float> %fmod
float3 test_fmod_float3(float3 p0, float3 p1) { return fmod(p0, p1); }

// DX-CHECK: define noundef <4 x float> @
// DX-CHECK: %dx.fmod = call <4 x float> @llvm.dx.fmod.v4f32
// DX-CHECK: ret <4 x float> %dx.fmod
//
// SPV-CHECK: define spir_func noundef <4 x float> @
// SPV-CHECK: %fmod = frem <4 x float>
// SPV-CHECK: ret <4 x float> %fmod
float4 test_fmod_float4(float4 p0, float4 p1) { return fmod(p0, p1); }

38 changes: 38 additions & 0 deletions clang/test/SemaHLSL/BuiltIns/fmod-errors.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

// 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

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

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

float builtin_bool_to_float_type_promotion(bool p1, bool p2) {
return __builtin_hlsl_elementwise_fmod(p1, p2);
// expected-error@-1 {{passing 'bool' to parameter of incompatible type 'float'}}
}

float builtin_fmod_int_to_float_promotion(int p1, int p2) {
return __builtin_hlsl_elementwise_fmod(p1, p2);
// expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}}
}

float2 builtin_fmod_int2_to_float2_promotion(int2 p1, int2 p2) {
return __builtin_hlsl_elementwise_fmod(p1, p2);
// 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)}}
}

// builtins are variadic functions and so are subject to DefaultVariadicArgumentPromotion
half builtin_fmod_half_scalar (half p0, half p1) {
return __builtin_hlsl_elementwise_fmod(p0, p1);
// expected-error@-1 {{passing 'double' to parameter of incompatible type 'float'}}
}

float builtin_fmod_float_scalar (float p0, float p1) {
return __builtin_hlsl_elementwise_fmod (p0, p1);
// expected-error@-1 {{passing 'double' to parameter of incompatible type 'float'}}
}
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/IntrinsicsDirectX.td
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]
def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty]>;
def int_dx_step : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>]>;
def int_dx_fmod : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>]>;
}

0 comments on commit 565ec72

Please sign in to comment.