[clang] 911d2dc - [NFC] [HLSL] Move common metadata to LLVMFrontend

Chris Bieneman via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 14 11:45:28 PDT 2022


Author: Chris Bieneman
Date: 2022-10-14T13:40:04-05:00
New Revision: 911d2dc23035454cb85422922c19259855e33bba

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

LOG: [NFC] [HLSL] Move common metadata to LLVMFrontend

This change pulls some code from the DirectX backend into a new
LLVMFrontendHLSL library to share utility data structures between the
HLSL code generation in Clang and the backend in LLVM.

This is a small refactoring as a first start to get code into the
right structure and get the library built and dependencies correct.

Fixes #58000 (https://github.com/llvm/llvm-project/issues/58000)

Reviewed By: python3kgae

Differential Revision: https://reviews.llvm.org/D135110

Added: 
    llvm/include/llvm/Frontend/HLSL/HLSLResource.h
    llvm/lib/Frontend/HLSL/CMakeLists.txt
    llvm/lib/Frontend/HLSL/HLSLResource.cpp

Modified: 
    clang/lib/CodeGen/CGHLSLRuntime.cpp
    clang/lib/CodeGen/CMakeLists.txt
    llvm/lib/Frontend/CMakeLists.txt
    llvm/lib/Target/DirectX/CMakeLists.txt
    llvm/lib/Target/DirectX/DXILResource.cpp
    llvm/lib/Target/DirectX/DXILResource.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index e1011db758d62..2782ca8c9c5b1 100644
--- 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 @@ void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) {
   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
index 0bb5abcf60455..a97042ee2008b 100644
--- a/clang/lib/CodeGen/CMakeLists.txt
+++ b/clang/lib/CodeGen/CMakeLists.txt
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
   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
index 0000000000000..fe98f6fc4194c
--- /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
index ea66917b8936a..fa48c975a8b3e 100644
--- 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
index 0000000000000..eda6cb8e69a49
--- /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
index 0000000000000..06c2cab3417b0
--- /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<GlobalVariable>(
+      cast<ConstantAsMetadata>(Entry->getOperand(0))->getValue());
+}
+
+StringRef FrontendResource::getSourceType() {
+  return cast<MDString>(Entry->getOperand(1))->getString();
+}
+
+Constant *FrontendResource::getID() {
+  return cast<ConstantAsMetadata>(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
index 82a0b41a6c89c..034b85d2fc7b1 100644
--- a/llvm/lib/Target/DirectX/CMakeLists.txt
+++ b/llvm/lib/Target/DirectX/CMakeLists.txt
@@ -35,6 +35,7 @@ add_llvm_target(DirectXCodeGen
   Support
   DirectXInfo
   DXILBitWriter
+  FrontendHLSL
 
   ADD_TO_COMPONENT
   DirectX

diff  --git a/llvm/lib/Target/DirectX/DXILResource.cpp b/llvm/lib/Target/DirectX/DXILResource.cpp
index 8aeda1e499afc..2c81347542a05 100644
--- 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<GlobalVariable>(
-      cast<ConstantAsMetadata>(Entry->getOperand(0))->getValue());
-}
-
-StringRef FrontendResource::getSourceType() {
-  return cast<MDString>(Entry->getOperand(1))->getString();
-}
-
-Constant *FrontendResource::getID() {
-  return cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue();
-}
+using namespace llvm::hlsl;
 
 void Resources::collectUAVs(Module &M) {
   NamedMDNode *Entry = M.getNamedMetadata("hlsl.uavs");

diff  --git a/llvm/lib/Target/DirectX/DXILResource.h b/llvm/lib/Target/DirectX/DXILResource.h
index d8ea9bfbdb314..b69accd2f284d 100644
--- 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 <cstdint>
@@ -26,22 +27,6 @@ class GlobalVariable;
 
 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 @@ class ResourceBase {
   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<Metadata *> Entries) const;
 
@@ -142,7 +127,7 @@ class UAVResource : public ResourceBase {
   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;


        


More information about the cfe-commits mailing list