[clang] becb03f - [DirectX] Add DirectXTargetCodeGenInfo (#104856)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 10 12:41:12 PDT 2024


Author: Helena Kotas
Date: 2024-09-10T12:41:08-07:00
New Revision: becb03f3c624a9570fdb3f2d5dee1ed75922e70b

URL: https://github.com/llvm/llvm-project/commit/becb03f3c624a9570fdb3f2d5dee1ed75922e70b
DIFF: https://github.com/llvm/llvm-project/commit/becb03f3c624a9570fdb3f2d5dee1ed75922e70b.diff

LOG: [DirectX] Add DirectXTargetCodeGenInfo (#104856)

Adds target codegen info class for DirectX. For now it always translates
`__hlsl_resource_t` handle to `target("dx.TypedBuffer", i32, 1, 0, 1)`
(`RWBuffer<int>`). More work is needed to determine the actual target
exp type and parameters based on the resource handle attributes.

Part 1/2 of #95952

Added: 
    clang/lib/CodeGen/Targets/DirectX.cpp
    clang/test/CodeGenHLSL/builtins/hlsl_resource_t.hlsl

Modified: 
    clang/lib/CodeGen/CMakeLists.txt
    clang/lib/CodeGen/CodeGenModule.cpp
    clang/lib/CodeGen/TargetInfo.h
    llvm/lib/IR/Type.cpp

Removed: 
    


################################################################################
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 df4c13c9ad97aa..50fa48656009ff 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..13da2c630629d7
--- /dev/null
+++ b/clang/lib/CodeGen/Targets/DirectX.cpp
@@ -0,0 +1,52 @@
+//===- 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/IR/DerivedTypes.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 {
+  auto *BuiltinTy = dyn_cast<BuiltinType>(Ty);
+  if (!BuiltinTy || BuiltinTy->getKind() != BuiltinType::HLSLResource)
+    return nullptr;
+
+  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
+  // FIXME: translate __hlsl_resource_t to target("dx.TypedBuffer", <4 x float>,
+  // 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 =
+      llvm::FixedVectorType::get(llvm::Type::getFloatTy(Ctx), 4);
+  unsigned Flags[] = {/*IsWriteable*/ 1, /*IsROV*/ 0, /*IsSigned*/ 0};
+  return llvm::TargetExtType::get(Ctx, "dx.TypedBuffer", {ElemType}, Flags);
+}
+
+} // namespace
+
+std::unique_ptr<TargetCodeGenInfo>
+CodeGen::createDirectXTargetCodeGenInfo(CodeGenModule &CGM) {
+  return std::make_unique<DirectXTargetCodeGenInfo>(CGM.getTypes());
+}

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..ce973309034781
--- /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 -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, 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 93891461dd663f..a4e3ebdd339cce 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 resources
+  if (Name.starts_with("dx."))
+    return TargetTypeInfo(PointerType::get(C, 0));
+
   return TargetTypeInfo(Type::getVoidTy(C));
 }
 


        


More information about the cfe-commits mailing list