Skip to content

Commit 70e76cd

Browse files
committed
Addressing RFC comments, replace LangBuiltin with TargetBuiltin
1 parent 588e4ac commit 70e76cd

File tree

14 files changed

+172
-73
lines changed

14 files changed

+172
-73
lines changed

.github/new-prs-labeler.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ backend:DirectX:
661661

662662
backend:SPIR-V:
663663
- clang/lib/Driver/ToolChains/SPIRV.*
664+
- clang/lib/Sema/SemaSPIRV.cpp
665+
- clang/include/clang/Sema/SemaSPIRV.h
664666
- llvm/lib/Target/SPIRV/**
665667
- llvm/test/CodeGen/SPIRV/**
666668
- llvm/test/Frontend/HLSL/**

clang/include/clang/Basic/BuiltinsSPIRV.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
include "clang/Basic/BuiltinsBase.td"
1010

11-
def HLSLDistance : LangBuiltin<"HLSL_LANG"> {
12-
let Spellings = ["__builtin_hlsl_distance"];
11+
def HLSLDistance : Builtin {
12+
let Spellings = ["__builtin_spirv_distance"];
1313
let Attributes = [NoThrow, Const];
1414
let Prototype = "void(...)";
1515
}
1616

17-
def HLSLLength : LangBuiltin<"HLSL_LANG"> {
18-
let Spellings = ["__builtin_hlsl_length"];
17+
def HLSLLength : Builtin {
18+
let Spellings = ["__builtin_spirv_length"];
1919
let Attributes = [NoThrow, Const];
2020
let Prototype = "void(...)";
2121
}

clang/include/clang/Sema/Sema.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_CLANG_SEMA_SEMA_H
1515
#define LLVM_CLANG_SEMA_SEMA_H
1616

17+
#include "SemaSPIRV.h"
1718
#include "clang/APINotes/APINotesManager.h"
1819
#include "clang/AST/ASTFwd.h"
1920
#include "clang/AST/Attr.h"
@@ -173,6 +174,7 @@ class SemaOpenMP;
173174
class SemaPPC;
174175
class SemaPseudoObject;
175176
class SemaRISCV;
177+
class SemaSPIRV;
176178
class SemaSYCL;
177179
class SemaSwift;
178180
class SemaSystemZ;
@@ -1142,6 +1144,11 @@ class Sema final : public SemaBase {
11421144
return *RISCVPtr;
11431145
}
11441146

1147+
SemaSPIRV &SPIRV() {
1148+
assert(SPIRVPtr);
1149+
return *SPIRVPtr;
1150+
}
1151+
11451152
SemaSYCL &SYCL() {
11461153
assert(SYCLPtr);
11471154
return *SYCLPtr;
@@ -1219,6 +1226,7 @@ class Sema final : public SemaBase {
12191226
std::unique_ptr<SemaPPC> PPCPtr;
12201227
std::unique_ptr<SemaPseudoObject> PseudoObjectPtr;
12211228
std::unique_ptr<SemaRISCV> RISCVPtr;
1229+
std::unique_ptr<SemaSPIRV> SPIRVPtr;
12221230
std::unique_ptr<SemaSYCL> SYCLPtr;
12231231
std::unique_ptr<SemaSwift> SwiftPtr;
12241232
std::unique_ptr<SemaSystemZ> SystemZPtr;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----- SemaSPIRV.h ----- Semantic Analysis for SPIRV constructs
2+
//---------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
/// \file
10+
/// This file declares semantic analysis for SPIRV constructs.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CLANG_SEMA_SEMASPIRV_H
15+
#define LLVM_CLANG_SEMA_SEMASPIRV_H
16+
17+
#include "clang/AST/ASTFwd.h"
18+
#include "clang/Sema/SemaBase.h"
19+
20+
namespace clang {
21+
class SemaSPIRV : public SemaBase {
22+
public:
23+
SemaSPIRV(Sema &S);
24+
25+
bool CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
26+
};
27+
} // namespace clang
28+
29+
#endif // LLVM_CLANG_SEMA_SEMASPIRV_H

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "llvm/IR/IntrinsicsR600.h"
5656
#include "llvm/IR/IntrinsicsRISCV.h"
5757
#include "llvm/IR/IntrinsicsS390.h"
58+
#include "llvm/IR/IntrinsicsSPIRV.h"
5859
#include "llvm/IR/IntrinsicsWebAssembly.h"
5960
#include "llvm/IR/IntrinsicsX86.h"
6061
#include "llvm/IR/MDBuilder.h"
@@ -67,6 +68,7 @@
6768
#include "llvm/TargetParser/AArch64TargetParser.h"
6869
#include "llvm/TargetParser/RISCVISAInfo.h"
6970
#include "llvm/TargetParser/RISCVTargetParser.h"
71+
#include "llvm/TargetParser/Triple.h"
7072
#include "llvm/TargetParser/X86TargetParser.h"
7173
#include <optional>
7274
#include <utility>
@@ -6719,6 +6721,8 @@ static Value *EmitTargetArchBuiltinExpr(CodeGenFunction *CGF,
67196721
case llvm::Triple::riscv32:
67206722
case llvm::Triple::riscv64:
67216723
return CGF->EmitRISCVBuiltinExpr(BuiltinID, E, ReturnValue);
6724+
case llvm::Triple::spirv:
6725+
return CGF->EmitSPIRVBuiltinExpr(BuiltinID, E);
67226726
case llvm::Triple::spirv64:
67236727
if (CGF->getTarget().getTriple().getOS() != llvm::Triple::OSType::AMDHSA)
67246728
return nullptr;
@@ -19097,20 +19101,6 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1909719101
/*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getCrossIntrinsic(),
1909819102
ArrayRef<Value *>{Op0, Op1}, nullptr, "hlsl.cross");
1909919103
}
19100-
case SPIRV::BI__builtin_hlsl_distance: {
19101-
Value *X = EmitScalarExpr(E->getArg(0));
19102-
Value *Y = EmitScalarExpr(E->getArg(1));
19103-
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
19104-
E->getArg(1)->getType()->hasFloatingRepresentation() &&
19105-
"Distance operands must have a float representation");
19106-
assert(E->getArg(0)->getType()->isVectorType() &&
19107-
E->getArg(1)->getType()->isVectorType() &&
19108-
"Distance operands must be a vector");
19109-
return Builder.CreateIntrinsic(
19110-
/*ReturnType=*/X->getType()->getScalarType(),
19111-
CGM.getHLSLRuntime().getDistanceIntrinsic(), ArrayRef<Value *>{X, Y},
19112-
nullptr, "hlsl.distance");
19113-
}
1911419104
case Builtin::BI__builtin_hlsl_dot: {
1911519105
Value *Op0 = EmitScalarExpr(E->getArg(0));
1911619106
Value *Op1 = EmitScalarExpr(E->getArg(1));
@@ -19188,17 +19178,6 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1918819178
/*ReturnType=*/X->getType(), CGM.getHLSLRuntime().getLerpIntrinsic(),
1918919179
ArrayRef<Value *>{X, Y, S}, nullptr, "hlsl.lerp");
1919019180
}
19191-
case SPIRV::BI__builtin_hlsl_length: {
19192-
Value *X = EmitScalarExpr(E->getArg(0));
19193-
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
19194-
"length operand must have a float representation");
19195-
assert(E->getArg(0)->getType()->isVectorType() &&
19196-
"length operand must be a vector");
19197-
return Builder.CreateIntrinsic(
19198-
/*ReturnType=*/X->getType()->getScalarType(),
19199-
CGM.getHLSLRuntime().getLengthIntrinsic(), ArrayRef<Value *>{X},
19200-
nullptr, "hlsl.length");
19201-
}
1920219181
case Builtin::BI__builtin_hlsl_normalize: {
1920319182
Value *X = EmitScalarExpr(E->getArg(0));
1920419183

@@ -19483,6 +19462,36 @@ void CodeGenFunction::AddAMDGPUFenceAddressSpaceMMRA(llvm::Instruction *Inst,
1948319462
Inst->setMetadata(LLVMContext::MD_mmra, MMRAMetadata::getMD(Ctx, MMRAs));
1948419463
}
1948519464

19465+
Value *CodeGenFunction::EmitSPIRVBuiltinExpr(unsigned BuiltinID,
19466+
const CallExpr *E) {
19467+
switch (BuiltinID) {
19468+
case SPIRV::BI__builtin_spirv_distance: {
19469+
Value *X = EmitScalarExpr(E->getArg(0));
19470+
Value *Y = EmitScalarExpr(E->getArg(1));
19471+
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
19472+
E->getArg(1)->getType()->hasFloatingRepresentation() &&
19473+
"Distance operands must have a float representation");
19474+
assert(E->getArg(0)->getType()->isVectorType() &&
19475+
E->getArg(1)->getType()->isVectorType() &&
19476+
"Distance operands must be a vector");
19477+
return Builder.CreateIntrinsic(
19478+
/*ReturnType=*/X->getType()->getScalarType(), Intrinsic::spv_distance,
19479+
ArrayRef<Value *>{X, Y}, nullptr, "hlsl.distance");
19480+
}
19481+
case SPIRV::BI__builtin_spirv_length: {
19482+
Value *X = EmitScalarExpr(E->getArg(0));
19483+
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
19484+
"length operand must have a float representation");
19485+
assert(E->getArg(0)->getType()->isVectorType() &&
19486+
"length operand must be a vector");
19487+
return Builder.CreateIntrinsic(
19488+
/*ReturnType=*/X->getType()->getScalarType(), Intrinsic::spv_length,
19489+
ArrayRef<Value *>{X}, nullptr, "hlsl.length");
19490+
}
19491+
}
19492+
return nullptr;
19493+
}
19494+
1948619495
Value *CodeGenFunction::EmitAMDGPUBuiltinExpr(unsigned BuiltinID,
1948719496
const CallExpr *E) {
1948819497
llvm::AtomicOrdering AO = llvm::AtomicOrdering::SequentiallyConsistent;

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ class CGHLSLRuntime {
9090
GENERATE_HLSL_INTRINSIC_FUNCTION_DEFAULT(Any, any)
9191
GENERATE_HLSL_INTRINSIC_FUNCTION_DEFAULT(Cross, cross)
9292
GENERATE_HLSL_INTRINSIC_FUNCTION_DEFAULT(Degrees, degrees)
93-
GENERATE_HLSL_INTRINSIC_FUNCTION(Distance, distance, /*IncludeDXIL*/ 0,
94-
/*IncludeSPIRV*/ 1)
9593
GENERATE_HLSL_INTRINSIC_FUNCTION_DEFAULT(Frac, frac)
96-
GENERATE_HLSL_INTRINSIC_FUNCTION(Length, length, /*IncludeDXIL*/ 0,
97-
/*IncludeSPIRV*/ 1)
9894
GENERATE_HLSL_INTRINSIC_FUNCTION_DEFAULT(Lerp, lerp)
9995
GENERATE_HLSL_INTRINSIC_FUNCTION_DEFAULT(Normalize, normalize)
10096
GENERATE_HLSL_INTRINSIC_FUNCTION_DEFAULT(Rsqrt, rsqrt)

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4717,6 +4717,7 @@ class CodeGenFunction : public CodeGenTypeCache {
47174717
llvm::Value *EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
47184718
llvm::Value *EmitHLSLBuiltinExpr(unsigned BuiltinID, const CallExpr *E,
47194719
ReturnValueSlot ReturnValue);
4720+
llvm::Value *EmitSPIRVBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
47204721
llvm::Value *EmitScalarOrConstFoldImmArg(unsigned ICEArguments, unsigned Idx,
47214722
const CallExpr *E);
47224723
llvm::Value *EmitSystemZBuiltinExpr(unsigned BuiltinID, const CallExpr *E);

clang/lib/Headers/hlsl/hlsl_detail.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ length_impl(T X) {
5050
template <typename T, int N>
5151
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
5252
length_vec_impl(vector<T, N> X) {
53-
#if (__has_builtin(__builtin_hlsl_length))
54-
return __builtin_hlsl_length(X);
55-
#endif
53+
#if (__has_builtin(__builtin_spirv_length))
54+
return __builtin_spirv_length(X);
55+
#else
5656
vector<T, N> XSquared = X * X;
5757
T XSquaredSum = __builtin_hlsl_reduce_add(XSquared);
5858
return __builtin_elementwise_sqrt(XSquaredSum);
59+
#endif
5960
}
6061

6162
template <typename T>
@@ -67,10 +68,11 @@ distance_impl(T X, T Y) {
6768
template <typename T, int N>
6869
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
6970
distance_vec_impl(vector<T, N> X, vector<T, N> Y) {
70-
#if (__has_builtin(__builtin_hlsl_distance))
71-
return __builtin_hlsl_distance(X, Y);
72-
#endif
71+
#if (__has_builtin(__builtin_spirv_distance))
72+
return __builtin_spirv_distance(X, Y);
73+
#else
7374
return length_vec_impl(X - Y);
75+
#endif
7476
}
7577

7678
} // namespace __detail

clang/lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ add_clang_library(clangSema
7979
SemaStmt.cpp
8080
SemaStmtAsm.cpp
8181
SemaStmtAttr.cpp
82+
SemaSPIRV.cpp
8283
SemaSYCL.cpp
8384
SemaSwift.cpp
8485
SemaSystemZ.cpp

clang/lib/Sema/Sema.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "clang/Sema/SemaPPC.h"
6262
#include "clang/Sema/SemaPseudoObject.h"
6363
#include "clang/Sema/SemaRISCV.h"
64+
#include "clang/Sema/SemaSPIRV.h"
6465
#include "clang/Sema/SemaSYCL.h"
6566
#include "clang/Sema/SemaSwift.h"
6667
#include "clang/Sema/SemaSystemZ.h"
@@ -239,6 +240,7 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
239240
PPCPtr(std::make_unique<SemaPPC>(*this)),
240241
PseudoObjectPtr(std::make_unique<SemaPseudoObject>(*this)),
241242
RISCVPtr(std::make_unique<SemaRISCV>(*this)),
243+
SPIRVPtr(std::make_unique<SemaSPIRV>(*this)),
242244
SYCLPtr(std::make_unique<SemaSYCL>(*this)),
243245
SwiftPtr(std::make_unique<SemaSwift>(*this)),
244246
SystemZPtr(std::make_unique<SemaSystemZ>(*this)),

0 commit comments

Comments
 (0)