Skip to content

Commit b2ca23a

Browse files
authored
[HLSL] implement exp intrinsic (#83832)
This change implements: #70072 - `hlsl_intrinsics.h` - add the `exp` api - `DXIL.td` - add the llvm intrinsic to DXIL opcode lowering mapping. - This change reuses llvm's existing intrinsic `__builtin_elementwise_exp` \ `int_exp` & `__builtin_elementwise_exp2` \ `int_exp2` - This PR is part 1 of 2. - Part 2 requires an intrinsic to instructions lowering. Part2 will expand `int_exp` to ``` A = Builder.CreateFMul(log2eConst, val); int_exp2(A) ``` just like we do in [TranslateExp](https://github.com/microsoft/DirectXShaderCompiler/blob/main/lib/HLSL/HLOperationLower.cpp#L2220C1-L2236C2)
1 parent 06fea93 commit b2ca23a

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsics.h

+64
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,70 @@ uint64_t dot(uint64_t3, uint64_t3);
277277
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_dot)
278278
uint64_t dot(uint64_t4, uint64_t4);
279279

280+
//===----------------------------------------------------------------------===//
281+
// exp builtins
282+
//===----------------------------------------------------------------------===//
283+
284+
/// \fn T exp(T x)
285+
/// \brief Returns the base-e exponential, or \a e**x, of the specified value.
286+
/// \param x The specified input value.
287+
///
288+
/// The return value is the base-e exponential of the \a x parameter.
289+
290+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
291+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp)
292+
half exp(half);
293+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
294+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp)
295+
half2 exp(half2);
296+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
297+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp)
298+
half3 exp(half3);
299+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
300+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp)
301+
half4 exp(half4);
302+
303+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp)
304+
float exp(float);
305+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp)
306+
float2 exp(float2);
307+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp)
308+
float3 exp(float3);
309+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp)
310+
float4 exp(float4);
311+
312+
//===----------------------------------------------------------------------===//
313+
// exp2 builtins
314+
//===----------------------------------------------------------------------===//
315+
316+
/// \fn T exp2(T x)
317+
/// \brief Returns the base 2 exponential, or \a 2**x, of the specified value.
318+
/// \param x The specified input value.
319+
///
320+
/// The base 2 exponential of the \a x parameter.
321+
322+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
323+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
324+
half exp2(half);
325+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
326+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
327+
half2 exp2(half2);
328+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
329+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
330+
half3 exp2(half3);
331+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
332+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
333+
half4 exp2(half4);
334+
335+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
336+
float exp2(float);
337+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
338+
float2 exp2(float2);
339+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
340+
float3 exp2(float3);
341+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
342+
float4 exp2(float4);
343+
280344
//===----------------------------------------------------------------------===//
281345
// floor builtins
282346
//===----------------------------------------------------------------------===//
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2+
// 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
5+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
7+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
8+
9+
// NATIVE_HALF: define noundef half @
10+
// NATIVE_HALF: %elt.exp = call half @llvm.exp.f16(
11+
// NATIVE_HALF: ret half %elt.exp
12+
// NO_HALF: define noundef float @"?test_exp_half@@YA$halff@$halff@@Z"(
13+
// NO_HALF: %elt.exp = call float @llvm.exp.f32(
14+
// NO_HALF: ret float %elt.exp
15+
half test_exp_half(half p0) { return exp(p0); }
16+
// NATIVE_HALF: define noundef <2 x half> @
17+
// NATIVE_HALF: %elt.exp = call <2 x half> @llvm.exp.v2f16
18+
// NATIVE_HALF: ret <2 x half> %elt.exp
19+
// NO_HALF: define noundef <2 x float> @
20+
// NO_HALF: %elt.exp = call <2 x float> @llvm.exp.v2f32(
21+
// NO_HALF: ret <2 x float> %elt.exp
22+
half2 test_exp_half2(half2 p0) { return exp(p0); }
23+
// NATIVE_HALF: define noundef <3 x half> @
24+
// NATIVE_HALF: %elt.exp = call <3 x half> @llvm.exp.v3f16
25+
// NATIVE_HALF: ret <3 x half> %elt.exp
26+
// NO_HALF: define noundef <3 x float> @
27+
// NO_HALF: %elt.exp = call <3 x float> @llvm.exp.v3f32(
28+
// NO_HALF: ret <3 x float> %elt.exp
29+
half3 test_exp_half3(half3 p0) { return exp(p0); }
30+
// NATIVE_HALF: define noundef <4 x half> @
31+
// NATIVE_HALF: %elt.exp = call <4 x half> @llvm.exp.v4f16
32+
// NATIVE_HALF: ret <4 x half> %elt.exp
33+
// NO_HALF: define noundef <4 x float> @
34+
// NO_HALF: %elt.exp = call <4 x float> @llvm.exp.v4f32(
35+
// NO_HALF: ret <4 x float> %elt.exp
36+
half4 test_exp_half4(half4 p0) { return exp(p0); }
37+
38+
// CHECK: define noundef float @
39+
// CHECK: %elt.exp = call float @llvm.exp.f32(
40+
// CHECK: ret float %elt.exp
41+
float test_exp_float(float p0) { return exp(p0); }
42+
// CHECK: define noundef <2 x float> @
43+
// CHECK: %elt.exp = call <2 x float> @llvm.exp.v2f32
44+
// CHECK: ret <2 x float> %elt.exp
45+
float2 test_exp_float2(float2 p0) { return exp(p0); }
46+
// CHECK: define noundef <3 x float> @
47+
// CHECK: %elt.exp = call <3 x float> @llvm.exp.v3f32
48+
// CHECK: ret <3 x float> %elt.exp
49+
float3 test_exp_float3(float3 p0) { return exp(p0); }
50+
// CHECK: define noundef <4 x float> @
51+
// CHECK: %elt.exp = call <4 x float> @llvm.exp.v4f32
52+
// CHECK: ret <4 x float> %elt.exp
53+
float4 test_exp_float4(float4 p0) { return exp(p0); }
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2+
// 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
5+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
7+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
8+
9+
// NATIVE_HALF: define noundef half @
10+
// NATIVE_HALF: %elt.exp2 = call half @llvm.exp2.f16(
11+
// NATIVE_HALF: ret half %elt.exp2
12+
// NO_HALF: define noundef float @"?test_exp2_half@@YA$halff@$halff@@Z"(
13+
// NO_HALF: %elt.exp2 = call float @llvm.exp2.f32(
14+
// NO_HALF: ret float %elt.exp2
15+
half test_exp2_half(half p0) { return exp2(p0); }
16+
// NATIVE_HALF: define noundef <2 x half> @
17+
// NATIVE_HALF: %elt.exp2 = call <2 x half> @llvm.exp2.v2f16
18+
// NATIVE_HALF: ret <2 x half> %elt.exp2
19+
// NO_HALF: define noundef <2 x float> @
20+
// NO_HALF: %elt.exp2 = call <2 x float> @llvm.exp2.v2f32(
21+
// NO_HALF: ret <2 x float> %elt.exp2
22+
half2 test_exp2_half2(half2 p0) { return exp2(p0); }
23+
// NATIVE_HALF: define noundef <3 x half> @
24+
// NATIVE_HALF: %elt.exp2 = call <3 x half> @llvm.exp2.v3f16
25+
// NATIVE_HALF: ret <3 x half> %elt.exp2
26+
// NO_HALF: define noundef <3 x float> @
27+
// NO_HALF: %elt.exp2 = call <3 x float> @llvm.exp2.v3f32(
28+
// NO_HALF: ret <3 x float> %elt.exp2
29+
half3 test_exp2_half3(half3 p0) { return exp2(p0); }
30+
// NATIVE_HALF: define noundef <4 x half> @
31+
// NATIVE_HALF: %elt.exp2 = call <4 x half> @llvm.exp2.v4f16
32+
// NATIVE_HALF: ret <4 x half> %elt.exp2
33+
// NO_HALF: define noundef <4 x float> @
34+
// NO_HALF: %elt.exp2 = call <4 x float> @llvm.exp2.v4f32(
35+
// NO_HALF: ret <4 x float> %elt.exp2
36+
half4 test_exp2_half4(half4 p0) { return exp2(p0); }
37+
38+
// CHECK: define noundef float @
39+
// CHECK: %elt.exp2 = call float @llvm.exp2.f32(
40+
// CHECK: ret float %elt.exp2
41+
float test_exp2_float(float p0) { return exp2(p0); }
42+
// CHECK: define noundef <2 x float> @
43+
// CHECK: %elt.exp2 = call <2 x float> @llvm.exp2.v2f32
44+
// CHECK: ret <2 x float> %elt.exp2
45+
float2 test_exp2_float2(float2 p0) { return exp2(p0); }
46+
// CHECK: define noundef <3 x float> @
47+
// CHECK: %elt.exp2 = call <3 x float> @llvm.exp2.v3f32
48+
// CHECK: ret <3 x float> %elt.exp2
49+
float3 test_exp2_float3(float3 p0) { return exp2(p0); }
50+
// CHECK: define noundef <4 x float> @
51+
// CHECK: %elt.exp2 = call <4 x float> @llvm.exp2.v4f32
52+
// CHECK: ret <4 x float> %elt.exp2
53+
float4 test_exp2_float4(float4 p0) { return exp2(p0); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// 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 -DTEST_FUNC=__builtin_elementwise_exp
3+
// 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 -DTEST_FUNC=__builtin_elementwise_exp2
4+
float test_too_few_arg() {
5+
return TEST_FUNC();
6+
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
7+
}
8+
9+
float2 test_too_many_arg(float2 p0) {
10+
return TEST_FUNC(p0, p0);
11+
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
12+
}
13+
14+
float builtin_bool_to_float_type_promotion(bool p1) {
15+
return TEST_FUNC(p1);
16+
// expected-error@-1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
17+
}
18+
19+
float builtin_exp_int_to_float_promotion(int p1) {
20+
return TEST_FUNC(p1);
21+
// expected-error@-1 {{1st argument must be a floating point type (was 'int')}}
22+
}
23+
24+
float2 builtin_exp_int2_to_float2_promotion(int2 p1) {
25+
return TEST_FUNC(p1);
26+
// expected-error@-1 {{1st argument must be a floating point type (was 'int2' (aka 'vector<int, 2>'))}}
27+
}

llvm/lib/Target/DirectX/DXIL.td

+3
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ class DXILOpMapping<int opCode, DXILOpClass opClass, Intrinsic intrinsic, string
218218
// Concrete definition of DXIL Operation mapping to corresponding LLVM intrinsic
219219
def Sin : DXILOpMapping<13, unary, int_sin,
220220
"Returns sine(theta) for theta in radians.">;
221+
def Exp2 : DXILOpMapping<21, unary, int_exp2,
222+
"Returns the base 2 exponential, or 2**x, of the specified value."
223+
"exp2(x) = 2**x.">;
221224
def Frac : DXILOpMapping<22, unary, int_dx_frac,
222225
"Returns a fraction from 0 to 1 that represents the "
223226
"decimal part of the input.">;

llvm/test/CodeGen/DirectX/exp2.ll

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
2+
3+
; Make sure dxil operation function calls for exp2 are generated for float and half.
4+
; CHECK:call float @dx.op.unary.f32(i32 21, float %{{.*}})
5+
; CHECK:call half @dx.op.unary.f16(i32 21, half %{{.*}})
6+
7+
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"
8+
target triple = "dxil-pc-shadermodel6.7-library"
9+
10+
; Function Attrs: noinline nounwind optnone
11+
define noundef float @exp2_float(float noundef %a) #0 {
12+
entry:
13+
%a.addr = alloca float, align 4
14+
store float %a, ptr %a.addr, align 4
15+
%0 = load float, ptr %a.addr, align 4
16+
%elt.exp2 = call float @llvm.exp2.f32(float %0)
17+
ret float %elt.exp2
18+
}
19+
20+
; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
21+
declare float @llvm.exp2.f32(float) #1
22+
23+
; Function Attrs: noinline nounwind optnone
24+
define noundef half @exp2_half(half noundef %a) #0 {
25+
entry:
26+
%a.addr = alloca half, align 2
27+
store half %a, ptr %a.addr, align 2
28+
%0 = load half, ptr %a.addr, align 2
29+
%elt.exp2 = call half @llvm.exp2.f16(half %0)
30+
ret half %elt.exp2
31+
}

0 commit comments

Comments
 (0)