Skip to content

[clang][SPIRV] Add builtin for OpGenericCastToPtrExplicit and its SPIR-V friendly binding #137805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions clang/include/clang/Basic/BuiltinsSPIRV.td

This file was deleted.

15 changes: 15 additions & 0 deletions clang/include/clang/Basic/BuiltinsSPIRVBase.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//===--- BuiltinsSPIRVBase.td - SPIRV Builtin function database -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

include "clang/Basic/BuiltinsBase.td"

class SPIRVBuiltin<string prototype, list<Attribute> Attr> : Builtin {
let Spellings = ["__builtin_spirv_"#NAME];
let Prototype = prototype;
let Attributes = !listconcat([NoThrow], Attr);
}
12 changes: 12 additions & 0 deletions clang/include/clang/Basic/BuiltinsSPIRVCL.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===--- BuiltinsSPIRVCL.td - SPIRV Builtin function database ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

include "clang/Basic/BuiltinsSPIRVBase.td"

def generic_cast_to_ptr_explicit
: SPIRVBuiltin<"void*(void*, int)", [NoThrow, Const, CustomTypeChecking]>;
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/BuiltinsSPIRVCommon.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===- BuiltinsSPIRVCommon.td - SPIRV Builtin function database -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

include "clang/Basic/BuiltinsSPIRVBase.td"

def distance : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
def length : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
def smoothstep : SPIRVBuiltin<"void(...)", [NoThrow, Const, CustomTypeChecking]>;
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/BuiltinsSPIRVVK.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===--- BuiltinsSPIRVVK.td - SPIRV Builtin function database ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

include "clang/Basic/BuiltinsSPIRVBase.td"


def reflect : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
def faceforward : SPIRVBuiltin<"void(...)", [NoThrow, Const, CustomTypeChecking]>;
14 changes: 11 additions & 3 deletions clang/include/clang/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,17 @@ clang_tablegen(BuiltinsRISCV.inc -gen-clang-builtins
SOURCE BuiltinsRISCV.td
TARGET ClangBuiltinsRISCV)

clang_tablegen(BuiltinsSPIRV.inc -gen-clang-builtins
SOURCE BuiltinsSPIRV.td
TARGET ClangBuiltinsSPIRV)
clang_tablegen(BuiltinsSPIRVCommon.inc -gen-clang-builtins
SOURCE BuiltinsSPIRVCommon.td
TARGET ClangBuiltinsSPIRVCommon)

clang_tablegen(BuiltinsSPIRVVK.inc -gen-clang-builtins
SOURCE BuiltinsSPIRVVK.td
TARGET ClangBuiltinsSPIRVVK)

clang_tablegen(BuiltinsSPIRVCL.inc -gen-clang-builtins
SOURCE BuiltinsSPIRVCL.td
TARGET ClangBuiltinsSPIRVCL)

clang_tablegen(BuiltinsX86.inc -gen-clang-builtins
SOURCE BuiltinsX86.td
Expand Down
12 changes: 11 additions & 1 deletion clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -4609,7 +4609,7 @@ def err_attribute_preferred_name_arg_invalid : Error<
"argument %0 to 'preferred_name' attribute is not a typedef for "
"a specialization of %1">;
def err_attribute_builtin_alias : Error<
"%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">;
"%0 attribute can only be applied to a ARM, HLSL, SPIR-V or RISC-V builtin">;

// called-once attribute diagnostics.
def err_called_once_attribute_wrong_type : Error<
Expand Down Expand Up @@ -12740,6 +12740,16 @@ def err_bit_int_bad_size : Error<"%select{signed|unsigned}0 _BitInt must "
def err_bit_int_max_size : Error<"%select{signed|unsigned}0 _BitInt of bit "
"sizes greater than %1 not supported">;

// SPIR-V builtins diagnostics
def err_spirv_invalid_target : Error<
"builtin requires %select{spirv|spirv32 or spirv64}0 target">;
def err_spirv_builtin_generic_cast_invalid_arg : Error<
"expecting a pointer argument to the generic address space">;
def err_spirv_enum_not_int : Error<
"%0{storage class} argument for SPIR-V builtin is not a 32-bits integer">;
def err_spirv_enum_not_valid : Error<
"invalid value for %select{storage class}0 argument">;

// errors of expect.with.probability
def err_probability_not_constant_float : Error<
"probability argument to __builtin_expect_with_probability must be constant "
Expand Down
12 changes: 11 additions & 1 deletion clang/include/clang/Basic/TargetBuiltins.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,17 @@ namespace clang {
enum {
LastTIBuiltin = clang::Builtin::FirstTSBuiltin - 1,
#define GET_BUILTIN_ENUMERATORS
#include "clang/Basic/BuiltinsSPIRV.inc"
#include "clang/Basic/BuiltinsSPIRVCommon.inc"
#undef GET_BUILTIN_ENUMERATORS
FirstVKBuiltin,
LastCoreBuiltin = FirstVKBuiltin - 1,
#define GET_BUILTIN_ENUMERATORS
#include "clang/Basic/BuiltinsSPIRVVK.inc"
#undef GET_BUILTIN_ENUMERATORS
FirstCLBuiltin,
LastVKBuiltin = FirstCLBuiltin - 1,
#define GET_BUILTIN_ENUMERATORS
#include "clang/Basic/BuiltinsSPIRVCL.inc"
#undef GET_BUILTIN_ENUMERATORS
LastTSBuiltin
};
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Sema/SemaSPIRV.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class SemaSPIRV : public SemaBase {
public:
SemaSPIRV(Sema &S);

bool CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
bool CheckSPIRVBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
CallExpr *TheCall);
};
} // namespace clang

Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10054,6 +10054,11 @@ bool ASTContext::canBuiltinBeRedeclared(const FunctionDecl *FD) const {
if (LangOpts.HLSL && FD->getBuiltinID() != Builtin::NotBuiltin &&
BuiltinInfo.hasCustomTypechecking(FD->getBuiltinID()))
return true;
// Allow redecl custom type checking builtin for SPIR-V.
if (getTargetInfo().getTriple().isSPIROrSPIRV() &&
BuiltinInfo.isTSBuiltin(FD->getBuiltinID()) &&
BuiltinInfo.hasCustomTypechecking(FD->getBuiltinID()))
return true;
return BuiltinInfo.canBeRedeclared(FD->getBuiltinID());
}

Expand Down
39 changes: 34 additions & 5 deletions clang/lib/Basic/Targets/SPIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,48 @@ static constexpr int NumBuiltins =
clang::SPIRV::LastTSBuiltin - Builtin::FirstTSBuiltin;

#define GET_BUILTIN_STR_TABLE
#include "clang/Basic/BuiltinsSPIRV.inc"
#include "clang/Basic/BuiltinsSPIRVCommon.inc"
#undef GET_BUILTIN_STR_TABLE

static constexpr Builtin::Info BuiltinInfos[] = {
#define GET_BUILTIN_INFOS
#include "clang/Basic/BuiltinsSPIRV.inc"
#include "clang/Basic/BuiltinsSPIRVCommon.inc"
#undef GET_BUILTIN_INFOS
};
static_assert(std::size(BuiltinInfos) == NumBuiltins);

namespace CL {
#define GET_BUILTIN_STR_TABLE
#include "clang/Basic/BuiltinsSPIRVCL.inc"
#undef GET_BUILTIN_STR_TABLE

static constexpr Builtin::Info BuiltinInfos[] = {
#define GET_BUILTIN_INFOS
#include "clang/Basic/BuiltinsSPIRVCL.inc"
#undef GET_BUILTIN_INFOS
};
} // namespace CL

namespace VK {
#define GET_BUILTIN_STR_TABLE
#include "clang/Basic/BuiltinsSPIRVVK.inc"
#undef GET_BUILTIN_STR_TABLE

static constexpr Builtin::Info BuiltinInfos[] = {
#define GET_BUILTIN_INFOS
#include "clang/Basic/BuiltinsSPIRVVK.inc"
#undef GET_BUILTIN_INFOS
};
} // namespace VK

static_assert(std::size(BuiltinInfos) + std::size(CL::BuiltinInfos) +
std::size(VK::BuiltinInfos) ==
NumBuiltins);

llvm::SmallVector<Builtin::InfosShard>
SPIRVTargetInfo::getTargetBuiltins() const {
return {{&BuiltinStrings, BuiltinInfos}};
BaseSPIRVTargetInfo::getTargetBuiltins() const {
return {{&BuiltinStrings, BuiltinInfos},
{&VK::BuiltinStrings, VK::BuiltinInfos},
{&CL::BuiltinStrings, CL::BuiltinInfos}};
}

void SPIRTargetInfo::getTargetDefines(const LangOptions &Opts,
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Basic/Targets/SPIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRVTargetInfo : public BaseSPIRTargetInfo {
assert(Triple.isSPIRV() && "Invalid architecture for SPIR-V.");
}

llvm::SmallVector<Builtin::InfosShard> getTargetBuiltins() const override;

bool hasFeature(StringRef Feature) const override {
return Feature == "spirv";
}
Expand Down Expand Up @@ -321,8 +323,6 @@ class LLVM_LIBRARY_VISIBILITY SPIRVTargetInfo : public BaseSPIRVTargetInfo {
"v256:256-v512:512-v1024:1024-n8:16:32:64-G10");
}

llvm::SmallVector<Builtin::InfosShard> getTargetBuiltins() const override;

void getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const override;
};
Expand Down
9 changes: 5 additions & 4 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ static Value *EmitTargetArchBuiltinExpr(CodeGenFunction *CGF,
case llvm::Triple::riscv32:
case llvm::Triple::riscv64:
return CGF->EmitRISCVBuiltinExpr(BuiltinID, E, ReturnValue);
case llvm::Triple::spirv32:
case llvm::Triple::spirv64:
if (CGF->getTarget().getTriple().getOS() == llvm::Triple::OSType::AMDHSA)
return CGF->EmitAMDGPUBuiltinExpr(BuiltinID, E);
[[fallthrough]];
case llvm::Triple::spirv:
return CGF->EmitSPIRVBuiltinExpr(BuiltinID, E);
case llvm::Triple::spirv64:
if (CGF->getTarget().getTriple().getOS() != llvm::Triple::OSType::AMDHSA)
return nullptr;
return CGF->EmitAMDGPUBuiltinExpr(BuiltinID, E);
default:
return nullptr;
}
Expand Down
14 changes: 14 additions & 0 deletions clang/lib/CodeGen/TargetBuiltins/SPIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ Value *CodeGenFunction::EmitSPIRVBuiltinExpr(unsigned BuiltinID,
/*ReturnType=*/N->getType(), Intrinsic::spv_faceforward,
ArrayRef<Value *>{N, I, Ng}, /*FMFSource=*/nullptr, "spv.faceforward");
}
case SPIRV::BI__builtin_spirv_generic_cast_to_ptr_explicit: {
Value *Ptr = EmitScalarExpr(E->getArg(0));
assert(E->getArg(0)->getType()->hasPointerRepresentation() &&
E->getArg(1)->getType()->hasIntegerRepresentation() &&
"GenericCastToPtrExplicit takes a pointer and an int");
llvm::Type *Res = getTypes().ConvertType(E->getType());
assert(Res->isPointerTy() &&
"GenericCastToPtrExplicit doesn't return a pointer");
llvm::CallInst *Call = Builder.CreateIntrinsic(
/*ReturnType=*/Res, Intrinsic::spv_generic_cast_to_ptr_explicit,
ArrayRef<Value *>{Ptr}, nullptr, "spv.generic_cast");
Call->addRetAttr(llvm::Attribute::AttrKind::NoUndef);
return Call;
}
}
return nullptr;
}
16 changes: 16 additions & 0 deletions clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ set(riscv_files
sifive_vector.h
)

set(spirv_files
__clang_spirv_builtins.h
)

set(systemz_files
s390intrin.h
vecintrin.h
Expand Down Expand Up @@ -316,6 +320,7 @@ set(files
${ppc_files}
${ppc_htm_files}
${riscv_files}
${spirv_files}
${systemz_files}
${ve_files}
${x86_files}
Expand Down Expand Up @@ -526,6 +531,7 @@ add_dependencies("clang-resource-headers"
"ppc-resource-headers"
"ppc-htm-resource-headers"
"riscv-resource-headers"
"spirv-resource-headers"
"systemz-resource-headers"
"ve-resource-headers"
"webassembly-resource-headers"
Expand Down Expand Up @@ -559,6 +565,7 @@ add_header_target("gpu-resource-headers" "${gpu_files}")

# Other header groupings
add_header_target("hlsl-resource-headers" ${hlsl_files})
add_header_target("spirv-resource-headers" ${spirv_files})
add_header_target("opencl-resource-headers" ${opencl_files})
add_header_target("llvm-libc-resource-headers" ${llvm_libc_wrapper_files})
add_header_target("openmp-resource-headers" ${openmp_wrapper_files})
Expand Down Expand Up @@ -764,6 +771,12 @@ install(
${EXCLUDE_HLSL}
COMPONENT hlsl-resource-headers)

install(
FILES ${spirv_files}
DESTINATION ${header_install_dir}
EXCLUDE_FROM_ALL
COMPONENT spirv-resource-headers)

install(
FILES ${opencl_files}
DESTINATION ${header_install_dir}
Expand Down Expand Up @@ -833,6 +846,9 @@ if (NOT LLVM_ENABLE_IDE)
add_llvm_install_targets(install-riscv-resource-headers
DEPENDS riscv-resource-headers
COMPONENT riscv-resource-headers)
add_llvm_install_targets(install-spirv-resource-headers
DEPENDS spirv-resource-headers
COMPONENT spirv-resource-headers)
add_llvm_install_targets(install-systemz-resource-headers
DEPENDS systemz-resource-headers
COMPONENT systemz-resource-headers)
Expand Down
Loading