python3kgae created this revision. python3kgae added reviewers: anakryiko, ast, yonghong-song, beanz, pow2clk. Herald added a subscriber: Anastasia. Herald added a project: All. python3kgae requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Two directx specific clang builtins are introduced: unsigned __builtin_dx_umax_i3232(unsigned, unsigned); unsigned __builtin_dx_umin_i3232(unsigned, unsigned); They're for https://github.com/Microsoft/DirectXShaderCompiler/blob/master/docs/DXIL.rst#umax and https://github.com/Microsoft/DirectXShaderCompiler/blob/master/docs/DXIL.rst#umini. These 2 builtins are translated into llvm umax/umin intrinsic in clang codeGen. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D124813 Files: clang/include/clang/Basic/BuiltinsDirectX.def clang/include/clang/Basic/TargetBuiltins.h clang/lib/Basic/Targets/DirectX.cpp clang/lib/Basic/Targets/DirectX.h clang/lib/CodeGen/CGBuiltin.cpp clang/lib/CodeGen/CodeGenFunction.h clang/test/CodeGenHLSL/umax_umin.hlsl
Index: clang/test/CodeGenHLSL/umax_umin.hlsl =================================================================== --- /dev/null +++ clang/test/CodeGenHLSL/umax_umin.hlsl @@ -0,0 +1,13 @@ +// RUN: %clang_dxc -Tlib_6_7 -Fo - %s | FileCheck %s + +// Make sure __builtin_dx_umax/umin_i32 is lowered to llvm.umax/umin.i32. +//CHECK:call i32 @llvm.umax.i32(i32 %{{.*}}, i32 %{{.*}}) +//CHECK:call i32 @llvm.umin.i32(i32 %{{.*}}, i32 %{{.*}}) + +unsigned foo(unsigned a, unsigned b) { + return __builtin_dx_umax_i32(a, b); +} + +unsigned bar(unsigned a, unsigned b) { + return __builtin_dx_umin_i32(a, b); +} Index: clang/lib/CodeGen/CodeGenFunction.h =================================================================== --- clang/lib/CodeGen/CodeGenFunction.h +++ clang/lib/CodeGen/CodeGenFunction.h @@ -4239,6 +4239,7 @@ llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E, llvm::Triple::ArchType Arch); llvm::Value *EmitBPFBuiltinExpr(unsigned BuiltinID, const CallExpr *E); + llvm::Value *EmitDirectXBuiltinExpr(unsigned BuiltinID, const CallExpr *E); llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops); llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); Index: clang/lib/CodeGen/CGBuiltin.cpp =================================================================== --- clang/lib/CodeGen/CGBuiltin.cpp +++ clang/lib/CodeGen/CGBuiltin.cpp @@ -5524,6 +5524,8 @@ case llvm::Triple::bpfeb: case llvm::Triple::bpfel: return CGF->EmitBPFBuiltinExpr(BuiltinID, E); + case llvm::Triple::dxil: + return CGF->EmitDirectXBuiltinExpr(BuiltinID, E); case llvm::Triple::x86: case llvm::Triple::x86_64: return CGF->EmitX86BuiltinExpr(BuiltinID, E); @@ -12088,6 +12090,20 @@ } } +Value *CodeGenFunction::EmitDirectXBuiltinExpr(unsigned BuiltinID, + const CallExpr *E) { + switch (BuiltinID) { + default: + llvm_unreachable("Unexpected DirectX builtin"); + case DirectX::BI__builtin_dx_umax_i32: { + return emitBinaryBuiltin(*this, E, Intrinsic::umax); + } + case DirectX::BI__builtin_dx_umin_i32: { + return emitBinaryBuiltin(*this, E, Intrinsic::umin); + } + } +} + llvm::Value *CodeGenFunction:: BuildVector(ArrayRef<llvm::Value*> Ops) { assert((Ops.size() & (Ops.size() - 1)) == 0 && Index: clang/lib/Basic/Targets/DirectX.h =================================================================== --- clang/lib/Basic/Targets/DirectX.h +++ clang/lib/Basic/Targets/DirectX.h @@ -44,6 +44,9 @@ }; class LLVM_LIBRARY_VISIBILITY DirectXTargetInfo : public TargetInfo { + + static const Builtin::Info BuiltinInfo[]; + public: DirectXTargetInfo(const llvm::Triple &Triple, const TargetOptions &) : TargetInfo(Triple) { @@ -66,7 +69,7 @@ return Feature == "directx"; } - ArrayRef<Builtin::Info> getTargetBuiltins() const override { return None; } + ArrayRef<Builtin::Info> getTargetBuiltins() const override; const char *getClobbers() const override { return ""; } Index: clang/lib/Basic/Targets/DirectX.cpp =================================================================== --- clang/lib/Basic/Targets/DirectX.cpp +++ clang/lib/Basic/Targets/DirectX.cpp @@ -12,6 +12,8 @@ #include "DirectX.h" #include "Targets.h" +#include "clang/Basic/Builtins.h" +#include "clang/Basic/TargetBuiltins.h" using namespace clang; using namespace clang::targets; @@ -20,3 +22,16 @@ MacroBuilder &Builder) const { DefineStd(Builder, "DIRECTX", Opts); } + +const Builtin::Info DirectXTargetInfo::BuiltinInfo[] = { +#define BUILTIN(ID, TYPE, ATTRS) \ + {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr}, +#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \ + {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE}, +#include "clang/Basic/BuiltinsDirectX.def" +}; + +ArrayRef<Builtin::Info> DirectXTargetInfo::getTargetBuiltins() const { + return llvm::makeArrayRef(BuiltinInfo, clang::DirectX::LastTSBuiltin - + Builtin::FirstTSBuiltin); +} Index: clang/include/clang/Basic/TargetBuiltins.h =================================================================== --- clang/include/clang/Basic/TargetBuiltins.h +++ clang/include/clang/Basic/TargetBuiltins.h @@ -75,6 +75,16 @@ }; } + /// DirectX builtins + namespace DirectX { + enum { + LastTIBuiltin = clang::Builtin::FirstTSBuiltin - 1, +#define BUILTIN(ID, TYPE, ATTRS) BI##ID, +#include "clang/Basic/BuiltinsDirectX.def" + LastTSBuiltin + }; + } // namespace DirectX + /// PPC builtins namespace PPC { enum { Index: clang/include/clang/Basic/BuiltinsDirectX.def =================================================================== --- /dev/null +++ clang/include/clang/Basic/BuiltinsDirectX.def @@ -0,0 +1,28 @@ +//===-- BuiltinsDirectX.def - DirectX 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 +// +//===----------------------------------------------------------------------===// +// +// This file defines the DirectX-specific builtin function database. Users of +// this file must define the BUILTIN macro to make use of this information. +// +//===----------------------------------------------------------------------===// + +// The format of this database matches clang/Basic/Builtins.def. + +#if defined(BUILTIN) && !defined(TARGET_BUILTIN) +# define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) BUILTIN(ID, TYPE, ATTRS) +#endif + +#if defined(BUILTIN) && !defined(TARGET_HEADER_BUILTIN) +# define TARGET_HEADER_BUILTIN(ID, TYPE, ATTRS, HEADER, LANG, FEATURE) BUILTIN(ID, TYPE, ATTRS) +#endif + +BUILTIN(__builtin_dx_umax_i32, "UiUiUi", "ne") +BUILTIN(__builtin_dx_umin_i32, "UiUiUi", "ne") + +#undef BUILTIN +#undef TARGET_BUILTIN
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits