[clang] [DirectX] Add DirectXTargetCodeGenInfo (PR #104856)

via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 19 13:50:41 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Helena Kotas (hekota)

<details>
<summary>Changes</summary>

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

---
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
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());
+}

``````````

</details>


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


More information about the cfe-commits mailing list