https://github.com/hekota updated https://github.com/llvm/llvm-project/pull/104856
>From 44e814b925a1ad8ac40fe6904542cbade516c065 Mon Sep 17 00:00:00 2001 From: Helena Kotas <heko...@microsoft.com> Date: Mon, 19 Aug 2024 13:34:13 -0700 Subject: [PATCH 1/3] [DirectX] Add DirectXTargetCodeGenInfo Adds TargetCodeGenInfo class for DirectX. Currently in only translates `target("dx.TypedBuffer", i32, 1, 0, 1)` for now (`RWBuffer<int>`). More work us needed to determine the actual target exp type and its parameters based on attributes on the handle type (not yet implemented). Part 1/2 of #95952 --- clang/lib/CodeGen/CMakeLists.txt | 1 + clang/lib/CodeGen/CodeGenModule.cpp | 2 + clang/lib/CodeGen/TargetInfo.h | 3 ++ clang/lib/CodeGen/Targets/DirectX.cpp | 60 +++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 clang/lib/CodeGen/Targets/DirectX.cpp diff --git a/clang/lib/CodeGen/CMakeLists.txt b/clang/lib/CodeGen/CMakeLists.txt index deb7b27266d736..aa0c871c5352a8 100644 --- a/clang/lib/CodeGen/CMakeLists.txt +++ b/clang/lib/CodeGen/CMakeLists.txt @@ -122,6 +122,7 @@ add_clang_library(clangCodeGen Targets/AVR.cpp Targets/BPF.cpp Targets/CSKY.cpp + Targets/DirectX.cpp Targets/Hexagon.cpp Targets/Lanai.cpp Targets/LoongArch.cpp diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 0b61ef0f89989c..f93e79d1dffecd 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -298,6 +298,8 @@ createTargetCodeGenInfo(CodeGenModule &CGM) { case llvm::Triple::spirv32: case llvm::Triple::spirv64: return createSPIRVTargetCodeGenInfo(CGM); + case llvm::Triple::dxil: + return createDirectXTargetCodeGenInfo(CGM); case llvm::Triple::ve: return createVETargetCodeGenInfo(CGM); case llvm::Triple::csky: { diff --git a/clang/lib/CodeGen/TargetInfo.h b/clang/lib/CodeGen/TargetInfo.h index 0244ca006d498b..3e503538b2b14d 100644 --- a/clang/lib/CodeGen/TargetInfo.h +++ b/clang/lib/CodeGen/TargetInfo.h @@ -555,6 +555,9 @@ createTCETargetCodeGenInfo(CodeGenModule &CGM); std::unique_ptr<TargetCodeGenInfo> createVETargetCodeGenInfo(CodeGenModule &CGM); +std::unique_ptr<TargetCodeGenInfo> +createDirectXTargetCodeGenInfo(CodeGenModule &CGM); + enum class WebAssemblyABIKind { MVP = 0, ExperimentalMV = 1, diff --git a/clang/lib/CodeGen/Targets/DirectX.cpp b/clang/lib/CodeGen/Targets/DirectX.cpp new file mode 100644 index 00000000000000..847ca7ddce1810 --- /dev/null +++ b/clang/lib/CodeGen/Targets/DirectX.cpp @@ -0,0 +1,60 @@ +//===- DirectX.cpp +//-----------------------------------------------------------===// +// +// 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 "ABIInfoImpl.h" +#include "TargetInfo.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/Support/ErrorHandling.h" + +using namespace clang; +using namespace clang::CodeGen; + +//===----------------------------------------------------------------------===// +// Target codegen info implementation for DirectX. +//===----------------------------------------------------------------------===// + +namespace { + +class DirectXTargetCodeGenInfo : public TargetCodeGenInfo { +public: + DirectXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) + : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {} + + llvm::Type *getHLSLType(CodeGenModule &CGM, const Type *T) const override; +}; + +llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(CodeGenModule &CGM, + const Type *Ty) const { + llvm::LLVMContext &Ctx = CGM.getLLVMContext(); + if (auto *BuiltinTy = dyn_cast<BuiltinType>(Ty)) { + switch (BuiltinTy->getKind()) { + case BuiltinType::HLSLResource: { + // FIXME: translate __hlsl_resource_t to target("dx.TypedBuffer", i32, 1, + // 0, 1) only for now (RWBuffer<int>); more work us needed to determine + // the target ext type and its parameters based on the handle type + // attributes (not yet implemented) + llvm::IntegerType *ElemType = llvm::IntegerType::getInt32Ty(Ctx); + ArrayRef<unsigned> Flags = {/*IsWriteable*/ 1, /*IsROV*/ 0, + /*IsSigned*/ 1}; + return llvm::TargetExtType::get(Ctx, "dx.TypedBuffer", {ElemType}, Flags); + } + default: + llvm_unreachable("unhandled builtin type"); + } + } + return nullptr; +} + +} // namespace + +std::unique_ptr<TargetCodeGenInfo> +CodeGen::createDirectXTargetCodeGenInfo(CodeGenModule &CGM) { + return std::make_unique<DirectXTargetCodeGenInfo>(CGM.getTypes()); +} >From efbf44be7bf6f55deb1d0cfd002b6219fda679bf Mon Sep 17 00:00:00 2001 From: Helena Kotas <heko...@microsoft.com> Date: Mon, 19 Aug 2024 22:29:37 -0700 Subject: [PATCH 2/3] code review feedback (no test yet) --- clang/lib/CodeGen/Targets/DirectX.cpp | 32 +++++++++++---------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/clang/lib/CodeGen/Targets/DirectX.cpp b/clang/lib/CodeGen/Targets/DirectX.cpp index 847ca7ddce1810..a915328cd68c25 100644 --- a/clang/lib/CodeGen/Targets/DirectX.cpp +++ b/clang/lib/CodeGen/Targets/DirectX.cpp @@ -9,9 +9,7 @@ #include "ABIInfoImpl.h" #include "TargetInfo.h" -#include "llvm/ADT/ArrayRef.h" #include "llvm/IR/DerivedTypes.h" -#include "llvm/Support/ErrorHandling.h" using namespace clang; using namespace clang::CodeGen; @@ -32,24 +30,20 @@ class DirectXTargetCodeGenInfo : public TargetCodeGenInfo { llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(CodeGenModule &CGM, const Type *Ty) const { + auto *BuiltinTy = dyn_cast<BuiltinType>(Ty); + if (!BuiltinTy || BuiltinTy->getKind() != BuiltinType::HLSLResource) + return nullptr; + llvm::LLVMContext &Ctx = CGM.getLLVMContext(); - if (auto *BuiltinTy = dyn_cast<BuiltinType>(Ty)) { - switch (BuiltinTy->getKind()) { - case BuiltinType::HLSLResource: { - // FIXME: translate __hlsl_resource_t to target("dx.TypedBuffer", i32, 1, - // 0, 1) only for now (RWBuffer<int>); more work us needed to determine - // the target ext type and its parameters based on the handle type - // attributes (not yet implemented) - llvm::IntegerType *ElemType = llvm::IntegerType::getInt32Ty(Ctx); - ArrayRef<unsigned> Flags = {/*IsWriteable*/ 1, /*IsROV*/ 0, - /*IsSigned*/ 1}; - return llvm::TargetExtType::get(Ctx, "dx.TypedBuffer", {ElemType}, Flags); - } - default: - llvm_unreachable("unhandled builtin type"); - } - } - return nullptr; + // FIXME: translate __hlsl_resource_t to target("dx.TypedBuffer", <4 x float>, + // 1, 0, 1) only for now (RWBuffer<float4>); more work us needed to determine + // the target ext type and its parameters based on the handle type + // attributes (not yet implemented) + llvm::FixedVectorType *ElemType = + llvm::FixedVectorType::get(llvm::Type::getFloatTy(Ctx), 4); + ArrayRef<unsigned> Flags = {/*IsWriteable*/ 1, /*IsROV*/ 0, + /*IsSigned*/ 1}; + return llvm::TargetExtType::get(Ctx, "dx.TypedBuffer", {ElemType}, Flags); } } // namespace >From 1ec8b0a44ee3c8c39bb3941fe2bd118bb656f6b2 Mon Sep 17 00:00:00 2001 From: Helena Kotas <heko...@microsoft.com> Date: Wed, 21 Aug 2024 15:44:09 -0700 Subject: [PATCH 3/3] Fix header comment formatting --- clang/lib/CodeGen/Targets/DirectX.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/Targets/DirectX.cpp b/clang/lib/CodeGen/Targets/DirectX.cpp index a915328cd68c25..ca3a236f9b5f3e 100644 --- a/clang/lib/CodeGen/Targets/DirectX.cpp +++ b/clang/lib/CodeGen/Targets/DirectX.cpp @@ -1,5 +1,4 @@ -//===- DirectX.cpp -//-----------------------------------------------------------===// +//===- DirectX.cpp---------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits