Skip to content

Commit c92d9b0

Browse files
authored
[SPIRV][HLSL] Add lowering of frac to SPIR-V (#97111)
Implements frac lowering to SPIR-V. Closes #88059
1 parent 6a8c5a9 commit c92d9b0

File tree

6 files changed

+166
-43
lines changed

6 files changed

+166
-43
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18471,10 +18471,10 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1847118471
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
1847218472
llvm_unreachable("frac operand must have a float representation");
1847318473
return Builder.CreateIntrinsic(
18474-
/*ReturnType=*/Op0->getType(), Intrinsic::dx_frac,
18475-
ArrayRef<Value *>{Op0}, nullptr, "dx.frac");
18476-
}
18477-
case Builtin::BI__builtin_hlsl_elementwise_isinf: {
18474+
/*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getFracIntrinsic(),
18475+
ArrayRef<Value *>{Op0}, nullptr, "hlsl.frac");
18476+
}
18477+
case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1847818478
Value *Op0 = EmitScalarExpr(E->getArg(0));
1847918479
llvm::Type *Xty = Op0->getType();
1848018480
llvm::Type *retType = llvm::Type::getInt1Ty(this->getLLVMContext());

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class CGHLSLRuntime {
7474

7575
GENERATE_HLSL_INTRINSIC_FUNCTION(All, all)
7676
GENERATE_HLSL_INTRINSIC_FUNCTION(Any, any)
77+
GENERATE_HLSL_INTRINSIC_FUNCTION(Frac, frac)
7778
GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
7879
GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
7980
GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,84 @@
11
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
22
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
3-
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
4-
// RUN: --check-prefixes=CHECK,NATIVE_HALF
3+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
4+
// RUN: --check-prefixes=CHECK,DXIL_CHECK,DXIL_NATIVE_HALF,NATIVE_HALF
55
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
66
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
7-
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
7+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,DXIL_CHECK,NO_HALF,DXIL_NO_HALF
8+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
9+
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
10+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
11+
// RUN: --check-prefixes=CHECK,SPIR_CHECK,NATIVE_HALF,SPIR_NATIVE_HALF
12+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
13+
// RUN: spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
14+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,SPIR_CHECK,NO_HALF,SPIR_NO_HALF
815

9-
// NATIVE_HALF: define noundef half @
10-
// NATIVE_HALF: %dx.frac = call half @llvm.dx.frac.f16(
11-
// NATIVE_HALF: ret half %dx.frac
12-
// NO_HALF: define noundef float @"?test_frac_half@@YA$halff@$halff@@Z"(
13-
// NO_HALF: %dx.frac = call float @llvm.dx.frac.f32(
14-
// NO_HALF: ret float %dx.frac
16+
// DXIL_NATIVE_HALF: define noundef half @
17+
// SPIR_NATIVE_HALF: define spir_func noundef half @
18+
// DXIL_NATIVE_HALF: %hlsl.frac = call half @llvm.dx.frac.f16(
19+
// SPIR_NATIVE_HALF: %hlsl.frac = call half @llvm.spv.frac.f16(
20+
// NATIVE_HALF: ret half %hlsl.frac
21+
// DXIL_NO_HALF: define noundef float @
22+
// SPIR_NO_HALF: define spir_func noundef float @
23+
// DXIL_NO_HALF: %hlsl.frac = call float @llvm.dx.frac.f32(
24+
// SPIR_NO_HALF: %hlsl.frac = call float @llvm.spv.frac.f32(
25+
// NO_HALF: ret float %hlsl.frac
1526
half test_frac_half(half p0) { return frac(p0); }
16-
// NATIVE_HALF: define noundef <2 x half> @
17-
// NATIVE_HALF: %dx.frac = call <2 x half> @llvm.dx.frac.v2f16
18-
// NATIVE_HALF: ret <2 x half> %dx.frac
19-
// NO_HALF: define noundef <2 x float> @
20-
// NO_HALF: %dx.frac = call <2 x float> @llvm.dx.frac.v2f32(
21-
// NO_HALF: ret <2 x float> %dx.frac
27+
// DXIL_NATIVE_HALF: define noundef <2 x half> @
28+
// SPIR_NATIVE_HALF: define spir_func noundef <2 x half> @
29+
// DXIL_NATIVE_HALF: %hlsl.frac = call <2 x half> @llvm.dx.frac.v2f16
30+
// SPIR_NATIVE_HALF: %hlsl.frac = call <2 x half> @llvm.spv.frac.v2f16
31+
// NATIVE_HALF: ret <2 x half> %hlsl.frac
32+
// DXIL_NO_HALF: define noundef <2 x float> @
33+
// SPIR_NO_HALF: define spir_func noundef <2 x float> @
34+
// DXIL_NO_HALF: %hlsl.frac = call <2 x float> @llvm.dx.frac.v2f32(
35+
// SPIR_NO_HALF: %hlsl.frac = call <2 x float> @llvm.spv.frac.v2f32(
36+
// NO_HALF: ret <2 x float> %hlsl.frac
2237
half2 test_frac_half2(half2 p0) { return frac(p0); }
23-
// NATIVE_HALF: define noundef <3 x half> @
24-
// NATIVE_HALF: %dx.frac = call <3 x half> @llvm.dx.frac.v3f16
25-
// NATIVE_HALF: ret <3 x half> %dx.frac
26-
// NO_HALF: define noundef <3 x float> @
27-
// NO_HALF: %dx.frac = call <3 x float> @llvm.dx.frac.v3f32(
28-
// NO_HALF: ret <3 x float> %dx.frac
38+
// DXIL_NATIVE_HALF: define noundef <3 x half> @
39+
// SPIR_NATIVE_HALF: define spir_func noundef <3 x half> @
40+
// DXIL_NATIVE_HALF: %hlsl.frac = call <3 x half> @llvm.dx.frac.v3f16
41+
// SPIR_NATIVE_HALF: %hlsl.frac = call <3 x half> @llvm.spv.frac.v3f16
42+
// NATIVE_HALF: ret <3 x half> %hlsl.frac
43+
// DXIL_NO_HALF: define noundef <3 x float> @
44+
// SPIR_NO_HALF: define spir_func noundef <3 x float> @
45+
// DXIL_NO_HALF: %hlsl.frac = call <3 x float> @llvm.dx.frac.v3f32(
46+
// SPIR_NO_HALF: %hlsl.frac = call <3 x float> @llvm.spv.frac.v3f32(
47+
// NO_HALF: ret <3 x float> %hlsl.frac
2948
half3 test_frac_half3(half3 p0) { return frac(p0); }
30-
// NATIVE_HALF: define noundef <4 x half> @
31-
// NATIVE_HALF: %dx.frac = call <4 x half> @llvm.dx.frac.v4f16
32-
// NATIVE_HALF: ret <4 x half> %dx.frac
33-
// NO_HALF: define noundef <4 x float> @
34-
// NO_HALF: %dx.frac = call <4 x float> @llvm.dx.frac.v4f32(
35-
// NO_HALF: ret <4 x float> %dx.frac
49+
// DXIL_NATIVE_HALF: define noundef <4 x half> @
50+
// SPIR_NATIVE_HALF: define spir_func noundef <4 x half> @
51+
// DXIL_NATIVE_HALF: %hlsl.frac = call <4 x half> @llvm.dx.frac.v4f16
52+
// SPIR_NATIVE_HALF: %hlsl.frac = call <4 x half> @llvm.spv.frac.v4f16
53+
// NATIVE_HALF: ret <4 x half> %hlsl.frac
54+
// DXIL_NO_HALF: define noundef <4 x float> @
55+
// SPIR_NO_HALF: define spir_func noundef <4 x float> @
56+
// DXIL_NO_HALF: %hlsl.frac = call <4 x float> @llvm.dx.frac.v4f32(
57+
// SPIR_NO_HALF: %hlsl.frac = call <4 x float> @llvm.spv.frac.v4f32(
58+
// NO_HALF: ret <4 x float> %hlsl.frac
3659
half4 test_frac_half4(half4 p0) { return frac(p0); }
3760

38-
// CHECK: define noundef float @
39-
// CHECK: %dx.frac = call float @llvm.dx.frac.f32(
40-
// CHECK: ret float %dx.frac
61+
// DXIL_CHECK: define noundef float @
62+
// SPIR_CHECK: define spir_func noundef float @
63+
// DXIL_CHECK: %hlsl.frac = call float @llvm.dx.frac.f32(
64+
// SPIR_CHECK: %hlsl.frac = call float @llvm.spv.frac.f32(
65+
// CHECK: ret float %hlsl.frac
4166
float test_frac_float(float p0) { return frac(p0); }
42-
// CHECK: define noundef <2 x float> @
43-
// CHECK: %dx.frac = call <2 x float> @llvm.dx.frac.v2f32
44-
// CHECK: ret <2 x float> %dx.frac
67+
// DXIL_CHECK: define noundef <2 x float> @
68+
// SPIR_CHECK: define spir_func noundef <2 x float> @
69+
// DXIL_CHECK: %hlsl.frac = call <2 x float> @llvm.dx.frac.v2f32
70+
// SPIR_CHECK: %hlsl.frac = call <2 x float> @llvm.spv.frac.v2f32
71+
// CHECK: ret <2 x float> %hlsl.frac
4572
float2 test_frac_float2(float2 p0) { return frac(p0); }
46-
// CHECK: define noundef <3 x float> @
47-
// CHECK: %dx.frac = call <3 x float> @llvm.dx.frac.v3f32
48-
// CHECK: ret <3 x float> %dx.frac
73+
// DXIL_CHECK: define noundef <3 x float> @
74+
// SPIR_CHECK: define spir_func noundef <3 x float> @
75+
// DXIL_CHECK: %hlsl.frac = call <3 x float> @llvm.dx.frac.v3f32
76+
// SPIR_CHECK: %hlsl.frac = call <3 x float> @llvm.spv.frac.v3f32
77+
// CHECK: ret <3 x float> %hlsl.frac
4978
float3 test_frac_float3(float3 p0) { return frac(p0); }
50-
// CHECK: define noundef <4 x float> @
51-
// CHECK: %dx.frac = call <4 x float> @llvm.dx.frac.v4f32
52-
// CHECK: ret <4 x float> %dx.frac
79+
// DXIL_CHECK: define noundef <4 x float> @
80+
// SPIR_CHECK: define spir_func noundef <4 x float> @
81+
// DXIL_CHECK: %hlsl.frac = call <4 x float> @llvm.dx.frac.v4f32
82+
// SPIR_CHECK: %hlsl.frac = call <4 x float> @llvm.spv.frac.v4f32
83+
// CHECK: ret <4 x float> %hlsl.frac
5384
float4 test_frac_float4(float4 p0) { return frac(p0); }

llvm/include/llvm/IR/IntrinsicsSPIRV.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ let TargetPrefix = "spv" in {
6060
Intrinsic<[ llvm_ptr_ty ], [llvm_i8_ty], [IntrWillReturn]>;
6161
def int_spv_all : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty]>;
6262
def int_spv_any : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty]>;
63+
def int_spv_frac : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty]>;
6364
def int_spv_lerp : Intrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>,LLVMMatchType<0>],
6465
[IntrNoMem, IntrWillReturn] >;
6566
def int_spv_rsqrt : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty]>;

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ class SPIRVInstructionSelector : public InstructionSelector {
173173
bool selectFmix(Register ResVReg, const SPIRVType *ResType,
174174
MachineInstr &I) const;
175175

176+
bool selectFrac(Register ResVReg, const SPIRVType *ResType,
177+
MachineInstr &I) const;
178+
176179
bool selectRsqrt(Register ResVReg, const SPIRVType *ResType,
177180
MachineInstr &I) const;
178181

@@ -1330,6 +1333,23 @@ bool SPIRVInstructionSelector::selectFmix(Register ResVReg,
13301333
.constrainAllUses(TII, TRI, RBI);
13311334
}
13321335

1336+
bool SPIRVInstructionSelector::selectFrac(Register ResVReg,
1337+
const SPIRVType *ResType,
1338+
MachineInstr &I) const {
1339+
1340+
assert(I.getNumOperands() == 3);
1341+
assert(I.getOperand(2).isReg());
1342+
MachineBasicBlock &BB = *I.getParent();
1343+
1344+
return BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpExtInst))
1345+
.addDef(ResVReg)
1346+
.addUse(GR.getSPIRVTypeID(ResType))
1347+
.addImm(static_cast<uint32_t>(SPIRV::InstructionSet::GLSL_std_450))
1348+
.addImm(GL::Fract)
1349+
.addUse(I.getOperand(2).getReg())
1350+
.constrainAllUses(TII, TRI, RBI);
1351+
}
1352+
13331353
bool SPIRVInstructionSelector::selectRsqrt(Register ResVReg,
13341354
const SPIRVType *ResType,
13351355
MachineInstr &I) const {
@@ -2059,6 +2079,8 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
20592079
return selectAny(ResVReg, ResType, I);
20602080
case Intrinsic::spv_lerp:
20612081
return selectFmix(ResVReg, ResType, I);
2082+
case Intrinsic::spv_frac:
2083+
return selectFrac(ResVReg, ResType, I);
20622084
case Intrinsic::spv_rsqrt:
20632085
return selectRsqrt(ResVReg, ResType, I);
20642086
case Intrinsic::spv_lifetime_start:
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
2+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
3+
4+
; CHECK-DAG: %[[#op_ext_glsl:]] = OpExtInstImport "GLSL.std.450"
5+
6+
; CHECK-DAG: %[[#float_32:]] = OpTypeFloat 32
7+
; CHECK-DAG: %[[#float_16:]] = OpTypeFloat 16
8+
; CHECK-DAG: %[[#float_64:]] = OpTypeFloat 64
9+
10+
; CHECK-DAG: %[[#vec4_float_32:]] = OpTypeVector %[[#float_32]] 4
11+
; CHECK-DAG: %[[#vec4_float_16:]] = OpTypeVector %[[#float_16]] 4
12+
; CHECK-DAG: %[[#vec4_float_64:]] = OpTypeVector %[[#float_64]] 4
13+
14+
define noundef float @frac_float(float noundef %a) {
15+
entry:
16+
; CHECK: %[[#float_32_arg:]] = OpFunctionParameter %[[#float_32]]
17+
; CHECK: %[[#]] = OpExtInst %[[#float_32]] %[[#op_ext_glsl]] Fract %[[#float_32_arg]]
18+
%elt.frac = call float @llvm.spv.frac.f32(float %a)
19+
ret float %elt.frac
20+
}
21+
22+
define noundef half @frac_half(half noundef %a) {
23+
entry:
24+
; CHECK: %[[#float_16_arg:]] = OpFunctionParameter %[[#float_16]]
25+
; CHECK: %[[#]] = OpExtInst %[[#float_16]] %[[#op_ext_glsl]] Fract %[[#float_16_arg]]
26+
%elt.frac = call half @llvm.spv.frac.f16(half %a)
27+
ret half %elt.frac
28+
}
29+
30+
define noundef double @frac_double(double noundef %a) {
31+
entry:
32+
; CHECK: %[[#float_64_arg:]] = OpFunctionParameter %[[#float_64]]
33+
; CHECK: %[[#]] = OpExtInst %[[#float_64]] %[[#op_ext_glsl]] Fract %[[#float_64_arg]]
34+
%elt.frac = call double @llvm.spv.frac.f64(double %a)
35+
ret double %elt.frac
36+
}
37+
38+
define noundef <4 x float> @frac_float_vector(<4 x float> noundef %a) {
39+
entry:
40+
; CHECK: %[[#vec4_float_32_arg:]] = OpFunctionParameter %[[#vec4_float_32]]
41+
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_32]] %[[#op_ext_glsl]] Fract %[[#vec4_float_32_arg]]
42+
%elt.frac = call <4 x float> @llvm.spv.frac.v4f32(<4 x float> %a)
43+
ret <4 x float> %elt.frac
44+
}
45+
46+
define noundef <4 x half> @frac_half_vector(<4 x half> noundef %a) {
47+
entry:
48+
; CHECK: %[[#vec4_float_16_arg:]] = OpFunctionParameter %[[#vec4_float_16]]
49+
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_16]] %[[#op_ext_glsl]] Fract %[[#vec4_float_16_arg]]
50+
%elt.frac = call <4 x half> @llvm.spv.frac.v4f16(<4 x half> %a)
51+
ret <4 x half> %elt.frac
52+
}
53+
54+
define noundef <4 x double> @frac_double_vector(<4 x double> noundef %a) {
55+
entry:
56+
; CHECK: %[[#vec4_float_64_arg:]] = OpFunctionParameter %[[#vec4_float_64]]
57+
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_64]] %[[#op_ext_glsl]] Fract %[[#vec4_float_64_arg]]
58+
%elt.frac = call <4 x double> @llvm.spv.frac.v4f64(<4 x double> %a)
59+
ret <4 x double> %elt.frac
60+
}
61+
62+
declare half @llvm.spv.frac.f16(half)
63+
declare float @llvm.spv.frac.f32(float)
64+
declare double @llvm.spv.frac.f64(double)
65+
66+
declare <4 x float> @llvm.spv.frac.v4f32(<4 x float>)
67+
declare <4 x half> @llvm.spv.frac.v4f16(<4 x half>)
68+
declare <4 x double> @llvm.spv.frac.v4f64(<4 x double>)

0 commit comments

Comments
 (0)