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/7] [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/7] 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/7] 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. >From 82f40210b1894a964e755e58f6fb6d92b0a600db Mon Sep 17 00:00:00 2001 From: Helena Kotas <heko...@microsoft.com> Date: Thu, 5 Sep 2024 12:41:15 -0700 Subject: [PATCH 4/7] Add target type layout and test --- clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl | 9 +++++++++ llvm/lib/IR/Type.cpp | 4 ++++ 2 files changed, 13 insertions(+) create mode 100644 clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl diff --git a/clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl b/clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl new file mode 100644 index 00000000000000..7e39b4ce418fca --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s + +void foo(__hlsl_resource_t res); + +// CHECK: define void @"?bar@@YAXU__hlsl_resource_t@@@Z"(target("dx.TypedBuffer", <4 x float>, 1, 0, 1) %a) +// CHECK: call void @"?foo@@YAXU__hlsl_resource_t@@@Z"(target("dx.TypedBuffer", <4 x float>, 1, 0, 1) %0) +void bar(__hlsl_resource_t a) { + foo(a); +} diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp index 93891461dd663f..c1367e80987359 100644 --- a/llvm/lib/IR/Type.cpp +++ b/llvm/lib/IR/Type.cpp @@ -879,6 +879,10 @@ static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) { ScalableVectorType::get(Type::getInt8Ty(C), TotalNumElts)); } + // DirectX intangible types + if (Name.starts_with("dx.")) + return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::CanBeGlobal); + return TargetTypeInfo(Type::getVoidTy(C)); } >From e5e0e5481aa57f680c7251cdba6caf622fb66faa Mon Sep 17 00:00:00 2001 From: Helena Kotas <heko...@microsoft.com> Date: Thu, 5 Sep 2024 14:27:05 -0700 Subject: [PATCH 5/7] Replace ArrayRef<unsigned> with unsigned[] --- 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 ca3a236f9b5f3e..3b9f78ef2e4482 100644 --- a/clang/lib/CodeGen/Targets/DirectX.cpp +++ b/clang/lib/CodeGen/Targets/DirectX.cpp @@ -40,8 +40,7 @@ llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(CodeGenModule &CGM, // attributes (not yet implemented) llvm::FixedVectorType *ElemType = llvm::FixedVectorType::get(llvm::Type::getFloatTy(Ctx), 4); - ArrayRef<unsigned> Flags = {/*IsWriteable*/ 1, /*IsROV*/ 0, - /*IsSigned*/ 1}; + unsigned Flags[] = {/*IsWriteable*/ 1, /*IsROV*/ 0, /*IsSigned*/ 1}; return llvm::TargetExtType::get(Ctx, "dx.TypedBuffer", {ElemType}, Flags); } >From 4d3322b024aa99b401327043b095fc9757848dcf Mon Sep 17 00:00:00 2001 From: Helena Kotas <heko...@microsoft.com> Date: Mon, 9 Sep 2024 11:52:37 -0700 Subject: [PATCH 6/7] review feedback - remove CanBeGlobal, float IsSigned flag is 0, update test to catch param name --- clang/lib/CodeGen/Targets/DirectX.cpp | 2 +- clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl | 6 +++--- llvm/lib/IR/Type.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/clang/lib/CodeGen/Targets/DirectX.cpp b/clang/lib/CodeGen/Targets/DirectX.cpp index 3b9f78ef2e4482..73510f8cc47e65 100644 --- a/clang/lib/CodeGen/Targets/DirectX.cpp +++ b/clang/lib/CodeGen/Targets/DirectX.cpp @@ -40,7 +40,7 @@ llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(CodeGenModule &CGM, // attributes (not yet implemented) llvm::FixedVectorType *ElemType = llvm::FixedVectorType::get(llvm::Type::getFloatTy(Ctx), 4); - unsigned Flags[] = {/*IsWriteable*/ 1, /*IsROV*/ 0, /*IsSigned*/ 1}; + unsigned Flags[] = {/*IsWriteable*/ 1, /*IsROV*/ 0, /*IsSigned*/ 0}; return llvm::TargetExtType::get(Ctx, "dx.TypedBuffer", {ElemType}, Flags); } diff --git a/clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl b/clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl index 7e39b4ce418fca..ce973309034781 100644 --- a/clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl +++ b/clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl @@ -1,9 +1,9 @@ -// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -O1 -o - %s | FileCheck %s void foo(__hlsl_resource_t res); -// CHECK: define void @"?bar@@YAXU__hlsl_resource_t@@@Z"(target("dx.TypedBuffer", <4 x float>, 1, 0, 1) %a) -// CHECK: call void @"?foo@@YAXU__hlsl_resource_t@@@Z"(target("dx.TypedBuffer", <4 x float>, 1, 0, 1) %0) +// CHECK: define void @"?bar@@YAXU__hlsl_resource_t@@@Z"(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %[[PARAM:[a-zA-Z0-9]+]]) +// CHECK: call void @"?foo@@YAXU__hlsl_resource_t@@@Z"(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %[[PARAM]]) void bar(__hlsl_resource_t a) { foo(a); } diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp index c1367e80987359..a4e3ebdd339cce 100644 --- a/llvm/lib/IR/Type.cpp +++ b/llvm/lib/IR/Type.cpp @@ -879,9 +879,9 @@ static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) { ScalableVectorType::get(Type::getInt8Ty(C), TotalNumElts)); } - // DirectX intangible types + // DirectX resources if (Name.starts_with("dx.")) - return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::CanBeGlobal); + return TargetTypeInfo(PointerType::get(C, 0)); return TargetTypeInfo(Type::getVoidTy(C)); } >From f0f77e3e34a1755eb847debae57f2a32fa33808c Mon Sep 17 00:00:00 2001 From: Helena Kotas <heko...@microsoft.com> Date: Mon, 9 Sep 2024 22:42:49 -0700 Subject: [PATCH 7/7] update comment --- clang/lib/CodeGen/Targets/DirectX.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/Targets/DirectX.cpp b/clang/lib/CodeGen/Targets/DirectX.cpp index 73510f8cc47e65..13da2c630629d7 100644 --- a/clang/lib/CodeGen/Targets/DirectX.cpp +++ b/clang/lib/CodeGen/Targets/DirectX.cpp @@ -35,7 +35,7 @@ llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(CodeGenModule &CGM, llvm::LLVMContext &Ctx = CGM.getLLVMContext(); // 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 + // 1, 0, 0) 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 = _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits