Skip to content

[DirectX] Add DirectXTargetCodeGenInfo #104856

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

Merged
merged 9 commits into from
Sep 10, 2024
Merged

Conversation

hekota
Copy link
Member

@hekota hekota commented Aug 19, 2024

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

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 llvm#95952
@hekota hekota requested a review from bogner August 19, 2024 20:50
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. backend:DirectX labels Aug 19, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 19, 2024

@llvm/pr-subscribers-llvm-ir
@llvm/pr-subscribers-hlsl
@llvm/pr-subscribers-backend-directx
@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Helena Kotas (hekota)

Changes

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&lt;int&gt;). More work is needed to determine the actual target exp type and parameters based on the resource handle attributes.

Part 1/2 of #95952


Full diff: https://github.com/llvm/llvm-project/pull/104856.diff

4 Files Affected:

  • (modified) clang/lib/CodeGen/CMakeLists.txt (+1)
  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+2)
  • (modified) clang/lib/CodeGen/TargetInfo.h (+3)
  • (added) clang/lib/CodeGen/Targets/DirectX.cpp (+60)
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());
+}

Copy link
Contributor

@bogner bogner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to test this by defining a global or calling a function with the __hlsl_resource_t type.

Copy link
Member

@farzonl farzonl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@damyanp damyanp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have some test coverage to go along with this.

@llvmbot llvmbot added HLSL HLSL Language Support llvm:ir labels Sep 5, 2024
@hekota
Copy link
Member Author

hekota commented Sep 5, 2024

It would be good to have some test coverage to go along with this.

Test added.

@hekota
Copy link
Member Author

hekota commented Sep 5, 2024

We should be able to test this by defining a global or calling a function with the __hlsl_resource_t type.

Test added.

Copy link
Contributor

@damyanp damyanp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've a few questions here, but otherwise LGTMy non-expert eyes. Would be good to get a review from @bogner before completing.

Copy link
Contributor

@bogner bogner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@hekota hekota merged commit becb03f into llvm:main Sep 10, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category HLSL HLSL Language Support llvm:ir
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

7 participants