diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -17,6 +17,7 @@ #include "CodeGenModule.h" #include "clang/AST/Decl.h" #include "clang/Basic/TargetOptions.h" +#include "llvm/Frontend/HLSL/HLSLResource.h" #include "llvm/IR/IntrinsicsDirectX.h" #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" @@ -227,9 +228,8 @@ auto &Ctx = CGM.getModule().getContext(); IRBuilder<> B(Ctx); QualType QT(Ty, 0); - ResourceMD->addOperand(MDNode::get( - Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, QT.getAsString()), - ConstantAsMetadata::get(B.getInt32(Counter))})); + llvm::hlsl::FrontendResource Res(GV, QT.getAsString(), Counter); + ResourceMD->addOperand(Res.getMetadata()); } void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes( diff --git a/clang/lib/CodeGen/CMakeLists.txt b/clang/lib/CodeGen/CMakeLists.txt --- a/clang/lib/CodeGen/CMakeLists.txt +++ b/clang/lib/CodeGen/CMakeLists.txt @@ -7,6 +7,7 @@ Coverage Demangle Extensions + FrontendHLSL FrontendOpenMP IPO IRReader diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLResource.h b/llvm/include/llvm/Frontend/HLSL/HLSLResource.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/Frontend/HLSL/HLSLResource.h @@ -0,0 +1,43 @@ +//===- HLSLResource.h - HLSL Resource helper objects ----------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file This file contains helper objects for working with HLSL Resources. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_FRONTEND_HLSL_HLSLRESOURCE_H +#define LLVM_FRONTEND_HLSL_HLSLRESOURCE_H + +#include "llvm/IR/Metadata.h" + +namespace llvm { +class Module; +class GlobalVariable; + +namespace hlsl { + +class FrontendResource { + MDNode *Entry; + +public: + FrontendResource(MDNode *E) : Entry(E) { + assert(Entry->getNumOperands() == 3 && "Unexpected metadata shape"); + } + + FrontendResource(GlobalVariable *GV, StringRef TypeStr, uint32_t Counter); + + GlobalVariable *getGlobalVariable(); + StringRef getSourceType(); + Constant *getID(); + + MDNode *getMetadata() { return Entry; } +}; +} // namespace hlsl +} // namespace llvm + +#endif // LLVM_FRONTEND_HLSL_HLSLRESOURCE_H diff --git a/llvm/lib/Frontend/CMakeLists.txt b/llvm/lib/Frontend/CMakeLists.txt --- a/llvm/lib/Frontend/CMakeLists.txt +++ b/llvm/lib/Frontend/CMakeLists.txt @@ -1,2 +1,3 @@ +add_subdirectory(HLSL) add_subdirectory(OpenACC) add_subdirectory(OpenMP) diff --git a/llvm/lib/Frontend/HLSL/CMakeLists.txt b/llvm/lib/Frontend/HLSL/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/llvm/lib/Frontend/HLSL/CMakeLists.txt @@ -0,0 +1,14 @@ +add_llvm_component_library(LLVMFrontendHLSL + HLSLResource.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend + ${LLVM_MAIN_INCLUDE_DIR}/llvm/Frontend/HLSL + + DEPENDS + intrinsics_gen + + LINK_COMPONENTS + Core + Support + ) diff --git a/llvm/lib/Frontend/HLSL/HLSLResource.cpp b/llvm/lib/Frontend/HLSL/HLSLResource.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/Frontend/HLSL/HLSLResource.cpp @@ -0,0 +1,41 @@ +//===- HLSLResource.cpp - HLSL Resource helper objects --------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file This file contains helper objects for working with HLSL Resources. +/// +//===----------------------------------------------------------------------===// + +#include "llvm/Frontend/HLSL/HLSLResource.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Metadata.h" +#include "llvm/IR/Module.h" + +using namespace llvm; +using namespace llvm::hlsl; + +GlobalVariable *FrontendResource::getGlobalVariable() { + return cast( + cast(Entry->getOperand(0))->getValue()); +} + +StringRef FrontendResource::getSourceType() { + return cast(Entry->getOperand(1))->getString(); +} + +Constant *FrontendResource::getID() { + return cast(Entry->getOperand(2))->getValue(); +} + +FrontendResource::FrontendResource(GlobalVariable *GV, StringRef TypeStr, + uint32_t Counter) { + auto &Ctx = GV->getContext(); + IRBuilder<> B(Ctx); + Entry = + MDNode::get(Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, TypeStr), + ConstantAsMetadata::get(B.getInt32(Counter))}); +} diff --git a/llvm/lib/Target/DirectX/CMakeLists.txt b/llvm/lib/Target/DirectX/CMakeLists.txt --- a/llvm/lib/Target/DirectX/CMakeLists.txt +++ b/llvm/lib/Target/DirectX/CMakeLists.txt @@ -35,6 +35,7 @@ Support DirectXInfo DXILBitWriter + FrontendHLSL ADD_TO_COMPONENT DirectX diff --git a/llvm/lib/Target/DirectX/DXILResource.h b/llvm/lib/Target/DirectX/DXILResource.h --- a/llvm/lib/Target/DirectX/DXILResource.h +++ b/llvm/lib/Target/DirectX/DXILResource.h @@ -16,6 +16,7 @@ #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Frontend/HLSL/HLSLResource.h" #include "llvm/IR/Metadata.h" #include "llvm/Support/Compiler.h" #include @@ -26,22 +27,6 @@ namespace dxil { -// FIXME: Ultimately this class and some of these utilities should be moved into -// a new LLVMFrontendHLSL library so that they can be reused in Clang. -// See issue https://github.com/llvm/llvm-project/issues/58000. -class FrontendResource { - MDNode *Entry; - -public: - FrontendResource(MDNode *E) : Entry(E) { - assert(Entry->getNumOperands() == 3 && "Unexpected metadata shape"); - } - - GlobalVariable *getGlobalVariable(); - StringRef getSourceType(); - Constant *getID(); -}; - class ResourceBase { protected: uint32_t ID; @@ -50,7 +35,7 @@ uint32_t Space; uint32_t LowerBound; uint32_t RangeSize; - ResourceBase(uint32_t I, FrontendResource R); + ResourceBase(uint32_t I, hlsl::FrontendResource R); void write(LLVMContext &Ctx, MutableArrayRef Entries) const; @@ -142,7 +127,7 @@ void parseSourceType(StringRef S); public: - UAVResource(uint32_t I, FrontendResource R); + UAVResource(uint32_t I, hlsl::FrontendResource R); MDNode *write() const; void print(raw_ostream &O) const; diff --git a/llvm/lib/Target/DirectX/DXILResource.cpp b/llvm/lib/Target/DirectX/DXILResource.cpp --- a/llvm/lib/Target/DirectX/DXILResource.cpp +++ b/llvm/lib/Target/DirectX/DXILResource.cpp @@ -20,19 +20,7 @@ using namespace llvm; using namespace llvm::dxil; - -GlobalVariable *FrontendResource::getGlobalVariable() { - return cast( - cast(Entry->getOperand(0))->getValue()); -} - -StringRef FrontendResource::getSourceType() { - return cast(Entry->getOperand(1))->getString(); -} - -Constant *FrontendResource::getID() { - return cast(Entry->getOperand(2))->getValue(); -} +using namespace llvm::hlsl; void Resources::collectUAVs(Module &M) { NamedMDNode *Entry = M.getNamedMetadata("hlsl.uavs");