Skip to content

Commit 588e4ac

Browse files
committed
move length and distance to a target builtin
1 parent f074649 commit 588e4ac

File tree

9 files changed

+60
-51
lines changed

9 files changed

+60
-51
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,12 +4810,6 @@ def HLSLDegrees : LangBuiltin<"HLSL_LANG"> {
48104810
let Prototype = "void(...)";
48114811
}
48124812

4813-
def HLSLDistance : LangBuiltin<"HLSL_LANG"> {
4814-
let Spellings = ["__builtin_hlsl_distance"];
4815-
let Attributes = [NoThrow, Const];
4816-
let Prototype = "void(...)";
4817-
}
4818-
48194813
def HLSLDotProduct : LangBuiltin<"HLSL_LANG"> {
48204814
let Spellings = ["__builtin_hlsl_dot"];
48214815
let Attributes = [NoThrow, Const];
@@ -4852,12 +4846,6 @@ def HLSLIsinf : LangBuiltin<"HLSL_LANG"> {
48524846
let Prototype = "void(...)";
48534847
}
48544848

4855-
def HLSLLength : LangBuiltin<"HLSL_LANG"> {
4856-
let Spellings = ["__builtin_hlsl_length"];
4857-
let Attributes = [NoThrow, Const];
4858-
let Prototype = "void(...)";
4859-
}
4860-
48614849
def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
48624850
let Spellings = ["__builtin_hlsl_lerp"];
48634851
let Attributes = [NoThrow, Const];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===--- BuiltinsSPIRV.td - SPIRV Builtin function database ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
include "clang/Basic/BuiltinsBase.td"
10+
11+
def HLSLDistance : LangBuiltin<"HLSL_LANG"> {
12+
let Spellings = ["__builtin_hlsl_distance"];
13+
let Attributes = [NoThrow, Const];
14+
let Prototype = "void(...)";
15+
}
16+
17+
def HLSLLength : LangBuiltin<"HLSL_LANG"> {
18+
let Spellings = ["__builtin_hlsl_length"];
19+
let Attributes = [NoThrow, Const];
20+
let Prototype = "void(...)";
21+
}

clang/include/clang/Basic/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ clang_tablegen(BuiltinsRISCV.inc -gen-clang-builtins
6060
SOURCE BuiltinsRISCV.td
6161
TARGET ClangBuiltinsRISCV)
6262

63+
clang_tablegen(BuiltinsSPIRV.inc -gen-clang-builtins
64+
SOURCE BuiltinsSPIRV.td
65+
TARGET ClangBuiltinsSPIRV)
66+
6367
clang_tablegen(BuiltinsX86.inc -gen-clang-builtins
6468
SOURCE BuiltinsX86.td
6569
TARGET ClangBuiltinsX86)

clang/include/clang/Basic/TargetBuiltins.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ namespace clang {
119119
};
120120
}
121121

122+
/// SPIRV builtins
123+
namespace SPIRV {
124+
enum {
125+
LastTIBuiltin = clang::Builtin::FirstTSBuiltin - 1,
126+
#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
127+
#include "clang/Basic/BuiltinsSPIRV.inc"
128+
LastTSBuiltin
129+
};
130+
} // namespace SPIRV
131+
122132
/// X86 builtins
123133
namespace X86 {
124134
enum {

clang/lib/Basic/Targets/SPIR.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,24 @@
1313
#include "SPIR.h"
1414
#include "AMDGPU.h"
1515
#include "Targets.h"
16+
#include "clang/Basic/MacroBuilder.h"
17+
#include "clang/Basic/TargetBuiltins.h"
1618
#include "llvm/TargetParser/TargetParser.h"
1719

1820
using namespace clang;
1921
using namespace clang::targets;
2022

23+
static constexpr Builtin::Info BuiltinInfo[] = {
24+
#define BUILTIN(ID, TYPE, ATTRS) \
25+
{#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
26+
#include "clang/Basic/BuiltinsSPIRV.inc"
27+
};
28+
29+
ArrayRef<Builtin::Info> SPIRVTargetInfo::getTargetBuiltins() const {
30+
return llvm::ArrayRef(BuiltinInfo,
31+
clang::SPIRV::LastTSBuiltin - Builtin::FirstTSBuiltin);
32+
}
33+
2134
void SPIRTargetInfo::getTargetDefines(const LangOptions &Opts,
2235
MacroBuilder &Builder) const {
2336
DefineStd(Builder, "SPIR", Opts);

clang/lib/Basic/Targets/SPIR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class LLVM_LIBRARY_VISIBILITY SPIRVTargetInfo : public BaseSPIRVTargetInfo {
313313
resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-"
314314
"v256:256-v512:512-v1024:1024-n8:16:32:64-G1");
315315
}
316-
316+
ArrayRef<Builtin::Info> getTargetBuiltins() const override;
317317
void getTargetDefines(const LangOptions &Opts,
318318
MacroBuilder &Builder) const override;
319319
};

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19097,7 +19097,7 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1909719097
/*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getCrossIntrinsic(),
1909819098
ArrayRef<Value *>{Op0, Op1}, nullptr, "hlsl.cross");
1909919099
}
19100-
case Builtin::BI__builtin_hlsl_distance: {
19100+
case SPIRV::BI__builtin_hlsl_distance: {
1910119101
Value *X = EmitScalarExpr(E->getArg(0));
1910219102
Value *Y = EmitScalarExpr(E->getArg(1));
1910319103
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
@@ -19188,7 +19188,7 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1918819188
/*ReturnType=*/X->getType(), CGM.getHLSLRuntime().getLerpIntrinsic(),
1918919189
ArrayRef<Value *>{X, Y, S}, nullptr, "hlsl.lerp");
1919019190
}
19191-
case Builtin::BI__builtin_hlsl_length: {
19191+
case SPIRV::BI__builtin_hlsl_length: {
1919219192
Value *X = EmitScalarExpr(E->getArg(0));
1919319193
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
1919419194
"length operand must have a float representation");

clang/lib/Headers/hlsl/hlsl_detail.h

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,6 @@
99
#ifndef _HLSL_HLSL_DETAILS_H_
1010
#define _HLSL_HLSL_DETAILS_H_
1111

12-
#if __is_target_arch(dxil)
13-
#define IS_ARCH_DXIL 1
14-
#else
15-
#define IS_ARCH_DXIL 0
16-
#endif
17-
18-
#if __is_target_arch(spirv)
19-
#define IS_ARCH_SPIRV 1
20-
#else
21-
#define IS_ARCH_SPIRV 0
22-
#endif
23-
24-
#define ARCH_CONDITION(arch) \
25-
if (IS_ARCH_##arch) \
26-
return true;
27-
28-
// Note: arch is used to bypass
29-
// the generic implementation
30-
#define EXPAND_ARCH_CONDITIONS(arch) \
31-
ARCH_CONDITION(arch) \
32-
/* Add more architectures as needed */
33-
34-
#define DEFINE_TARGET_LOWERING(function_name, ...) \
35-
constexpr bool Has##function_name##Lowering() { \
36-
EXPAND_ARCH_CONDITIONS(__VA_ARGS__) \
37-
return false; /* Default case if no match */ \
38-
}
39-
4012
namespace hlsl {
4113

4214
namespace __detail {
@@ -69,7 +41,6 @@ constexpr enable_if_t<sizeof(U) == sizeof(T), U> bit_cast(T F) {
6941
return __builtin_bit_cast(U, F);
7042
}
7143

72-
DEFINE_TARGET_LOWERING(Length, SPIRV)
7344
template <typename T>
7445
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
7546
length_impl(T X) {
@@ -79,14 +50,14 @@ length_impl(T X) {
7950
template <typename T, int N>
8051
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
8152
length_vec_impl(vector<T, N> X) {
82-
if (HasLengthLowering())
83-
return __builtin_hlsl_length(X);
53+
#if (__has_builtin(__builtin_hlsl_length))
54+
return __builtin_hlsl_length(X);
55+
#endif
8456
vector<T, N> XSquared = X * X;
8557
T XSquaredSum = __builtin_hlsl_reduce_add(XSquared);
8658
return __builtin_elementwise_sqrt(XSquaredSum);
8759
}
8860

89-
DEFINE_TARGET_LOWERING(Distance, SPIRV)
9061
template <typename T>
9162
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
9263
distance_impl(T X, T Y) {
@@ -96,8 +67,9 @@ distance_impl(T X, T Y) {
9667
template <typename T, int N>
9768
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
9869
distance_vec_impl(vector<T, N> X, vector<T, N> Y) {
99-
if (HasDistanceLowering())
100-
return __builtin_hlsl_distance(X, Y);
70+
#if (__has_builtin(__builtin_hlsl_distance))
71+
return __builtin_hlsl_distance(X, Y);
72+
#endif
10173
return length_vec_impl(X - Y);
10274
}
10375

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "clang/Basic/DiagnosticSema.h"
2424
#include "clang/Basic/LLVM.h"
2525
#include "clang/Basic/SourceLocation.h"
26+
#include "clang/Basic/TargetBuiltins.h"
2627
#include "clang/Basic/TargetInfo.h"
2728
#include "clang/Sema/Initialization.h"
2829
#include "clang/Sema/ParsedAttr.h"
@@ -1976,7 +1977,7 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
19761977
TheCall->setType(ArgTyA);
19771978
break;
19781979
}
1979-
case Builtin::BI__builtin_hlsl_distance: {
1980+
case SPIRV::BI__builtin_hlsl_distance: {
19801981
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
19811982
return true;
19821983
if (SemaRef.checkArgCount(TheCall, 2))
@@ -2077,7 +2078,7 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
20772078
return true;
20782079
break;
20792080
}
2080-
case Builtin::BI__builtin_hlsl_length: {
2081+
case SPIRV::BI__builtin_hlsl_length: {
20812082
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
20822083
return true;
20832084
if (SemaRef.checkArgCount(TheCall, 1))

0 commit comments

Comments
 (0)