-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
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
@llvm/pr-subscribers-llvm-ir @llvm/pr-subscribers-clang Author: Helena Kotas (hekota) ChangesAdds target codegen info class for DirectX. For now it always translates Part 1/2 of #95952 Full diff: https://github.com/llvm/llvm-project/pull/104856.diff 4 Files Affected:
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());
+}
|
There was a problem hiding this 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this 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.
Test added. |
Test added. |
There was a problem hiding this 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.
…e test to catch param name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Adds target codegen info class for DirectX. For now it always translates
__hlsl_resource_t
handle totarget("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